Skip to main content

GasAnalyzer

Struct GasAnalyzer 

Source
pub struct GasAnalyzer;
Expand description

Analyses gas usage for opcode sequences without full execution context.

Implementations§

Source§

impl GasAnalyzer

Source

pub fn analyze_gas_usage(opcodes: &[u8], fork: Fork) -> GasAnalysis

Analyse gas usage for a raw opcode byte sequence.

Operands are estimated heuristically; for precise analysis, use DynamicGasCalculator::analyze_sequence_gas with explicit operands.

Examples found in repository?
examples/gas_analysis.rs (line 36)
10fn main() {
11    println!("EOT - Gas Analysis Example\n");
12
13    // 1. Dynamic gas calculation
14    println!("== Dynamic Gas (Berlin) ==");
15    let calc = DynamicGasCalculator::new(Fork::Berlin);
16    let mut ctx = ExecutionContext::new();
17
18    let cold = calc.calculate_gas_cost(0x54, &ctx, &[0x100]).unwrap();
19    println!("SLOAD cold: {cold} gas");
20
21    // Warm the slot
22    let key = {
23        let mut k = [0u8; 32];
24        k[24..32].copy_from_slice(&0x100u64.to_be_bytes());
25        k
26    };
27    let addr = ctx.current_address;
28    ctx.mark_storage_accessed(&addr, &key);
29    let warm = calc.calculate_gas_cost(0x54, &ctx, &[0x100]).unwrap();
30    println!("SLOAD warm: {warm} gas");
31    println!();
32
33    // 2. Sequence analysis
34    println!("== Sequence Analysis ==");
35    let seq = [0x60, 0x54, 0x54, 0x01, 0x55];
36    let analysis = GasAnalyzer::analyze_gas_usage(&seq, Fork::London);
37    println!("Total gas: {}", analysis.total_gas);
38    println!("Efficiency: {}%", analysis.efficiency_score());
39    for opt in analysis.get_optimization_recommendations() {
40        println!("  Tip: {opt}");
41    }
42    println!();
43
44    // 3. Fork comparison
45    println!("== Istanbul -> Berlin Changes ==");
46    let report = GasComparator::generate_comparison_report(Fork::Istanbul, Fork::Berlin);
47    report.print_report();
48    println!();
49
50    // 4. Optimization advice
51    println!("== Cancun Optimizations ==");
52    for tip in GasOptimizationAdvisor::get_fork_optimizations(Fork::Cancun) {
53        println!("  - {tip}");
54    }
55}
Source

pub fn validate_opcode_sequence( opcodes: &[u8], fork: Fork, ) -> Result<(), String>

Validate that an opcode sequence doesn’t have obvious gas issues.

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> 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, 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.