From 12677e670fc1f6649fc41ebb812aca4df6be9f88 Mon Sep 17 00:00:00 2001 From: wanger Date: Thu, 13 Mar 2025 03:19:49 +0800 Subject: [PATCH] fix: add UnmarshalJSON for vm.Opcode --- core/vm/opcodes.go | 23 +++++++++++++++++++++++ eth/tracers/native/call.go | 2 +- eth/tracers/native/gen_callframe_json.go | 4 ++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/core/vm/opcodes.go b/core/vm/opcodes.go index 0820b20fb1..c65186a96a 100644 --- a/core/vm/opcodes.go +++ b/core/vm/opcodes.go @@ -17,7 +17,9 @@ package vm import ( + "encoding/json" "fmt" + "strings" ) // OpCode is an EVM opcode @@ -459,6 +461,27 @@ func (op OpCode) String() string { return fmt.Sprintf("opcode %#x not defined", int(op)) } +func (op OpCode) MarshalJSON() ([]byte, error) { + return json.Marshal(op.String()) +} + +// UnmarshalJSON implements json.Unmarshaler +func (op *OpCode) UnmarshalJSON(data []byte) error { + var s string + if err := json.Unmarshal(data, &s); err != nil { + return fmt.Errorf("OpCode must be a string, got: %v", err) + } + + // Case-insensitive lookup + code, exists := stringToOp[strings.ToUpper(s)] + if !exists { + return fmt.Errorf("unknown OpCode: %s", s) + } + + *op = code + return nil +} + var stringToOp = map[string]OpCode{ "STOP": STOP, "ADD": ADD, diff --git a/eth/tracers/native/call.go b/eth/tracers/native/call.go index b85df1b98e..68194cf0f5 100644 --- a/eth/tracers/native/call.go +++ b/eth/tracers/native/call.go @@ -52,7 +52,7 @@ type callLog struct { } type callFrame struct { - Type vm.OpCode `json:"-"` + Type vm.OpCode `json:"type"` From common.Address `json:"from"` Gas uint64 `json:"gas"` GasUsed uint64 `json:"gasUsed"` diff --git a/eth/tracers/native/gen_callframe_json.go b/eth/tracers/native/gen_callframe_json.go index c44f38390d..40e35f1674 100644 --- a/eth/tracers/native/gen_callframe_json.go +++ b/eth/tracers/native/gen_callframe_json.go @@ -16,7 +16,7 @@ var _ = (*callFrameMarshaling)(nil) // MarshalJSON marshals as JSON. func (c callFrame) MarshalJSON() ([]byte, error) { type callFrame0 struct { - Type vm.OpCode `json:"-"` + Type vm.OpCode `json:"type"` From common.Address `json:"from"` Gas hexutil.Uint64 `json:"gas"` GasUsed hexutil.Uint64 `json:"gasUsed"` @@ -50,7 +50,7 @@ func (c callFrame) MarshalJSON() ([]byte, error) { // UnmarshalJSON unmarshals from JSON. func (c *callFrame) UnmarshalJSON(input []byte) error { type callFrame0 struct { - Type *vm.OpCode `json:"-"` + Type *vm.OpCode `json:"type"` From *common.Address `json:"from"` Gas *hexutil.Uint64 `json:"gas"` GasUsed *hexutil.Uint64 `json:"gasUsed"`