kona_executor/db/
traits.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! Contains the [TrieDBProvider] trait for fetching EVM bytecode hash preimages as well as [Header]
//! preimages.

use alloc::string::String;
use alloy_consensus::Header;
use alloy_primitives::{Bytes, B256};
use kona_mpt::{TrieNode, TrieProvider};

/// The [TrieDBProvider] trait defines the synchronous interface for fetching EVM bytecode hash
/// preimages as well as [Header] preimages.
pub trait TrieDBProvider: TrieProvider {
    /// Fetches the preimage of the bytecode hash provided.
    ///
    /// ## Takes
    /// - `hash`: The hash of the bytecode.
    ///
    /// ## Returns
    /// - Ok(Bytes): The bytecode of the contract.
    /// - Err(anyhow::Error): If the bytecode hash could not be fetched.
    ///
    /// [TrieDB]: crate::TrieDB
    fn bytecode_by_hash(&self, code_hash: B256) -> Result<Bytes, Self::Error>;

    /// Fetches the preimage of [Header] hash provided.
    ///
    /// ## Takes
    /// - `hash`: The hash of the RLP-encoded [Header].
    ///
    /// ## Returns
    /// - Ok(Bytes): The [Header].
    /// - Err(anyhow::Error): If the [Header] could not be fetched.
    ///
    /// [TrieDB]: crate::TrieDB
    fn header_by_hash(&self, hash: B256) -> Result<Header, Self::Error>;
}

/// The default, no-op implementation of the [TrieDBProvider] trait, used for testing.
#[derive(Debug, Clone, Copy)]
pub struct NoopTrieDBProvider;

impl TrieProvider for NoopTrieDBProvider {
    type Error = String;

    fn trie_node_by_hash(&self, _key: B256) -> Result<TrieNode, Self::Error> {
        Ok(TrieNode::Empty)
    }
}

impl TrieDBProvider for NoopTrieDBProvider {
    fn bytecode_by_hash(&self, _code_hash: B256) -> Result<Bytes, Self::Error> {
        Ok(Bytes::default())
    }

    fn header_by_hash(&self, _hash: B256) -> Result<Header, Self::Error> {
        Ok(Header::default())
    }
}