Skip to content

Commit

Permalink
feat(executor): Clean ups (#719)
Browse files Browse the repository at this point in the history
* feat(executor): Clean ups

* update

* doc test

* fmt
  • Loading branch information
clabby authored Oct 19, 2024
1 parent 6b5b242 commit f1f7868
Show file tree
Hide file tree
Showing 19 changed files with 1,248 additions and 1,265 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

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

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

use alloc::sync::Arc;
use kona_mpt::{TrieDB, TrieHinter, TrieProvider};
use kona_executor::TrieDB;
use kona_mpt::{TrieHinter, TrieProvider};
use revm::{
handler::register::EvmHandler,
primitives::{spec_to_generic, SpecId},
Expand Down
3 changes: 2 additions & 1 deletion crates/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ workspace = true
kona-mpt.workspace = true

# Alloy
alloy-eips.workspace = true
alloy-consensus = { workspace = true, features = ["k256"] }
alloy-primitives = { workspace = true, features = ["rlp"] }
alloy-eips.workspace = true
alloy-rlp.workspace = true

# Op Alloy
op-alloy-consensus.workspace = true
Expand Down
15 changes: 15 additions & 0 deletions crates/executor/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Protocol constants for the executor.

use alloy_primitives::{address, Address};

/// The address of the fee recipient.
pub(crate) const FEE_RECIPIENT: Address = address!("4200000000000000000000000000000000000011");

/// The address of the L2 to L1 bridge predeploy.
pub(crate) const L2_TO_L1_BRIDGE: Address = address!("4200000000000000000000000000000000000016");

/// The current version of the output root format.
pub(crate) const OUTPUT_ROOT_VERSION: u8 = 0;

/// The version byte for the Holocene extra data.
pub(crate) const HOLOCENE_EXTRA_DATA_VERSION: u8 = 0x00;
File renamed without changes.
82 changes: 18 additions & 64 deletions crates/mpt/src/db/mod.rs → crates/executor/src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
//! This module contains an implementation of an in-memory Trie DB for [revm], that allows for
//! incremental updates through fetching node preimages on the fly during execution.

use crate::{
errors::{TrieDBError, TrieDBResult},
TrieHinter, TrieNode, TrieNodeError, TrieProvider,
};
use crate::errors::{TrieDBError, TrieDBResult};
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 alloy_trie::Nibbles;
use kona_mpt::{Nibbles, TrieHinter, TrieNode, TrieNodeError, TrieProvider};
use revm::{
db::{states::StorageSlot, BundleState},
primitives::{AccountInfo, Bytecode, HashMap, BLOCK_HASH_HISTORY},
Expand Down Expand Up @@ -50,7 +47,8 @@ pub use account::TrieAccount;
/// use alloy_consensus::{Header, Sealable};
/// use alloy_primitives::{Bytes, B256};
/// use anyhow::Result;
/// use kona_mpt::{NoopTrieHinter, NoopTrieProvider, TrieDB};
/// use kona_executor::TrieDB;
/// use kona_mpt::{NoopTrieHinter, NoopTrieProvider};
/// use revm::{db::states::bundle_state::BundleRetention, EvmBuilder, StateBuilder};
///
/// let mock_starting_root = B256::default();
Expand Down Expand Up @@ -116,35 +114,27 @@ where
}

/// Returns a shared reference to the root [TrieNode] of the trie DB.
pub const fn root_node_ref(&self) -> &TrieNode {
pub const fn root(&self) -> &TrieNode {
&self.root_node
}

/// Returns a mutable reference to the root [TrieNode] of the trie DB.
///
/// # Safety
/// This method is unsafe because it allows for the mutation of the root node, which enables
/// the caller to mutate the [TrieNode] of the DB without updating the root hash. This can lead
/// to inconsistencies in the trie DB's state. The caller must ensure that the root hash is
/// updated after mutating the root node.
pub unsafe fn root_node_mut(&mut self) -> &mut TrieNode {
&mut self.root_node
}

/// Returns the mapping of [Address]es to storage roots.
pub const fn storage_roots(&self) -> &HashMap<Address, TrieNode> {
&self.storage_roots
}

/// Returns the mapping of [Address]es to storage roots.
/// Returns a reference to the current parent block header of the trie DB.
pub const fn parent_block_header(&self) -> &Sealed<Header> {
&self.parent_block_header
}

/// Sets the parent block header of the trie DB. Should be called after a block has been
/// executed and the Header has been created.
///
/// # Safety
/// This method is unsafe because it allows for the mutation of the storage roots, which enables
/// the caller to mutate the [TrieNode] of an account in the DB without updating the root hash
/// or validating the account storage root within the state trie. The caller must ensure
/// that any changes to the storage roots are consistent with the state trie.
pub unsafe fn storage_roots_mut(&mut self) -> &mut HashMap<Address, TrieNode> {
&mut self.storage_roots
/// ## Takes
/// - `parent_block_header`: The parent block header of the current block.
pub fn set_parent_block_header(&mut self, parent_block_header: Sealed<Header>) {
self.parent_block_header = parent_block_header;
}

/// Applies a [BundleState] changeset to the [TrieNode] and recomputes the state root hash.
Expand Down Expand Up @@ -174,20 +164,6 @@ where
self.root_node.blinded_commitment().ok_or(TrieDBError::RootNotBlinded)
}

/// Returns a reference to the current parent block header of the trie DB.
pub const fn parent_block_header(&self) -> &Sealed<Header> {
&self.parent_block_header
}

/// Sets the parent block header of the trie DB. Should be called after a block has been
/// executed and the Header has been created.
///
/// ## Takes
/// - `parent_block_header`: The parent block header of the current block.
pub fn set_parent_block_header(&mut self, parent_block_header: Sealed<Header>) {
self.parent_block_header = parent_block_header;
}

/// Fetches the [TrieAccount] of an account from the trie DB.
///
/// ## Takes
Expand Down Expand Up @@ -409,9 +385,9 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::{NoopTrieHinter, NoopTrieProvider};
use alloy_consensus::Sealable;
use alloy_primitives::b256;
use kona_mpt::{NoopTrieHinter, NoopTrieProvider};

fn new_test_db() -> TrieDB<NoopTrieProvider, NoopTrieHinter> {
TrieDB::new(
Expand All @@ -432,18 +408,7 @@ mod tests {
#[test]
fn test_trie_db_root_node_ref() {
let db = new_test_db();
let root_node = db.root_node_ref();
assert_eq!(root_node.blinded_commitment(), Some(B256::default()));
}

#[test]
fn test_trie_db_root_node_mut() {
let mut db = new_test_db();
unsafe {
let root_node = db.root_node_mut();
root_node.blind()
}
let root_node = db.root_node_ref();
let root_node = db.root();
assert_eq!(root_node.blinded_commitment(), Some(B256::default()));
}

Expand All @@ -454,17 +419,6 @@ mod tests {
assert!(storage_roots.is_empty());
}

#[test]
fn test_trie_db_storage_roots_mut() {
let mut db = new_test_db();
unsafe {
let storage_roots = db.storage_roots_mut();
storage_roots.insert(Address::default(), TrieNode::new_blinded(B256::default()));
}
let storage_roots = db.storage_roots();
assert_eq!(storage_roots.len(), 1);
}

#[test]
fn test_block_hash_above_range() {
let mut db = new_test_db();
Expand Down
25 changes: 24 additions & 1 deletion crates/executor/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Errors for the `kona-executor` crate.

use kona_mpt::TrieDBError;
use alloc::string::String;
use kona_mpt::TrieNodeError;
use revm::primitives::EVMError;
use thiserror::Error;

Expand Down Expand Up @@ -46,3 +47,25 @@ pub enum ExecutorError {

/// A [Result] type for the [ExecutorError] enum.
pub type ExecutorResult<T> = Result<T, ExecutorError>;

/// A [Result] type alias where the error is [TrieDBError].
pub type TrieDBResult<T> = Result<T, TrieDBError>;

/// An error type for [TrieDB] operations.
///
/// [TrieDB]: crate::TrieDB
#[derive(Error, Debug, PartialEq, Eq)]
pub enum TrieDBError {
/// Trie root node has not been blinded.
#[error("Trie root node has not been blinded")]
RootNotBlinded,
/// Missing account info for bundle account.
#[error("Missing account info for bundle account.")]
MissingAccountInfo,
/// Trie node error.
#[error("Trie node error: {0}")]
TrieNode(#[from] TrieNodeError),
/// Trie provider error.
#[error("Trie provider error: {0}")]
Provider(String),
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Contains the builder pattern for the [StatelessL2BlockExecutor].

use crate::StatelessL2BlockExecutor;
use super::StatelessL2BlockExecutor;
use crate::db::TrieDB;
use alloy_consensus::{Header, Sealable, Sealed};
use kona_mpt::{TrieDB, TrieHinter, TrieProvider};
use kona_mpt::{TrieHinter, TrieProvider};
use op_alloy_genesis::RollupConfig;
use revm::{db::State, handler::register::EvmHandler};

Expand Down
Loading

0 comments on commit f1f7868

Please sign in to comment.