Skip to content

Commit e3a5a8d

Browse files
committed
eth/tracers/logger: use omitempty to reduce log bloat ethereum#24547
1 parent 8712fec commit e3a5a8d

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"`
@@ -83,8 +83,8 @@ type structLogMarshaling struct {
8383
Memory hexutil.Bytes
8484
ReturnData hexutil.Bytes
8585
Stack []hexutil.U256
86-
OpName string `json:"opName"` // adds call to OpName() in MarshalJSON
87-
ErrorString string `json:"error"` // adds call to ErrorString() in MarshalJSON
86+
OpName string `json:"opName"` // adds call to OpName() in MarshalJSON
87+
ErrorString string `json:"error,omitempty"` // adds call to ErrorString() in MarshalJSON
8888
}
8989

9090
// 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

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

0 commit comments

Comments
 (0)