Skip to content

Commit

Permalink
review: nits
Browse files Browse the repository at this point in the history
  • Loading branch information
0xaatif committed Jun 21, 2024
1 parent b26895b commit daf294a
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 58 deletions.
18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ keywords = ["cryptography", "STARK", "plonky2", "ethereum", "zk"]
categories = ["cryptography::cryptocurrencies"]

[workspace.dependencies]
__compat_primitive_types = "0.12.2"
alloy = { git = "https://github.com/alloy-rs/alloy", tag = 'v0.1.1', default-features = false, features = [
"consensus",
"reqwest",
Expand All @@ -40,18 +41,20 @@ alloy = { git = "https://github.com/alloy-rs/alloy", tag = 'v0.1.1', default-fea
anyhow = "1.0.40"
async-stream = "0.3.5"
axum = "0.7.4"
bitflags = "2.5.0"
bitvec = "1.0.1"
bytes = "1.5.0"
ciborium = "0.2.1"
ciborium-io = "0.2.1"
clap = { version = "4.4.6", features = ["derive", "env"] }
__compat_primitive_types = "0.12.2"
criterion = "0.5.1"
dotenvy = "0.15.7"
either = "1.12.0"
enum-as-inner = "0.6.0"
enumn = "0.1.12"
env_logger = "0.10.0"
ethereum-types = "0.14.1"
eth_trie = "0.4.0"
ethereum-types = "0.14.1"
evm_arithmetization = { path = "evm_arithmetization", version = "0.2.0" }
futures = "0.3.29"
hashbrown = "0.14.0"
Expand All @@ -68,6 +71,7 @@ mpt_trie = { path = "mpt_trie", version = "0.3.0" }
num = "0.4.1"
num-bigint = "0.4.3"
num-traits = "0.2.19"
nunny = "0.2.1"
once_cell = "1.13.0"
paladin-core = "0.4.2"
parking_lot = "0.12.1"
Expand All @@ -87,6 +91,7 @@ serde_json = "1.0.96"
serde_path_to_error = "0.1.14"
serde_with = "3.4.0"
sha2 = "0.10.6"
smt_trie = { git = "https://github.com/0xPolygonZero/zk_evm", branch = "feat/type2" }
static_assertions = "1.1.0"
thiserror = "1.0.49"
tiny-keccak = "2.0.2"
Expand All @@ -96,8 +101,10 @@ tower = "0.4"
trace_decoder = { path = "trace_decoder", version = "0.4.0" }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
u4 = "0.1.0"
uint = "0.9.5"
url = "2.5.0"
winnow = "0.6.13"

# zero-bin related dependencies
ops = { path = "zero_bin/ops" }
Expand All @@ -110,10 +117,3 @@ plonky2 = "0.2.2"
plonky2_maybe_rayon = "0.2.0"
plonky2_util = "0.2.0"
starky = "0.4.0"
bitflags = "2.5.0"
bitvec = "1.0.1"
either = "1.12.0"
nunny = "0.2.1"
smt_trie = { git = "https://github.com/0xPolygonZero/zk_evm", branch = "feat/type2" }
u4 = "0.1.0"
winnow = "0.6.13"
2 changes: 1 addition & 1 deletion trace_decoder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ keccak-hash = { workspace = true }
log = { workspace = true }
mpt_trie = { workspace = true }
nunny = { workspace = true, features = ["serde"] }
plonky2.workspace = true
plonky2 = { workspace = true }
rlp = { workspace = true }
rlp-derive = { workspace = true }
serde = { workspace = true }
Expand Down
5 changes: 3 additions & 2 deletions trace_decoder/benches/block_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
//! (<https://etherscan.io/block/19240650>) containing 201 transactions and 16 withdrawals.
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use trace_decoder::{BlockTrace, OtherBlockData};

#[derive(Clone, Debug, serde::Deserialize)]
pub struct ProverInput {
pub block_trace: trace_decoder::BlockTrace,
pub other_data: trace_decoder::OtherBlockData,
pub block_trace: BlockTrace,
pub other_data: OtherBlockData,
}

fn criterion_benchmark(c: &mut Criterion) {
Expand Down
2 changes: 1 addition & 1 deletion trace_decoder/src/decoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ impl ProcessedBlockTrace {

/// If a branch collapse occurred after a delete, then we must ensure that
/// the other single child that remains also is not hashed when passed into
/// plonky2. Returns the key to the remaining child if a collapse occured.
/// plonky2. Returns the key to the remaining child if a collapse occurred.
fn delete_node_and_report_remaining_key_if_branch_collapsed(
trie: &mut HashedPartialTrie,
delete_k: &Nibbles,
Expand Down
2 changes: 1 addition & 1 deletion trace_decoder/src/hermez_cdk_erigon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct Frontend {

/// # Panics
/// - Liberally, both in this module and the [`smt_trie`] library. Therefore, do
/// NOT call this function on untrusted inputs
/// NOT call this function on untrusted inputs.
pub fn frontend(instructions: impl IntoIterator<Item = Instruction>) -> anyhow::Result<Frontend> {
let (node, code) = fold(instructions).context("couldn't fold smt from instructions")?;
let (trie, collation) =
Expand Down
5 changes: 3 additions & 2 deletions zero_bin/prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ use proof_gen::proof_types::GeneratedBlockProof;
use serde::{Deserialize, Serialize};
use tokio::io::AsyncWriteExt;
use tokio::sync::oneshot;
use trace_decoder::{BlockTrace, OtherBlockData};
use tracing::info;
use zero_bin_common::fs::generate_block_proof_file_name;

#[derive(Debug, Deserialize, Serialize)]
pub struct BlockProverInput {
pub block_trace: trace_decoder::BlockTrace,
pub other_data: trace_decoder::OtherBlockData,
pub block_trace: BlockTrace,
pub other_data: OtherBlockData,
}

impl BlockProverInput {
Expand Down
13 changes: 6 additions & 7 deletions zero_bin/rpc/src/jerigon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use alloy::{
use prover::BlockProverInput;
use serde::Deserialize;
use serde_json::json;
use trace_decoder::{BlockTrace, BlockTraceTriePreImages, CombinedPreImages, TxnInfo};

use super::fetch_other_block_data;

Expand All @@ -12,7 +13,7 @@ use super::fetch_other_block_data;
pub struct ZeroTxResult {
#[serde(rename(deserialize = "txHash"))]
pub tx_hash: alloy::primitives::TxHash,
pub result: trace_decoder::TxnInfo,
pub result: TxnInfo,
}

/// Block witness retrieved from Erigon zeroTracer.
Expand Down Expand Up @@ -46,12 +47,10 @@ where

// Assemble
Ok(BlockProverInput {
block_trace: trace_decoder::BlockTrace {
trie_pre_images: trace_decoder::BlockTraceTriePreImages::Combined(
trace_decoder::CombinedPreImages {
compact: block_witness.0,
},
),
block_trace: BlockTrace {
trie_pre_images: BlockTraceTriePreImages::Combined(CombinedPreImages {
compact: block_witness.0,
}),
txn_info: tx_results.into_iter().map(|it| it.result).collect(),
code_db: Default::default(),
},
Expand Down
7 changes: 4 additions & 3 deletions zero_bin/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use clap::ValueEnum;
use evm_arithmetization::proof::{BlockHashes, BlockMetadata};
use futures::{StreamExt as _, TryStreamExt as _};
use prover::ProverInput;
use trace_decoder::{BlockLevelData, OtherBlockData};
use zero_bin_common::block_interval::BlockInterval;

mod compat;
Expand Down Expand Up @@ -70,7 +71,7 @@ async fn fetch_other_block_data<ProviderT, TransportT>(
provider: ProviderT,
target_block_id: BlockId,
checkpoint_state_trie_root: B256,
) -> anyhow::Result<trace_decoder::OtherBlockData>
) -> anyhow::Result<OtherBlockData>
where
ProviderT: Provider<TransportT>,
TransportT: Transport + Clone,
Expand Down Expand Up @@ -112,8 +113,8 @@ where
.await
.context("couldn't fill previous hashes")?;

let other_data = trace_decoder::OtherBlockData {
b_data: trace_decoder::BlockLevelData {
let other_data = OtherBlockData {
b_data: BlockLevelData {
b_meta: BlockMetadata {
block_beneficiary: target_block.header.miner.compat(),
block_timestamp: target_block.header.timestamp.into(),
Expand Down
5 changes: 3 additions & 2 deletions zero_bin/rpc/src/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use alloy::{
use anyhow::Context as _;
use futures::try_join;
use prover::BlockProverInput;
use trace_decoder::BlockTrace;

mod state;
mod txn;
Expand Down Expand Up @@ -40,7 +41,7 @@ where
async fn process_block_trace<ProviderT, TransportT>(
provider: &ProviderT,
block_number: BlockId,
) -> anyhow::Result<trace_decoder::BlockTrace>
) -> anyhow::Result<BlockTrace>
where
ProviderT: Provider<TransportT>,
TransportT: Transport + Clone,
Expand All @@ -53,7 +54,7 @@ where
let (code_db, txn_info) = txn::process_transactions(&block, provider).await?;
let trie_pre_images = state::process_state_witness(provider, block, &txn_info).await?;

Ok(trace_decoder::BlockTrace {
Ok(BlockTrace {
txn_info,
code_db: Option::from(code_db).filter(|x| !x.is_empty()),
trie_pre_images,
Expand Down
35 changes: 16 additions & 19 deletions zero_bin/rpc/src/native/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ use alloy::{
use anyhow::Context as _;
use futures::future::{try_join, try_join_all};
use mpt_trie::{builder::PartialTrieBuilder, partial_trie::HashedPartialTrie};
use trace_decoder::{
BlockTraceTriePreImages, SeparateStorageTriesPreImage, SeparateTriePreImage,
SeparateTriePreImages, TxnInfo,
};

use crate::compat::Compat;

/// Processes the state witness for the given block.
pub async fn process_state_witness<ProviderT, TransportT>(
provider: &ProviderT,
block: Block,
txn_infos: &[trace_decoder::TxnInfo],
) -> anyhow::Result<trace_decoder::BlockTraceTriePreImages>
txn_infos: &[TxnInfo],
) -> anyhow::Result<BlockTraceTriePreImages>
where
ProviderT: Provider<TransportT>,
TransportT: Transport + Clone,
Expand All @@ -38,22 +42,15 @@ where
let (state, storage_proofs) =
generate_state_witness(prev_state_root, state_access, provider, block_number).await?;

Ok(trace_decoder::BlockTraceTriePreImages::Separate(
trace_decoder::SeparateTriePreImages {
state: trace_decoder::SeparateTriePreImage::Direct(state.build()),
storage: trace_decoder::SeparateStorageTriesPreImage::MultipleTries(
storage_proofs
.into_iter()
.map(|(a, m)| {
(
a.compat(),
trace_decoder::SeparateTriePreImage::Direct(m.build()),
)
})
.collect(),
),
},
))
Ok(BlockTraceTriePreImages::Separate(SeparateTriePreImages {
state: SeparateTriePreImage::Direct(state.build()),
storage: SeparateStorageTriesPreImage::MultipleTries(
storage_proofs
.into_iter()
.map(|(a, m)| (a.compat(), SeparateTriePreImage::Direct(m.build())))
.collect(),
),
}))
}

/// Iterate over the tx_infos and process the state access for each address.
Expand All @@ -62,7 +59,7 @@ where
/// Returns a map from address to the set of storage keys accessed by that
/// address.
pub fn process_states_access(
tx_infos: &[trace_decoder::TxnInfo],
tx_infos: &[TxnInfo],
block: &Block,
) -> anyhow::Result<HashMap<Address, HashSet<StorageKey>>> {
let mut state_access = HashMap::<Address, HashSet<StorageKey>>::new();
Expand Down
23 changes: 12 additions & 11 deletions zero_bin/rpc/src/native/txn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use alloy::{
};
use anyhow::Context as _;
use futures::stream::{FuturesOrdered, TryStreamExt};
use trace_decoder::{ContractCodeUsage, TxnInfo, TxnMeta, TxnTrace};

use super::CodeDb;
use crate::compat::Compat;
Expand All @@ -29,7 +30,7 @@ use crate::compat::Compat;
pub(super) async fn process_transactions<ProviderT, TransportT>(
block: &Block,
provider: &ProviderT,
) -> anyhow::Result<(CodeDb, Vec<trace_decoder::TxnInfo>)>
) -> anyhow::Result<(CodeDb, Vec<TxnInfo>)>
where
ProviderT: Provider<TransportT>,
TransportT: Transport + Clone,
Expand Down Expand Up @@ -57,7 +58,7 @@ where
async fn process_transaction<ProviderT, TransportT>(
provider: &ProviderT,
tx: &Transaction,
) -> anyhow::Result<(CodeDb, trace_decoder::TxnInfo)>
) -> anyhow::Result<(CodeDb, TxnInfo)>
where
ProviderT: Provider<TransportT>,
TransportT: Transport + Clone,
Expand All @@ -66,7 +67,7 @@ where
let tx_receipt = tx_receipt.map_inner(rlp::map_receipt_envelope);
let access_list = parse_access_list(tx.access_list.as_ref());

let tx_meta = trace_decoder::TxnMeta {
let tx_meta = TxnMeta {
byte_code: <Ethereum as Network>::TxEnvelope::try_from(tx.clone())?.encoded_2718(),
new_txn_trie_node_byte: vec![],
new_receipt_trie_node_byte: alloy::rlp::encode(tx_receipt.inner),
Expand All @@ -83,7 +84,7 @@ where

Ok((
code_db,
trace_decoder::TxnInfo {
TxnInfo {
meta: tx_meta,
traces: tx_traces
.into_iter()
Expand Down Expand Up @@ -137,7 +138,7 @@ async fn process_tx_traces(
mut access_list: HashMap<Address, HashSet<H256>>,
read_trace: PreStateMode,
diff_trace: DiffMode,
) -> anyhow::Result<(CodeDb, HashMap<Address, trace_decoder::TxnTrace>)> {
) -> anyhow::Result<(CodeDb, HashMap<Address, TxnTrace>)> {
let DiffMode {
pre: pre_trace,
post: post_trace,
Expand Down Expand Up @@ -171,7 +172,7 @@ async fn process_tx_traces(
let nonce = process_nonce(post_state, &code);
let self_destructed = process_self_destruct(post_state, pre_state);

let result = trace_decoder::TxnTrace {
let result = TxnTrace {
balance,
nonce,
storage_read,
Expand All @@ -191,12 +192,12 @@ async fn process_tx_traces(
/// If a contract is created, the nonce is set to 1.
fn process_nonce(
post_state: Option<&AccountState>,
code_usage: &Option<trace_decoder::ContractCodeUsage>,
code_usage: &Option<ContractCodeUsage>,
) -> Option<U256> {
post_state
.and_then(|x| x.nonce.map(Into::into))
.or_else(|| {
if let Some(trace_decoder::ContractCodeUsage::Write(_)) = code_usage.as_ref() {
if let Some(ContractCodeUsage::Write(_)) = code_usage.as_ref() {
Some(U256::from(1))
} else {
None
Expand Down Expand Up @@ -256,21 +257,21 @@ async fn process_code(
post_state: Option<&AccountState>,
read_state: Option<&AccountState>,
code_db: &mut CodeDb,
) -> Option<trace_decoder::ContractCodeUsage> {
) -> Option<ContractCodeUsage> {
match (
post_state.and_then(|x| x.code.as_ref()),
read_state.and_then(|x| x.code.as_ref()),
) {
(Some(post_code), _) => {
let code_hash = keccak256(post_code).compat();
code_db.insert(code_hash, post_code.to_vec());
Some(trace_decoder::ContractCodeUsage::Write(post_code.to_vec()))
Some(ContractCodeUsage::Write(post_code.to_vec()))
}
(_, Some(read_code)) => {
let code_hash = keccak256(read_code).compat();
code_db.insert(code_hash, read_code.to_vec());

Some(trace_decoder::ContractCodeUsage::Read(code_hash))
Some(ContractCodeUsage::Read(code_hash))
}
_ => None,
}
Expand Down

0 comments on commit daf294a

Please sign in to comment.