reth_storage_api/
legacy.rs

1//! Traits used by the legacy execution engine.
2//!
3//! This module is scheduled for removal in the future.
4
5use alloy_eips::BlockNumHash;
6use alloy_primitives::{BlockHash, BlockNumber};
7use auto_impl::auto_impl;
8use reth_execution_types::ExecutionOutcome;
9use reth_storage_errors::provider::{ProviderError, ProviderResult};
10
11/// Blockchain trait provider that gives access to the blockchain state that is not yet committed
12/// (pending).
13pub trait BlockchainTreePendingStateProvider: Send + Sync {
14    /// Returns a state provider that includes all state changes of the given (pending) block hash.
15    ///
16    /// In other words, the state provider will return the state after all transactions of the given
17    /// hash have been executed.
18    fn pending_state_provider(
19        &self,
20        block_hash: BlockHash,
21    ) -> ProviderResult<Box<dyn FullExecutionDataProvider>> {
22        self.find_pending_state_provider(block_hash)
23            .ok_or(ProviderError::StateForHashNotFound(block_hash))
24    }
25
26    /// Returns state provider if a matching block exists.
27    fn find_pending_state_provider(
28        &self,
29        block_hash: BlockHash,
30    ) -> Option<Box<dyn FullExecutionDataProvider>>;
31}
32
33/// Provides data required for post-block execution.
34///
35/// This trait offers methods to access essential post-execution data, including the state changes
36/// in accounts and storage, as well as block hashes for both the pending and canonical chains.
37///
38/// The trait includes:
39/// * [`ExecutionOutcome`] - Captures all account and storage changes in the pending chain.
40/// * Block hashes - Provides access to the block hashes of both the pending chain and canonical
41///   blocks.
42#[auto_impl(&, Box)]
43pub trait ExecutionDataProvider: Send + Sync {
44    /// Return the execution outcome.
45    fn execution_outcome(&self) -> &ExecutionOutcome;
46    /// Return block hash by block number of pending or canonical chain.
47    fn block_hash(&self, block_number: BlockNumber) -> Option<BlockHash>;
48}
49
50impl ExecutionDataProvider for ExecutionOutcome {
51    fn execution_outcome(&self) -> &ExecutionOutcome {
52        self
53    }
54
55    /// Always returns [None] because we don't have any information about the block header.
56    fn block_hash(&self, _block_number: BlockNumber) -> Option<BlockHash> {
57        None
58    }
59}
60
61/// Fork data needed for execution on it.
62///
63/// It contains a canonical fork, the block on what pending chain was forked from.
64#[auto_impl(&, Box)]
65pub trait BlockExecutionForkProvider {
66    /// Return canonical fork, the block on what post state was forked from.
67    ///
68    /// Needed to create state provider.
69    fn canonical_fork(&self) -> BlockNumHash;
70}
71
72/// Provides comprehensive post-execution state data required for further execution.
73///
74/// This trait is used to create a state provider over the pending state and is a combination of
75/// [`ExecutionDataProvider`] and [`BlockExecutionForkProvider`].
76///
77/// The pending state includes:
78/// * `ExecutionOutcome`: Contains all changes to accounts and storage within the pending chain.
79/// * Block hashes: Represents hashes of both the pending chain and canonical blocks.
80/// * Canonical fork: Denotes the block from which the pending chain forked.
81pub trait FullExecutionDataProvider: ExecutionDataProvider + BlockExecutionForkProvider {}
82
83impl<T> FullExecutionDataProvider for T where T: ExecutionDataProvider + BlockExecutionForkProvider {}