From 6e15b07fd9621914dcee591aacea777ff2edb85f Mon Sep 17 00:00:00 2001 From: rakita Date: Thu, 28 Mar 2024 13:53:48 +0100 Subject: [PATCH] fix(GasInspector): calculate correct remaining gas after call return --- crates/revm/src/inspector/gas.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/crates/revm/src/inspector/gas.rs b/crates/revm/src/inspector/gas.rs index d22ae0451e..dc98bcaa95 100644 --- a/crates/revm/src/inspector/gas.rs +++ b/crates/revm/src/inspector/gas.rs @@ -35,14 +35,22 @@ impl Inspector for GasInspector { self.gas_remaining = interp.gas.limit(); } + fn step( + &mut self, + interp: &mut crate::interpreter::Interpreter, + _context: &mut EvmContext, + ) { + self.gas_remaining = interp.gas.remaining(); + } + fn step_end( &mut self, interp: &mut crate::interpreter::Interpreter, _context: &mut EvmContext, ) { - let last_gas_remaining = - core::mem::replace(&mut self.gas_remaining, interp.gas.remaining()); - self.last_gas_cost = last_gas_remaining.saturating_sub(self.gas_remaining); + let remaining = interp.gas.remaining(); + self.last_gas_cost = self.gas_remaining.saturating_sub(remaining); + self.gas_remaining = remaining; } fn call_end( @@ -65,8 +73,15 @@ impl Inspector for GasInspector { &mut self, _context: &mut EvmContext, _inputs: &CreateInputs, - outcome: CreateOutcome, + mut outcome: CreateOutcome, ) -> CreateOutcome { + if outcome.result.result.is_error() { + outcome + .result + .gas + .record_cost(outcome.result.gas.remaining()); + self.gas_remaining = 0; + } outcome } }