Skip to content

Commit

Permalink
chore: Simplify GasInspector (#1919)
Browse files Browse the repository at this point in the history
* chore: Simplify GasInspector

* fmt
  • Loading branch information
rakita authored Dec 16, 2024
1 parent 99d1219 commit 3774f6e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 47 deletions.
2 changes: 1 addition & 1 deletion crates/inspector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ database = { workspace = true, features = ["serde"] }

[features]
default = ["std"]
# Preserve ordeder of json
# Preserve order of json field
std = ["serde?/std", "serde_json?/std", "serde_json?/preserve_order"]
serde = ["dep:serde", "revm/serde", "database/serde"]
serde-json = ["serde", "dep:serde_json"]
29 changes: 13 additions & 16 deletions crates/inspector/src/eip3155.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::io::Write;
pub struct TracerEip3155<CTX, INTR> {
#[derive_where(skip)]
output: Box<dyn Write>,
gas_inspector: GasInspector<CTX, INTR>,
gas_inspector: GasInspector,

/// Print summary of the execution.
print_summary: bool,
Expand All @@ -36,6 +36,7 @@ pub struct TracerEip3155<CTX, INTR> {
skip: bool,
include_memory: bool,
memory: Option<String>,
_phantom: std::marker::PhantomData<(CTX, INTR)>,
}

// # Output
Expand Down Expand Up @@ -153,6 +154,7 @@ where
refunded: 0,
mem_size: 0,
skip: false,
_phantom: Default::default(),
}
}

Expand Down Expand Up @@ -209,12 +211,12 @@ where
type Context = CTX;
type InterpreterTypes = INTR;

fn initialize_interp(&mut self, interp: &mut Interpreter<INTR>, context: &mut CTX) {
self.gas_inspector.initialize_interp(interp, context);
fn initialize_interp(&mut self, interp: &mut Interpreter<INTR>, _: &mut CTX) {
self.gas_inspector.initialize_interp(interp.control.gas());
}

fn step(&mut self, interp: &mut Interpreter<INTR>, context: &mut CTX) {
self.gas_inspector.step(interp, context);
fn step(&mut self, interp: &mut Interpreter<INTR>, _: &mut CTX) {
self.gas_inspector.step(interp.control.gas());
self.stack = interp.stack.clone_from();
self.memory = if self.include_memory {
Some(hex::encode_prefixed(
Expand All @@ -230,8 +232,8 @@ where
self.refunded = interp.control.gas().refunded();
}

fn step_end(&mut self, interp: &mut Interpreter<INTR>, context: &mut CTX) {
self.gas_inspector.step_end(interp, context);
fn step_end(&mut self, interp: &mut Interpreter<INTR>, _: &mut CTX) {
self.gas_inspector.step_end(interp.control.gas());
if self.skip {
self.skip = false;
return;
Expand Down Expand Up @@ -280,8 +282,8 @@ where
None
}

fn call_end(&mut self, context: &mut CTX, inputs: &CallInputs, outcome: &mut CallOutcome) {
self.gas_inspector.call_end(context, inputs, outcome);
fn call_end(&mut self, context: &mut CTX, _: &CallInputs, outcome: &mut CallOutcome) {
self.gas_inspector.call_end(outcome);
self.depth -= 1;

if self.depth == 0 {
Expand All @@ -291,13 +293,8 @@ where
}
}

fn create_end(
&mut self,
context: &mut CTX,
inputs: &CreateInputs,
outcome: &mut CreateOutcome,
) {
self.gas_inspector.create_end(context, inputs, outcome);
fn create_end(&mut self, context: &mut CTX, _: &CreateInputs, outcome: &mut CreateOutcome) {
self.gas_inspector.create_end(outcome);
self.depth -= 1;

if self.depth == 0 {
Expand Down
44 changes: 14 additions & 30 deletions crates/inspector/src/gas.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
//! GasIspector. Helper Inspector to calculate gas for others.
use crate::Inspector;
use revm::interpreter::{
interpreter_types::LoopControl, CallInputs, CallOutcome, CreateInputs, CreateOutcome,
Interpreter, InterpreterTypes,
};
use revm::interpreter::{CallOutcome, CreateOutcome, Gas};

/// Helper [Inspector] that keeps track of gas.
#[allow(dead_code)]
#[derive(Clone, Copy, Debug)]
pub struct GasInspector<CTX, INTR> {
pub struct GasInspector {
gas_remaining: u64,
last_gas_cost: u64,
_phantom: core::marker::PhantomData<(CTX, INTR)>,
}

impl<CTX, INTR> Default for GasInspector<CTX, INTR> {
impl Default for GasInspector {
fn default() -> Self {
Self::new()
}
}

impl<CTX, INTR> GasInspector<CTX, INTR> {
impl GasInspector {
pub fn gas_remaining(&self) -> u64 {
self.gas_remaining
}
Expand All @@ -34,49 +29,38 @@ impl<CTX, INTR> GasInspector<CTX, INTR> {
Self {
gas_remaining: 0,
last_gas_cost: 0,
_phantom: core::marker::PhantomData,
}
}
}

impl<CTX, INTR: InterpreterTypes> Inspector for GasInspector<CTX, INTR> {
type Context = CTX;
type InterpreterTypes = INTR;

impl GasInspector {
#[inline]
fn initialize_interp(
&mut self,
interp: &mut Interpreter<Self::InterpreterTypes>,
_: &mut Self::Context,
) {
self.gas_remaining = interp.control.gas().limit();
pub fn initialize_interp(&mut self, gas: &Gas) {
self.gas_remaining = gas.limit();
}

#[inline]
fn step(&mut self, interp: &mut Interpreter<Self::InterpreterTypes>, _: &mut Self::Context) {
self.gas_remaining = interp.control.gas().remaining();
pub fn step(&mut self, gas: &Gas) {
self.gas_remaining = gas.remaining();
}

#[inline]
fn step_end(
&mut self,
interp: &mut Interpreter<Self::InterpreterTypes>,
_: &mut Self::Context,
) {
let remaining = interp.control.gas().remaining();
pub fn step_end(&mut self, gas: &mut Gas) {
let remaining = gas.remaining();
self.last_gas_cost = self.gas_remaining.saturating_sub(remaining);
self.gas_remaining = remaining;
}

#[inline]
fn call_end(&mut self, _: &mut Self::Context, _: &CallInputs, outcome: &mut CallOutcome) {
pub fn call_end(&mut self, outcome: &mut CallOutcome) {
if outcome.result.result.is_error() {
outcome.result.gas.spend_all();
self.gas_remaining = 0;
}
}

#[inline]
fn create_end(&mut self, _: &mut Self::Context, _: &CreateInputs, outcome: &mut CreateOutcome) {
pub fn create_end(&mut self, outcome: &mut CreateOutcome) {
if outcome.result.result.is_error() {
outcome.result.gas.spend_all();
self.gas_remaining = 0;
Expand Down

0 comments on commit 3774f6e

Please sign in to comment.