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

refactor(storage): unify blocks insertion logic #12694

Merged
merged 4 commits into from
Nov 20, 2024
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions bin/reth/src/commands/debug_cmd/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use reth_node_ethereum::EthExecutorProvider;
use reth_provider::{
providers::ProviderNodeTypes, writer::UnifiedStorageWriter, BlockNumReader, BlockWriter,
ChainSpecProvider, DatabaseProviderFactory, HeaderProvider, LatestStateProviderRef,
OriginalValuesKnown, ProviderError, ProviderFactory, StateWriter,
OriginalValuesKnown, ProviderError, ProviderFactory, StateWriter, StorageLocation,
};
use reth_revm::database::StateProviderDatabase;
use reth_stages::{
Expand Down Expand Up @@ -148,7 +148,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
.map_err(|block| eyre::eyre!("Error sealing block with senders: {block:?}"))?;
trace!(target: "reth::cli", block_number, "Executing block");

provider_rw.insert_block(sealed_block.clone())?;
provider_rw.insert_block(sealed_block.clone(), StorageLocation::Database)?;

td += sealed_block.difficulty;
let mut executor = executor_provider.batch_executor(StateProviderDatabase::new(
Expand Down
23 changes: 16 additions & 7 deletions crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Implementation of [`BlockchainTree`]

use crate::{
externals::TreeNodeTypes,
metrics::{MakeCanonicalAction, MakeCanonicalDurationsRecorder, TreeMetrics},
state::{SidechainId, TreeState},
AppendableChain, BlockIndices, BlockchainTreeConfig, ExecutionData, TreeExternals,
Expand All @@ -21,10 +22,10 @@ use reth_primitives::{
SealedHeader, StaticFileSegment,
};
use reth_provider::{
providers::ProviderNodeTypes, BlockExecutionWriter, BlockNumReader, BlockWriter,
CanonStateNotification, CanonStateNotificationSender, CanonStateNotifications,
ChainSpecProvider, ChainSplit, ChainSplitTarget, DBProvider, DisplayBlocksChain,
HeaderProvider, ProviderError, StaticFileProviderFactory,
BlockExecutionWriter, BlockNumReader, BlockWriter, CanonStateNotification,
CanonStateNotificationSender, CanonStateNotifications, ChainSpecProvider, ChainSplit,
ChainSplitTarget, DBProvider, DisplayBlocksChain, HeaderProvider, ProviderError,
StaticFileProviderFactory,
};
use reth_stages_api::{MetricEvent, MetricEventsSender};
use reth_storage_errors::provider::{ProviderResult, RootMismatch};
Expand Down Expand Up @@ -93,7 +94,7 @@ impl<N: NodeTypesWithDB, E> BlockchainTree<N, E> {

impl<N, E> BlockchainTree<N, E>
where
N: ProviderNodeTypes,
N: TreeNodeTypes,
E: BlockExecutorProvider,
{
/// Builds the blockchain tree for the node.
Expand Down Expand Up @@ -1386,16 +1387,18 @@ mod tests {
use reth_db_api::transaction::DbTxMut;
use reth_evm::test_utils::MockExecutorProvider;
use reth_evm_ethereum::execute::EthExecutorProvider;
use reth_node_types::FullNodePrimitives;
use reth_primitives::{
proofs::{calculate_receipt_root, calculate_transaction_root},
Account, BlockBody, Transaction, TransactionSigned, TransactionSignedEcRecovered,
};
use reth_provider::{
providers::ProviderNodeTypes,
test_utils::{
blocks::BlockchainTestData, create_test_provider_factory_with_chain_spec,
MockNodeTypesWithDB,
},
ProviderFactory,
ProviderFactory, StorageLocation,
};
use reth_revm::primitives::AccountInfo;
use reth_stages_api::StageCheckpoint;
Expand All @@ -1420,7 +1423,12 @@ mod tests {
TreeExternals::new(provider_factory, consensus, executor_factory)
}

fn setup_genesis<N: ProviderNodeTypes>(factory: &ProviderFactory<N>, mut genesis: SealedBlock) {
fn setup_genesis<
N: ProviderNodeTypes<Primitives: FullNodePrimitives<Block = reth_primitives::Block>>,
>(
factory: &ProviderFactory<N>,
mut genesis: SealedBlock,
) {
// insert genesis to db.

genesis.header.set_block_number(10);
Expand Down Expand Up @@ -1551,6 +1559,7 @@ mod tests {
SealedBlock::new(chain_spec.sealed_genesis_header(), Default::default())
.try_seal_with_senders()
.unwrap(),
StorageLocation::Database,
)
.unwrap();
let account = Account { balance: initial_signer_balance, ..Default::default() };
Expand Down
14 changes: 12 additions & 2 deletions crates/blockchain-tree/src/externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@ 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::StaticFileSegment;
use reth_node_types::{Block, FullNodePrimitives, NodeTypesWithDB};
use reth_primitives::{BlockBody, StaticFileSegment};
use reth_provider::{
providers::ProviderNodeTypes, ChainStateBlockReader, ChainStateBlockWriter, ProviderFactory,
StaticFileProviderFactory, StatsReader,
};
use reth_storage_errors::provider::ProviderResult;
use std::{collections::BTreeMap, sync::Arc};

/// A helper trait with requirements for [`ProviderNodeTypes`] to be used within [`TreeExternals`].
pub trait TreeNodeTypes:
ProviderNodeTypes<Primitives: FullNodePrimitives<Block: Block<Body = BlockBody>>>
{
}
impl<T> TreeNodeTypes for T where
T: ProviderNodeTypes<Primitives: FullNodePrimitives<Block: Block<Body = BlockBody>>>
{
}

/// A container for external components.
///
/// This is a simple container for external components used throughout the blockchain tree
Expand Down
8 changes: 5 additions & 3 deletions crates/blockchain-tree/src/shareable.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Wrapper around `BlockchainTree` that allows for it to be shared.

use crate::externals::TreeNodeTypes;

use super::BlockchainTree;
use alloy_eips::BlockNumHash;
use alloy_primitives::{BlockHash, BlockNumber};
Expand Down Expand Up @@ -36,7 +38,7 @@ impl<N: NodeTypesWithDB, E> ShareableBlockchainTree<N, E> {

impl<N, E> BlockchainTreeEngine for ShareableBlockchainTree<N, E>
where
N: ProviderNodeTypes,
N: TreeNodeTypes,
E: BlockExecutorProvider,
{
fn buffer_block(&self, block: SealedBlockWithSenders) -> Result<(), InsertBlockError> {
Expand Down Expand Up @@ -107,7 +109,7 @@ where

impl<N, E> BlockchainTreeViewer for ShareableBlockchainTree<N, E>
where
N: ProviderNodeTypes,
N: TreeNodeTypes,
E: BlockExecutorProvider,
{
fn header_by_hash(&self, hash: BlockHash) -> Option<SealedHeader> {
Expand Down Expand Up @@ -170,7 +172,7 @@ where

impl<N, E> BlockchainTreePendingStateProvider for ShareableBlockchainTree<N, E>
where
N: ProviderNodeTypes,
N: TreeNodeTypes,
E: BlockExecutorProvider,
{
fn find_pending_state_provider(
Expand Down
18 changes: 9 additions & 9 deletions crates/blockchain-tree/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ mod tests {
let mut tree_state = TreeState::new(0, vec![], 5);

// Create a chain with two blocks
let block = SealedBlockWithSenders::default();
let block: SealedBlockWithSenders = Default::default();
let block1_hash = B256::random();
let block2_hash = B256::random();

Expand Down Expand Up @@ -254,8 +254,8 @@ mod tests {
let block1_hash = B256::random();
let block2_hash = B256::random();

let mut block1 = SealedBlockWithSenders::default();
let mut block2 = SealedBlockWithSenders::default();
let mut block1: SealedBlockWithSenders = Default::default();
let mut block2: SealedBlockWithSenders = Default::default();

block1.block.header.set_hash(block1_hash);
block1.block.header.set_block_number(9);
Expand Down Expand Up @@ -296,8 +296,8 @@ mod tests {
let block1_hash = B256::random();
let block2_hash = B256::random();

let mut block1 = SealedBlockWithSenders::default();
let mut block2 = SealedBlockWithSenders::default();
let mut block1: SealedBlockWithSenders = Default::default();
let mut block2: SealedBlockWithSenders = Default::default();

block1.block.header.set_hash(block1_hash);
block1.block.header.set_block_number(9);
Expand Down Expand Up @@ -336,7 +336,7 @@ mod tests {

// Create a block with a random hash and add it to the buffer
let block_hash = B256::random();
let mut block = SealedBlockWithSenders::default();
let mut block: SealedBlockWithSenders = Default::default();
block.block.header.set_hash(block_hash);

// Add the block to the buffered blocks in the TreeState
Expand All @@ -363,8 +363,8 @@ mod tests {
let ancestor_hash = B256::random();
let descendant_hash = B256::random();

let mut ancestor_block = SealedBlockWithSenders::default();
let mut descendant_block = SealedBlockWithSenders::default();
let mut ancestor_block: SealedBlockWithSenders = Default::default();
let mut descendant_block: SealedBlockWithSenders = Default::default();

ancestor_block.block.header.set_hash(ancestor_hash);
descendant_block.block.header.set_hash(descendant_hash);
Expand Down Expand Up @@ -397,7 +397,7 @@ mod tests {
let receipt1 = Receipt::default();
let receipt2 = Receipt::default();

let mut block = SealedBlockWithSenders::default();
let mut block: SealedBlockWithSenders = Default::default();
block.block.header.set_hash(block_hash);

let receipts = vec![receipt1, receipt2];
Expand Down
10 changes: 5 additions & 5 deletions crates/chain-state/src/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ mod tests {

#[test]
fn test_commit_notification() {
let block = SealedBlockWithSenders::default();
let block: SealedBlockWithSenders = Default::default();
let block1_hash = B256::new([0x01; 32]);
let block2_hash = B256::new([0x02; 32]);

Expand Down Expand Up @@ -233,7 +233,7 @@ mod tests {

#[test]
fn test_reorg_notification() {
let block = SealedBlockWithSenders::default();
let block: SealedBlockWithSenders = Default::default();
let block1_hash = B256::new([0x01; 32]);
let block2_hash = B256::new([0x02; 32]);
let block3_hash = B256::new([0x03; 32]);
Expand Down Expand Up @@ -275,7 +275,7 @@ mod tests {
#[test]
fn test_block_receipts_commit() {
// Create a default block instance for use in block definitions.
let block = SealedBlockWithSenders::default();
let block: SealedBlockWithSenders = Default::default();

// Define unique hashes for two blocks to differentiate them in the chain.
let block1_hash = B256::new([0x01; 32]);
Expand Down Expand Up @@ -341,7 +341,7 @@ mod tests {
#[test]
fn test_block_receipts_reorg() {
// Define block1 for the old chain segment, which will be reverted.
let mut old_block1 = SealedBlockWithSenders::default();
let mut old_block1: SealedBlockWithSenders = Default::default();
old_block1.set_block_number(1);
old_block1.set_hash(B256::new([0x01; 32]));
old_block1.block.body.transactions.push(TransactionSigned::default());
Expand All @@ -364,7 +364,7 @@ mod tests {
let old_chain = Arc::new(Chain::new(vec![old_block1.clone()], old_execution_outcome, None));

// Define block2 for the new chain segment, which will be committed.
let mut new_block1 = SealedBlockWithSenders::default();
let mut new_block1: SealedBlockWithSenders = Default::default();
new_block1.set_block_number(2);
new_block1.set_hash(B256::new([0x02; 32]));
new_block1.block.body.transactions.push(TransactionSigned::default());
Expand Down
15 changes: 8 additions & 7 deletions crates/cli/commands/src/init_state/without_evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ use alloy_rlp::Decodable;

use alloy_consensus::Header;
use reth_node_builder::NodePrimitives;
use reth_primitives::{
BlockBody, SealedBlock, SealedBlockWithSenders, SealedHeader, StaticFileSegment,
};
use reth_primitives::{SealedBlock, SealedBlockWithSenders, SealedHeader, StaticFileSegment};
use reth_provider::{
providers::StaticFileProvider, BlockWriter, StageCheckpointWriter, StaticFileProviderFactory,
StaticFileWriter,
StaticFileWriter, StorageLocation,
};
use reth_stages::{StageCheckpoint, StageId};

Expand All @@ -33,7 +31,9 @@ pub fn setup_without_evm<Provider>(
total_difficulty: U256,
) -> Result<(), eyre::Error>
where
Provider: StaticFileProviderFactory + StageCheckpointWriter + BlockWriter,
Provider: StaticFileProviderFactory
+ StageCheckpointWriter
+ BlockWriter<Body: reth_node_api::BlockBody>,
{
info!(target: "reth::cli", "Setting up dummy EVM chain before importing state.");

Expand Down Expand Up @@ -64,11 +64,12 @@ fn append_first_block<Provider>(
total_difficulty: U256,
) -> Result<(), eyre::Error>
where
Provider: BlockWriter + StaticFileProviderFactory,
Provider: BlockWriter<Body: reth_node_api::BlockBody> + StaticFileProviderFactory,
{
provider_rw.insert_block(
SealedBlockWithSenders::new(SealedBlock::new(header.clone(), BlockBody::default()), vec![])
SealedBlockWithSenders::new(SealedBlock::new(header.clone(), Default::default()), vec![])
.expect("no senders or txes"),
StorageLocation::Database,
)?;

let sf_provider = provider_rw.static_file_provider();
Expand Down
9 changes: 7 additions & 2 deletions crates/consensus/beacon/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1991,7 +1991,8 @@ mod tests {
use alloy_rpc_types_engine::{ForkchoiceState, ForkchoiceUpdated, PayloadStatus};
use assert_matches::assert_matches;
use reth_chainspec::{ChainSpecBuilder, MAINNET};
use reth_provider::{BlockWriter, ProviderFactory};
use reth_node_types::FullNodePrimitives;
use reth_provider::{BlockWriter, ProviderFactory, StorageLocation};
use reth_rpc_types_compat::engine::payload::block_to_payload_v1;
use reth_stages::{ExecOutput, PipelineError, StageError};
use reth_stages_api::StageCheckpoint;
Expand Down Expand Up @@ -2169,7 +2170,10 @@ mod tests {
assert_matches!(rx.await, Ok(Ok(())));
}

fn insert_blocks<'a, N: ProviderNodeTypes>(
fn insert_blocks<
'a,
N: ProviderNodeTypes<Primitives: FullNodePrimitives<Block = reth_primitives::Block>>,
>(
provider_factory: ProviderFactory<N>,
mut blocks: impl Iterator<Item = &'a SealedBlock>,
) {
Expand All @@ -2179,6 +2183,7 @@ mod tests {
provider
.insert_block(
b.clone().try_seal_with_senders().expect("invalid tx signature in block"),
StorageLocation::Database,
)
.map(drop)
})
Expand Down
4 changes: 2 additions & 2 deletions crates/engine/local/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use reth_engine_tree::{
EngineApiKind, EngineApiRequest, EngineApiRequestHandler, EngineRequestHandler, FromEngine,
RequestHandlerEvent,
},
persistence::PersistenceHandle,
persistence::{PersistenceHandle, PersistenceNodeTypes},
tree::{EngineApiTreeHandler, InvalidBlockHook, TreeConfig},
};
use reth_evm::execute::BlockExecutorProvider;
Expand Down Expand Up @@ -59,7 +59,7 @@ where

impl<N> LocalEngineService<N>
where
N: EngineNodeTypes,
N: EngineNodeTypes + PersistenceNodeTypes,
{
/// Constructor for [`LocalEngineService`].
#[allow(clippy::too_many_arguments)]
Expand Down
4 changes: 2 additions & 2 deletions crates/engine/service/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use reth_engine_tree::{
backfill::PipelineSync,
download::BasicBlockDownloader,
engine::{EngineApiKind, EngineApiRequest, EngineApiRequestHandler, EngineHandler},
persistence::PersistenceHandle,
persistence::{PersistenceHandle, PersistenceNodeTypes},
tree::{EngineApiTreeHandler, InvalidBlockHook, TreeConfig},
};
pub use reth_engine_tree::{
Expand Down Expand Up @@ -59,7 +59,7 @@ where

impl<N, Client, E> EngineService<N, Client, E>
where
N: EngineNodeTypes,
N: EngineNodeTypes + PersistenceNodeTypes,
Client: EthBlockClient + 'static,
E: BlockExecutorProvider + 'static,
{
Expand Down
2 changes: 2 additions & 0 deletions crates/engine/tree/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ reth-payload-builder-primitives.workspace = true
reth-payload-primitives.workspace = true
reth-payload-validator.workspace = true
reth-primitives.workspace = true
reth-primitives-traits.workspace = true
reth-provider.workspace = true
reth-prune.workspace = true
reth-revm.workspace = true
Expand Down Expand Up @@ -107,4 +108,5 @@ test-utils = [
"reth-provider/test-utils",
"reth-trie/test-utils",
"reth-prune-types?/test-utils",
"reth-primitives-traits/test-utils",
]
Loading