GasAnalysis

Struct GasAnalysis 

Source
pub struct GasAnalysis {
    pub total_gas: u64,
    pub breakdown: Vec<(u8, u16)>,
    pub optimizations: Vec<String>,
    pub warnings: Vec<String>,
}
Expand description

Enhanced gas analysis structure for compatibility with existing validation system

Fields§

§total_gas: u64

Total base gas cost

§breakdown: Vec<(u8, u16)>

Gas cost breakdown by opcode

§optimizations: Vec<String>

Potential optimizations

§warnings: Vec<String>

Warnings about expensive operations

Implementations§

Source§

impl GasAnalysis

Source

pub fn new() -> Self

Create a new gas analysis

Source

pub fn efficiency_score(&self) -> u8

Calculate gas efficiency score (0-100, higher is better)

Examples found in repository?
examples/gas_analysis.rs (line 62)
41fn basic_gas_analysis() -> Result<(), Box<dyn std::error::Error>> {
42    println!("📊 Example 1: Basic Gas Analysis");
43
44    // Create a simple bytecode sequence
45    let opcodes = vec![
46        0x60, 0x10, // PUSH1 0x10
47        0x60, 0x20, // PUSH1 0x20
48        0x01, // ADD
49        0x60, 0x00, // PUSH1 0x00
50        0x52, // MSTORE
51        0x60, 0x20, // PUSH1 0x20
52        0x60, 0x00, // PUSH1 0x00
53        0xf3, // RETURN
54    ];
55
56    // Analyze gas usage using the registry
57    use crate::traits::OpcodeAnalysis;
58    let analysis = OpcodeRegistry::analyze_gas_usage(&opcodes, Fork::London);
59
60    println!("Bytecode analysis:");
61    println!("  Total gas: {} gas", analysis.total_gas);
62    println!("  Efficiency score: {}/100", analysis.efficiency_score());
63    println!("  Opcodes analyzed: {}", analysis.breakdown.len());
64
65    // Show gas breakdown
66    println!("\nGas breakdown:");
67    for (opcode, gas_cost) in &analysis.breakdown {
68        println!("  0x{:02x}: {} gas", opcode, gas_cost);
69    }
70
71    // Get optimization suggestions
72    let suggestions = OpcodeRegistry::get_optimization_suggestions(&opcodes, Fork::Shanghai);
73    if !suggestions.is_empty() {
74        println!("\nOptimization suggestions:");
75        for (i, suggestion) in suggestions.iter().enumerate() {
76            println!("  {}. {}", i + 1, suggestion);
77        }
78    }
79
80    // Validate the sequence
81    match OpcodeRegistry::validate_opcode_sequence(&opcodes, Fork::London) {
82        Ok(()) => println!("\n✅ Opcode sequence is valid"),
83        Err(e) => println!("\n❌ Validation error: {}", e),
84    }
85
86    Ok(())
87}
Source

pub fn get_optimization_recommendations(&self) -> Vec<String>

Get recommendations for gas optimization

Source

pub fn is_optimized(&self) -> bool

Check if this represents an optimized gas usage pattern

Source

pub fn gas_by_category(&self) -> HashMap<GasCostCategory, u64>

Get gas usage by category

Source

pub fn find_gas_bombs(&self) -> Vec<String>

Find potential gas bombs (operations that could cause out-of-gas)

Source

pub fn estimate_optimization_savings(&self) -> u64

Estimate gas savings from proposed optimizations

Trait Implementations§

Source§

impl Clone for GasAnalysis

Source§

fn clone(&self) -> GasAnalysis

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for GasAnalysis

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for GasAnalysis

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.