Skip to content

Commit

Permalink
Refactor handler functions in EVM implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
johntaiko committed Dec 18, 2023
1 parent 2e96144 commit 327dc05
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 96 deletions.
2 changes: 1 addition & 1 deletion crates/revm/src/evm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB,
let journaled_state = JournaledState::new(precompiles.len(), GSPEC::SPEC_ID);

#[cfg(feature = "optimism")]
let handler = if env.cfg.optimism {
let handler: Handler<DB> = if env.cfg.optimism {
Handler::optimism::<GSPEC>()
} else {
Handler::mainnet::<GSPEC>()
Expand Down
4 changes: 2 additions & 2 deletions crates/revm/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl<DB: Database> Handler<DB> {
pub fn taiko<SPEC: Spec>() -> Self {
use crate::taiko::handler;
Self {
call_return: handler::handle_call_return::<SPEC>,
calculate_gas_refund: handler::calculate_gas_refund::<SPEC>,
call_return: mainnet::handle_call_return::<SPEC>,
calculate_gas_refund: mainnet::calculate_gas_refund::<SPEC>,
reimburse_caller: handler::handle_reimburse_caller::<SPEC, DB>,
reward_beneficiary: handler::reward_beneficiary::<SPEC, DB>,
}
Expand Down
93 changes: 0 additions & 93 deletions crates/revm/src/taiko/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,6 @@ use crate::{
EVMData,
};

/// Handle output of the transaction
pub fn handle_call_return<SPEC: Spec>(
env: &Env,
call_result: InstructionResult,
returned_gas: Gas,
) -> Gas {
let tx_gas_limit = env.tx.gas_limit;
// Spend the gas limit. Gas is reimbursed when the tx returns successfully.
let mut gas = Gas::new(tx_gas_limit);
if env.tx.taiko.is_anchor {
return gas;
}
gas.record_cost(tx_gas_limit);

match call_result {
return_ok!() => {
gas.erase_cost(returned_gas.remaining());
gas.record_refund(returned_gas.refunded());
}
return_revert!() => {
gas.erase_cost(returned_gas.remaining());
}
_ => {}
}
gas
}

#[inline]
pub fn handle_reimburse_caller<SPEC: Spec, DB: Database>(
data: &mut EVMData<'_, DB>,
Expand Down Expand Up @@ -108,69 +81,3 @@ pub fn reward_beneficiary<SPEC: Spec, DB: Database>(
.saturating_add(basefee * U256::from(gas.spend() - gas_refund));
Ok(())
}

/// Calculate gas refund for transaction.
///
/// If config is set to disable gas refund, it will return 0.
///
/// If spec is set to london, it will decrease the maximum refund amount to 5th part of
/// gas spend. (Before london it was 2th part of gas spend)
#[inline]
pub fn calculate_gas_refund<SPEC: Spec>(env: &Env, gas: &Gas) -> u64 {
// no refund for anchor tx and gas refund disabled.
if env.tx.taiko.is_anchor || env.cfg.is_gas_refund_disabled() {
0
} else {
// EIP-3529: Reduction in refunds
let max_refund_quotient = if SPEC::enabled(LONDON) { 5 } else { 2 };
(gas.refunded() as u64).min(gas.spend() / max_refund_quotient)
}
}

#[cfg(test)]
mod tests {
use revm_interpreter::primitives::CancunSpec;

use super::*;

#[test]
fn test_consume_gas() {
let mut env = Env::default();
env.tx.gas_limit = 100;

let gas = handle_call_return::<CancunSpec>(&env, InstructionResult::Stop, Gas::new(90));
assert_eq!(gas.remaining(), 90);
assert_eq!(gas.spend(), 10);
assert_eq!(gas.refunded(), 0);
}

#[test]
fn test_consume_gas_with_refund() {
let mut env = Env::default();
env.tx.gas_limit = 100;

let mut return_gas = Gas::new(90);
return_gas.record_refund(30);

let gas = handle_call_return::<CancunSpec>(&env, InstructionResult::Stop, return_gas);
assert_eq!(gas.remaining(), 90);
assert_eq!(gas.spend(), 10);
assert_eq!(gas.refunded(), 30);

let gas = handle_call_return::<CancunSpec>(&env, InstructionResult::Revert, return_gas);
assert_eq!(gas.remaining(), 90);
assert_eq!(gas.spend(), 10);
assert_eq!(gas.refunded(), 0);
}

#[test]
fn test_revert_gas() {
let mut env = Env::default();
env.tx.gas_limit = 100;

let gas = handle_call_return::<CancunSpec>(&env, InstructionResult::Revert, Gas::new(90));
assert_eq!(gas.remaining(), 90);
assert_eq!(gas.spend(), 10);
assert_eq!(gas.refunded(), 0);
}
}

0 comments on commit 327dc05

Please sign in to comment.