Skip to content

Commit

Permalink
refactor(storage): unify blocks insertion logic (#12694)
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr authored Nov 20, 2024
1 parent 749f98e commit 0c59841
Show file tree
Hide file tree
Showing 24 changed files with 226 additions and 269 deletions.
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
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",
]
16 changes: 14 additions & 2 deletions crates/engine/tree/src/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::metrics::PersistenceMetrics;
use alloy_eips::BlockNumHash;
use reth_chain_state::ExecutedBlock;
use reth_errors::ProviderError;
use reth_primitives::BlockBody;
use reth_primitives_traits::{Block, FullNodePrimitives};
use reth_provider::{
providers::ProviderNodeTypes, writer::UnifiedStorageWriter, BlockHashReader,
ChainStateBlockWriter, DatabaseProviderFactory, ProviderFactory, StaticFileProviderFactory,
Expand All @@ -16,6 +18,16 @@ use thiserror::Error;
use tokio::sync::oneshot;
use tracing::{debug, error};

/// A helper trait with requirements for [`ProviderNodeTypes`] to be used within
/// [`PersistenceService`].
pub trait PersistenceNodeTypes:
ProviderNodeTypes<Primitives: FullNodePrimitives<Block: Block<Body = BlockBody>>>
{
}
impl<T> PersistenceNodeTypes for T where
T: ProviderNodeTypes<Primitives: FullNodePrimitives<Block: Block<Body = BlockBody>>>
{
}
/// Writes parts of reth's in memory tree state to the database and static files.
///
/// This is meant to be a spawned service that listens for various incoming persistence operations,
Expand Down Expand Up @@ -60,7 +72,7 @@ impl<N: ProviderNodeTypes> PersistenceService<N> {
}
}

impl<N: ProviderNodeTypes> PersistenceService<N> {
impl<N: PersistenceNodeTypes> PersistenceService<N> {
/// This is the main loop, that will listen to database events and perform the requested
/// database actions
pub fn run(mut self) -> Result<(), PersistenceError> {
Expand Down Expand Up @@ -198,7 +210,7 @@ impl PersistenceHandle {
}

/// Create a new [`PersistenceHandle`], and spawn the persistence service.
pub fn spawn_service<N: ProviderNodeTypes>(
pub fn spawn_service<N: PersistenceNodeTypes>(
provider_factory: ProviderFactory<N>,
pruner: PrunerWithFactory<ProviderFactory<N>>,
sync_metrics_tx: MetricEventsSender,
Expand Down
6 changes: 4 additions & 2 deletions crates/exex/exex/src/backfill/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use reth_evm::execute::{
BatchExecutor, BlockExecutionInput, BlockExecutionOutput, BlockExecutorProvider, Executor,
};
use reth_evm_ethereum::execute::EthExecutorProvider;
use reth_node_api::FullNodePrimitives;
use reth_primitives::{
Block, BlockBody, BlockWithSenders, Receipt, SealedBlockWithSenders, Transaction,
};
Expand Down Expand Up @@ -57,7 +58,7 @@ pub(crate) fn execute_block_and_commit_to_database<N>(
block: &BlockWithSenders,
) -> eyre::Result<BlockExecutionOutput<Receipt>>
where
N: ProviderNodeTypes,
N: ProviderNodeTypes<Primitives: FullNodePrimitives<Block = reth_primitives::Block>>,
{
let provider = provider_factory.provider()?;

Expand Down Expand Up @@ -161,7 +162,7 @@ pub(crate) fn blocks_and_execution_outputs<N>(
key_pair: Keypair,
) -> eyre::Result<Vec<(SealedBlockWithSenders, BlockExecutionOutput<Receipt>)>>
where
N: ProviderNodeTypes,
N: ProviderNodeTypes<Primitives: FullNodePrimitives<Block = reth_primitives::Block>>,
{
let (block1, block2) = blocks(chain_spec.clone(), key_pair)?;

Expand All @@ -183,6 +184,7 @@ pub(crate) fn blocks_and_execution_outcome<N>(
) -> eyre::Result<(Vec<SealedBlockWithSenders>, ExecutionOutcome)>
where
N: ProviderNodeTypes,
N::Primitives: FullNodePrimitives<Block = reth_primitives::Block>,
{
let (block1, block2) = blocks(chain_spec.clone(), key_pair)?;

Expand Down
4 changes: 2 additions & 2 deletions crates/exex/exex/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ mod tests {
use reth_primitives::SealedBlockWithSenders;
use reth_provider::{
providers::BlockchainProvider2, test_utils::create_test_provider_factory, BlockReader,
BlockWriter, Chain, DatabaseProviderFactory, TransactionVariant,
BlockWriter, Chain, DatabaseProviderFactory, StorageLocation, TransactionVariant,
};
use reth_testing_utils::generators::{self, random_block, BlockParams};

Expand Down Expand Up @@ -1238,7 +1238,7 @@ mod tests {
.seal_with_senders()
.unwrap();
let provider_rw = provider_factory.database_provider_rw().unwrap();
provider_rw.insert_block(block.clone()).unwrap();
provider_rw.insert_block(block.clone(), StorageLocation::Database).unwrap();
provider_rw.commit().unwrap();

let provider = BlockchainProvider2::new(provider_factory).unwrap();
Expand Down
Loading

0 comments on commit 0c59841

Please sign in to comment.