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
14 changes: 2 additions & 12 deletions bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,9 @@ impl<'a> CircuitInputBuilder {
geth_traces: &[eth_types::GethExecTrace],
) -> Result<(), Error> {
// accumulates gas across all txs in the block
let mut cumulative_gas_used = HashMap::new();
for (tx_index, tx) in eth_block.transactions.iter().enumerate() {
let geth_trace = &geth_traces[tx_index];
self.handle_tx(
tx,
geth_trace,
tx_index + 1 == eth_block.transactions.len(),
&mut cumulative_gas_used,
)?;
self.handle_tx(tx, geth_trace, tx_index + 1 == eth_block.transactions.len())?;
}
self.set_value_ops_call_context_rwc_eor();
Ok(())
Expand All @@ -162,7 +156,6 @@ impl<'a> CircuitInputBuilder {
eth_tx: &eth_types::Transaction,
geth_trace: &GethExecTrace,
is_last_tx: bool,
cumulative_gas_used: &mut HashMap<usize, u64>,
) -> Result<(), Error> {
let mut tx = self.new_tx(eth_tx, !geth_trace.failed)?;
let mut tx_ctx = TransactionContext::new(eth_tx, geth_trace, is_last_tx)?;
Expand All @@ -189,10 +182,7 @@ impl<'a> CircuitInputBuilder {
// - execution_state: EndTx
// - op: None
// Generate EndTx step
let end_tx_step = gen_end_tx_ops(
&mut self.state_ref(&mut tx, &mut tx_ctx),
cumulative_gas_used,
)?;
let end_tx_step = gen_end_tx_ops(&mut self.state_ref(&mut tx, &mut tx_ctx))?;
tx.steps_mut().push(end_tx_step);

self.sdb.commit_tx();
Expand Down
3 changes: 3 additions & 0 deletions bus-mapping/src/circuit_input_builder/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub struct BlockContext {
/// in Block.txs and call_index is the index used in Transaction.
/// calls).
pub(crate) call_map: HashMap<usize, (usize, usize)>,
/// Total gas used by previous transactions in this block.
pub(crate) cumulative_gas_used: u64,
}

impl Default for BlockContext {
Expand All @@ -32,6 +34,7 @@ impl BlockContext {
Self {
rwc: RWCounter::new(),
call_map: HashMap::new(),
cumulative_gas_used: 0,
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions bus-mapping/src/circuit_input_builder/input_state_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,31 @@ impl<'a> CircuitInputStateRef<'a> {
Ok(())
}

/// Push a write type [`TxReceiptOp`] into the
/// [`OperationContainer`](crate::operation::OperationContainer) with the
/// next [`RWCounter`](crate::operation::RWCounter), and then
/// adds a reference to the stored operation ([`OperationRef`]) inside
/// the bus-mapping instance of the current [`ExecStep`]. Then increase
/// the `block_ctx` [`RWCounter`](crate::operation::RWCounter) by one.
pub fn tx_receipt_write(
&mut self,
step: &mut ExecStep,
tx_id: usize,
field: TxReceiptField,
value: u64,
) -> Result<(), Error> {
self.push_op(
step,
RW::WRITE,
TxReceiptOp {
tx_id,
field,
value,
},
);
Ok(())
}

/// Push a write type [`TxAccessListAccountOp`] into the
/// [`OperationContainer`](crate::operation::OperationContainer) with the
/// next [`RWCounter`](crate::operation::RWCounter), and then
Expand Down
22 changes: 7 additions & 15 deletions bus-mapping/src/evm/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use eth_types::{
};
use keccak256::EMPTY_HASH;
use log::warn;
use std::collections::HashMap;

mod call;
mod calldatacopy;
Expand Down Expand Up @@ -372,10 +371,7 @@ pub fn gen_begin_tx_ops(state: &mut CircuitInputStateRef) -> Result<ExecStep, Er
}
}

pub fn gen_end_tx_ops(
state: &mut CircuitInputStateRef,
cumulative_gas_used: &mut HashMap<usize, u64>,
) -> Result<ExecStep, Error> {
pub fn gen_end_tx_ops(state: &mut CircuitInputStateRef) -> Result<ExecStep, Error> {
let mut exec_step = state.new_end_tx_step();
let call = state.tx.calls()[0].clone();

Expand Down Expand Up @@ -438,43 +434,39 @@ pub fn gen_end_tx_ops(
)?;

// handle tx receipt tag
state.tx_receipt_read(
state.tx_receipt_write(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if change to write type, pls. update spec accordingly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you take a look at privacy-ethereum/zkevm-specs#234 please? It won't let me add you as a reviewer.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will have a look !

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approved the spec change.

&mut exec_step,
state.tx_ctx.id(),
TxReceiptField::PostStateOrStatus,
call.is_persistent as u64,
)?;

let log_id = exec_step.log_id;
state.tx_receipt_read(
state.tx_receipt_write(
&mut exec_step,
state.tx_ctx.id(),
TxReceiptField::LogLength,
log_id as u64,
)?;

let gas_used = state.tx.gas - exec_step.gas_left.0;
let mut current_cumulative_gas_used: u64 = 0;
if state.tx_ctx.id() > 1 {
current_cumulative_gas_used = *cumulative_gas_used.get(&(state.tx_ctx.id() - 1)).unwrap();
// query pre tx cumulative gas
state.tx_receipt_read(
&mut exec_step,
state.tx_ctx.id() - 1,
TxReceiptField::CumulativeGasUsed,
current_cumulative_gas_used,
state.block_ctx.cumulative_gas_used,
)?;
}

state.tx_receipt_read(
state.block_ctx.cumulative_gas_used += state.tx.gas - exec_step.gas_left.0;
state.tx_receipt_write(
&mut exec_step,
state.tx_ctx.id(),
TxReceiptField::CumulativeGasUsed,
current_cumulative_gas_used + gas_used,
state.block_ctx.cumulative_gas_used,
)?;

cumulative_gas_used.insert(state.tx_ctx.id(), current_cumulative_gas_used + gas_used);

if !state.tx_ctx.is_last_tx() {
state.call_context_read(
&mut exec_step,
Expand Down
4 changes: 4 additions & 0 deletions zkevm-circuits/src/evm_circuit/execution/end_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,13 @@ impl<F: Field> ExecutionGadget<F> for EndTxGadget<F> {

// constrain tx receipt fields
cb.tx_receipt_lookup(
1.expr(),
tx_id.expr(),
TxReceiptFieldTag::PostStateOrStatus,
is_persistent.expr(),
);
cb.tx_receipt_lookup(
1.expr(),
tx_id.expr(),
TxReceiptFieldTag::LogLength,
cb.curr.state.log_id.expr(),
Expand All @@ -126,13 +128,15 @@ impl<F: Field> ExecutionGadget<F> for EndTxGadget<F> {

cb.condition(1.expr() - is_first_tx.expr(), |cb| {
cb.tx_receipt_lookup(
0.expr(),
tx_id.expr() - 1.expr(),
TxReceiptFieldTag::CumulativeGasUsed,
current_cumulative_gas_used.expr(),
);
});

cb.tx_receipt_lookup(
1.expr(),
tx_id.expr(),
TxReceiptFieldTag::CumulativeGasUsed,
gas_used + current_cumulative_gas_used.expr(),
Expand Down
13 changes: 2 additions & 11 deletions zkevm-circuits/src/evm_circuit/util/constraint_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,25 +1041,16 @@ impl<'a, F: FieldExt> ConstraintBuilder<'a, F> {

// Tx Receipt

pub(crate) fn tx_receipt(
&mut self,
tx_id: Expression<F>,
field_tag: TxReceiptFieldTag,
) -> Cell<F> {
let cell = self.query_cell();
self.tx_receipt_lookup(tx_id, field_tag, cell.expr());
cell
}

pub(crate) fn tx_receipt_lookup(
&mut self,
is_write: Expression<F>,
tx_id: Expression<F>,
tag: TxReceiptFieldTag,
value: Expression<F>,
) {
self.rw_lookup(
"tx receipt lookup",
0.expr(),
is_write,
RwTableTag::TxReceipt,
[
tx_id,
Expand Down
9 changes: 3 additions & 6 deletions zkevm-circuits/src/state_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,9 @@ impl<F: Field, const N_ROWS: usize> Circuit<F> for StateCircuit<F, N_ROWS> {

// TODO: Get initial_values from MPT updates instead.
if is_first_access {
// TODO: Set initial values for Rw::CallContext and Rw::TxReceipt to be
// 0 instead of special casing them.
initial_value = if matches!(
row.tag(),
RwTableTag::CallContext | RwTableTag::TxReceipt
) {
// TODO: Set initial values for Rw::CallContext to be 0 instead of
// special casing it.
initial_value = if matches!(row.tag(), RwTableTag::CallContext) {
row.value_assignment(self.randomness)
} else {
row.value_prev_assignment(self.randomness)
Expand Down