revm_context/
journal.rs

1//! This module contains [`Journal`] struct and implements [`JournalTr`] trait for it.
2//!
3//! Entry submodule contains [`JournalEntry`] and [`JournalEntryTr`] traits.
4//! and inner submodule contains [`JournalInner`] struct that contains state.
5pub mod entry;
6pub mod inner;
7
8pub use entry::{JournalEntry, JournalEntryTr};
9pub use inner::JournalInner;
10
11use bytecode::Bytecode;
12use context_interface::{
13    context::{SStoreResult, SelfDestructResult, StateLoad},
14    journaled_state::{AccountLoad, JournalCheckpoint, JournalTr, TransferError},
15};
16use core::ops::{Deref, DerefMut};
17use database_interface::Database;
18use primitives::{hardfork::SpecId, Address, HashSet, Log, StorageKey, StorageValue, B256, U256};
19use state::{Account, EvmState};
20use std::vec::Vec;
21
22/// A journal of state changes internal to the EVM
23///
24/// On each additional call, the depth of the journaled state is increased (`depth`) and a new journal is added.
25///
26/// The journal contains every state change that happens within that call, making it possible to revert changes made in a specific call.
27#[derive(Debug, Clone, PartialEq, Eq)]
28#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
29pub struct Journal<DB, ENTRY = JournalEntry>
30where
31    ENTRY: JournalEntryTr,
32{
33    /// Database
34    pub database: DB,
35    /// Inner journal state.
36    pub inner: JournalInner<ENTRY>,
37}
38
39impl<DB, ENTRY> Deref for Journal<DB, ENTRY>
40where
41    ENTRY: JournalEntryTr,
42{
43    type Target = JournalInner<ENTRY>;
44
45    fn deref(&self) -> &Self::Target {
46        &self.inner
47    }
48}
49
50impl<DB, ENTRY> DerefMut for Journal<DB, ENTRY>
51where
52    ENTRY: JournalEntryTr,
53{
54    fn deref_mut(&mut self) -> &mut Self::Target {
55        &mut self.inner
56    }
57}
58
59impl<DB, ENTRY: JournalEntryTr> Journal<DB, ENTRY> {
60    /// Creates a new JournaledState by copying state data from a JournalInit and provided database.
61    /// This allows reusing the state, logs, and other data from a previous execution context while
62    /// connecting it to a different database backend.
63    pub fn new_with_inner(database: DB, inner: JournalInner<ENTRY>) -> Self {
64        Self { database, inner }
65    }
66
67    /// Consumes the [`Journal`] and returns [`JournalInner`].
68    ///
69    /// If you need to preserve the original journal, use [`Self::to_inner`] instead which clones the state.
70    pub fn into_init(self) -> JournalInner<ENTRY> {
71        self.inner
72    }
73}
74
75impl<DB, ENTRY: JournalEntryTr + Clone> Journal<DB, ENTRY> {
76    /// Creates a new [`JournalInner`] by cloning all internal state data (state, storage, logs, etc)
77    /// This allows creating a new journaled state with the same state data but without
78    /// carrying over the original database.
79    ///
80    /// This is useful when you want to reuse the current state for a new transaction or
81    /// execution context, but want to start with a fresh database.
82    pub fn to_inner(&self) -> JournalInner<ENTRY> {
83        self.inner.clone()
84    }
85}
86
87impl<DB: Database, ENTRY: JournalEntryTr> JournalTr for Journal<DB, ENTRY> {
88    type Database = DB;
89    type State = EvmState;
90
91    fn new(database: DB) -> Journal<DB, ENTRY> {
92        Self {
93            inner: JournalInner::new(),
94            database,
95        }
96    }
97
98    fn db(&self) -> &Self::Database {
99        &self.database
100    }
101
102    fn db_mut(&mut self) -> &mut Self::Database {
103        &mut self.database
104    }
105
106    fn sload(
107        &mut self,
108        address: Address,
109        key: StorageKey,
110    ) -> Result<StateLoad<StorageValue>, <Self::Database as Database>::Error> {
111        self.inner.sload(&mut self.database, address, key)
112    }
113
114    fn sstore(
115        &mut self,
116        address: Address,
117        key: StorageKey,
118        value: StorageValue,
119    ) -> Result<StateLoad<SStoreResult>, <Self::Database as Database>::Error> {
120        self.inner.sstore(&mut self.database, address, key, value)
121    }
122
123    fn tload(&mut self, address: Address, key: StorageKey) -> StorageValue {
124        self.inner.tload(address, key)
125    }
126
127    fn tstore(&mut self, address: Address, key: StorageKey, value: StorageValue) {
128        self.inner.tstore(address, key, value)
129    }
130
131    fn log(&mut self, log: Log) {
132        self.inner.log(log)
133    }
134
135    fn selfdestruct(
136        &mut self,
137        address: Address,
138        target: Address,
139    ) -> Result<StateLoad<SelfDestructResult>, DB::Error> {
140        self.inner.selfdestruct(&mut self.database, address, target)
141    }
142
143    fn warm_account(&mut self, address: Address) {
144        self.inner.warm_preloaded_addresses.insert(address);
145    }
146
147    fn warm_precompiles(&mut self, address: HashSet<Address>) {
148        self.inner.precompiles = address;
149        self.inner.warm_preloaded_addresses = self.inner.precompiles.clone();
150    }
151
152    #[inline]
153    fn precompile_addresses(&self) -> &HashSet<Address> {
154        &self.inner.precompiles
155    }
156
157    /// Returns call depth.
158    #[inline]
159    fn depth(&self) -> usize {
160        self.inner.depth
161    }
162
163    #[inline]
164    fn warm_account_and_storage(
165        &mut self,
166        address: Address,
167        storage_keys: impl IntoIterator<Item = StorageKey>,
168    ) -> Result<(), <Self::Database as Database>::Error> {
169        self.inner
170            .load_account_optional(&mut self.database, address, false, storage_keys)?;
171        Ok(())
172    }
173
174    #[inline]
175    fn set_spec_id(&mut self, spec_id: SpecId) {
176        self.inner.spec = spec_id;
177    }
178
179    #[inline]
180    fn transfer(
181        &mut self,
182        from: Address,
183        to: Address,
184        balance: U256,
185    ) -> Result<Option<TransferError>, DB::Error> {
186        self.inner.transfer(&mut self.database, from, to, balance)
187    }
188
189    #[inline]
190    fn touch_account(&mut self, address: Address) {
191        self.inner.touch(address);
192    }
193
194    #[inline]
195    fn caller_accounting_journal_entry(
196        &mut self,
197        address: Address,
198        old_balance: U256,
199        bump_nonce: bool,
200    ) {
201        self.inner
202            .caller_accounting_journal_entry(address, old_balance, bump_nonce);
203    }
204
205    /// Increments the balance of the account.
206    #[inline]
207    fn balance_incr(
208        &mut self,
209        address: Address,
210        balance: U256,
211    ) -> Result<(), <Self::Database as Database>::Error> {
212        self.inner
213            .balance_incr(&mut self.database, address, balance)
214    }
215
216    /// Increments the nonce of the account.
217    #[inline]
218    fn nonce_bump_journal_entry(&mut self, address: Address) {
219        self.inner.nonce_bump_journal_entry(address)
220    }
221
222    #[inline]
223    fn load_account(&mut self, address: Address) -> Result<StateLoad<&mut Account>, DB::Error> {
224        self.inner.load_account(&mut self.database, address)
225    }
226
227    #[inline]
228    fn load_account_code(
229        &mut self,
230        address: Address,
231    ) -> Result<StateLoad<&mut Account>, DB::Error> {
232        self.inner.load_code(&mut self.database, address)
233    }
234
235    #[inline]
236    fn load_account_delegated(
237        &mut self,
238        address: Address,
239    ) -> Result<StateLoad<AccountLoad>, DB::Error> {
240        self.inner
241            .load_account_delegated(&mut self.database, address)
242    }
243
244    #[inline]
245    fn checkpoint(&mut self) -> JournalCheckpoint {
246        self.inner.checkpoint()
247    }
248
249    #[inline]
250    fn checkpoint_commit(&mut self) {
251        self.inner.checkpoint_commit()
252    }
253
254    #[inline]
255    fn checkpoint_revert(&mut self, checkpoint: JournalCheckpoint) {
256        self.inner.checkpoint_revert(checkpoint)
257    }
258
259    #[inline]
260    fn set_code_with_hash(&mut self, address: Address, code: Bytecode, hash: B256) {
261        self.inner.set_code_with_hash(address, code, hash);
262    }
263
264    #[inline]
265    fn create_account_checkpoint(
266        &mut self,
267        caller: Address,
268        address: Address,
269        balance: U256,
270        spec_id: SpecId,
271    ) -> Result<JournalCheckpoint, TransferError> {
272        // Ignore error.
273        self.inner
274            .create_account_checkpoint(caller, address, balance, spec_id)
275    }
276
277    #[inline]
278    fn take_logs(&mut self) -> Vec<Log> {
279        self.inner.take_logs()
280    }
281
282    #[inline]
283    fn commit_tx(&mut self) {
284        self.inner.commit_tx()
285    }
286
287    #[inline]
288    fn discard_tx(&mut self) {
289        self.inner.discard_tx();
290    }
291
292    /// Clear current journal resetting it to initial state and return changes state.
293    #[inline]
294    fn finalize(&mut self) -> Self::State {
295        self.inner.finalize()
296    }
297}