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

Commit

Permalink
use max_rws to identify fixed/dynamic circuit params
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Feb 2, 2024
1 parent 879d1b6 commit 64154f7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
33 changes: 14 additions & 19 deletions bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,6 @@ pub trait CircuitsParams: Debug + Copy {
fn set_total_chunk(&mut self, total_chunks: usize);
/// Return the maximun Rw
fn max_rws(&self) -> Option<usize>;
/// Return whether the parameters are dynamic.
/// If true, the `total_chunks` and `max_rws` will serve as a target value for chunking
/// and [`FixedCParams`] will be recomputed from each generated chunk witness.
fn dynamic_update(&self) -> bool;
}

impl CircuitsParams for FixedCParams {
Expand All @@ -155,9 +151,6 @@ impl CircuitsParams for FixedCParams {
fn max_rws(&self) -> Option<usize> {
Some(self.max_rws)
}
fn dynamic_update(&self) -> bool {
false
}
}
impl CircuitsParams for DynamicCParams {
fn total_chunks(&self) -> usize {
Expand All @@ -169,9 +162,6 @@ impl CircuitsParams for DynamicCParams {
fn max_rws(&self) -> Option<usize> {
None
}
fn dynamic_update(&self) -> bool {
true
}
}

impl Default for DynamicCParams {
Expand Down Expand Up @@ -257,7 +247,7 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
block,
chunks,
block_ctx: BlockContext::new(),
chunk_ctx: ChunkContext::new(total_chunks, params.dynamic_update()),
chunk_ctx: ChunkContext::new(total_chunks),
circuits_params: params,
feature_config,
}
Expand Down Expand Up @@ -354,12 +344,12 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
return Ok(());
}
let is_last_tx = tx_ctx.is_last_tx();
let dynamic = self.chunk_ctx.dynamic_update;
let is_dynamic_max_row = self.circuits_params.max_rws().is_none();
let mut gen_chunk =
// No lookahead, if chunk_rws exceed max just chunk then update param
(dynamic && self.chunk_rws() > self.circuits_params.max_rws().unwrap_or_default() - self.rws_reserve())
(is_dynamic_max_row && self.chunk_rws() > self.circuits_params.max_rws().unwrap_or_default().saturating_sub(self.rws_reserve()))
// Lookahead, chunk_rws should never exceed, never update param
|| (!dynamic && self.chunk_rws() + RW_BUFFER >= self.circuits_params.max_rws().unwrap_or_default() - self.rws_reserve());
|| (!is_dynamic_max_row && self.chunk_rws() + RW_BUFFER >= self.circuits_params.max_rws().unwrap_or_default().saturating_sub(self.rws_reserve()));

if gen_chunk {
// Optain the first op of the next GethExecStep, for fixed case also lookahead
Expand All @@ -382,10 +372,14 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
// Check again, 1) if dynamic keep chunking 2) if fixed chunk when lookahead exceed
// 3) gen chunk steps there're more chunks after
gen_chunk = !self.chunk_ctx.is_last_chunk()
&& (dynamic
&& (is_dynamic_max_row
|| cib.chunk_rws()
> self.circuits_params.max_rws().unwrap_or_default() - cib.rws_reserve());
if dynamic {
> self
.circuits_params
.max_rws()
.unwrap_or_default()
.saturating_sub(cib.rws_reserve()));
if is_dynamic_max_row {
self.cur_chunk_mut().fixed_param = self.compute_param(&self.block.eth_block);
}
if gen_chunk {
Expand Down Expand Up @@ -687,8 +681,9 @@ impl CircuitInputBuilder<FixedCParams> {
println!("--------------{:?}", self.circuits_params);
// accumulates gas across all txs in the block
let last_call = self.begin_handle_block(eth_block, geth_traces)?;

// At the last chunk fixed param also need to be updated
if self.chunk_ctx.dynamic_update {
if self.circuits_params.max_rws().is_none() {
self.cur_chunk_mut().fixed_param = self.compute_param(&self.block.eth_block);
} else {
self.cur_chunk_mut().fixed_param = self.circuits_params;
Expand Down Expand Up @@ -918,7 +913,7 @@ impl CircuitInputBuilder<DynamicCParams> {
block: self.block,
chunks: self.chunks,
block_ctx: self.block_ctx,
chunk_ctx: ChunkContext::new(total_chunks, true),
chunk_ctx: ChunkContext::new(total_chunks),
circuits_params: target_params,
feature_config: self.feature_config,
};
Expand Down
8 changes: 2 additions & 6 deletions bus-mapping/src/circuit_input_builder/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,19 @@ pub struct ChunkContext {
pub initial_copy: usize,
///
pub end_copy: usize,
/// If this block is chunked dynamically, update the param
pub dynamic_update: bool,
/// Druing dry run, chuncking is desabled
pub enable: bool,
}

impl Default for ChunkContext {
fn default() -> Self {
Self::new(1, false)
Self::new(1)
}
}

impl ChunkContext {
/// Create a new Self
pub fn new(total_chunks: usize, dynamic_update: bool) -> Self {
pub fn new(total_chunks: usize) -> Self {
Self {
rwc: RWCounter::new(),
idx: 0,
Expand All @@ -66,7 +64,6 @@ impl ChunkContext {
end_tx: 0,
initial_copy: 0,
end_copy: 0,
dynamic_update,
enable: true,
}
}
Expand All @@ -83,7 +80,6 @@ impl ChunkContext {
end_tx: 0,
initial_copy: 0,
end_copy: 0,
dynamic_update: false,
enable: true,
}
}
Expand Down

0 comments on commit 64154f7

Please sign in to comment.