Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.

Commit 64154f7

Browse files
committed
use max_rws to identify fixed/dynamic circuit params
1 parent 879d1b6 commit 64154f7

File tree

2 files changed

+16
-25
lines changed

2 files changed

+16
-25
lines changed

bus-mapping/src/circuit_input_builder.rs

+14-19
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ pub trait CircuitsParams: Debug + Copy {
139139
fn set_total_chunk(&mut self, total_chunks: usize);
140140
/// Return the maximun Rw
141141
fn max_rws(&self) -> Option<usize>;
142-
/// Return whether the parameters are dynamic.
143-
/// If true, the `total_chunks` and `max_rws` will serve as a target value for chunking
144-
/// and [`FixedCParams`] will be recomputed from each generated chunk witness.
145-
fn dynamic_update(&self) -> bool;
146142
}
147143

148144
impl CircuitsParams for FixedCParams {
@@ -155,9 +151,6 @@ impl CircuitsParams for FixedCParams {
155151
fn max_rws(&self) -> Option<usize> {
156152
Some(self.max_rws)
157153
}
158-
fn dynamic_update(&self) -> bool {
159-
false
160-
}
161154
}
162155
impl CircuitsParams for DynamicCParams {
163156
fn total_chunks(&self) -> usize {
@@ -169,9 +162,6 @@ impl CircuitsParams for DynamicCParams {
169162
fn max_rws(&self) -> Option<usize> {
170163
None
171164
}
172-
fn dynamic_update(&self) -> bool {
173-
true
174-
}
175165
}
176166

177167
impl Default for DynamicCParams {
@@ -257,7 +247,7 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
257247
block,
258248
chunks,
259249
block_ctx: BlockContext::new(),
260-
chunk_ctx: ChunkContext::new(total_chunks, params.dynamic_update()),
250+
chunk_ctx: ChunkContext::new(total_chunks),
261251
circuits_params: params,
262252
feature_config,
263253
}
@@ -354,12 +344,12 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
354344
return Ok(());
355345
}
356346
let is_last_tx = tx_ctx.is_last_tx();
357-
let dynamic = self.chunk_ctx.dynamic_update;
347+
let is_dynamic_max_row = self.circuits_params.max_rws().is_none();
358348
let mut gen_chunk =
359349
// No lookahead, if chunk_rws exceed max just chunk then update param
360-
(dynamic && self.chunk_rws() > self.circuits_params.max_rws().unwrap_or_default() - self.rws_reserve())
350+
(is_dynamic_max_row && self.chunk_rws() > self.circuits_params.max_rws().unwrap_or_default().saturating_sub(self.rws_reserve()))
361351
// Lookahead, chunk_rws should never exceed, never update param
362-
|| (!dynamic && self.chunk_rws() + RW_BUFFER >= self.circuits_params.max_rws().unwrap_or_default() - self.rws_reserve());
352+
|| (!is_dynamic_max_row && self.chunk_rws() + RW_BUFFER >= self.circuits_params.max_rws().unwrap_or_default().saturating_sub(self.rws_reserve()));
363353

364354
if gen_chunk {
365355
// Optain the first op of the next GethExecStep, for fixed case also lookahead
@@ -382,10 +372,14 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
382372
// Check again, 1) if dynamic keep chunking 2) if fixed chunk when lookahead exceed
383373
// 3) gen chunk steps there're more chunks after
384374
gen_chunk = !self.chunk_ctx.is_last_chunk()
385-
&& (dynamic
375+
&& (is_dynamic_max_row
386376
|| cib.chunk_rws()
387-
> self.circuits_params.max_rws().unwrap_or_default() - cib.rws_reserve());
388-
if dynamic {
377+
> self
378+
.circuits_params
379+
.max_rws()
380+
.unwrap_or_default()
381+
.saturating_sub(cib.rws_reserve()));
382+
if is_dynamic_max_row {
389383
self.cur_chunk_mut().fixed_param = self.compute_param(&self.block.eth_block);
390384
}
391385
if gen_chunk {
@@ -687,8 +681,9 @@ impl CircuitInputBuilder<FixedCParams> {
687681
println!("--------------{:?}", self.circuits_params);
688682
// accumulates gas across all txs in the block
689683
let last_call = self.begin_handle_block(eth_block, geth_traces)?;
684+
690685
// At the last chunk fixed param also need to be updated
691-
if self.chunk_ctx.dynamic_update {
686+
if self.circuits_params.max_rws().is_none() {
692687
self.cur_chunk_mut().fixed_param = self.compute_param(&self.block.eth_block);
693688
} else {
694689
self.cur_chunk_mut().fixed_param = self.circuits_params;
@@ -918,7 +913,7 @@ impl CircuitInputBuilder<DynamicCParams> {
918913
block: self.block,
919914
chunks: self.chunks,
920915
block_ctx: self.block_ctx,
921-
chunk_ctx: ChunkContext::new(total_chunks, true),
916+
chunk_ctx: ChunkContext::new(total_chunks),
922917
circuits_params: target_params,
923918
feature_config: self.feature_config,
924919
};

bus-mapping/src/circuit_input_builder/chunk.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,19 @@ pub struct ChunkContext {
4141
pub initial_copy: usize,
4242
///
4343
pub end_copy: usize,
44-
/// If this block is chunked dynamically, update the param
45-
pub dynamic_update: bool,
4644
/// Druing dry run, chuncking is desabled
4745
pub enable: bool,
4846
}
4947

5048
impl Default for ChunkContext {
5149
fn default() -> Self {
52-
Self::new(1, false)
50+
Self::new(1)
5351
}
5452
}
5553

5654
impl ChunkContext {
5755
/// Create a new Self
58-
pub fn new(total_chunks: usize, dynamic_update: bool) -> Self {
56+
pub fn new(total_chunks: usize) -> Self {
5957
Self {
6058
rwc: RWCounter::new(),
6159
idx: 0,
@@ -66,7 +64,6 @@ impl ChunkContext {
6664
end_tx: 0,
6765
initial_copy: 0,
6866
end_copy: 0,
69-
dynamic_update,
7067
enable: true,
7168
}
7269
}
@@ -83,7 +80,6 @@ impl ChunkContext {
8380
end_tx: 0,
8481
initial_copy: 0,
8582
end_copy: 0,
86-
dynamic_update: false,
8783
enable: true,
8884
}
8985
}

0 commit comments

Comments
 (0)