eth/tracers/logger: use omitempty to reduce log bloat (#24547)

Makes the evm json output less verbose: omitting output of `memory` and `returndata` in case they are empty.
This commit is contained in:
Martin Holst Swende
2022-03-29 22:36:55 +02:00
committed by GitHub
parent b5a129ea24
commit 67c070c379
3 changed files with 42 additions and 9 deletions

View File

@ -17,6 +17,8 @@
package logger
import (
"encoding/json"
"fmt"
"math/big"
"testing"
@ -72,3 +74,34 @@ 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 *StructLog
want string
}{
{"empty err and no fields", &StructLog{},
`{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`},
{"with err", &StructLog{Err: fmt.Errorf("this failed")},
`{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP","error":"this failed"}`},
{"with mem", &StructLog{Memory: make([]byte, 2), MemorySize: 2},
`{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memory":"0x0000","memSize":2,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`},
{"with 0-size mem", &StructLog{Memory: make([]byte, 0)},
`{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`},
}
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: %v\n\twant: %v", have, want)
}
})
}
}