Skip to main content

reth_engine_tree/tree/
types.rs

1//! Shared types for blockchain tree validation.
2
3use crate::tree::{error::InsertPayloadError, payload_processor::reuse::PoolSeeds};
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, HaltReasonFor};
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    /// Results of pool transactions executed on top of the parent state that prewarming may
41    /// reuse.
42    ///
43    /// None if txpool prewarming does not record results or the block environment differs from
44    /// the predicted one.
45    pub(crate) pool_seeds: Option<PoolSeeds<HaltReasonFor<Evm>>>,
46}
47
48impl<Evm: ConfigureEvm> ExecutionEnv<Evm>
49where
50    EvmEnvFor<Evm>: Default,
51{
52    /// Creates a new [`ExecutionEnv`] with default values for testing.
53    #[cfg(any(test, feature = "test-utils"))]
54    pub fn test_default() -> Self {
55        Self {
56            evm_env: Default::default(),
57            hash: Default::default(),
58            parent_hash: Default::default(),
59            parent_state_root: Default::default(),
60            transaction_count: 0,
61            gas_used: 0,
62            withdrawals: None,
63            decoded_bal: None,
64            txpool_snapshot: None,
65            pool_seeds: None,
66        }
67    }
68}
69
70/// Result of block or payload validation.
71pub type ValidationOutcome<N, E = InsertPayloadError<BlockTy<N>>> = Result<ValidationOutput<N>, E>;
72
73/// Result type for block validation with optional timing stats.
74pub(crate) type InsertPayloadResult<N> =
75    Result<ValidationOutput<N>, InsertPayloadError<<N as NodePrimitives>::Block>>;
76
77/// Output of block or payload validation.
78#[derive(Clone, Debug)]
79pub struct ValidationOutput<N: NodePrimitives> {
80    /// The executed block produced by validation.
81    pub executed_block: ExecutedBlock<N>,
82    /// Optional execution timing stats collected during validation.
83    pub execution_timing_stats: Option<Box<ExecutionTimingStats>>,
84}
85
86impl<N: NodePrimitives> ValidationOutput<N> {
87    /// Creates a new validation output.
88    pub const fn new(
89        executed_block: ExecutedBlock<N>,
90        execution_timing_stats: Option<Box<ExecutionTimingStats>>,
91    ) -> Self {
92        Self { executed_block, execution_timing_stats }
93    }
94}