From 0e044532e6f791096e953a302ea070b0c6cbab64 Mon Sep 17 00:00:00 2001 From: mio Date: Wed, 12 Jul 2023 15:53:55 +0800 Subject: [PATCH 1/4] More rusty and support shanghai fork --- src/types.rs | 588 ++++++++++++++++++++++++++------------------------- 1 file changed, 301 insertions(+), 287 deletions(-) diff --git a/src/types.rs b/src/types.rs index 5302670..3798848 100644 --- a/src/types.rs +++ b/src/types.rs @@ -8,298 +8,312 @@ use std::fmt; #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[allow(missing_docs)] pub enum Opcode { - STOP, - ADD, - MUL, - SUB, - DIV, - SDIV, - MOD, - SMOD, - ADDMOD, - MULMOD, - EXP, - SIGNEXTEND, - LT, - GT, - SLT, - SGT, - EQ, - ISZERO, - AND, - OR, - XOR, - NOT, - BYTE, - SHL, - SHR, - SAR, - SHA3, - ADDRESS, - BALANCE, - ORIGIN, - CALLER, - CALLVALUE, - CALLDATALOAD, - CALLDATASIZE, - CALLDATACOPY, - CODESIZE, - CODECOPY, - GASPRICE, - EXTCODESIZE, - EXTCODECOPY, - RETURNDATASIZE, - RETURNDATACOPY, - EXTCODEHASH, - BLOCKHASH, - COINBASE, - TIMESTAMP, - NUMBER, - DIFFICULTY, - GASLIMIT, - CHAINID, - SELFBALANCE, - BASEFEE, - POP, - MLOAD, - MSTORE, - MSTORE8, - SLOAD, - SSTORE, - JUMP, - JUMPI, - PC, - MSIZE, - GAS, - JUMPDEST, - PUSH1, - PUSH2, - PUSH3, - PUSH4, - PUSH5, - PUSH6, - PUSH7, - PUSH8, - PUSH9, - PUSH10, - PUSH11, - PUSH12, - PUSH13, - PUSH14, - PUSH15, - PUSH16, - PUSH17, - PUSH18, - PUSH19, - PUSH20, - PUSH21, - PUSH22, - PUSH23, - PUSH24, - PUSH25, - PUSH26, - PUSH27, - PUSH28, - PUSH29, - PUSH30, - PUSH31, - PUSH32, - DUP1, - DUP2, - DUP3, - DUP4, - DUP5, - DUP6, - DUP7, - DUP8, - DUP9, - DUP10, - DUP11, - DUP12, - DUP13, - DUP14, - DUP15, - DUP16, - SWAP1, - SWAP2, - SWAP3, - SWAP4, - SWAP5, - SWAP6, - SWAP7, - SWAP8, - SWAP9, - SWAP10, - SWAP11, - SWAP12, - SWAP13, - SWAP14, - SWAP15, - SWAP16, - LOG0, - LOG1, - LOG2, - LOG3, - LOG4, - CREATE, - CALL, - CALLCODE, - RETURN, - DELEGATECALL, - CREATE2, - STATICCALL, - REVERT, - INVALID, - SELFDESTRUCT, + STOP = 0, + ADD = 1, + MUL = 2, + SUB = 3, + DIV = 4, + SDIV = 5, + MOD = 6, + SMOD = 7, + ADDMOD = 8, + MULMOD = 9, + EXP = 10, + SIGNEXTEND = 11, + LT = 16, + GT = 17, + SLT = 18, + SGT = 19, + EQ = 20, + ISZERO = 21, + AND = 22, + OR = 23, + XOR = 24, + NOT = 25, + BYTE = 26, + SHL = 27, + SHR = 28, + SAR = 29, + SHA3 = 32, + ADDRESS = 48, + BALANCE = 49, + ORIGIN = 50, + CALLER = 51, + CALLVALUE = 52, + CALLDATALOAD = 53, + CALLDATASIZE = 54, + CALLDATACOPY = 55, + CODESIZE = 56, + CODECOPY = 57, + GASPRICE = 58, + EXTCODESIZE = 59, + EXTCODECOPY = 60, + RETURNDATASIZE = 61, + RETURNDATACOPY = 62, + EXTCODEHASH = 63, + BLOCKHASH = 64, + COINBASE = 65, + TIMESTAMP = 66, + NUMBER = 67, + DIFFICULTY = 68, + GASLIMIT = 69, + CHAINID = 70, + SELFBALANCE = 71, + BASEFEE = 72, + POP = 80, + MLOAD = 81, + MSTORE = 82, + MSTORE8 = 83, + SLOAD = 84, + SSTORE = 85, + JUMP = 86, + JUMPI = 87, + PC = 88, + MSIZE = 89, + GAS = 90, + JUMPDEST = 91, + PUSH0 = 95, + PUSH1 = 96, + PUSH2 = 97, + PUSH3 = 98, + PUSH4 = 99, + PUSH5 = 100, + PUSH6 = 101, + PUSH7 = 102, + PUSH8 = 103, + PUSH9 = 104, + PUSH10 = 105, + PUSH11 = 106, + PUSH12 = 107, + PUSH13 = 108, + PUSH14 = 109, + PUSH15 = 110, + PUSH16 = 111, + PUSH17 = 112, + PUSH18 = 113, + PUSH19 = 114, + PUSH20 = 115, + PUSH21 = 116, + PUSH22 = 117, + PUSH23 = 118, + PUSH24 = 119, + PUSH25 = 120, + PUSH26 = 121, + PUSH27 = 122, + PUSH28 = 123, + PUSH29 = 124, + PUSH30 = 125, + PUSH31 = 126, + PUSH32 = 127, + DUP1 = 128, + DUP2 = 129, + DUP3 = 130, + DUP4 = 131, + DUP5 = 132, + DUP6 = 133, + DUP7 = 134, + DUP8 = 135, + DUP9 = 136, + DUP10 = 137, + DUP11 = 138, + DUP12 = 139, + DUP13 = 140, + DUP14 = 141, + DUP15 = 142, + DUP16 = 143, + SWAP1 = 144, + SWAP2 = 145, + SWAP3 = 146, + SWAP4 = 147, + SWAP5 = 148, + SWAP6 = 149, + SWAP7 = 150, + SWAP8 = 151, + SWAP9 = 152, + SWAP10 = 153, + SWAP11 = 154, + SWAP12 = 155, + SWAP13 = 156, + SWAP14 = 157, + SWAP15 = 158, + SWAP16 = 159, + LOG0 = 160, + LOG1 = 161, + LOG2 = 162, + LOG3 = 163, + LOG4 = 164, + CREATE = 240, + CALL = 241, + CALLCODE = 242, + RETURN = 243, + DELEGATECALL = 244, + CREATE2 = 245, + STATICCALL = 250, + REVERT = 253, + INVALID = 254, + SELFDESTRUCT = 255, +} + +impl TryFrom for Opcode { + type Error = (); + + fn try_from(x: u8) -> Result { + match x { + x if x == Opcode::STOP as u8 => Ok(Opcode::STOP), + x if x == Opcode::ADD as u8 => Ok(Opcode::ADD), + x if x == Opcode::MUL as u8 => Ok(Opcode::MUL), + x if x == Opcode::SUB as u8 => Ok(Opcode::SUB), + x if x == Opcode::DIV as u8 => Ok(Opcode::DIV), + x if x == Opcode::SDIV as u8 => Ok(Opcode::SDIV), + x if x == Opcode::MOD as u8 => Ok(Opcode::MOD), + x if x == Opcode::SMOD as u8 => Ok(Opcode::SMOD), + x if x == Opcode::ADDMOD as u8 => Ok(Opcode::ADDMOD), + x if x == Opcode::MULMOD as u8 => Ok(Opcode::MULMOD), + x if x == Opcode::EXP as u8 => Ok(Opcode::EXP), + x if x == Opcode::SIGNEXTEND as u8 => Ok(Opcode::SIGNEXTEND), + x if x == Opcode::LT as u8 => Ok(Opcode::LT), + x if x == Opcode::GT as u8 => Ok(Opcode::GT), + x if x == Opcode::SLT as u8 => Ok(Opcode::SLT), + x if x == Opcode::SGT as u8 => Ok(Opcode::SGT), + x if x == Opcode::EQ as u8 => Ok(Opcode::EQ), + x if x == Opcode::ISZERO as u8 => Ok(Opcode::ISZERO), + x if x == Opcode::AND as u8 => Ok(Opcode::AND), + x if x == Opcode::OR as u8 => Ok(Opcode::OR), + x if x == Opcode::XOR as u8 => Ok(Opcode::XOR), + x if x == Opcode::NOT as u8 => Ok(Opcode::NOT), + x if x == Opcode::BYTE as u8 => Ok(Opcode::BYTE), + x if x == Opcode::SHL as u8 => Ok(Opcode::SHL), + x if x == Opcode::SHR as u8 => Ok(Opcode::SHR), + x if x == Opcode::SAR as u8 => Ok(Opcode::SAR), + x if x == Opcode::SHA3 as u8 => Ok(Opcode::SHA3), + x if x == Opcode::ADDRESS as u8 => Ok(Opcode::ADDRESS), + x if x == Opcode::BALANCE as u8 => Ok(Opcode::BALANCE), + x if x == Opcode::ORIGIN as u8 => Ok(Opcode::ORIGIN), + x if x == Opcode::CALLER as u8 => Ok(Opcode::CALLER), + x if x == Opcode::CALLVALUE as u8 => Ok(Opcode::CALLVALUE), + x if x == Opcode::CALLDATALOAD as u8 => Ok(Opcode::CALLDATALOAD), + x if x == Opcode::CALLDATASIZE as u8 => Ok(Opcode::CALLDATASIZE), + x if x == Opcode::CALLDATACOPY as u8 => Ok(Opcode::CALLDATACOPY), + x if x == Opcode::CODESIZE as u8 => Ok(Opcode::CODESIZE), + x if x == Opcode::CODECOPY as u8 => Ok(Opcode::CODECOPY), + x if x == Opcode::GASPRICE as u8 => Ok(Opcode::GASPRICE), + x if x == Opcode::EXTCODESIZE as u8 => Ok(Opcode::EXTCODESIZE), + x if x == Opcode::EXTCODECOPY as u8 => Ok(Opcode::EXTCODECOPY), + x if x == Opcode::RETURNDATASIZE as u8 => Ok(Opcode::RETURNDATASIZE), + x if x == Opcode::RETURNDATACOPY as u8 => Ok(Opcode::RETURNDATACOPY), + x if x == Opcode::EXTCODEHASH as u8 => Ok(Opcode::EXTCODEHASH), + x if x == Opcode::BLOCKHASH as u8 => Ok(Opcode::BLOCKHASH), + x if x == Opcode::COINBASE as u8 => Ok(Opcode::COINBASE), + x if x == Opcode::TIMESTAMP as u8 => Ok(Opcode::TIMESTAMP), + x if x == Opcode::NUMBER as u8 => Ok(Opcode::NUMBER), + x if x == Opcode::DIFFICULTY as u8 => Ok(Opcode::DIFFICULTY), + x if x == Opcode::GASLIMIT as u8 => Ok(Opcode::GASLIMIT), + x if x == Opcode::CHAINID as u8 => Ok(Opcode::CHAINID), + x if x == Opcode::SELFBALANCE as u8 => Ok(Opcode::SELFBALANCE), + x if x == Opcode::BASEFEE as u8 => Ok(Opcode::BASEFEE), + x if x == Opcode::POP as u8 => Ok(Opcode::POP), + x if x == Opcode::MLOAD as u8 => Ok(Opcode::MLOAD), + x if x == Opcode::MSTORE as u8 => Ok(Opcode::MSTORE), + x if x == Opcode::MSTORE8 as u8 => Ok(Opcode::MSTORE8), + x if x == Opcode::SLOAD as u8 => Ok(Opcode::SLOAD), + x if x == Opcode::SSTORE as u8 => Ok(Opcode::SSTORE), + x if x == Opcode::JUMP as u8 => Ok(Opcode::JUMP), + x if x == Opcode::JUMPI as u8 => Ok(Opcode::JUMPI), + x if x == Opcode::PC as u8 => Ok(Opcode::PC), + x if x == Opcode::MSIZE as u8 => Ok(Opcode::MSIZE), + x if x == Opcode::GAS as u8 => Ok(Opcode::GAS), + x if x == Opcode::JUMPDEST as u8 => Ok(Opcode::JUMPDEST), + x if x == Opcode::PUSH0 as u8 => Ok(Opcode::PUSH0), + x if x == Opcode::PUSH1 as u8 => Ok(Opcode::PUSH1), + x if x == Opcode::PUSH2 as u8 => Ok(Opcode::PUSH2), + x if x == Opcode::PUSH3 as u8 => Ok(Opcode::PUSH3), + x if x == Opcode::PUSH4 as u8 => Ok(Opcode::PUSH4), + x if x == Opcode::PUSH5 as u8 => Ok(Opcode::PUSH5), + x if x == Opcode::PUSH6 as u8 => Ok(Opcode::PUSH6), + x if x == Opcode::PUSH7 as u8 => Ok(Opcode::PUSH7), + x if x == Opcode::PUSH8 as u8 => Ok(Opcode::PUSH8), + x if x == Opcode::PUSH9 as u8 => Ok(Opcode::PUSH9), + x if x == Opcode::PUSH10 as u8 => Ok(Opcode::PUSH10), + x if x == Opcode::PUSH11 as u8 => Ok(Opcode::PUSH11), + x if x == Opcode::PUSH12 as u8 => Ok(Opcode::PUSH12), + x if x == Opcode::PUSH13 as u8 => Ok(Opcode::PUSH13), + x if x == Opcode::PUSH14 as u8 => Ok(Opcode::PUSH14), + x if x == Opcode::PUSH15 as u8 => Ok(Opcode::PUSH15), + x if x == Opcode::PUSH16 as u8 => Ok(Opcode::PUSH16), + x if x == Opcode::PUSH17 as u8 => Ok(Opcode::PUSH17), + x if x == Opcode::PUSH18 as u8 => Ok(Opcode::PUSH18), + x if x == Opcode::PUSH19 as u8 => Ok(Opcode::PUSH19), + x if x == Opcode::PUSH20 as u8 => Ok(Opcode::PUSH20), + x if x == Opcode::PUSH21 as u8 => Ok(Opcode::PUSH21), + x if x == Opcode::PUSH22 as u8 => Ok(Opcode::PUSH22), + x if x == Opcode::PUSH23 as u8 => Ok(Opcode::PUSH23), + x if x == Opcode::PUSH24 as u8 => Ok(Opcode::PUSH24), + x if x == Opcode::PUSH25 as u8 => Ok(Opcode::PUSH25), + x if x == Opcode::PUSH26 as u8 => Ok(Opcode::PUSH26), + x if x == Opcode::PUSH27 as u8 => Ok(Opcode::PUSH27), + x if x == Opcode::PUSH28 as u8 => Ok(Opcode::PUSH28), + x if x == Opcode::PUSH29 as u8 => Ok(Opcode::PUSH29), + x if x == Opcode::PUSH30 as u8 => Ok(Opcode::PUSH30), + x if x == Opcode::PUSH31 as u8 => Ok(Opcode::PUSH31), + x if x == Opcode::PUSH32 as u8 => Ok(Opcode::PUSH32), + x if x == Opcode::DUP1 as u8 => Ok(Opcode::DUP1), + x if x == Opcode::DUP2 as u8 => Ok(Opcode::DUP2), + x if x == Opcode::DUP3 as u8 => Ok(Opcode::DUP3), + x if x == Opcode::DUP4 as u8 => Ok(Opcode::DUP4), + x if x == Opcode::DUP5 as u8 => Ok(Opcode::DUP5), + x if x == Opcode::DUP6 as u8 => Ok(Opcode::DUP6), + x if x == Opcode::DUP7 as u8 => Ok(Opcode::DUP7), + x if x == Opcode::DUP8 as u8 => Ok(Opcode::DUP8), + x if x == Opcode::DUP9 as u8 => Ok(Opcode::DUP9), + x if x == Opcode::DUP10 as u8 => Ok(Opcode::DUP10), + x if x == Opcode::DUP11 as u8 => Ok(Opcode::DUP11), + x if x == Opcode::DUP12 as u8 => Ok(Opcode::DUP12), + x if x == Opcode::DUP13 as u8 => Ok(Opcode::DUP13), + x if x == Opcode::DUP14 as u8 => Ok(Opcode::DUP14), + x if x == Opcode::DUP15 as u8 => Ok(Opcode::DUP15), + x if x == Opcode::DUP16 as u8 => Ok(Opcode::DUP16), + x if x == Opcode::SWAP1 as u8 => Ok(Opcode::SWAP1), + x if x == Opcode::SWAP2 as u8 => Ok(Opcode::SWAP2), + x if x == Opcode::SWAP3 as u8 => Ok(Opcode::SWAP3), + x if x == Opcode::SWAP4 as u8 => Ok(Opcode::SWAP4), + x if x == Opcode::SWAP5 as u8 => Ok(Opcode::SWAP5), + x if x == Opcode::SWAP6 as u8 => Ok(Opcode::SWAP6), + x if x == Opcode::SWAP7 as u8 => Ok(Opcode::SWAP7), + x if x == Opcode::SWAP8 as u8 => Ok(Opcode::SWAP8), + x if x == Opcode::SWAP9 as u8 => Ok(Opcode::SWAP9), + x if x == Opcode::SWAP10 as u8 => Ok(Opcode::SWAP10), + x if x == Opcode::SWAP11 as u8 => Ok(Opcode::SWAP11), + x if x == Opcode::SWAP12 as u8 => Ok(Opcode::SWAP12), + x if x == Opcode::SWAP13 as u8 => Ok(Opcode::SWAP13), + x if x == Opcode::SWAP14 as u8 => Ok(Opcode::SWAP14), + x if x == Opcode::SWAP15 as u8 => Ok(Opcode::SWAP15), + x if x == Opcode::SWAP16 as u8 => Ok(Opcode::SWAP16), + x if x == Opcode::LOG0 as u8 => Ok(Opcode::LOG0), + x if x == Opcode::LOG1 as u8 => Ok(Opcode::LOG1), + x if x == Opcode::LOG2 as u8 => Ok(Opcode::LOG2), + x if x == Opcode::LOG3 as u8 => Ok(Opcode::LOG3), + x if x == Opcode::LOG4 as u8 => Ok(Opcode::LOG4), + x if x == Opcode::CREATE as u8 => Ok(Opcode::CREATE), + x if x == Opcode::CALL as u8 => Ok(Opcode::CALL), + x if x == Opcode::CALLCODE as u8 => Ok(Opcode::CALLCODE), + x if x == Opcode::RETURN as u8 => Ok(Opcode::RETURN), + x if x == Opcode::DELEGATECALL as u8 => Ok(Opcode::DELEGATECALL), + x if x == Opcode::CREATE2 as u8 => Ok(Opcode::CREATE2), + x if x == Opcode::STATICCALL as u8 => Ok(Opcode::STATICCALL), + x if x == Opcode::REVERT as u8 => Ok(Opcode::REVERT), + x if x == Opcode::INVALID as u8 => Ok(Opcode::INVALID), + x if x == Opcode::SELFDESTRUCT as u8 => Ok(Opcode::SELFDESTRUCT), + _ => Err(()), + } + } } impl Opcode { /// Convert a byte into an Opcode pub fn from_byte(byte: u8) -> Opcode { - match byte { - 0x00 => Opcode::STOP, - 0x01 => Opcode::ADD, - 0x02 => Opcode::MUL, - 0x03 => Opcode::SUB, - 0x04 => Opcode::DIV, - 0x05 => Opcode::SDIV, - 0x06 => Opcode::MOD, - 0x07 => Opcode::SMOD, - 0x08 => Opcode::ADDMOD, - 0x09 => Opcode::MULMOD, - 0x0A => Opcode::EXP, - 0x0B => Opcode::SIGNEXTEND, - 0x10 => Opcode::LT, - 0x11 => Opcode::GT, - 0x12 => Opcode::SLT, - 0x13 => Opcode::SGT, - 0x14 => Opcode::EQ, - 0x15 => Opcode::ISZERO, - 0x16 => Opcode::AND, - 0x17 => Opcode::OR, - 0x18 => Opcode::XOR, - 0x19 => Opcode::NOT, - 0x1a => Opcode::BYTE, - 0x1b => Opcode::SHL, - 0x1c => Opcode::SHR, - 0x1d => Opcode::SAR, - 0x20 => Opcode::SHA3, - 0x30 => Opcode::ADDRESS, - 0x31 => Opcode::BALANCE, - 0x32 => Opcode::ORIGIN, - 0x33 => Opcode::CALLER, - 0x34 => Opcode::CALLVALUE, - 0x35 => Opcode::CALLDATALOAD, - 0x36 => Opcode::CALLDATASIZE, - 0x37 => Opcode::CALLDATACOPY, - 0x38 => Opcode::CODESIZE, - 0x39 => Opcode::CODECOPY, - 0x3a => Opcode::GASPRICE, - 0x3b => Opcode::EXTCODESIZE, - 0x3c => Opcode::EXTCODECOPY, - 0x3d => Opcode::RETURNDATASIZE, - 0x3e => Opcode::RETURNDATACOPY, - 0x3f => Opcode::EXTCODEHASH, - 0x40 => Opcode::BLOCKHASH, - 0x41 => Opcode::COINBASE, - 0x42 => Opcode::TIMESTAMP, - 0x43 => Opcode::NUMBER, - 0x44 => Opcode::DIFFICULTY, - 0x45 => Opcode::GASLIMIT, - 0x46 => Opcode::CHAINID, - 0x47 => Opcode::SELFBALANCE, - 0x48 => Opcode::BASEFEE, - 0x50 => Opcode::POP, - 0x51 => Opcode::MLOAD, - 0x52 => Opcode::MSTORE, - 0x53 => Opcode::MSTORE8, - 0x54 => Opcode::SLOAD, - 0x55 => Opcode::SSTORE, - 0x56 => Opcode::JUMP, - 0x57 => Opcode::JUMPI, - 0x58 => Opcode::PC, - 0x59 => Opcode::MSIZE, - 0x5a => Opcode::GAS, - 0x5b => Opcode::JUMPDEST, - 0x60 => Opcode::PUSH1, - 0x61 => Opcode::PUSH2, - 0x62 => Opcode::PUSH3, - 0x63 => Opcode::PUSH4, - 0x64 => Opcode::PUSH5, - 0x65 => Opcode::PUSH6, - 0x66 => Opcode::PUSH7, - 0x67 => Opcode::PUSH8, - 0x68 => Opcode::PUSH9, - 0x69 => Opcode::PUSH10, - 0x6a => Opcode::PUSH11, - 0x6b => Opcode::PUSH12, - 0x6c => Opcode::PUSH13, - 0x6d => Opcode::PUSH14, - 0x6e => Opcode::PUSH15, - 0x6f => Opcode::PUSH16, - 0x70 => Opcode::PUSH17, - 0x71 => Opcode::PUSH18, - 0x72 => Opcode::PUSH19, - 0x73 => Opcode::PUSH20, - 0x74 => Opcode::PUSH21, - 0x75 => Opcode::PUSH22, - 0x76 => Opcode::PUSH23, - 0x77 => Opcode::PUSH24, - 0x78 => Opcode::PUSH25, - 0x79 => Opcode::PUSH26, - 0x7a => Opcode::PUSH27, - 0x7b => Opcode::PUSH28, - 0x7c => Opcode::PUSH29, - 0x7d => Opcode::PUSH30, - 0x7e => Opcode::PUSH31, - 0x7f => Opcode::PUSH32, - 0x80 => Opcode::DUP1, - 0x81 => Opcode::DUP2, - 0x82 => Opcode::DUP3, - 0x83 => Opcode::DUP4, - 0x84 => Opcode::DUP5, - 0x85 => Opcode::DUP6, - 0x86 => Opcode::DUP7, - 0x87 => Opcode::DUP8, - 0x88 => Opcode::DUP9, - 0x89 => Opcode::DUP10, - 0x8a => Opcode::DUP11, - 0x8b => Opcode::DUP12, - 0x8c => Opcode::DUP13, - 0x8d => Opcode::DUP14, - 0x8e => Opcode::DUP15, - 0x8f => Opcode::DUP16, - 0x90 => Opcode::SWAP1, - 0x91 => Opcode::SWAP2, - 0x92 => Opcode::SWAP3, - 0x93 => Opcode::SWAP4, - 0x94 => Opcode::SWAP5, - 0x95 => Opcode::SWAP6, - 0x96 => Opcode::SWAP7, - 0x97 => Opcode::SWAP8, - 0x98 => Opcode::SWAP9, - 0x99 => Opcode::SWAP10, - 0x9a => Opcode::SWAP11, - 0x9b => Opcode::SWAP12, - 0x9c => Opcode::SWAP13, - 0x9d => Opcode::SWAP14, - 0x9e => Opcode::SWAP15, - 0x9f => Opcode::SWAP16, - 0xa0 => Opcode::LOG0, - 0xa1 => Opcode::LOG1, - 0xa2 => Opcode::LOG2, - 0xa3 => Opcode::LOG3, - 0xa4 => Opcode::LOG4, - 0xf0 => Opcode::CREATE, - 0xf1 => Opcode::CALL, - 0xf2 => Opcode::CALLCODE, - 0xf3 => Opcode::RETURN, - 0xf4 => Opcode::DELEGATECALL, - 0xf5 => Opcode::CREATE2, - 0xfa => Opcode::STATICCALL, - 0xfd => Opcode::REVERT, - 0xff => Opcode::SELFDESTRUCT, - _ => Opcode::INVALID, + match byte.try_into() { + Ok(v) => v, + Err(_) => Opcode::INVALID, } } } From 3e15c0290ddf413cf6c842bc8ade503d271aed3a Mon Sep 17 00:00:00 2001 From: mio Date: Wed, 12 Jul 2023 16:09:56 +0800 Subject: [PATCH 2/4] Even more rusty! --- src/decode.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/decode.rs b/src/decode.rs index 76beeb4..11fe0cc 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -7,7 +7,9 @@ pub fn decode_operation( ) -> Result<(Operation, u32)> { let encoded_opcode = bytes.next().expect("Unexpected end of input"); let num_bytes = match encoded_opcode { - 0x60..=0x7f => encoded_opcode - 0x5f, + _ if encoded_opcode <= Opcode::PUSH32 as u8 && encoded_opcode >= Opcode::PUSH1 as u8 => { + encoded_opcode + 1 - Opcode::PUSH1 as u8 + } _ => 0, }; From 74feeb65b41ce966c5865f44c7883c37f62bd7f2 Mon Sep 17 00:00:00 2001 From: mio Date: Sat, 15 Jul 2023 21:15:08 +0800 Subject: [PATCH 3/4] pub use decode_operation --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 31e2c81..731f004 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,13 +24,13 @@ //! //! ``` #![warn(missing_docs)] -use crate::decode::decode_operation; use std::fmt::Write; use eyre::Result; mod decode; +pub use crate::decode::decode_operation; pub mod types; pub use types::{Opcode, Operation}; From bb4e709661446cf098f5b614ba6a15a2cad1733f Mon Sep 17 00:00:00 2001 From: lazymio Date: Mon, 16 Oct 2023 14:04:12 +0800 Subject: [PATCH 4/4] No println --- src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 731f004..5a18574 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,8 +82,7 @@ pub fn disassemble_bytes(bytes: Vec) -> Result> { (new_operation, offset) = match decode_operation(&mut bytes_iter, offset) { Ok((operation, new_offset)) => (operation, new_offset), Err(e) => { - println!("Stop decoding at offset {offset} due to error : {e}"); - break; + return Err(e); } }; operations.push(new_operation);