Skip to main content

reth_engine_tree/tree/
types.rs

1//! Shared types for blockchain tree validation.
2
3use crate::tree::error::InsertPayloadError;
4use alloy_eip7928::bal::{DecodedBal, RawBal};
5use alloy_eips::eip4895::Withdrawal;
6use alloy_primitives::B256;
7use reth_chain_state::{ExecutedBlock, ExecutionTimingStats};
8use reth_evm::{ConfigureEvm, EvmEnvFor};
9use reth_execution_cache::TxPoolPrewarmCacheSnapshot;
10use reth_primitives_traits::{BlockTy, NodePrimitives};
11use std::sync::Arc;
12
13/// EVM context required to execute a block.
14#[derive(Debug, Clone)]
15pub struct ExecutionEnv<Evm: ConfigureEvm> {
16    /// Evm environment.
17    pub evm_env: EvmEnvFor<Evm>,
18    /// Hash of the block being executed.
19    pub hash: B256,
20    /// Hash of the parent block.
21    pub parent_hash: B256,
22    /// State root of the parent block.
23    /// Used for sparse trie continuation: if the preserved trie's anchor matches this,
24    /// the trie can be reused directly.
25    pub parent_state_root: B256,
26    /// Number of transactions in the block.
27    /// Used to determine parallel worker count for prewarming.
28    pub transaction_count: usize,
29    /// Total gas used by all transactions in the block.
30    /// Used to adaptively select multiproof chunk size for optimal throughput.
31    pub gas_used: u64,
32    /// Withdrawals included in the block.
33    /// Used to generate prefetch targets for withdrawal addresses.
34    pub withdrawals: Option<Vec<Withdrawal>>,
35    /// Optional decoded BAL for the block.
36    /// Used to validate and optimize execution.
37    pub decoded_bal: Option<Arc<DecodedBal>>,
38    /// Latest completed txpool-prewarm snapshot for this block's parent state.
39    ///
40    /// Can be None if txpool prewarming is disabled or the snapshot is not ready for some reason.
41    pub txpool_snapshot: Option<TxPoolPrewarmCacheSnapshot>,
42}
43
44impl<Evm: ConfigureEvm> ExecutionEnv<Evm>
45where
46    EvmEnvFor<Evm>: Default,
47{
48    /// Creates a new [`ExecutionEnv`] with default values for testing.
49    #[cfg(any(test, feature = "test-utils"))]
50    pub fn test_default() -> Self {
51        Self {
52            evm_env: Default::default(),
53            hash: Default::default(),
54            parent_hash: Default::default(),
55            parent_state_root: Default::default(),
56            transaction_count: 0,
57            gas_used: 0,
58            withdrawals: None,
59            decoded_bal: None,
60            txpool_snapshot: None,
61        }
62    }
63}
64
65/// Result of block or payload validation.
66pub type ValidationOutcome<N, E = InsertPayloadError<BlockTy<N>>> = Result<ValidationOutput<N>, E>;
67
68/// Result type for block validation with optional timing stats.
69pub(crate) type InsertPayloadResult<N> =
70    Result<ValidationOutput<N>, InsertPayloadError<<N as NodePrimitives>::Block>>;
71
72/// Output of block or payload validation.
73#[derive(Clone, Debug)]
74pub struct ValidationOutput<N: NodePrimitives> {
75    /// The executed block produced by validation.
76    pub executed_block: ExecutedBlock<N>,
77    /// Optional execution timing stats collected during validation.
78    pub execution_timing_stats: Option<Box<ExecutionTimingStats>>,
79    /// Validated raw block access list carried by the payload.
80    pub raw_bal: Option<RawBal>,
81}
82
83impl<N: NodePrimitives> ValidationOutput<N> {
84    /// Creates a new validation output.
85    pub const fn new(
86        executed_block: ExecutedBlock<N>,
87        execution_timing_stats: Option<Box<ExecutionTimingStats>>,
88    ) -> Self {
89        Self { executed_block, execution_timing_stats, raw_bal: None }
90    }
91
92    /// Sets the validated raw block access list carried by the payload.
93    pub fn with_raw_bal(mut self, raw_bal: Option<RawBal>) -> Self {
94        self.raw_bal = raw_bal;
95        self
96    }
97}