Skip to main content

revm_context/journal/
inner.rs

1//! Module containing the [`JournalInner`] that is part of [`crate::Journal`].
2use super::warm_addresses::WarmAddresses;
3use bytecode::Bytecode;
4use context_interface::{
5    context::{SStoreResult, SelfDestructResult, StateLoad},
6    journaled_state::{
7        account::{JournaledAccount, JournaledAccountTr},
8        entry::{JournalEntryTr, SelfdestructionRevertStatus},
9        AccountLoad, JournalCheckpoint, JournalLoadError, TransferError,
10    },
11};
12use core::mem;
13use database_interface::Database;
14use primitives::{
15    eip7708::{ETH_TRANSFER_LOG_ADDRESS, ETH_TRANSFER_LOG_TOPIC, SELFDESTRUCT_LOG_TOPIC},
16    hardfork::SpecId::{self, *},
17    hash_map::Entry,
18    hints_util::unlikely,
19    Address, Bytes, HashMap, Log, LogData, StorageKey, StorageValue, B256, KECCAK_EMPTY, U256,
20};
21use state::{Account, EvmState, TransientStorage};
22use std::vec::Vec;
23
24/// Configuration for the journal that affects EVM execution behavior.
25///
26/// This struct bundles the spec ID and EIP-7708 configuration flags.
27#[derive(Debug, Clone, Default, PartialEq, Eq)]
28#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
29pub struct JournalCfg {
30    /// The spec ID for the EVM. Spec is required for some journal entries and needs to be set for
31    /// JournalInner to be functional.
32    ///
33    /// If spec is set it assumed that precompile addresses are set as well for this particular spec.
34    ///
35    /// This spec is used for two things:
36    ///
37    /// - [EIP-161]: Prior to this EIP, Ethereum had separate definitions for empty and non-existing accounts.
38    /// - [EIP-6780]: `SELFDESTRUCT` only in same transaction
39    ///
40    /// [EIP-161]: https://eips.ethereum.org/EIPS/eip-161
41    /// [EIP-6780]: https://eips.ethereum.org/EIPS/eip-6780
42    pub spec: SpecId,
43    /// Whether EIP-7708 (ETH transfers emit logs) is disabled.
44    pub eip7708_disabled: bool,
45    /// Whether EIP-7708 delayed burn logging is disabled.
46    ///
47    /// When enabled, revm tracks all self-destructed addresses and emits logs for
48    /// accounts that still have remaining balance at the end of the transaction.
49    /// This can be disabled for performance reasons as it requires storing and
50    /// iterating over all self-destructed accounts. When disabled, the logging
51    /// can be done outside of revm when applying accounts to database state.
52    pub eip7708_delayed_burn_disabled: bool,
53}
54/// Inner journal state that contains journal and state changes.
55///
56/// Spec Id is a essential information for the Journal.
57#[derive(Debug, Clone, PartialEq, Eq)]
58#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
59pub struct JournalInner<ENTRY> {
60    /// The current state
61    pub state: EvmState,
62    /// Transient storage that is discarded after every transaction.
63    ///
64    /// See [EIP-1153](https://eips.ethereum.org/EIPS/eip-1153).
65    pub transient_storage: TransientStorage,
66    /// Emitted logs
67    pub logs: Vec<Log>,
68    /// The current call stack depth
69    pub depth: usize,
70    /// The journal of state changes, one for each transaction
71    pub journal: Vec<ENTRY>,
72    /// Global transaction id that represent number of transactions executed (Including reverted ones).
73    /// It can be different from number of `journal_history` as some transaction could be
74    /// reverted or had a error on execution.
75    ///
76    /// This ID is used in `Self::state` to determine if account/storage is touched/warm/cold.
77    pub transaction_id: usize,
78    /// Journal configuration containing spec ID and EIP-7708 flags.
79    pub cfg: JournalCfg,
80    /// Warm addresses containing both coinbase and current precompiles.
81    pub warm_addresses: WarmAddresses,
82    /// Addresses that were self-destructed for the first time in this transaction.
83    ///
84    /// This is used by [EIP-7708] to emit logs for self-destructed accounts that still
85    /// have balance at the end of the transaction.
86    ///
87    /// The vec is indexed by checkpoint - on revert, entries added after the checkpoint
88    /// are removed.
89    ///
90    /// [EIP-7708]: https://eips.ethereum.org/EIPS/eip-7708
91    pub selfdestructed_addresses: Vec<Address>,
92}
93
94impl<ENTRY: JournalEntryTr> Default for JournalInner<ENTRY> {
95    fn default() -> Self {
96        Self::new()
97    }
98}
99
100impl<ENTRY: JournalEntryTr> JournalInner<ENTRY> {
101    /// Creates new [`JournalInner`].
102    ///
103    /// `warm_preloaded_addresses` is used to determine if address is considered warm loaded.
104    /// In ordinary case this is precompile or beneficiary.
105    pub fn new() -> JournalInner<ENTRY> {
106        Self {
107            state: HashMap::default(),
108            transient_storage: TransientStorage::default(),
109            logs: Vec::new(),
110            journal: Vec::default(),
111            transaction_id: 0,
112            depth: 0,
113            cfg: JournalCfg::default(),
114            warm_addresses: WarmAddresses::new(),
115            selfdestructed_addresses: Vec::new(),
116        }
117    }
118
119    /// Returns the logs.
120    ///
121    /// Before returning, this function emits EIP-7708 logs for any self-destructed
122    /// accounts that still have a non-zero balance.
123    #[inline]
124    pub fn take_logs(&mut self) -> Vec<Log> {
125        // EIP-7708: Emit logs for self-destructed accounts with remaining balance
126        self.eip7708_emit_selfdestruct_remaining_balance_logs();
127        mem::take(&mut self.logs)
128    }
129
130    /// Prepare for next transaction, by committing the current journal to history, incrementing the transaction id
131    /// and returning the logs.
132    ///
133    /// This function is used to prepare for next transaction. It will save the current journal
134    /// and clear the journal for the next transaction.
135    ///
136    /// `commit_tx` is used even for discarding transactions so transaction_id will be incremented.
137    pub fn commit_tx(&mut self) {
138        // Clears all field from JournalInner. Doing it this way to avoid
139        // missing any field.
140        let Self {
141            state,
142            transient_storage,
143            logs,
144            depth,
145            journal,
146            transaction_id,
147            cfg,
148            warm_addresses,
149            selfdestructed_addresses,
150        } = self;
151        // Cfg and state are not changed. They are always set again before execution.
152        let _ = cfg;
153        let _ = state;
154        transient_storage.clear();
155        *depth = 0;
156
157        // Do nothing with journal history so we can skip cloning present journal.
158        journal.clear();
159
160        // Clear coinbase address warming for next tx
161        warm_addresses.clear_coinbase_and_access_list();
162        // increment transaction id.
163        *transaction_id += 1;
164
165        logs.clear();
166        selfdestructed_addresses.clear();
167    }
168
169    /// Discard the current transaction, by reverting the journal entries and incrementing the transaction id.
170    pub fn discard_tx(&mut self) {
171        // if there is no journal entries, there has not been any changes.
172        let Self {
173            state,
174            transient_storage,
175            logs,
176            depth,
177            journal,
178            transaction_id,
179            cfg,
180            warm_addresses,
181            selfdestructed_addresses,
182        } = self;
183        let is_spurious_dragon_enabled = cfg.spec.is_enabled_in(SPURIOUS_DRAGON);
184        // iterate over all journals entries and revert our global state
185        journal.drain(..).rev().for_each(|entry| {
186            entry.revert(state, None, is_spurious_dragon_enabled);
187        });
188        transient_storage.clear();
189        *depth = 0;
190        logs.clear();
191        selfdestructed_addresses.clear();
192        *transaction_id += 1;
193
194        // Clear coinbase address warming for next tx
195        warm_addresses.clear_coinbase_and_access_list();
196    }
197
198    /// Take the [`EvmState`] and clears the journal by resetting it to initial state.
199    ///
200    /// Note: Precompile addresses and spec are preserved and initial state of
201    /// warm_preloaded_addresses will contain precompiles addresses.
202    #[inline]
203    pub fn finalize(&mut self) -> EvmState {
204        // Clears all field from JournalInner. Doing it this way to avoid
205        // missing any field.
206        let Self {
207            state,
208            transient_storage,
209            logs,
210            depth,
211            journal,
212            transaction_id,
213            cfg,
214            warm_addresses,
215            selfdestructed_addresses,
216        } = self;
217        // Cfg is not changed. It is always set again before execution.
218        let _ = cfg;
219        // Clear coinbase address warming for next tx
220        warm_addresses.clear_coinbase_and_access_list();
221        selfdestructed_addresses.clear();
222
223        let state = mem::take(state);
224        logs.clear();
225        transient_storage.clear();
226
227        // clear journal and journal history.
228        journal.clear();
229        *depth = 0;
230        // reset transaction id.
231        *transaction_id = 0;
232
233        state
234    }
235
236    /// Emit EIP-7708 logs for self-destructed accounts that still have balance.
237    ///
238    /// This should be called before `take_logs()` at the end of transaction execution.
239    /// It checks all accounts that were self-destructed in this transaction and emits
240    /// a `SelfBalanceLog` for any that still have a non-zero balance.
241    ///
242    /// This can happen when an account receives ETH after being self-destructed
243    /// in the same transaction.
244    ///
245    /// Logs are emitted sorted by address in ascending order.
246    ///
247    /// [EIP-7708](https://eips.ethereum.org/EIPS/eip-7708)
248    #[inline]
249    pub fn eip7708_emit_selfdestruct_remaining_balance_logs(&mut self) {
250        if !self.cfg.spec.is_enabled_in(AMSTERDAM)
251            || self.cfg.eip7708_disabled
252            || self.cfg.eip7708_delayed_burn_disabled
253        {
254            return;
255        }
256
257        // Collect addresses with non-zero balance and sort by address
258        let mut addresses_with_balance: Vec<(Address, U256)> = self
259            .selfdestructed_addresses
260            .iter()
261            .filter_map(|address| {
262                self.state
263                    .get(address)
264                    .filter(|account| !account.info.balance.is_zero())
265                    .map(|account| (*address, account.info.balance))
266            })
267            .collect();
268
269        // Sort by address (ascending)
270        addresses_with_balance.sort_by_key(|(addr, _)| *addr);
271
272        // Emit logs in sorted order
273        for (address, balance) in addresses_with_balance {
274            self.eip7708_selfdestruct_to_self_log(address, balance);
275        }
276    }
277
278    /// Return reference to state.
279    #[inline]
280    pub fn state(&mut self) -> &mut EvmState {
281        &mut self.state
282    }
283
284    /// Sets SpecId.
285    #[inline]
286    pub fn set_spec_id(&mut self, spec: SpecId) {
287        self.cfg.spec = spec;
288    }
289
290    /// Sets EIP-7708 configuration flags.
291    #[inline]
292    pub fn set_eip7708_config(&mut self, disabled: bool, delayed_burn_disabled: bool) {
293        self.cfg.eip7708_disabled = disabled;
294        self.cfg.eip7708_delayed_burn_disabled = delayed_burn_disabled;
295    }
296
297    /// Mark account as touched as only touched accounts will be added to state.
298    /// This is especially important for state clear where touched empty accounts needs to
299    /// be removed from state.
300    #[inline]
301    pub fn touch(&mut self, address: Address) {
302        if let Some(account) = self.state.get_mut(&address) {
303            Self::touch_account(&mut self.journal, address, account);
304        }
305    }
306
307    /// Mark account as touched.
308    #[inline]
309    fn touch_account(journal: &mut Vec<ENTRY>, address: Address, account: &mut Account) {
310        if !account.is_touched() {
311            journal.push(ENTRY::account_touched(address));
312            account.mark_touch();
313        }
314    }
315
316    /// Returns the _loaded_ [Account] for the given address.
317    ///
318    /// This assumes that the account has already been loaded.
319    ///
320    /// # Panics
321    ///
322    /// Panics if the account has not been loaded and is missing from the state set.
323    #[inline]
324    pub fn account(&self, address: Address) -> &Account {
325        self.state
326            .get(&address)
327            .expect("Account expected to be loaded") // Always assume that acc is already loaded
328    }
329
330    /// Set code and its hash to the account.
331    ///
332    /// Note: Assume account is warm and that hash is calculated from code.
333    #[inline]
334    pub fn set_code_with_hash(&mut self, address: Address, code: Bytecode, hash: B256) {
335        let account = self.state.get_mut(&address).unwrap();
336        Self::touch_account(&mut self.journal, address, account);
337
338        self.journal.push(ENTRY::code_changed(address));
339
340        account.info.code_hash = hash;
341        account.info.code = Some(code);
342    }
343
344    /// Use it only if you know that acc is warm.
345    ///
346    /// Assume account is warm.
347    ///
348    /// In case of EIP-7702 code with zero address, the bytecode will be erased.
349    #[inline]
350    pub fn set_code(&mut self, address: Address, code: Bytecode) {
351        if let Some(eip7702_address) = code.eip7702_address() {
352            if eip7702_address.is_zero() {
353                self.set_code_with_hash(address, Bytecode::default(), KECCAK_EMPTY);
354                return;
355            }
356        }
357
358        let hash = code.hash_slow();
359        self.set_code_with_hash(address, code, hash)
360    }
361
362    /// Add journal entry for caller accounting.
363    #[inline]
364    #[deprecated]
365    pub fn caller_accounting_journal_entry(
366        &mut self,
367        address: Address,
368        old_balance: U256,
369        bump_nonce: bool,
370    ) {
371        // account balance changed.
372        self.journal
373            .push(ENTRY::balance_changed(address, old_balance));
374        // account is touched.
375        self.journal.push(ENTRY::account_touched(address));
376
377        if bump_nonce {
378            // nonce changed.
379            self.journal.push(ENTRY::nonce_bumped(address));
380        }
381    }
382
383    /// Increments the balance of the account.
384    ///
385    /// Mark account as touched.
386    #[inline]
387    pub fn balance_incr<DB: Database>(
388        &mut self,
389        db: &mut DB,
390        address: Address,
391        balance: U256,
392    ) -> Result<(), DB::Error> {
393        let mut account = self.load_account_mut(db, address)?.data;
394        account.incr_balance(balance);
395        Ok(())
396    }
397
398    /// Increments the nonce of the account.
399    #[inline]
400    #[deprecated]
401    pub fn nonce_bump_journal_entry(&mut self, address: Address) {
402        self.journal.push(ENTRY::nonce_bumped(address));
403    }
404
405    /// Transfers balance from two accounts. Returns error if sender balance is not enough.
406    ///
407    /// # Panics
408    ///
409    /// Panics if from or to are not loaded.
410    #[inline]
411    pub fn transfer_loaded(
412        &mut self,
413        from: Address,
414        to: Address,
415        balance: U256,
416    ) -> Option<TransferError> {
417        if from == to {
418            let from_balance = self.state.get_mut(&to).unwrap().info.balance;
419            // Check if from balance is enough to transfer the balance.
420            if balance > from_balance {
421                return Some(TransferError::OutOfFunds);
422            }
423            return None;
424        }
425
426        if balance.is_zero() {
427            Self::touch_account(&mut self.journal, to, self.state.get_mut(&to).unwrap());
428            return None;
429        }
430
431        // sub balance from
432        let from_account = self.state.get_mut(&from).unwrap();
433        Self::touch_account(&mut self.journal, from, from_account);
434        let from_balance = &mut from_account.info.balance;
435        let Some(from_balance_decr) = from_balance.checked_sub(balance) else {
436            return Some(TransferError::OutOfFunds);
437        };
438        *from_balance = from_balance_decr;
439
440        // add balance to
441        let to_account = self.state.get_mut(&to).unwrap();
442        Self::touch_account(&mut self.journal, to, to_account);
443        let to_balance = &mut to_account.info.balance;
444        let Some(to_balance_incr) = to_balance.checked_add(balance) else {
445            // Overflow of U256 balance is not possible to happen on mainnet. We don't bother to return funds from from_acc.
446            return Some(TransferError::OverflowPayment);
447        };
448        *to_balance = to_balance_incr;
449
450        // add journal entry
451        self.journal
452            .push(ENTRY::balance_transfer(from, to, balance));
453
454        // EIP-7708: emit ETH transfer log
455        self.eip7708_transfer_log(from, to, balance);
456
457        None
458    }
459
460    /// Transfers balance from two accounts. Returns error if sender balance is not enough.
461    #[inline]
462    pub fn transfer<DB: Database>(
463        &mut self,
464        db: &mut DB,
465        from: Address,
466        to: Address,
467        balance: U256,
468    ) -> Result<Option<TransferError>, DB::Error> {
469        self.load_account(db, from)?;
470        self.load_account(db, to)?;
471        Ok(self.transfer_loaded(from, to, balance))
472    }
473
474    /// Creates account or returns false if collision is detected.
475    ///
476    /// There are few steps done:
477    /// 1. Make created account warm loaded (AccessList) and this should
478    ///    be done before subroutine checkpoint is created.
479    /// 2. Check if there is collision of newly created account with existing one.
480    /// 3. Mark created account as created.
481    /// 4. Add fund to created account
482    /// 5. Increment nonce of created account if SpuriousDragon is active
483    /// 6. Decrease balance of caller account.
484    ///
485    /// # Panics
486    ///
487    /// Panics if the caller is not loaded inside the EVM state.
488    /// This should have been done inside `create_inner`.
489    #[inline]
490    pub fn create_account_checkpoint(
491        &mut self,
492        caller: Address,
493        target_address: Address,
494        balance: U256,
495        spec_id: SpecId,
496    ) -> Result<JournalCheckpoint, TransferError> {
497        // Enter subroutine
498        let checkpoint = self.checkpoint();
499
500        // Newly created account is present, as we just loaded it.
501        let target_acc = self.state.get_mut(&target_address).unwrap();
502        let last_journal = &mut self.journal;
503
504        // New account can be created if:
505        // Bytecode is not empty.
506        // Nonce is not zero
507        // Account is not precompile.
508        if target_acc.info.code_hash != KECCAK_EMPTY || target_acc.info.nonce != 0 {
509            self.checkpoint_revert(checkpoint);
510            return Err(TransferError::CreateCollision);
511        }
512
513        // set account status to create.
514        let is_created_globally = target_acc.mark_created_locally();
515
516        // this entry will revert set nonce.
517        last_journal.push(ENTRY::account_created(target_address, is_created_globally));
518        target_acc.info.code = None;
519        // EIP-161: State trie clearing (invariant-preserving alternative)
520        if spec_id.is_enabled_in(SPURIOUS_DRAGON) {
521            // nonce is going to be reset to zero in AccountCreated journal entry.
522            target_acc.info.nonce = 1;
523        }
524
525        // touch account. This is important as for pre SpuriousDragon account could be
526        // saved even empty.
527        Self::touch_account(last_journal, target_address, target_acc);
528
529        // If balance is zero, we don't need to add any journal entries or emit any logs.
530        if balance.is_zero() {
531            return Ok(checkpoint);
532        }
533
534        // Add balance to created account, as we already have target here.
535        let Some(new_balance) = target_acc.info.balance.checked_add(balance) else {
536            self.checkpoint_revert(checkpoint);
537            return Err(TransferError::OverflowPayment);
538        };
539        target_acc.info.balance = new_balance;
540
541        // safe to decrement for the caller as balance check is already done.
542        let caller_account = self.state.get_mut(&caller).unwrap();
543        caller_account.info.balance -= balance;
544
545        // add journal entry of transferred balance
546        last_journal.push(ENTRY::balance_transfer(caller, target_address, balance));
547
548        // EIP-7708: emit ETH transfer log
549        self.eip7708_transfer_log(caller, target_address, balance);
550
551        Ok(checkpoint)
552    }
553
554    /// Makes a checkpoint that in case of Revert can bring back state to this point.
555    #[inline]
556    pub fn checkpoint(&mut self) -> JournalCheckpoint {
557        let checkpoint = JournalCheckpoint {
558            log_i: self.logs.len(),
559            journal_i: self.journal.len(),
560            selfdestructed_i: self.selfdestructed_addresses.len(),
561        };
562        self.depth += 1;
563        checkpoint
564    }
565
566    /// Commits the checkpoint.
567    #[inline]
568    pub fn checkpoint_commit(&mut self) {
569        self.depth = self.depth.saturating_sub(1);
570    }
571
572    /// Reverts all changes to state until given checkpoint.
573    #[inline]
574    pub fn checkpoint_revert(&mut self, checkpoint: JournalCheckpoint) {
575        let is_spurious_dragon_enabled = self.cfg.spec.is_enabled_in(SPURIOUS_DRAGON);
576        let state = &mut self.state;
577        let transient_storage = &mut self.transient_storage;
578        self.depth = self.depth.saturating_sub(1);
579        self.logs.truncate(checkpoint.log_i);
580        // EIP-7708: Remove selfdestructed addresses added after checkpoint
581        self.selfdestructed_addresses
582            .truncate(checkpoint.selfdestructed_i);
583
584        // iterate over last N journals sets and revert our global state
585        if checkpoint.journal_i < self.journal.len() {
586            self.journal
587                .drain(checkpoint.journal_i..)
588                .rev()
589                .for_each(|entry| {
590                    entry.revert(state, Some(transient_storage), is_spurious_dragon_enabled);
591                });
592        }
593    }
594
595    /// Performs selfdestruct action.
596    /// Transfers balance from address to target. Check if target exist/is_cold
597    ///
598    /// Note: Balance will be lost if address and target are the same BUT when
599    /// current spec enables Cancun, this happens only when the account associated to address
600    /// is created in the same tx
601    ///
602    /// # References:
603    ///  * <https://github.com/ethereum/go-ethereum/blob/141cd425310b503c5678e674a8c3872cf46b7086/core/vm/instructions.go#L832-L833>
604    ///  * <https://github.com/ethereum/go-ethereum/blob/141cd425310b503c5678e674a8c3872cf46b7086/core/state/statedb.go#L449>
605    ///  * <https://eips.ethereum.org/EIPS/eip-6780>
606    #[inline]
607    pub fn selfdestruct<DB: Database>(
608        &mut self,
609        db: &mut DB,
610        address: Address,
611        target: Address,
612        skip_cold_load: bool,
613    ) -> Result<StateLoad<SelfDestructResult>, JournalLoadError<DB::Error>> {
614        let spec = self.cfg.spec;
615        let account_load = self.load_account_optional(db, target, false, skip_cold_load)?;
616        let is_cold = account_load.is_cold;
617        let is_empty = account_load.state_clear_aware_is_empty(spec);
618
619        if address != target {
620            // Both accounts are loaded before this point, `address` as we execute its contract.
621            // and `target` at the beginning of the function.
622            let acc_balance = self.state.get(&address).unwrap().info.balance;
623
624            let target_account = self.state.get_mut(&target).unwrap();
625            Self::touch_account(&mut self.journal, target, target_account);
626            target_account.info.balance += acc_balance;
627        }
628
629        let acc = self.state.get_mut(&address).unwrap();
630        let balance = acc.info.balance;
631
632        let destroyed_status = if !acc.is_selfdestructed() {
633            SelfdestructionRevertStatus::GloballySelfdestroyed
634        } else if !acc.is_selfdestructed_locally() {
635            SelfdestructionRevertStatus::LocallySelfdestroyed
636        } else {
637            SelfdestructionRevertStatus::RepeatedSelfdestruction
638        };
639
640        let is_cancun_enabled = spec.is_enabled_in(CANCUN);
641
642        // EIP-6780 (Cancun hard-fork): selfdestruct only if contract is created in the same tx
643        let journal_entry = if acc.is_created_locally() || !is_cancun_enabled {
644            // EIP-7708: Track first self-destruction for remaining balance log.
645            // Only track when account is actually destroyed and delayed burn is not disabled.
646            if destroyed_status == SelfdestructionRevertStatus::GloballySelfdestroyed
647                && !self.cfg.eip7708_delayed_burn_disabled
648            {
649                self.selfdestructed_addresses.push(address);
650            }
651
652            acc.mark_selfdestructed_locally();
653            acc.info.balance = U256::ZERO;
654
655            // EIP-7708: emit appropriate log for selfdestruct
656            if target != address {
657                // Transfer log for balance transferred to different address
658                self.eip7708_transfer_log(address, target, balance);
659            } else {
660                // Selfdestruct to self log
661                self.eip7708_selfdestruct_to_self_log(address, balance);
662            }
663            Some(ENTRY::account_destroyed(
664                address,
665                target,
666                destroyed_status,
667                balance,
668            ))
669        } else if address != target {
670            acc.info.balance = U256::ZERO;
671            // EIP-7708: emit appropriate log for selfdestruct
672            // Transfer log for balance transferred to different address
673            self.eip7708_transfer_log(address, target, balance);
674            Some(ENTRY::balance_transfer(address, target, balance))
675        } else {
676            // State is not changed:
677            // * if we are after Cancun upgrade and
678            // * Selfdestruct account that is created in the same transaction and
679            // * Specify the target is same as selfdestructed account. The balance stays unchanged.
680            None
681        };
682
683        if let Some(entry) = journal_entry {
684            self.journal.push(entry);
685        };
686
687        Ok(StateLoad {
688            data: SelfDestructResult {
689                had_value: !balance.is_zero(),
690                target_exists: !is_empty,
691                previously_destroyed: destroyed_status
692                    == SelfdestructionRevertStatus::RepeatedSelfdestruction,
693            },
694            is_cold,
695        })
696    }
697
698    /// Loads account into memory. return if it is cold or warm accessed
699    #[inline]
700    pub fn load_account<'a, 'db, DB: Database>(
701        &'a mut self,
702        db: &'db mut DB,
703        address: Address,
704    ) -> Result<StateLoad<&'a Account>, DB::Error>
705    where
706        'db: 'a,
707    {
708        self.load_account_optional(db, address, false, false)
709            .map_err(JournalLoadError::unwrap_db_error)
710    }
711
712    /// Loads account into memory. If account is EIP-7702 type it will additionally
713    /// load delegated account.
714    ///
715    /// It will mark both this and delegated account as warm loaded.
716    ///
717    /// Returns information about the account (If it is empty or cold loaded) and if present the information
718    /// about the delegated account (If it is cold loaded).
719    #[inline]
720    pub fn load_account_delegated<DB: Database>(
721        &mut self,
722        db: &mut DB,
723        address: Address,
724    ) -> Result<StateLoad<AccountLoad>, DB::Error> {
725        let spec = self.cfg.spec;
726        let is_eip7702_enabled = spec.is_enabled_in(SpecId::PRAGUE);
727        let account = self
728            .load_account_optional(db, address, is_eip7702_enabled, false)
729            .map_err(JournalLoadError::unwrap_db_error)?;
730        let is_empty = account.state_clear_aware_is_empty(spec);
731
732        let mut account_load = StateLoad::new(
733            AccountLoad {
734                is_delegate_account_cold: None,
735                is_empty,
736            },
737            account.is_cold,
738        );
739
740        // load delegate code if account is EIP-7702
741        if let Some(address) = account
742            .info
743            .code
744            .as_ref()
745            .and_then(Bytecode::eip7702_address)
746        {
747            let delegate_account = self
748                .load_account_optional(db, address, true, false)
749                .map_err(JournalLoadError::unwrap_db_error)?;
750            account_load.data.is_delegate_account_cold = Some(delegate_account.is_cold);
751        }
752
753        Ok(account_load)
754    }
755
756    /// Loads account and its code. If account is already loaded it will load its code.
757    ///
758    /// It will mark account as warm loaded. If not existing Database will be queried for data.
759    ///
760    /// In case of EIP-7702 delegated account will not be loaded,
761    /// [`Self::load_account_delegated`] should be used instead.
762    #[inline]
763    pub fn load_code<'a, 'db, DB: Database>(
764        &'a mut self,
765        db: &'db mut DB,
766        address: Address,
767    ) -> Result<StateLoad<&'a Account>, DB::Error>
768    where
769        'db: 'a,
770    {
771        self.load_account_optional(db, address, true, false)
772            .map_err(JournalLoadError::unwrap_db_error)
773    }
774
775    /// Loads account into memory. If account is already loaded it will be marked as warm.
776    #[inline]
777    pub fn load_account_optional<'a, 'db, DB: Database>(
778        &'a mut self,
779        db: &'db mut DB,
780        address: Address,
781        load_code: bool,
782        skip_cold_load: bool,
783    ) -> Result<StateLoad<&'a Account>, JournalLoadError<DB::Error>>
784    where
785        'db: 'a,
786    {
787        let mut load = self.load_account_mut_optional(db, address, skip_cold_load)?;
788        if load_code {
789            load.data.load_code_preserve_error()?;
790        }
791        Ok(load.map(|i| i.into_account()))
792    }
793
794    /// Loads account into memory. If account is already loaded it will be marked as warm.
795    #[inline]
796    pub fn load_account_mut<'a, 'db, DB: Database>(
797        &'a mut self,
798        db: &'db mut DB,
799        address: Address,
800    ) -> Result<StateLoad<JournaledAccount<'a, DB, ENTRY>>, DB::Error>
801    where
802        'db: 'a,
803    {
804        self.load_account_mut_optional(db, address, false)
805            .map_err(JournalLoadError::unwrap_db_error)
806    }
807
808    /// Loads account. If account is already loaded it will be marked as warm.
809    #[inline]
810    pub fn load_account_mut_optional_code<'a, 'db, DB: Database>(
811        &'a mut self,
812        db: &'db mut DB,
813        address: Address,
814        load_code: bool,
815        skip_cold_load: bool,
816    ) -> Result<StateLoad<JournaledAccount<'a, DB, ENTRY>>, JournalLoadError<DB::Error>>
817    where
818        'db: 'a,
819    {
820        let mut load = self.load_account_mut_optional(db, address, skip_cold_load)?;
821        if load_code {
822            load.data.load_code_preserve_error()?;
823        }
824        Ok(load)
825    }
826
827    /// Gets the account mut reference.
828    ///
829    /// # Load Unsafe
830    ///
831    /// Use this function only if you know what you are doing. It will not mark the account as warm or cold.
832    /// It will not bump transition_id or return if it is cold or warm loaded. This function is useful
833    /// when we know account is warm, touched and already loaded.
834    ///
835    /// It is useful when we want to access storage from account that is currently being executed.
836    #[inline]
837    pub fn get_account_mut<'a, 'db, DB: Database>(
838        &'a mut self,
839        db: &'db mut DB,
840        address: Address,
841    ) -> Option<JournaledAccount<'a, DB, ENTRY>>
842    where
843        'db: 'a,
844    {
845        let account = self.state.get_mut(&address)?;
846        Some(JournaledAccount::new(
847            address,
848            account,
849            &mut self.journal,
850            db,
851            self.warm_addresses.access_list(),
852            self.transaction_id,
853        ))
854    }
855
856    /// Loads account. If account is already loaded it will be marked as warm.
857    #[inline(never)]
858    pub fn load_account_mut_optional<'a, 'db, DB: Database>(
859        &'a mut self,
860        db: &'db mut DB,
861        address: Address,
862        skip_cold_load: bool,
863    ) -> Result<StateLoad<JournaledAccount<'a, DB, ENTRY>>, JournalLoadError<DB::Error>>
864    where
865        'db: 'a,
866    {
867        let (account, is_cold) = match self.state.entry(address) {
868            Entry::Occupied(entry) => {
869                let account = entry.into_mut();
870
871                // skip load if account is cold.
872                let mut is_cold = account.is_cold_transaction_id(self.transaction_id);
873
874                if unlikely(is_cold) {
875                    is_cold = self
876                        .warm_addresses
877                        .check_is_cold(&address, skip_cold_load)?;
878
879                    // mark it warm.
880                    account.mark_warm_with_transaction_id(self.transaction_id);
881
882                    // if it is cold loaded and we have selfdestructed locally it means that
883                    // account was selfdestructed in previous transaction and we need to clear its information and storage.
884                    if account.is_selfdestructed_locally() {
885                        account.selfdestruct();
886                        account.unmark_selfdestructed_locally();
887                    }
888                    // set original info to current info.
889                    *account.original_info = account.info.clone();
890
891                    // unmark locally created
892                    account.unmark_created_locally();
893
894                    // journal loading of cold account.
895                    self.journal.push(ENTRY::account_warmed(address));
896                }
897                (account, is_cold)
898            }
899            Entry::Vacant(vac) => {
900                // Precompiles,  among some other account(access list and coinbase included)
901                // are warm loaded so we need to take that into account
902                let is_cold = self
903                    .warm_addresses
904                    .check_is_cold(&address, skip_cold_load)?;
905
906                let account = if let Some(account) = db.basic(address)? {
907                    let mut account: Account = account.into();
908                    account.transaction_id = self.transaction_id;
909                    account
910                } else {
911                    Account::new_not_existing(self.transaction_id)
912                };
913
914                // journal loading of cold account.
915                if is_cold {
916                    self.journal.push(ENTRY::account_warmed(address));
917                }
918
919                (vac.insert(account), is_cold)
920            }
921        };
922
923        Ok(StateLoad::new(
924            JournaledAccount::new(
925                address,
926                account,
927                &mut self.journal,
928                db,
929                self.warm_addresses.access_list(),
930                self.transaction_id,
931            ),
932            is_cold,
933        ))
934    }
935
936    /// Loads storage slot.
937    #[inline]
938    pub fn sload<DB: Database>(
939        &mut self,
940        db: &mut DB,
941        address: Address,
942        key: StorageKey,
943        skip_cold_load: bool,
944    ) -> Result<StateLoad<StorageValue>, JournalLoadError<DB::Error>> {
945        self.load_account_mut(db, address)?
946            .sload_concrete_error(key, skip_cold_load)
947            .map(|s| s.map(|s| s.present_value))
948    }
949
950    /// Loads storage slot.
951    ///
952    /// If account is not present it will return [`JournalLoadError::ColdLoadSkipped`] error.
953    #[inline]
954    pub fn sload_assume_account_present<DB: Database>(
955        &mut self,
956        db: &mut DB,
957        address: Address,
958        key: StorageKey,
959        skip_cold_load: bool,
960    ) -> Result<StateLoad<StorageValue>, JournalLoadError<DB::Error>> {
961        let Some(mut account) = self.get_account_mut(db, address) else {
962            return Err(JournalLoadError::ColdLoadSkipped);
963        };
964
965        account
966            .sload_concrete_error(key, skip_cold_load)
967            .map(|s| s.map(|s| s.present_value))
968    }
969
970    /// Stores storage slot.
971    ///
972    /// If account is not present it will load from database
973    #[inline]
974    pub fn sstore<DB: Database>(
975        &mut self,
976        db: &mut DB,
977        address: Address,
978        key: StorageKey,
979        new: StorageValue,
980        skip_cold_load: bool,
981    ) -> Result<StateLoad<SStoreResult>, JournalLoadError<DB::Error>> {
982        self.load_account_mut(db, address)?
983            .sstore_concrete_error(key, new, skip_cold_load)
984    }
985
986    /// Stores storage slot.
987    ///
988    /// And returns (original,present,new) slot value.
989    ///
990    /// **Note**: Account should already be present in our state.
991    #[inline]
992    pub fn sstore_assume_account_present<DB: Database>(
993        &mut self,
994        db: &mut DB,
995        address: Address,
996        key: StorageKey,
997        new: StorageValue,
998        skip_cold_load: bool,
999    ) -> Result<StateLoad<SStoreResult>, JournalLoadError<DB::Error>> {
1000        let Some(mut account) = self.get_account_mut(db, address) else {
1001            return Err(JournalLoadError::ColdLoadSkipped);
1002        };
1003
1004        account.sstore_concrete_error(key, new, skip_cold_load)
1005    }
1006
1007    /// Read transient storage tied to the account.
1008    ///
1009    /// EIP-1153: Transient storage opcodes
1010    #[inline]
1011    pub fn tload(&mut self, address: Address, key: StorageKey) -> StorageValue {
1012        self.transient_storage
1013            .get(&(address, key))
1014            .copied()
1015            .unwrap_or_default()
1016    }
1017
1018    /// Store transient storage tied to the account.
1019    ///
1020    /// If values is different add entry to the journal
1021    /// so that old state can be reverted if that action is needed.
1022    ///
1023    /// EIP-1153: Transient storage opcodes
1024    #[inline]
1025    pub fn tstore(&mut self, address: Address, key: StorageKey, new: StorageValue) {
1026        let had_value = if new.is_zero() {
1027            // if new values is zero, remove entry from transient storage.
1028            // if previous values was some insert it inside journal.
1029            // If it is none nothing should be inserted.
1030            self.transient_storage.remove(&(address, key))
1031        } else {
1032            // insert values
1033            let previous_value = self
1034                .transient_storage
1035                .insert((address, key), new)
1036                .unwrap_or_default();
1037
1038            // check if previous value is same
1039            if previous_value != new {
1040                // if it is different, insert previous values inside journal.
1041                Some(previous_value)
1042            } else {
1043                None
1044            }
1045        };
1046
1047        if let Some(had_value) = had_value {
1048            // insert in journal only if value was changed.
1049            self.journal
1050                .push(ENTRY::transient_storage_changed(address, key, had_value));
1051        }
1052    }
1053
1054    /// Pushes log into subroutine.
1055    #[inline]
1056    pub fn log(&mut self, log: Log) {
1057        self.logs.push(log);
1058    }
1059
1060    /// Creates and pushes an EIP-7708 ETH transfer log.
1061    ///
1062    /// This emits a LOG3 with the Transfer event signature, matching ERC-20 transfer events.
1063    /// Only emitted if EIP-7708 is enabled (Amsterdam and later) and balance is non-zero.
1064    ///
1065    /// [EIP-7708](https://eips.ethereum.org/EIPS/eip-7708)
1066    #[inline]
1067    pub fn eip7708_transfer_log(&mut self, from: Address, to: Address, balance: U256) {
1068        // Only emit log if EIP-7708 is enabled and balance is non-zero
1069        if !self.cfg.spec.is_enabled_in(AMSTERDAM) || self.cfg.eip7708_disabled || balance.is_zero()
1070        {
1071            return;
1072        }
1073
1074        // Create LOG3 with Transfer(address,address,uint256) event signature
1075        // Topic[0]: Transfer event signature
1076        // Topic[1]: from address (zero-padded to 32 bytes)
1077        // Topic[2]: to address (zero-padded to 32 bytes)
1078        // Data: amount in wei (big-endian uint256)
1079        let topics = std::vec![
1080            ETH_TRANSFER_LOG_TOPIC,
1081            B256::left_padding_from(from.as_slice()),
1082            B256::left_padding_from(to.as_slice()),
1083        ];
1084        let data = Bytes::copy_from_slice(&balance.to_be_bytes::<32>());
1085
1086        self.logs.push(Log {
1087            address: ETH_TRANSFER_LOG_ADDRESS,
1088            data: LogData::new(topics, data).expect("3 topics is valid"),
1089        });
1090    }
1091
1092    /// Creates and pushes an EIP-7708 selfdestruct-to-self log.
1093    ///
1094    /// This emits a LOG2 when a contract self-destructs to itself.
1095    /// Only emitted if EIP-7708 is enabled (Amsterdam and later) and balance is non-zero.
1096    ///
1097    /// [EIP-7708](https://eips.ethereum.org/EIPS/eip-7708)
1098    #[inline]
1099    pub fn eip7708_selfdestruct_to_self_log(&mut self, address: Address, balance: U256) {
1100        // Only emit log if EIP-7708 is enabled and balance is non-zero
1101        if !self.cfg.spec.is_enabled_in(AMSTERDAM) || self.cfg.eip7708_disabled || balance.is_zero()
1102        {
1103            return;
1104        }
1105
1106        // Create LOG2 with SelfBalanceLog(address,uint256) event signature
1107        // Topic[0]: SelfBalanceLog event signature
1108        // Topic[1]: account address (zero-padded to 32 bytes)
1109        // Data: amount in wei (big-endian uint256)
1110        let topics = std::vec![
1111            SELFDESTRUCT_LOG_TOPIC,
1112            B256::left_padding_from(address.as_slice()),
1113        ];
1114        let data = Bytes::copy_from_slice(&balance.to_be_bytes::<32>());
1115
1116        self.logs.push(Log {
1117            address: ETH_TRANSFER_LOG_ADDRESS,
1118            data: LogData::new(topics, data).expect("2 topics is valid"),
1119        });
1120    }
1121}
1122
1123#[cfg(test)]
1124mod tests {
1125    use super::*;
1126    use context_interface::journaled_state::entry::JournalEntry;
1127    use database_interface::EmptyDB;
1128    use primitives::{address, HashSet, U256};
1129    use state::AccountInfo;
1130
1131    #[test]
1132    fn test_sload_skip_cold_load() {
1133        let mut journal = JournalInner::<JournalEntry>::new();
1134        let test_address = address!("1000000000000000000000000000000000000000");
1135        let test_key = U256::from(1);
1136
1137        // Insert account into state
1138        let account_info = AccountInfo {
1139            balance: U256::from(1000),
1140            nonce: 1,
1141            code_hash: KECCAK_EMPTY,
1142            code: Some(Bytecode::default()),
1143            account_id: None,
1144        };
1145        journal
1146            .state
1147            .insert(test_address, Account::from(account_info));
1148
1149        // Add storage slot to access list (make it warm)
1150        let mut access_list = HashMap::default();
1151        let mut storage_keys = HashSet::default();
1152        storage_keys.insert(test_key);
1153        access_list.insert(test_address, storage_keys);
1154        journal.warm_addresses.set_access_list(access_list);
1155
1156        // Try to sload with skip_cold_load=true - should succeed because slot is in access list
1157        let mut db = EmptyDB::new();
1158        let result = journal.sload_assume_account_present(&mut db, test_address, test_key, true);
1159
1160        // Should succeed and return as warm
1161        assert!(result.is_ok());
1162        let state_load = result.unwrap();
1163        assert!(!state_load.is_cold); // Should be warm
1164        assert_eq!(state_load.data, U256::ZERO); // Empty slot
1165    }
1166}