Skip to content

Commit

Permalink
chore: move traits used by legacy engine to separate mod
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Nov 22, 2024
1 parent 87ecb43 commit 4090124
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 77 deletions.
83 changes: 83 additions & 0 deletions crates/storage/storage-api/src/legacy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//! Traits used by the legacy execution engine.
//!
//! This module is scheduled for removal in the future.

use alloy_eips::BlockNumHash;
use alloy_primitives::{BlockHash, BlockNumber};
use auto_impl::auto_impl;
use reth_execution_types::ExecutionOutcome;
use reth_storage_errors::provider::{ProviderError, ProviderResult};

/// Blockchain trait provider that gives access to the blockchain state that is not yet committed
/// (pending).
pub trait BlockchainTreePendingStateProvider: Send + Sync {
/// Returns a state provider that includes all state changes of the given (pending) block hash.
///
/// In other words, the state provider will return the state after all transactions of the given
/// hash have been executed.
fn pending_state_provider(
&self,
block_hash: BlockHash,
) -> ProviderResult<Box<dyn FullExecutionDataProvider>> {
self.find_pending_state_provider(block_hash)
.ok_or(ProviderError::StateForHashNotFound(block_hash))
}

/// Returns state provider if a matching block exists.
fn find_pending_state_provider(
&self,
block_hash: BlockHash,
) -> Option<Box<dyn FullExecutionDataProvider>>;
}

/// Provides data required for post-block execution.
///
/// This trait offers methods to access essential post-execution data, including the state changes
/// in accounts and storage, as well as block hashes for both the pending and canonical chains.
///
/// The trait includes:
/// * [`ExecutionOutcome`] - Captures all account and storage changes in the pending chain.
/// * Block hashes - Provides access to the block hashes of both the pending chain and canonical
/// blocks.
#[auto_impl(&, Box)]
pub trait ExecutionDataProvider: Send + Sync {
/// Return the execution outcome.
fn execution_outcome(&self) -> &ExecutionOutcome;
/// Return block hash by block number of pending or canonical chain.
fn block_hash(&self, block_number: BlockNumber) -> Option<BlockHash>;
}

impl ExecutionDataProvider for ExecutionOutcome {
fn execution_outcome(&self) -> &ExecutionOutcome {
self
}

/// Always returns [None] because we don't have any information about the block header.
fn block_hash(&self, _block_number: BlockNumber) -> Option<BlockHash> {
None
}
}

/// Fork data needed for execution on it.
///
/// It contains a canonical fork, the block on what pending chain was forked from.
#[auto_impl(&, Box)]
pub trait BlockExecutionForkProvider {
/// Return canonical fork, the block on what post state was forked from.
///
/// Needed to create state provider.
fn canonical_fork(&self) -> BlockNumHash;
}

/// Provides comprehensive post-execution state data required for further execution.
///
/// This trait is used to create a state provider over the pending state and is a combination of
/// [`ExecutionDataProvider`] and [`BlockExecutionForkProvider`].
///
/// The pending state includes:
/// * `ExecutionOutcome`: Contains all changes to accounts and storage within the pending chain.
/// * Block hashes: Represents hashes of both the pending chain and canonical blocks.
/// * Canonical fork: Denotes the block from which the pending chain forked.
pub trait FullExecutionDataProvider: ExecutionDataProvider + BlockExecutionForkProvider {}

impl<T> FullExecutionDataProvider for T where T: ExecutionDataProvider + BlockExecutionForkProvider {}
3 changes: 3 additions & 0 deletions crates/storage/storage-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,6 @@ mod hashing;
pub use hashing::*;
mod stats;
pub use stats::*;

mod legacy;
pub use legacy::*;
79 changes: 2 additions & 77 deletions crates/storage/storage-api/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ use super::{
StorageRootProvider,
};
use alloy_consensus::constants::KECCAK_EMPTY;
use alloy_eips::{BlockId, BlockNumHash, BlockNumberOrTag};
use alloy_eips::{BlockId, BlockNumberOrTag};
use alloy_primitives::{Address, BlockHash, BlockNumber, StorageKey, StorageValue, B256, U256};
use auto_impl::auto_impl;
use reth_execution_types::ExecutionOutcome;
use reth_primitives::Bytecode;
use reth_storage_errors::provider::{ProviderError, ProviderResult};
use reth_storage_errors::provider::ProviderResult;

/// Type alias of boxed [`StateProvider`].
pub type StateProviderBox = Box<dyn StateProvider>;
Expand Down Expand Up @@ -167,77 +166,3 @@ pub trait StateProviderFactory: BlockIdReader + Send + Sync {
/// If the block couldn't be found, returns `None`.
fn pending_state_by_hash(&self, block_hash: B256) -> ProviderResult<Option<StateProviderBox>>;
}

/// Blockchain trait provider that gives access to the blockchain state that is not yet committed
/// (pending).
pub trait BlockchainTreePendingStateProvider: Send + Sync {
/// Returns a state provider that includes all state changes of the given (pending) block hash.
///
/// In other words, the state provider will return the state after all transactions of the given
/// hash have been executed.
fn pending_state_provider(
&self,
block_hash: BlockHash,
) -> ProviderResult<Box<dyn FullExecutionDataProvider>> {
self.find_pending_state_provider(block_hash)
.ok_or(ProviderError::StateForHashNotFound(block_hash))
}

/// Returns state provider if a matching block exists.
fn find_pending_state_provider(
&self,
block_hash: BlockHash,
) -> Option<Box<dyn FullExecutionDataProvider>>;
}

/// Provides data required for post-block execution.
///
/// This trait offers methods to access essential post-execution data, including the state changes
/// in accounts and storage, as well as block hashes for both the pending and canonical chains.
///
/// The trait includes:
/// * [`ExecutionOutcome`] - Captures all account and storage changes in the pending chain.
/// * Block hashes - Provides access to the block hashes of both the pending chain and canonical
/// blocks.
#[auto_impl(&, Box)]
pub trait ExecutionDataProvider: Send + Sync {
/// Return the execution outcome.
fn execution_outcome(&self) -> &ExecutionOutcome;
/// Return block hash by block number of pending or canonical chain.
fn block_hash(&self, block_number: BlockNumber) -> Option<BlockHash>;
}

impl ExecutionDataProvider for ExecutionOutcome {
fn execution_outcome(&self) -> &ExecutionOutcome {
self
}

/// Always returns [None] because we don't have any information about the block header.
fn block_hash(&self, _block_number: BlockNumber) -> Option<BlockHash> {
None
}
}

/// Fork data needed for execution on it.
///
/// It contains a canonical fork, the block on what pending chain was forked from.
#[auto_impl(&, Box)]
pub trait BlockExecutionForkProvider {
/// Return canonical fork, the block on what post state was forked from.
///
/// Needed to create state provider.
fn canonical_fork(&self) -> BlockNumHash;
}

/// Provides comprehensive post-execution state data required for further execution.
///
/// This trait is used to create a state provider over the pending state and is a combination of
/// [`ExecutionDataProvider`] and [`BlockExecutionForkProvider`].
///
/// The pending state includes:
/// * `ExecutionOutcome`: Contains all changes to accounts and storage within the pending chain.
/// * Block hashes: Represents hashes of both the pending chain and canonical blocks.
/// * Canonical fork: Denotes the block from which the pending chain forked.
pub trait FullExecutionDataProvider: ExecutionDataProvider + BlockExecutionForkProvider {}

impl<T> FullExecutionDataProvider for T where T: ExecutionDataProvider + BlockExecutionForkProvider {}

0 comments on commit 4090124

Please sign in to comment.