blockchain-tree: replace reth-primitive imports (#10771)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Håvard Anda Estensen
2024-09-09 19:30:46 +02:00
committed by GitHub
parent 6f7c8c8caf
commit e7a4c85aa9
12 changed files with 53 additions and 45 deletions

1
Cargo.lock generated
View File

@ -6328,6 +6328,7 @@ name = "reth-blockchain-tree"
version = "1.0.6"
dependencies = [
"alloy-genesis",
"alloy-primitives",
"aquamarine",
"assert_matches",
"linked_hash_set",

View File

@ -31,6 +31,9 @@ reth-network.workspace = true
reth-consensus.workspace = true
reth-node-types.workspace = true
# ethereum
alloy-primitives.workspace = true
# common
parking_lot.workspace = true
tracing.workspace = true

View File

@ -1,6 +1,7 @@
use crate::metrics::BlockBufferMetrics;
use alloy_primitives::{BlockHash, BlockNumber};
use reth_network::cache::LruCache;
use reth_primitives::{BlockHash, BlockNumber, SealedBlockWithSenders};
use reth_primitives::SealedBlockWithSenders;
use std::collections::{btree_map, hash_map, BTreeMap, HashMap, HashSet};
/// Contains the tree of pending blocks that cannot be executed due to missing parent.
@ -182,7 +183,8 @@ impl BlockBuffer {
#[cfg(test)]
mod tests {
use crate::BlockBuffer;
use reth_primitives::{BlockHash, BlockNumHash, SealedBlockWithSenders};
use alloy_primitives::BlockHash;
use reth_primitives::{BlockNumHash, SealedBlockWithSenders};
use reth_testing_utils::generators::{self, random_block, BlockParams, Rng};
use std::collections::HashMap;

View File

@ -2,9 +2,10 @@
use super::state::SidechainId;
use crate::canonical_chain::CanonicalChain;
use alloy_primitives::{BlockHash, BlockNumber};
use linked_hash_set::LinkedHashSet;
use reth_execution_types::Chain;
use reth_primitives::{BlockHash, BlockNumHash, BlockNumber, SealedBlockWithSenders};
use reth_primitives::{BlockNumHash, SealedBlockWithSenders};
use std::collections::{btree_map, hash_map, BTreeMap, BTreeSet, HashMap, HashSet};
/// Internal indices of the blocks and chains.
@ -178,7 +179,7 @@ impl BlockIndices {
if new_block_value.1 != old_block_value.1 {
// remove block hash as it is different
removed.push(old_block_value);
added.push(new_block_value.into());
added.push(new_block_value.into())
}
new_hash = new_hashes.next();
old_hash = old_hashes.next();
@ -375,7 +376,8 @@ impl BlockIndices {
#[cfg(test)]
mod tests {
use super::*;
use reth_primitives::{Header, SealedBlock, SealedHeader, B256};
use alloy_primitives::B256;
use reth_primitives::{Header, SealedBlock, SealedHeader};
#[test]
fn pending_block_num_hash_returns_none_if_no_fork() {

View File

@ -5,6 +5,7 @@ use crate::{
state::{SidechainId, TreeState},
AppendableChain, BlockIndices, BlockchainTreeConfig, ExecutionData, TreeExternals,
};
use alloy_primitives::{BlockHash, BlockNumber, B256, U256};
use reth_blockchain_tree_api::{
error::{BlockchainTreeError, CanonicalError, InsertBlockError, InsertBlockErrorKind},
BlockAttachment, BlockStatus, BlockValidationKind, CanonicalOutcome, InsertPayloadOk,
@ -15,8 +16,8 @@ use reth_execution_errors::{BlockExecutionError, BlockValidationError};
use reth_execution_types::{Chain, ExecutionOutcome};
use reth_node_types::NodeTypesWithDB;
use reth_primitives::{
BlockHash, BlockNumHash, BlockNumber, EthereumHardfork, ForkBlock, GotExpected, Receipt,
SealedBlock, SealedBlockWithSenders, SealedHeader, StaticFileSegment, B256, U256,
BlockNumHash, EthereumHardfork, ForkBlock, GotExpected, Receipt, SealedBlock,
SealedBlockWithSenders, SealedHeader, StaticFileSegment,
};
use reth_provider::{
providers::ProviderNodeTypes, BlockExecutionWriter, BlockNumReader, BlockWriter,
@ -187,7 +188,7 @@ where
) -> Result<Option<BlockStatus>, InsertBlockErrorKind> {
// check if block is canonical
if self.is_block_hash_canonical(&block.hash)? {
return Ok(Some(BlockStatus::Valid(BlockAttachment::Canonical)))
return Ok(Some(BlockStatus::Valid(BlockAttachment::Canonical)));
}
let last_finalized_block = self.block_indices().last_finalized_block();
@ -195,7 +196,7 @@ where
if block.number <= last_finalized_block {
// check if block is inside database
if self.externals.provider_factory.provider()?.block_number(block.hash)?.is_some() {
return Ok(Some(BlockStatus::Valid(BlockAttachment::Canonical)))
return Ok(Some(BlockStatus::Valid(BlockAttachment::Canonical)));
}
return Err(BlockchainTreeError::PendingBlockIsFinalized {
@ -206,7 +207,7 @@ where
// is block inside chain
if let Some(attachment) = self.is_block_inside_sidechain(&block) {
return Ok(Some(BlockStatus::Valid(attachment)))
return Ok(Some(BlockStatus::Valid(attachment)));
}
// check if block is disconnected
@ -290,7 +291,7 @@ where
let Some((first_pending_block_number, _)) = parent_block_hashes.first_key_value()
else {
debug!(target: "blockchain_tree", ?chain_id, "No block hashes stored");
return None
return None;
};
let canonical_chain = canonical_chain
.iter()
@ -300,7 +301,7 @@ where
// get canonical fork.
let canonical_fork = self.canonical_fork(chain_id)?;
return Some(ExecutionData { execution_outcome, parent_block_hashes, canonical_fork })
return Some(ExecutionData { execution_outcome, parent_block_hashes, canonical_fork });
}
// check if there is canonical block
@ -310,7 +311,7 @@ where
canonical_fork: ForkBlock { number: canonical_number, hash: block_hash },
execution_outcome: ExecutionOutcome::default(),
parent_block_hashes: canonical_chain.inner().clone(),
})
});
}
None
@ -333,12 +334,12 @@ where
// check if block parent can be found in any side chain.
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)
return self.try_insert_block_into_side_chain(block, chain_id, block_validation_kind);
}
// if not found, check if the parent can be found inside canonical chain.
if self.is_block_hash_canonical(&parent.hash)? {
return self.try_append_canonical_chain(block.clone(), block_validation_kind)
return self.try_append_canonical_chain(block.clone(), block_validation_kind);
}
// this is another check to ensure that if the block points to a canonical block its block
@ -689,7 +690,7 @@ where
pub fn buffer_block(&mut self, block: SealedBlockWithSenders) -> Result<(), InsertBlockError> {
// validate block consensus rules
if let Err(err) = self.validate_block(&block) {
return Err(InsertBlockError::consensus_error(err, block.block))
return Err(InsertBlockError::consensus_error(err, block.block));
}
self.state.buffered_blocks.insert_block(block);
@ -707,17 +708,17 @@ where
"Failed to validate total difficulty for block {}: {e}",
block.header.hash()
);
return Err(e)
return Err(e);
}
if let Err(e) = self.externals.consensus.validate_header(block) {
error!(?block, "Failed to validate header {}: {e}", block.header.hash());
return Err(e)
return Err(e);
}
if let Err(e) = self.externals.consensus.validate_block_pre_execution(block) {
error!(?block, "Failed to validate block {}: {e}", block.header.hash());
return Err(e)
return Err(e);
}
Ok(())
@ -742,7 +743,7 @@ where
Some(BlockAttachment::Canonical)
} else {
Some(BlockAttachment::HistoricalFork)
}
};
}
None
}
@ -783,7 +784,7 @@ where
// validate block consensus rules
if let Err(err) = self.validate_block(&block) {
return Err(InsertBlockError::consensus_error(err, block.block))
return Err(InsertBlockError::consensus_error(err, block.block));
}
let status = self
@ -1057,7 +1058,7 @@ where
}
let head = self.state.block_indices.canonical_tip();
return Ok(CanonicalOutcome::AlreadyCanonical { header, head })
return Ok(CanonicalOutcome::AlreadyCanonical { header, head });
}
let Some(chain_id) = self.block_indices().get_side_chain_id(&block_hash) else {
@ -1072,7 +1073,7 @@ where
debug!(target: "blockchain_tree", ?block_hash, ?chain_id, "Chain not present");
return Err(CanonicalError::from(BlockchainTreeError::BlockSideChainIdConsistency {
chain_id: chain_id.into(),
}));
}))
};
trace!(target: "blockchain_tree", chain = ?canonical, "Found chain to make canonical");
durations_recorder.record_relative(MakeCanonicalAction::SplitChain);
@ -1102,7 +1103,7 @@ where
debug!(target: "blockchain_tree", "No blocks in the chain to make canonical");
return Err(CanonicalError::from(BlockchainTreeError::BlockHashNotFoundInChain {
block_hash: fork_block.hash,
}));
}))
};
trace!(target: "blockchain_tree", ?new_canon_chain, "Merging chains");
let mut chain_appended = false;
@ -1283,7 +1284,7 @@ where
pub fn unwind(&mut self, unwind_to: BlockNumber) -> Result<(), CanonicalError> {
// nothing to be done if unwind_to is higher then the tip
if self.block_indices().canonical_tip().number <= unwind_to {
return Ok(())
return Ok(());
}
// revert `N` blocks from current canonical chain and put them inside BlockchainTree
let old_canon_chain = self.revert_canonical_from_database(unwind_to)?;
@ -1325,7 +1326,7 @@ where
"Reverting optimistic canonical chain to block {}",
revert_until
);
return Err(CanonicalError::OptimisticTargetRevert(revert_until))
return Err(CanonicalError::OptimisticTargetRevert(revert_until));
}
// read data that is needed for new sidechain
@ -1379,6 +1380,7 @@ where
mod tests {
use super::*;
use alloy_genesis::{Genesis, GenesisAccount};
use alloy_primitives::{keccak256, Address, B256};
use assert_matches::assert_matches;
use linked_hash_set::LinkedHashSet;
use reth_chainspec::{ChainSpecBuilder, MAINNET};
@ -1389,11 +1391,10 @@ mod tests {
use reth_evm_ethereum::execute::EthExecutorProvider;
use reth_primitives::{
constants::{EIP1559_INITIAL_BASE_FEE, EMPTY_ROOT_HASH},
keccak256,
proofs::{calculate_receipt_root, calculate_transaction_root},
revm_primitives::AccountInfo,
Account, Address, Header, Signature, Transaction, TransactionSigned,
TransactionSignedEcRecovered, TxEip1559, Withdrawals, B256,
Account, Header, Signature, Transaction, TransactionSigned, TransactionSignedEcRecovered,
TxEip1559, Withdrawals,
};
use reth_provider::{
test_utils::{

View File

@ -1,6 +1,7 @@
//! [`ExecutionDataProvider`] implementations used by the tree.
use reth_primitives::{BlockHash, BlockNumber, ForkBlock};
use alloy_primitives::{BlockHash, BlockNumber};
use reth_primitives::ForkBlock;
use reth_provider::{BlockExecutionForkProvider, ExecutionDataProvider, ExecutionOutcome};
use std::collections::BTreeMap;
@ -25,7 +26,7 @@ impl<'a> ExecutionDataProvider for BundleStateDataRef<'a> {
fn block_hash(&self, block_number: BlockNumber) -> Option<BlockHash> {
let block_hash = self.sidechain_block_hashes.get(&block_number).copied();
if block_hash.is_some() {
return block_hash
return block_hash;
}
self.canonical_block_hashes.get(&block_number).copied()

View File

@ -1,4 +1,5 @@
use reth_primitives::{BlockHash, BlockNumHash, BlockNumber};
use alloy_primitives::{BlockHash, BlockNumber};
use reth_primitives::BlockNumHash;
use std::collections::BTreeMap;
/// This keeps track of (non-finalized) blocks of the canonical chain.

View File

@ -5,6 +5,7 @@
use super::externals::TreeExternals;
use crate::BundleStateDataRef;
use alloy_primitives::{BlockHash, BlockNumber, U256};
use reth_blockchain_tree_api::{
error::{BlockchainTreeError, InsertBlockErrorKind},
BlockAttachment, BlockValidationKind,
@ -13,9 +14,7 @@ use reth_consensus::{Consensus, ConsensusError, PostExecutionInput};
use reth_evm::execute::{BlockExecutorProvider, Executor};
use reth_execution_errors::BlockExecutionError;
use reth_execution_types::{Chain, ExecutionOutcome};
use reth_primitives::{
BlockHash, BlockNumber, ForkBlock, GotExpected, SealedBlockWithSenders, SealedHeader, U256,
};
use reth_primitives::{ForkBlock, GotExpected, SealedBlockWithSenders, SealedHeader};
use reth_provider::{
providers::{BundleStateProvider, ConsistentDbView, ProviderNodeTypes},
FullExecutionDataProvider, ProviderError, StateRootProvider,

View File

@ -1,10 +1,11 @@
//! Blockchain tree externals.
use alloy_primitives::{BlockHash, BlockNumber};
use reth_consensus::Consensus;
use reth_db::{static_file::HeaderMask, tables};
use reth_db_api::{cursor::DbCursorRO, transaction::DbTx};
use reth_node_types::NodeTypesWithDB;
use reth_primitives::{BlockHash, BlockNumber, StaticFileSegment};
use reth_primitives::StaticFileSegment;
use reth_provider::{
providers::ProviderNodeTypes, FinalizedBlockReader, FinalizedBlockWriter, ProviderFactory,
StaticFileProviderFactory, StatsReader,

View File

@ -1,13 +1,11 @@
use alloy_primitives::{BlockHash, BlockNumber};
use reth_blockchain_tree_api::{
self,
error::{BlockchainTreeError, CanonicalError, InsertBlockError, ProviderError},
BlockValidationKind, BlockchainTreeEngine, BlockchainTreeViewer, CanonicalOutcome,
InsertPayloadOk,
};
use reth_primitives::{
BlockHash, BlockNumHash, BlockNumber, Receipt, SealedBlock, SealedBlockWithSenders,
SealedHeader,
};
use reth_primitives::{BlockNumHash, Receipt, SealedBlock, SealedBlockWithSenders, SealedHeader};
use reth_provider::{
BlockchainTreePendingStateProvider, CanonStateNotificationSender, CanonStateNotifications,
CanonStateSubscriptions, FullExecutionDataProvider,

View File

@ -1,6 +1,7 @@
//! Wrapper around `BlockchainTree` that allows for it to be shared.
use super::BlockchainTree;
use alloy_primitives::{BlockHash, BlockNumber};
use parking_lot::RwLock;
use reth_blockchain_tree_api::{
error::{CanonicalError, InsertBlockError},
@ -9,10 +10,7 @@ use reth_blockchain_tree_api::{
};
use reth_evm::execute::BlockExecutorProvider;
use reth_node_types::NodeTypesWithDB;
use reth_primitives::{
BlockHash, BlockNumHash, BlockNumber, Receipt, SealedBlock, SealedBlockWithSenders,
SealedHeader,
};
use reth_primitives::{BlockNumHash, Receipt, SealedBlock, SealedBlockWithSenders, SealedHeader};
use reth_provider::{
providers::ProviderNodeTypes, BlockchainTreePendingStateProvider, CanonStateSubscriptions,
FullExecutionDataProvider, ProviderError,

View File

@ -1,7 +1,8 @@
//! Blockchain tree state.
use crate::{AppendableChain, BlockBuffer, BlockIndices};
use reth_primitives::{BlockHash, BlockNumber, Receipt, SealedBlock, SealedBlockWithSenders};
use alloy_primitives::{BlockHash, BlockNumber};
use reth_primitives::{Receipt, SealedBlock, SealedBlockWithSenders};
use std::collections::{BTreeMap, HashMap};
/// Container to hold the state of the blockchain tree.