mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
chore: use new type for BlockchainId (#5012)
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
//! Implementation of [`BlockIndices`] related to [`super::BlockchainTree`]
|
//! Implementation of [`BlockIndices`] related to [`super::BlockchainTree`]
|
||||||
|
|
||||||
use super::chain::BlockChainId;
|
use super::state::BlockChainId;
|
||||||
use crate::canonical_chain::CanonicalChain;
|
use crate::canonical_chain::CanonicalChain;
|
||||||
use linked_hash_set::LinkedHashSet;
|
use linked_hash_set::LinkedHashSet;
|
||||||
use reth_primitives::{BlockHash, BlockNumHash, BlockNumber, SealedBlockWithSenders};
|
use reth_primitives::{BlockHash, BlockNumHash, BlockNumber, SealedBlockWithSenders};
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
//! Implementation of [`BlockchainTree`]
|
//! Implementation of [`BlockchainTree`]
|
||||||
use crate::{
|
use crate::{
|
||||||
canonical_chain::CanonicalChain,
|
canonical_chain::CanonicalChain,
|
||||||
chain::{BlockChainId, BlockKind},
|
chain::BlockKind,
|
||||||
metrics::TreeMetrics,
|
metrics::TreeMetrics,
|
||||||
state::TreeState,
|
state::{BlockChainId, TreeState},
|
||||||
AppendableChain, BlockIndices, BlockchainTreeConfig, BundleStateData, TreeExternals,
|
AppendableChain, BlockIndices, BlockchainTreeConfig, BundleStateData, TreeExternals,
|
||||||
};
|
};
|
||||||
use reth_db::{cursor::DbCursorRO, database::Database, tables, transaction::DbTx};
|
use reth_db::{cursor::DbCursorRO, database::Database, tables, transaction::DbTx};
|
||||||
@ -458,7 +458,7 @@ impl<DB: Database, EF: ExecutorFactory> BlockchainTree<DB, EF> {
|
|||||||
let canonical_fork = match self.canonical_fork(chain_id) {
|
let canonical_fork = match self.canonical_fork(chain_id) {
|
||||||
None => {
|
None => {
|
||||||
return Err(InsertBlockError::tree_error(
|
return Err(InsertBlockError::tree_error(
|
||||||
BlockchainTreeError::BlockSideChainIdConsistency { chain_id },
|
BlockchainTreeError::BlockSideChainIdConsistency { chain_id: chain_id.into() },
|
||||||
block.block,
|
block.block,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
@ -470,7 +470,7 @@ impl<DB: Database, EF: ExecutorFactory> BlockchainTree<DB, EF> {
|
|||||||
Some(parent_chain) => parent_chain,
|
Some(parent_chain) => parent_chain,
|
||||||
None => {
|
None => {
|
||||||
return Err(InsertBlockError::tree_error(
|
return Err(InsertBlockError::tree_error(
|
||||||
BlockchainTreeError::BlockSideChainIdConsistency { chain_id },
|
BlockchainTreeError::BlockSideChainIdConsistency { chain_id: chain_id.into() },
|
||||||
block.block,
|
block.block,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
@ -1371,7 +1371,7 @@ mod tests {
|
|||||||
// |
|
// |
|
||||||
TreeTester::default()
|
TreeTester::default()
|
||||||
.with_chain_num(1)
|
.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_fork_to_child(HashMap::from([(block1.parent_hash, HashSet::from([block1.hash]))]))
|
||||||
.with_pending_blocks((block1.number, HashSet::from([block1.hash])))
|
.with_pending_blocks((block1.number, HashSet::from([block1.hash])))
|
||||||
.assert(&tree);
|
.assert(&tree);
|
||||||
@ -1430,7 +1430,7 @@ mod tests {
|
|||||||
|
|
||||||
TreeTester::default()
|
TreeTester::default()
|
||||||
.with_chain_num(1)
|
.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([(
|
.with_fork_to_child(HashMap::from([(
|
||||||
block1.parent_hash,
|
block1.parent_hash,
|
||||||
HashSet::from([block1a_hash]),
|
HashSet::from([block1a_hash]),
|
||||||
@ -1453,7 +1453,10 @@ mod tests {
|
|||||||
// |
|
// |
|
||||||
TreeTester::default()
|
TreeTester::default()
|
||||||
.with_chain_num(2)
|
.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([
|
.with_fork_to_child(HashMap::from([
|
||||||
(block1.parent_hash, HashSet::from([block1a_hash])),
|
(block1.parent_hash, HashSet::from([block1a_hash])),
|
||||||
(block1.hash(), HashSet::from([block2a_hash])),
|
(block1.hash(), HashSet::from([block2a_hash])),
|
||||||
@ -1480,7 +1483,7 @@ mod tests {
|
|||||||
// |
|
// |
|
||||||
TreeTester::default()
|
TreeTester::default()
|
||||||
.with_chain_num(2)
|
.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([
|
.with_fork_to_child(HashMap::from([
|
||||||
(block1.parent_hash, HashSet::from([block1a_hash])),
|
(block1.parent_hash, HashSet::from([block1a_hash])),
|
||||||
(block1.hash(), HashSet::from([block2.hash])),
|
(block1.hash(), HashSet::from([block2.hash])),
|
||||||
@ -1501,9 +1504,9 @@ mod tests {
|
|||||||
TreeTester::default()
|
TreeTester::default()
|
||||||
.with_chain_num(2)
|
.with_chain_num(2)
|
||||||
.with_block_to_chain(HashMap::from([
|
.with_block_to_chain(HashMap::from([
|
||||||
(block1.hash, 4),
|
(block1.hash, 4.into()),
|
||||||
(block2a_hash, 4),
|
(block2a_hash, 4.into()),
|
||||||
(block2.hash, 3),
|
(block2.hash, 3.into()),
|
||||||
]))
|
]))
|
||||||
.with_fork_to_child(HashMap::from([
|
.with_fork_to_child(HashMap::from([
|
||||||
(block1.parent_hash, HashSet::from([block1.hash])),
|
(block1.parent_hash, HashSet::from([block1.hash])),
|
||||||
@ -1538,7 +1541,10 @@ mod tests {
|
|||||||
// |
|
// |
|
||||||
TreeTester::default()
|
TreeTester::default()
|
||||||
.with_chain_num(2)
|
.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([
|
.with_fork_to_child(HashMap::from([
|
||||||
(block1.parent_hash, HashSet::from([block1a_hash])),
|
(block1.parent_hash, HashSet::from([block1a_hash])),
|
||||||
(block1.hash(), HashSet::from([block2a_hash])),
|
(block1.hash(), HashSet::from([block2a_hash])),
|
||||||
@ -1567,7 +1573,7 @@ mod tests {
|
|||||||
// |
|
// |
|
||||||
TreeTester::default()
|
TreeTester::default()
|
||||||
.with_chain_num(1)
|
.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_fork_to_child(HashMap::from([(block1.hash(), HashSet::from([block2a_hash]))]))
|
||||||
.with_pending_blocks((block2.number + 1, HashSet::from([])))
|
.with_pending_blocks((block2.number + 1, HashSet::from([])))
|
||||||
.assert(&tree);
|
.assert(&tree);
|
||||||
@ -1586,7 +1592,7 @@ mod tests {
|
|||||||
// |
|
// |
|
||||||
TreeTester::default()
|
TreeTester::default()
|
||||||
.with_chain_num(2)
|
.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([(
|
.with_fork_to_child(HashMap::from([(
|
||||||
block1.hash(),
|
block1.hash(),
|
||||||
HashSet::from([block2a_hash, block2.hash]),
|
HashSet::from([block2a_hash, block2.hash]),
|
||||||
@ -1607,7 +1613,7 @@ mod tests {
|
|||||||
// |
|
// |
|
||||||
TreeTester::default()
|
TreeTester::default()
|
||||||
.with_chain_num(1)
|
.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_fork_to_child(HashMap::from([(block1.hash(), HashSet::from([block2a_hash]))]))
|
||||||
.with_pending_blocks((block2.number + 1, HashSet::new()))
|
.with_pending_blocks((block2.number + 1, HashSet::new()))
|
||||||
.assert(&tree);
|
.assert(&tree);
|
||||||
|
|||||||
@ -22,9 +22,6 @@ use std::{
|
|||||||
ops::{Deref, DerefMut},
|
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
|
/// A chain if the blockchain tree, that has functionality to execute blocks and append them to the
|
||||||
/// it self.
|
/// it self.
|
||||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
//! Blockchain tree state.
|
//! Blockchain tree state.
|
||||||
|
|
||||||
use crate::{chain::BlockChainId, AppendableChain, BlockBuffer, BlockIndices};
|
use crate::{AppendableChain, BlockBuffer, BlockIndices};
|
||||||
use reth_primitives::{BlockHash, BlockNumber, Receipt, SealedBlock, SealedBlockWithSenders};
|
use reth_primitives::{BlockHash, BlockNumber, Receipt, SealedBlock, SealedBlockWithSenders};
|
||||||
use std::collections::{BTreeMap, HashMap};
|
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.
|
/// Expose internal indices of the BlockchainTree.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn block_indices(&self) -> &BlockIndices {
|
pub(crate) fn block_indices(&self) -> &BlockIndices {
|
||||||
@ -70,8 +78,7 @@ impl TreeState {
|
|||||||
if chain.is_empty() {
|
if chain.is_empty() {
|
||||||
return None
|
return None
|
||||||
}
|
}
|
||||||
let chain_id = self.block_chain_id_generator;
|
let chain_id = self.next_id();
|
||||||
self.block_chain_id_generator += 1;
|
|
||||||
|
|
||||||
self.block_indices.insert_chain(chain_id, &chain);
|
self.block_indices.insert_chain(chain_id, &chain);
|
||||||
// add chain_id -> chain index
|
// add chain_id -> chain index
|
||||||
@ -92,3 +99,20 @@ impl TreeState {
|
|||||||
self.buffered_blocks.lowest_ancestor(hash)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user