Skip to content

Commit

Permalink
feat: Make Ctx journal generic (#1933)
Browse files Browse the repository at this point in the history
* fix: clear JournalState and set first journal vec

* chore: EVM transact make output generic for  POSTEXEC

* Make ExecCommit generic on HaltReason

* feat: Make journal generic

* fix rest

* clippy

* doc
  • Loading branch information
rakita authored Dec 20, 2024
1 parent 4a1d633 commit 3124e5e
Show file tree
Hide file tree
Showing 20 changed files with 342 additions and 285 deletions.
29 changes: 26 additions & 3 deletions crates/context/interface/src/host.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
mod dummy;

pub use crate::journaled_state::StateLoad;
pub use dummy::DummyHost;

use crate::{
journaled_state::{AccountLoad, Eip7702CodeLoad, StateLoad},
Block, CfgEnv, Transaction,
journaled_state::{AccountLoad, Eip7702CodeLoad},
Block, Cfg, Transaction,
};
use primitives::{Address, Bytes, Log, B256, U256};

Expand All @@ -10,6 +15,7 @@ pub trait Host {
/// Chain specification.
type BLOCK: Block;
type TX: Transaction;
type CFG: Cfg;

/// Returns a reference to the environment.
fn tx(&self) -> &Self::TX;
Expand All @@ -18,7 +24,7 @@ pub trait Host {
fn block(&self) -> &Self::BLOCK;

/// TODO make it generic in future
fn cfg(&self) -> &CfgEnv;
fn cfg(&self) -> &Self::CFG;

/// Load an account code.
fn load_account_delegated(&mut self, address: Address) -> Option<AccountLoad>;
Expand Down Expand Up @@ -125,3 +131,20 @@ pub struct SelfDestructResult {
pub target_exists: bool,
pub previously_destroyed: bool,
}

// TODO TEST
// #[cfg(test)]
// mod tests {
// use database_interface::EmptyDB;
// use context_interface::EthereumWiring;

// use super::*;

// fn assert_host<H: Host + ?Sized>() {}

// #[test]
// fn object_safety() {
// assert_host::<DummyHost<EthereumWiring<EmptyDB, ()>>>();
// assert_host::<dyn Host<EvmWiringT = EthereumWiring<EmptyDB, ()>>>();
// }
// }
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{Host, SStoreResult, SelfDestructResult};
use context_interface::{Block, Cfg, Transaction};
use super::{Host, SStoreResult, SelfDestructResult};
use crate::{Block, Cfg, Transaction};
use primitives::{hash_map::Entry, Address, Bytes, HashMap, Log, B256, KECCAK_EMPTY, U256};
use std::vec::Vec;

Expand Down
56 changes: 51 additions & 5 deletions crates/context/interface/src/journaled_state.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,60 @@
use core::ops::{Deref, DerefMut};
use database_interface::{Database, DatabaseGetter};
use primitives::{Address, B256, U256};
use primitives::{Address, Log, B256, U256};
use specification::hardfork::SpecId;
use state::{Account, Bytecode};
use std::boxed::Box;

pub trait JournaledState {
use crate::host::{SStoreResult, SelfDestructResult};

pub trait Journal {
type Database: Database;
type FinalOutput;

/// Creates new Journaled state.
///
/// Dont forget to set spec_id.
fn new(database: Self::Database) -> Self;

/// Returns the database.
fn db(&self) -> &Self::Database;

/// Returns the mutable database.
fn db_mut(&mut self) -> &mut Self::Database;

/// Returns the storage value from Journal state.
///
/// Loads the storage from database if not found in Journal state.
fn sload(
&mut self,
address: Address,
key: U256,
) -> Result<StateLoad<U256>, <Self::Database as Database>::Error>;

/// Stores the storage value in Journal state.
fn sstore(
&mut self,
address: Address,
key: U256,
value: U256,
) -> Result<StateLoad<SStoreResult>, <Self::Database as Database>::Error>;

/// Loads transient storage value.
fn tload(&mut self, address: Address, key: U256) -> U256;

/// Stores transient storage value.
fn tstore(&mut self, address: Address, key: U256, value: U256);

/// Logs the log in Journal state.
fn log(&mut self, log: Log);

/// Marks the account for selfdestruction and transfers all the balance to the target.
fn selfdestruct(
&mut self,
address: Address,
target: Address,
) -> Result<StateLoad<SelfDestructResult>, <Self::Database as Database>::Error>;

fn warm_account_and_storage(
&mut self,
address: Address,
Expand Down Expand Up @@ -80,7 +126,7 @@ pub trait JournaledState {

/// Does cleanup and returns modified state.
///
/// This resets the [JournaledState] to its initial state.
/// This resets the [Journal] to its initial state.
fn finalize(&mut self) -> Result<Self::FinalOutput, <Self::Database as Database>::Error>;
}

Expand Down Expand Up @@ -240,10 +286,10 @@ impl<T> Eip7702CodeLoad<T> {

/// Helper that extracts database error from [`JournalStateGetter`].
pub type JournalStateGetterDBError<CTX> =
<<<CTX as JournalStateGetter>::Journal as JournaledState>::Database as Database>::Error;
<<<CTX as JournalStateGetter>::Journal as Journal>::Database as Database>::Error;

pub trait JournalStateGetter: DatabaseGetter {
type Journal: JournaledState<Database = <Self as DatabaseGetter>::Database>;
type Journal: Journal<Database = <Self as DatabaseGetter>::Database>;

fn journal(&mut self) -> &mut Self::Journal;
}
Expand Down
3 changes: 2 additions & 1 deletion crates/context/interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern crate alloc as std;
pub mod block;
pub mod cfg;
pub mod errors;
pub mod host;
pub mod journaled_state;
pub mod result;
pub mod transaction;
Expand All @@ -16,5 +17,5 @@ pub use block::{Block, BlockGetter};
pub use cfg::{Cfg, CfgGetter, CreateScheme, TransactTo};
pub use database_interface::{DBErrorMarker, Database, DatabaseGetter};
pub use errors::ErrorGetter;
pub use journaled_state::{JournalStateGetter, JournalStateGetterDBError, JournaledState};
pub use journaled_state::{Journal, JournalStateGetter, JournalStateGetterDBError};
pub use transaction::{Transaction, TransactionGetter, TransactionType};
Loading

0 comments on commit 3124e5e

Please sign in to comment.