Skip to content

Commit 3b4c683

Browse files
holimanunclezoro
authored andcommitted
eth/tracers/logger: use omitempty to reduce log bloat (ethereum#24547)
Makes the evm json output less verbose: omitting output of `memory` and `returndata` in case they are empty.
1 parent 3d5896a commit 3b4c683

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

eth/tracers/logger/gen_structlog.go

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eth/tracers/logger/logger.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ type StructLog struct {
6666
Op vm.OpCode `json:"op"`
6767
Gas uint64 `json:"gas"`
6868
GasCost uint64 `json:"gasCost"`
69-
Memory []byte `json:"memory"`
69+
Memory []byte `json:"memory,omitempty"`
7070
MemorySize int `json:"memSize"`
7171
Stack []uint256.Int `json:"stack"`
72-
ReturnData []byte `json:"returnData"`
72+
ReturnData []byte `json:"returnData,omitempty"`
7373
Storage map[common.Hash]common.Hash `json:"-"`
7474
Depth int `json:"depth"`
7575
RefundCounter uint64 `json:"refund"`
@@ -82,8 +82,8 @@ type structLogMarshaling struct {
8282
GasCost math.HexOrDecimal64
8383
Memory hexutil.Bytes
8484
ReturnData hexutil.Bytes
85-
OpName string `json:"opName"` // adds call to OpName() in MarshalJSON
86-
ErrorString string `json:"error"` // adds call to ErrorString() in MarshalJSON
85+
OpName string `json:"opName"` // adds call to OpName() in MarshalJSON
86+
ErrorString string `json:"error,omitempty"` // adds call to ErrorString() in MarshalJSON
8787
}
8888

8989
// OpName formats the operand name in a human-readable format.

eth/tracers/logger/logger_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package logger
1818

1919
import (
20+
"encoding/json"
21+
"fmt"
2022
"math/big"
2123
"testing"
2224

@@ -72,3 +74,34 @@ func TestStoreCapture(t *testing.T) {
7274
t.Errorf("expected %x, got %x", exp, logger.storage[contract.Address()][index])
7375
}
7476
}
77+
78+
// Tests that blank fields don't appear in logs when JSON marshalled, to reduce
79+
// logs bloat and confusion. See https://github.com/ethereum/go-ethereum/issues/24487
80+
func TestStructLogMarshalingOmitEmpty(t *testing.T) {
81+
tests := []struct {
82+
name string
83+
log *StructLog
84+
want string
85+
}{
86+
{"empty err and no fields", &StructLog{},
87+
`{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`},
88+
{"with err", &StructLog{Err: fmt.Errorf("this failed")},
89+
`{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP","error":"this failed"}`},
90+
{"with mem", &StructLog{Memory: make([]byte, 2), MemorySize: 2},
91+
`{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memory":"0x0000","memSize":2,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`},
92+
{"with 0-size mem", &StructLog{Memory: make([]byte, 0)},
93+
`{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`},
94+
}
95+
96+
for _, tt := range tests {
97+
t.Run(tt.name, func(t *testing.T) {
98+
blob, err := json.Marshal(tt.log)
99+
if err != nil {
100+
t.Fatal(err)
101+
}
102+
if have, want := string(blob), tt.want; have != want {
103+
t.Fatalf("mismatched results\n\thave: %v\n\twant: %v", have, want)
104+
}
105+
})
106+
}
107+
}

0 commit comments

Comments
 (0)