mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 07:06:42 +00:00
fix: add UnmarshalJSON for vm.Opcode
This commit is contained in:
parent
3962af30e1
commit
12677e670f
3 changed files with 26 additions and 3 deletions
|
|
@ -17,7 +17,9 @@
|
||||||
package vm
|
package vm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// OpCode is an EVM opcode
|
// OpCode is an EVM opcode
|
||||||
|
|
@ -459,6 +461,27 @@ func (op OpCode) String() string {
|
||||||
return fmt.Sprintf("opcode %#x not defined", int(op))
|
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{
|
var stringToOp = map[string]OpCode{
|
||||||
"STOP": STOP,
|
"STOP": STOP,
|
||||||
"ADD": ADD,
|
"ADD": ADD,
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,7 @@ type callLog struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type callFrame struct {
|
type callFrame struct {
|
||||||
Type vm.OpCode `json:"-"`
|
Type vm.OpCode `json:"type"`
|
||||||
From common.Address `json:"from"`
|
From common.Address `json:"from"`
|
||||||
Gas uint64 `json:"gas"`
|
Gas uint64 `json:"gas"`
|
||||||
GasUsed uint64 `json:"gasUsed"`
|
GasUsed uint64 `json:"gasUsed"`
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ var _ = (*callFrameMarshaling)(nil)
|
||||||
// MarshalJSON marshals as JSON.
|
// MarshalJSON marshals as JSON.
|
||||||
func (c callFrame) MarshalJSON() ([]byte, error) {
|
func (c callFrame) MarshalJSON() ([]byte, error) {
|
||||||
type callFrame0 struct {
|
type callFrame0 struct {
|
||||||
Type vm.OpCode `json:"-"`
|
Type vm.OpCode `json:"type"`
|
||||||
From common.Address `json:"from"`
|
From common.Address `json:"from"`
|
||||||
Gas hexutil.Uint64 `json:"gas"`
|
Gas hexutil.Uint64 `json:"gas"`
|
||||||
GasUsed hexutil.Uint64 `json:"gasUsed"`
|
GasUsed hexutil.Uint64 `json:"gasUsed"`
|
||||||
|
|
@ -50,7 +50,7 @@ func (c callFrame) MarshalJSON() ([]byte, error) {
|
||||||
// UnmarshalJSON unmarshals from JSON.
|
// UnmarshalJSON unmarshals from JSON.
|
||||||
func (c *callFrame) UnmarshalJSON(input []byte) error {
|
func (c *callFrame) UnmarshalJSON(input []byte) error {
|
||||||
type callFrame0 struct {
|
type callFrame0 struct {
|
||||||
Type *vm.OpCode `json:"-"`
|
Type *vm.OpCode `json:"type"`
|
||||||
From *common.Address `json:"from"`
|
From *common.Address `json:"from"`
|
||||||
Gas *hexutil.Uint64 `json:"gas"`
|
Gas *hexutil.Uint64 `json:"gas"`
|
||||||
GasUsed *hexutil.Uint64 `json:"gasUsed"`
|
GasUsed *hexutil.Uint64 `json:"gasUsed"`
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue