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;
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, used as the base state for execution.
23    pub parent_state_root: B256,
24    /// Number of transactions in the block.
25    /// Used to determine parallel worker count for prewarming.
26    pub transaction_count: usize,
27    /// Total gas used by all transactions in the block.
28    /// Used to adaptively select multiproof chunk size for optimal throughput.
29    pub gas_used: u64,
30    /// Withdrawals included in the block.
31    /// Used to generate prefetch targets for withdrawal addresses.
32    pub withdrawals: Option<Vec<Withdrawal>>,
33    /// Optional decoded BAL for the block.
34    /// Used to validate and optimize execution.
35    pub decoded_bal: Option<Arc<DecodedBal>>,
36    /// Latest completed txpool-prewarm snapshot for this block's parent state.
37    ///
38    /// Can be None if txpool prewarming is disabled or the snapshot is not ready for some reason.
39    pub txpool_snapshot: Option<TxPoolPrewarmCacheSnapshot>,
40}
41
42impl<Evm: ConfigureEvm> ExecutionEnv<Evm>
43where
44    EvmEnvFor<Evm>: Default,
45{
46    /// Creates a new [`ExecutionEnv`] with default values for testing.
47    #[cfg(any(test, feature = "test-utils"))]
48    pub fn test_default() -> Self {
49        Self {
50            evm_env: Default::default(),
51            hash: Default::default(),
52            parent_hash: Default::default(),
53            parent_state_root: Default::default(),
54            transaction_count: 0,
55            gas_used: 0,
56            withdrawals: None,
57            decoded_bal: None,
58            txpool_snapshot: None,
59        }
60    }
61}
62
63/// Result of block or payload validation.
64pub type ValidationOutcome<N, E = InsertPayloadError<BlockTy<N>>> = Result<ValidationOutput<N>, E>;
65
66/// Result type for block validation with optional timing stats.
67pub(crate) type InsertPayloadResult<N> =
68    Result<ValidationOutput<N>, InsertPayloadError<<N as NodePrimitives>::Block>>;
69
70/// Output of block or payload validation.
71#[derive(Clone, Debug)]
72pub struct ValidationOutput<N: NodePrimitives> {
73    /// The executed block produced by validation.
74    pub executed_block: ExecutedBlock<N>,
75    /// Optional execution timing stats collected during validation.
76    pub execution_timing_stats: Option<Box<ExecutionTimingStats>>,
77}
78
79impl<N: NodePrimitives> ValidationOutput<N> {
80    /// Creates a new validation output.
81    pub const fn new(
82        executed_block: ExecutedBlock<N>,
83        execution_timing_stats: Option<Box<ExecutionTimingStats>>,
84    ) -> Self {
85        Self { executed_block, execution_timing_stats }
86    }
87}