Skip to content

Commit

Permalink
fix(GasInspector): calculate correct remaining gas after call return (b…
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita authored and fubuloubu committed Apr 11, 2024
1 parent 8f95aa8 commit 00ea9cc
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions crates/revm/src/inspector/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,22 @@ impl<DB: Database> Inspector<DB> for GasInspector {
self.gas_remaining = interp.gas.limit();
}

fn step(
&mut self,
interp: &mut crate::interpreter::Interpreter,
_context: &mut EvmContext<DB>,
) {
self.gas_remaining = interp.gas.remaining();
}

fn step_end(
&mut self,
interp: &mut crate::interpreter::Interpreter,
_context: &mut EvmContext<DB>,
) {
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(
Expand All @@ -65,8 +73,15 @@ impl<DB: Database> Inspector<DB> for GasInspector {
&mut self,
_context: &mut EvmContext<DB>,
_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
}
}
Expand Down

0 comments on commit 00ea9cc

Please sign in to comment.