Skip to content

Commit

Permalink
feat: add opcode gas iter
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Mar 5, 2024
1 parent 5d560be commit 524a11c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ clippy.lint_groups_priority = "allow"
# eth
alloy-sol-types = "0.6"
alloy-primitives = "0.6"
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy", rev = "785c667" }
alloy-rpc-trace-types = { git = "https://github.com/alloy-rs/alloy", rev = "785c667" }
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy", rev = "975a52a" }
alloy-rpc-trace-types = { git = "https://github.com/alloy-rs/alloy", rev = "975a52a" }
revm = { version = "6.0", default-features = false, features = ["std"] }

anstyle = "1.0"
Expand Down
16 changes: 15 additions & 1 deletion src/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use revm::{
Database, EvmContext, Inspector,
};
use std::collections::HashMap;
use alloy_rpc_trace_types::opcode::OpcodeGas;

/// An Inspector that counts opcodes and measures gas usage per opcode.
#[derive(Clone, Debug, Default)]
Expand Down Expand Up @@ -32,12 +33,25 @@ impl OpcodeCounterInspector {
}

/// Returns an iterator over all opcodes with their count and combined gas usage.
pub fn iter_opcodes(&self) -> impl Iterator<Item = (OpCode, (u64, u64))> + '_ {
///
/// Note: this returns in no particular order.
pub fn opcode_iter(&self) -> impl Iterator<Item = (OpCode, (u64, u64))> + '_ {
self.opcode_counts.iter().map(move |(&opcode, &count)| {
let gas = self.opcode_gas.get(&opcode).copied().unwrap_or_default();
(opcode, (count, gas))
})
}

/// Returns an iterator over all opcodes with their count and combined gas usage.
///
/// Note: this returns in no particular order.
pub fn opcode_gas_iter(&self) -> impl Iterator<Item = OpcodeGas> + '_ {
self.opcode_iter().map(|(opcode, (count, gas_used))| OpcodeGas {
opcode: opcode.to_string(),
count,
gas_used,
})
}
}

impl<DB> Inspector<DB> for OpcodeCounterInspector
Expand Down

0 comments on commit 524a11c

Please sign in to comment.