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

Add tx_id to Rw::Acount #905

Draft
wants to merge 11 commits into
base: develop
Choose a base branch
from
Draft
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
9 changes: 7 additions & 2 deletions bus-mapping/src/circuit_input_builder/input_state_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ impl<'a> CircuitInputStateRef<'a> {
field: AccountField,
value: Word,
) -> Result<(), Error> {
let op = AccountOp::new(address, field, value, value);
let op = AccountOp::new(address, self.tx_ctx.id(), field, value, value);
self.push_op(step, RW::READ, op)
}

Expand All @@ -485,7 +485,7 @@ impl<'a> CircuitInputStateRef<'a> {
value: Word,
value_prev: Word,
) -> Result<(), Error> {
let op = AccountOp::new(address, field, value, value_prev);
let op = AccountOp::new(address, self.tx_ctx.id(), field, value, value_prev);
self.push_op(step, RW::WRITE, op)
}

Expand Down Expand Up @@ -653,6 +653,7 @@ impl<'a> CircuitInputStateRef<'a> {
step,
RW::WRITE,
AccountOp {
tx_id: self.tx_ctx.id(),
address: sender,
field: AccountField::Balance,
value: sender_balance,
Expand All @@ -672,6 +673,7 @@ impl<'a> CircuitInputStateRef<'a> {
self.push_op_reversible(
step,
AccountOp {
tx_id: self.tx_ctx.id(),
address: sender,
field: AccountField::Balance,
value: sender_balance,
Expand Down Expand Up @@ -705,6 +707,7 @@ impl<'a> CircuitInputStateRef<'a> {
self.account_read(step, receiver, AccountField::CodeHash, prev_code_hash)?;
let write_op = AccountOp::new(
receiver,
self.tx_ctx.id(),
AccountField::CodeHash,
CodeDB::empty_code_hash().to_word(),
prev_code_hash,
Expand All @@ -729,6 +732,7 @@ impl<'a> CircuitInputStateRef<'a> {
)?;
let write_op = AccountOp::new(
receiver,
self.tx_ctx.id(),
AccountField::KeccakCodeHash,
KECCAK_CODE_HASH_ZERO.to_word(),
prev_keccak_code_hash,
Expand Down Expand Up @@ -757,6 +761,7 @@ impl<'a> CircuitInputStateRef<'a> {
);
let write_op = AccountOp::new(
receiver,
self.tx_ctx.id(),
AccountField::Balance,
receiver_balance,
receiver_balance_prev,
Expand Down
4 changes: 4 additions & 0 deletions bus-mapping/src/evm/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,7 @@ pub fn gen_begin_tx_ops(
state.push_op_reversible(
&mut exec_step,
AccountOp {
tx_id: state.tx_ctx.id(),
address: call.address,
field: AccountField::Nonce,
value: 1.into(),
Expand Down Expand Up @@ -1111,6 +1112,7 @@ fn dummy_gen_selfdestruct_ops(
state.push_op_reversible(
&mut exec_step,
AccountOp {
tx_id: state.tx_ctx.id(),
address: sender,
field: AccountField::Balance,
value: Word::zero(),
Expand All @@ -1120,6 +1122,7 @@ fn dummy_gen_selfdestruct_ops(
state.push_op_reversible(
&mut exec_step,
AccountOp {
tx_id: state.tx_ctx.id(),
address: sender,
field: AccountField::Nonce,
value: Word::zero(),
Expand All @@ -1129,6 +1132,7 @@ fn dummy_gen_selfdestruct_ops(
state.push_op_reversible(
&mut exec_step,
AccountOp {
tx_id: state.tx_ctx.id(),
address: sender,
field: AccountField::CodeHash,
value: Word::zero(),
Expand Down
2 changes: 2 additions & 0 deletions bus-mapping/src/evm/opcodes/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ mod balance_tests {
operation.op(),
&AccountOp {
address,
tx_id,
field: AccountField::CodeHash,
value: if exists { code_hash } else { U256::zero() },
value_prev: if exists { code_hash } else { U256::zero() },
Expand All @@ -280,6 +281,7 @@ mod balance_tests {
operation.op(),
&AccountOp {
address,
tx_id,
field: AccountField::Balance,
value: balance,
value_prev: balance,
Expand Down
2 changes: 2 additions & 0 deletions bus-mapping/src/evm/opcodes/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ impl<const IS_CREATE2: bool> Opcode for Create<IS_CREATE2> {
state.push_op_reversible(
&mut exec_step,
AccountOp {
tx_id: state.tx_ctx.id(),
address: caller.address,
field: AccountField::Nonce,
value: (caller_nonce + 1).into(),
Expand Down Expand Up @@ -225,6 +226,7 @@ impl<const IS_CREATE2: bool> Opcode for Create<IS_CREATE2> {
state.push_op_reversible(
&mut exec_step,
AccountOp {
tx_id: state.tx_ctx.id(),
address: callee.address,
field: AccountField::Nonce,
value: 1.into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl<const IS_CREATE2: bool> Opcode for ContractAddressCollision<IS_CREATE2> {
state.push_op_reversible(
&mut exec_step,
AccountOp {
tx_id: state.tx_ctx.id(),
address: caller.address,
field: AccountField::Nonce,
value: (caller_nonce + 1).into(),
Expand Down
1 change: 1 addition & 0 deletions bus-mapping/src/evm/opcodes/extcodecopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ mod extcodecopy_tests {
(
RW::READ,
&AccountOp {
tx_id,
address: external_address,
field: AccountField::CodeHash,
value: code_hash.to_word(),
Expand Down
1 change: 1 addition & 0 deletions bus-mapping/src/evm/opcodes/extcodehash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ mod extcodehash_tests {
(
RW::READ,
&AccountOp {
tx_id,
address: external_address,
field: if cfg!(feature = "scroll") {
AccountField::KeccakCodeHash
Expand Down
2 changes: 2 additions & 0 deletions bus-mapping/src/evm/opcodes/extcodesize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ mod extcodesize_tests {
assert_eq!(
operation.op(),
&AccountOp {
tx_id,
address: account.address,
field: AccountField::CodeHash,
value: if exists { code_hash } else { Word::zero() },
Expand All @@ -259,6 +260,7 @@ mod extcodesize_tests {
assert_eq!(
operation.op(),
&AccountOp {
tx_id,
address: account.address,
field: AccountField::CodeSize,
value: code_size,
Expand Down
4 changes: 4 additions & 0 deletions bus-mapping/src/evm/opcodes/return_revert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl Opcode for ReturnRevert {
)?;

for (field, value) in [
(CallContextField::TxId, state.tx_ctx.id().to_word()),
(CallContextField::CallerId, call.caller_id.to_word()),
(CallContextField::CalleeAddress, call.address.to_word()),
(
Expand Down Expand Up @@ -95,6 +96,7 @@ impl Opcode for ReturnRevert {
state.push_op_reversible(
&mut exec_step,
AccountOp {
tx_id: state.tx_ctx.id(),
address: call.address,
field: AccountField::CodeHash,
value: code_info.hash.to_word(),
Expand All @@ -118,6 +120,7 @@ impl Opcode for ReturnRevert {
state.push_op_reversible(
&mut exec_step,
AccountOp {
tx_id: state.tx_ctx.id(),
address: call.address,
field: AccountField::KeccakCodeHash,
value: code_info.keccak_hash.to_word(),
Expand All @@ -128,6 +131,7 @@ impl Opcode for ReturnRevert {
state.push_op_reversible(
&mut exec_step,
AccountOp {
tx_id: state.tx_ctx.id(),
address: call.address,
field: AccountField::CodeSize,
value: code_info.size.to_word(),
Expand Down
32 changes: 29 additions & 3 deletions bus-mapping/src/evm/opcodes/selfbalance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ impl Opcode for Selfbalance {
let self_balance = geth_steps[1].stack.last()?;
let callee_address = state.call()?.address;

// CallContext read of the callee_address
state.call_context_read(
&mut exec_step,
state.call()?.call_id,
CallContextField::TxId,
state.tx_ctx.id().into(),
)?;

// CallContext read of the callee_address
state.call_context_read(
&mut exec_step,
Expand Down Expand Up @@ -84,7 +92,8 @@ mod selfbalance_tests {
.handle_block(&block.eth_block, &block.geth_traces)
.unwrap();

let step = builder.block.txs()[0]
let tx_id = 1;
let step = builder.block.txs()[tx_id - 1]
.steps()
.iter()
.find(|step| step.exec_state == ExecState::Op(OpcodeId::SELFBALANCE))
Expand All @@ -100,6 +109,22 @@ mod selfbalance_tests {
&builder.block.container.call_context[step.bus_mapping_instance[0].as_usize()];
(operation.rw(), operation.op())
},
(
RW::READ,
&CallContextOp {
call_id,
field: CallContextField::TxId,
value: tx_id.to_word(),
}
)
);

assert_eq!(
{
let operation =
&builder.block.container.call_context[step.bus_mapping_instance[1].as_usize()];
(operation.rw(), operation.op())
},
(
RW::READ,
&CallContextOp {
Expand All @@ -112,12 +137,13 @@ mod selfbalance_tests {
assert_eq!(
{
let operation =
&builder.block.container.account[step.bus_mapping_instance[1].as_usize()];
&builder.block.container.account[step.bus_mapping_instance[2].as_usize()];
(operation.rw(), operation.op())
},
(
RW::READ,
&AccountOp {
tx_id,
address: callee_address,
field: AccountField::Balance,
value: self_balance,
Expand All @@ -128,7 +154,7 @@ mod selfbalance_tests {
assert_eq!(
{
let operation =
&builder.block.container.stack[step.bus_mapping_instance[2].as_usize()];
&builder.block.container.stack[step.bus_mapping_instance[3].as_usize()];
(operation.rw(), operation.op())
},
(
Expand Down
10 changes: 7 additions & 3 deletions bus-mapping/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,8 @@ pub enum AccountField {
pub struct AccountOp {
/// Account Address
pub address: Address,
/// id of the tx this occurs in
pub tx_id: usize,
/// Field
pub field: AccountField,
/// Field Value after the operation
Expand All @@ -574,15 +576,17 @@ pub struct AccountOp {
}

impl AccountOp {
/// Create a new instance of a `AccountOp` from it's components.
/// Create a new instance of a `AccountOp` from its components.
pub const fn new(
address: Address,
tx_id: usize,
field: AccountField,
value: Word,
value_prev: Word,
) -> AccountOp {
AccountOp {
address,
tx_id,
field,
value,
value_prev,
Expand All @@ -594,8 +598,8 @@ impl fmt::Debug for AccountOp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("AccountOp { ")?;
f.write_fmt(format_args!(
"addr: {:?}, field: {:?}, val_prev: 0x{:x}, val: 0x{:x}",
self.address, self.field, self.value_prev, self.value
"addr: {:?}, tx_id: {:?}, field: {:?}, val_prev: 0x{:x}, val: 0x{:x}",
self.address, self.tx_id, self.field, self.value_prev, self.value
))?;
f.write_str(" }")
}
Expand Down
14 changes: 12 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,22 @@ impl<F: Field> ExecutionGadget<F> for BalanceGadget<F> {
);
let code_hash = cb.query_cell_phase2();
// For non-existing accounts the code_hash must be 0 in the rw_table.
cb.account_read(address.expr(), AccountFieldTag::CodeHash, code_hash.expr());
cb.account_read(
tx_id.expr(),
address.expr(),
AccountFieldTag::CodeHash,
code_hash.expr(),
);
let not_exists = IsZeroGadget::construct(cb, code_hash.expr());
let exists = not::expr(not_exists.expr());
let balance = cb.query_cell_phase2();
cb.condition(exists.expr(), |cb| {
cb.account_read(address.expr(), AccountFieldTag::Balance, balance.expr());
cb.account_read(
tx_id.expr(),
address.expr(),
AccountFieldTag::Balance,
balance.expr(),
);
});
cb.condition(not_exists.expr(), |cb| {
cb.require_zero("balance is zero when non_exists", balance.expr());
Expand Down
4 changes: 4 additions & 0 deletions zkevm-circuits/src/evm_circuit/execution/begin_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ impl<F: Field> ExecutionGadget<F> for BeginTxGadget<F> {
// Increase caller's nonce.
// (tx caller's nonce always increases even when tx ends with error)
cb.account_write(
tx_id.expr(),
tx_caller_address.expr(),
AccountFieldTag::Nonce,
sender_nonce.expr() + 1.expr(),
Expand Down Expand Up @@ -324,6 +325,7 @@ impl<F: Field> ExecutionGadget<F> for BeginTxGadget<F> {
call_code_hash_is_empty.expr() + call_code_hash_is_zero.expr();

cb.account_read(
tx_id.expr(),
call_callee_address.expr(),
AccountFieldTag::CodeHash,
account_code_hash.expr(),
Expand All @@ -332,6 +334,7 @@ impl<F: Field> ExecutionGadget<F> for BeginTxGadget<F> {
// Transfer value from caller to callee, creating account if necessary.
let transfer_with_gas_fee = TransferWithGasFeeGadget::construct(
cb,
tx_id.expr(),
tx_caller_address.expr(),
call_callee_address.expr(),
not::expr(account_code_hash_is_zero.expr()),
Expand Down Expand Up @@ -414,6 +417,7 @@ impl<F: Field> ExecutionGadget<F> for BeginTxGadget<F> {
});

cb.account_write(
tx_id.expr(),
call_callee_address.expr(),
AccountFieldTag::Nonce,
1.expr(),
Expand Down
3 changes: 3 additions & 0 deletions zkevm-circuits/src/evm_circuit/execution/callop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl<F: Field> ExecutionGadget<F> for CallOpGadget<F> {
let call_gadget: CommonCallGadget<F, MemoryAddressGadget<F>, true> =
CommonCallGadget::construct(
cb,
tx_id.expr(),
is_call.expr(),
is_callcode.expr(),
is_delegatecall.expr(),
Expand Down Expand Up @@ -189,6 +190,7 @@ impl<F: Field> ExecutionGadget<F> for CallOpGadget<F> {

let caller_balance_word = cb.query_word_rlc();
cb.account_read(
tx_id.expr(),
caller_address.expr(),
AccountFieldTag::Balance,
caller_balance_word.expr(),
Expand Down Expand Up @@ -246,6 +248,7 @@ impl<F: Field> ExecutionGadget<F> for CallOpGadget<F> {
let transfer = cb.condition(and::expr(&[is_call.expr(), is_precheck_ok.expr()]), |cb| {
TransferGadget::construct(
cb,
tx_id.expr(),
caller_address.expr(),
callee_address.expr(),
not::expr(call_gadget.callee_not_exists.expr()),
Expand Down
Loading