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

feat(mpt): Migrate to thiserror #541

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions bin/client/src/fault/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! [KonaHandleRegister]: kona_executor::KonaHandleRegister

use alloc::sync::Arc;
use kona_mpt::{TrieDB, TrieDBFetcher, TrieDBHinter};
use kona_mpt::{TrieDB, TrieHinter, TrieProvider};
use revm::{
handler::register::EvmHandler,
primitives::{spec_to_generic, SpecId},
Expand All @@ -20,8 +20,8 @@ mod kzg_point_eval;
pub(crate) fn fpvm_handle_register<F, H>(
handler: &mut EvmHandler<'_, (), &mut State<&mut TrieDB<F, H>>>,
) where
F: TrieDBFetcher,
H: TrieDBHinter,
F: TrieProvider,
H: TrieHinter,
{
let spec_id = handler.cfg.spec_id;

Expand Down
2 changes: 1 addition & 1 deletion bin/client/src/kona.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn main() -> Result<()> {

let mut executor = StatelessL2BlockExecutor::builder(&boot.rollup_config)
.with_parent_header(driver.take_l2_safe_head_header())
.with_fetcher(l2_provider.clone())
.with_provider(l2_provider.clone())
.with_hinter(l2_provider)
.with_handle_register(fpvm_handle_register)
.build()?;
Expand Down
10 changes: 6 additions & 4 deletions bin/client/src/l1/chain_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use alloy_rlp::Decodable;
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use kona_derive::traits::ChainProvider;
use kona_mpt::{OrderedListWalker, TrieDBFetcher};
use kona_mpt::{OrderedListWalker, TrieProvider};
use kona_preimage::{CommsClient, PreimageKey, PreimageKeyType};
use op_alloy_protocol::BlockInfo;

Expand Down Expand Up @@ -121,7 +121,9 @@ impl<T: CommsClient + Sync + Send> ChainProvider for OracleL1ChainProvider<T> {
}
}

impl<T: CommsClient> TrieDBFetcher for OracleL1ChainProvider<T> {
impl<T: CommsClient> TrieProvider for OracleL1ChainProvider<T> {
type Error = anyhow::Error;

fn trie_node_preimage(&self, key: B256) -> Result<Bytes> {
// On L1, trie node preimages are stored as keccak preimage types in the oracle. We assume
// that a hint for these preimages has already been sent, prior to this call.
Expand All @@ -134,10 +136,10 @@ impl<T: CommsClient> TrieDBFetcher for OracleL1ChainProvider<T> {
}

fn bytecode_by_hash(&self, _: B256) -> Result<Bytes> {
unimplemented!("TrieDBFetcher::bytecode_by_hash unimplemented for OracleL1ChainProvider")
unimplemented!("TrieProvider::bytecode_by_hash unimplemented for OracleL1ChainProvider")
}

fn header_by_hash(&self, _: B256) -> Result<Header> {
unimplemented!("TrieDBFetcher::header_by_hash unimplemented for OracleL1ChainProvider")
unimplemented!("TrieProvider::header_by_hash unimplemented for OracleL1ChainProvider")
}
}
2 changes: 1 addition & 1 deletion bin/client/src/l1/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use kona_derive::{
},
traits::{BlobProvider, ChainProvider, L2ChainProvider, OriginProvider},
};
use kona_mpt::TrieDBFetcher;
use kona_mpt::TrieProvider;
use kona_preimage::{CommsClient, PreimageKey, PreimageKeyType};
use op_alloy_protocol::{BlockInfo, L2BlockInfo};
use op_alloy_rpc_types_engine::OptimismAttributesWithParent;
Expand Down
10 changes: 7 additions & 3 deletions bin/client/src/l2/chain_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use alloy_rlp::Decodable;
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use kona_derive::traits::L2ChainProvider;
use kona_mpt::{OrderedListWalker, TrieDBFetcher, TrieDBHinter};
use kona_mpt::{OrderedListWalker, TrieHinter, TrieProvider};
use kona_preimage::{CommsClient, PreimageKey, PreimageKeyType};
use kona_primitives::{L2ExecutionPayloadEnvelope, OpBlock};
use op_alloy_consensus::OpTxEnvelope;
Expand Down Expand Up @@ -120,7 +120,9 @@ impl<T: CommsClient + Send + Sync> L2ChainProvider for OracleL2ChainProvider<T>
}
}

impl<T: CommsClient> TrieDBFetcher for OracleL2ChainProvider<T> {
impl<T: CommsClient> TrieProvider for OracleL2ChainProvider<T> {
type Error = anyhow::Error;

fn trie_node_preimage(&self, key: B256) -> Result<Bytes> {
// On L2, trie node preimages are stored as keccak preimage types in the oracle. We assume
// that a hint for these preimages has already been sent, prior to this call.
Expand Down Expand Up @@ -157,7 +159,9 @@ impl<T: CommsClient> TrieDBFetcher for OracleL2ChainProvider<T> {
}
}

impl<T: CommsClient> TrieDBHinter for OracleL2ChainProvider<T> {
impl<T: CommsClient> TrieHinter for OracleL2ChainProvider<T> {
type Error = anyhow::Error;

fn hint_trie_node(&self, hash: B256) -> Result<()> {
kona_common::block_on(async move {
self.oracle.write(&HintType::L2StateNode.encode_with(&[hash.as_slice()])).await
Expand Down
18 changes: 10 additions & 8 deletions crates/executor/benches/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ use alloy_rpc_types_engine::PayloadAttributes;
use anyhow::{anyhow, Result};
use criterion::{criterion_group, criterion_main, Bencher, Criterion};
use kona_executor::StatelessL2BlockExecutor;
use kona_mpt::{NoopTrieDBHinter, TrieDBFetcher};
use kona_mpt::{NoopTrieHinter, TrieProvider};
use op_alloy_genesis::{RollupConfig, OP_BASE_FEE_PARAMS, OP_CANYON_BASE_FEE_PARAMS};
use op_alloy_rpc_types_engine::OptimismPayloadAttributes;
use pprof::criterion::{Output, PProfProfiler};
use serde::Deserialize;
use std::collections::HashMap;

/// A [TrieDBFetcher] implementation that fetches trie nodes and bytecode from the local
/// A [TrieProvider] implementation that fetches trie nodes and bytecode from the local
/// testdata folder.
#[derive(Deserialize)]
struct TestdataTrieDBFetcher {
struct TestdataTrieProvider {
preimages: HashMap<B256, Bytes>,
}

impl TestdataTrieDBFetcher {
/// Constructs a new [TestdataTrieDBFetcher] with the given testdata folder.
impl TestdataTrieProvider {
/// Constructs a new [TestdataTrieProvider] with the given testdata folder.
pub(crate) fn new(testdata_folder: &str) -> Self {
let file_name = format!("testdata/{}/output.json", testdata_folder);
let preimages = serde_json::from_str::<HashMap<B256, Bytes>>(
Expand All @@ -33,7 +33,9 @@ impl TestdataTrieDBFetcher {
}
}

impl TrieDBFetcher for TestdataTrieDBFetcher {
impl TrieProvider for TestdataTrieProvider {
type Error = anyhow::Error;

fn trie_node_preimage(&self, key: B256) -> Result<Bytes> {
self.preimages
.get(&key)
Expand Down Expand Up @@ -79,8 +81,8 @@ fn op_mainnet_exec_bench(
bencher.iter(|| {
let mut l2_block_executor = StatelessL2BlockExecutor::builder(&rollup_config)
.with_parent_header(pre_state_header.clone().seal_slow())
.with_fetcher(TestdataTrieDBFetcher::new(data_folder))
.with_hinter(NoopTrieDBHinter)
.with_provider(TestdataTrieProvider::new(data_folder))
.with_hinter(NoopTrieHinter)
.build()
.unwrap();
l2_block_executor.execute_payload(payload_attrs.clone()).unwrap();
Expand Down
20 changes: 10 additions & 10 deletions crates/executor/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::StatelessL2BlockExecutor;
use alloy_consensus::{Header, Sealable, Sealed};
use anyhow::Result;
use kona_mpt::{TrieDB, TrieDBFetcher, TrieDBHinter};
use kona_mpt::{TrieDB, TrieHinter, TrieProvider};
use op_alloy_genesis::RollupConfig;
use revm::{db::State, handler::register::EvmHandler};

Expand All @@ -15,25 +15,25 @@ pub type KonaHandleRegister<F, H> =
#[derive(Debug)]
pub struct StatelessL2BlockExecutorBuilder<'a, F, H>
where
F: TrieDBFetcher,
H: TrieDBHinter,
F: TrieProvider,
H: TrieHinter,
{
/// The [RollupConfig].
config: &'a RollupConfig,
/// The parent [Header] to begin execution from.
parent_header: Option<Sealed<Header>>,
/// The [KonaHandleRegister] to use during execution.
handler_register: Option<KonaHandleRegister<F, H>>,
/// The [TrieDBFetcher] to fetch the state trie preimages.
/// The [TrieProvider] to fetch the state trie preimages.
fetcher: Option<F>,
/// The [TrieDBHinter] to hint the state trie preimages.
/// The [TrieHinter] to hint the state trie preimages.
hinter: Option<H>,
}

impl<'a, F, H> StatelessL2BlockExecutorBuilder<'a, F, H>
where
F: TrieDBFetcher,
H: TrieDBHinter,
F: TrieProvider,
H: TrieHinter,
{
/// Instantiate a new builder with the given [RollupConfig].
pub fn with_config(config: &'a RollupConfig) -> Self {
Expand All @@ -46,13 +46,13 @@ where
self
}

/// Set the [TrieDBFetcher] to fetch the state trie preimages.
pub fn with_fetcher(mut self, fetcher: F) -> Self {
/// Set the [TrieProvider] to fetch the state trie preimages.
pub fn with_provider(mut self, fetcher: F) -> Self {
self.fetcher = Some(fetcher);
self
}

/// Set the [TrieDBHinter] to hint the state trie preimages.
/// Set the [TrieHinter] to hint the state trie preimages.
pub fn with_hinter(mut self, hinter: H) -> Self {
self.hinter = Some(hinter);
self
Expand Down
6 changes: 3 additions & 3 deletions crates/executor/src/canyon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use alloy_primitives::{address, b256, hex, Address, Bytes, B256};
use anyhow::Result;
use kona_mpt::{TrieDB, TrieDBFetcher, TrieDBHinter};
use kona_mpt::{TrieDB, TrieHinter, TrieProvider};
use op_alloy_genesis::RollupConfig;
use revm::{
primitives::{Account, Bytecode, HashMap},
Expand All @@ -28,8 +28,8 @@ pub(crate) fn ensure_create2_deployer_canyon<F, H>(
timestamp: u64,
) -> Result<()>
where
F: TrieDBFetcher,
H: TrieDBHinter,
F: TrieProvider,
H: TrieHinter,
{
// If the canyon hardfork is active at the current timestamp, and it was not active at the
// previous block timestamp, then we need to force-deploy the create2 deployer contract.
Expand Down
48 changes: 25 additions & 23 deletions crates/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use alloy_consensus::{Header, Sealable, EMPTY_OMMER_ROOT_HASH, EMPTY_ROOT_HASH};
use alloy_eips::eip2718::{Decodable2718, Encodable2718};
use alloy_primitives::{address, keccak256, Address, Bytes, TxKind, B256, U256};
use anyhow::{anyhow, Result};
use kona_mpt::{ordered_trie_with_encoder, TrieDB, TrieDBFetcher, TrieDBHinter};
use kona_mpt::{ordered_trie_with_encoder, TrieDB, TrieHinter, TrieProvider};
use op_alloy_consensus::{OpReceiptEnvelope, OpTxEnvelope};
use op_alloy_genesis::RollupConfig;
use op_alloy_rpc_types_engine::OptimismPayloadAttributes;
Expand Down Expand Up @@ -43,8 +43,8 @@ use util::{extract_tx_gas_limit, is_system_transaction, logs_bloom, receipt_enve
#[derive(Debug)]
pub struct StatelessL2BlockExecutor<'a, F, H>
where
F: TrieDBFetcher,
H: TrieDBHinter,
F: TrieProvider,
H: TrieHinter,
{
/// The [RollupConfig].
config: &'a RollupConfig,
Expand All @@ -56,8 +56,8 @@ where

impl<'a, F, H> StatelessL2BlockExecutor<'a, F, H>
where
F: TrieDBFetcher,
H: TrieDBHinter,
F: TrieProvider,
H: TrieHinter,
{
/// Constructs a new [StatelessL2BlockExecutorBuilder] with the given [RollupConfig].
pub fn builder(config: &'a RollupConfig) -> StatelessL2BlockExecutorBuilder<'a, F, H> {
Expand Down Expand Up @@ -647,20 +647,20 @@ mod test {
use alloy_primitives::{address, b256, hex};
use alloy_rlp::Decodable;
use alloy_rpc_types_engine::PayloadAttributes;
use kona_mpt::NoopTrieDBHinter;
use kona_mpt::NoopTrieHinter;
use op_alloy_genesis::{OP_BASE_FEE_PARAMS, OP_CANYON_BASE_FEE_PARAMS};
use serde::Deserialize;
use std::{collections::HashMap, format};

/// A [TrieDBFetcher] implementation that fetches trie nodes and bytecode from the local
/// A [TrieProvider] implementation that fetches trie nodes and bytecode from the local
/// testdata folder.
#[derive(Deserialize)]
struct TestdataTrieDBFetcher {
struct TestdataTrieProvider {
preimages: HashMap<B256, Bytes>,
}

impl TestdataTrieDBFetcher {
/// Constructs a new [TestdataTrieDBFetcher] with the given testdata folder.
impl TestdataTrieProvider {
/// Constructs a new [TestdataTrieProvider] with the given testdata folder.
pub(crate) fn new(testdata_folder: &str) -> Self {
let file_name = format!("testdata/{}/output.json", testdata_folder);
let preimages = serde_json::from_str::<HashMap<B256, Bytes>>(
Expand All @@ -671,7 +671,9 @@ mod test {
}
}

impl TrieDBFetcher for TestdataTrieDBFetcher {
impl TrieProvider for TestdataTrieProvider {
type Error = anyhow::Error;

fn trie_node_preimage(&self, key: B256) -> Result<Bytes> {
self.preimages
.get(&key)
Expand Down Expand Up @@ -721,8 +723,8 @@ mod test {
// Initialize the block executor on block #120794431's post-state.
let mut l2_block_executor = StatelessL2BlockExecutor::builder(&rollup_config)
.with_parent_header(header.seal_slow())
.with_fetcher(TestdataTrieDBFetcher::new("block_120794432_exec"))
.with_hinter(NoopTrieDBHinter)
.with_provider(TestdataTrieProvider::new("block_120794432_exec"))
.with_hinter(NoopTrieHinter)
.build()
.unwrap();

Expand Down Expand Up @@ -778,8 +780,8 @@ mod test {
// Initialize the block executor on block #121049888's post-state.
let mut l2_block_executor = StatelessL2BlockExecutor::builder(&rollup_config)
.with_parent_header(parent_header.seal_slow())
.with_fetcher(TestdataTrieDBFetcher::new("block_121049889_exec"))
.with_hinter(NoopTrieDBHinter)
.with_provider(TestdataTrieProvider::new("block_121049889_exec"))
.with_hinter(NoopTrieHinter)
.build()
.unwrap();

Expand Down Expand Up @@ -839,8 +841,8 @@ mod test {
// Initialize the block executor on block #121003240's post-state.
let mut l2_block_executor = StatelessL2BlockExecutor::builder(&rollup_config)
.with_parent_header(parent_header.seal_slow())
.with_fetcher(TestdataTrieDBFetcher::new("block_121003241_exec"))
.with_hinter(NoopTrieDBHinter)
.with_provider(TestdataTrieProvider::new("block_121003241_exec"))
.with_hinter(NoopTrieHinter)
.build()
.unwrap();

Expand Down Expand Up @@ -907,8 +909,8 @@ mod test {
// Initialize the block executor on block #121057302's post-state.
let mut l2_block_executor = StatelessL2BlockExecutor::builder(&rollup_config)
.with_parent_header(parent_header.seal_slow())
.with_fetcher(TestdataTrieDBFetcher::new("block_121057303_exec"))
.with_hinter(NoopTrieDBHinter)
.with_provider(TestdataTrieProvider::new("block_121057303_exec"))
.with_hinter(NoopTrieHinter)
.build()
.unwrap();

Expand Down Expand Up @@ -969,8 +971,8 @@ mod test {
// Initialize the block executor on block #121057302's post-state.
let mut l2_block_executor = StatelessL2BlockExecutor::builder(&rollup_config)
.with_parent_header(parent_header.seal_slow())
.with_fetcher(TestdataTrieDBFetcher::new("block_121065789_exec"))
.with_hinter(NoopTrieDBHinter)
.with_provider(TestdataTrieProvider::new("block_121065789_exec"))
.with_hinter(NoopTrieHinter)
.build()
.unwrap();

Expand Down Expand Up @@ -1040,8 +1042,8 @@ mod test {
// Initialize the block executor on block #121135703's post-state.
let mut l2_block_executor = StatelessL2BlockExecutor::builder(&rollup_config)
.with_parent_header(parent_header.seal_slow())
.with_fetcher(TestdataTrieDBFetcher::new("block_121135704_exec"))
.with_hinter(NoopTrieDBHinter)
.with_provider(TestdataTrieProvider::new("block_121135704_exec"))
.with_hinter(NoopTrieHinter)
.build()
.unwrap();

Expand Down
3 changes: 2 additions & 1 deletion crates/mpt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ homepage.workspace = true

[dependencies]
# General
anyhow.workspace = true
tracing.workspace = true
thiserror.workspace = true

# Revm + Alloy
revm.workspace = true
Expand All @@ -22,6 +22,7 @@ alloy-trie.workspace = true

[dev-dependencies]
tokio.workspace = true
anyhow.workspace = true
reqwest.workspace = true
futures.workspace = true
tracing-subscriber.workspace = true
Expand Down
Loading