Skip to main content

reth_execution_types/
compat.rs

1//! State conversion at the evm2 execution boundary.
2
3use alloy_primitives::{keccak256, B256};
4use reth_primitives_traits::Account;
5use reth_trie_common::{HashedPostState, HashedStorage};
6
7pub use evm2::block::{AccountUpdate, BlockState, BundleSource, StateUpdate, TransactionChanges};
8
9/// Hashes a transaction's state update into the trie update consumed by state root tasks.
10pub fn state_update_to_hashed_post_state(update: &StateUpdate) -> HashedPostState {
11    let mut hashed_state = HashedPostState::with_capacity(update.accounts.len());
12    for account in &update.accounts {
13        let hashed_address = keccak256(account.address);
14        let destroyed = account.current.is_none();
15        let info = account.current.clone().unwrap_or_default();
16        let unchanged = account.original.as_ref() == Some(&info);
17        // A deletion is explicit: an account that ends up empty is still present unless the
18        // producer removed it, as EIP-161 does from Spurious Dragon on.
19        if destroyed {
20            hashed_state.accounts.insert(hashed_address, None);
21        } else if !unchanged {
22            hashed_state.accounts.insert(hashed_address, Some(Account::from(&info)));
23        }
24        if !destroyed && !account.storage.is_empty() {
25            hashed_state.storages.insert(
26                hashed_address,
27                HashedStorage::from_iter(
28                    account
29                        .storage
30                        .iter()
31                        .map(|(key, slot)| (keccak256(B256::from(*key)), slot.present_value)),
32                ),
33            );
34        }
35    }
36    hashed_state
37}