From f59ce8c5a65870564fea08f87c24e6a809e36b60 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Mon, 29 Apr 2024 13:36:10 +0200 Subject: [PATCH] feat(interpreter): add helpers for spending all gas (#1360) --- crates/interpreter/src/gas.rs | 19 +++++++++++++++++++ crates/revm/src/handler/mainnet/execution.rs | 3 +-- crates/revm/src/inspector/gas.rs | 10 ++-------- crates/revm/src/optimism/handler_register.rs | 3 +-- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/crates/interpreter/src/gas.rs b/crates/interpreter/src/gas.rs index 387f473774..5d1b5d75ce 100644 --- a/crates/interpreter/src/gas.rs +++ b/crates/interpreter/src/gas.rs @@ -35,6 +35,18 @@ impl Gas { } } + /// Creates a new `Gas` struct with the given gas limit, but without any gas remaining. + #[inline] + pub const fn new_spent(limit: u64) -> Self { + Self { + limit, + remaining: 0, + remaining_nomem: 0, + memory: 0, + refunded: 0, + } + } + /// Returns the gas limit. #[inline] pub const fn limit(&self) -> u64 { @@ -79,6 +91,13 @@ impl Gas { self.remaining += returned; } + /// Spends all remaining gas. + #[inline] + pub fn spend_all(&mut self) { + self.remaining = 0; + self.remaining_nomem = 0; + } + /// Records a refund value. /// /// `refund` can be negative but `self.refunded` should always be positive diff --git a/crates/revm/src/handler/mainnet/execution.rs b/crates/revm/src/handler/mainnet/execution.rs index c4985b5445..852dab3721 100644 --- a/crates/revm/src/handler/mainnet/execution.rs +++ b/crates/revm/src/handler/mainnet/execution.rs @@ -24,8 +24,7 @@ pub fn frame_return_with_refund_flag( let refunded = gas.refunded(); // Spend the gas limit. Gas is reimbursed when the tx returns successfully. - *gas = Gas::new(env.tx.gas_limit); - gas.record_cost(env.tx.gas_limit); + *gas = Gas::new_spent(env.tx.gas_limit); match instruction_result { return_ok!() => { diff --git a/crates/revm/src/inspector/gas.rs b/crates/revm/src/inspector/gas.rs index dc98bcaa95..7542c3d3bf 100644 --- a/crates/revm/src/inspector/gas.rs +++ b/crates/revm/src/inspector/gas.rs @@ -60,10 +60,7 @@ impl Inspector for GasInspector { mut outcome: CallOutcome, ) -> CallOutcome { if outcome.result.result.is_error() { - outcome - .result - .gas - .record_cost(outcome.result.gas.remaining()); + outcome.result.gas.spend_all(); self.gas_remaining = 0; } outcome @@ -76,10 +73,7 @@ impl Inspector for GasInspector { mut outcome: CreateOutcome, ) -> CreateOutcome { if outcome.result.result.is_error() { - outcome - .result - .gas - .record_cost(outcome.result.gas.remaining()); + outcome.result.gas.spend_all(); self.gas_remaining = 0; } outcome diff --git a/crates/revm/src/optimism/handler_register.rs b/crates/revm/src/optimism/handler_register.rs index 600d6709d0..0d3bcf4048 100644 --- a/crates/revm/src/optimism/handler_register.rs +++ b/crates/revm/src/optimism/handler_register.rs @@ -82,8 +82,7 @@ pub fn last_frame_return( let remaining = gas.remaining(); let refunded = gas.refunded(); // Spend the gas limit. Gas is reimbursed when the tx returns successfully. - *gas = Gas::new(tx_gas_limit); - gas.record_cost(tx_gas_limit); + *gas = Gas::new_spent(tx_gas_limit); match instruction_result { return_ok!() => {