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