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

Commit f36df6e

Browse files
authored
[fix] skip value is zero in end_tx (privacy-scaling-explorations#1600)
1 parent c67b4e8 commit f36df6e

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

bus-mapping/src/circuit_input_builder/input_state_ref.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,43 @@ impl<'a> CircuitInputStateRef<'a> {
611611
)
612612
}
613613

614+
/// Transfer to an address irreversibly.
615+
pub fn transfer_to_irreversible(
616+
&mut self,
617+
step: &mut ExecStep,
618+
receiver: Address,
619+
receiver_exists: bool,
620+
must_create: bool,
621+
value: Word,
622+
) -> Result<(), Error> {
623+
// If receiver doesn't exist, create it
624+
if (!receiver_exists && !value.is_zero()) || must_create {
625+
self.account_write(
626+
step,
627+
receiver,
628+
AccountField::CodeHash,
629+
CodeDB::empty_code_hash().to_word(),
630+
Word::zero(),
631+
)?;
632+
}
633+
if value.is_zero() {
634+
// Skip transfer if value == 0
635+
return Ok(());
636+
}
637+
let (_found, receiver_account) = self.sdb.get_account(&receiver);
638+
let receiver_balance_prev = receiver_account.balance;
639+
let receiver_balance = receiver_account.balance + value;
640+
self.account_write(
641+
step,
642+
receiver,
643+
AccountField::Balance,
644+
receiver_balance,
645+
receiver_balance_prev,
646+
)?;
647+
648+
Ok(())
649+
}
650+
614651
/// Fetch and return code for the given code hash from the code DB.
615652
pub fn code(&self, code_hash: H256) -> Result<Vec<u8>, Error> {
616653
self.code_db

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,8 @@ fn gen_end_tx_steps(state: &mut CircuitInputStateRef) -> Result<ExecStep, Error>
277277
if !found {
278278
return Err(Error::AccountNotFound(state.block.coinbase));
279279
}
280-
let coinbase_account = coinbase_account.clone();
281-
let coinbase_balance_prev = coinbase_account.balance;
280+
let coinbase_exist = !coinbase_account.is_empty();
282281
let coinbase_transfer_value = effective_tip * (state.tx.gas() - exec_step.gas_left);
283-
let coinbase_balance = coinbase_balance_prev + coinbase_transfer_value;
284282
state.account_read(
285283
&mut exec_step,
286284
state.block.coinbase,
@@ -291,21 +289,12 @@ fn gen_end_tx_steps(state: &mut CircuitInputStateRef) -> Result<ExecStep, Error>
291289
coinbase_account.code_hash.to_word()
292290
},
293291
);
294-
if coinbase_account.is_empty() {
295-
state.account_write(
296-
&mut exec_step,
297-
state.block.coinbase,
298-
AccountField::CodeHash,
299-
CodeDB::empty_code_hash().to_word(),
300-
Word::zero(),
301-
)?;
302-
}
303-
state.account_write(
292+
state.transfer_to_irreversible(
304293
&mut exec_step,
305294
state.block.coinbase,
306-
AccountField::Balance,
307-
coinbase_balance,
308-
coinbase_balance_prev,
295+
coinbase_exist,
296+
false,
297+
coinbase_transfer_value,
309298
)?;
310299

311300
// handle tx receipt tag

0 commit comments

Comments
 (0)