Skip to content

Commit

Permalink
chore: Add helper functions for JournalInit #1879 (#1961)
Browse files Browse the repository at this point in the history
* add journalinit type

* replace from into convention with explicit helpers
  • Loading branch information
FredCoen authored Jan 8, 2025
1 parent 7fcbd7d commit c28ea1b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
35 changes: 35 additions & 0 deletions crates/context/src/journal_init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use super::journaled_state::JournaledState;
use database_interface::EmptyDB;

/// A clonable version of JournaledState that uses EmptyDB.
pub type JournalInit = JournaledState<EmptyDB>;

impl<DB> JournaledState<DB> {
pub fn into_init(self) -> JournalInit {
JournalInit {
database: EmptyDB::default(),
state: self.state,
transient_storage: self.transient_storage,
logs: self.logs,
depth: self.depth,
journal: self.journal,
spec: self.spec,
warm_preloaded_addresses: self.warm_preloaded_addresses,
precompiles: self.precompiles,
}
}

pub fn to_init(&self) -> JournalInit {
JournalInit {
database: EmptyDB::default(),
state: self.state.clone(),
transient_storage: self.transient_storage.clone(),
logs: self.logs.clone(),
depth: self.depth,
journal: self.journal.clone(),
spec: self.spec,
warm_preloaded_addresses: self.warm_preloaded_addresses.clone(),
precompiles: self.precompiles.clone(),
}
}
}
19 changes: 19 additions & 0 deletions crates/context/src/journaled_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use state::{Account, EvmState, EvmStorageSlot, TransientStorage};
use core::mem;
use std::{vec, vec::Vec};

use crate::JournalInit;

/// A journal of state changes internal to the EVM
///
/// On each additional call, the depth of the journaled state is increased (`depth`) and a new journal is added.
Expand Down Expand Up @@ -1045,3 +1047,20 @@ pub enum JournalEntry {
/// Revert: Revert to previous bytecode.
CodeChange { address: Address },
}

impl<DB> JournaledState<DB> {
/// Initialize a new JournaledState from JournalInit with a database
pub fn from_init(init: &JournalInit, database: DB) -> Self {
Self {
database,
state: init.state.clone(),
transient_storage: init.transient_storage.clone(),
logs: init.logs.clone(),
depth: init.depth,
journal: init.journal.clone(),
spec: init.spec,
warm_preloaded_addresses: init.warm_preloaded_addresses.clone(),
precompiles: init.precompiles.clone(),
}
}
}
2 changes: 2 additions & 0 deletions crates/context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ extern crate alloc as std;
pub mod block;
pub mod cfg;
pub mod context;
mod journal_init;
pub mod journaled_state;
pub mod tx;

pub use block::BlockEnv;
pub use cfg::{Cfg, CfgEnv};
pub use context::*;
pub use journal_init::JournalInit;
pub use journaled_state::*;
pub use tx::TxEnv;

0 comments on commit c28ea1b

Please sign in to comment.