From 913f15dc84251be57a219203959fdcc9977162f0 Mon Sep 17 00:00:00 2001 From: "MouseLess.eth" Date: Fri, 2 Dec 2022 12:51:16 -0800 Subject: [PATCH] fix: remove `OpCode` enum and update `VMOperation`'s `op` field type (#1904) * remove OpCode enum and update VMOperation's op field * restore opcodes + update 0x20 to keccak256 * enum to deserialize opcodes into known or unknown * Update ethers-core/src/types/trace/mod.rs Co-authored-by: Georgios Konstantopoulos * fix typo: ExecutedInstruction Co-authored-by: Georgios Konstantopoulos --- ethers-core/src/types/trace/mod.rs | 36 ++++++++++++++++++++++++-- ethers-core/src/types/trace/opcodes.rs | 7 +++-- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/ethers-core/src/types/trace/mod.rs b/ethers-core/src/types/trace/mod.rs index ce1e7082..5f93bb5a 100644 --- a/ethers-core/src/types/trace/mod.rs +++ b/ethers-core/src/types/trace/mod.rs @@ -134,9 +134,9 @@ pub struct VMOperation { /// Subordinate trace of the CALL/CREATE if applicable. // #[serde(bound="VMTrace: Deserialize")] pub sub: Option, - /// The executed opcode name + /// The opcode of the executed instruction #[serde(rename = "op")] - pub op: OpCode, + pub op: ExecutedInstruction, } #[derive(Debug, Clone, PartialEq, Eq, Default, Deserialize, Serialize)] @@ -176,6 +176,23 @@ pub struct StorageDiff { pub val: U256, } +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(untagged)] +#[allow(clippy::upper_case_acronyms)] +/// Helper to classify the executed instruction +pub enum ExecutedInstruction { + /// The instruction is recognized + Known(Opcode), + /// The instruction is not recognized + Unknown(String), +} + +impl Default for ExecutedInstruction { + fn default() -> Self { + Self::Known(Opcode::INVALID) + } +} + #[cfg(test)] mod tests { use super::*; @@ -208,4 +225,19 @@ mod tests { fn test_deserialize_blocktraces() { let _traces: Vec = serde_json::from_str(EXAMPLE_TRACES).unwrap(); } + + #[test] + fn test_deserialize_unknown_opcode() { + let example_opcodes = r#"["GAS", "CREATE2", "CUSTOMOP"]"#; + let parsed_opcodes: Vec = + serde_json::from_str(example_opcodes).unwrap(); + assert_eq!( + vec![ + ExecutedInstruction::Known(Opcode::GAS), + ExecutedInstruction::Known(Opcode::CREATE2), + ExecutedInstruction::Unknown("CUSTOMOP".to_string()) + ], + parsed_opcodes + ) + } } diff --git a/ethers-core/src/types/trace/opcodes.rs b/ethers-core/src/types/trace/opcodes.rs index 0c90a632..347a3e7d 100644 --- a/ethers-core/src/types/trace/opcodes.rs +++ b/ethers-core/src/types/trace/opcodes.rs @@ -2,9 +2,9 @@ use serde::{Deserialize, Serialize}; // opcode descriptions taken from evm.codes https://github.com/comitylabs/evm.codes/blob/bc7f102808055d88365559d40c190c5bd6d164c3/opcodes.json // https://github.com/ethereum/go-ethereum/blob/2b1299b1c006077c56ecbad32e79fc16febe3dd6/core/vm/opcodes.go -#[derive(Debug, Clone, PartialEq, Eq, Default, Deserialize, Serialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] /// Name of executed EVM opcode -pub enum OpCode { +pub enum Opcode { // 0x0 range - arithmetic ops. /// Opcode 0x0 - Halts execution STOP, @@ -67,7 +67,7 @@ pub enum OpCode { // 0x20 range - crypto. /// Opcode 0x20 - Compute Keccak-256 hash - SHA3, + KECCAK256, // 0x21 - 0x2F are invalid @@ -335,7 +335,6 @@ pub enum OpCode { // 0xfd range - closures /// Opcode 0xFD - Halt execution reverting state changes but returning data and remaining gas REVERT, - #[default] /// Opcode 0xFE - Designated invalid instruction INVALID, /// Opcode 0xFF - Halt execution and register account for later deletion