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