Skip to main content

reth_execution_types/
execute.rs

1use alloc::sync::Arc;
2use alloy_primitives::{Address, B256, U256};
3use reth_primitives_traits::{Account, Block, Bytecode, RecoveredBlock};
4use revm::database::{states::BundleState, BundleAccount};
5
6pub use alloy_evm::block::BlockExecutionResult;
7
8/// [`BlockExecutionResult`] combined with state.
9#[derive(
10    Debug,
11    Clone,
12    PartialEq,
13    Eq,
14    derive_more::AsRef,
15    derive_more::AsMut,
16    derive_more::Deref,
17    derive_more::DerefMut,
18)]
19pub struct BlockExecutionOutput<T> {
20    /// All the receipts of the transactions in the block.
21    #[as_ref]
22    #[as_mut]
23    #[deref]
24    #[deref_mut]
25    pub result: BlockExecutionResult<T>,
26    /// The changed state of the block after execution.
27    pub state: BundleState,
28}
29
30impl<T> BlockExecutionOutput<T> {
31    /// Return bytecode if known.
32    pub fn bytecode(&self, code_hash: &B256) -> Option<Bytecode> {
33        self.state.bytecode(code_hash).map(Bytecode)
34    }
35
36    /// Get account if account is known.
37    pub fn account(&self, address: &Address) -> Option<Option<Account>> {
38        self.state.account(address).map(|a| a.info.as_ref().map(Into::into))
39    }
40
41    /// Returns the state [`BundleAccount`] for the given address.
42    pub fn account_state(&self, address: &Address) -> Option<&BundleAccount> {
43        self.state.account(address)
44    }
45
46    /// Get storage if value is known.
47    ///
48    /// This means that depending on status we can potentially return `U256::ZERO`.
49    pub fn storage(&self, address: &Address, storage_key: U256) -> Option<U256> {
50        self.state.storage(address, storage_key)
51    }
52}
53
54impl<T> Default for BlockExecutionOutput<T> {
55    fn default() -> Self {
56        Self {
57            result: BlockExecutionResult {
58                receipts: Default::default(),
59                requests: Default::default(),
60                gas_used: 0,
61                blob_gas_used: 0,
62            },
63            state: Default::default(),
64        }
65    }
66}
67
68/// A recovered block and its shared execution output.
69///
70/// The execution output contains the block's receipts.
71#[derive(Debug)]
72pub struct RecoveredBlockAndExecutionOutput<B: Block, R> {
73    block: Arc<RecoveredBlock<B>>,
74    execution_output: Arc<BlockExecutionOutput<R>>,
75}
76
77impl<B: Block, R> RecoveredBlockAndExecutionOutput<B, R> {
78    /// Creates a block with its execution output.
79    pub const fn new(
80        block: Arc<RecoveredBlock<B>>,
81        execution_output: Arc<BlockExecutionOutput<R>>,
82    ) -> Self {
83        Self { block, execution_output }
84    }
85
86    /// Returns the recovered block.
87    pub const fn block(&self) -> &Arc<RecoveredBlock<B>> {
88        &self.block
89    }
90
91    /// Returns the execution output.
92    pub const fn execution_output(&self) -> &Arc<BlockExecutionOutput<R>> {
93        &self.execution_output
94    }
95
96    /// Consumes the container and returns its shared parts.
97    pub fn into_parts(self) -> (Arc<RecoveredBlock<B>>, Arc<BlockExecutionOutput<R>>) {
98        (self.block, self.execution_output)
99    }
100}