|
| 1 | +package blocklog |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestRethParser_ParseLine(t *testing.T) { |
| 12 | + parser := NewRethParser() |
| 13 | + |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + line string |
| 17 | + wantOK bool |
| 18 | + // checkJSON is called when wantOK is true to verify the parsed output. |
| 19 | + checkJSON func(t *testing.T, data map[string]any) |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "valid slow_block line with all fields", |
| 23 | + line: `2026-03-10T10:29:20.098444Z WARN reth::slow_block: Slow block block.number=1 block.hash=0xc957abc123 block.gas_used=100000000 block.tx_count=6 timing.execution_ms=91 timing.state_read_ms=5 timing.state_hash_ms=10 timing.commit_ms=3 timing.total_ms=109 throughput.mgas_per_sec="1091.46" state_reads.accounts=8 state_reads.storage_slots=12 state_reads.code=2 state_reads.code_bytes=1024 state_writes.accounts=4 state_writes.accounts_deleted=0 state_writes.storage_slots=6 state_writes.code=1 cache.account.hits=1 cache.account.misses=7 cache.storage.hits=3 cache.storage.misses=9 cache.code.hits=0 cache.code.misses=2`, |
| 24 | + wantOK: true, |
| 25 | + checkJSON: func(t *testing.T, data map[string]any) { |
| 26 | + t.Helper() |
| 27 | + |
| 28 | + assert.Equal(t, "warn", data["level"]) |
| 29 | + assert.Equal(t, "Slow block", data["msg"]) |
| 30 | + |
| 31 | + block := data["block"].(map[string]any) |
| 32 | + assert.Equal(t, float64(1), block["number"]) |
| 33 | + assert.Equal(t, "0xc957abc123", block["hash"]) |
| 34 | + assert.Equal(t, float64(100000000), block["gas_used"]) |
| 35 | + assert.Equal(t, float64(6), block["tx_count"]) |
| 36 | + |
| 37 | + timing := data["timing"].(map[string]any) |
| 38 | + assert.Equal(t, float64(91), timing["execution_ms"]) |
| 39 | + assert.Equal(t, float64(5), timing["state_read_ms"]) |
| 40 | + assert.Equal(t, float64(10), timing["state_hash_ms"]) |
| 41 | + assert.Equal(t, float64(3), timing["commit_ms"]) |
| 42 | + assert.Equal(t, float64(109), timing["total_ms"]) |
| 43 | + |
| 44 | + throughput := data["throughput"].(map[string]any) |
| 45 | + assert.Equal(t, 1091.46, throughput["mgas_per_sec"]) |
| 46 | + |
| 47 | + stateReads := data["state_reads"].(map[string]any) |
| 48 | + assert.Equal(t, float64(8), stateReads["accounts"]) |
| 49 | + assert.Equal(t, float64(12), stateReads["storage_slots"]) |
| 50 | + assert.Equal(t, float64(2), stateReads["code"]) |
| 51 | + assert.Equal(t, float64(1024), stateReads["code_bytes"]) |
| 52 | + |
| 53 | + stateWrites := data["state_writes"].(map[string]any) |
| 54 | + assert.Equal(t, float64(4), stateWrites["accounts"]) |
| 55 | + assert.Equal(t, float64(0), stateWrites["accounts_deleted"]) |
| 56 | + assert.Equal(t, float64(6), stateWrites["storage_slots"]) |
| 57 | + assert.Equal(t, float64(1), stateWrites["code"]) |
| 58 | + |
| 59 | + cache := data["cache"].(map[string]any) |
| 60 | + account := cache["account"].(map[string]any) |
| 61 | + assert.Equal(t, float64(1), account["hits"]) |
| 62 | + assert.Equal(t, float64(7), account["misses"]) |
| 63 | + |
| 64 | + storage := cache["storage"].(map[string]any) |
| 65 | + assert.Equal(t, float64(3), storage["hits"]) |
| 66 | + assert.Equal(t, float64(9), storage["misses"]) |
| 67 | + |
| 68 | + code := cache["code"].(map[string]any) |
| 69 | + assert.Equal(t, float64(0), code["hits"]) |
| 70 | + assert.Equal(t, float64(2), code["misses"]) |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "quoted float values parsed correctly", |
| 75 | + line: `2026-03-10T10:29:20.098444Z WARN reth::slow_block: Slow block throughput.mgas_per_sec="12.50" timing.execution_ms="91.3"`, |
| 76 | + wantOK: true, |
| 77 | + checkJSON: func(t *testing.T, data map[string]any) { |
| 78 | + t.Helper() |
| 79 | + |
| 80 | + throughput := data["throughput"].(map[string]any) |
| 81 | + assert.Equal(t, 12.50, throughput["mgas_per_sec"]) |
| 82 | + |
| 83 | + timing := data["timing"].(map[string]any) |
| 84 | + assert.Equal(t, 91.3, timing["execution_ms"]) |
| 85 | + }, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "non-slow-block reth log line", |
| 89 | + line: `2026-03-10T10:29:20.098444Z INFO reth::engine: Block received block.number=1`, |
| 90 | + wantOK: false, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "empty line", |
| 94 | + line: "", |
| 95 | + wantOK: false, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "random text", |
| 99 | + line: "some random log output that does not match", |
| 100 | + wantOK: false, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "line with ANSI escape codes", |
| 104 | + line: "\x1b[2m2026-03-10T10:50:19.731231Z\x1b[0m \x1b[33m WARN\x1b[0m \x1b[2mreth::slow_block\x1b[0m\x1b[2m:\x1b[0m Slow block \x1b[3mblock.number\x1b[0m\x1b[2m=\x1b[0m1 \x1b[3mblock.hash\x1b[0m\x1b[2m=\x1b[0m0x9f566dc9f8beb533db8611872f4ed57847d147224b59586d2c86e1bf957b8809 \x1b[3mblock.gas_used\x1b[0m\x1b[2m=\x1b[0m184074778176 \x1b[3mblock.tx_count\x1b[0m\x1b[2m=\x1b[0m12339 \x1b[3mtiming.execution_ms\x1b[0m\x1b[2m=\x1b[0m2783 \x1b[3mthroughput.mgas_per_sec\x1b[0m\x1b[2m=\x1b[0m\"66126.09\"", |
| 105 | + wantOK: true, |
| 106 | + checkJSON: func(t *testing.T, data map[string]any) { |
| 107 | + t.Helper() |
| 108 | + |
| 109 | + assert.Equal(t, "warn", data["level"]) |
| 110 | + assert.Equal(t, "Slow block", data["msg"]) |
| 111 | + |
| 112 | + block := data["block"].(map[string]any) |
| 113 | + assert.Equal(t, float64(1), block["number"]) |
| 114 | + assert.Equal(t, "0x9f566dc9f8beb533db8611872f4ed57847d147224b59586d2c86e1bf957b8809", block["hash"]) |
| 115 | + assert.Equal(t, float64(184074778176), block["gas_used"]) |
| 116 | + assert.Equal(t, float64(12339), block["tx_count"]) |
| 117 | + |
| 118 | + timing := data["timing"].(map[string]any) |
| 119 | + assert.Equal(t, float64(2783), timing["execution_ms"]) |
| 120 | + |
| 121 | + throughput := data["throughput"].(map[string]any) |
| 122 | + assert.Equal(t, 66126.09, throughput["mgas_per_sec"]) |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "extra unknown fields are preserved", |
| 127 | + line: `2026-03-10T10:29:20.098444Z WARN reth::slow_block: Slow block block.number=42 eip7702_delegations.set=5`, |
| 128 | + wantOK: true, |
| 129 | + checkJSON: func(t *testing.T, data map[string]any) { |
| 130 | + t.Helper() |
| 131 | + |
| 132 | + block := data["block"].(map[string]any) |
| 133 | + assert.Equal(t, float64(42), block["number"]) |
| 134 | + |
| 135 | + eip := data["eip7702_delegations"].(map[string]any) |
| 136 | + assert.Equal(t, float64(5), eip["set"]) |
| 137 | + }, |
| 138 | + }, |
| 139 | + } |
| 140 | + |
| 141 | + for _, tt := range tests { |
| 142 | + t.Run(tt.name, func(t *testing.T) { |
| 143 | + result, ok := parser.ParseLine(tt.line) |
| 144 | + |
| 145 | + assert.Equal(t, tt.wantOK, ok) |
| 146 | + |
| 147 | + if tt.wantOK { |
| 148 | + require.NotNil(t, result) |
| 149 | + |
| 150 | + var parsed map[string]any |
| 151 | + err := json.Unmarshal(result, &parsed) |
| 152 | + require.NoError(t, err) |
| 153 | + |
| 154 | + tt.checkJSON(t, parsed) |
| 155 | + } else { |
| 156 | + assert.Nil(t, result) |
| 157 | + } |
| 158 | + }) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +func TestRethParser_ClientType(t *testing.T) { |
| 163 | + parser := NewRethParser() |
| 164 | + assert.Equal(t, "reth", string(parser.ClientType())) |
| 165 | +} |
0 commit comments