reth_blockchain_tree/
config.rs

1//! Blockchain tree configuration
2
3/// The configuration for the blockchain tree.
4#[derive(Clone, Copy, Debug)]
5pub struct BlockchainTreeConfig {
6    /// Number of blocks after the last finalized block that we are storing.
7    ///
8    /// It should be more than the finalization window for the canonical chain.
9    max_blocks_in_chain: u64,
10    /// The number of blocks that can be re-orged (finalization windows)
11    max_reorg_depth: u64,
12    /// The number of unconnected blocks that we are buffering
13    max_unconnected_blocks: u32,
14    /// Number of additional block hashes to save in blockchain tree. For `BLOCKHASH` EVM opcode we
15    /// need last 256 block hashes.
16    ///
17    /// The total number of block hashes retained in-memory will be
18    /// `max(additional_canonical_block_hashes, max_reorg_depth)`, and for Ethereum that would
19    /// be 256. It covers both number of blocks required for reorg, and number of blocks
20    /// required for `BLOCKHASH` EVM opcode.
21    num_of_additional_canonical_block_hashes: u64,
22}
23
24impl Default for BlockchainTreeConfig {
25    fn default() -> Self {
26        // The defaults for Ethereum mainnet
27        Self {
28            // Gasper allows reorgs of any length from 1 to 64.
29            max_reorg_depth: 64,
30            // This default is just an assumption. Has to be greater than the `max_reorg_depth`.
31            max_blocks_in_chain: 65,
32            // EVM requires that last 256 block hashes are available.
33            num_of_additional_canonical_block_hashes: 256,
34            // max unconnected blocks.
35            max_unconnected_blocks: 200,
36        }
37    }
38}
39
40impl BlockchainTreeConfig {
41    /// Create tree configuration.
42    pub fn new(
43        max_reorg_depth: u64,
44        max_blocks_in_chain: u64,
45        num_of_additional_canonical_block_hashes: u64,
46        max_unconnected_blocks: u32,
47    ) -> Self {
48        assert!(
49            max_reorg_depth <= max_blocks_in_chain,
50            "Side chain size should be more than finalization window"
51        );
52        Self {
53            max_blocks_in_chain,
54            max_reorg_depth,
55            num_of_additional_canonical_block_hashes,
56            max_unconnected_blocks,
57        }
58    }
59
60    /// Return the maximum reorg depth.
61    pub const fn max_reorg_depth(&self) -> u64 {
62        self.max_reorg_depth
63    }
64
65    /// Return the maximum number of blocks in one chain.
66    pub const fn max_blocks_in_chain(&self) -> u64 {
67        self.max_blocks_in_chain
68    }
69
70    /// Return number of additional canonical block hashes that we need to retain
71    /// in order to have enough information for EVM execution.
72    pub const fn num_of_additional_canonical_block_hashes(&self) -> u64 {
73        self.num_of_additional_canonical_block_hashes
74    }
75
76    /// Return total number of canonical hashes that we need to retain in order to have enough
77    /// information for reorg and EVM execution.
78    ///
79    /// It is calculated as the maximum of `max_reorg_depth` (which is the number of blocks required
80    /// for the deepest reorg possible according to the consensus protocol) and
81    /// `num_of_additional_canonical_block_hashes` (which is the number of block hashes needed to
82    /// satisfy the `BLOCKHASH` opcode in the EVM. See [`crate::BundleStateDataRef`]).
83    pub fn num_of_canonical_hashes(&self) -> u64 {
84        self.max_reorg_depth.max(self.num_of_additional_canonical_block_hashes)
85    }
86
87    /// Return max number of unconnected blocks that we are buffering
88    pub const fn max_unconnected_blocks(&self) -> u32 {
89        self.max_unconnected_blocks
90    }
91}