From b7bee7278f7f0b7480f2be2c114298d87389edf8 Mon Sep 17 00:00:00 2001 From: Robin Salen <30937548+Nashtare@users.noreply.github.com> Date: Wed, 11 Sep 2024 18:30:27 -0400 Subject: [PATCH 1/2] Tweak naming and move to common crate (#619) --- Cargo.lock | 1 + common/src/lib.rs | 22 +++++++++++++++++++- evm_arithmetization/Cargo.toml | 1 + evm_arithmetization/src/testing_utils.rs | 6 ------ evm_arithmetization/tests/selfdestruct.rs | 3 ++- evm_arithmetization/tests/simple_transfer.rs | 3 ++- trace_decoder/src/decoding.rs | 8 ++----- 7 files changed, 29 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dcd859ede..85268325c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2075,6 +2075,7 @@ dependencies = [ "static_assertions", "thiserror", "tiny-keccak", + "zk_evm_common", "zk_evm_proc_macro", ] diff --git a/common/src/lib.rs b/common/src/lib.rs index 9f33bfaf1..bac6dd440 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -1,4 +1,4 @@ -use ethereum_types::H256; +use ethereum_types::{H256, U256}; /// The hash value of an account empty EVM code. /// 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 @@ -14,6 +14,26 @@ pub const EMPTY_TRIE_HASH: H256 = H256([ 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33, ]); +/// Converts an amount in `ETH` to `wei` units. +pub fn eth_to_wei(eth: U256) -> U256 { + // 1 ether = 10^18 wei. + eth * U256::from(10).pow(18.into()) +} + +/// Converts an amount in `gwei` to `wei` units. +/// This also works for converting `ETH` to `gwei`. +pub fn gwei_to_wei(eth: U256) -> U256 { + // 1 ether = 10^9 gwei = 10^18 wei. + eth * U256::from(10).pow(9.into()) +} + +#[test] +fn test_eth_conversion() { + assert_eq!( + eth_to_wei(U256::one()), + gwei_to_wei(gwei_to_wei(U256::one())) + ); +} #[test] fn test_empty_code_hash() { assert_eq!(EMPTY_CODE_HASH, keccak_hash::keccak([])); diff --git a/evm_arithmetization/Cargo.toml b/evm_arithmetization/Cargo.toml index a2b38cc7e..90d01c071 100644 --- a/evm_arithmetization/Cargo.toml +++ b/evm_arithmetization/Cargo.toml @@ -50,6 +50,7 @@ serde-big-array = { workspace = true } mpt_trie = { workspace = true } smt_trie = { workspace = true, optional = true } zk_evm_proc_macro = { workspace = true } +zk_evm_common = { workspace = true } [dev-dependencies] criterion = { workspace = true } diff --git a/evm_arithmetization/src/testing_utils.rs b/evm_arithmetization/src/testing_utils.rs index 4152da5b6..36b4dedb1 100644 --- a/evm_arithmetization/src/testing_utils.rs +++ b/evm_arithmetization/src/testing_utils.rs @@ -161,9 +161,3 @@ pub fn scalable_contract_from_storage(storage_trie: &HashedPartialTrie) -> Accou ..Default::default() } } - -/// Converts an amount in `ETH` to `wei` units. -pub fn eth_to_wei(eth: U256) -> U256 { - // 1 ether = 10^18 wei. - eth * U256::from(10).pow(18.into()) -} diff --git a/evm_arithmetization/tests/selfdestruct.rs b/evm_arithmetization/tests/selfdestruct.rs index 271630512..dde580498 100644 --- a/evm_arithmetization/tests/selfdestruct.rs +++ b/evm_arithmetization/tests/selfdestruct.rs @@ -9,7 +9,7 @@ use evm_arithmetization::generation::{GenerationInputs, TrieInputs}; use evm_arithmetization::proof::{BlockHashes, BlockMetadata, TrieRoots}; use evm_arithmetization::prover::testing::prove_all_segments; use evm_arithmetization::testing_utils::{ - beacon_roots_account_nibbles, beacon_roots_contract_from_storage, eth_to_wei, init_logger, + beacon_roots_account_nibbles, beacon_roots_contract_from_storage, init_logger, preinitialized_state_and_storage_tries, update_beacon_roots_account_storage, }; use evm_arithmetization::verifier::testing::verify_all_proofs; @@ -21,6 +21,7 @@ use mpt_trie::partial_trie::{HashedPartialTrie, PartialTrie}; use plonky2::field::goldilocks_field::GoldilocksField; use plonky2::plonk::config::KeccakGoldilocksConfig; use plonky2::util::timing::TimingTree; +use zk_evm_common::eth_to_wei; type F = GoldilocksField; const D: usize = 2; diff --git a/evm_arithmetization/tests/simple_transfer.rs b/evm_arithmetization/tests/simple_transfer.rs index 81454fb0e..2e371071c 100644 --- a/evm_arithmetization/tests/simple_transfer.rs +++ b/evm_arithmetization/tests/simple_transfer.rs @@ -10,7 +10,7 @@ use evm_arithmetization::generation::{GenerationInputs, TrieInputs}; use evm_arithmetization::proof::{BlockHashes, BlockMetadata, TrieRoots}; use evm_arithmetization::prover::testing::prove_all_segments; use evm_arithmetization::testing_utils::{ - beacon_roots_account_nibbles, beacon_roots_contract_from_storage, eth_to_wei, init_logger, + beacon_roots_account_nibbles, beacon_roots_contract_from_storage, init_logger, preinitialized_state_and_storage_tries, update_beacon_roots_account_storage, }; use evm_arithmetization::verifier::testing::verify_all_proofs; @@ -22,6 +22,7 @@ use mpt_trie::partial_trie::{HashedPartialTrie, PartialTrie}; use plonky2::field::goldilocks_field::GoldilocksField; use plonky2::plonk::config::KeccakGoldilocksConfig; use plonky2::util::timing::TimingTree; +use zk_evm_common::eth_to_wei; type F = GoldilocksField; const D: usize = 2; diff --git a/trace_decoder/src/decoding.rs b/trace_decoder/src/decoding.rs index b0f909374..59af0dfb2 100644 --- a/trace_decoder/src/decoding.rs +++ b/trace_decoder/src/decoding.rs @@ -20,6 +20,7 @@ use mpt_trie::{ trie_ops::TrieOpError, utils::{IntoTrieKey as _, TriePath}, }; +use zk_evm_common::gwei_to_wei; use crate::{ hash, @@ -442,7 +443,7 @@ fn add_withdrawals_to_txns( ) -> anyhow::Result<()> { // Scale withdrawals amounts. for (_addr, amt) in withdrawals.iter_mut() { - *amt = eth_to_gwei(*amt) + *amt = gwei_to_wei(*amt) } let withdrawals_with_hashed_addrs_iter = || { @@ -692,11 +693,6 @@ fn create_trie_subset_wrapped( .context(format!("missing keys when creating {}", trie_type)) } -fn eth_to_gwei(eth: U256) -> U256 { - // 1 ether = 10^9 gwei. - eth * U256::from(10).pow(9.into()) -} - // This is just `rlp(0)`. const ZERO_STORAGE_SLOT_VAL_RLPED: [u8; 1] = [128]; From 7d436c1546fd73b41751d9a2c3b66f42f4cd4e4d Mon Sep 17 00:00:00 2001 From: Marko Atanasievski Date: Thu, 12 Sep 2024 11:27:52 +0200 Subject: [PATCH 2/2] chore: move utility functions to common crate (#615) * chore: move utility functions to common crate * fix: cleanup * chore: move get previous proof * chore: cleanup dependencies --- Cargo.lock | 8 ++--- trace_decoder/Cargo.toml | 1 - zero_bin/common/Cargo.toml | 9 ++--- zero_bin/common/src/env.rs | 16 +++++++++ zero_bin/common/src/fs.rs | 15 ++++++++ zero_bin/common/src/lib.rs | 2 ++ .../src/init.rs => common/src/tracing.rs} | 3 +- zero_bin/leader/Cargo.toml | 18 +++++----- zero_bin/leader/src/main.rs | 36 +++---------------- zero_bin/ops/Cargo.toml | 5 +-- zero_bin/rpc/Cargo.toml | 4 +-- zero_bin/verifier/Cargo.toml | 8 ++--- zero_bin/worker/src/init.rs | 11 ------ zero_bin/worker/src/main.rs | 6 ++-- 14 files changed, 65 insertions(+), 77 deletions(-) create mode 100644 zero_bin/common/src/env.rs rename zero_bin/{leader/src/init.rs => common/src/tracing.rs} (92%) delete mode 100644 zero_bin/worker/src/init.rs diff --git a/Cargo.lock b/Cargo.lock index 85268325c..be7d8a8c0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2860,7 +2860,6 @@ dependencies = [ "axum", "cargo_metadata", "clap", - "dotenvy", "evm_arithmetization", "futures", "ops", @@ -2872,7 +2871,6 @@ dependencies = [ "serde_json", "serde_path_to_error", "tokio", - "toml", "tracing", "tracing-subscriber", "vergen", @@ -3303,7 +3301,6 @@ dependencies = [ "paladin-core", "proof_gen", "serde", - "trace_decoder", "tracing", "zero_bin_common", ] @@ -5136,7 +5133,6 @@ dependencies = [ "nunny", "plonky2", "plonky2_maybe_rayon 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "pretty_env_logger", "prover", "rlp", "serde", @@ -5814,6 +5810,7 @@ dependencies = [ "cargo_metadata", "clap", "directories", + "dotenvy", "evm_arithmetization", "futures", "lru", @@ -5822,10 +5819,11 @@ dependencies = [ "proof_gen", "serde", "serde_json", + "serde_path_to_error", "thiserror", "tokio", - "trace_decoder", "tracing", + "tracing-subscriber", "vergen", ] diff --git a/trace_decoder/Cargo.toml b/trace_decoder/Cargo.toml index 4febd85f2..8bdeaf022 100644 --- a/trace_decoder/Cargo.toml +++ b/trace_decoder/Cargo.toml @@ -50,7 +50,6 @@ criterion = { workspace = true } glob = "0.3.1" libtest-mimic = "0.7.3" plonky2_maybe_rayon = { workspace = true } -pretty_env_logger = { workspace = true } prover = { workspace = true } serde_json = { workspace = true } serde_path_to_error = { workspace = true } diff --git a/zero_bin/common/Cargo.toml b/zero_bin/common/Cargo.toml index 0b389cf9f..c66397ca1 100644 --- a/zero_bin/common/Cargo.toml +++ b/zero_bin/common/Cargo.toml @@ -14,15 +14,18 @@ anyhow = { workspace = true } async-stream = { workspace = true } cargo_metadata = { workspace = true } clap = { workspace = true } +dotenvy = { workspace = true } futures = { workspace = true } lru = { workspace = true } once_cell = { workspace = true } plonky2 = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } +serde_path_to_error = { workspace = true } thiserror = { workspace = true } tokio = { workspace = true } tracing = { workspace = true } +tracing-subscriber = { workspace = true } vergen = { workspace = true } directories = "5.0.1" @@ -30,24 +33,22 @@ directories = "5.0.1" # Local dependencies evm_arithmetization = { workspace = true } proof_gen = { workspace = true } -trace_decoder = { workspace = true } [build-dependencies] +anyhow = { workspace = true } cargo_metadata = { workspace = true } vergen = { workspace = true } -anyhow = { workspace = true } + [features] default = ["eth_mainnet"] eth_mainnet = [ "evm_arithmetization/eth_mainnet", "proof_gen/eth_mainnet", - "trace_decoder/eth_mainnet", ] cdk_erigon = [ "evm_arithmetization/cdk_erigon", "proof_gen/cdk_erigon", - "trace_decoder/cdk_erigon", ] [lints] diff --git a/zero_bin/common/src/env.rs b/zero_bin/common/src/env.rs new file mode 100644 index 000000000..f1356247e --- /dev/null +++ b/zero_bin/common/src/env.rs @@ -0,0 +1,16 @@ +use std::io; + +use dotenvy::dotenv; +use tracing::warn; + +/// Attempt to load in the local `.env` if present and set any environment +/// variables specified inside of it. +/// +/// To keep things simple, any IO error we will treat as the file not existing +/// and continue moving on without the `env` variables set. +pub fn load_dotenvy_vars_if_present() { + match dotenv() { + Ok(_) | Err(dotenvy::Error::Io(io::Error { .. })) => (), + Err(e) => warn!("Found local `.env` file but was unable to parse it! (err: {e})",), + } +} diff --git a/zero_bin/common/src/fs.rs b/zero_bin/common/src/fs.rs index 7603f5100..6d274903e 100644 --- a/zero_bin/common/src/fs.rs +++ b/zero_bin/common/src/fs.rs @@ -1,7 +1,22 @@ +use std::fs::File; use std::path::PathBuf; +use proof_gen::proof_types::GeneratedBlockProof; + pub fn generate_block_proof_file_name(directory: &Option<&str>, block_height: u64) -> PathBuf { let mut path = PathBuf::from(directory.unwrap_or("")); path.push(format!("b{}.zkproof", block_height)); path } + +pub fn get_previous_proof(path: Option) -> anyhow::Result> { + if path.is_none() { + return Ok(None); + } + + let path = path.unwrap(); + let file = File::open(path)?; + let des = &mut serde_json::Deserializer::from_reader(&file); + let proof: GeneratedBlockProof = serde_path_to_error::deserialize(des)?; + Ok(Some(proof)) +} diff --git a/zero_bin/common/src/lib.rs b/zero_bin/common/src/lib.rs index 42ce661d7..d810ecb53 100644 --- a/zero_bin/common/src/lib.rs +++ b/zero_bin/common/src/lib.rs @@ -1,10 +1,12 @@ pub mod block_interval; pub mod debug_utils; +pub mod env; pub mod fs; pub mod parsing; pub mod pre_checks; pub mod prover_state; pub mod provider; +pub mod tracing; pub mod version; /// Size of the channel used to send block prover inputs to the per block diff --git a/zero_bin/leader/src/init.rs b/zero_bin/common/src/tracing.rs similarity index 92% rename from zero_bin/leader/src/init.rs rename to zero_bin/common/src/tracing.rs index f93914895..6c3f9a8bc 100644 --- a/zero_bin/leader/src/init.rs +++ b/zero_bin/common/src/tracing.rs @@ -1,5 +1,6 @@ use tracing_subscriber::{prelude::*, util::SubscriberInitExt, EnvFilter}; -pub(crate) fn tracing() { + +pub fn init() { tracing_subscriber::Registry::default() .with( tracing_subscriber::fmt::layer() diff --git a/zero_bin/leader/Cargo.toml b/zero_bin/leader/Cargo.toml index 5b9a67f34..c90d70948 100644 --- a/zero_bin/leader/Cargo.toml +++ b/zero_bin/leader/Cargo.toml @@ -10,20 +10,18 @@ categories.workspace = true build = "../common/build.rs" [dependencies] -paladin-core = { workspace = true } -clap = { workspace = true } -tracing = { workspace = true } -tracing-subscriber = { workspace = true } +alloy = { workspace = true } anyhow = { workspace = true } +axum = { workspace = true } +clap = { workspace = true } +futures = { workspace = true } +paladin-core = { workspace = true } serde = { workspace = true } -dotenvy = { workspace = true } -tokio = { workspace = true } serde_json = { workspace = true } serde_path_to_error = { workspace = true } -futures = { workspace = true } -alloy.workspace = true -axum = { workspace = true } -toml = { workspace = true } +tokio = { workspace = true } +tracing = { workspace = true } +tracing-subscriber = { workspace = true } # Local dependencies evm_arithmetization = { workspace = true } diff --git a/zero_bin/leader/src/main.rs b/zero_bin/leader/src/main.rs index c0311b15c..b230009cc 100644 --- a/zero_bin/leader/src/main.rs +++ b/zero_bin/leader/src/main.rs @@ -1,17 +1,16 @@ +use std::env; use std::sync::Arc; -use std::{env, io}; -use std::{fs::File, path::PathBuf}; use anyhow::Result; use clap::Parser; use cli::Command; use client::RpcParams; -use dotenvy::dotenv; use ops::register; use paladin::runtime::Runtime; -use proof_gen::proof_types::GeneratedBlockProof; use prover::ProverConfig; -use tracing::{info, warn}; +use tracing::info; +use zero_bin_common::env::load_dotenvy_vars_if_present; +use zero_bin_common::fs::get_previous_proof; use zero_bin_common::{ block_interval::BlockInterval, prover_state::persistence::set_circuit_cache_dir_env_if_not_set, }; @@ -22,26 +21,13 @@ use crate::client::{client_main, LeaderConfig}; mod cli; mod client; mod http; -mod init; mod stdio; -fn get_previous_proof(path: Option) -> Result> { - if path.is_none() { - return Ok(None); - } - - let path = path.unwrap(); - let file = File::open(path)?; - let des = &mut serde_json::Deserializer::from_reader(&file); - let proof: GeneratedBlockProof = serde_path_to_error::deserialize(des)?; - Ok(Some(proof)) -} - #[tokio::main] async fn main() -> Result<()> { load_dotenvy_vars_if_present(); set_circuit_cache_dir_env_if_not_set()?; - init::tracing(); + zero_bin_common::tracing::init(); let args: Vec = env::args().collect(); @@ -127,15 +113,3 @@ async fn main() -> Result<()> { Ok(()) } - -/// Attempt to load in the local `.env` if present and set any environment -/// variables specified inside of it. -/// -/// To keep things simple, any IO error we will treat as the file not existing -/// and continue moving on without the `env` variables set. -fn load_dotenvy_vars_if_present() { - match dotenv() { - Ok(_) | Err(dotenvy::Error::Io(io::Error { .. })) => (), - Err(e) => warn!("Found local `.env` file but was unable to parse it! (err: {e})",), - } -} diff --git a/zero_bin/ops/Cargo.toml b/zero_bin/ops/Cargo.toml index 4e7bef01a..14b59eadd 100644 --- a/zero_bin/ops/Cargo.toml +++ b/zero_bin/ops/Cargo.toml @@ -9,15 +9,14 @@ keywords.workspace = true categories.workspace = true [dependencies] +keccak-hash = { workspace = true } paladin-core = { workspace = true } serde = { workspace = true } tracing = { workspace = true } -keccak-hash = { workspace = true } # Local dependencies evm_arithmetization = { workspace = true } proof_gen = { workspace = true } -trace_decoder = { workspace = true } zero_bin_common = { workspace = true } [features] @@ -25,12 +24,10 @@ default = ["eth_mainnet"] eth_mainnet = [ "evm_arithmetization/eth_mainnet", "proof_gen/eth_mainnet", - "trace_decoder/eth_mainnet", "zero_bin_common/eth_mainnet", ] cdk_erigon = [ "evm_arithmetization/cdk_erigon", "proof_gen/cdk_erigon", - "trace_decoder/cdk_erigon", "zero_bin_common/cdk_erigon", ] diff --git a/zero_bin/rpc/Cargo.toml b/zero_bin/rpc/Cargo.toml index 8d4444bb6..64e494ca8 100644 --- a/zero_bin/rpc/Cargo.toml +++ b/zero_bin/rpc/Cargo.toml @@ -16,6 +16,7 @@ anyhow = { workspace = true } clap = { workspace = true } futures = { workspace = true } hex = { workspace = true } +itertools = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } tokio = { workspace = true } @@ -23,7 +24,6 @@ tower = { workspace = true, features = ["retry"] } tracing = { workspace = true } tracing-subscriber = { workspace = true } url = { workspace = true } -itertools = { workspace = true } # Local dependencies compat = { workspace = true } @@ -34,9 +34,9 @@ trace_decoder = { workspace = true } zero_bin_common = { workspace = true } [build-dependencies] +anyhow = { workspace = true } cargo_metadata = { workspace = true } vergen = { workspace = true } -anyhow = { workspace = true } [features] default = ["eth_mainnet"] diff --git a/zero_bin/verifier/Cargo.toml b/zero_bin/verifier/Cargo.toml index 80d64df1f..259c6d3f5 100644 --- a/zero_bin/verifier/Cargo.toml +++ b/zero_bin/verifier/Cargo.toml @@ -6,22 +6,22 @@ edition = "2021" build = "../common/build.rs" [dependencies] +anyhow = { workspace = true } clap = { workspace = true } -tracing = { workspace = true } -tracing-subscriber = { workspace = true } dotenvy = { workspace = true } -anyhow = { workspace = true } serde_json = { workspace = true } serde_path_to_error = { workspace = true } +tracing = { workspace = true } +tracing-subscriber = { workspace = true } # Local dependencies proof_gen = { workspace = true } zero_bin_common = { workspace = true } [build-dependencies] +anyhow = { workspace = true } cargo_metadata = { workspace = true } vergen = { workspace = true } -anyhow = { workspace = true } [features] diff --git a/zero_bin/worker/src/init.rs b/zero_bin/worker/src/init.rs deleted file mode 100644 index f93914895..000000000 --- a/zero_bin/worker/src/init.rs +++ /dev/null @@ -1,11 +0,0 @@ -use tracing_subscriber::{prelude::*, util::SubscriberInitExt, EnvFilter}; -pub(crate) fn tracing() { - tracing_subscriber::Registry::default() - .with( - tracing_subscriber::fmt::layer() - .with_ansi(false) - .compact() - .with_filter(EnvFilter::from_default_env()), - ) - .init(); -} diff --git a/zero_bin/worker/src/main.rs b/zero_bin/worker/src/main.rs index 08b125ad9..3ea4c7cae 100644 --- a/zero_bin/worker/src/main.rs +++ b/zero_bin/worker/src/main.rs @@ -9,9 +9,7 @@ use zero_bin_common::prover_state::{ cli::CliProverStateConfig, persistence::{set_circuit_cache_dir_env_if_not_set, CIRCUIT_VERSION}, }; -use zero_bin_common::version; - -mod init; +use zero_bin_common::{tracing, version}; // TODO: https://github.com/0xPolygonZero/zk_evm/issues/302 // this should probably be removed. @@ -40,7 +38,7 @@ async fn main() -> Result<()> { } dotenv().ok(); - init::tracing(); + tracing::init(); set_circuit_cache_dir_env_if_not_set()?; let args = Cli::parse();