Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 35 additions & 28 deletions bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ pub struct ExecStep {
pub call_index: usize,
/// The global counter when this step was executed.
pub rwc: RWCounter,
/// State Write Counter. Counter of state write operations in the call
/// that haven't been reverted yet up to this step.
pub swc: usize,
/// Reversible Write Counter. Counter of write operations in the call that
/// will need to be undone in case of a revert.
pub reversible_write_counter: usize,
/// The list of references to Operations in the container
pub bus_mapping_instance: Vec<OperationRef>,
/// Error generated by this step
Expand All @@ -195,7 +195,7 @@ impl ExecStep {
step: &GethExecStep,
call_index: usize,
rwc: RWCounter,
swc: usize, // State Write Counter
reversible_write_counter: usize,
) -> Self {
ExecStep {
exec_state: ExecState::Op(step.op),
Expand All @@ -206,7 +206,7 @@ impl ExecStep {
gas_cost: step.gas_cost,
call_index,
rwc,
swc,
reversible_write_counter,
bus_mapping_instance: Vec::new(),
error: None,
aux_data: None,
Expand All @@ -225,7 +225,7 @@ impl Default for ExecStep {
gas_cost: GasCost(0),
call_index: 0,
rwc: RWCounter(0),
swc: 0,
reversible_write_counter: 0,
bus_mapping_instance: Vec::new(),
error: None,
aux_data: None,
Expand Down Expand Up @@ -431,10 +431,10 @@ impl Call {
pub struct CallContext {
/// Index of call
pub index: usize,
/// State Write Counter tracks the count of state write operations in the
/// call. When a subcall in this call succeeds, the `swc` increases by the
/// number of successful state writes in the subcall.
pub swc: usize,
/// Reversible Write Counter tracks the number of write operations in the
/// call. It is incremented when a subcall in this call succeeds by the
/// number of successful writes in the subcall.
pub reversible_write_counter: usize,
}

/// A reversion group is the collection of calls and the operations which are
Expand All @@ -443,9 +443,10 @@ pub struct CallContext {
/// failure (and thus reverts).
#[derive(Debug, Default)]
pub struct ReversionGroup {
/// List of `index` and `swc_offset` of calls belong to this group.
/// `swc_offset` is the number of reversible operations that have
/// happened before the call within the same reversion group.
/// List of `index` and `reversible_write_counter_offset` of calls belong to
/// this group. `reversible_write_counter_offset` is the number of
/// reversible operations that have happened before the call within the
/// same reversion group.
calls: Vec<(usize, usize)>,
/// List of `step_index` and `OperationRef` that have been done in this
/// group.
Expand Down Expand Up @@ -567,31 +568,36 @@ impl TransactionContext {
})
} else if let Some(reversion_group) = self.reversion_groups.last_mut() {
let caller_ctx = self.calls.last().expect("calls should not be empty");
let caller_swc = caller_ctx.swc;
let caller_swc_offset = reversion_group
let caller_reversible_write_counter = self
.calls
.last()
.expect("calls should not be empty")
.reversible_write_counter;
let caller_reversible_write_counter_offset = reversion_group
.calls
.iter()
.find(|(call_idx, _)| *call_idx == caller_ctx.index)
.expect("calls should not be empty")
.1;
reversion_group
.calls
.push((call_idx, caller_swc + caller_swc_offset));
reversion_group.calls.push((
call_idx,
caller_reversible_write_counter + caller_reversible_write_counter_offset,
));
}

self.calls.push(CallContext {
index: call_idx,
swc: 0,
reversible_write_counter: 0,
});
}

/// Pop the last entry in the call stack.
fn pop_call_ctx(&mut self) {
let call = self.calls.pop().expect("calls should not be empty");
// Accumulate state_write_counter if call is success
// Accumulate reversible_write_counter if call is success
if self.call_is_success[call.index] {
if let Some(caller) = self.calls.last_mut() {
caller.swc += call.swc;
caller.reversible_write_counter += call.reversible_write_counter;
}
}
}
Expand Down Expand Up @@ -738,7 +744,7 @@ impl<'a> CircuitInputStateRef<'a> {
geth_step,
call_ctx.index,
self.block_ctx.rwc,
call_ctx.swc,
call_ctx.reversible_write_counter,
))
}

Expand All @@ -764,8 +770,8 @@ impl<'a> CircuitInputStateRef<'a> {
gas_left: Gas(prev_step.gas_left.0 - prev_step.gas_cost.0),
rwc: self.block_ctx.rwc,
// For tx without code execution
swc: if let Some(call_ctx) = self.tx_ctx.calls().last() {
call_ctx.swc
reversible_write_counter: if let Some(call_ctx) = self.tx_ctx.calls().last() {
call_ctx.reversible_write_counter
} else {
0
},
Expand Down Expand Up @@ -806,8 +812,8 @@ impl<'a> CircuitInputStateRef<'a> {
));
step.bus_mapping_instance.push(op_ref);

// Increase state_write_counter
self.call_ctx_mut()?.swc += 1;
// Increase reversible_write_counter
self.call_ctx_mut()?.reversible_write_counter += 1;

// Add the operation into reversible_ops if this call is not persistent
if !self.call()?.is_persistent {
Expand Down Expand Up @@ -1144,8 +1150,9 @@ impl<'a> CircuitInputStateRef<'a> {

// Set calls' `rw_counter_end_of_reversion`
let rwc = self.block_ctx.rwc.0 - 1;
for (call_idx, swc_offset) in reversion_group.calls {
self.tx.calls[call_idx].rw_counter_end_of_reversion = rwc - swc_offset;
for (call_idx, reversible_write_counter_offset) in reversion_group.calls {
self.tx.calls[call_idx].rw_counter_end_of_reversion =
rwc - reversible_write_counter_offset;
}
}

Expand Down
2 changes: 1 addition & 1 deletion bus-mapping/src/evm/opcodes/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl Opcode for Call {
(CallContextField::MemorySize, next_memory_word_size.into()),
(
CallContextField::StateWriteCounter,
(exec_step.swc + 1).into(),
(exec_step.reversible_write_counter + 1).into(),
),
] {
state.push_op(
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/execution/begin_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl<F: Field> ExecutionGadget<F> for BeginTxGadget<F> {
is_create: To(false.expr()),
code_source: To(code_hash.expr()),
gas_left: To(gas_left),
state_write_counter: To(2.expr()),
reversible_write_counter: To(2.expr()),
..StepStateTransition::new_context()
});

Expand Down
8 changes: 4 additions & 4 deletions zkevm-circuits/src/evm_circuit/execution/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl<F: Field> ExecutionGadget<F> for CallGadget<F> {
);
cb.condition(is_success.expr() * (1.expr() - reversion_info.is_persistent()), |cb| {
cb.require_equal(
"callee_rw_counter_end_of_reversion == rw_counter_end_of_reversion - (state_write_counter + 1)",
"callee_rw_counter_end_of_reversion == rw_counter_end_of_reversion - (reversible_write_counter + 1)",
callee_reversion_info.rw_counter_end_of_reversion(),
reversion_info.rw_counter_of_reversion(),
);
Expand Down Expand Up @@ -235,7 +235,7 @@ impl<F: Field> ExecutionGadget<F> for CallGadget<F> {
has_value.clone() * GAS_STIPEND_CALL_WITH_VALUE.expr() - gas_cost.clone(),
),
memory_word_size: To(memory_expansion.next_memory_word_size()),
state_write_counter: Delta(3.expr()),
reversible_write_counter: Delta(3.expr()),
..StepStateTransition::default()
});
});
Expand All @@ -261,7 +261,7 @@ impl<F: Field> ExecutionGadget<F> for CallGadget<F> {
),
(
CallContextFieldTag::StateWriteCounter,
cb.curr.state.state_write_counter.expr() + 1.expr(),
cb.curr.state.reversible_write_counter.expr() + 1.expr(),
),
] {
cb.call_context_lookup(true.expr(), None, field_tag, value);
Expand Down Expand Up @@ -301,7 +301,7 @@ impl<F: Field> ExecutionGadget<F> for CallGadget<F> {
is_create: To(false.expr()),
code_source: To(callee_code_hash.expr()),
gas_left: To(callee_gas_left),
state_write_counter: To(2.expr()),
reversible_write_counter: To(2.expr()),
..StepStateTransition::new_context()
});
});
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/execution/extcodehash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl<F: Field> ExecutionGadget<F> for ExtcodehashGadget<F> {
program_counter: Delta(1.expr()),
stack_pointer: Delta(0.expr()),
gas_left: Delta(-gas_cost),
state_write_counter: Delta(1.expr()),
reversible_write_counter: Delta(1.expr()),
..Default::default()
};

Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/sload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl<F: Field> ExecutionGadget<F> for SloadGadget<F> {
let step_state_transition = StepStateTransition {
rw_counter: Delta(8.expr()),
program_counter: Delta(1.expr()),
state_write_counter: Delta(1.expr()),
reversible_write_counter: Delta(1.expr()),
gas_left: Delta(-gas_cost),
..Default::default()
};
Expand Down Expand Up @@ -279,7 +279,7 @@ mod test {
stack_pointer: STACK_CAPACITY,
gas_left: 0,
opcode: Some(OpcodeId::STOP),
state_write_counter: 1,
reversible_write_counter: 1,
..Default::default()
},
],
Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/sstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl<F: Field> ExecutionGadget<F> for SstoreGadget<F> {
rw_counter: Delta(9.expr()),
program_counter: Delta(1.expr()),
stack_pointer: Delta(2.expr()),
state_write_counter: Delta(3.expr()),
reversible_write_counter: Delta(3.expr()),
gas_left: Delta(-gas_cost.expr()),
..Default::default()
};
Expand Down Expand Up @@ -724,7 +724,7 @@ mod test {
stack_pointer: STACK_CAPACITY,
gas_left: 0,
opcode: Some(OpcodeId::STOP),
state_write_counter: 3,
reversible_write_counter: 3,
..Default::default()
},
],
Expand Down
10 changes: 5 additions & 5 deletions zkevm-circuits/src/evm_circuit/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,8 @@ pub(crate) struct StepState<F> {
pub(crate) gas_left: Cell<F>,
/// Memory size in words (32 bytes)
pub(crate) memory_word_size: Cell<F>,
/// The counter for state writes
pub(crate) state_write_counter: Cell<F>,
/// The counter for reversible writes
pub(crate) reversible_write_counter: Cell<F>,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -511,7 +511,7 @@ impl<F: FieldExt> Step<F> {
stack_pointer: cells.pop_front().unwrap(),
gas_left: cells.pop_front().unwrap(),
memory_word_size: cells.pop_front().unwrap(),
state_write_counter: cells.pop_front().unwrap(),
reversible_write_counter: cells.pop_front().unwrap(),
}
};

Expand Down Expand Up @@ -606,10 +606,10 @@ impl<F: FieldExt> Step<F> {
offset,
Some(F::from(step.memory_word_size())),
)?;
self.state.state_write_counter.assign(
self.state.reversible_write_counter.assign(
region,
offset,
Some(F::from(step.state_write_counter as u64)),
Some(F::from(step.reversible_write_counter as u64)),
)?;
Ok(())
}
Expand Down
Loading