Skip to content

Commit

Permalink
feat: introduce StateCommitment in StateProviders
Browse files Browse the repository at this point in the history
  • Loading branch information
frisitano committed Oct 18, 2024
1 parent 5b5fe8f commit 03ea70e
Show file tree
Hide file tree
Showing 17 changed files with 345 additions and 203 deletions.
9 changes: 3 additions & 6 deletions bin/reth/src/commands/debug_cmd/in_memory_merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use reth_node_ethereum::EthExecutorProvider;
use reth_primitives::BlockHashOrNumber;
use reth_provider::{
writer::UnifiedStorageWriter, AccountExtReader, ChainSpecProvider, HashingWriter,
HeaderProvider, LatestStateProviderRef, OriginalValuesKnown, ProviderFactory,
StageCheckpointReader, StateWriter, StaticFileProviderFactory, StorageReader,
HeaderProvider, OriginalValuesKnown, ProviderFactory, StageCheckpointReader, StateWriter,
StorageReader, ToLatestStateProviderRef,
};
use reth_revm::database::StateProviderDatabase;
use reth_stages::StageId;
Expand Down Expand Up @@ -131,10 +131,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
)
.await?;

let db = StateProviderDatabase::new(LatestStateProviderRef::new(
provider.tx_ref(),
provider_factory.static_file_provider(),
));
let db = StateProviderDatabase::new(provider.latest());

let executor = EthExecutorProvider::ethereum(provider_factory.chain_spec()).executor(db);

Expand Down
12 changes: 4 additions & 8 deletions bin/reth/src/commands/debug_cmd/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use reth_node_ethereum::EthExecutorProvider;
use reth_primitives::BlockHashOrNumber;
use reth_provider::{
writer::UnifiedStorageWriter, BlockNumReader, BlockWriter, ChainSpecProvider,
DatabaseProviderFactory, HeaderProvider, LatestStateProviderRef, OriginalValuesKnown,
ProviderError, ProviderFactory, StateWriter, StaticFileProviderFactory,
DatabaseProviderFactory, HeaderProvider, OriginalValuesKnown, ProviderError, ProviderFactory,
StateWriter, ToLatestStateProviderRef,
};
use reth_revm::database::StateProviderDatabase;
use reth_stages::{
Expand Down Expand Up @@ -152,12 +152,8 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
provider_rw.insert_block(sealed_block.clone())?;

td += sealed_block.difficulty;
let mut executor = executor_provider.batch_executor(StateProviderDatabase::new(
LatestStateProviderRef::new(
provider_rw.tx_ref(),
provider_rw.static_file_provider().clone(),
),
));
let mut executor =
executor_provider.batch_executor(StateProviderDatabase::new(provider_rw.latest()));
executor.execute_and_verify_one((&sealed_block.clone().unseal(), td).into())?;
let execution_outcome = executor.finalize();

Expand Down
15 changes: 5 additions & 10 deletions crates/exex/exex/src/backfill/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use reth_primitives::{
SealedBlockWithSenders, Transaction,
};
use reth_provider::{
providers::ProviderNodeTypes, BlockWriter as _, ExecutionOutcome, LatestStateProviderRef,
ProviderFactory, StaticFileProviderFactory,
providers::ProviderNodeTypes, BlockWriter as _, ExecutionOutcome, ProviderFactory,
ToLatestStateProviderRef,
};
use reth_revm::database::StateProviderDatabase;
use reth_testing_utils::generators::sign_tx_with_key_pair;
Expand Down Expand Up @@ -64,10 +64,7 @@ where

// Execute the block to produce a block execution output
let mut block_execution_output = EthExecutorProvider::ethereum(chain_spec)
.executor(StateProviderDatabase::new(LatestStateProviderRef::new(
provider.tx_ref(),
provider.static_file_provider(),
)))
.executor(StateProviderDatabase::new(provider.latest()))
.execute(BlockExecutionInput { block, total_difficulty: U256::ZERO })?;
block_execution_output.state.reverts.sort();

Expand Down Expand Up @@ -192,10 +189,8 @@ where

let provider = provider_factory.provider()?;

let executor =
EthExecutorProvider::ethereum(chain_spec).batch_executor(StateProviderDatabase::new(
LatestStateProviderRef::new(provider.tx_ref(), provider.static_file_provider()),
));
let executor = EthExecutorProvider::ethereum(chain_spec)
.batch_executor(StateProviderDatabase::new(provider.latest()));

let mut execution_outcome = executor.execute_and_verify_batch(vec![
(&block1, U256::ZERO).into(),
Expand Down
10 changes: 9 additions & 1 deletion crates/stages/stages/benches/criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use reth_stages::{
StageCheckpoint,
};
use reth_stages_api::{ExecInput, Stage, StageExt, UnwindInput};
use reth_trie_db::MerklePatriciaTrie;
use std::ops::RangeInclusive;
use tokio::runtime::Runtime;

Expand Down Expand Up @@ -148,7 +149,14 @@ fn measure_stage<F, S>(
block_interval: RangeInclusive<BlockNumber>,
label: String,
) where
S: Clone + Stage<DatabaseProvider<<TempDatabase<DatabaseEnv> as Database>::TXMut, ChainSpec>>,
S: Clone
+ Stage<
DatabaseProvider<
<TempDatabase<DatabaseEnv> as Database>::TXMut,
ChainSpec,
MerklePatriciaTrie,
>,
>,
F: Fn(S, &TestStageDB, StageRange),
{
let stage_range = (
Expand Down
20 changes: 17 additions & 3 deletions crates/stages/stages/benches/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,19 @@ mod constants;
mod account_hashing;
pub use account_hashing::*;
use reth_stages_api::{ExecInput, Stage, UnwindInput};
use reth_trie_db::DatabaseStateRoot;
use reth_trie_db::{DatabaseStateRoot, MerklePatriciaTrie};

pub(crate) type StageRange = (ExecInput, UnwindInput);

pub(crate) fn stage_unwind<
S: Clone + Stage<DatabaseProvider<<TempDatabase<DatabaseEnv> as Database>::TXMut, ChainSpec>>,
S: Clone
+ Stage<
DatabaseProvider<
<TempDatabase<DatabaseEnv> as Database>::TXMut,
ChainSpec,
MerklePatriciaTrie,
>,
>,
>(
stage: S,
db: &TestStageDB,
Expand Down Expand Up @@ -63,7 +70,14 @@ pub(crate) fn stage_unwind<

pub(crate) fn unwind_hashes<S>(stage: S, db: &TestStageDB, range: StageRange)
where
S: Clone + Stage<DatabaseProvider<<TempDatabase<DatabaseEnv> as Database>::TXMut, ChainSpec>>,
S: Clone
+ Stage<
DatabaseProvider<
<TempDatabase<DatabaseEnv> as Database>::TXMut,
ChainSpec,
MerklePatriciaTrie,
>,
>,
{
let (input, unwind) = range;

Expand Down
22 changes: 12 additions & 10 deletions crates/stages/stages/src/stages/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use reth_primitives_traits::format_gas_throughput;
use reth_provider::{
providers::{StaticFileProvider, StaticFileProviderRWRefMut, StaticFileWriter},
writer::UnifiedStorageWriter,
BlockReader, DBProvider, HeaderProvider, LatestStateProviderRef, OriginalValuesKnown,
ProviderError, StateChangeWriter, StateWriter, StaticFileProviderFactory, StatsReader,
BlockReader, DBProvider, HeaderProvider, OriginalValuesKnown, ProviderError, StateChangeWriter,
StateWriter, StaticFileProviderFactory, StatsReader, ToLatestStateProviderRef,
TransactionVariant,
};
use reth_prune_types::PruneModes;
Expand Down Expand Up @@ -45,8 +45,9 @@ use tracing::*;
/// - [`tables::BlockBodyIndices`] to get tx number
/// - [`tables::Transactions`] to execute
///
/// For state access [`LatestStateProviderRef`] provides us latest state and history state
/// For latest most recent state [`LatestStateProviderRef`] would need (Used for execution Stage):
/// For state access [`reth_provider::LatestStateProviderRef`] provides us latest state and history
/// state For latest most recent state [`reth_provider::LatestStateProviderRef`] would need (Used
/// for execution Stage):
/// - [`tables::PlainAccountState`]
/// - [`tables::Bytecodes`]
/// - [`tables::PlainStorageState`]
Expand Down Expand Up @@ -174,8 +175,12 @@ impl<E> ExecutionStage<E> {
impl<E, Provider> Stage<Provider> for ExecutionStage<E>
where
E: BlockExecutorProvider,
Provider:
DBProvider + BlockReader + StaticFileProviderFactory + StatsReader + StateChangeWriter,
Provider: DBProvider
+ BlockReader
+ StaticFileProviderFactory
+ StatsReader
+ StateChangeWriter
+ ToLatestStateProviderRef,
for<'a> UnifiedStorageWriter<'a, Provider, StaticFileProviderRWRefMut<'a>>: StateWriter,
{
/// Return the id of the stage
Expand Down Expand Up @@ -219,10 +224,7 @@ where
None
};

let db = StateProviderDatabase(LatestStateProviderRef::new(
provider.tx_ref(),
provider.static_file_provider(),
));
let db = StateProviderDatabase(provider.latest());
let mut executor = self.executor_provider.batch_executor(db);
executor.set_tip(max_block);
executor.set_prune_modes(prune_modes);
Expand Down
3 changes: 2 additions & 1 deletion crates/stages/stages/src/stages/hashing_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ impl AccountHashingStage {
pub fn seed<
Tx: DbTx + DbTxMut + 'static,
Spec: Send + Sync + 'static + reth_chainspec::EthereumHardforks,
SC: Send + Sync + 'static,
>(
provider: &reth_provider::DatabaseProvider<Tx, Spec>,
provider: &reth_provider::DatabaseProvider<Tx, Spec, SC>,
opts: SeedOpts,
) -> Result<Vec<(alloy_primitives::Address, reth_primitives::Account)>, StageError> {
use alloy_primitives::U256;
Expand Down
10 changes: 8 additions & 2 deletions crates/stages/stages/src/test_utils/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use reth_stages_api::{
ExecInput, ExecOutput, Stage, StageError, StageExt, UnwindInput, UnwindOutput,
};
use reth_storage_errors::db::DatabaseError;
use reth_trie_db::MerklePatriciaTrie;
use tokio::sync::oneshot;

#[derive(thiserror::Error, Debug)]
Expand All @@ -20,8 +21,13 @@ pub(crate) enum TestRunnerError {

/// A generic test runner for stages.
pub(crate) trait StageTestRunner {
type S: Stage<DatabaseProvider<<TempDatabase<DatabaseEnv> as Database>::TXMut, ChainSpec>>
+ 'static;
type S: Stage<
DatabaseProvider<
<TempDatabase<DatabaseEnv> as Database>::TXMut,
ChainSpec,
MerklePatriciaTrie,
>,
> + 'static;

/// Return a reference to the database.
fn db(&self) -> &TestStageDB;
Expand Down
10 changes: 6 additions & 4 deletions crates/storage/provider/src/providers/blockchain_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ impl<N: ProviderNodeTypes> BlockchainProvider2<N> {
) -> ProviderResult<Vec<T>>
where
F: FnOnce(
&DatabaseProviderRO<N::DB, N::ChainSpec>,
&DatabaseProviderRO<N::DB, N::ChainSpec, N::StateCommitment>,
RangeInclusive<BlockNumber>,
&mut P,
) -> ProviderResult<Vec<T>>,
Expand Down Expand Up @@ -418,7 +418,7 @@ impl<N: ProviderNodeTypes> BlockchainProvider2<N> {
) -> ProviderResult<Vec<R>>
where
S: FnOnce(
DatabaseProviderRO<N::DB, N::ChainSpec>,
DatabaseProviderRO<N::DB, N::ChainSpec, N::StateCommitment>,
RangeInclusive<TxNumber>,
) -> ProviderResult<Vec<R>>,
M: Fn(RangeInclusive<usize>, Arc<BlockState>) -> ProviderResult<Vec<R>>,
Expand Down Expand Up @@ -516,7 +516,9 @@ impl<N: ProviderNodeTypes> BlockchainProvider2<N> {
fetch_from_block_state: M,
) -> ProviderResult<Option<R>>
where
S: FnOnce(DatabaseProviderRO<N::DB, N::ChainSpec>) -> ProviderResult<Option<R>>,
S: FnOnce(
DatabaseProviderRO<N::DB, N::ChainSpec, N::StateCommitment>,
) -> ProviderResult<Option<R>>,
M: Fn(usize, TxNumber, Arc<BlockState>) -> ProviderResult<Option<R>>,
{
// Order of instantiation matters. More information on:
Expand Down Expand Up @@ -585,7 +587,7 @@ impl<N: ProviderNodeTypes> BlockchainProvider2<N> {
fetch_from_block_state: M,
) -> ProviderResult<R>
where
S: FnOnce(DatabaseProviderRO<N::DB, N::ChainSpec>) -> ProviderResult<R>,
S: FnOnce(DatabaseProviderRO<N::DB, N::ChainSpec, N::StateCommitment>) -> ProviderResult<R>,
M: Fn(Arc<BlockState>) -> ProviderResult<R>,
{
let block_state = match id {
Expand Down
18 changes: 13 additions & 5 deletions crates/storage/provider/src/providers/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ impl<N: ProviderNodeTypes> ProviderFactory<N> {
/// This sets the [`PruneModes`] to [`None`], because they should only be relevant for writing
/// data.
#[track_caller]
pub fn provider(&self) -> ProviderResult<DatabaseProviderRO<N::DB, N::ChainSpec>> {
pub fn provider(
&self,
) -> ProviderResult<DatabaseProviderRO<N::DB, N::ChainSpec, N::StateCommitment>> {
Ok(DatabaseProvider::new(
self.db.tx()?,
self.chain_spec.clone(),
Expand All @@ -144,7 +146,9 @@ impl<N: ProviderNodeTypes> ProviderFactory<N> {
/// [`BlockHashReader`]. This may fail if the inner read/write database transaction fails to
/// open.
#[track_caller]
pub fn provider_rw(&self) -> ProviderResult<DatabaseProviderRW<N::DB, N::ChainSpec>> {
pub fn provider_rw(
&self,
) -> ProviderResult<DatabaseProviderRW<N::DB, N::ChainSpec, N::StateCommitment>> {
Ok(DatabaseProviderRW(DatabaseProvider::new_rw(
self.db.tx_mut()?,
self.chain_spec.clone(),
Expand All @@ -157,7 +161,10 @@ impl<N: ProviderNodeTypes> ProviderFactory<N> {
#[track_caller]
pub fn latest(&self) -> ProviderResult<StateProviderBox> {
trace!(target: "providers::db", "Returning latest state provider");
Ok(Box::new(LatestStateProvider::new(self.db.tx()?, self.static_file_provider())))
Ok(Box::new(LatestStateProvider::<_, N::StateCommitment>::new(
self.db.tx()?,
self.static_file_provider(),
)))
}

/// Storage provider for state at that given block
Expand Down Expand Up @@ -186,8 +193,9 @@ impl<N: ProviderNodeTypes> ProviderFactory<N> {

impl<N: ProviderNodeTypes> DatabaseProviderFactory for ProviderFactory<N> {
type DB = N::DB;
type Provider = DatabaseProvider<<N::DB as Database>::TX, N::ChainSpec>;
type ProviderRW = DatabaseProvider<<N::DB as Database>::TXMut, N::ChainSpec>;
type Provider = DatabaseProvider<<N::DB as Database>::TX, N::ChainSpec, N::StateCommitment>;
type ProviderRW =
DatabaseProvider<<N::DB as Database>::TXMut, N::ChainSpec, N::StateCommitment>;

fn database_provider_ro(&self) -> ProviderResult<Self::Provider> {
self.provider()
Expand Down
Loading

0 comments on commit 03ea70e

Please sign in to comment.