From 855bc31e81742840449cf6a70c3781ad044ae9e5 Mon Sep 17 00:00:00 2001 From: Roman Date: Fri, 29 Mar 2024 02:41:03 +0800 Subject: [PATCH] Don't burn base fee (#302) * Don't burn base fee * small change on estimate gas test --------- Co-authored-by: eyusufatik --- crates/evm/src/evm/handler.rs | 46 ++++++++++++++++--- crates/evm/src/tests/call_tests.rs | 6 ++- .../src/tests/queries/estimate_gas_tests.rs | 6 +-- 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/crates/evm/src/evm/handler.rs b/crates/evm/src/evm/handler.rs index 7cd063c47..a2979eb5d 100644 --- a/crates/evm/src/evm/handler.rs +++ b/crates/evm/src/evm/handler.rs @@ -3,8 +3,10 @@ use std::mem::size_of; use std::sync::Arc; use revm::handler::register::{EvmHandler, HandleRegisters}; -use revm::interpreter::InstructionResult; -use revm::primitives::{Address, EVMError, HandlerCfg, ResultAndState, B256, U256}; +use revm::interpreter::{Gas, InstructionResult}; +use revm::primitives::{ + spec_to_generic, Address, EVMError, HandlerCfg, ResultAndState, Spec, SpecId, B256, U256, +}; use revm::{Context, Database, FrameResult, InnerEvmContext, JournalEntry}; #[derive(Copy, Clone)] @@ -90,15 +92,45 @@ where DB: Database, EXT: CitreaHandlerContext, { - let post_execution = &mut handler.post_execution; - post_execution.output = Arc::new(CitreaHandler::::post_execution_output); + spec_to_generic!(handler.cfg.spec_id, { + let post_execution = &mut handler.post_execution; + post_execution.reward_beneficiary = + Arc::new(CitreaHandler::::reward_beneficiary); + post_execution.output = Arc::new(CitreaHandler::::post_execution_output); + }) } -struct CitreaHandler { - _phantom: std::marker::PhantomData<(EXT, DB)>, +struct CitreaHandler { + _phantom: std::marker::PhantomData<(SPEC, EXT, DB)>, } -impl CitreaHandler { +impl CitreaHandler { + fn reward_beneficiary( + context: &mut Context, + gas: &Gas, + ) -> Result<(), EVMError> { + let beneficiary = context.evm.env.block.coinbase; + let effective_gas_price = context.evm.env.effective_gas_price(); + + // EIP-1559 discard basefee for coinbase transfer. + // ^ But we don't do that. + // We don't sub block.basefee from effective_gas_price. + let coinbase_gas_price = effective_gas_price; + + let (coinbase_account, _) = context + .evm + .inner + .journaled_state + .load_account(beneficiary, &mut context.evm.inner.db)?; + + coinbase_account.mark_touch(); + coinbase_account.info.balance = coinbase_account + .info + .balance + .saturating_add(coinbase_gas_price * U256::from(gas.spend() - gas.refunded() as u64)); + + Ok(()) + } fn post_execution_output( context: &mut Context, result: FrameResult, diff --git a/crates/evm/src/tests/call_tests.rs b/crates/evm/src/tests/call_tests.rs index 3e25e1602..99527b337 100644 --- a/crates/evm/src/tests/call_tests.rs +++ b/crates/evm/src/tests/call_tests.rs @@ -791,8 +791,10 @@ fn test_l1_fee_success() { ) } - run_tx(0, U256::from(885765), U256::ZERO); - run_tx(1, U256::from(885288), U256::from(477)); + let gas_fee_paid = 114235; + + run_tx(0, U256::from(885765), U256::from(gas_fee_paid)); + run_tx(1, U256::from(885288), U256::from(gas_fee_paid + 477)); } #[test] diff --git a/crates/evm/src/tests/queries/estimate_gas_tests.rs b/crates/evm/src/tests/queries/estimate_gas_tests.rs index 1b59a61b6..7fd3382e3 100644 --- a/crates/evm/src/tests/queries/estimate_gas_tests.rs +++ b/crates/evm/src/tests/queries/estimate_gas_tests.rs @@ -87,6 +87,7 @@ fn test_tx_request_fields_gas() { let tx_req_no_sender = TransactionRequest { from: None, + nonce: None, ..tx_req_contract_call.clone() }; @@ -95,10 +96,7 @@ fn test_tx_request_fields_gas() { Some(BlockNumberOrTag::Latest), &mut working_set, ); - assert_eq!( - result_no_sender, - Err(RpcInvalidTransactionError::GasTooHigh.into()) - ); + assert_eq!(result_no_sender.unwrap(), Uint::from_str("0x6601").unwrap()); working_set.unset_archival_version(); let tx_req_no_recipient = TransactionRequest {