From c28ea1b010ddeee11cecd1905ba481cd03b8ae79 Mon Sep 17 00:00:00 2001 From: FredCoen <43670554+FredCoen@users.noreply.github.com> Date: Wed, 8 Jan 2025 01:52:37 +0100 Subject: [PATCH] chore: Add helper functions for JournalInit #1879 (#1961) * add journalinit type * replace from into convention with explicit helpers --- crates/context/src/journal_init.rs | 35 +++++++++++++++++++++++++++ crates/context/src/journaled_state.rs | 19 +++++++++++++++ crates/context/src/lib.rs | 2 ++ 3 files changed, 56 insertions(+) create mode 100644 crates/context/src/journal_init.rs diff --git a/crates/context/src/journal_init.rs b/crates/context/src/journal_init.rs new file mode 100644 index 0000000000..083cf36a09 --- /dev/null +++ b/crates/context/src/journal_init.rs @@ -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; + +impl JournaledState { + 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(), + } + } +} diff --git a/crates/context/src/journaled_state.rs b/crates/context/src/journaled_state.rs index bf356cb168..9462695d56 100644 --- a/crates/context/src/journaled_state.rs +++ b/crates/context/src/journaled_state.rs @@ -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. @@ -1045,3 +1047,20 @@ pub enum JournalEntry { /// Revert: Revert to previous bytecode. CodeChange { address: Address }, } + +impl JournaledState { + /// 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(), + } + } +} diff --git a/crates/context/src/lib.rs b/crates/context/src/lib.rs index 44a5b394c0..96400f2991 100644 --- a/crates/context/src/lib.rs +++ b/crates/context/src/lib.rs @@ -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;