Skip to content

Commit e47ffbe

Browse files
Imporve opcode type usage by reusing evm::Opcode
1 parent a08006e commit e47ffbe

File tree

7 files changed

+20
-199
lines changed

7 files changed

+20
-199
lines changed

Cargo.lock

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/evm-tracing-client/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ publish = false
88
evm-tracing-events = { path = "../evm-tracing-events" }
99

1010
codec = { workspace = true }
11+
evm = { workspace = true, features = ["with-codec", "with-serde"] }
1112
frame-support = { workspace = true, features = ["std"] }
1213
hex = { workspace = true, features = ["serde"] }
1314
serde = { workspace = true, features = ["derive"] }

crates/evm-tracing-client/src/listeners/call_list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ mod tests {
757757
match event_type {
758758
TestRuntimeEvent::Step => RuntimeEvent::Step {
759759
context: test_context(),
760-
opcode: Vec::new(),
760+
opcode: evm::Opcode::STOP,
761761
position: Ok(0u64),
762762
stack: test_stack(),
763763
memory: test_memory(),

crates/evm-tracing-client/src/listeners/raw.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct Context {
4848
#[derive(Debug)]
4949
struct Step {
5050
/// Current opcode.
51-
opcode: Vec<u8>,
51+
opcode: evm::Opcode,
5252
/// Depth of the context.
5353
depth: usize,
5454
/// Remaining gas.
@@ -289,7 +289,7 @@ impl Listener {
289289
}
290290
}
291291
}
292-
Err(Capture::Trap(opcode)) if ContextType::from(opcode.clone()).is_some() => {
292+
Err(Capture::Trap(opcode)) if ContextType::from(opcode).is_some() => {
293293
self.new_context = true;
294294
}
295295
_ => (),

crates/evm-tracing-client/src/types/mod.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,13 @@ pub enum ContextType {
7777

7878
impl ContextType {
7979
/// Obtain context type from opcode.
80-
pub fn from(opcode: Vec<u8>) -> Option<Self> {
81-
let opcode = match alloc::str::from_utf8(&opcode[..]) {
82-
Ok(op) => op.to_uppercase(),
83-
_ => return None,
84-
};
85-
match &opcode[..] {
86-
"CREATE" | "CREATE2" => Some(ContextType::Create),
87-
"CALL" => Some(ContextType::Call(CallType::Call)),
88-
"CALLCODE" => Some(ContextType::Call(CallType::CallCode)),
89-
"DELEGATECALL" => Some(ContextType::Call(CallType::DelegateCall)),
90-
"STATICCALL" => Some(ContextType::Call(CallType::StaticCall)),
80+
pub fn from(opcode: evm::Opcode) -> Option<Self> {
81+
match opcode {
82+
evm::Opcode::CREATE | evm::Opcode::CREATE2 => Some(ContextType::Create),
83+
evm::Opcode::CALL => Some(ContextType::Call(CallType::Call)),
84+
evm::Opcode::CALLCODE => Some(ContextType::Call(CallType::CallCode)),
85+
evm::Opcode::DELEGATECALL => Some(ContextType::Call(CallType::DelegateCall)),
86+
evm::Opcode::STATICCALL => Some(ContextType::Call(CallType::StaticCall)),
9187
_ => None,
9288
}
9389
}

crates/evm-tracing-client/src/types/single.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ pub struct RawStepLog {
7676
)]
7777
pub memory: Option<Vec<H256>>,
7878
/// Op.
79-
#[serde(serialize_with = "opcode_serialize")]
80-
pub op: Vec<u8>,
79+
pub op: evm::Opcode,
8180
/// Pc.
8281
#[serde(serialize_with = "u256_serialize")]
8382
pub pc: U256,

crates/evm-tracing-events/src/runtime.rs

Lines changed: 4 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
extern crate alloc;
44

55
use codec::{Decode, Encode};
6-
#[cfg(feature = "evm-tracing")]
7-
use evm::Opcode;
86
pub use evm::{ExitError, ExitReason, ExitSucceed};
97
use sp_core::{sp_std::vec::Vec, H160, H256, U256};
108

@@ -62,7 +60,7 @@ pub enum Capture<E, T> {
6260
}
6361

6462
/// A type alias representing trap data. Should hold the marshalled `Opcode`.
65-
pub type Trap = Vec<u8>;
63+
pub type Trap = evm::Opcode;
6664

6765
/// EVM runtime event.
6866
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
@@ -72,7 +70,7 @@ pub enum RuntimeEvent {
7270
/// Context.
7371
context: Context,
7472
/// Opcode.
75-
opcode: Vec<u8>,
73+
opcode: evm::Opcode,
7674
/// Position.
7775
position: Result<u64, ExitReason>,
7876
/// Stack.
@@ -121,7 +119,7 @@ impl RuntimeEvent {
121119
memory,
122120
} => Self::Step {
123121
context: context.clone().into(),
124-
opcode: opcodes_string(opcode),
122+
opcode,
125123
position: match position {
126124
Ok(position) => Ok(*position as u64),
127125
Err(e) => Err(e.clone()),
@@ -145,7 +143,7 @@ impl RuntimeEvent {
145143
Ok(_) => Ok(()),
146144
Err(capture) => match capture {
147145
evm::Capture::Exit(e) => Err(Capture::Exit(e.clone())),
148-
evm::Capture::Trap(t) => Err(Capture::Trap(opcodes_string(*t))),
146+
evm::Capture::Trap(t) => Err(Capture::Trap(*t)),
149147
},
150148
},
151149
return_value: return_value.to_vec(),
@@ -171,177 +169,3 @@ impl RuntimeEvent {
171169
}
172170
}
173171
}
174-
175-
/// Converts an `Opcode` into its name, stored in a `Vec<u8>`.
176-
#[cfg(feature = "evm-tracing")]
177-
pub fn opcodes_string(opcode: Opcode) -> Vec<u8> {
178-
match opcode_str(opcode) {
179-
Some(s) => s.into(),
180-
None => alloc::format!("Unknown({})", opcode.0).into(),
181-
}
182-
}
183-
184-
/// Converts an `Opcode` into its name.
185-
#[cfg(feature = "evm-tracing")]
186-
pub fn opcode_str(opcode: Opcode) -> Option<&'static str> {
187-
Some(match opcode.0 {
188-
0 => "Stop",
189-
1 => "Add",
190-
2 => "Mul",
191-
3 => "Sub",
192-
4 => "Div",
193-
5 => "SDiv",
194-
6 => "Mod",
195-
7 => "SMod",
196-
8 => "AddMod",
197-
9 => "MulMod",
198-
10 => "Exp",
199-
11 => "SignExtend",
200-
16 => "Lt",
201-
17 => "Gt",
202-
18 => "Slt",
203-
19 => "Sgt",
204-
20 => "Eq",
205-
21 => "IsZero",
206-
22 => "And",
207-
23 => "Or",
208-
24 => "Xor",
209-
25 => "Not",
210-
26 => "Byte",
211-
27 => "Shl",
212-
28 => "Shr",
213-
29 => "Sar",
214-
32 => "Keccak256",
215-
48 => "Address",
216-
49 => "Balance",
217-
50 => "Origin",
218-
51 => "Caller",
219-
52 => "CallValue",
220-
53 => "CallDataLoad",
221-
54 => "CallDataSize",
222-
55 => "CallDataCopy",
223-
56 => "CodeSize",
224-
57 => "CodeCopy",
225-
58 => "GasPrice",
226-
59 => "ExtCodeSize",
227-
60 => "ExtCodeCopy",
228-
61 => "ReturnDataSize",
229-
62 => "ReturnDataCopy",
230-
63 => "ExtCodeHash",
231-
64 => "BlockHash",
232-
65 => "Coinbase",
233-
66 => "Timestamp",
234-
67 => "Number",
235-
68 => "Difficulty",
236-
69 => "GasLimit",
237-
70 => "ChainId",
238-
80 => "Pop",
239-
81 => "MLoad",
240-
82 => "MStore",
241-
83 => "MStore8",
242-
84 => "SLoad",
243-
85 => "SStore",
244-
86 => "Jump",
245-
87 => "JumpI",
246-
88 => "GetPc",
247-
89 => "MSize",
248-
90 => "Gas",
249-
91 => "JumpDest",
250-
92 => "TLoad",
251-
93 => "TStore",
252-
94 => "MCopy",
253-
96 => "Push1",
254-
97 => "Push2",
255-
98 => "Push3",
256-
99 => "Push4",
257-
100 => "Push5",
258-
101 => "Push6",
259-
102 => "Push7",
260-
103 => "Push8",
261-
104 => "Push9",
262-
105 => "Push10",
263-
106 => "Push11",
264-
107 => "Push12",
265-
108 => "Push13",
266-
109 => "Push14",
267-
110 => "Push15",
268-
111 => "Push16",
269-
112 => "Push17",
270-
113 => "Push18",
271-
114 => "Push19",
272-
115 => "Push20",
273-
116 => "Push21",
274-
117 => "Push22",
275-
118 => "Push23",
276-
119 => "Push24",
277-
120 => "Push25",
278-
121 => "Push26",
279-
122 => "Push27",
280-
123 => "Push28",
281-
124 => "Push29",
282-
125 => "Push30",
283-
126 => "Push31",
284-
127 => "Push32",
285-
128 => "Dup1",
286-
129 => "Dup2",
287-
130 => "Dup3",
288-
131 => "Dup4",
289-
132 => "Dup5",
290-
133 => "Dup6",
291-
134 => "Dup7",
292-
135 => "Dup8",
293-
136 => "Dup9",
294-
137 => "Dup10",
295-
138 => "Dup11",
296-
139 => "Dup12",
297-
140 => "Dup13",
298-
141 => "Dup14",
299-
142 => "Dup15",
300-
143 => "Dup16",
301-
144 => "Swap1",
302-
145 => "Swap2",
303-
146 => "Swap3",
304-
147 => "Swap4",
305-
148 => "Swap5",
306-
149 => "Swap6",
307-
150 => "Swap7",
308-
151 => "Swap8",
309-
152 => "Swap9",
310-
153 => "Swap10",
311-
154 => "Swap11",
312-
155 => "Swap12",
313-
156 => "Swap13",
314-
157 => "Swap14",
315-
158 => "Swap15",
316-
159 => "Swap16",
317-
160 => "Log0",
318-
161 => "Log1",
319-
162 => "Log2",
320-
163 => "Log3",
321-
164 => "Log4",
322-
176 => "JumpTo",
323-
177 => "JumpIf",
324-
178 => "JumpSub",
325-
180 => "JumpSubv",
326-
181 => "BeginSub",
327-
182 => "BeginData",
328-
184 => "ReturnSub",
329-
185 => "PutLocal",
330-
186 => "GetLocal",
331-
225 => "SLoadBytes",
332-
226 => "SStoreBytes",
333-
227 => "SSize",
334-
240 => "Create",
335-
241 => "Call",
336-
242 => "CallCode",
337-
243 => "Return",
338-
244 => "DelegateCall",
339-
245 => "Create2",
340-
250 => "StaticCall",
341-
252 => "TxExecGas",
342-
253 => "Revert",
343-
254 => "Invalid",
344-
255 => "SelfDestruct",
345-
_ => return None,
346-
})
347-
}

0 commit comments

Comments
 (0)