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

Commit 156a3a5

Browse files
lispclightsing
andauthored
fix accoute creation rw inside callop (#672)
* fix accoute creation rw inside callop * add testcase * fmt --------- Co-authored-by: lightsing <[email protected]>
1 parent a9ecb03 commit 156a3a5

File tree

7 files changed

+56
-14
lines changed

7 files changed

+56
-14
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bus-mapping/src/circuit_input_builder/input_state_ref.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ impl<'a> CircuitInputStateRef<'a> {
684684
value_prev: prev_keccak_code_hash,
685685
},
686686
)?;
687+
// TODO: set code size to 0?
687688
}
688689
}
689690
if value.is_zero() {

bus-mapping/src/evm/opcodes/return_revert.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ impl Opcode for ReturnRevert {
7676
state.call_context_read(&mut exec_step, call.call_id, field, value);
7777
}
7878

79+
// the 'nonce' field has already been set to 1 inside 'create' or 'begin_tx',
80+
// so here account should not be empty.
81+
// TODO: optimize this later.
7982
let account = state.sdb.get_account(&call.address).1.clone();
8083
let prev_code_hash = if account.is_empty() {
8184
Word::zero()

zkevm-circuits/src/evm_circuit/execution/begin_tx.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,11 +687,11 @@ impl<F: Field> ExecutionGadget<F> for BeginTxGadget<F> {
687687
let callee_exists = is_precompile || !account_code_hash.is_zero();
688688
let caller_balance_sub_fee_pair = rws.next().account_balance_pair();
689689
if (!callee_exists && !tx.value.is_zero()) || tx.is_create {
690-
rws.next();
690+
rws.next(); // codehash read
691691
account_code_hash = rws.next().account_codehash_pair().1;
692692
#[cfg(feature = "scroll")]
693693
{
694-
rws.next();
694+
rws.next(); // keccak codehash read
695695
let account_keccak_code_hash = rws.next().account_keccak_codehash_pair().1;
696696
self.account_keccak_code_hash.assign(
697697
region,

zkevm-circuits/src/evm_circuit/execution/callop.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,11 @@ impl<F: Field> ExecutionGadget<F> for CallOpGadget<F> {
817817
let (caller_balance_pair, callee_balance_pair) =
818818
if is_call && is_precheck_ok && !value.is_zero() {
819819
if !callee_exists {
820-
rw_offset += 1;
820+
rw_offset += 2; // codehash read and write
821+
#[cfg(feature = "scroll")]
822+
{
823+
rw_offset += 2; // keccak codehash read and write
824+
}
821825
}
822826
let caller_balance_pair =
823827
block.rws[step.rw_indices[18 + rw_offset]].account_balance_pair();
@@ -1181,7 +1185,10 @@ mod test {
11811185
};
11821186

11831187
use itertools::Itertools;
1184-
use mock::{test_ctx::helpers::account_0_code_account_1_no_code, TestContext};
1188+
use mock::{
1189+
test_ctx::helpers::{account_0_code_account_1_no_code, tx_from_1_to_0},
1190+
TestContext,
1191+
};
11851192

11861193
use rayon::prelude::{ParallelBridge, ParallelIterator};
11871194
use std::default::Default;
@@ -1552,6 +1559,28 @@ mod test {
15521559
);
15531560
}
15541561

1562+
#[test]
1563+
fn call_non_exist_with_value() {
1564+
let callee_code = bytecode! {
1565+
.op_call(0xc350, 0xff, 0x13, 0x0, 0x0, 0x0, 0x0)
1566+
};
1567+
1568+
let ctx = TestContext::<2, 1>::new(
1569+
None,
1570+
account_0_code_account_1_no_code(callee_code),
1571+
tx_from_1_to_0,
1572+
|block, _tx| block.number(0xcafeu64),
1573+
)
1574+
.unwrap();
1575+
1576+
CircuitTestBuilder::new_from_test_ctx(ctx)
1577+
.params(CircuitsParams {
1578+
max_rws: 500,
1579+
..Default::default()
1580+
})
1581+
.run();
1582+
}
1583+
15551584
#[test]
15561585
fn callop_error_depth() {
15571586
let callee_code = bytecode! {

zkevm-circuits/src/evm_circuit/execution/create.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -640,17 +640,27 @@ impl<F: Field, const IS_CREATE2: bool, const S: ExecutionState> ExecutionGadget<
640640

641641
if is_precheck_ok == 1 && !is_address_collision {
642642
/*
643-
14 + rw_offset: read code_hash
644-
15/16 + rw_offset: write code_hash inside transfer
645-
17/18 + rw_offset: write keccak_code_hash inside transfer
643+
rws:
644+
...
645+
read code_hash // 14 + rw_offset
646+
if creation needed:
647+
code_hash read // 15 + rw_offset
648+
code_hash write // 16 + rw_offset
649+
rw_offset += 2
650+
if feature = "scroll"
651+
keccak_code_hash read // 15 + rw_offset
652+
keecak_code_hash write // 16 + rw_offset
653+
rw_offset += 2
654+
caller balance // 15 + rw_offset
655+
callee balance // 16 + rw_offset
646656
*/
647-
rw_offset += 1;
657+
rw_offset += 2;
648658
#[cfg(feature = "scroll")]
649659
{
650660
rw_offset += 2; // Read Write empty Keccak code hash.
651661
}
652662
let [caller_balance_pair, callee_balance_pair] = if !value.is_zero() {
653-
let account_balance_pair = [16, 17]
663+
let account_balance_pair = [15, 16]
654664
.map(|i| block.rws[step.rw_indices[i + rw_offset]].account_balance_pair());
655665
rw_offset += 2;
656666
account_balance_pair
@@ -696,7 +706,7 @@ impl<F: Field, const IS_CREATE2: bool, const S: ExecutionState> ExecutionGadget<
696706
Value::known(if is_precheck_ok == 0 || is_address_collision {
697707
F::zero()
698708
} else {
699-
block.rws[step.rw_indices[23 + rw_offset]]
709+
block.rws[step.rw_indices[22 + rw_offset]]
700710
.call_context_value()
701711
.to_scalar()
702712
.unwrap()

zkevm-circuits/src/evm_circuit/execution/return_revert.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,10 +438,9 @@ impl<F: Field> ExecutionGadget<F> for ReturnRevertGadget<F> {
438438

439439
let is_contract_deployment = call.is_create && call.is_success && !length.is_zero();
440440
if !call.is_root {
441-
let mut rw_counter_offset = 3;
441+
let mut rw_counter_offset = 3; // stack read, stack read, call_context_read is_success
442442
if is_contract_deployment {
443-
rw_counter_offset += 6 + copy_rwc_inc;
444-
//rw_counter_offset += 6 + length.as_u64();
443+
rw_counter_offset += 6 + copy_rwc_inc; // 4 call_context_read + 2 codehash rw
445444
#[cfg(feature = "scroll")]
446445
{
447446
rw_counter_offset += 3; // keccak code hash rw, code size

0 commit comments

Comments
 (0)