Skip to content

Commit

Permalink
Don't burn base fee (#302)
Browse files Browse the repository at this point in the history
* Don't burn base fee

* small change on estimate gas test

---------

Co-authored-by: eyusufatik <[email protected]>
  • Loading branch information
kpp and eyusufatik authored Mar 28, 2024
1 parent f97c974 commit 855bc31
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
46 changes: 39 additions & 7 deletions crates/evm/src/evm/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -90,15 +92,45 @@ where
DB: Database,
EXT: CitreaHandlerContext,
{
let post_execution = &mut handler.post_execution;
post_execution.output = Arc::new(CitreaHandler::<EXT, DB>::post_execution_output);
spec_to_generic!(handler.cfg.spec_id, {
let post_execution = &mut handler.post_execution;
post_execution.reward_beneficiary =
Arc::new(CitreaHandler::<SPEC, EXT, DB>::reward_beneficiary);
post_execution.output = Arc::new(CitreaHandler::<SPEC, EXT, DB>::post_execution_output);
})
}

struct CitreaHandler<EXT, DB> {
_phantom: std::marker::PhantomData<(EXT, DB)>,
struct CitreaHandler<SPEC, EXT, DB> {
_phantom: std::marker::PhantomData<(SPEC, EXT, DB)>,
}

impl<EXT: CitreaHandlerContext, DB: Database> CitreaHandler<EXT, DB> {
impl<SPEC: Spec, EXT: CitreaHandlerContext, DB: Database> CitreaHandler<SPEC, EXT, DB> {
fn reward_beneficiary(
context: &mut Context<EXT, DB>,
gas: &Gas,
) -> Result<(), EVMError<DB::Error>> {
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<EXT, DB>,
result: FrameResult,
Expand Down
6 changes: 4 additions & 2 deletions crates/evm/src/tests/call_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 2 additions & 4 deletions crates/evm/src/tests/queries/estimate_gas_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
};

Expand All @@ -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 {
Expand Down

0 comments on commit 855bc31

Please sign in to comment.