Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit 1beeb35

Browse files
committed
chunk prover error and refactor
1 parent b621bce commit 1beeb35

File tree

12 files changed

+157
-111
lines changed

12 files changed

+157
-111
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prover/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ serde.workspace = true
3434
serde_derive = "1.0"
3535
serde_json = { workspace = true, features = ["unbounded_depth"] }
3636
serde_stacker.workspace = true
37+
thiserror = "1.0"
3738
sha2 ="0.10.2"
3839
revm = { version = "3.5.0", default-features = false, features = ["std"] }
3940

prover/src/aggregator/prover.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,11 @@ pub fn check_chunk_hashes(
345345
) -> Result<()> {
346346
for (idx, (in_arg, chunk_proof)) in chunk_hashes_proofs.iter().enumerate() {
347347
let in_proof = &chunk_proof.chunk_info;
348-
crate::proof::compare_chunk_info(&format!("{name} chunk num {idx}"), in_arg, in_proof)?;
348+
if let Err(e) =
349+
crate::proof::compare_chunk_info(&format!("{name} chunk num {idx}"), in_arg, in_proof)
350+
{
351+
bail!(e);
352+
}
349353
}
350354
Ok(())
351355
}

prover/src/inner/prover.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use crate::{
22
common,
33
config::INNER_DEGREE,
44
io::serialize_vk,
5-
utils::{chunk_trace_to_witness_block, gen_rng},
6-
zkevm::circuit::TargetCircuit,
5+
utils::gen_rng,
6+
zkevm::circuit::{chunk_trace_to_witness_block, TargetCircuit},
77
Proof,
88
};
99
use anyhow::Result;

prover/src/proof/chunk.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use super::{dump_as_json, dump_data, dump_vk, from_json_file, Proof};
22
use crate::{types::base64, zkevm::SubCircuitRowUsage};
33
use aggregator::ChunkInfo;
4-
use anyhow::{bail, Result};
54
use halo2_proofs::{halo2curves::bn256::G1Affine, plonk::ProvingKey};
65
use serde_derive::{Deserialize, Serialize};
76
use snark_verifier::Protocol;
@@ -37,33 +36,34 @@ pub struct ChunkProof {
3736
macro_rules! compare_field {
3837
($desc:expr, $field:ident, $lhs:ident, $rhs:ident) => {
3938
if $lhs.$field != $rhs.$field {
40-
bail!(
39+
return Err(format!(
4140
"{} chunk different {}: {} != {}",
4241
$desc,
4342
stringify!($field),
4443
$lhs.$field,
4544
$rhs.$field
46-
);
45+
));
4746
}
4847
};
4948
}
5049

5150
/// Check chunk info is consistent with chunk info embedded inside proof
52-
pub fn compare_chunk_info(name: &str, lhs: &ChunkInfo, rhs: &ChunkInfo) -> Result<()> {
51+
pub fn compare_chunk_info(name: &str, lhs: &ChunkInfo, rhs: &ChunkInfo) -> Result<(), String> {
5352
compare_field!(name, chain_id, lhs, rhs);
5453
compare_field!(name, prev_state_root, lhs, rhs);
5554
compare_field!(name, post_state_root, lhs, rhs);
5655
compare_field!(name, withdraw_root, lhs, rhs);
5756
compare_field!(name, data_hash, lhs, rhs);
5857
if lhs.tx_bytes != rhs.tx_bytes {
59-
bail!(
58+
return Err(format!(
6059
"{} chunk different {}: {} != {}",
6160
name,
6261
"tx_bytes",
6362
hex::encode(&lhs.tx_bytes),
6463
hex::encode(&rhs.tx_bytes)
65-
);
64+
));
6665
}
66+
6767
Ok(())
6868
}
6969

@@ -74,7 +74,7 @@ impl ChunkProof {
7474
chunk_info: ChunkInfo,
7575
chunk_kind: ChunkKind,
7676
row_usages: Vec<SubCircuitRowUsage>,
77-
) -> Result<Self> {
77+
) -> anyhow::Result<Self> {
7878
let protocol = serde_json::to_vec(&snark.protocol)?;
7979
let proof = Proof::new(snark.proof, &snark.instances, pk);
8080

@@ -87,11 +87,11 @@ impl ChunkProof {
8787
})
8888
}
8989

90-
pub fn from_json_file(dir: &str, name: &str) -> Result<Self> {
90+
pub fn from_json_file(dir: &str, name: &str) -> anyhow::Result<Self> {
9191
from_json_file(dir, &dump_filename(name))
9292
}
9393

94-
pub fn dump(&self, dir: &str, name: &str) -> Result<()> {
94+
pub fn dump(&self, dir: &str, name: &str) -> anyhow::Result<()> {
9595
let filename = dump_filename(name);
9696

9797
// Dump vk and protocol.

prover/src/utils.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#![allow(deprecated)]
2-
use crate::{
3-
types::BlockTraceJsonRpcResult,
4-
zkevm::circuit::{block_traces_to_witness_block, print_chunk_stats},
5-
};
2+
use crate::types::BlockTraceJsonRpcResult;
63
use anyhow::{bail, Result};
74
use chrono::Utc;
85
use eth_types::l2_types::BlockTrace;
@@ -127,14 +124,6 @@ pub fn metric_of_witness_block(block: &Block) -> ChunkMetric {
127124
}
128125
}
129126

130-
pub fn chunk_trace_to_witness_block(chunk_trace: Vec<BlockTrace>) -> Result<Block> {
131-
if chunk_trace.is_empty() {
132-
bail!("Empty chunk trace");
133-
}
134-
print_chunk_stats(&chunk_trace);
135-
block_traces_to_witness_block(chunk_trace)
136-
}
137-
138127
// Return the output dir.
139128
pub fn init_env_and_log(id: &str) -> String {
140129
dotenvy::dotenv().ok();

prover/src/zkevm.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#[cfg(feature = "scroll")]
22
mod capacity_checker;
33
pub mod circuit;
4+
mod error;
45
mod prover;
56
mod verifier;
67

78
pub use self::prover::Prover;
89
#[cfg(feature = "scroll")]
910
pub use capacity_checker::{CircuitCapacityChecker, RowUsage};
11+
pub use error::ChunkProverError;
1012
use serde::{Deserialize, Serialize};
1113
pub use verifier::Verifier;
1214

prover/src/zkevm/capacity_checker.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,7 @@ impl CircuitCapacityChecker {
137137
self.acc_row_usage.clone()
138138
}
139139
}
140-
pub fn estimate_circuit_capacity(
141-
&mut self,
142-
trace: BlockTrace,
143-
) -> Result<RowUsage, anyhow::Error> {
140+
pub fn estimate_circuit_capacity(&mut self, trace: BlockTrace) -> anyhow::Result<RowUsage> {
144141
let (mut estimate_builder, codedb_prev) =
145142
if let Some((code_db, sdb, mpt_state)) = self.builder_ctx.take() {
146143
// here we create a new builder for another (sealed) witness block

prover/src/zkevm/circuit.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
use builder::dummy_witness_block;
21
use halo2_proofs::halo2curves::bn256::Fr;
32
use snark_verifier_sdk::CircuitExt;
43
use zkevm_circuits::{super_circuit::params::ScrollSuperCircuit, util::SubCircuit, witness};
54

65
mod builder;
76
pub use self::builder::{
8-
block_traces_to_witness_block, calculate_row_usage_of_witness_block, finalize_builder,
9-
print_chunk_stats,
7+
block_traces_to_witness_block, calculate_row_usage_of_witness_block,
8+
chunk_trace_to_witness_block, finalize_builder,
109
};
1110

1211
pub use zkevm_circuits::super_circuit::params::{MAX_CALLDATA, MAX_INNER_BLOCKS, MAX_TXS};
@@ -23,7 +22,7 @@ pub trait TargetCircuit {
2322
where
2423
Self: Sized,
2524
{
26-
let witness_block = dummy_witness_block()?;
25+
let witness_block = builder::dummy_witness_block()?;
2726
let circuit = Self::from_witness_block(&witness_block)?;
2827
Ok(circuit)
2928
}

prover/src/zkevm/circuit/builder.rs

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use crate::zkevm::SubCircuitRowUsage;
2-
use anyhow::{bail, Result};
3-
use bus_mapping::circuit_input_builder::CircuitInputBuilder;
1+
use crate::zkevm::{ChunkProverError, SubCircuitRowUsage};
2+
use bus_mapping::{circuit_input_builder::CircuitInputBuilder, Error as CircuitBuilderError};
43
use eth_types::{l2_types::BlockTrace, ToWord};
54
use itertools::Itertools;
65
use mpt_zktrie::state::ZkTrieHash;
@@ -12,12 +11,14 @@ use zkevm_circuits::{
1211

1312
pub fn calculate_row_usage_of_witness_block(
1413
witness_block: &Block,
15-
) -> Result<Vec<SubCircuitRowUsage>> {
14+
) -> Result<Vec<SubCircuitRowUsage>, ChunkProverError> {
1615
let rows = ScrollSuperCircuit::min_num_rows_block_subcircuits(witness_block);
1716

1817
// Check whether we need to "estimate" poseidon sub circuit row usage
1918
if witness_block.mpt_updates.smt_traces.is_empty() {
20-
bail!("light mode no longer supported");
19+
return Err(ChunkProverError::Custom(
20+
"light mode no longer supported".to_string(),
21+
));
2122
}
2223

2324
let first_block_num = witness_block.first_block_number();
@@ -34,58 +35,44 @@ pub fn calculate_row_usage_of_witness_block(
3435
.sum::<usize>(),
3536
rows,
3637
);
37-
let row_usage_details: Vec<SubCircuitRowUsage> = rows
38+
39+
Ok(rows
3840
.into_iter()
3941
.map(|x| SubCircuitRowUsage {
4042
name: x.name,
4143
row_number: x.row_num_real,
4244
})
43-
.collect_vec();
44-
Ok(row_usage_details)
45-
}
46-
47-
pub fn print_chunk_stats(block_traces: &[BlockTrace]) {
48-
let num_blocks = block_traces.len();
49-
let num_txs = block_traces
50-
.iter()
51-
.map(|b| b.transactions.len())
52-
.sum::<usize>();
53-
let total_tx_len = block_traces
54-
.iter()
55-
.flat_map(|b| b.transactions.iter().map(|t| t.data.len()))
56-
.sum::<usize>();
57-
log::info!(
58-
"check capacity of block traces, num_block {}, num_tx {}, tx total len {}",
59-
num_blocks,
60-
num_txs,
61-
total_tx_len
62-
);
45+
.collect_vec())
6346
}
6447

65-
pub fn dummy_witness_block() -> Result<Block> {
48+
pub fn dummy_witness_block() -> anyhow::Result<Block> {
6649
log::debug!("generate dummy witness block");
6750
let dummy_chain_id = 0;
6851
let witness_block = zkevm_circuits::witness::dummy_witness_block(dummy_chain_id);
6952
log::debug!("generate dummy witness block done");
7053
Ok(witness_block)
7154
}
7255

73-
pub fn block_traces_to_witness_block(block_traces: Vec<BlockTrace>) -> Result<Block> {
56+
pub fn block_traces_to_witness_block(
57+
block_traces: Vec<BlockTrace>,
58+
) -> Result<Block, ChunkProverError> {
7459
if block_traces.is_empty() {
75-
bail!("use dummy_witness_block instead");
60+
return Err(ChunkProverError::Custom(
61+
"empty block traces! hint: use dummy_witness_block instead".to_string(),
62+
));
7663
}
7764
let block_num = block_traces.len();
7865
let total_tx_num = block_traces
7966
.iter()
8067
.map(|b| b.transactions.len())
8168
.sum::<usize>();
8269
if total_tx_num > MAX_TXS {
83-
bail!(
70+
return Err(ChunkProverError::Custom(format!(
8471
"tx num overflow {}, block range {} to {}",
8572
total_tx_num,
8673
block_traces[0].header.number.unwrap(),
8774
block_traces[block_num - 1].header.number.unwrap()
88-
);
75+
)));
8976
}
9077
log::info!(
9178
"block_traces_to_witness_block, block num {}, tx num {}",
@@ -116,8 +103,18 @@ pub fn block_traces_to_witness_block(block_traces: Vec<BlockTrace>) -> Result<Bl
116103
Ok(witness_block)
117104
}
118105

106+
pub fn chunk_trace_to_witness_block(
107+
chunk_trace: Vec<BlockTrace>,
108+
) -> Result<Block, ChunkProverError> {
109+
if chunk_trace.is_empty() {
110+
return Err(ChunkProverError::Custom("Empty chunk trace".to_string()));
111+
}
112+
print_chunk_stats(&chunk_trace);
113+
block_traces_to_witness_block(chunk_trace)
114+
}
115+
119116
/// Finalize building and return witness block
120-
pub fn finalize_builder(builder: &mut CircuitInputBuilder) -> Result<Block> {
117+
pub fn finalize_builder(builder: &mut CircuitInputBuilder) -> Result<Block, CircuitBuilderError> {
121118
builder.finalize_building()?;
122119

123120
log::debug!("converting builder.block to witness block");
@@ -152,3 +149,21 @@ pub fn finalize_builder(builder: &mut CircuitInputBuilder) -> Result<Block> {
152149

153150
Ok(witness_block)
154151
}
152+
153+
fn print_chunk_stats(block_traces: &[BlockTrace]) {
154+
let num_blocks = block_traces.len();
155+
let num_txs = block_traces
156+
.iter()
157+
.map(|b| b.transactions.len())
158+
.sum::<usize>();
159+
let total_tx_len = block_traces
160+
.iter()
161+
.flat_map(|b| b.transactions.iter().map(|t| t.data.len()))
162+
.sum::<usize>();
163+
log::info!(
164+
"check capacity of block traces, num_block {}, num_tx {}, tx total len {}",
165+
num_blocks,
166+
num_txs,
167+
total_tx_len
168+
);
169+
}

0 commit comments

Comments
 (0)