reth_blockchain_tree/
bundle.rs

1//! [`ExecutionDataProvider`] implementations used by the tree.
2
3use alloy_eips::ForkBlock;
4use alloy_primitives::{BlockHash, BlockNumber};
5use reth_provider::{BlockExecutionForkProvider, ExecutionDataProvider, ExecutionOutcome};
6use std::collections::BTreeMap;
7
8/// Structure that combines references of required data to be a [`ExecutionDataProvider`].
9#[derive(Clone, Debug)]
10pub struct BundleStateDataRef<'a> {
11    /// The execution outcome after execution of one or more transactions and/or blocks.
12    pub execution_outcome: &'a ExecutionOutcome,
13    /// The blocks in the sidechain.
14    pub sidechain_block_hashes: &'a BTreeMap<BlockNumber, BlockHash>,
15    /// The blocks in the canonical chain.
16    pub canonical_block_hashes: &'a BTreeMap<BlockNumber, BlockHash>,
17    /// Canonical fork
18    pub canonical_fork: ForkBlock,
19}
20
21impl ExecutionDataProvider for BundleStateDataRef<'_> {
22    fn execution_outcome(&self) -> &ExecutionOutcome {
23        self.execution_outcome
24    }
25
26    fn block_hash(&self, block_number: BlockNumber) -> Option<BlockHash> {
27        let block_hash = self.sidechain_block_hashes.get(&block_number).copied();
28        if block_hash.is_some() {
29            return block_hash;
30        }
31
32        self.canonical_block_hashes.get(&block_number).copied()
33    }
34}
35
36impl BlockExecutionForkProvider for BundleStateDataRef<'_> {
37    fn canonical_fork(&self) -> ForkBlock {
38        self.canonical_fork
39    }
40}
41
42/// Structure that owns the relevant data needs to be a [`ExecutionDataProvider`]
43#[derive(Clone, Debug)]
44pub struct ExecutionData {
45    /// Execution outcome.
46    pub execution_outcome: ExecutionOutcome,
47    /// Parent block hashes needs for evm BLOCKHASH opcode.
48    /// NOTE: it does not mean that all hashes are there but all until finalized are there.
49    /// Other hashes can be obtained from provider
50    pub parent_block_hashes: BTreeMap<BlockNumber, BlockHash>,
51    /// Canonical block where state forked from.
52    pub canonical_fork: ForkBlock,
53}
54
55impl ExecutionDataProvider for ExecutionData {
56    fn execution_outcome(&self) -> &ExecutionOutcome {
57        &self.execution_outcome
58    }
59
60    fn block_hash(&self, block_number: BlockNumber) -> Option<BlockHash> {
61        self.parent_block_hashes.get(&block_number).copied()
62    }
63}
64
65impl BlockExecutionForkProvider for ExecutionData {
66    fn canonical_fork(&self) -> ForkBlock {
67        self.canonical_fork
68    }
69}