|
| 1 | +use alloy::hex; |
| 2 | +use revm::{ |
| 3 | + interpreter::{ |
| 4 | + CallInputs, CallOutcome, CreateInputs, CreateOutcome, EOFCreateInputs, Interpreter, |
| 5 | + InterpreterTypes, |
| 6 | + }, |
| 7 | + Inspector, |
| 8 | +}; |
| 9 | +use tracing::{ |
| 10 | + debug_span, error_span, info_span, span::EnteredSpan, trace_span, warn_span, Level, Span, |
| 11 | +}; |
| 12 | + |
| 13 | +macro_rules! runtime_level_span { |
| 14 | + ($level:expr, $($args:tt)*) => {{ |
| 15 | + match $level { |
| 16 | + Level::TRACE => trace_span!($($args)*), |
| 17 | + Level::DEBUG => debug_span!($($args)*), |
| 18 | + Level::INFO => info_span!($($args)*), |
| 19 | + Level::WARN => warn_span!($($args)*), |
| 20 | + Level::ERROR => error_span!($($args)*), |
| 21 | + } |
| 22 | + }}; |
| 23 | +} |
| 24 | + |
| 25 | +/// Inspector that creates spans for each call and create operation. |
| 26 | +/// |
| 27 | +/// This inspector is useful for tracing the execution of the EVM and |
| 28 | +/// contextualizing information from other tracing inspectors. It uses |
| 29 | +/// [`tracing`] to create spans for each call frame, at a specfied [`Level`], |
| 30 | +/// and adds interpreter information to the span. |
| 31 | +/// |
| 32 | +/// Spans are created at the beginning of each call and create operation, |
| 33 | +/// and closed at the end of the operation. The spans are named |
| 34 | +/// according to the operation type (call or create) and include |
| 35 | +/// the call depth, gas remaining, and other relevant information. |
| 36 | +/// |
| 37 | +/// # Note on functionality |
| 38 | +/// |
| 39 | +/// We assume that the EVM execution is synchronous, so [`EnteredSpan`]s will |
| 40 | +/// not be held across await points. This means we can simply keep a |
| 41 | +/// `Vec<EnteredSpan>` to which push and from which we pop. The first span in |
| 42 | +/// the vec will always be our root span, and 1 span will be held for each |
| 43 | +/// active callframe. |
| 44 | +/// |
| 45 | +pub struct SpanningInspector { |
| 46 | + active: Vec<EnteredSpan>, |
| 47 | + level: Level, |
| 48 | +} |
| 49 | + |
| 50 | +impl core::fmt::Debug for SpanningInspector { |
| 51 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 52 | + f.debug_struct("SpanningInspector") |
| 53 | + .field("active", &self.active.len()) |
| 54 | + .field("level", &self.level) |
| 55 | + .finish() |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl SpanningInspector { |
| 60 | + /// Create a new `SpanningInspector` with the given tracing level. |
| 61 | + /// Spans will be created at this level. |
| 62 | + pub const fn new(level: Level) -> Self { |
| 63 | + Self { active: Vec::new(), level } |
| 64 | + } |
| 65 | + |
| 66 | + /// Create a new `SpanningInspector` producing spans at [`Level::TRACE`]. |
| 67 | + pub const fn at_trace() -> Self { |
| 68 | + Self::new(Level::TRACE) |
| 69 | + } |
| 70 | + |
| 71 | + /// Create a new `SpanningInspector` producing spans at [`Level::DEBUG`]. |
| 72 | + pub const fn at_debug() -> Self { |
| 73 | + Self::new(Level::DEBUG) |
| 74 | + } |
| 75 | + |
| 76 | + /// Create a new `SpanningInspector` producing spans at [`Level::WARN`]. |
| 77 | + pub const fn at_info() -> Self { |
| 78 | + Self::new(Level::INFO) |
| 79 | + } |
| 80 | + |
| 81 | + /// Create a root span |
| 82 | + fn root_span<Ctx, Int>(&mut self, _interp: &mut Interpreter<Int>, _context: &mut Ctx) -> Span |
| 83 | + where |
| 84 | + Int: InterpreterTypes, |
| 85 | + { |
| 86 | + runtime_level_span!( |
| 87 | + self.level, |
| 88 | + parent: None, // this is the root span :) |
| 89 | + "evm_execution", |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + /// Init the inspector by setting the root span. This should be called only in [`Inspector::initialize_interp`]. |
| 94 | + fn init<Ctx, Int>(&mut self, interp: &mut Interpreter<Int>, context: &mut Ctx) |
| 95 | + where |
| 96 | + Int: InterpreterTypes, |
| 97 | + { |
| 98 | + // just in case |
| 99 | + self.active.clear(); |
| 100 | + let span = self.root_span(interp, context).entered(); |
| 101 | + self.active.push(span); |
| 102 | + } |
| 103 | + |
| 104 | + /// Exit the active span. |
| 105 | + fn exit_span(&mut self) { |
| 106 | + self.active.pop(); |
| 107 | + // If there's only 1 left, it's the root span for the trace, and all |
| 108 | + // callframes are closed. We are now done with execution. |
| 109 | + if self.active.len() == 1 { |
| 110 | + self.active.pop(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /// Create a span for a `CALL`-family opcode. |
| 115 | + fn span_call<Ctx>(&self, _context: &Ctx, inputs: &CallInputs) -> Span { |
| 116 | + let mut selector = inputs.input.clone(); |
| 117 | + selector.truncate(4); |
| 118 | + runtime_level_span!( |
| 119 | + self.level, |
| 120 | + "call", |
| 121 | + input_len = inputs.input.len(), |
| 122 | + selector = hex::encode(&selector), |
| 123 | + gas_limit = inputs.gas_limit, |
| 124 | + bytecode_address = %inputs.bytecode_address, |
| 125 | + target_addrses = %inputs.target_address, |
| 126 | + caller = %inputs.caller, |
| 127 | + value = %inputs.value.get(), |
| 128 | + scheme = ?inputs.scheme, |
| 129 | + ) |
| 130 | + } |
| 131 | + |
| 132 | + /// Create, enter, and store a span for a `CALL`-family opcode. |
| 133 | + fn enter_call<Ctx>(&mut self, context: &Ctx, inputs: &CallInputs) { |
| 134 | + self.active.push(self.span_call(context, inputs).entered()) |
| 135 | + } |
| 136 | + |
| 137 | + /// Create a span for a `CREATE`-family opcode. |
| 138 | + fn span_create<Ctx>(&self, _context: &Ctx, inputs: &CreateInputs) -> Span { |
| 139 | + runtime_level_span!( |
| 140 | + self.level, |
| 141 | + "create", |
| 142 | + caller = %inputs.caller, |
| 143 | + value = %inputs.value, |
| 144 | + gas_limit = inputs.gas_limit, |
| 145 | + scheme = ?inputs.scheme, |
| 146 | + ) |
| 147 | + } |
| 148 | + |
| 149 | + /// Create, enter, and store a span for a `CREATE`-family opcode. |
| 150 | + fn enter_create<Ctx>(&mut self, context: &Ctx, inputs: &CreateInputs) { |
| 151 | + self.active.push(self.span_create(context, inputs).entered()) |
| 152 | + } |
| 153 | + |
| 154 | + /// Create a span for an EOF `CREATE`-family opcode. |
| 155 | + fn span_eof_create<Ctx>(&self, _context: &Ctx, inputs: &EOFCreateInputs) -> Span { |
| 156 | + runtime_level_span!( |
| 157 | + self.level, |
| 158 | + "eof_create", |
| 159 | + caller = %inputs.caller, |
| 160 | + value = %inputs.value, |
| 161 | + gas_limit = inputs.gas_limit, |
| 162 | + kind = ?inputs.kind, |
| 163 | + ) |
| 164 | + } |
| 165 | + |
| 166 | + /// Create, enter, and store a span for an EOF `CREATE`-family opcode. |
| 167 | + fn enter_eof_create<Ctx>(&mut self, context: &Ctx, inputs: &EOFCreateInputs) { |
| 168 | + self.active.push(self.span_eof_create(context, inputs).entered()) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +impl<Ctx, Int> Inspector<Ctx, Int> for SpanningInspector |
| 173 | +where |
| 174 | + Int: InterpreterTypes, |
| 175 | +{ |
| 176 | + fn initialize_interp(&mut self, interp: &mut Interpreter<Int>, context: &mut Ctx) { |
| 177 | + self.init(interp, context); |
| 178 | + } |
| 179 | + |
| 180 | + fn call(&mut self, context: &mut Ctx, inputs: &mut CallInputs) -> Option<CallOutcome> { |
| 181 | + self.enter_call(context, inputs); |
| 182 | + None |
| 183 | + } |
| 184 | + |
| 185 | + fn call_end(&mut self, _context: &mut Ctx, _inputs: &CallInputs, _outcome: &mut CallOutcome) { |
| 186 | + self.exit_span(); |
| 187 | + } |
| 188 | + |
| 189 | + fn create(&mut self, context: &mut Ctx, inputs: &mut CreateInputs) -> Option<CreateOutcome> { |
| 190 | + self.enter_create(context, inputs); |
| 191 | + None |
| 192 | + } |
| 193 | + |
| 194 | + fn create_end( |
| 195 | + &mut self, |
| 196 | + _context: &mut Ctx, |
| 197 | + _inputs: &CreateInputs, |
| 198 | + _outcome: &mut CreateOutcome, |
| 199 | + ) { |
| 200 | + self.exit_span(); |
| 201 | + } |
| 202 | + |
| 203 | + fn eofcreate( |
| 204 | + &mut self, |
| 205 | + context: &mut Ctx, |
| 206 | + inputs: &mut EOFCreateInputs, |
| 207 | + ) -> Option<CreateOutcome> { |
| 208 | + self.enter_eof_create(context, inputs); |
| 209 | + None |
| 210 | + } |
| 211 | + |
| 212 | + fn eofcreate_end( |
| 213 | + &mut self, |
| 214 | + _context: &mut Ctx, |
| 215 | + _inputs: &EOFCreateInputs, |
| 216 | + _outcome: &mut CreateOutcome, |
| 217 | + ) { |
| 218 | + self.exit_span(); |
| 219 | + } |
| 220 | +} |
0 commit comments