Skip to content

Commit

Permalink
eth/tracers/logger: use JSON struct tag ",omitempty" to reduce log bl…
Browse files Browse the repository at this point in the history
…oat from blank fields

Noticed while debugging Tharsis/EVMOS logs that the logs were
extraneous with empty fields such as
```json
{"pc":4716,"op":80,"gas":"0x688e5","gasCost":"0x2","memory":"0x","memSize":352,"stack":["0xfa31de01","0x29a","0x7d0","0xabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd","0x80","0x534","0xc0","0x5f646b5592c4159d4963fbf901f81144ef53cc51a7082435e3a6120a45002ff7","0x15dc","0x34","0x5f646b5592c4159d4963fbf901f81144ef53cc51a7082435e3a6120a45002ff7"],"returnData":"0x","depth":2,"refund":0,"opName":"POP","error":""}
{"pc":4717,"op":80,"gas":"0x688e3","gasCost":"0x2","memory":"0x","memSize":352,"stack":["0xfa31de01","0x29a","0x7d0","0xabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd","0x80","0x534","0xc0","0x5f646b5592c4159d4963fbf901f81144ef53cc51a7082435e3a6120a45002ff7","0x15dc","0x34"],"returnData":"0x","depth":2,"refund":0,"opName":"POP","error":""}
```

with empty fields like "error", "refund", "memory", "returnData".

This change adds the JSON tag ",omitempty" to remove the bloat from the
above empty fields which shows immediate impact of reducing the logs to

```json
{"pc":4716,"op":80,"gas":"0x688e5","gasCost":"0x2","memSize":352,"stack":["0xfa31de01","0x29a","0x7d0","0xabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd","0x80","0x534","0xc0","0x5f646b5592c4159d4963fbf901f81144ef53cc51a7082435e3a6120a45002ff7","0x15dc","0x34","0x5f646b5592c4159d4963fbf901f81144ef53cc51a7082435e3a6120a45002ff7"],"depth":2,"opName":"POP"}
{"pc":4717,"op":80,"gas":"0x688e3","gasCost":"0x2","memSize":352,"stack":["0xfa31de01","0x29a","0x7d0","0xabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd","0x80","0x534","0xc0","0x5f646b5592c4159d4963fbf901f81144ef53cc51a7082435e3a6120a45002ff7","0x15dc","0x34"],"depth":2,"opName":"POP"}
```

and not only does that reduce the size of logs but it also makes it much
easier to quickly debug and grep for errors, which will only be present if
erroring.

Fixes #24487
  • Loading branch information
odeke-em committed Mar 2, 2022
1 parent f4ff426 commit dd4551b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
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"`
Memory hexutil.Bytes `json:",omitempty"`
ReturnData hexutil.Bytes `json:",omitempty"`
OpName string `json:"opName,omitempty"` // 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.
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)
}
})
}
}

0 comments on commit dd4551b

Please sign in to comment.