eth/tracers/logger: use omitempty to reduce log bloat (#24547)

Makes the evm json output less verbose: omitting output of `memory` and `returndata` in case they are empty.
pull/24614/head
Martin Holst Swende 3 years ago committed by GitHub
parent b5a129ea24
commit 67c070c379
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      eth/tracers/logger/gen_structlog.go
  2. 8
      eth/tracers/logger/logger.go
  3. 33
      eth/tracers/logger/logger_test.go

@ -21,16 +21,16 @@ func (s StructLog) MarshalJSON() ([]byte, error) {
Op vm.OpCode `json:"op"`
Gas math.HexOrDecimal64 `json:"gas"`
GasCost math.HexOrDecimal64 `json:"gasCost"`
Memory hexutil.Bytes `json:"memory"`
Memory hexutil.Bytes `json:"memory,omitempty"`
MemorySize int `json:"memSize"`
Stack []uint256.Int `json:"stack"`
ReturnData hexutil.Bytes `json:"returnData"`
ReturnData hexutil.Bytes `json:"returnData,omitempty"`
Storage map[common.Hash]common.Hash `json:"-"`
Depth int `json:"depth"`
RefundCounter uint64 `json:"refund"`
Err error `json:"-"`
OpName string `json:"opName"`
ErrorString string `json:"error"`
ErrorString string `json:"error,omitempty"`
}
var enc StructLog
enc.Pc = s.Pc
@ -57,10 +57,10 @@ func (s *StructLog) UnmarshalJSON(input []byte) error {
Op *vm.OpCode `json:"op"`
Gas *math.HexOrDecimal64 `json:"gas"`
GasCost *math.HexOrDecimal64 `json:"gasCost"`
Memory *hexutil.Bytes `json:"memory"`
Memory *hexutil.Bytes `json:"memory,omitempty"`
MemorySize *int `json:"memSize"`
Stack []uint256.Int `json:"stack"`
ReturnData *hexutil.Bytes `json:"returnData"`
ReturnData *hexutil.Bytes `json:"returnData,omitempty"`
Storage map[common.Hash]common.Hash `json:"-"`
Depth *int `json:"depth"`
RefundCounter *uint64 `json:"refund"`

@ -66,10 +66,10 @@ type StructLog struct {
Op vm.OpCode `json:"op"`
Gas uint64 `json:"gas"`
GasCost uint64 `json:"gasCost"`
Memory []byte `json:"memory"`
Memory []byte `json:"memory,omitempty"`
MemorySize int `json:"memSize"`
Stack []uint256.Int `json:"stack"`
ReturnData []byte `json:"returnData"`
ReturnData []byte `json:"returnData,omitempty"`
Storage map[common.Hash]common.Hash `json:"-"`
Depth int `json:"depth"`
RefundCounter uint64 `json:"refund"`
@ -82,8 +82,8 @@ type structLogMarshaling struct {
GasCost math.HexOrDecimal64
Memory hexutil.Bytes
ReturnData hexutil.Bytes
OpName string `json:"opName"` // adds call to OpName() in MarshalJSON
ErrorString string `json:"error"` // adds call to ErrorString() in MarshalJSON
OpName string `json:"opName"` // adds call to OpName() in MarshalJSON
ErrorString string `json:"error,omitempty"` // adds call to ErrorString() in MarshalJSON
}
// OpName formats the operand name in a human-readable format.

@ -17,6 +17,8 @@
package logger
import (
"encoding/json"
"fmt"
"math/big"
"testing"
@ -72,3 +74,34 @@ func TestStoreCapture(t *testing.T) {
t.Errorf("expected %x, got %x", exp, logger.storage[contract.Address()][index])
}
}
// Tests that blank fields don't appear in logs when JSON marshalled, to reduce
// logs bloat and confusion. See https://github.com/ethereum/go-ethereum/issues/24487
func TestStructLogMarshalingOmitEmpty(t *testing.T) {
tests := []struct {
name string
log *StructLog
want string
}{
{"empty err and no fields", &StructLog{},
`{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`},
{"with err", &StructLog{Err: fmt.Errorf("this failed")},
`{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP","error":"this failed"}`},
{"with mem", &StructLog{Memory: make([]byte, 2), MemorySize: 2},
`{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memory":"0x0000","memSize":2,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`},
{"with 0-size mem", &StructLog{Memory: make([]byte, 0)},
`{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
blob, err := json.Marshal(tt.log)
if err != nil {
t.Fatal(err)
}
if have, want := string(blob), tt.want; have != want {
t.Fatalf("mismatched results\n\thave: %v\n\twant: %v", have, want)
}
})
}
}

Loading…
Cancel
Save