Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add beacon roots touched slots into state_access with native tracer #353

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions zero_bin/rpc/src/native/state.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::collections::{HashMap, HashSet};

use alloy::{
primitives::{keccak256, Address, StorageKey, B256},
primitives::{keccak256, Address, StorageKey, B256, U256},
providers::Provider,
rpc::types::eth::{Block, BlockTransactionsKind, EIP1186AccountProofResponse},
transports::Transport,
};
use anyhow::Context as _;
use evm_arithmetization::testing_utils::{BEACON_ROOTS_CONTRACT_STATE_KEY, HISTORY_BUFFER_LENGTH};
use futures::future::{try_join, try_join_all};
use mpt_trie::{builder::PartialTrieBuilder, partial_trie::HashedPartialTrie};
use trace_decoder::trace_protocol::{
Expand Down Expand Up @@ -59,7 +60,8 @@ where
}

/// Iterate over the tx_infos and process the state access for each address.
/// Also includes the state access for withdrawals and the block author.
/// Also includes the state access for the beacon roots contract, withdrawals
/// and the block author.
///
/// Returns a map from address to the set of storage keys accessed by that
/// address.
Expand All @@ -69,6 +71,8 @@ pub fn process_states_access(
) -> anyhow::Result<HashMap<Address, HashSet<StorageKey>>> {
let mut state_access = HashMap::<Address, HashSet<StorageKey>>::new();

insert_beacon_roots_update(&mut state_access, block)?;

if let Some(w) = block.withdrawals.as_ref() {
w.iter().for_each(|w| {
state_access.insert(w.address, Default::default());
Expand All @@ -93,6 +97,24 @@ pub fn process_states_access(
Ok(state_access)
}

/// Cancun HF specific, see <https://eips.ethereum.org/EIPS/eip-4788>.
fn insert_beacon_roots_update(
state_access: &mut HashMap<Address, HashSet<StorageKey>>,
block: &Block,
) -> anyhow::Result<()> {
let timestamp = block.header.timestamp;

const MODULUS: u64 = HISTORY_BUFFER_LENGTH.1;

let keys = HashSet::from_iter([
U256::from(timestamp % MODULUS).into(), // timestamp_idx
U256::from((timestamp % MODULUS) + MODULUS).into(), // root_idx
]);
state_access.insert(BEACON_ROOTS_CONTRACT_STATE_KEY.1.into(), keys);

Ok(())
}

/// Generates the state witness for the given block.
async fn generate_state_witness<ProviderT, TransportT>(
prev_state_root: B256,
Expand Down
Loading