Skip to content

Commit

Permalink
feat(mpt): Extend TrieProvider in kona-executor (#813)
Browse files Browse the repository at this point in the history
* reorg common

* lint

* feat(mpt): Extend `TrieProvider` in `kona-executor`

* test fix

* lint
  • Loading branch information
clabby authored Nov 14, 2024
1 parent 6ac0642 commit 6e333b3
Show file tree
Hide file tree
Showing 21 changed files with 143 additions and 119 deletions.
16 changes: 8 additions & 8 deletions bin/client/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ use alloc::sync::Arc;
use alloy_consensus::{Header, Sealed};
use alloy_primitives::B256;
use kona_driver::{Executor, ExecutorConstructor};
use kona_executor::{KonaHandleRegister, StatelessL2BlockExecutor};
use kona_mpt::{TrieHinter, TrieProvider};
use kona_executor::{KonaHandleRegister, StatelessL2BlockExecutor, TrieDBProvider};
use kona_mpt::TrieHinter;
use op_alloy_genesis::RollupConfig;
use op_alloy_rpc_types_engine::OpPayloadAttributes;

/// An executor wrapper type.
#[derive(Debug)]
pub struct KonaExecutor<'a, P, H>(StatelessL2BlockExecutor<'a, P, H>)
where
P: TrieProvider + Send + Sync + Clone,
P: TrieDBProvider + Send + Sync + Clone,
H: TrieHinter + Send + Sync + Clone;

impl<'a, P, H> KonaExecutor<'a, P, H>
where
P: TrieProvider + Send + Sync + Clone,
P: TrieDBProvider + Send + Sync + Clone,
H: TrieHinter + Send + Sync + Clone,
{
/// Creates a new executor.
Expand All @@ -29,7 +29,7 @@ where

impl<P, H> Executor for KonaExecutor<'_, P, H>
where
P: TrieProvider + Send + Sync + Clone,
P: TrieDBProvider + Send + Sync + Clone,
H: TrieHinter + Send + Sync + Clone,
{
type Error = kona_executor::ExecutorError;
Expand All @@ -49,7 +49,7 @@ where
#[derive(Debug)]
pub struct KonaExecutorConstructor<'a, P, H>
where
P: TrieProvider + Send + Sync + Clone,
P: TrieDBProvider + Send + Sync + Clone,
H: TrieHinter + Send + Sync + Clone,
{
/// The rollup config for the executor.
Expand All @@ -64,7 +64,7 @@ where

impl<'a, P, H> KonaExecutorConstructor<'a, P, H>
where
P: TrieProvider + Send + Sync + Clone,
P: TrieDBProvider + Send + Sync + Clone,
H: TrieHinter + Send + Sync + Clone,
{
/// Creates a new executor constructor.
Expand All @@ -80,7 +80,7 @@ where

impl<'a, P, H> ExecutorConstructor<KonaExecutor<'a, P, H>> for KonaExecutorConstructor<'a, P, H>
where
P: TrieProvider + Send + Sync + Clone,
P: TrieDBProvider + Send + Sync + Clone,
H: TrieHinter + Send + Sync + Clone,
{
/// Constructs the executor.
Expand Down
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,8 +3,8 @@
//! [KonaHandleRegister]: kona_executor::KonaHandleRegister
use alloc::sync::Arc;
use kona_executor::TrieDB;
use kona_mpt::{TrieHinter, TrieProvider};
use kona_executor::{TrieDB, TrieDBProvider};
use kona_mpt::TrieHinter;
use revm::{
handler::register::EvmHandler,
primitives::{spec_to_generic, SpecId},
Expand All @@ -21,7 +21,7 @@ mod kzg_point_eval;
pub(crate) fn fpvm_handle_register<F, H>(
handler: &mut EvmHandler<'_, (), &mut State<&mut TrieDB<F, H>>>,
) where
F: TrieProvider,
F: TrieDBProvider,
H: TrieHinter,
{
let spec_id = handler.cfg.spec_id;
Expand Down
10 changes: 1 addition & 9 deletions bin/client/src/l1/chain_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{errors::OracleProviderError, BootInfo, HintType};
use alloc::{boxed::Box, sync::Arc, vec::Vec};
use alloy_consensus::{Header, Receipt, ReceiptEnvelope, TxEnvelope};
use alloy_eips::eip2718::Decodable2718;
use alloy_primitives::{Bytes, B256};
use alloy_primitives::B256;
use alloy_rlp::Decodable;
use async_trait::async_trait;
use kona_derive::traits::ChainProvider;
Expand Down Expand Up @@ -153,12 +153,4 @@ impl<T: CommsClient> TrieProvider for OracleL1ChainProvider<T> {
.map_err(OracleProviderError::Rlp)
})
}

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

fn header_by_hash(&self, _: B256) -> Result<Header, Self::Error> {
unimplemented!("TrieProvider::header_by_hash unimplemented for OracleL1ChainProvider")
}
}
3 changes: 3 additions & 0 deletions bin/client/src/l2/chain_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use alloy_primitives::{Address, Bytes, B256};
use alloy_rlp::Decodable;
use async_trait::async_trait;
use kona_derive::traits::L2ChainProvider;
use kona_executor::TrieDBProvider;
use kona_mpt::{OrderedListWalker, TrieHinter, TrieNode, TrieProvider};
use kona_preimage::{CommsClient, PreimageKey, PreimageKeyType};
use op_alloy_consensus::{OpBlock, OpTxEnvelope};
Expand Down Expand Up @@ -159,7 +160,9 @@ impl<T: CommsClient> TrieProvider for OracleL2ChainProvider<T> {
.map_err(OracleProviderError::Rlp)
})
}
}

impl<T: CommsClient> TrieDBProvider for OracleL2ChainProvider<T> {
fn bytecode_by_hash(&self, hash: B256) -> Result<Bytes, OracleProviderError> {
// Fetch the bytecode preimage from the caching oracle.
kona_common::block_on(async move {
Expand Down
2 changes: 1 addition & 1 deletion bin/client/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use alloy_consensus::Sealed;
use core::fmt::Debug;
use kona_derive::traits::ChainProvider;
use kona_driver::{PipelineCursor, TipCursor};
use kona_mpt::TrieProvider;
use kona_executor::TrieDBProvider;
use kona_preimage::{CommsClient, PreimageKey, PreimageKeyType};
use op_alloy_protocol::BatchValidationProvider;

Expand Down
4 changes: 3 additions & 1 deletion crates/executor/benches/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use alloy_rlp::Decodable;
use alloy_rpc_types_engine::PayloadAttributes;
use anyhow::{anyhow, Result};
use criterion::{criterion_group, criterion_main, Bencher, Criterion};
use kona_executor::StatelessL2BlockExecutor;
use kona_executor::{StatelessL2BlockExecutor, TrieDBProvider};
use kona_mpt::{NoopTrieHinter, TrieNode, TrieProvider};
use op_alloy_genesis::{RollupConfig, OP_MAINNET_BASE_FEE_PARAMS};
use op_alloy_rpc_types_engine::OpPayloadAttributes;
Expand Down Expand Up @@ -48,7 +48,9 @@ impl TrieProvider for TestdataTrieProvider {
)
.map_err(Into::into)
}
}

impl TrieDBProvider for TestdataTrieProvider {
fn bytecode_by_hash(&self, code_hash: B256) -> Result<Bytes> {
self.preimages
.get(&code_hash)
Expand Down
25 changes: 14 additions & 11 deletions crates/executor/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use alloc::{string::ToString, vec::Vec};
use alloy_consensus::{Header, Sealed, EMPTY_ROOT_HASH};
use alloy_primitives::{keccak256, Address, B256, U256};
use alloy_rlp::{Decodable, Encodable};
use kona_mpt::{Nibbles, TrieHinter, TrieNode, TrieNodeError, TrieProvider};
use kona_mpt::{Nibbles, TrieHinter, TrieNode, TrieNodeError};
use revm::{
db::{states::StorageSlot, BundleState},
primitives::{AccountInfo, Bytecode, HashMap, BLOCK_HASH_HISTORY},
Expand All @@ -17,6 +17,9 @@ use tracing::debug;
mod account;
pub use account::TrieAccount;

mod traits;
pub use traits::{NoopTrieDBProvider, TrieDBProvider};

/// A Trie DB that caches open state in-memory.
///
/// When accounts that don't already exist within the cached [TrieNode] are queried, the database
Expand Down Expand Up @@ -47,8 +50,8 @@ pub use account::TrieAccount;
/// use alloy_consensus::{Header, Sealable};
/// use alloy_primitives::{Bytes, B256};
/// use anyhow::Result;
/// use kona_executor::TrieDB;
/// use kona_mpt::{NoopTrieHinter, NoopTrieProvider};
/// use kona_executor::{NoopTrieDBProvider, TrieDB};
/// use kona_mpt::NoopTrieHinter;
/// use revm::{db::states::bundle_state::BundleRetention, EvmBuilder, StateBuilder};
///
/// let mock_starting_root = B256::default();
Expand All @@ -57,7 +60,7 @@ pub use account::TrieAccount;
/// let trie_db = TrieDB::new(
/// mock_starting_root,
/// mock_parent_block_header.seal_slow(),
/// NoopTrieProvider,
/// NoopTrieDBProvider,
/// NoopTrieHinter,
/// );
/// let mut state = StateBuilder::new_with_database(trie_db).with_bundle_update().build();
Expand All @@ -77,7 +80,7 @@ pub use account::TrieAccount;
#[derive(Debug, Clone)]
pub struct TrieDB<F, H>
where
F: TrieProvider,
F: TrieDBProvider,
H: TrieHinter,
{
/// The [TrieNode] representation of the root node.
Expand All @@ -86,15 +89,15 @@ where
storage_roots: HashMap<Address, TrieNode>,
/// The parent block hash of the current block.
parent_block_header: Sealed<Header>,
/// The [TrieProvider]
/// The [TrieDBProvider]
fetcher: F,
/// The [TrieHinter]
hinter: H,
}

impl<F, H> TrieDB<F, H>
where
F: TrieProvider,
F: TrieDBProvider,
H: TrieHinter,
{
/// Creates a new [TrieDB] with the given root node.
Expand Down Expand Up @@ -294,7 +297,7 @@ where

impl<F, H> Database for TrieDB<F, H>
where
F: TrieProvider,
F: TrieDBProvider,
H: TrieHinter,
{
type Error = TrieDBError;
Expand Down Expand Up @@ -387,13 +390,13 @@ mod tests {
use super::*;
use alloy_consensus::Sealable;
use alloy_primitives::b256;
use kona_mpt::{NoopTrieHinter, NoopTrieProvider};
use kona_mpt::NoopTrieHinter;

fn new_test_db() -> TrieDB<NoopTrieProvider, NoopTrieHinter> {
fn new_test_db() -> TrieDB<NoopTrieDBProvider, NoopTrieHinter> {
TrieDB::new(
B256::default(),
Header::default().seal_slow(),
NoopTrieProvider,
NoopTrieDBProvider,
NoopTrieHinter,
)
}
Expand Down
57 changes: 57 additions & 0 deletions crates/executor/src/db/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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())
}
}
17 changes: 9 additions & 8 deletions crates/executor/src/executor/builder.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Contains the builder pattern for the [StatelessL2BlockExecutor].
use super::StatelessL2BlockExecutor;
use crate::db::TrieDB;
use crate::db::{TrieDB, TrieDBProvider};
use alloy_consensus::{Header, Sealable, Sealed};
use kona_mpt::{TrieHinter, TrieProvider};
use kona_mpt::TrieHinter;
use op_alloy_genesis::RollupConfig;
use revm::{db::State, handler::register::EvmHandler};

Expand All @@ -15,12 +15,12 @@ pub type KonaHandleRegister<F, H> =
#[derive(Debug)]
pub struct StatelessL2BlockExecutorBuilder<'a, F, H>
where
F: TrieProvider,
F: TrieDBProvider,
H: TrieHinter,
{
/// The [RollupConfig].
config: &'a RollupConfig,
/// The [TrieProvider] to fetch the state trie preimages.
/// The [TrieDBProvider] to fetch the state trie preimages.
provider: F,
/// The [TrieHinter] to hint the state trie preimages.
hinter: H,
Expand All @@ -32,7 +32,7 @@ where

impl<'a, F, H> StatelessL2BlockExecutorBuilder<'a, F, H>
where
F: TrieProvider,
F: TrieDBProvider,
H: TrieHinter,
{
/// Instantiate a new builder with the given [RollupConfig].
Expand Down Expand Up @@ -72,7 +72,8 @@ where
#[cfg(test)]
mod tests {
use super::*;
use kona_mpt::{NoopTrieHinter, NoopTrieProvider};
use crate::NoopTrieDBProvider;
use kona_mpt::NoopTrieHinter;

#[test]
fn test_build_full() {
Expand All @@ -81,13 +82,13 @@ mod tests {

fn test_handler_register<F, H>(_: &mut EvmHandler<'_, (), &mut State<&mut TrieDB<F, H>>>)
where
F: TrieProvider,
F: TrieDBProvider,
H: TrieHinter,
{
}

let executor =
StatelessL2BlockExecutorBuilder::new(&config, NoopTrieProvider, NoopTrieHinter)
StatelessL2BlockExecutorBuilder::new(&config, NoopTrieDBProvider, NoopTrieHinter)
.with_handle_register(test_handler_register)
.build();

Expand Down
6 changes: 3 additions & 3 deletions crates/executor/src/executor/env.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Environment preparation for the executor.
use super::{util::decode_holocene_eip_1559_params, StatelessL2BlockExecutor};
use crate::{constants::FEE_RECIPIENT, ExecutorError, ExecutorResult};
use crate::{constants::FEE_RECIPIENT, ExecutorError, ExecutorResult, TrieDBProvider};
use alloy_consensus::Header;
use alloy_eips::eip1559::BaseFeeParams;
use alloy_primitives::{TxKind, U256};
use kona_mpt::{TrieHinter, TrieProvider};
use kona_mpt::TrieHinter;
use op_alloy_consensus::OpTxEnvelope;
use op_alloy_genesis::RollupConfig;
use op_alloy_rpc_types_engine::OpPayloadAttributes;
Expand All @@ -16,7 +16,7 @@ use revm::primitives::{

impl<P, H> StatelessL2BlockExecutor<'_, P, H>
where
P: TrieProvider,
P: TrieDBProvider,
H: TrieHinter,
{
/// Returns the active [SpecId] for the executor.
Expand Down
Loading

0 comments on commit 6e333b3

Please sign in to comment.