Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use new type for BlockchainId #5012

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/blockchain-tree/src/block_indices.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
36 changes: 21 additions & 15 deletions crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -458,7 +458,7 @@ impl<DB: Database, EF: ExecutorFactory> BlockchainTree<DB, EF> {
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,
))
}
Expand All @@ -470,7 +470,7 @@ impl<DB: Database, EF: ExecutorFactory> BlockchainTree<DB, EF> {
Some(parent_chain) => parent_chain,
None => {
return Err(InsertBlockError::tree_error(
BlockchainTreeError::BlockSideChainIdConsistency { chain_id },
BlockchainTreeError::BlockSideChainIdConsistency { chain_id: chain_id.into() },
block.block,
))
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]),
Expand All @@ -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])),
Expand All @@ -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])),
Expand All @@ -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])),
Expand Down Expand Up @@ -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])),
Expand Down Expand Up @@ -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);
Expand All @@ -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]),
Expand All @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions crates/blockchain-tree/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
30 changes: 27 additions & 3 deletions crates/blockchain-tree/src/state.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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<BlockChainId> for u64 {
fn from(value: BlockChainId) -> Self {
value.0
}
}

#[cfg(test)]
impl From<u64> for BlockChainId {
fn from(value: u64) -> Self {
BlockChainId(value)
}
}
Loading