1use interpreter::{CallOutcome, CreateOutcome, Gas};
3
4#[allow(dead_code)]
6#[derive(Clone, Copy, Debug)]
7pub struct GasInspector {
8 gas_remaining: u64,
9 last_gas_cost: u64,
10}
11
12impl Default for GasInspector {
13 fn default() -> Self {
14 Self::new()
15 }
16}
17
18impl GasInspector {
19 #[inline]
21 pub fn gas_remaining(&self) -> u64 {
22 self.gas_remaining
23 }
24
25 #[inline]
27 pub fn last_gas_cost(&self) -> u64 {
28 self.last_gas_cost
29 }
30
31 pub fn new() -> Self {
33 Self {
34 gas_remaining: 0,
35 last_gas_cost: 0,
36 }
37 }
38
39 #[inline]
41 pub fn initialize_interp(&mut self, gas: &Gas) {
42 self.gas_remaining = gas.limit();
43 }
44
45 #[inline]
47 pub fn step(&mut self, gas: &Gas) {
48 self.gas_remaining = gas.remaining();
49 }
50
51 #[inline]
53 pub fn step_end(&mut self, gas: &Gas) {
54 let remaining = gas.remaining();
55 self.last_gas_cost = self.gas_remaining.saturating_sub(remaining);
56 self.gas_remaining = remaining;
57 }
58
59 #[inline]
61 pub fn call_end(&mut self, outcome: &mut CallOutcome) {
62 if outcome.result.result.is_error() {
63 outcome.result.gas.spend_all();
64 self.gas_remaining = 0;
65 }
66 }
67
68 #[inline]
70 pub fn create_end(&mut self, outcome: &mut CreateOutcome) {
71 if outcome.result.result.is_error() {
72 outcome.result.gas.spend_all();
73 self.gas_remaining = 0;
74 }
75 }
76}
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81 use crate::{InspectEvm, Inspector};
82 use context::{Context, TxEnv};
83 use database::{BenchmarkDB, BENCH_CALLER, BENCH_TARGET};
84 use handler::{MainBuilder, MainContext};
85 use interpreter::{
86 interpreter_types::{Jumps, ReturnData},
87 CallInputs, CreateInputs, Interpreter, InterpreterResult, InterpreterTypes,
88 };
89 use primitives::{Address, Bytes, TxKind};
90 use state::bytecode::{opcode, Bytecode};
91
92 #[derive(Default, Debug)]
93 struct StackInspector {
94 pc: usize,
95 opcode: u8,
96 gas_inspector: GasInspector,
97 gas_remaining_steps: Vec<(usize, u64)>,
98 }
99
100 impl<CTX, INTR: InterpreterTypes> Inspector<CTX, INTR> for StackInspector {
101 fn initialize_interp(&mut self, interp: &mut Interpreter<INTR>, _context: &mut CTX) {
102 self.gas_inspector.initialize_interp(&interp.gas);
103 }
104
105 fn step(&mut self, interp: &mut Interpreter<INTR>, _context: &mut CTX) {
106 self.pc = interp.bytecode.pc();
107 self.opcode = interp.bytecode.opcode();
108 self.gas_inspector.step(&interp.gas);
109 }
110
111 fn step_end(&mut self, interp: &mut Interpreter<INTR>, _context: &mut CTX) {
112 self.gas_inspector.step_end(&interp.gas);
113 self.gas_remaining_steps
114 .push((self.pc, self.gas_inspector.gas_remaining()));
115 }
116
117 fn call_end(&mut self, _c: &mut CTX, _i: &CallInputs, outcome: &mut CallOutcome) {
118 self.gas_inspector.call_end(outcome)
119 }
120
121 fn create_end(&mut self, _c: &mut CTX, _i: &CreateInputs, outcome: &mut CreateOutcome) {
122 self.gas_inspector.create_end(outcome)
123 }
124 }
125
126 #[test]
127 fn test_gas_inspector() {
128 let contract_data: Bytes = Bytes::from(vec![
129 opcode::PUSH1,
130 0x1,
131 opcode::PUSH1,
132 0xb,
133 opcode::JUMPI,
134 opcode::PUSH1,
135 0x1,
136 opcode::PUSH1,
137 0x1,
138 opcode::PUSH1,
139 0x1,
140 opcode::JUMPDEST,
141 opcode::STOP,
142 ]);
143 let bytecode = Bytecode::new_raw(contract_data);
144
145 let ctx = Context::mainnet().with_db(BenchmarkDB::new_bytecode(bytecode.clone()));
146
147 let mut evm = ctx.build_mainnet_with_inspector(StackInspector::default());
148
149 evm.inspect_one_tx(
151 TxEnv::builder()
152 .caller(BENCH_CALLER)
153 .kind(TxKind::Call(BENCH_TARGET))
154 .gas_limit(21100)
155 .build()
156 .unwrap(),
157 )
158 .unwrap();
159
160 let inspector = &evm.inspector;
161
162 let steps = vec![
164 (0, 97),
166 (2, 94),
168 (4, 84),
170 (11, 83),
172 (12, 83),
174 ];
175
176 assert_eq!(inspector.gas_remaining_steps, steps);
177 }
178
179 #[derive(Default, Debug)]
180 struct CallOverrideInspector {
181 call_override: Vec<Option<CallOutcome>>,
182 create_override: Vec<Option<CreateOutcome>>,
183 return_buffer: Vec<Bytes>,
184 }
185
186 impl<CTX, INTR: InterpreterTypes> Inspector<CTX, INTR> for CallOverrideInspector {
187 fn call(&mut self, _context: &mut CTX, _inputs: &mut CallInputs) -> Option<CallOutcome> {
188 self.call_override.pop().unwrap_or_default()
189 }
190
191 fn step(&mut self, interpreter: &mut Interpreter<INTR>, _context: &mut CTX) {
192 let this_buffer = interpreter.return_data.buffer();
193 let Some(buffer) = self.return_buffer.last() else {
194 self.return_buffer.push(this_buffer.clone());
195 return;
196 };
197 if this_buffer != buffer {
198 self.return_buffer.push(this_buffer.clone());
199 }
200 }
201
202 fn create(
203 &mut self,
204 _context: &mut CTX,
205 _inputs: &mut CreateInputs,
206 ) -> Option<CreateOutcome> {
207 self.create_override.pop().unwrap_or_default()
208 }
209 }
210
211 #[test]
212 fn test_call_override_inspector() {
213 use interpreter::{CallOutcome, CreateOutcome, InstructionResult};
214
215 let mut inspector = CallOverrideInspector::default();
216 inspector.call_override.push(Some(CallOutcome::new(
217 InterpreterResult::new(InstructionResult::Return, [0x01].into(), Gas::new(100_000)),
218 0..1,
219 )));
220 inspector.call_override.push(None);
221 inspector.create_override.push(Some(CreateOutcome::new(
222 InterpreterResult::new(InstructionResult::Revert, [0x02].into(), Gas::new(100_000)),
223 Some(Address::ZERO),
224 )));
225
226 let contract_data: Bytes = Bytes::from(vec![
227 opcode::PUSH1,
228 0x01,
229 opcode::PUSH1,
230 0x0,
231 opcode::DUP1,
232 opcode::DUP1,
233 opcode::DUP1,
234 opcode::DUP1,
235 opcode::ADDRESS,
236 opcode::CALL,
237 opcode::PUSH1,
238 0x01,
239 opcode::PUSH1,
240 0x0,
241 opcode::DUP1,
242 opcode::DUP1,
243 opcode::DUP1,
244 opcode::DUP1,
245 opcode::DUP1,
246 opcode::ADDRESS,
247 opcode::CREATE,
248 opcode::STOP,
249 ]);
250
251 let bytecode = Bytecode::new_raw(contract_data);
252
253 let mut evm = Context::mainnet()
254 .with_db(BenchmarkDB::new_bytecode(bytecode.clone()))
255 .build_mainnet_with_inspector(inspector);
256
257 let _ = evm
258 .inspect_one_tx(TxEnv::builder_for_bench().build().unwrap())
259 .unwrap();
260 assert_eq!(evm.inspector.return_buffer.len(), 3);
261 assert_eq!(
262 evm.inspector.return_buffer,
263 [Bytes::new(), Bytes::from([0x01]), Bytes::from([0x02])].to_vec()
264 );
265 }
266}