chore: misc blockchain tree (#2453)

This commit is contained in:
Matthias Seitz
2023-04-28 20:07:28 +02:00
committed by GitHub
parent a40f3d775c
commit e5caf7b4f7
5 changed files with 13 additions and 12 deletions

View File

@ -266,7 +266,7 @@ impl BlockIndices {
/// this is function that is going to remove N number of last canonical hashes.
///
/// NOTE: This is not safe standalone, as it will not disconnect
/// blocks that deppends on unwinded canonical chain. And should be
/// blocks that depends on unwinded canonical chain. And should be
/// used when canonical chain is reinserted inside Tree.
pub(crate) fn unwind_canonical_chain(&mut self, unwind_to: BlockNumber) {
// this will remove all blocks numbers that are going to be replaced.
@ -316,8 +316,8 @@ impl BlockIndices {
}
/// get canonical hash
pub fn canonical_hash(&self, block_number: &BlockNumber) -> Option<BlockHash> {
self.canonical_chain.get(block_number).cloned()
pub fn canonical_hash(&self, block_number: BlockNumber) -> Option<BlockHash> {
self.canonical_chain.get(&block_number).cloned()
}
/// get canonical tip

View File

@ -221,7 +221,8 @@ impl<DB: Database, C: Consensus, EF: ExecutorFactory> BlockchainTree<DB, C, EF>
}
/// Try inserting block inside the tree.
/// If blocks does not have parent [`BlockStatus::Disconnected`] would be returned
///
/// If blocks does not have parent [`BlockStatus::Disconnected`] would be returned.
pub fn try_insert_block(
&mut self,
block: SealedBlockWithSenders,
@ -274,7 +275,7 @@ impl<DB: Database, C: Consensus, EF: ExecutorFactory> BlockchainTree<DB, C, EF>
}
}
// if not found, check if the parent can be found inside canonical chain.
if Some(block.parent_hash) == self.block_indices.canonical_hash(&(block.number - 1)) {
if Some(block.parent_hash) == self.block_indices.canonical_hash(block.number - 1) {
// create new chain that points to that block
//return self.fork_canonical_chain(block.clone());
// TODO save pending block to database
@ -370,7 +371,7 @@ impl<DB: Database, C: Consensus, EF: ExecutorFactory> BlockchainTree<DB, C, EF>
}
break
}
(self.block_indices.canonical_hash(&fork.number) == Some(fork.hash)).then_some(fork)
(self.block_indices.canonical_hash(fork.number) == Some(fork.hash)).then_some(fork)
}
/// Insert a chain into the tree.
@ -440,7 +441,7 @@ impl<DB: Database, C: Consensus, EF: ExecutorFactory> BlockchainTree<DB, C, EF>
}
// check if block is part of canonical chain
if self.block_indices.canonical_hash(&block.number) == Some(block.hash()) {
if self.block_indices.canonical_hash(block.number) == Some(block.hash()) {
// block is part of canonical chain
return Ok(BlockStatus::Valid)
}
@ -595,7 +596,7 @@ impl<DB: Database, C: Consensus, EF: ExecutorFactory> BlockchainTree<DB, C, EF>
let canon_fork = new_canon_chain.fork_block();
// sanity check
if self.block_indices.canonical_hash(&canon_fork.number) != Some(canon_fork.hash) {
if self.block_indices.canonical_hash(canon_fork.number) != Some(canon_fork.hash) {
unreachable!("all chains should point to canonical chain.");
}

View File

@ -160,7 +160,7 @@ impl AppendableChain {
}
/// Validate and execute the given block, and append it to this chain.
pub fn append_block<DB, C, EF>(
pub(crate) fn append_block<DB, C, EF>(
&mut self,
block: SealedBlockWithSenders,
side_chain_block_hashes: BTreeMap<BlockNumber, BlockHash>,