Skip to content

Commit a17cb59

Browse files
seidroid[bot]codchenclaude
authored
Backport release/v6.8: fix(evm): discard cached traces written under another format version (#4876)
Backport of #4819 to `release/v6.8`. Co-authored-by: Tony Chen <codchen03@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c681d56 commit a17cb59

3 files changed

Lines changed: 194 additions & 4 deletions

File tree

‎evmrpc/AGENTS.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ Legacy **`sei_*`** JSON-RPC (EVM HTTP only) are **gated** by the `[evm].enabled_
5555

5656
**Tracer gating (deviation from geth defaults):** caller-supplied `TraceConfig.Tracer` values on `debug_traceCall` / `debug_traceTransaction` / `debug_traceBlockBy*` / `debug_traceTransactionProfile` are gated by `[evm]` config in `app.toml`. `trace_allowed_tracers` lists the native geth tracer names callers may request (validated native-only at startup; `muxTracer` nested tracer names are validated recursively with a bounded depth). `trace_allow_js_tracers` (default `false`) is a separate explicit opt-in for request-supplied JavaScript tracer source — upstream geth accepts JS tracers by default, Sei does not. Enabling JS does **not** widen the native allowlist. Validation runs in `validateTraceTracer` (`tracers.go`) before trace-cache lookups and before any tracer is constructed; the default struct logger (no `tracer` field) is always available. `trace_bake_tracers` is held to the same native-only rule at startup.
5757

58+
**Trace cache format version:** the trace baker's cache (`x/evm/keeper/trace_db.go`) is stamped with `traceDBFormatVersion`, and a node deletes every cached trace when it opens a cache stamped with any other version. A change that alters the trace a replay of an already committed block produces (the state `initializeBlock` rebuilds, transaction execution during replay, tracer output or its encoding) must increment `traceDBFormatVersion` in the same PR. Otherwise nodes that baked before the change keep serving the old traces, because cached results are served before any replay and are never pruned under the default `trace_bake_window_blocks = 0`.
59+
5860
## Consistency
5961
RPC responses for historical heights should never change as the blockchain progresses, or as the blockchain code gets upgraded.
6062

‎x/evm/keeper/trace_db.go‎

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,88 @@ type TraceDB struct {
2626
}
2727

2828
const (
29-
traceDBPrefix = "ts/"
30-
traceDBBlockPrefix = "tb/"
31-
traceDBLastBakedKey = "meta/last_baked_height"
29+
traceDBPrefix = "ts/"
30+
traceDBBlockPrefix = "tb/"
31+
traceDBLastBakedKey = "meta/last_baked_height"
32+
traceDBFormatVersionKey = "meta/format_version"
3233
)
3334

35+
// traceDBFormatVersion identifies the replay semantics that produced a TraceDB's
36+
// rows; NewTraceDB discards rows stored under any other version, and a DB without
37+
// a stamp reads as version 0. Increment it in any change that alters the trace a
38+
// replay of an already committed block produces.
39+
const traceDBFormatVersion uint64 = 1
40+
41+
// NewTraceDB opens the trace DB at <homeDir>/data/trace_db. When the DB is stamped
42+
// with a format version other than traceDBFormatVersion, it deletes every row,
43+
// the last baked height included, before returning.
3444
func NewTraceDB(homeDir string) (*TraceDB, error) {
3545
dir := filepath.Join(homeDir, "data", "trace_db")
3646
db, err := pebble.Open(dir, &pebble.Options{})
3747
if err != nil {
3848
return nil, fmt.Errorf("open trace db: %w", err)
3949
}
40-
return &TraceDB{db: db}, nil
50+
c := &TraceDB{db: db}
51+
if err := c.discardOtherFormatVersions(); err != nil {
52+
_ = db.Close()
53+
return nil, err
54+
}
55+
return c, nil
56+
}
57+
58+
// discardOtherFormatVersions resets the DB to traceDBFormatVersion unless it
59+
// already carries that stamp.
60+
func (c *TraceDB) discardOtherFormatVersions() error {
61+
stored, err := c.storedFormatVersion()
62+
if err != nil {
63+
return err
64+
}
65+
if stored == traceDBFormatVersion {
66+
return nil
67+
}
68+
if err := c.resetToFormatVersion(traceDBFormatVersion); err != nil {
69+
return err
70+
}
71+
logger.Info("trace db reset to current format version",
72+
"stored_version", stored, "format_version", traceDBFormatVersion)
73+
return nil
74+
}
75+
76+
// storedFormatVersion returns the format version stamped on the DB, or 0 when the
77+
// stamp is absent or malformed.
78+
func (c *TraceDB) storedFormatVersion() (uint64, error) {
79+
val, closer, err := c.db.Get([]byte(traceDBFormatVersionKey))
80+
if err != nil {
81+
if errors.Is(err, pebble.ErrNotFound) {
82+
return 0, nil
83+
}
84+
return 0, fmt.Errorf("read trace db format version: %w", err)
85+
}
86+
defer func() { _ = closer.Close() }()
87+
if len(val) != 8 {
88+
return 0, nil
89+
}
90+
return binary.BigEndian.Uint64(val), nil
91+
}
92+
93+
// resetToFormatVersion deletes every key and stamps version in one synced batch.
94+
func (c *TraceDB) resetToFormatVersion(version uint64) error {
95+
var stamp [8]byte
96+
binary.BigEndian.PutUint64(stamp[:], version)
97+
b := c.db.NewBatch()
98+
defer func() { _ = b.Close() }()
99+
// Every key starts with an ASCII prefix, so this range spans the whole keyspace.
100+
// The stamp is written after the range delete in the same batch, so it survives it.
101+
if err := b.DeleteRange([]byte{}, []byte{0xff}, nil); err != nil {
102+
return fmt.Errorf("trace db reset: %w", err)
103+
}
104+
if err := b.Set([]byte(traceDBFormatVersionKey), stamp[:], nil); err != nil {
105+
return fmt.Errorf("trace db reset: %w", err)
106+
}
107+
if err := b.Commit(pebble.Sync); err != nil {
108+
return fmt.Errorf("trace db reset: %w", err)
109+
}
110+
return nil
41111
}
42112

43113
func (c *TraceDB) Close() error {

‎x/evm/keeper/trace_db_test.go‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package keeper
22

33
import (
4+
"encoding/binary"
45
"encoding/json"
6+
"path/filepath"
57
"sync/atomic"
68
"testing"
79

10+
"github.com/cockroachdb/pebble/v2"
811
"github.com/ethereum/go-ethereum/common"
912
"github.com/stretchr/testify/require"
1013
)
@@ -156,6 +159,121 @@ func TestTraceDBPruneCoversBothKeyspaces(t *testing.T) {
156159
}
157160
}
158161

162+
// writeTraceRows stores a per-tx row, a per-block row and a last baked height at
163+
// height 7 in the trace DB under home.
164+
func writeTraceRows(t *testing.T, home string) {
165+
t.Helper()
166+
c, err := NewTraceDB(home)
167+
require.NoError(t, err)
168+
require.NoError(t, c.Put(7, "callTracer", common.HexToHash("0x07"), json.RawMessage(`{"calls":[]}`)))
169+
require.NoError(t, c.PutBlock(7, "callTracer", json.RawMessage(`[{"txHash":"0x07"}]`)))
170+
require.NoError(t, c.SetLastBakedHeight(7))
171+
require.NoError(t, c.Close())
172+
}
173+
174+
// setFormatStamp replaces the format stamp of the trace DB under home with stamp,
175+
// or removes it when stamp is nil, writing to pebble directly as another build would.
176+
func setFormatStamp(t *testing.T, home string, stamp []byte) {
177+
t.Helper()
178+
db, err := pebble.Open(filepath.Join(home, "data", "trace_db"), &pebble.Options{})
179+
require.NoError(t, err)
180+
if stamp == nil {
181+
require.NoError(t, db.Delete([]byte(traceDBFormatVersionKey), pebble.Sync))
182+
} else {
183+
require.NoError(t, db.Set([]byte(traceDBFormatVersionKey), stamp, pebble.Sync))
184+
}
185+
require.NoError(t, db.Close())
186+
}
187+
188+
func formatStamp(version uint64) []byte {
189+
var b [8]byte
190+
binary.BigEndian.PutUint64(b[:], version)
191+
return b[:]
192+
}
193+
194+
func TestTraceDBDiscardsRowsFromOtherFormatVersions(t *testing.T) {
195+
cases := []struct {
196+
name string
197+
stamp []byte
198+
}{
199+
{"no stamp", nil},
200+
{"other version", formatStamp(traceDBFormatVersion + 1)},
201+
{"malformed stamp", []byte{0x01}},
202+
}
203+
for _, tc := range cases {
204+
t.Run(tc.name, func(t *testing.T) {
205+
home := t.TempDir()
206+
writeTraceRows(t, home)
207+
setFormatStamp(t, home, tc.stamp)
208+
209+
c, err := NewTraceDB(home)
210+
require.NoError(t, err)
211+
212+
_, ok, err := c.Get(7, "callTracer", common.HexToHash("0x07"))
213+
require.NoError(t, err)
214+
require.False(t, ok, "per-tx row stored under another format version must not be served")
215+
_, ok, err = c.GetBlock(7, "callTracer")
216+
require.NoError(t, err)
217+
require.False(t, ok, "per-block row stored under another format version must not be served")
218+
last, err := c.LastBakedHeight()
219+
require.NoError(t, err)
220+
require.Equal(t, int64(0), last, "last baked height must reset so the baker bakes again")
221+
stored, err := c.storedFormatVersion()
222+
require.NoError(t, err)
223+
require.Equal(t, traceDBFormatVersion, stored)
224+
225+
// Rows baked after the reset are kept by the next open.
226+
require.NoError(t, c.Put(8, "callTracer", common.HexToHash("0x08"), json.RawMessage(`{"calls":[1]}`)))
227+
require.NoError(t, c.Close())
228+
c, err = NewTraceDB(home)
229+
require.NoError(t, err)
230+
defer c.Close()
231+
got, ok, err := c.Get(8, "callTracer", common.HexToHash("0x08"))
232+
require.NoError(t, err)
233+
require.True(t, ok)
234+
require.JSONEq(t, `{"calls":[1]}`, string(got))
235+
})
236+
}
237+
}
238+
239+
func TestTraceDBKeepsRowsOfCurrentFormatVersion(t *testing.T) {
240+
home := t.TempDir()
241+
writeTraceRows(t, home)
242+
243+
c, err := NewTraceDB(home)
244+
require.NoError(t, err)
245+
defer c.Close()
246+
247+
got, ok, err := c.Get(7, "callTracer", common.HexToHash("0x07"))
248+
require.NoError(t, err)
249+
require.True(t, ok)
250+
require.JSONEq(t, `{"calls":[]}`, string(got))
251+
got, ok, err = c.GetBlock(7, "callTracer")
252+
require.NoError(t, err)
253+
require.True(t, ok)
254+
require.JSONEq(t, `[{"txHash":"0x07"}]`, string(got))
255+
last, err := c.LastBakedHeight()
256+
require.NoError(t, err)
257+
require.Equal(t, int64(7), last)
258+
}
259+
260+
func TestTraceDBPruneSparesFormatStamp(t *testing.T) {
261+
home := t.TempDir()
262+
c, err := NewTraceDB(home)
263+
require.NoError(t, err)
264+
require.NoError(t, c.Put(1, "callTracer", common.HexToHash("0x1"), json.RawMessage(`"x"`)))
265+
require.NoError(t, c.Prune(1_000_000))
266+
require.NoError(t, c.Put(2_000_000, "callTracer", common.HexToHash("0x2"), json.RawMessage(`"y"`)))
267+
require.NoError(t, c.Close())
268+
269+
c, err = NewTraceDB(home)
270+
require.NoError(t, err)
271+
defer c.Close()
272+
_, ok, err := c.Get(2_000_000, "callTracer", common.HexToHash("0x2"))
273+
require.NoError(t, err)
274+
require.True(t, ok, "a pruned DB must still carry the format stamp, or its next open would discard its rows")
275+
}
276+
159277
type recordingEnqueuer struct{ heights atomic.Value }
160278

161279
func (r *recordingEnqueuer) Enqueue(h int64) {

0 commit comments

Comments
 (0)