kona_executor/db/traits.rs
1//! Contains the [TrieDBProvider] trait for fetching EVM bytecode hash preimages as well as [Header]
2//! preimages.
3
4use alloc::string::String;
5use alloy_consensus::Header;
6use alloy_primitives::{B256, Bytes};
7use kona_mpt::{TrieNode, TrieProvider};
8
9/// The [TrieDBProvider] trait defines the synchronous interface for fetching EVM bytecode hash
10/// preimages as well as [Header] preimages.
11pub trait TrieDBProvider: TrieProvider {
12 /// Fetches the preimage of the bytecode hash provided.
13 ///
14 /// ## Takes
15 /// - `hash`: The hash of the bytecode.
16 ///
17 /// ## Returns
18 /// - Ok(Bytes): The bytecode of the contract.
19 /// - Err(Self::Error): If the bytecode hash could not be fetched.
20 ///
21 /// [TrieDB]: crate::TrieDB
22 fn bytecode_by_hash(&self, code_hash: B256) -> Result<Bytes, Self::Error>;
23
24 /// Fetches the preimage of [Header] hash provided.
25 ///
26 /// ## Takes
27 /// - `hash`: The hash of the RLP-encoded [Header].
28 ///
29 /// ## Returns
30 /// - Ok(Bytes): The [Header].
31 /// - Err(Self::Error): If the [Header] could not be fetched.
32 ///
33 /// [TrieDB]: crate::TrieDB
34 fn header_by_hash(&self, hash: B256) -> Result<Header, Self::Error>;
35}
36
37/// The default, no-op implementation of the [TrieDBProvider] trait, used for testing.
38#[derive(Debug, Clone, Copy)]
39pub struct NoopTrieDBProvider;
40
41impl TrieProvider for NoopTrieDBProvider {
42 type Error = String;
43
44 fn trie_node_by_hash(&self, _key: B256) -> Result<TrieNode, Self::Error> {
45 Ok(TrieNode::Empty)
46 }
47}
48
49impl TrieDBProvider for NoopTrieDBProvider {
50 fn bytecode_by_hash(&self, _code_hash: B256) -> Result<Bytes, Self::Error> {
51 Ok(Bytes::default())
52 }
53
54 fn header_by_hash(&self, _hash: B256) -> Result<Header, Self::Error> {
55 Ok(Header::default())
56 }
57}