From 5faedc9f262acf022d4ef8cd26053ea9e8f96303 Mon Sep 17 00:00:00 2001 From: lightsing Date: Wed, 13 Sep 2023 15:59:31 +0800 Subject: [PATCH] add transfer_to_irreversible --- .../circuit_input_builder/input_state_ref.rs | 37 +++++++++++++++++++ bus-mapping/src/evm/opcodes/begin_end_tx.rs | 21 +++-------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/bus-mapping/src/circuit_input_builder/input_state_ref.rs b/bus-mapping/src/circuit_input_builder/input_state_ref.rs index c3b7519fba..43ff543398 100644 --- a/bus-mapping/src/circuit_input_builder/input_state_ref.rs +++ b/bus-mapping/src/circuit_input_builder/input_state_ref.rs @@ -611,6 +611,43 @@ impl<'a> CircuitInputStateRef<'a> { ) } + /// Transfer to an address irreversibly. + pub fn transfer_to_irreversible( + &mut self, + step: &mut ExecStep, + receiver: Address, + receiver_exists: bool, + must_create: bool, + value: Word, + ) -> Result<(), Error> { + // If receiver doesn't exist, create it + if (!receiver_exists && !value.is_zero()) || must_create { + self.account_write( + step, + receiver, + AccountField::CodeHash, + CodeDB::empty_code_hash().to_word(), + Word::zero(), + )?; + } + if value.is_zero() { + // Skip transfer if value == 0 + return Ok(()); + } + let (_found, receiver_account) = self.sdb.get_account(&receiver); + let receiver_balance_prev = receiver_account.balance; + let receiver_balance = receiver_account.balance + value; + self.account_write( + step, + receiver, + AccountField::Balance, + receiver_balance, + receiver_balance_prev, + )?; + + Ok(()) + } + /// Fetch and return code for the given code hash from the code DB. pub fn code(&self, code_hash: H256) -> Result, Error> { self.code_db diff --git a/bus-mapping/src/evm/opcodes/begin_end_tx.rs b/bus-mapping/src/evm/opcodes/begin_end_tx.rs index 9a6656443f..ec26998080 100644 --- a/bus-mapping/src/evm/opcodes/begin_end_tx.rs +++ b/bus-mapping/src/evm/opcodes/begin_end_tx.rs @@ -277,10 +277,8 @@ fn gen_end_tx_steps(state: &mut CircuitInputStateRef) -> Result if !found { return Err(Error::AccountNotFound(state.block.coinbase)); } - let coinbase_account = coinbase_account.clone(); - let coinbase_balance_prev = coinbase_account.balance; + let coinbase_exist = !coinbase_account.is_empty(); let coinbase_transfer_value = effective_tip * (state.tx.gas() - exec_step.gas_left); - let coinbase_balance = coinbase_balance_prev + coinbase_transfer_value; state.account_read( &mut exec_step, state.block.coinbase, @@ -291,21 +289,12 @@ fn gen_end_tx_steps(state: &mut CircuitInputStateRef) -> Result coinbase_account.code_hash.to_word() }, ); - if coinbase_account.is_empty() { - state.account_write( - &mut exec_step, - state.block.coinbase, - AccountField::CodeHash, - CodeDB::empty_code_hash().to_word(), - Word::zero(), - )?; - } - state.account_write( + state.transfer_to_irreversible( &mut exec_step, state.block.coinbase, - AccountField::Balance, - coinbase_balance, - coinbase_balance_prev, + coinbase_exist, + false, + coinbase_transfer_value, )?; // handle tx receipt tag