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 omitempty to reduce log bloat #24547

Merged
merged 4 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion eth/tracers/logger/gen_structlog.go

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

8 changes: 4 additions & 4 deletions eth/tracers/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ type StructLog struct {
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
Memory hexutil.Bytes `json:",omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

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

❯ ./evm --debug --json --code "0x60006000" run                                                                                                                                 <<<
{"pc":0,"op":96,"gas":"0x2540be400","gasCost":"0x3","memory":"0x","memSize":0,"stack":[],"returnData":"0x","depth":1,"refund":0,"opName":"PUSH1"}
{"pc":2,"op":96,"gas":"0x2540be3fd","gasCost":"0x3","memory":"0x","memSize":0,"stack":["0x0"],"returnData":"0x","depth":1,"refund":0,"opName":"PUSH1"}
{"pc":4,"op":0,"gas":"0x2540be3fa","gasCost":"0x0","memory":"0x","memSize":0,"stack":["0x0","0x0"],"returnData":"0x","depth":1,"refund":0,"opName":"STOP"}

memory and returnData still have 0x when they're empty so they're not really omitted

Copy link
Contributor Author

@holiman holiman Mar 16, 2022

Choose a reason for hiding this comment

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

Here's how I want it to work (not sure it does):

  • If you run it with memory enabled, and returndata enabled, then you get "memory":"0x" if memory is indeed blank.
  • If you run it with memory disabled, then we don't output "memory" at all.

Copy link
Contributor Author

@holiman holiman Mar 16, 2022

Choose a reason for hiding this comment

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

(not sure it does):

Narrator: And it doesn't

Copy link
Contributor

Choose a reason for hiding this comment

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

The type Bytes seems to encode 0x even if the slice is nil

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I haven't studied in-dept how omitempty works. But it seems like I cannot make it differentiate between a null-slice and an empty array.

So the choice is:

  • Always output 0x as memory, even when memory disabled, or
  • Never display blank memory.

This PR currently uses option 2. Makes the output a bit less verbose.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The type Bytes seems to encode 0x even if the slice is nil

Yes.. omitempty might just look at the encoded return value, and check if it's empty. We don't want to change the marshaller for types.Bytes, so I think we'll have to just pick one behaviour.

ReturnData hexutil.Bytes `json:",omitempty"`
OpName string `json:"opName"` // adds call to OpName() in MarshalJSON
s1na marked this conversation as resolved.
Show resolved Hide resolved
ErrorString string `json:"error,omitempty"` // adds call to ErrorString() in MarshalJSON
}

// OpName formats the operand name in a human-readable format.
Expand Down
26 changes: 26 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,28 @@ 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: ""}, `{"Gas":"0x0","GasCost":"0x0","opName":""}`},
{"with err", &structLogMarshaling{ErrorString: "this failed"}, `{"Gas":"0x0","GasCost":"0x0","opName":"","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 have, want := string(blob), tt.want; have != want {
t.Fatalf("mismatched results\n\thave: %q\n\twant: %q", have, want)
}
})
}
}