chore(tree): misc tree cleanup (#8423)

This commit is contained in:
Roman Krasiuk
2024-05-28 12:17:20 +02:00
committed by GitHub
parent 20aeb2be0d
commit 1e2a0c372e
2 changed files with 27 additions and 41 deletions

View File

@ -65,13 +65,14 @@ pub struct BlockchainTree<DB, E> {
externals: TreeExternals<DB, E>,
/// Tree configuration
config: BlockchainTreeConfig,
/// Prune modes.
prune_modes: Option<PruneModes>,
/// Broadcast channel for canon state changes notifications.
canon_state_notification_sender: CanonStateNotificationSender,
/// Metrics for the blockchain tree.
metrics: TreeMetrics,
/// Metrics for sync stages.
sync_metrics_tx: Option<MetricEventsSender>,
prune_modes: Option<PruneModes>,
/// Metrics for the blockchain tree.
metrics: TreeMetrics,
}
impl<DB, E> BlockchainTree<DB, E> {
@ -148,10 +149,10 @@ where
config.max_unconnected_blocks(),
),
config,
canon_state_notification_sender,
metrics: Default::default(),
sync_metrics_tx: None,
prune_modes,
canon_state_notification_sender,
sync_metrics_tx: None,
metrics: Default::default(),
})
}
@ -190,14 +191,14 @@ where
&self,
block: BlockNumHash,
) -> Result<Option<BlockStatus>, InsertBlockErrorKind> {
// check if block is canonical
if self.is_block_hash_canonical(&block.hash)? {
return Ok(Some(BlockStatus::Valid(BlockAttachment::Canonical)))
}
let last_finalized_block = self.block_indices().last_finalized_block();
// check db if block is finalized.
if block.number <= last_finalized_block {
// check if block is canonical
if self.is_block_hash_canonical(&block.hash)? {
return Ok(Some(BlockStatus::Valid(BlockAttachment::Canonical)))
}
// check if block is inside database
if self.externals.provider_factory.provider()?.block_number(block.hash)?.is_some() {
return Ok(Some(BlockStatus::Valid(BlockAttachment::Canonical)))
@ -209,11 +210,6 @@ where
.into())
}
// check if block is part of canonical chain
if self.is_block_hash_canonical(&block.hash)? {
return Ok(Some(BlockStatus::Valid(BlockAttachment::Canonical)))
}
// is block inside chain
if let Some(attachment) = self.is_block_inside_sidechain(&block) {
return Ok(Some(BlockStatus::Valid(attachment)))
@ -240,7 +236,7 @@ where
///
/// Caution: This will not return blocks from the canonical chain.
#[inline]
pub fn block_by_hash(&self, block_hash: BlockHash) -> Option<&SealedBlock> {
pub fn sidechain_block_by_hash(&self, block_hash: BlockHash) -> Option<&SealedBlock> {
self.state.block_by_hash(block_hash)
}
@ -262,15 +258,10 @@ where
self.state.receipts_by_block_hash(block_hash)
}
/// Returns true if the block is included in a side-chain.
fn is_block_hash_inside_sidechain(&self, block_hash: BlockHash) -> bool {
self.block_by_hash(block_hash).is_some()
}
/// Returns the block that's considered the `Pending` block, if it exists.
pub fn pending_block(&self) -> Option<&SealedBlock> {
let b = self.block_indices().pending_block_num_hash()?;
self.block_by_hash(b.hash)
self.sidechain_block_by_hash(b.hash)
}
/// Return items needed to execute on the pending state.
@ -302,16 +293,14 @@ where
// get parent hashes
let mut parent_block_hashes = self.all_chain_hashes(chain_id);
let first_pending_block_number =
if let Some(key_value) = parent_block_hashes.first_key_value() {
*key_value.0
} else {
debug!(target: "blockchain_tree", ?chain_id, "No blockhashes stored");
return None
};
let Some((first_pending_block_number, _)) = parent_block_hashes.first_key_value()
else {
debug!(target: "blockchain_tree", ?chain_id, "No block hashes stored");
return None
};
let canonical_chain = canonical_chain
.iter()
.filter(|&(key, _)| key < first_pending_block_number)
.filter(|&(key, _)| &key < first_pending_block_number)
.collect::<Vec<_>>();
parent_block_hashes.extend(canonical_chain);
@ -916,8 +905,8 @@ where
// check unconnected block buffer for children of the chains
let mut all_chain_blocks = Vec::new();
for (_, chain) in self.state.chains.iter() {
for (&number, blocks) in chain.blocks().iter() {
all_chain_blocks.push(BlockNumHash { number, hash: blocks.hash() })
for (&number, block) in chain.blocks().iter() {
all_chain_blocks.push(BlockNumHash { number, hash: block.hash() })
}
}
for block in all_chain_blocks.into_iter() {
@ -940,14 +929,11 @@ where
let include_blocks = self.state.buffered_blocks.remove_block_with_children(&new_block.hash);
// then try to reinsert them into the tree
for block in include_blocks.into_iter() {
// dont fail on error, just ignore the block.
// don't fail on error, just ignore the block.
let _ = self
.try_insert_validated_block(block, BlockValidationKind::SkipStateRootValidation)
.map_err(|err| {
debug!(
target: "blockchain_tree", %err,
"Failed to insert buffered block",
);
debug!(target: "blockchain_tree", %err, "Failed to insert buffered block");
err
});
}
@ -1004,7 +990,7 @@ where
header = provider.header_by_number(num)?;
}
if header.is_none() && self.is_block_hash_inside_sidechain(*hash) {
if header.is_none() && self.sidechain_block_by_hash(*hash).is_some() {
return Ok(None)
}

View File

@ -118,12 +118,12 @@ where
fn header_by_hash(&self, hash: BlockHash) -> Option<SealedHeader> {
trace!(target: "blockchain_tree", ?hash, "Returning header by hash");
self.tree.read().block_by_hash(hash).map(|b| b.header.clone())
self.tree.read().sidechain_block_by_hash(hash).map(|b| b.header.clone())
}
fn block_by_hash(&self, block_hash: BlockHash) -> Option<SealedBlock> {
trace!(target: "blockchain_tree", ?block_hash, "Returning block by hash");
self.tree.read().block_by_hash(block_hash).cloned()
self.tree.read().sidechain_block_by_hash(block_hash).cloned()
}
fn block_with_senders_by_hash(&self, block_hash: BlockHash) -> Option<SealedBlockWithSenders> {