From 204a4437ff0f6c83fdf0af4fbe9119b8560c908b Mon Sep 17 00:00:00 2001 From: BGluth Date: Thu, 18 Apr 2024 18:51:27 -0600 Subject: [PATCH 1/2] Now logs time to generate proofs --- zero_bin/Cargo.lock | 4 ++ zero_bin/Cargo.toml | 2 + zero_bin/ops/Cargo.toml | 4 ++ zero_bin/ops/src/lib.rs | 128 ++++++++++++++++++++++++++++++++++++---- 4 files changed, 128 insertions(+), 10 deletions(-) diff --git a/zero_bin/Cargo.lock b/zero_bin/Cargo.lock index 5e901dc67..53a3d9706 100644 --- a/zero_bin/Cargo.lock +++ b/zero_bin/Cargo.lock @@ -2104,11 +2104,15 @@ name = "ops" version = "0.1.0" dependencies = [ "common", + "ethereum-types", "evm_arithmetization", + "keccak-hash 0.10.0", "paladin-core", "proof_gen", + "rlp", "serde", "trace_decoder", + "tracing", ] [[package]] diff --git a/zero_bin/Cargo.toml b/zero_bin/Cargo.toml index 07c6f2074..eeca6b8a8 100644 --- a/zero_bin/Cargo.toml +++ b/zero_bin/Cargo.toml @@ -16,6 +16,8 @@ serde_json = "1.0.107" ethereum-types = "0.14.1" thiserror = "1.0.50" futures = "0.3.29" +rlp = "0.5.2" +keccak-hash = "0.10.0" # zk-evm dependencies plonky2 = "0.2.0" diff --git a/zero_bin/ops/Cargo.toml b/zero_bin/ops/Cargo.toml index 48235c647..3072cdb08 100644 --- a/zero_bin/ops/Cargo.toml +++ b/zero_bin/ops/Cargo.toml @@ -14,6 +14,10 @@ serde = { workspace = true } evm_arithmetization = { workspace = true, optional = true} proof_gen = { workspace = true } trace_decoder = { workspace = true } +tracing = { workspace = true } +rlp = { workspace = true } +ethereum-types = "0.14.1" +keccak-hash = { workspace = true } common = { path = "../common" } diff --git a/zero_bin/ops/src/lib.rs b/zero_bin/ops/src/lib.rs index c1b56e062..34fe6156d 100644 --- a/zero_bin/ops/src/lib.rs +++ b/zero_bin/ops/src/lib.rs @@ -1,4 +1,7 @@ +use std::{ops::RangeInclusive, time::Instant}; + use common::prover_state::p_state; +use keccak_hash::keccak; use paladin::{ operation::{FatalError, FatalStrategy, Monoid, Operation, Result}, registry, RemoteExecute, @@ -8,22 +11,43 @@ use proof_gen::{ proof_types::{AggregatableProof, GeneratedAggProof, GeneratedBlockProof}, }; use serde::{Deserialize, Serialize}; -use trace_decoder::types::TxnProofGenIR; +use trace_decoder::types::{BlockHeight, TxnProofGenIR}; +use tracing::{event, info_span, Level}; registry!(); #[derive(Deserialize, Serialize, RemoteExecute)] pub struct TxProof; +fn run_and_wrap_closure_in_elapsed_span(f: F, ident: String) -> Result +where + F: Fn() -> Result, +{ + let _span = info_span!("proof generation", ident).entered(); + let start = Instant::now(); + + let proof = f()?; + + event!(Level::INFO, "Proof {:.4} took {:?}", ident, start.elapsed()); + Ok(proof) +} + #[cfg(not(feature = "test_only"))] impl Operation for TxProof { type Input = TxnProofGenIR; type Output = proof_gen::proof_types::AggregatableProof; fn execute(&self, input: Self::Input) -> Result { - let proof = common::prover_state::p_manager() - .generate_txn_proof(input) - .map_err(|err| FatalError::from_anyhow(err, FatalStrategy::Terminate))?; + let txn_ident = Self::txn_ident(&input); + + let proof = run_and_wrap_closure_in_elapsed_span( + || { + common::prover_state::p_manager() + .generate_txn_proof(input.clone()) + .map_err(|err| FatalError::from_anyhow(err, FatalStrategy::Terminate).into()) + }, + txn_ident, + )?; Ok(proof.into()) } @@ -35,13 +59,37 @@ impl Operation for TxProof { type Output = (); fn execute(&self, input: Self::Input) -> Result { - evm_arithmetization::prover::testing::simulate_execution::(input) - .map_err(|err| FatalError::from_anyhow(err, FatalStrategy::Terminate))?; + let txn_ident = Self::txn_ident(&input); + + run_and_wrap_closure_in_elapsed_span( + || { + evm_arithmetization::prover::testing::simulate_execution::( + input.clone(), + ) + .map_err(|err| FatalError::from_anyhow(err, FatalStrategy::Terminate).into()) + }, + txn_ident, + )?; Ok(()) } } +impl TxProof { + fn txn_ident(ir: &TxnProofGenIR) -> String { + let txn_hash_str = ir + .signed_txn + .as_ref() + .map(|txn| format!("{:x}", keccak(txn))) + .unwrap_or_else(|| "Dummy".to_string()); + + format!( + "Txn b{} - {} ({})", + ir.block_metadata.block_number, ir.txn_number_before, txn_hash_str + ) + } +} + #[derive(Deserialize, Serialize, RemoteExecute)] pub struct AggProof; @@ -49,7 +97,11 @@ impl Monoid for AggProof { type Elem = AggregatableProof; fn combine(&self, a: Self::Elem, b: Self::Elem) -> Result { - let result = generate_agg_proof(p_state(), &a, &b).map_err(FatalError::from)?; + let ident = Self::agg_ident(&a, &b); + let result = run_and_wrap_closure_in_elapsed_span( + || generate_agg_proof(p_state(), &a, &b).map_err(|e| FatalError::from(e).into()), + ident, + )?; Ok(result.into()) } @@ -60,6 +112,21 @@ impl Monoid for AggProof { } } +impl AggProof { + fn agg_ident(a: &AggregatableProof, b: &AggregatableProof) -> String { + let b_height = b_height_from_aggregatable_proof(a); + let a_range = proof_range_from_aggregatable_proof(a); + let b_range = proof_range_from_aggregatable_proof(b); + + format!( + "Agg b{} - {}..={}", + b_height, + *a_range.start(), + *b_range.end() + ) + } +} + #[derive(Deserialize, Serialize, RemoteExecute)] pub struct BlockProof { pub prev: Option, @@ -70,9 +137,50 @@ impl Operation for BlockProof { type Output = GeneratedBlockProof; fn execute(&self, input: Self::Input) -> Result { - Ok( - generate_block_proof(p_state(), self.prev.as_ref(), &input) - .map_err(FatalError::from)?, + let ident = Self::block_ident(&input); + + run_and_wrap_closure_in_elapsed_span( + || { + generate_block_proof(p_state(), self.prev.as_ref(), &input) + .map_err(|e| FatalError::from(e).into()) + }, + ident, ) } } + +impl BlockProof { + fn block_ident(p: &GeneratedAggProof) -> String { + let b_height = p.p_vals.block_metadata.block_number; + let b_range = aggregated_proof_range(p); + + format!( + "Block b{} ({}..={})", + b_height, + *b_range.start(), + *b_range.end() + ) + } +} + +fn proof_range_from_aggregatable_proof(p: &AggregatableProof) -> RangeInclusive { + match p { + AggregatableProof::Txn(info) => { + let txn_idx = info.p_vals.extra_block_data.txn_number_before.as_usize(); + txn_idx..=txn_idx + } + AggregatableProof::Agg(info) => aggregated_proof_range(info), + } +} + +fn aggregated_proof_range(p: &GeneratedAggProof) -> RangeInclusive { + p.p_vals.extra_block_data.txn_number_before.as_usize() + ..=p.p_vals.extra_block_data.txn_number_after.as_usize() +} + +fn b_height_from_aggregatable_proof(p: &AggregatableProof) -> BlockHeight { + match p { + AggregatableProof::Txn(info) => info.p_vals.block_metadata.block_number.as_u64(), + AggregatableProof::Agg(info) => info.p_vals.block_metadata.block_number.as_u64(), + } +} From 17535a910f051fa49926f6f830464715a34d0ffe Mon Sep 17 00:00:00 2001 From: BGluth Date: Fri, 19 Apr 2024 09:07:23 -0600 Subject: [PATCH 2/2] Removed agg & block proof timing logic --- zero_bin/ops/Cargo.toml | 2 +- zero_bin/ops/src/lib.rs | 87 ++++++++--------------------------------- 2 files changed, 17 insertions(+), 72 deletions(-) diff --git a/zero_bin/ops/Cargo.toml b/zero_bin/ops/Cargo.toml index 3072cdb08..9ca1ea27b 100644 --- a/zero_bin/ops/Cargo.toml +++ b/zero_bin/ops/Cargo.toml @@ -16,7 +16,7 @@ proof_gen = { workspace = true } trace_decoder = { workspace = true } tracing = { workspace = true } rlp = { workspace = true } -ethereum-types = "0.14.1" +ethereum-types = { workspace = true } keccak-hash = { workspace = true } common = { path = "../common" } diff --git a/zero_bin/ops/src/lib.rs b/zero_bin/ops/src/lib.rs index 34fe6156d..0c0b4ca22 100644 --- a/zero_bin/ops/src/lib.rs +++ b/zero_bin/ops/src/lib.rs @@ -1,4 +1,4 @@ -use std::{ops::RangeInclusive, time::Instant}; +use std::time::Instant; use common::prover_state::p_state; use keccak_hash::keccak; @@ -11,7 +11,7 @@ use proof_gen::{ proof_types::{AggregatableProof, GeneratedAggProof, GeneratedBlockProof}, }; use serde::{Deserialize, Serialize}; -use trace_decoder::types::{BlockHeight, TxnProofGenIR}; +use trace_decoder::types::TxnProofGenIR; use tracing::{event, info_span, Level}; registry!(); @@ -19,7 +19,7 @@ registry!(); #[derive(Deserialize, Serialize, RemoteExecute)] pub struct TxProof; -fn run_and_wrap_closure_in_elapsed_span(f: F, ident: String) -> Result +fn run_and_wrap_txn_proof_in_elapsed_span(f: F, ident: String) -> Result where F: Fn() -> Result, { @@ -28,7 +28,12 @@ where let proof = f()?; - event!(Level::INFO, "Proof {:.4} took {:?}", ident, start.elapsed()); + event!( + Level::INFO, + "txn proof {:.4} took {:?}", + ident, + start.elapsed() + ); Ok(proof) } @@ -40,7 +45,7 @@ impl Operation for TxProof { fn execute(&self, input: Self::Input) -> Result { let txn_ident = Self::txn_ident(&input); - let proof = run_and_wrap_closure_in_elapsed_span( + let proof = run_and_wrap_txn_proof_in_elapsed_span( || { common::prover_state::p_manager() .generate_txn_proof(input.clone()) @@ -61,7 +66,7 @@ impl Operation for TxProof { fn execute(&self, input: Self::Input) -> Result { let txn_ident = Self::txn_ident(&input); - run_and_wrap_closure_in_elapsed_span( + run_and_wrap_txn_proof_in_elapsed_span( || { evm_arithmetization::prover::testing::simulate_execution::( input.clone(), @@ -84,7 +89,7 @@ impl TxProof { .unwrap_or_else(|| "Dummy".to_string()); format!( - "Txn b{} - {} ({})", + "b{} - {} ({})", ir.block_metadata.block_number, ir.txn_number_before, txn_hash_str ) } @@ -97,11 +102,7 @@ impl Monoid for AggProof { type Elem = AggregatableProof; fn combine(&self, a: Self::Elem, b: Self::Elem) -> Result { - let ident = Self::agg_ident(&a, &b); - let result = run_and_wrap_closure_in_elapsed_span( - || generate_agg_proof(p_state(), &a, &b).map_err(|e| FatalError::from(e).into()), - ident, - )?; + let result = generate_agg_proof(p_state(), &a, &b).map_err(FatalError::from)?; Ok(result.into()) } @@ -112,21 +113,6 @@ impl Monoid for AggProof { } } -impl AggProof { - fn agg_ident(a: &AggregatableProof, b: &AggregatableProof) -> String { - let b_height = b_height_from_aggregatable_proof(a); - let a_range = proof_range_from_aggregatable_proof(a); - let b_range = proof_range_from_aggregatable_proof(b); - - format!( - "Agg b{} - {}..={}", - b_height, - *a_range.start(), - *b_range.end() - ) - } -} - #[derive(Deserialize, Serialize, RemoteExecute)] pub struct BlockProof { pub prev: Option, @@ -137,50 +123,9 @@ impl Operation for BlockProof { type Output = GeneratedBlockProof; fn execute(&self, input: Self::Input) -> Result { - let ident = Self::block_ident(&input); - - run_and_wrap_closure_in_elapsed_span( - || { - generate_block_proof(p_state(), self.prev.as_ref(), &input) - .map_err(|e| FatalError::from(e).into()) - }, - ident, - ) - } -} - -impl BlockProof { - fn block_ident(p: &GeneratedAggProof) -> String { - let b_height = p.p_vals.block_metadata.block_number; - let b_range = aggregated_proof_range(p); - - format!( - "Block b{} ({}..={})", - b_height, - *b_range.start(), - *b_range.end() + Ok( + generate_block_proof(p_state(), self.prev.as_ref(), &input) + .map_err(FatalError::from)?, ) } } - -fn proof_range_from_aggregatable_proof(p: &AggregatableProof) -> RangeInclusive { - match p { - AggregatableProof::Txn(info) => { - let txn_idx = info.p_vals.extra_block_data.txn_number_before.as_usize(); - txn_idx..=txn_idx - } - AggregatableProof::Agg(info) => aggregated_proof_range(info), - } -} - -fn aggregated_proof_range(p: &GeneratedAggProof) -> RangeInclusive { - p.p_vals.extra_block_data.txn_number_before.as_usize() - ..=p.p_vals.extra_block_data.txn_number_after.as_usize() -} - -fn b_height_from_aggregatable_proof(p: &AggregatableProof) -> BlockHeight { - match p { - AggregatableProof::Txn(info) => info.p_vals.block_metadata.block_number.as_u64(), - AggregatableProof::Agg(info) => info.p_vals.block_metadata.block_number.as_u64(), - } -}