From e5caf7b4f70af4c7a929c8390ec3494acaced2e3 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 28 Apr 2023 20:07:28 +0200 Subject: [PATCH] chore: misc blockchain tree (#2453) --- crates/blockchain-tree/src/block_indices.rs | 6 +++--- crates/blockchain-tree/src/blockchain_tree.rs | 11 ++++++----- crates/blockchain-tree/src/chain.rs | 2 +- crates/interfaces/src/blockchain_tree.rs | 4 ++-- crates/interfaces/src/consensus.rs | 2 +- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/crates/blockchain-tree/src/block_indices.rs b/crates/blockchain-tree/src/block_indices.rs index 8637a3092e..92c4cb129b 100644 --- a/crates/blockchain-tree/src/block_indices.rs +++ b/crates/blockchain-tree/src/block_indices.rs @@ -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 { - self.canonical_chain.get(block_number).cloned() + pub fn canonical_hash(&self, block_number: BlockNumber) -> Option { + self.canonical_chain.get(&block_number).cloned() } /// get canonical tip diff --git a/crates/blockchain-tree/src/blockchain_tree.rs b/crates/blockchain-tree/src/blockchain_tree.rs index 50184aa4d0..d3cd345fab 100644 --- a/crates/blockchain-tree/src/blockchain_tree.rs +++ b/crates/blockchain-tree/src/blockchain_tree.rs @@ -221,7 +221,8 @@ impl BlockchainTree } /// 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 BlockchainTree } } // 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 BlockchainTree } 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 BlockchainTree } // 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 BlockchainTree 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."); } diff --git a/crates/blockchain-tree/src/chain.rs b/crates/blockchain-tree/src/chain.rs index 2ba93d7f1b..67ee8e2fa7 100644 --- a/crates/blockchain-tree/src/chain.rs +++ b/crates/blockchain-tree/src/chain.rs @@ -160,7 +160,7 @@ impl AppendableChain { } /// Validate and execute the given block, and append it to this chain. - pub fn append_block( + pub(crate) fn append_block( &mut self, block: SealedBlockWithSenders, side_chain_block_hashes: BTreeMap, diff --git a/crates/interfaces/src/blockchain_tree.rs b/crates/interfaces/src/blockchain_tree.rs index 4d74cda2c6..00c9ee8ba2 100644 --- a/crates/interfaces/src/blockchain_tree.rs +++ b/crates/interfaces/src/blockchain_tree.rs @@ -54,9 +54,9 @@ pub trait BlockchainTreeEngine: BlockchainTreeViewer + Send + Sync { } /// From Engine API spec, block inclusion can be valid, accepted or invalid. -/// Invalid case is already covered by error but we needs to make distinction +/// Invalid case is already covered by error, but we need to make distinction /// between if it is valid (extends canonical chain) or just accepted (is side chain). -/// If we dont know the block parent we are returning Disconnected status +/// If we don't know the block parent we are returning Disconnected status /// as we can't make a claim if block is valid or not. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum BlockStatus { diff --git a/crates/interfaces/src/consensus.rs b/crates/interfaces/src/consensus.rs index a288ca8923..526096a513 100644 --- a/crates/interfaces/src/consensus.rs +++ b/crates/interfaces/src/consensus.rs @@ -13,7 +13,7 @@ pub use reth_rpc_types::engine::ForkchoiceState; pub trait Consensus: Debug + Send + Sync { /// Validate if header is correct and follows consensus specification. /// - /// This is called on standalone header to check if all hashe + /// This is called on standalone header to check if all hashes are correct. fn validate_header(&self, header: &SealedHeader) -> Result<(), ConsensusError>; /// Validate that the header information regarding parent are correct.