fix: add UnmarshalJSON for vm.Opcode

This commit is contained in:
wanger 2025-03-13 03:19:49 +08:00
parent 3962af30e1
commit 12677e670f
3 changed files with 26 additions and 3 deletions

View file

@ -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,

View file

@ -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"`

View file

@ -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"`