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

[fix] skip value is zero in end_tx #1600

Merged
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
37 changes: 37 additions & 0 deletions bus-mapping/src/circuit_input_builder/input_state_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>, Error> {
self.code_db
Expand Down
21 changes: 5 additions & 16 deletions bus-mapping/src/evm/opcodes/begin_end_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,8 @@ fn gen_end_tx_steps(state: &mut CircuitInputStateRef) -> Result<ExecStep, Error>
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know when will the coinbase_transfer_value be zero? Is it when the signature or nonce was bad or tx.gas_limit was set to zero?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lispc said according to spec, the gas price can be zero, then value is zero. though it won't happen in real world

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a wrong assumption that the coinbase_transfer_value will never be zero so in the previous pr I didn't include this

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lispc said according to spec, the gas price can be zero, then value is zero.

Makes sense. Thanks for elaborating on it.

let coinbase_balance = coinbase_balance_prev + coinbase_transfer_value;
state.account_read(
&mut exec_step,
state.block.coinbase,
Expand All @@ -291,21 +289,12 @@ fn gen_end_tx_steps(state: &mut CircuitInputStateRef) -> Result<ExecStep, Error>
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
Expand Down
Loading