From 334a2d4ba047ebaec01798637bc4ab0564912d65 Mon Sep 17 00:00:00 2001 From: Delweng Date: Wed, 31 Jul 2024 06:49:54 +0800 Subject: [PATCH] chore(blockchain-tree): rename BlockchainId to SidechainId (#9891) Signed-off-by: jsvisa --- crates/blockchain-tree/src/block_indices.rs | 26 ++++----- crates/blockchain-tree/src/blockchain_tree.rs | 53 +++++++++---------- crates/blockchain-tree/src/state.rs | 22 ++++---- 3 files changed, 50 insertions(+), 51 deletions(-) diff --git a/crates/blockchain-tree/src/block_indices.rs b/crates/blockchain-tree/src/block_indices.rs index 3b4c30eae..f805df7a4 100644 --- a/crates/blockchain-tree/src/block_indices.rs +++ b/crates/blockchain-tree/src/block_indices.rs @@ -1,6 +1,6 @@ //! Implementation of [`BlockIndices`] related to [`super::BlockchainTree`] -use super::state::BlockchainId; +use super::state::SidechainId; use crate::canonical_chain::CanonicalChain; use linked_hash_set::LinkedHashSet; use reth_execution_types::Chain; @@ -38,8 +38,8 @@ pub struct BlockIndices { /// Note: This is a bijection: at all times `blocks_to_chain` and this map contain the block /// hashes. block_number_to_block_hashes: BTreeMap>, - /// Block hashes and side chain they belong - blocks_to_chain: HashMap, + /// Block hashes to the sidechain IDs they belong to. + blocks_to_chain: HashMap, } impl BlockIndices { @@ -62,9 +62,9 @@ impl BlockIndices { &self.fork_to_child } - /// Return block to chain id + /// Return block to sidechain id #[allow(dead_code)] - pub(crate) const fn blocks_to_chain(&self) -> &HashMap { + pub(crate) const fn blocks_to_chain(&self) -> &HashMap { &self.blocks_to_chain } @@ -104,14 +104,14 @@ impl BlockIndices { &mut self, block_number: BlockNumber, block_hash: BlockHash, - chain_id: BlockchainId, + chain_id: SidechainId, ) { self.block_number_to_block_hashes.entry(block_number).or_default().insert(block_hash); self.blocks_to_chain.insert(block_hash, chain_id); } /// Insert block to chain and fork child indices of the new chain - pub(crate) fn insert_chain(&mut self, chain_id: BlockchainId, chain: &Chain) { + pub(crate) fn insert_chain(&mut self, chain_id: SidechainId, chain: &Chain) { for (number, block) in chain.blocks() { // add block -> chain_id index self.blocks_to_chain.insert(block.hash(), chain_id); @@ -123,8 +123,8 @@ impl BlockIndices { self.fork_to_child.entry(first.parent_hash).or_default().insert_if_absent(first.hash()); } - /// Get the [`BlockchainId`] the given block belongs to if it exists. - pub(crate) fn get_block_chain_id(&self, block: &BlockHash) -> Option { + /// Get the [`SidechainId`] for the given block hash if it exists. + pub(crate) fn get_side_chain_id(&self, block: &BlockHash) -> Option { self.blocks_to_chain.get(block).cloned() } @@ -134,7 +134,7 @@ impl BlockIndices { pub(crate) fn update_block_hashes( &mut self, hashes: BTreeMap, - ) -> (BTreeSet, Vec) { + ) -> (BTreeSet, Vec) { // set new canonical hashes. self.canonical_chain.replace(hashes.clone()); @@ -203,7 +203,7 @@ impl BlockIndices { /// Remove chain from indices and return dependent chains that need to be removed. /// Does the cleaning of the tree and removing blocks from the chain. - pub(crate) fn remove_chain(&mut self, chain: &Chain) -> BTreeSet { + pub(crate) fn remove_chain(&mut self, chain: &Chain) -> BTreeSet { chain .blocks() .iter() @@ -219,7 +219,7 @@ impl BlockIndices { &mut self, block_number: BlockNumber, block_hash: BlockHash, - ) -> BTreeSet { + ) -> BTreeSet { // rm number -> block if let btree_map::Entry::Occupied(mut entry) = self.block_number_to_block_hashes.entry(block_number) @@ -312,7 +312,7 @@ impl BlockIndices { &mut self, finalized_block: BlockNumber, num_of_additional_canonical_hashes_to_retain: u64, - ) -> BTreeSet { + ) -> BTreeSet { // get finalized chains. blocks between [self.last_finalized,finalized_block). // Dont remove finalized_block, as sidechain can point to it. let finalized_blocks: Vec = self diff --git a/crates/blockchain-tree/src/blockchain_tree.rs b/crates/blockchain-tree/src/blockchain_tree.rs index 2fb567463..d32834f45 100644 --- a/crates/blockchain-tree/src/blockchain_tree.rs +++ b/crates/blockchain-tree/src/blockchain_tree.rs @@ -2,7 +2,7 @@ use crate::{ metrics::{MakeCanonicalAction, MakeCanonicalDurationsRecorder, TreeMetrics}, - state::{BlockchainId, TreeState}, + state::{SidechainId, TreeState}, AppendableChain, BlockIndices, BlockchainTreeConfig, ExecutionData, TreeExternals, }; use reth_blockchain_tree_api::{ @@ -275,7 +275,7 @@ where let canonical_chain = self.state.block_indices.canonical_chain(); // if it is part of the chain - if let Some(chain_id) = self.block_indices().get_block_chain_id(&block_hash) { + if let Some(chain_id) = self.block_indices().get_side_chain_id(&block_hash) { trace!(target: "blockchain_tree", ?block_hash, "Constructing post state data based on non-canonical chain"); // get block state let Some(chain) = self.state.chains.get(&chain_id) else { @@ -331,7 +331,7 @@ where let parent = block.parent_num_hash(); // check if block parent can be found in any side chain. - if let Some(chain_id) = self.block_indices().get_block_chain_id(&parent.hash) { + if let Some(chain_id) = self.block_indices().get_side_chain_id(&parent.hash) { // found parent in side tree, try to insert there return self.try_insert_block_into_side_chain(block, chain_id, block_validation_kind) } @@ -447,12 +447,12 @@ where /// Try inserting a block into the given side chain. /// - /// WARNING: This expects a valid side chain id, see [BlockIndices::get_block_chain_id] + /// WARNING: This expects a valid side chain id, see [BlockIndices::get_side_chain_id] #[instrument(level = "trace", skip_all, target = "blockchain_tree")] fn try_insert_block_into_side_chain( &mut self, block: SealedBlockWithSenders, - chain_id: BlockchainId, + chain_id: SidechainId, block_validation_kind: BlockValidationKind, ) -> Result { let block_num_hash = block.num_hash(); @@ -525,7 +525,7 @@ where /// # Note /// /// This is not cached in order to save memory. - fn all_chain_hashes(&self, chain_id: BlockchainId) -> BTreeMap { + fn all_chain_hashes(&self, chain_id: SidechainId) -> BTreeMap { let mut chain_id = chain_id; let mut hashes = BTreeMap::new(); loop { @@ -546,7 +546,7 @@ where } let fork_block = chain.fork_block(); - if let Some(next_chain_id) = self.block_indices().get_block_chain_id(&fork_block.hash) { + if let Some(next_chain_id) = self.block_indices().get_side_chain_id(&fork_block.hash) { chain_id = next_chain_id; } else { // if there is no fork block that point to other chains, break the loop. @@ -563,14 +563,14 @@ where /// the block on /// /// Returns `None` if the chain is unknown. - fn canonical_fork(&self, chain_id: BlockchainId) -> Option { + fn canonical_fork(&self, chain_id: SidechainId) -> Option { let mut chain_id = chain_id; let mut fork; loop { // chain fork block fork = self.state.chains.get(&chain_id)?.fork_block(); // get fork block chain - if let Some(fork_chain_id) = self.block_indices().get_block_chain_id(&fork.hash) { + if let Some(fork_chain_id) = self.block_indices().get_side_chain_id(&fork.hash) { chain_id = fork_chain_id; continue } @@ -582,13 +582,13 @@ where /// Insert a chain into the tree. /// /// Inserts a chain into the tree and builds the block indices. - fn insert_chain(&mut self, chain: AppendableChain) -> Option { + fn insert_chain(&mut self, chain: AppendableChain) -> Option { self.state.insert_chain(chain) } /// Iterate over all child chains that depend on this block and return /// their ids. - fn find_all_dependent_chains(&self, block: &BlockHash) -> HashSet { + fn find_all_dependent_chains(&self, block: &BlockHash) -> HashSet { // Find all forks of given block. let mut dependent_block = self.block_indices().fork_to_child().get(block).cloned().unwrap_or_default(); @@ -596,7 +596,7 @@ where while let Some(block) = dependent_block.pop_back() { // Get chain of dependent block. - let Some(chain_id) = self.block_indices().get_block_chain_id(&block) else { + let Some(chain_id) = self.block_indices().get_side_chain_id(&block) else { debug!(target: "blockchain_tree", ?block, "Block not in tree"); return Default::default(); }; @@ -625,7 +625,7 @@ where /// plain state during the unwind. /// Returns the result of inserting the chain or None if any of the dependent chains is not /// in the tree. - fn insert_unwound_chain(&mut self, chain: AppendableChain) -> Option { + fn insert_unwound_chain(&mut self, chain: AppendableChain) -> Option { // iterate over all blocks in chain and find any fork blocks that are in tree. for (number, block) in chain.blocks() { let hash = block.hash(); @@ -731,7 +731,7 @@ where #[track_caller] fn is_block_inside_sidechain(&self, block: &BlockNumHash) -> Option { // check if block known and is already in the tree - if let Some(chain_id) = self.block_indices().get_block_chain_id(&block.hash) { + if let Some(chain_id) = self.block_indices().get_side_chain_id(&block.hash) { // find the canonical fork of this chain let Some(canonical_fork) = self.canonical_fork(chain_id) else { debug!(target: "blockchain_tree", chain_id=?chain_id, block=?block.hash, "Chain id not valid"); @@ -945,7 +945,7 @@ where /// The pending part of the chain is reinserted back into the tree with the same `chain_id`. fn remove_and_split_chain( &mut self, - chain_id: BlockchainId, + chain_id: SidechainId, split_at: ChainSplitTarget, ) -> Option { let chain = self.state.chains.remove(&chain_id)?; @@ -1060,7 +1060,7 @@ where return Ok(CanonicalOutcome::AlreadyCanonical { header, head }) } - let Some(chain_id) = self.block_indices().get_block_chain_id(&block_hash) else { + let Some(chain_id) = self.block_indices().get_side_chain_id(&block_hash) else { debug!(target: "blockchain_tree", ?block_hash, "Block hash not found in block indices"); return Err(CanonicalError::from(BlockchainTreeError::BlockHashNotFoundInChain { block_hash, @@ -1081,7 +1081,7 @@ where let mut chains_to_promote = vec![canonical]; // loop while fork blocks are found in Tree. - while let Some(chain_id) = self.block_indices().get_block_chain_id(&fork_block.hash) { + while let Some(chain_id) = self.block_indices().get_side_chain_id(&fork_block.hash) { // canonical chain is lower part of the chain. let Some(canonical) = self.remove_and_split_chain(chain_id, ChainSplitTarget::Number(fork_block.number)) @@ -1196,7 +1196,7 @@ where .unwrap_or_default() .into_iter() .for_each(|child| { - if let Some(chain_id) = self.block_indices().get_block_chain_id(&child) { + if let Some(chain_id) = self.block_indices().get_side_chain_id(&child) { if let Some(chain) = self.state.chains.get_mut(&chain_id) { chain.clear_trie_updates(); } @@ -1458,7 +1458,7 @@ mod tests { /// Number of chains chain_num: Option, /// Check block to chain index - block_to_chain: Option>, + block_to_chain: Option>, /// Check fork to child index fork_to_child: Option>>, /// Pending blocks @@ -1473,7 +1473,7 @@ mod tests { self } - fn with_block_to_chain(mut self, block_to_chain: HashMap) -> Self { + fn with_block_to_chain(mut self, block_to_chain: HashMap) -> Self { self.block_to_chain = Some(block_to_chain); self } @@ -1783,8 +1783,7 @@ mod tests { InsertPayloadOk::Inserted(BlockStatus::Valid(BlockAttachment::Canonical)) /* TODO: this is incorrect, figure out why */ ); - let block3a_chain_id = - tree.state.block_indices.get_block_chain_id(&block3a.hash()).unwrap(); + let block3a_chain_id = tree.state.block_indices.get_side_chain_id(&block3a.hash()).unwrap(); assert_eq!( tree.all_chain_hashes(block3a_chain_id), BTreeMap::from([ @@ -1825,7 +1824,7 @@ mod tests { tree.insert_block(block1.clone(), BlockValidationKind::Exhaustive).unwrap(), InsertPayloadOk::Inserted(BlockStatus::Valid(BlockAttachment::Canonical)) ); - let block1_chain_id = tree.state.block_indices.get_block_chain_id(&block1.hash()).unwrap(); + let block1_chain_id = tree.state.block_indices.get_side_chain_id(&block1.hash()).unwrap(); let block1_chain = tree.state.chains.get(&block1_chain_id).unwrap(); assert!(block1_chain.trie_updates().is_some()); @@ -1833,7 +1832,7 @@ mod tests { tree.insert_block(block2.clone(), BlockValidationKind::Exhaustive).unwrap(), InsertPayloadOk::Inserted(BlockStatus::Valid(BlockAttachment::Canonical)) ); - let block2_chain_id = tree.state.block_indices.get_block_chain_id(&block2.hash()).unwrap(); + let block2_chain_id = tree.state.block_indices.get_side_chain_id(&block2.hash()).unwrap(); let block2_chain = tree.state.chains.get(&block2_chain_id).unwrap(); assert!(block2_chain.trie_updates().is_none()); @@ -1846,7 +1845,7 @@ mod tests { tree.insert_block(block3.clone(), BlockValidationKind::Exhaustive).unwrap(), InsertPayloadOk::Inserted(BlockStatus::Valid(BlockAttachment::Canonical)) ); - let block3_chain_id = tree.state.block_indices.get_block_chain_id(&block3.hash()).unwrap(); + let block3_chain_id = tree.state.block_indices.get_side_chain_id(&block3.hash()).unwrap(); let block3_chain = tree.state.chains.get(&block3_chain_id).unwrap(); assert!(block3_chain.trie_updates().is_some()); @@ -1859,7 +1858,7 @@ mod tests { tree.insert_block(block4.clone(), BlockValidationKind::Exhaustive).unwrap(), InsertPayloadOk::Inserted(BlockStatus::Valid(BlockAttachment::Canonical)) ); - let block4_chain_id = tree.state.block_indices.get_block_chain_id(&block4.hash()).unwrap(); + let block4_chain_id = tree.state.block_indices.get_side_chain_id(&block4.hash()).unwrap(); let block4_chain = tree.state.chains.get(&block4_chain_id).unwrap(); assert!(block4_chain.trie_updates().is_some()); @@ -1868,7 +1867,7 @@ mod tests { InsertPayloadOk::Inserted(BlockStatus::Valid(BlockAttachment::Canonical)) ); - let block5_chain_id = tree.state.block_indices.get_block_chain_id(&block5.hash()).unwrap(); + let block5_chain_id = tree.state.block_indices.get_side_chain_id(&block5.hash()).unwrap(); let block5_chain = tree.state.chains.get(&block5_chain_id).unwrap(); assert!(block5_chain.trie_updates().is_none()); diff --git a/crates/blockchain-tree/src/state.rs b/crates/blockchain-tree/src/state.rs index 43e47743b..dcf91d8bb 100644 --- a/crates/blockchain-tree/src/state.rs +++ b/crates/blockchain-tree/src/state.rs @@ -10,7 +10,7 @@ pub(crate) struct TreeState { /// Keeps track of new unique identifiers for chains block_chain_id_generator: u64, /// The tracked chains and their current data. - pub(crate) chains: HashMap, + pub(crate) chains: HashMap, /// Indices to block and their connection to the canonical chain. /// /// This gets modified by the tree itself and is read from engine API/RPC to access the pending @@ -39,12 +39,12 @@ impl TreeState { } } - /// Issues a new unique identifier for a new chain. + /// Issues a new unique identifier for a new sidechain. #[inline] - fn next_id(&mut self) -> BlockchainId { + fn next_id(&mut self) -> SidechainId { let id = self.block_chain_id_generator; self.block_chain_id_generator += 1; - BlockchainId(id) + SidechainId(id) } /// Expose internal indices of the `BlockchainTree`. @@ -68,7 +68,7 @@ impl TreeState { &self, block_hash: BlockHash, ) -> Option<&SealedBlockWithSenders> { - let id = self.block_indices.get_block_chain_id(&block_hash)?; + let id = self.block_indices.get_side_chain_id(&block_hash)?; let chain = self.chains.get(&id)?; chain.block_with_senders(block_hash) } @@ -77,7 +77,7 @@ impl TreeState { /// /// Caution: This will not return blocks from the canonical chain. pub(crate) fn receipts_by_block_hash(&self, block_hash: BlockHash) -> Option> { - let id = self.block_indices.get_block_chain_id(&block_hash)?; + let id = self.block_indices.get_side_chain_id(&block_hash)?; let chain = self.chains.get(&id)?; chain.receipts_by_block_hash(block_hash) } @@ -85,7 +85,7 @@ impl TreeState { /// Insert a chain into the tree. /// /// Inserts a chain into the tree and builds the block indices. - pub(crate) fn insert_chain(&mut self, chain: AppendableChain) -> Option { + pub(crate) fn insert_chain(&mut self, chain: AppendableChain) -> Option { if chain.is_empty() { return None } @@ -113,16 +113,16 @@ impl TreeState { /// The ID of a sidechain internally in a [`BlockchainTree`][super::BlockchainTree]. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] -pub(crate) struct BlockchainId(u64); +pub(crate) struct SidechainId(u64); -impl From for u64 { - fn from(value: BlockchainId) -> Self { +impl From for u64 { + fn from(value: SidechainId) -> Self { value.0 } } #[cfg(test)] -impl From for BlockchainId { +impl From for SidechainId { fn from(value: u64) -> Self { Self(value) }