revm_inspector/
gas.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! GasIspector. Helper Inspector to calculate gas for others.

use crate::Inspector;
use revm::{
    interpreter::{CallInputs, CallOutcome, CreateInputs, CreateOutcome, Interpreter},
    EvmContext, EvmWiring,
};

/// Helper [Inspector] that keeps track of gas.
#[allow(dead_code)]
#[derive(Clone, Copy, Debug, Default)]
pub struct GasInspector {
    gas_remaining: u64,
    last_gas_cost: u64,
}

impl GasInspector {
    pub fn gas_remaining(&self) -> u64 {
        self.gas_remaining
    }

    pub fn last_gas_cost(&self) -> u64 {
        self.last_gas_cost
    }
}

impl<EvmWiringT: EvmWiring> Inspector<EvmWiringT> for GasInspector {
    fn initialize_interp(
        &mut self,
        interp: &mut Interpreter,
        _context: &mut EvmContext<EvmWiringT>,
    ) {
        self.gas_remaining = interp.gas.limit();
    }

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

    fn step_end(&mut self, interp: &mut Interpreter, _context: &mut EvmContext<EvmWiringT>) {
        let remaining = interp.gas.remaining();
        self.last_gas_cost = self.gas_remaining.saturating_sub(remaining);
        self.gas_remaining = remaining;
    }

    fn call_end(
        &mut self,
        _context: &mut EvmContext<EvmWiringT>,
        _inputs: &CallInputs,
        mut outcome: CallOutcome,
    ) -> CallOutcome {
        if outcome.result.result.is_error() {
            outcome.result.gas.spend_all();
            self.gas_remaining = 0;
        }
        outcome
    }

    fn create_end(
        &mut self,
        _context: &mut EvmContext<EvmWiringT>,
        _inputs: &CreateInputs,
        mut outcome: CreateOutcome,
    ) -> CreateOutcome {
        if outcome.result.result.is_error() {
            outcome.result.gas.spend_all();
            self.gas_remaining = 0;
        }
        outcome
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::inspector_handle_register;
    use database::BenchmarkDB;
    use revm::{
        bytecode::{opcode, Bytecode},
        interpreter::Interpreter,
        primitives::{address, Bytes, Log, TxKind},
        wiring::EvmWiring as PrimitiveEvmWiring,
        wiring::{DefaultEthereumWiring, EthereumWiring},
        Evm, EvmWiring,
    };

    type TestEvmWiring = DefaultEthereumWiring;

    #[derive(Default, Debug)]
    struct StackInspector {
        pc: usize,
        gas_inspector: GasInspector,
        gas_remaining_steps: Vec<(usize, u64)>,
    }

    impl<EvmWiringT: EvmWiring> Inspector<EvmWiringT> for StackInspector {
        fn initialize_interp(
            &mut self,
            interp: &mut Interpreter,
            context: &mut EvmContext<EvmWiringT>,
        ) {
            self.gas_inspector.initialize_interp(interp, context);
        }

        fn step(&mut self, interp: &mut Interpreter, context: &mut EvmContext<EvmWiringT>) {
            self.pc = interp.program_counter();
            self.gas_inspector.step(interp, context);
        }

        fn log(
            &mut self,
            interp: &mut Interpreter,
            context: &mut EvmContext<EvmWiringT>,
            log: &Log,
        ) {
            self.gas_inspector.log(interp, context, log);
        }

        fn step_end(&mut self, interp: &mut Interpreter, context: &mut EvmContext<EvmWiringT>) {
            self.gas_inspector.step_end(interp, context);
            self.gas_remaining_steps
                .push((self.pc, self.gas_inspector.gas_remaining()));
        }

        fn call(
            &mut self,
            context: &mut EvmContext<EvmWiringT>,
            call: &mut CallInputs,
        ) -> Option<CallOutcome> {
            self.gas_inspector.call(context, call)
        }

        fn call_end(
            &mut self,
            context: &mut EvmContext<EvmWiringT>,
            inputs: &CallInputs,
            outcome: CallOutcome,
        ) -> CallOutcome {
            self.gas_inspector.call_end(context, inputs, outcome)
        }

        fn create(
            &mut self,
            context: &mut EvmContext<EvmWiringT>,
            call: &mut CreateInputs,
        ) -> Option<CreateOutcome> {
            self.gas_inspector.create(context, call);
            None
        }

        fn create_end(
            &mut self,
            context: &mut EvmContext<EvmWiringT>,
            inputs: &CreateInputs,
            outcome: CreateOutcome,
        ) -> CreateOutcome {
            self.gas_inspector.create_end(context, inputs, outcome)
        }
    }

    #[test]
    fn test_gas_inspector() {
        let contract_data: Bytes = Bytes::from(vec![
            opcode::PUSH1,
            0x1,
            opcode::PUSH1,
            0xb,
            opcode::JUMPI,
            opcode::PUSH1,
            0x1,
            opcode::PUSH1,
            0x1,
            opcode::PUSH1,
            0x1,
            opcode::JUMPDEST,
            opcode::STOP,
        ]);
        let bytecode = Bytecode::new_raw(contract_data);

        let mut evm = Evm::<EthereumWiring<BenchmarkDB, StackInspector>>::builder()
            .with_db(BenchmarkDB::new_bytecode(bytecode.clone()))
            .with_default_ext_ctx()
            .modify_tx_env(|tx| {
                *tx = <TestEvmWiring as PrimitiveEvmWiring>::Transaction::default();

                tx.caller = address!("1000000000000000000000000000000000000000");
                tx.transact_to = TxKind::Call(address!("0000000000000000000000000000000000000000"));
                tx.gas_limit = 21100;
            })
            .append_handler_register(inspector_handle_register)
            .build();

        // run evm.
        evm.transact().unwrap();

        let inspector = evm.into_context().external;

        // starting from 100gas
        let steps = vec![
            // push1 -3
            (0, 97),
            // push1 -3
            (2, 94),
            // jumpi -10
            (4, 84),
            // jumpdest 1
            (11, 83),
            // stop 0
            (12, 83),
        ];

        assert_eq!(inspector.gas_remaining_steps, steps);
    }
}