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