From 03c2047cfca5fa479310fe368defa546f226e04a Mon Sep 17 00:00:00 2001 From: johntaiko Date: Tue, 8 Aug 2023 03:49:40 +0000 Subject: [PATCH] feat: treat anchor specially --- .../circuit_input_builder/input_state_ref.rs | 25 ++-- bus-mapping/src/evm/opcodes/begin_end_tx.rs | 121 ++++++++++-------- 2 files changed, 77 insertions(+), 69 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 b012115263..564f63d466 100644 --- a/bus-mapping/src/circuit_input_builder/input_state_ref.rs +++ b/bus-mapping/src/circuit_input_builder/input_state_ref.rs @@ -474,29 +474,21 @@ impl<'a> CircuitInputStateRef<'a> { must_create: bool, value: Word, fee: Option, - is_anchor_tx: bool, ) -> Result<(), Error> { let (found, sender_account) = self.sdb.get_account(&sender); if !found { return Err(Error::AccountNotFound(sender)); } let mut sender_balance_prev = sender_account.balance; - if !is_anchor_tx { - debug_assert!( - sender_account.balance >= value + fee.unwrap_or_default(), - "invalid amount balance {:?} value {:?} fee {:?}", - sender_balance_prev, - value, - fee - ); - } + debug_assert!( + sender_account.balance >= value + fee.unwrap_or_default(), + "invalid amount balance {:?} value {:?} fee {:?}", + sender_balance_prev, + value, + fee + ); if let Some(fee) = fee { - let sender_balance = if is_anchor_tx { - // anchor tx doesn't need fee - sender_balance_prev - } else { - sender_balance_prev - fee - }; + let sender_balance = sender_balance_prev - fee; log::trace!( "sender balance update with fee (not reversible): {:?} {:?}->{:?}", sender, @@ -583,7 +575,6 @@ impl<'a> CircuitInputStateRef<'a> { must_create, value, None, - false, ) } diff --git a/bus-mapping/src/evm/opcodes/begin_end_tx.rs b/bus-mapping/src/evm/opcodes/begin_end_tx.rs index dcce22dd01..57fd10a9dd 100644 --- a/bus-mapping/src/evm/opcodes/begin_end_tx.rs +++ b/bus-mapping/src/evm/opcodes/begin_end_tx.rs @@ -98,6 +98,13 @@ fn gen_begin_tx_steps(state: &mut CircuitInputStateRef) -> Result Result Result }, ); - if !state.tx_ctx.is_anchor_tx() { - let effective_refund = refund - .min((state.tx.gas() - exec_step.gas_left.0) / MAX_REFUND_QUOTIENT_OF_GAS_USED as u64); - let (found, caller_account) = state.sdb.get_account(&call.caller_address); - if !found { - return Err(Error::AccountNotFound(call.caller_address)); - } - let caller_balance_prev = caller_account.balance; - let caller_balance = - caller_balance_prev + state.tx.tx.gas_price * (exec_step.gas_left.0 + effective_refund); - state.account_write( - &mut exec_step, - call.caller_address, - AccountField::Balance, - caller_balance, - caller_balance_prev, - )?; + let effective_refund = refund + .min((state.tx.gas() - exec_step.gas_left.0) / MAX_REFUND_QUOTIENT_OF_GAS_USED as u64); + let (found, caller_account) = state.sdb.get_account(&call.caller_address); + if !found { + return Err(Error::AccountNotFound(call.caller_address)); + } + let caller_balance_prev = caller_account.balance; + let caller_balance = caller_balance_prev + + if state.tx_ctx.is_anchor_tx() { + 0.into() + } else { + state.tx.tx.gas_price * (exec_step.gas_left.0 + effective_refund) + }; + state.account_write( + &mut exec_step, + call.caller_address, + AccountField::Balance, + caller_balance, + caller_balance_prev, + )?; - let effective_tip = state.tx.tx.gas_price - state.block.base_fee; - let (found, coinbase_account) = state.sdb.get_account(&state.block.coinbase); - if !found { - return Err(Error::AccountNotFound(state.block.coinbase)); - } - let coinbase_balance_prev = coinbase_account.balance; - let coinbase_reward = effective_tip * (state.tx.gas() - exec_step.gas_left.0); - let coinbase_balance = coinbase_balance_prev + coinbase_reward; - state.account_write( - &mut exec_step, - state.block.coinbase, - AccountField::Balance, - coinbase_balance, - coinbase_balance_prev, - )?; + let effective_tip = state.tx.tx.gas_price - state.block.base_fee; + let (found, coinbase_account) = state.sdb.get_account(&state.block.coinbase); + if !found { + return Err(Error::AccountNotFound(state.block.coinbase)); + } + let coinbase_balance_prev = coinbase_account.balance; + let coinbase_balance = coinbase_balance_prev + + if state.tx_ctx.is_anchor_tx() { + 0.into() + } else { + effective_tip * (state.tx.gas() - exec_step.gas_left.0) + }; + state.account_write( + &mut exec_step, + state.block.coinbase, + AccountField::Balance, + coinbase_balance, + coinbase_balance_prev, + )?; - // add treasury account - let (found, treasury_account) = state - .sdb - .get_account(&state.block.protocol_instance.meta_hash.treasury); - if !found { - return Err(Error::AccountNotFound( - state.block.protocol_instance.meta_hash.treasury, - )); - } - let treasury_balance_prev = treasury_account.balance; - let treasury_balance = - treasury_balance_prev + state.block.base_fee * (state.tx.gas() - exec_step.gas_left.0); - state.account_write( - &mut exec_step, + // add treasury account + let (found, treasury_account) = state + .sdb + .get_account(&state.block.protocol_instance.meta_hash.treasury); + if !found { + return Err(Error::AccountNotFound( state.block.protocol_instance.meta_hash.treasury, - AccountField::Balance, - treasury_balance, - treasury_balance_prev, - )?; + )); } + let treasury_balance_prev = treasury_account.balance; + let treasury_balance = treasury_balance_prev + + if state.tx_ctx.is_anchor_tx() { + 0.into() + } else { + state.block.base_fee * (state.tx.gas() - exec_step.gas_left.0) + }; + state.account_write( + &mut exec_step, + state.block.protocol_instance.meta_hash.treasury, + AccountField::Balance, + treasury_balance, + treasury_balance_prev, + )?; + // handle tx receipt tag state.tx_receipt_write( &mut exec_step,