Skip to content

Commit c28ea1b

Browse files
authored
chore: Add helper functions for JournalInit #1879 (#1961)
* add journalinit type * replace from into convention with explicit helpers
1 parent 7fcbd7d commit c28ea1b

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

crates/context/src/journal_init.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use super::journaled_state::JournaledState;
2+
use database_interface::EmptyDB;
3+
4+
/// A clonable version of JournaledState that uses EmptyDB.
5+
pub type JournalInit = JournaledState<EmptyDB>;
6+
7+
impl<DB> JournaledState<DB> {
8+
pub fn into_init(self) -> JournalInit {
9+
JournalInit {
10+
database: EmptyDB::default(),
11+
state: self.state,
12+
transient_storage: self.transient_storage,
13+
logs: self.logs,
14+
depth: self.depth,
15+
journal: self.journal,
16+
spec: self.spec,
17+
warm_preloaded_addresses: self.warm_preloaded_addresses,
18+
precompiles: self.precompiles,
19+
}
20+
}
21+
22+
pub fn to_init(&self) -> JournalInit {
23+
JournalInit {
24+
database: EmptyDB::default(),
25+
state: self.state.clone(),
26+
transient_storage: self.transient_storage.clone(),
27+
logs: self.logs.clone(),
28+
depth: self.depth,
29+
journal: self.journal.clone(),
30+
spec: self.spec,
31+
warm_preloaded_addresses: self.warm_preloaded_addresses.clone(),
32+
precompiles: self.precompiles.clone(),
33+
}
34+
}
35+
}

crates/context/src/journaled_state.rs

+19
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use state::{Account, EvmState, EvmStorageSlot, TransientStorage};
1313
use core::mem;
1414
use std::{vec, vec::Vec};
1515

16+
use crate::JournalInit;
17+
1618
/// A journal of state changes internal to the EVM
1719
///
1820
/// 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 {
10451047
/// Revert: Revert to previous bytecode.
10461048
CodeChange { address: Address },
10471049
}
1050+
1051+
impl<DB> JournaledState<DB> {
1052+
/// Initialize a new JournaledState from JournalInit with a database
1053+
pub fn from_init(init: &JournalInit, database: DB) -> Self {
1054+
Self {
1055+
database,
1056+
state: init.state.clone(),
1057+
transient_storage: init.transient_storage.clone(),
1058+
logs: init.logs.clone(),
1059+
depth: init.depth,
1060+
journal: init.journal.clone(),
1061+
spec: init.spec,
1062+
warm_preloaded_addresses: init.warm_preloaded_addresses.clone(),
1063+
precompiles: init.precompiles.clone(),
1064+
}
1065+
}
1066+
}

crates/context/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ extern crate alloc as std;
88
pub mod block;
99
pub mod cfg;
1010
pub mod context;
11+
mod journal_init;
1112
pub mod journaled_state;
1213
pub mod tx;
1314

1415
pub use block::BlockEnv;
1516
pub use cfg::{Cfg, CfgEnv};
1617
pub use context::*;
18+
pub use journal_init::JournalInit;
1719
pub use journaled_state::*;
1820
pub use tx::TxEnv;

0 commit comments

Comments
 (0)