Skip to content

Commit

Permalink
refactor: NamedConstant
Browse files Browse the repository at this point in the history
  • Loading branch information
0xaatif committed Sep 9, 2024
1 parent 8639067 commit ab5c74e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 23 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 13 additions & 5 deletions evm_arithmetization/src/cpu/kernel/constants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ pub(crate) mod journal_entry;
pub(crate) mod trie_type;
pub(crate) mod txn_fields;

/// A named constant.
/// Prefer this over `(name, value)` tuples.
pub struct Named<'a, T> {
pub name: &'a str,
pub value: T,
}

/// Constants that are accessible to our kernel assembly code.
pub(crate) fn evm_constants() -> HashMap<String, U256> {
let mut c = HashMap::new();
Expand Down Expand Up @@ -71,8 +78,8 @@ pub(crate) fn evm_constants() -> HashMap<String, U256> {
U256::from_big_endian(&cancun_constants::BEACON_ROOTS_CONTRACT_STATE_KEY.1),
);
c.insert(
cancun_constants::HISTORY_BUFFER_LENGTH.0.into(),
cancun_constants::HISTORY_BUFFER_LENGTH.1.into(),
cancun_constants::HISTORY_BUFFER_LENGTH.name.into(),
cancun_constants::HISTORY_BUFFER_LENGTH.value,
);

c.insert(
Expand Down Expand Up @@ -424,9 +431,10 @@ pub mod cancun_constants {
// Beacon constants
///////////////////

pub const HISTORY_BUFFER_LENGTH_VALUE: u64 = 8191;
pub const HISTORY_BUFFER_LENGTH: (&str, u64) =
("HISTORY_BUFFER_LENGTH", HISTORY_BUFFER_LENGTH_VALUE);
pub const HISTORY_BUFFER_LENGTH: Named<U256> = Named {
name: "HISTORY_BUFFER_LENGTH",
value: U256([8191, 0, 0, 0]),
};

pub const BEACON_ROOTS_CONTRACT_ADDRESS: Address =
H160(hex!("000F3df6D732807Ef1319fB7B8bB8522d0Beac02"));
Expand Down
4 changes: 2 additions & 2 deletions evm_arithmetization/src/testing_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ pub fn update_beacon_roots_account_storage(
timestamp: U256,
parent_root: H256,
) -> anyhow::Result<()> {
let timestamp_idx = timestamp % HISTORY_BUFFER_LENGTH.1;
let root_idx = timestamp_idx + HISTORY_BUFFER_LENGTH.1;
let timestamp_idx = timestamp % HISTORY_BUFFER_LENGTH.value;
let root_idx = timestamp_idx + HISTORY_BUFFER_LENGTH.value;

insert_storage(storage_trie, timestamp_idx, timestamp)?;
insert_storage(storage_trie, root_idx, h2u(parent_root))
Expand Down
7 changes: 3 additions & 4 deletions trace_decoder/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use ethereum_types::{Address, U256};
use evm_arithmetization::{
generation::{mpt::AccountRlp, TrieInputs},
proof::TrieRoots,
testing_utils::{BEACON_ROOTS_CONTRACT_ADDRESS, HISTORY_BUFFER_LENGTH_VALUE},
testing_utils::{BEACON_ROOTS_CONTRACT_ADDRESS, HISTORY_BUFFER_LENGTH},
GenerationInputs,
};
use itertools::Itertools as _;
Expand Down Expand Up @@ -541,9 +541,8 @@ fn do_beacon_hook<StateTrieT: StateTrie + Clone>(
trim_state: &mut BTreeSet<TrieKey>,
state_trie: &mut StateTrieT,
) -> anyhow::Result<()> {
let history_buffer_length = U256::from(HISTORY_BUFFER_LENGTH_VALUE);
let history_timestamp = block_timestamp % history_buffer_length;
let history_timestamp_next = history_timestamp + history_buffer_length;
let history_timestamp = block_timestamp % HISTORY_BUFFER_LENGTH.value;
let history_timestamp_next = history_timestamp + HISTORY_BUFFER_LENGTH.value;
let beacon_storage = storage
.get_mut(&keccak_hash::keccak(BEACON_ROOTS_CONTRACT_ADDRESS))
.context("missing beacon contract storage trie")?;
Expand Down
1 change: 1 addition & 0 deletions zero_bin/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ build = "../common/build.rs"
[dependencies]
__compat_primitive_types = { workspace = true }
alloy = { workspace = true }
alloy-compat = "0.1.0"
anyhow = { workspace = true }
clap = { workspace = true }
futures = { workspace = true }
Expand Down
27 changes: 15 additions & 12 deletions zero_bin/rpc/src/native/state.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use alloy::primitives::Bytes;
use alloy::{
primitives::{keccak256, Address, StorageKey, B256, U256},
providers::Provider,
rpc::types::eth::{Block, BlockTransactionsKind, EIP1186AccountProofResponse},
transports::Transport,
};
use alloy_compat::Compat;
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};
Expand All @@ -17,8 +19,6 @@ use trace_decoder::{
};
use zero_bin_common::provider::CachedProvider;

use crate::Compat;

/// Processes the state witness for the given block.
pub async fn process_state_witness<ProviderT, TransportT>(
cached_provider: Arc<CachedProvider<ProviderT, TransportT>>,
Expand Down Expand Up @@ -93,13 +93,12 @@ 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 timestamp = U256::from(block.header.timestamp);

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

Expand Down Expand Up @@ -128,7 +127,7 @@ where

// Insert account proofs
for (address, proof) in account_proofs.into_iter() {
state.insert_proof(proof.account_proof.compat());
state.insert_proof(conv_vec_bytes(proof.account_proof));

let storage_mpt =
storage_proofs
Expand All @@ -138,24 +137,28 @@ where
Default::default(),
));
for proof in proof.storage_proof {
storage_mpt.insert_proof(proof.proof.compat());
storage_mpt.insert_proof(conv_vec_bytes(proof.proof));
}
}

// Insert short node variants from next proofs
for (address, proof) in next_account_proofs.into_iter() {
state.insert_short_node_variants_from_proof(proof.account_proof.compat());
state.insert_short_node_variants_from_proof(conv_vec_bytes(proof.account_proof));

if let Some(storage_mpt) = storage_proofs.get_mut(&keccak256(address)) {
for proof in proof.storage_proof {
storage_mpt.insert_short_node_variants_from_proof(proof.proof.compat());
storage_mpt.insert_short_node_variants_from_proof(conv_vec_bytes(proof.proof));
}
}
}

Ok((state, storage_proofs))
}

fn conv_vec_bytes(bytes: Vec<Bytes>) -> Vec<Vec<u8>> {
bytes.into_iter().map(|bytes| bytes.to_vec()).collect()
}

/// Fetches the proof data for the given accounts and associated storage keys.
async fn fetch_proof_data<ProviderT, TransportT>(
accounts_state: HashMap<Address, HashSet<StorageKey>>,
Expand Down

0 comments on commit ab5c74e

Please sign in to comment.