Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(mpt): mpt noop trait impls #649

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ ignore:
- "bin/"
- "crates/providers-alloy/src/alloy_providers.rs" # Flaky testing with online providers
- "crates/providers-alloy/src/beacon_client.rs" # Flaky testing with online providers
- "crates/mpt/src/noop.rs" # Uncovered noop implementations used downstream and by tests

# Make comments less noisy
comment:
Expand Down
47 changes: 1 addition & 46 deletions crates/mpt/src/fetcher.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Contains the [TrieProvider] trait for fetching trie node preimages, contract bytecode, and
//! headers.

use alloc::string::{String, ToString};
use alloc::string::ToString;
use alloy_consensus::Header;
use alloy_primitives::{Address, Bytes, B256, U256};
use core::fmt::Display;
Expand Down Expand Up @@ -93,48 +93,3 @@ pub trait TrieHinter {
block_number: u64,
) -> Result<(), Self::Error>;
}

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

impl TrieProvider for NoopTrieProvider {
type Error = String;

fn trie_node_preimage(&self, _key: B256) -> Result<Bytes, Self::Error> {
Ok(Bytes::new())
}

fn bytecode_by_hash(&self, _code_hash: B256) -> Result<Bytes, Self::Error> {
Ok(Bytes::new())
}

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

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

impl TrieHinter for NoopTrieHinter {
type Error = String;

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

fn hint_account_proof(&self, _address: Address, _block_number: u64) -> Result<(), Self::Error> {
Ok(())
}

fn hint_storage_proof(
&self,
_address: Address,
_slot: U256,
_block_number: u64,
) -> Result<(), Self::Error> {
Ok(())
}
}
5 changes: 4 additions & 1 deletion crates/mpt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ pub use errors::{
};

mod fetcher;
pub use fetcher::{NoopTrieHinter, NoopTrieProvider, TrieHinter, TrieProvider};
pub use fetcher::{TrieHinter, TrieProvider};

mod node;
pub use node::TrieNode;

mod list_walker;
pub use list_walker::OrderedListWalker;

mod noop;
pub use noop::{NoopTrieHinter, NoopTrieProvider};

mod util;
pub use util::ordered_trie_with_encoder;

Expand Down
4 changes: 2 additions & 2 deletions crates/mpt/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,8 @@ impl Decodable for TrieNode {
mod test {
use super::*;
use crate::{
fetcher::NoopTrieProvider, ordered_trie_with_encoder, test_util::TrieNodeProvider,
NoopTrieHinter, TrieNode,
ordered_trie_with_encoder, test_util::TrieNodeProvider, NoopTrieHinter, NoopTrieProvider,
TrieNode,
};
use alloc::{collections::BTreeMap, vec, vec::Vec};
use alloy_primitives::{b256, bytes, hex, keccak256};
Expand Down
52 changes: 52 additions & 0 deletions crates/mpt/src/noop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//! Trait implementations for `kona-mpt` traits that are effectively a no-op.
//! Providers trait implementations for downstream users who do not require hinting.

use crate::{TrieHinter, TrieProvider};
use alloc::string::String;
use alloy_consensus::Header;
use alloy_primitives::{Address, Bytes, B256, U256};

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

impl TrieProvider for NoopTrieProvider {
type Error = String;

fn trie_node_preimage(&self, _key: B256) -> Result<Bytes, Self::Error> {
Ok(Bytes::new())
}

fn bytecode_by_hash(&self, _code_hash: B256) -> Result<Bytes, Self::Error> {
Ok(Bytes::new())
}

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

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

impl TrieHinter for NoopTrieHinter {
type Error = String;

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

fn hint_account_proof(&self, _address: Address, _block_number: u64) -> Result<(), Self::Error> {
Ok(())
}

fn hint_storage_proof(
&self,
_address: Address,
_slot: U256,
_block_number: u64,
) -> Result<(), Self::Error> {
Ok(())
}
}