From 15fe5edf068bf0303e77c114eb59966ed5ba7f12 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 13 Oct 2023 16:56:20 +0200 Subject: [PATCH] chore: use new type for BlockchainId (#5012) --- crates/blockchain-tree/src/block_indices.rs | 2 +- crates/blockchain-tree/src/blockchain_tree.rs | 36 +++++++++++-------- crates/blockchain-tree/src/chain.rs | 3 -- crates/blockchain-tree/src/state.rs | 30 ++++++++++++++-- 4 files changed, 49 insertions(+), 22 deletions(-) diff --git a/crates/blockchain-tree/src/block_indices.rs b/crates/blockchain-tree/src/block_indices.rs index 7097f9747..24c4aca64 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::chain::BlockChainId; +use super::state::BlockChainId; use crate::canonical_chain::CanonicalChain; use linked_hash_set::LinkedHashSet; use reth_primitives::{BlockHash, BlockNumHash, BlockNumber, SealedBlockWithSenders}; diff --git a/crates/blockchain-tree/src/blockchain_tree.rs b/crates/blockchain-tree/src/blockchain_tree.rs index 9303790f1..291d316d7 100644 --- a/crates/blockchain-tree/src/blockchain_tree.rs +++ b/crates/blockchain-tree/src/blockchain_tree.rs @@ -1,9 +1,9 @@ //! Implementation of [`BlockchainTree`] use crate::{ canonical_chain::CanonicalChain, - chain::{BlockChainId, BlockKind}, + chain::BlockKind, metrics::TreeMetrics, - state::TreeState, + state::{BlockChainId, TreeState}, AppendableChain, BlockIndices, BlockchainTreeConfig, BundleStateData, TreeExternals, }; use reth_db::{cursor::DbCursorRO, database::Database, tables, transaction::DbTx}; @@ -458,7 +458,7 @@ impl BlockchainTree { let canonical_fork = match self.canonical_fork(chain_id) { None => { return Err(InsertBlockError::tree_error( - BlockchainTreeError::BlockSideChainIdConsistency { chain_id }, + BlockchainTreeError::BlockSideChainIdConsistency { chain_id: chain_id.into() }, block.block, )) } @@ -470,7 +470,7 @@ impl BlockchainTree { Some(parent_chain) => parent_chain, None => { return Err(InsertBlockError::tree_error( - BlockchainTreeError::BlockSideChainIdConsistency { chain_id }, + BlockchainTreeError::BlockSideChainIdConsistency { chain_id: chain_id.into() }, block.block, )) } @@ -1371,7 +1371,7 @@ mod tests { // | TreeTester::default() .with_chain_num(1) - .with_block_to_chain(HashMap::from([(block1.hash, 0), (block2.hash, 0)])) + .with_block_to_chain(HashMap::from([(block1.hash, 0.into()), (block2.hash, 0.into())])) .with_fork_to_child(HashMap::from([(block1.parent_hash, HashSet::from([block1.hash]))])) .with_pending_blocks((block1.number, HashSet::from([block1.hash]))) .assert(&tree); @@ -1430,7 +1430,7 @@ mod tests { TreeTester::default() .with_chain_num(1) - .with_block_to_chain(HashMap::from([(block1a_hash, 1)])) + .with_block_to_chain(HashMap::from([(block1a_hash, 1.into())])) .with_fork_to_child(HashMap::from([( block1.parent_hash, HashSet::from([block1a_hash]), @@ -1453,7 +1453,10 @@ mod tests { // | TreeTester::default() .with_chain_num(2) - .with_block_to_chain(HashMap::from([(block1a_hash, 1), (block2a_hash, 2)])) + .with_block_to_chain(HashMap::from([ + (block1a_hash, 1.into()), + (block2a_hash, 2.into()), + ])) .with_fork_to_child(HashMap::from([ (block1.parent_hash, HashSet::from([block1a_hash])), (block1.hash(), HashSet::from([block2a_hash])), @@ -1480,7 +1483,7 @@ mod tests { // | TreeTester::default() .with_chain_num(2) - .with_block_to_chain(HashMap::from([(block1a_hash, 1), (block2.hash, 3)])) + .with_block_to_chain(HashMap::from([(block1a_hash, 1.into()), (block2.hash, 3.into())])) .with_fork_to_child(HashMap::from([ (block1.parent_hash, HashSet::from([block1a_hash])), (block1.hash(), HashSet::from([block2.hash])), @@ -1501,9 +1504,9 @@ mod tests { TreeTester::default() .with_chain_num(2) .with_block_to_chain(HashMap::from([ - (block1.hash, 4), - (block2a_hash, 4), - (block2.hash, 3), + (block1.hash, 4.into()), + (block2a_hash, 4.into()), + (block2.hash, 3.into()), ])) .with_fork_to_child(HashMap::from([ (block1.parent_hash, HashSet::from([block1.hash])), @@ -1538,7 +1541,10 @@ mod tests { // | TreeTester::default() .with_chain_num(2) - .with_block_to_chain(HashMap::from([(block1a_hash, 5), (block2a_hash, 4)])) + .with_block_to_chain(HashMap::from([ + (block1a_hash, 5.into()), + (block2a_hash, 4.into()), + ])) .with_fork_to_child(HashMap::from([ (block1.parent_hash, HashSet::from([block1a_hash])), (block1.hash(), HashSet::from([block2a_hash])), @@ -1567,7 +1573,7 @@ mod tests { // | TreeTester::default() .with_chain_num(1) - .with_block_to_chain(HashMap::from([(block2a_hash, 4)])) + .with_block_to_chain(HashMap::from([(block2a_hash, 4.into())])) .with_fork_to_child(HashMap::from([(block1.hash(), HashSet::from([block2a_hash]))])) .with_pending_blocks((block2.number + 1, HashSet::from([]))) .assert(&tree); @@ -1586,7 +1592,7 @@ mod tests { // | TreeTester::default() .with_chain_num(2) - .with_block_to_chain(HashMap::from([(block2a_hash, 4), (block2.hash, 6)])) + .with_block_to_chain(HashMap::from([(block2a_hash, 4.into()), (block2.hash, 6.into())])) .with_fork_to_child(HashMap::from([( block1.hash(), HashSet::from([block2a_hash, block2.hash]), @@ -1607,7 +1613,7 @@ mod tests { // | TreeTester::default() .with_chain_num(1) - .with_block_to_chain(HashMap::from([(block2a_hash, 4)])) + .with_block_to_chain(HashMap::from([(block2a_hash, 4.into())])) .with_fork_to_child(HashMap::from([(block1.hash(), HashSet::from([block2a_hash]))])) .with_pending_blocks((block2.number + 1, HashSet::new())) .assert(&tree); diff --git a/crates/blockchain-tree/src/chain.rs b/crates/blockchain-tree/src/chain.rs index fc50847ae..12460f3f7 100644 --- a/crates/blockchain-tree/src/chain.rs +++ b/crates/blockchain-tree/src/chain.rs @@ -22,9 +22,6 @@ use std::{ ops::{Deref, DerefMut}, }; -/// The ID of a sidechain internally in a [`BlockchainTree`][super::BlockchainTree]. -pub(crate) type BlockChainId = u64; - /// A chain if the blockchain tree, that has functionality to execute blocks and append them to the /// it self. #[derive(Clone, Debug, Default, PartialEq, Eq)] diff --git a/crates/blockchain-tree/src/state.rs b/crates/blockchain-tree/src/state.rs index b7659490a..3fcbcf2e8 100644 --- a/crates/blockchain-tree/src/state.rs +++ b/crates/blockchain-tree/src/state.rs @@ -1,6 +1,6 @@ //! Blockchain tree state. -use crate::{chain::BlockChainId, AppendableChain, BlockBuffer, BlockIndices}; +use crate::{AppendableChain, BlockBuffer, BlockIndices}; use reth_primitives::{BlockHash, BlockNumber, Receipt, SealedBlock, SealedBlockWithSenders}; use std::collections::{BTreeMap, HashMap}; @@ -39,6 +39,14 @@ impl TreeState { } } + /// Issues a new unique identifier for a new chain. + #[inline] + fn next_id(&mut self) -> BlockChainId { + let id = self.block_chain_id_generator; + self.block_chain_id_generator += 1; + BlockChainId(id) + } + /// Expose internal indices of the BlockchainTree. #[inline] pub(crate) fn block_indices(&self) -> &BlockIndices { @@ -70,8 +78,7 @@ impl TreeState { if chain.is_empty() { return None } - let chain_id = self.block_chain_id_generator; - self.block_chain_id_generator += 1; + let chain_id = self.next_id(); self.block_indices.insert_chain(chain_id, &chain); // add chain_id -> chain index @@ -92,3 +99,20 @@ impl TreeState { self.buffered_blocks.lowest_ancestor(hash) } } + +/// The ID of a sidechain internally in a [`BlockchainTree`][super::BlockchainTree]. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] +pub struct BlockChainId(u64); + +impl From for u64 { + fn from(value: BlockChainId) -> Self { + value.0 + } +} + +#[cfg(test)] +impl From for BlockChainId { + fn from(value: u64) -> Self { + BlockChainId(value) + } +}