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

Commit

Permalink
[fix] end_tx circuit coinbase transfer (#1609)
Browse files Browse the repository at this point in the history
### Description

fix end_tx circuit coinbase transfer

### Issue Link


https://github.com/privacy-scaling-explorations/zkevm-circuits/actions/runs/6180463331/job/16776966002?pr=1581

### Type of change

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update

### Contents

- add rw_delta to TransferTo

### Rationale

N/A

### How Has This Been Tested?

previous failed unit test passed
  • Loading branch information
lightsing authored and ChihChengLiang committed Sep 20, 2023
1 parent d0aefc8 commit 8415194
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
45 changes: 22 additions & 23 deletions zkevm-circuits/src/evm_circuit/execution/end_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,7 @@ impl<F: Field> ExecutionGadget<F> for EndTxGadget<F> {
);

cb.require_step_state_transition(StepStateTransition {
rw_counter: Delta(
11.expr() - is_first_tx.expr() + coinbase_code_hash_is_zero.expr(),
),
rw_counter: Delta(10.expr() - is_first_tx.expr() + coinbase_reward.rw_delta()),
..StepStateTransition::any()
});
},
Expand All @@ -185,9 +183,7 @@ impl<F: Field> ExecutionGadget<F> for EndTxGadget<F> {
cb.next.execution_state_selector([ExecutionState::EndBlock]),
|cb| {
cb.require_step_state_transition(StepStateTransition {
rw_counter: Delta(
10.expr() - is_first_tx.expr() + coinbase_code_hash_is_zero.expr(),
),
rw_counter: Delta(9.expr() - is_first_tx.expr() + coinbase_reward.rw_delta()),
// We propagate call_id so that EndBlock can get the last tx_id
// in order to count processed txs.
call_id: Same,
Expand Down Expand Up @@ -230,16 +226,6 @@ impl<F: Field> ExecutionGadget<F> for EndTxGadget<F> {
let (refund, _) = block.get_rws(step, 2).tx_refund_value_pair();
let (caller_balance, caller_balance_prev) = block.get_rws(step, 3).account_balance_pair();
let (coinbase_code_hash_prev, _) = block.get_rws(step, 4).account_codehash_pair();
let (coinbase_balance, coinbase_balance_prev) = block
.get_rws(
step,
if coinbase_code_hash_prev.is_zero() {
6
} else {
5
},
)
.account_balance_pair();

self.tx_id
.assign(region, offset, Value::known(F::from(tx.id)))?;
Expand Down Expand Up @@ -273,6 +259,7 @@ impl<F: Field> ExecutionGadget<F> for EndTxGadget<F> {
caller_balance,
)?;
let effective_tip = tx.gas_price - block.context.base_fee;
let coinbase_reward = effective_tip * gas_used;
self.sub_gas_price_by_base_fee.assign(
region,
offset,
Expand All @@ -284,20 +271,32 @@ impl<F: Field> ExecutionGadget<F> for EndTxGadget<F> {
offset,
effective_tip,
gas_used,
effective_tip * gas_used,
coinbase_reward,
)?;
self.coinbase
.assign_h160(region, offset, block.context.coinbase)?;
self.coinbase_code_hash
.assign_u256(region, offset, coinbase_code_hash_prev)?;
self.coinbase_code_hash_is_zero
.assign_u256(region, offset, coinbase_code_hash_prev)?;
self.coinbase_reward.assign(
region,
offset,
(coinbase_balance, coinbase_balance_prev),
effective_tip * gas_used,
)?;
if !coinbase_reward.is_zero() {
let coinbase_balance_pair = block
.get_rws(
step,
if coinbase_code_hash_prev.is_zero() {
6
} else {
5
},
)
.account_balance_pair();
self.coinbase_reward.assign(
region,
offset,
coinbase_balance_pair,
effective_tip * gas_used,
)?;
}

let current_cumulative_gas_used: u64 = if tx.id == 1 {
0
Expand Down
10 changes: 10 additions & 0 deletions zkevm-circuits/src/evm_circuit/util/common_gadget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,16 @@ impl<F: Field> TransferToGadget<F> {
.assign_value(region, offset, Value::known(Word::from(value)))?;
Ok(())
}

pub(crate) fn rw_delta(&self) -> Expression<F> {
// +1 Write Account (receiver) CodeHash (account creation via code_hash update)
or::expr([
not::expr(self.value_is_zero.expr()) * not::expr(self.receiver_exists.expr()),
self.must_create.clone()]
) +
// +1 Write Account (receiver) Balance
not::expr(self.value_is_zero.expr())
}
}

// TODO: Merge with TransferGadget
Expand Down

0 comments on commit 8415194

Please sign in to comment.