Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eth/tracers/logger: use JSON struct tag ",omitempty" to reduce log bloat from blank fields #24491

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions eth/tracers/logger/gen_structlog.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions eth/tracers/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ type StructLog struct {

// overrides for gencodec
type structLogMarshaling struct {
Gas math.HexOrDecimal64
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
Gas math.HexOrDecimal64 `json:",omitempty"`
GasCost math.HexOrDecimal64 `json:",omitempty"`
Comment on lines +81 to +82
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will cause 0 to become omitted, which is technically incorrect, IMO. So I don't think we should set omitEmpty for Gas and GasCost.

Memory hexutil.Bytes `json:",omitempty"`
ReturnData hexutil.Bytes `json:",omitempty"`
OpName string `json:"opName,omitempty"` // adds call to OpName() in MarshalJSON
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OpName should never be empty, if so that's an error. Please revert

ErrorString string `json:"error,omitempty"` // adds call to ErrorString() in MarshalJSON
}

// OpName formats the operand name in a human-readable format.
Expand Down
27 changes: 27 additions & 0 deletions eth/tracers/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package logger

import (
"encoding/json"
"math/big"
"testing"

Expand Down Expand Up @@ -72,3 +73,29 @@ 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 *structLogMarshaling
want string
}{
{"empty err and no fields", &structLogMarshaling{ErrorString: ""}, `{}`},
{"with Gas cost only", &structLogMarshaling{GasCost: 10}, `{"GasCost":"0xa"}`},
{"with err", &structLogMarshaling{ErrorString: "this failed"}, `{"error":"this failed"}`},
}

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 g, w := string(blob), tt.want; g != w {
t.Fatalf("Mismatched results\n\tGot: %q\n\tWant: %q", g, w)
}
})
}
}