From 283393b2dbc49704eacdf21d735a28d777dbf571 Mon Sep 17 00:00:00 2001 From: blindchaser Date: Fri, 13 Nov 2026 17:39:59 -0500 Subject: [PATCH 01/12] flatkv: bound the bytes an import holds before it flushes KVImporter now holds at most 256 MiB of key and value bytes that are not yet flushed. When the budget is full, AddNode waits for a flush to return bytes. Each worker also flushes when it holds budget/16 bytes, so the four workers together hold less than a quarter of the budget, and a node larger than half the budget is refused. A waiting AddNode therefore always waits on bytes that are in a channel or in a flush, and those bytes always come back, so the import cannot deadlock. The worker threshold is 16 MiB, a quarter of the default 64 MiB memtable. Pebble keeps a batch of half a memtable or more on the Go heap until it is written to disk, after the commit that returns its bytes to the budget. In an import of 512 MiB of 24 KiB rows, the peak live heap was 0.7 to 1.0 GiB with a 32 MiB threshold and 0.4 GiB with 16 MiB. An error broadcasts under the budget lock, so a waiting AddNode cannot miss it. A release that takes the count below zero fails the import instead of being hidden. Co-authored-by: Cursor --- .../state_db/sc/flatkv/import_export_test.go | 128 ++++++++++++++++++ sei-db/state_db/sc/flatkv/importer.go | 99 +++++++++++++- 2 files changed, 224 insertions(+), 3 deletions(-) diff --git a/sei-db/state_db/sc/flatkv/import_export_test.go b/sei-db/state_db/sc/flatkv/import_export_test.go index e01a4cfeed..b8b12fe234 100644 --- a/sei-db/state_db/sc/flatkv/import_export_test.go +++ b/sei-db/state_db/sc/flatkv/import_export_test.go @@ -1,10 +1,14 @@ package flatkv import ( + "bytes" "errors" + "fmt" "os" "path/filepath" + "sync" "testing" + "time" "github.com/stretchr/testify/require" @@ -302,6 +306,130 @@ func TestExporterEOAAccountOmitsCodeHash(t *testing.T) { require.Equal(t, &zeroHash, acct.GetCodeHash()) } +// withImportByteBudget makes importers built during the test use budget. +func withImportByteBudget(t *testing.T, budget int64) { + t.Helper() + importByteBudgetForTest.Store(budget) + t.Cleanup(func() { importByteBudgetForTest.Store(0) }) +} + +// budgetRow returns the misc row i of an import, with a key and value of keyLen+valueLen bytes in total. +func budgetRow(i int, valueLen int) *types.SnapshotNode { + return &types.SnapshotNode{ + Key: []byte(fmt.Sprintf("bank/k%05d", i)), + Value: bytes.Repeat([]byte{'v'}, valueLen), + Version: 1, + } +} + +func (imp *KVImporter) budgetInUse() int64 { + imp.budgetMu.Lock() + defer imp.budgetMu.Unlock() + return imp.budgetUsed +} + +func TestImporterRefusesNodeAboveHalfTheByteBudget(t *testing.T) { + withImportByteBudget(t, 8) + s, imp := newKVImporterForTest(t, 1) + defer s.Close() + + err := imp.AddNode(&types.SnapshotNode{Key: []byte("bank/a"), Value: []byte("v"), Version: 1}) + require.ErrorContains(t, err, "more than the 4-byte node limit") + require.Error(t, imp.Close()) +} + +// TestImporterFlushesOnBytesBeforeTheBudgetFills sends one worker far more bytes than the budget in far +// fewer rows than importBatchSize. A worker that flushed only on its pair count would hold the whole +// budget unflushed, and AddNode would wait forever. +func TestImporterFlushesOnBytesBeforeTheBudgetFills(t *testing.T) { + withImportByteBudget(t, 4096) + s, imp := newKVImporterForTest(t, 1) + defer s.Close() + + const rows = 1000 + added := make(chan error, 1) + go func() { + for i := range rows { + if err := imp.AddNode(budgetRow(i, 100)); err != nil { + added <- err + return + } + } + added <- nil + }() + select { + case err := <-added: + require.NoError(t, err) + case <-time.After(30 * time.Second): + t.Fatal("AddNode did not return: no worker flushed while the budget was full") + } + require.NoError(t, imp.Close()) + flushes, pairs := imp.importStats() + require.Equal(t, int64(rows), pairs) + require.Greater(t, flushes, int64(1)) +} + +// TestImporterByteBudgetBlocksWhileFlushesAreHeld holds every flush, so no bytes return to the budget. The +// producer then cannot add more rows than the budget holds, whatever the scheduling. +func TestImporterByteBudgetBlocksWhileFlushesAreHeld(t *testing.T) { + const budget, rowBytes = 1024, 32 + release := make(chan struct{}) + hook := func(string) { <-release } + flushHookForTest.Store(&hook) + t.Cleanup(func() { flushHookForTest.Store(nil) }) + withImportByteBudget(t, budget) + s, imp := newKVImporterForTest(t, 1) + defer s.Close() + releaseFlushes := sync.OnceFunc(func() { close(release) }) + // Runs before s.Close, which waits forever on a worker held in the flush hook. + defer releaseFlushes() + + const rows = 3 * budget / rowBytes + added := make(chan error, 1) + go func() { + for i := range rows { + if err := imp.AddNode(budgetRow(i, rowBytes-len("bank/k00000"))); err != nil { + added <- err + return + } + } + added <- nil + }() + + require.Eventually(t, func() bool { return imp.budgetInUse()+rowBytes > budget }, 10*time.Second, time.Millisecond) + select { + case err := <-added: + t.Fatalf("the producer added %d rows of %d bytes into a %d-byte budget with no flush done (err %v)", + rows, rowBytes, budget, err) + default: + } + + releaseFlushes() + select { + case err := <-added: + require.NoError(t, err) + case <-time.After(30 * time.Second): + t.Fatal("AddNode did not return after the flushes were released") + } + require.NoError(t, imp.Close()) + _, pairs := imp.importStats() + require.Equal(t, int64(rows), pairs) +} + +// TestImportWorkerFlushesAtMostAQuarterOfTheDefaultMemTable keeps a worker's batch below the size at which +// Pebble holds the batch itself in its flush queue, which is half a memtable. +func TestImportWorkerFlushesAtMostAQuarterOfTheDefaultMemTable(t *testing.T) { + require.LessOrEqual(t, 4*uint64(workerFlushBytes(importByteBudget)), pebbledb.DefaultConfig().MemTableSize) +} + +func TestImporterFailsWhenItReleasesMoreBytesThanItHolds(t *testing.T) { + s, imp := newKVImporterForTest(t, 1) + defer s.Close() + + imp.releaseBytes(1) + require.ErrorContains(t, imp.Close(), "after a release of 1 bytes") +} + func TestImportSurvivesReopen(t *testing.T) { src := setupTestStore(t) defer src.Close() diff --git a/sei-db/state_db/sc/flatkv/importer.go b/sei-db/state_db/sc/flatkv/importer.go index d579f20bc5..c63d8c9e18 100644 --- a/sei-db/state_db/sc/flatkv/importer.go +++ b/sei-db/state_db/sc/flatkv/importer.go @@ -21,10 +21,30 @@ const ( importBatchSize = 20000 ingestChanSize = 1 << 16 // 64K buffered main channel workerChanSize = 1024 // per-DB worker channel + // importByteBudget limits the key and value bytes that an import holds in memory before they are flushed. + importByteBudget = 256 << 20 ) var _ types.Importer = (*KVImporter)(nil) +// importByteBudgetForTest, when non-zero, replaces importByteBudget for importers built after it is set. +var importByteBudgetForTest atomic.Int64 + +// workerFlushBytes returns the unflushed byte count at which a worker flushes its batch. +func workerFlushBytes(budget int64) int64 { + // Pebble keeps a batch of half a memtable or more on the Go heap until it is written to disk, which is + // after the commit that returns its bytes to the budget. A sixteenth of the default budget is a quarter + // of the default memtable, so a batch is copied into a memtable instead. + return max(1, budget/(4*int64(len(dataDBDirs)))) +} + +// maxNodeBytes returns the largest node an import with budget accepts. +func maxNodeBytes(budget int64) int64 { + // The workers together hold less than budget/4 unflushed, so a node of at most budget/2 that waits for + // budget always waits on bytes that some worker will flush. + return budget / 2 +} + // flushHookForTest, when set by tests in this package, is invoked at the // start of every dbWorker flush. It exists solely for whitebox tests of // the backpressure / fail-fast paths (see importer_test.go) and loads @@ -65,6 +85,10 @@ type dbWorker struct { chunkSize uint32 flushes int64 pairs int64 + bytes int64 + // flushBytes is the unflushed byte count at which the worker flushes, whatever its pair count. + flushBytes int64 + release func(int64) } func newDBWorker( @@ -77,6 +101,8 @@ func newDBWorker( moduleLtHash map[string]*lthash.LtHash, moduleStats map[string]lthash.ModuleStats, chunkSize uint32, + flushBytes int64, + release func(int64), ) *dbWorker { return &dbWorker{ ctx: ctx, @@ -91,12 +117,14 @@ func newDBWorker( pool: pool, moduleOf: moduleOf, chunkSize: chunkSize, + flushBytes: flushBytes, + release: release, } } // run drains the worker channel until closed, flushing whenever the -// buffer reaches importBatchSize. If done fires, the worker abandons -// remaining work and exits immediately. +// buffer reaches importBatchSize pairs or flushBytes bytes. If done fires, +// the worker abandons remaining work and exits immediately. func (w *dbWorker) run(done <-chan struct{}) error { defer func() { if w.batch != nil { @@ -112,11 +140,12 @@ func (w *dbWorker) run(done <-chan struct{}) error { if err := w.batch.Set(kv.Key, kv.Value); err != nil { return fmt.Errorf("%s set: %w", w.dir, err) } + w.bytes += int64(len(kv.Key)) + int64(len(kv.Value)) w.ltMutations = append(w.ltMutations, mutation.Mutation{ Key: kv.Key, NewValue: kv.Value, }) - if len(w.ltMutations) >= importBatchSize { + if len(w.ltMutations) >= importBatchSize || w.bytes >= w.flushBytes { if err := w.flush(); err != nil { return err } @@ -132,6 +161,11 @@ func (w *dbWorker) flush() (err error) { if len(w.ltMutations) == 0 { return nil } + defer func(n int64) { + if w.release != nil { + w.release(n) + } + }(w.bytes) if hook := flushHookForTest.Load(); hook != nil { (*hook)(w.dir) } @@ -183,6 +217,7 @@ func (w *dbWorker) flush() (err error) { } w.batch = w.db.NewBatch() w.ltMutations = w.ltMutations[:0] + w.bytes = 0 return nil } @@ -209,6 +244,11 @@ type KVImporter struct { firstErr atomic.Pointer[error] finishOnce sync.Once finishErr error + + budgetMu sync.Mutex + budgetCond *sync.Cond + budgetUsed int64 + budget int64 } // NewKVImporter builds the import pipeline over dbs, the raw databases the import writes into. The handles @@ -216,6 +256,10 @@ type KVImporter struct { // must be handed the databases explicitly by whoever opened them. When requireAscendingKeys is set, the // import fails unless every physical key is strictly greater than the one before it. func NewKVImporter(store *CommitStore, version int64, dbs rawDBs, requireAscendingKeys bool) types.Importer { + budget := int64(importByteBudget) + if b := importByteBudgetForTest.Load(); b > 0 { + budget = b + } imp := &KVImporter{ store: store, version: version, @@ -223,7 +267,9 @@ func NewKVImporter(store *CommitStore, version int64, dbs rawDBs, requireAscendi ingestCh: make(chan rawKVPair, ingestChanSize), workers: make(map[string]*dbWorker, len(dataDBDirs)), done: make(chan struct{}), + budget: budget, } + imp.budgetCond = sync.NewCond(&imp.budgetMu) for _, dir := range dataDBDirs { w := newDBWorker( @@ -236,6 +282,8 @@ func NewKVImporter(store *CommitStore, version int64, dbs rawDBs, requireAscendi cloneModuleHashes(store.loadedHashes.PerModule[dir]), cloneModuleStats(store.loadedHashes.PerModuleStats[dir]), store.config.HashEngineConfig.ChunkSize, + workerFlushBytes(budget), + imp.releaseBytes, ) imp.workers[dir] = w } @@ -278,6 +326,7 @@ func (imp *KVImporter) dispatch() { } if imp.requireAscendingKeys { if err := checkAscending(prevKey, kv.Key); err != nil { + imp.releaseBytes(int64(len(kv.Key)) + int64(len(kv.Value))) imp.setErr(err) return } @@ -285,12 +334,14 @@ func (imp *KVImporter) dispatch() { } dir, err := routePhysicalKey(kv.Key) if err != nil { + imp.releaseBytes(int64(len(kv.Key)) + int64(len(kv.Value))) imp.setErr(fmt.Errorf("route key: %w", err)) return } select { case imp.workers[dir].ch <- kv: case <-imp.done: + imp.releaseBytes(int64(len(kv.Key)) + int64(len(kv.Value))) return } case <-imp.done: @@ -319,6 +370,11 @@ func checkAscending(prevKey []byte, key []byte) error { func (imp *KVImporter) setErr(err error) { if imp.firstErr.CompareAndSwap(nil, &err) { imp.closeOnce.Do(func() { close(imp.done) }) + // Broadcast under the lock, so a waiter in acquireBytes either sees the error before it waits or + // is already waiting. + imp.budgetMu.Lock() + imp.budgetCond.Broadcast() + imp.budgetMu.Unlock() } } @@ -348,15 +404,52 @@ func (imp *KVImporter) AddNode(node *types.SnapshotNode) error { imp.setErr(err) return err } + size := int64(len(node.Key)) + int64(len(node.Value)) + if err := imp.acquireBytes(size); err != nil { + imp.setErr(err) + return err + } + sent := false select { case imp.ingestCh <- rawKVPair{Key: node.Key, Value: node.Value}: + sent = true case <-imp.done: } + if !sent { + imp.releaseBytes(size) + } // A worker can fail while the send is pending, and select may still pick the send, so the failure is // read again here rather than reporting the node as accepted. return imp.getErr() } +func (imp *KVImporter) acquireBytes(n int64) error { + if limit := maxNodeBytes(imp.budget); n > limit { + return fmt.Errorf("flatkv import: node is %d bytes, more than the %d-byte node limit", n, limit) + } + imp.budgetMu.Lock() + defer imp.budgetMu.Unlock() + for imp.budgetUsed+n > imp.budget { + if err := imp.getErr(); err != nil { + return err + } + imp.budgetCond.Wait() + } + imp.budgetUsed += n + return nil +} + +func (imp *KVImporter) releaseBytes(n int64) { + imp.budgetMu.Lock() + imp.budgetUsed -= n + used := imp.budgetUsed + imp.budgetMu.Unlock() + imp.budgetCond.Broadcast() + if used < 0 { + imp.setErr(fmt.Errorf("flatkv import: %d bytes of the byte budget in use after a release of %d bytes", used, n)) + } +} + // checkNode returns an error unless node is a row this import can store. func (imp *KVImporter) checkNode(node *types.SnapshotNode) error { if node.Height != 0 { From cddb76763615a6aa356c4e688cf7bc923dd58179 Mon Sep 17 00:00:00 2001 From: blindchaser Date: Fri, 13 Nov 2026 11:00:07 -0500 Subject: [PATCH 02/12] flatkv: export and read a snapshot dir without opening a store ExportSnapshotDir streams the live rows of a closed snapshot dir in physical-key order, merging the four data databases. SnapshotDirStats returns what the snapshot metadata records: the row count, the key and value bytes, and the state hash, which is the checksum of the store-wide LtHash. The export counts the rows and bytes it emits, and fails when they differ from the metadata. ReadSnapshotMiscValue reads one non-EVM row. It fails when the dir has no misc database, so a wrong path is not read as a snapshot without the row. The state-sync snapshot tool uses these functions on a staged checkpoint where no CommitStore is open. Co-authored-by: Cursor --- .../state_db/sc/flatkv/import_export_test.go | 133 +++++++++ sei-db/state_db/sc/flatkv/snapshot_read.go | 264 ++++++++++++++++++ .../state_db/sc/flatkv/snapshot_read_test.go | 51 ++++ 3 files changed, 448 insertions(+) create mode 100644 sei-db/state_db/sc/flatkv/snapshot_read.go create mode 100644 sei-db/state_db/sc/flatkv/snapshot_read_test.go diff --git a/sei-db/state_db/sc/flatkv/import_export_test.go b/sei-db/state_db/sc/flatkv/import_export_test.go index b8b12fe234..63eab5334c 100644 --- a/sei-db/state_db/sc/flatkv/import_export_test.go +++ b/sei-db/state_db/sc/flatkv/import_export_test.go @@ -10,6 +10,7 @@ import ( "testing" "time" + "github.com/cockroachdb/pebble/v2" "github.com/stretchr/testify/require" errorutils "github.com/sei-protocol/sei-chain/sei-db/common/errors" @@ -306,6 +307,138 @@ func TestExporterEOAAccountOmitsCodeHash(t *testing.T) { require.Equal(t, &zeroHash, acct.GetCodeHash()) } +func TestExportSnapshotDirMatchesKVExporter(t *testing.T) { + s := setupTestStore(t) + defer s.Close() + + codeAddr := addrN(0x02) + require.NoError(t, s.ApplyChangeSets(s.Version()+1, []*proto.NamedChangeSet{ + {Name: "bank", Changeset: proto.ChangeSet{Pairs: []*proto.KVPair{ + {Key: []byte("balance/alice"), Value: []byte("100")}, + {Key: []byte("supply"), Value: []byte("100")}, + }}}, + {Name: "evm", Changeset: proto.ChangeSet{Pairs: []*proto.KVPair{ + {Key: keys.BuildEVMKey(keys.EVMKeyStorage, ktype.StorageKey(addrN(0x01), slotN(0x01))), Value: padLeft32(0x11)}, + {Key: keys.BuildEVMKey(keys.EVMKeyCode, codeAddr[:]), Value: []byte{0x60, 0x80}}, + }}}, + })) + commitAndCheck(t, s) + require.NoError(t, s.outOfBandSnapshot()) + + exp, err := s.Exporter(1) + require.NoError(t, err) + want := drainExporter(t, exp) + require.NoError(t, exp.Close()) + + var got []*types.SnapshotNode + stats, err := ExportSnapshotDir(t.Context(), filepath.Join(s.flatkvDir(), snapshotName(1)), 1, func(key, value []byte) error { + got = append(got, &types.SnapshotNode{ + Key: bytes.Clone(key), + Value: bytes.Clone(value), + Version: 1, + Height: 0, + }) + return nil + }) + require.NoError(t, err) + var wantBytes int64 + for _, n := range want { + wantBytes += int64(len(n.Key)) + int64(len(n.Value)) + } + require.Equal(t, SnapshotStats{ + RowCount: int64(len(want)), + Bytes: wantBytes, + StateHash: s.maintainedHashes().Global.Checksum(), + }, stats) + require.Equal(t, want, got) +} + +func TestImportedSnapshotRecordsTheStateHashOfItsRows(t *testing.T) { + s := setupTestStore(t) + defer s.Close() + + require.NoError(t, s.ApplyChangeSets(s.Version()+1, []*proto.NamedChangeSet{ + {Name: "bank", Changeset: proto.ChangeSet{Pairs: []*proto.KVPair{ + {Key: []byte("balance/alice"), Value: []byte("100")}, + {Key: []byte("supply"), Value: []byte("100")}, + }}}, + })) + commitAndCheck(t, s) + require.NoError(t, s.outOfBandSnapshot()) + src := filepath.Join(s.flatkvDir(), snapshotName(1)) + exported, err := SnapshotDirStats(src, 1) + require.NoError(t, err) + + reimport := func(t *testing.T, change func(key, value []byte)) SnapshotStats { + t.Helper() + dst := setupTestStore(t) + imp, err := dst.Importer(1) + require.NoError(t, err) + _, err = ExportSnapshotDir(t.Context(), src, 1, func(key, value []byte) error { + key, value = bytes.Clone(key), bytes.Clone(value) + change(key, value) + return imp.AddNode(&types.SnapshotNode{Key: key, Value: value, Version: 1}) + }) + require.NoError(t, err) + require.NoError(t, imp.Close()) + require.NoError(t, dst.Close()) + stats, err := SnapshotDirStats(filepath.Join(dst.flatkvDir(), snapshotName(1)), 1) + require.NoError(t, err) + return stats + } + + require.Equal(t, exported, reimport(t, func(_, _ []byte) {})) + + changed := reimport(t, func(key, value []byte) { + if bytes.HasSuffix(key, []byte("balance/alice")) { + value[len(value)-1] ^= 1 + } + }) + require.Equal(t, exported.RowCount, changed.RowCount) + require.Equal(t, exported.Bytes, changed.Bytes) + require.NotEqual(t, exported.StateHash, changed.StateHash) +} + +func TestExportSnapshotDirRefusesRowsThatDisagreeWithMetadata(t *testing.T) { + s := setupTestStore(t) + defer s.Close() + + require.NoError(t, s.ApplyChangeSets(s.Version()+1, []*proto.NamedChangeSet{ + {Name: "bank", Changeset: proto.ChangeSet{Pairs: []*proto.KVPair{ + {Key: []byte("balance/alice"), Value: []byte("100")}, + }}}, + })) + commitAndCheck(t, s) + require.NoError(t, s.outOfBandSnapshot()) + + dir := filepath.Join(s.flatkvDir(), snapshotName(1)) + db, err := pebble.Open(filepath.Join(dir, miscDBDir), &pebble.Options{FormatMajorVersion: pebble.FormatVirtualSSTables}) + require.NoError(t, err) + require.NoError(t, db.Set([]byte("bank/unrecorded"), []byte("1"), pebble.Sync)) + require.NoError(t, db.Close()) + + _, err = ExportSnapshotDir(t.Context(), dir, 1, func(_, _ []byte) error { return nil }) + require.ErrorContains(t, err, "its metadata records") +} + +func TestExportSnapshotDirRefusesVersionMismatch(t *testing.T) { + s := setupTestStore(t) + defer s.Close() + + require.NoError(t, s.ApplyChangeSets(s.Version()+1, []*proto.NamedChangeSet{ + {Name: "bank", Changeset: proto.ChangeSet{Pairs: []*proto.KVPair{ + {Key: []byte("balance/alice"), Value: []byte("100")}, + }}}, + })) + commitAndCheck(t, s) + require.NoError(t, s.outOfBandSnapshot()) + + _, err := ExportSnapshotDir(t.Context(), filepath.Join(s.flatkvDir(), snapshotName(1)), 2, func(_, _ []byte) error { + return nil + }) + require.ErrorContains(t, err, "records version 1, want 2") +} + // withImportByteBudget makes importers built during the test use budget. func withImportByteBudget(t *testing.T, budget int64) { t.Helper() diff --git a/sei-db/state_db/sc/flatkv/snapshot_read.go b/sei-db/state_db/sc/flatkv/snapshot_read.go new file mode 100644 index 0000000000..02e176b4ee --- /dev/null +++ b/sei-db/state_db/sc/flatkv/snapshot_read.go @@ -0,0 +1,264 @@ +package flatkv + +import ( + "bytes" + "context" + "errors" + "fmt" + "path/filepath" + + "github.com/cockroachdb/pebble/v2" + + "github.com/sei-protocol/sei-chain/sei-db/common/keys" + "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv/ktype" + "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv/lthash" + "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv/vtype" +) + +// SnapshotStats is the metadata a FlatKV snapshot records about its rows. +type SnapshotStats struct { + RowCount int64 + Bytes int64 + // StateHash is the checksum of the store-wide LtHash, which the app hash holds as its state hash. + StateHash [32]byte +} + +// SnapshotDirStats returns the stats that the metadata of snapshot dir records. +func SnapshotDirStats(dir string, version int64) (SnapshotStats, error) { + dbs, err := openSnapshotDataDBs(dir) + if err != nil { + return SnapshotStats{}, err + } + defer closeSnapshotDataDBs(dbs) + return snapshotStats(dbs, version) +} + +// ExportSnapshotDir emits the live non-metadata rows of snapshot dir in ascending physical-key order, and +// returns the stats that the snapshot metadata records. It returns an error if the count or key+value bytes +// of the emitted rows differ from those stats. emit must not keep key or value after it returns. +func ExportSnapshotDir(ctx context.Context, dir string, version int64, emit func(key, value []byte) error) (SnapshotStats, error) { + dbs, err := openSnapshotDataDBs(dir) + if err != nil { + return SnapshotStats{}, err + } + defer closeSnapshotDataDBs(dbs) + + recorded, err := snapshotStats(dbs, version) + if err != nil { + return SnapshotStats{}, err + } + + cursors, err := openSnapshotCursors(dbs) + if err != nil { + return SnapshotStats{}, err + } + defer closeSnapshotCursors(cursors) + + var emittedRows, emittedBytes int64 + for { + if err := ctx.Err(); err != nil { + return SnapshotStats{}, err + } + next := minSnapshotCursor(cursors) + if next == nil { + break + } + key := next.iter.Key() + value, err := next.iter.ValueAndErr() + if err != nil { + return SnapshotStats{}, fmt.Errorf("read %s value %x: %w", next.name, key, err) + } + emittedRows++ + emittedBytes += int64(len(key)) + int64(len(value)) + if err := emit(key, value); err != nil { + return SnapshotStats{}, err + } + next.iter.Next() + if err := next.err(); err != nil { + return SnapshotStats{}, err + } + next.skipMeta() + } + if emittedRows != recorded.RowCount || emittedBytes != recorded.Bytes { + return SnapshotStats{}, fmt.Errorf("%s holds %d rows and %d bytes, its metadata records %d rows and %d bytes", + dir, emittedRows, emittedBytes, recorded.RowCount, recorded.Bytes) + } + return recorded, nil +} + +type snapshotDataDB struct { + name string + db *pebble.DB +} + +func openSnapshotDataDBs(dir string) ([]snapshotDataDB, error) { + dbs := make([]snapshotDataDB, 0, len(dataDBDirs)) + for _, name := range dataDBDirs { + db, err := openSnapshotPebble(filepath.Join(dir, name)) + if err != nil { + closeSnapshotDataDBs(dbs) + return nil, fmt.Errorf("open %s database: %w", name, err) + } + dbs = append(dbs, snapshotDataDB{name: name, db: db}) + } + return dbs, nil +} + +func closeSnapshotDataDBs(dbs []snapshotDataDB) { + for _, db := range dbs { + _ = db.db.Close() + } +} + +func snapshotStats(dbs []snapshotDataDB, version int64) (SnapshotStats, error) { + var stats SnapshotStats + perDB := make(map[string]*lthash.LtHash, len(dbs)) + for _, db := range dbs { + meta, err := readSnapshotLocalMeta(db) + if err != nil { + return SnapshotStats{}, err + } + if meta.CommittedVersion != version { + return SnapshotStats{}, fmt.Errorf("%s records version %d, want %d", db.name, meta.CommittedVersion, version) + } + if meta.LtHash == nil { + return SnapshotStats{}, fmt.Errorf("%s records no LtHash", db.name) + } + if err := validatePerModuleMetadata(db.name, meta); err != nil { + return SnapshotStats{}, err + } + perDB[db.name] = meta.LtHash + for _, moduleStats := range meta.ModuleStats { + stats.RowCount += moduleStats.KeyCount + stats.Bytes += moduleStats.Bytes + } + } + stats.StateHash = lthash.SumDBHashes(dataDBDirs, perDB).Checksum() + return stats, nil +} + +func readSnapshotLocalMeta(db snapshotDataDB) (*LocalMeta, error) { + iter, err := db.db.NewIter(&pebble.IterOptions{ + LowerBound: ktype.MetaKeyPrefixBytes, + UpperBound: ktype.PrefixEnd(ktype.MetaKeyPrefixBytes), + }) + if err != nil { + return nil, fmt.Errorf("open metadata iterator: %w", err) + } + defer func() { _ = iter.Close() }() + + var decoder LocalMetaDecoder + for valid := iter.First(); valid; valid = iter.Next() { + value, err := iter.ValueAndErr() + if err != nil { + return nil, fmt.Errorf("read metadata value %x: %w", iter.Key(), err) + } + decoder.Add(iter.Key(), value) + } + if err := iter.Error(); err != nil { + return nil, fmt.Errorf("iterate metadata: %w", err) + } + return decoder.Finish() +} + +type snapshotCursor struct { + name string + iter *pebble.Iterator +} + +func openSnapshotCursors(dbs []snapshotDataDB) ([]*snapshotCursor, error) { + cursors := make([]*snapshotCursor, 0, len(dbs)) + for _, db := range dbs { + iter, err := db.db.NewIter(nil) + if err != nil { + closeSnapshotCursors(cursors) + return nil, fmt.Errorf("open %s iterator: %w", db.name, err) + } + c := &snapshotCursor{name: db.name, iter: iter} + iter.First() + if err := c.err(); err != nil { + _ = iter.Close() + closeSnapshotCursors(cursors) + return nil, err + } + c.skipMeta() + if err := c.err(); err != nil { + _ = iter.Close() + closeSnapshotCursors(cursors) + return nil, err + } + cursors = append(cursors, c) + } + return cursors, nil +} + +func closeSnapshotCursors(cursors []*snapshotCursor) { + for _, c := range cursors { + _ = c.iter.Close() + } +} + +func (c *snapshotCursor) skipMeta() { + for c.iter.Valid() && ktype.IsMetaKey(c.iter.Key()) { + c.iter.Next() + } +} + +func (c *snapshotCursor) err() error { + if err := c.iter.Error(); err != nil { + return fmt.Errorf("iterate %s database: %w", c.name, err) + } + return nil +} + +func minSnapshotCursor(cursors []*snapshotCursor) *snapshotCursor { + var min *snapshotCursor + for _, c := range cursors { + if !c.iter.Valid() { + continue + } + if min == nil || bytes.Compare(c.iter.Key(), min.iter.Key()) < 0 { + min = c + } + } + return min +} + +// ReadSnapshotMiscValue returns the value of key in the non-EVM module moduleName in the snapshot +// directory dir, and whether the snapshot holds that row. It opens the snapshot's misc database +// read-only and does not change its data files. It returns an error if dir has no misc database. +func ReadSnapshotMiscValue(dir, moduleName string, key []byte) ([]byte, bool, error) { + if moduleName == keys.EVMStoreKey { + return nil, false, fmt.Errorf("module %q is not a misc module", moduleName) + } + dbDir := filepath.Join(dir, miscDBDir) + db, err := openSnapshotPebble(dbDir) + if err != nil { + return nil, false, fmt.Errorf("open %q read-only: %w", dbDir, err) + } + defer func() { _ = db.Close() }() + + raw, closer, err := db.Get(ktype.BuildPhysicalKey(moduleName, key)) + if errors.Is(err, pebble.ErrNotFound) { + return nil, false, nil + } + if err != nil { + return nil, false, fmt.Errorf("read %s/%x in %q: %w", moduleName, key, dbDir, err) + } + defer func() { _ = closer.Close() }() + row, err := vtype.DeserializeMiscData(raw) + if err != nil { + return nil, false, fmt.Errorf("decode %s/%x in %q: %w", moduleName, key, dbDir, err) + } + if row.IsDelete() { + return nil, false, nil + } + return row.GetValue(), true, nil +} + +func openSnapshotPebble(dbDir string) (*pebble.DB, error) { + return pebble.Open(dbDir, &pebble.Options{ + ReadOnly: true, + FormatMajorVersion: pebble.FormatVirtualSSTables, + }) +} diff --git a/sei-db/state_db/sc/flatkv/snapshot_read_test.go b/sei-db/state_db/sc/flatkv/snapshot_read_test.go new file mode 100644 index 0000000000..15a65ad6d9 --- /dev/null +++ b/sei-db/state_db/sc/flatkv/snapshot_read_test.go @@ -0,0 +1,51 @@ +package flatkv + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/sei-protocol/sei-chain/sei-db/common/keys" + "github.com/sei-protocol/sei-chain/sei-db/proto" +) + +func TestReadSnapshotMiscValue(t *testing.T) { + s := setupSnapshottingStore(t, 10) + defer func() { require.NoError(t, s.Close()) }() + commitToEveryDB(t, s, 1) + commitToEveryDB(t, s, 2) + require.NoError(t, s.ApplyChangeSets(s.Version()+1, []*proto.NamedChangeSet{ + {Name: "bank", Changeset: proto.ChangeSet{Pairs: []*proto.KVPair{ + {Key: []byte{'b', 1}, Delete: true}, + {Key: []byte{'e'}, Value: []byte{}}, + }}}, + })) + h := commitAndCheck(t, s) + dir := filepath.Join(t.TempDir(), "linked") + require.NoError(t, HardLinkSnapshot(s.flatkvDir(), h, dir)) + + got, found, err := ReadSnapshotMiscValue(dir, "bank", []byte{'b', 2}) + require.NoError(t, err) + require.True(t, found) + require.Equal(t, []byte{2}, got) + + got, found, err = ReadSnapshotMiscValue(dir, "bank", []byte{'e'}) + require.NoError(t, err) + require.True(t, found, "an empty value is a row") + require.Empty(t, got) + + for name, key := range map[string][]byte{"deleted": {'b', 1}, "never written": {'b', 9}} { + _, found, err := ReadSnapshotMiscValue(dir, "bank", key) + require.NoError(t, err, name) + require.False(t, found, name) + } + + _, _, err = ReadSnapshotMiscValue(dir, keys.EVMStoreKey, []byte{1}) + require.Error(t, err) + + require.NoError(t, os.RemoveAll(filepath.Join(dir, miscDBDir))) + _, _, err = ReadSnapshotMiscValue(dir, "bank", []byte{'b', 2}) + require.ErrorContains(t, err, "read-only") +} From 0ddd4dcecf2b8e38a34f0f58578540787e2bd9cc Mon Sep 17 00:00:00 2001 From: blindchaser Date: Fri, 13 Nov 2026 17:39:59 -0500 Subject: [PATCH 03/12] flatkv: bound the bytes an import holds before it flushes KVImporter now holds at most 256 MiB of key and value bytes that are not yet flushed. When the budget is full, AddNode waits for a flush to return bytes. Each worker also flushes when it holds budget/16 bytes, so the four workers together hold less than a quarter of the budget, and a node larger than half the budget is refused. A waiting AddNode therefore always waits on bytes that are in a channel or in a flush, and those bytes always come back, so the import cannot deadlock. The worker threshold is 16 MiB, a quarter of the default 64 MiB memtable. Pebble keeps a batch of half a memtable or more on the Go heap until it is written to disk, after the commit that returns its bytes to the budget. In an import of 512 MiB of 24 KiB rows, the peak live heap was 0.7 to 1.0 GiB with a 32 MiB threshold and 0.4 GiB with 16 MiB. An error broadcasts under the budget lock, so a waiting AddNode cannot miss it. A release that takes the count below zero fails the import instead of being hidden. Co-authored-by: Cursor --- .../state_db/sc/flatkv/import_export_test.go | 128 ++++++++++++++++++ sei-db/state_db/sc/flatkv/importer.go | 99 +++++++++++++- 2 files changed, 224 insertions(+), 3 deletions(-) diff --git a/sei-db/state_db/sc/flatkv/import_export_test.go b/sei-db/state_db/sc/flatkv/import_export_test.go index e01a4cfeed..b8b12fe234 100644 --- a/sei-db/state_db/sc/flatkv/import_export_test.go +++ b/sei-db/state_db/sc/flatkv/import_export_test.go @@ -1,10 +1,14 @@ package flatkv import ( + "bytes" "errors" + "fmt" "os" "path/filepath" + "sync" "testing" + "time" "github.com/stretchr/testify/require" @@ -302,6 +306,130 @@ func TestExporterEOAAccountOmitsCodeHash(t *testing.T) { require.Equal(t, &zeroHash, acct.GetCodeHash()) } +// withImportByteBudget makes importers built during the test use budget. +func withImportByteBudget(t *testing.T, budget int64) { + t.Helper() + importByteBudgetForTest.Store(budget) + t.Cleanup(func() { importByteBudgetForTest.Store(0) }) +} + +// budgetRow returns the misc row i of an import, with a key and value of keyLen+valueLen bytes in total. +func budgetRow(i int, valueLen int) *types.SnapshotNode { + return &types.SnapshotNode{ + Key: []byte(fmt.Sprintf("bank/k%05d", i)), + Value: bytes.Repeat([]byte{'v'}, valueLen), + Version: 1, + } +} + +func (imp *KVImporter) budgetInUse() int64 { + imp.budgetMu.Lock() + defer imp.budgetMu.Unlock() + return imp.budgetUsed +} + +func TestImporterRefusesNodeAboveHalfTheByteBudget(t *testing.T) { + withImportByteBudget(t, 8) + s, imp := newKVImporterForTest(t, 1) + defer s.Close() + + err := imp.AddNode(&types.SnapshotNode{Key: []byte("bank/a"), Value: []byte("v"), Version: 1}) + require.ErrorContains(t, err, "more than the 4-byte node limit") + require.Error(t, imp.Close()) +} + +// TestImporterFlushesOnBytesBeforeTheBudgetFills sends one worker far more bytes than the budget in far +// fewer rows than importBatchSize. A worker that flushed only on its pair count would hold the whole +// budget unflushed, and AddNode would wait forever. +func TestImporterFlushesOnBytesBeforeTheBudgetFills(t *testing.T) { + withImportByteBudget(t, 4096) + s, imp := newKVImporterForTest(t, 1) + defer s.Close() + + const rows = 1000 + added := make(chan error, 1) + go func() { + for i := range rows { + if err := imp.AddNode(budgetRow(i, 100)); err != nil { + added <- err + return + } + } + added <- nil + }() + select { + case err := <-added: + require.NoError(t, err) + case <-time.After(30 * time.Second): + t.Fatal("AddNode did not return: no worker flushed while the budget was full") + } + require.NoError(t, imp.Close()) + flushes, pairs := imp.importStats() + require.Equal(t, int64(rows), pairs) + require.Greater(t, flushes, int64(1)) +} + +// TestImporterByteBudgetBlocksWhileFlushesAreHeld holds every flush, so no bytes return to the budget. The +// producer then cannot add more rows than the budget holds, whatever the scheduling. +func TestImporterByteBudgetBlocksWhileFlushesAreHeld(t *testing.T) { + const budget, rowBytes = 1024, 32 + release := make(chan struct{}) + hook := func(string) { <-release } + flushHookForTest.Store(&hook) + t.Cleanup(func() { flushHookForTest.Store(nil) }) + withImportByteBudget(t, budget) + s, imp := newKVImporterForTest(t, 1) + defer s.Close() + releaseFlushes := sync.OnceFunc(func() { close(release) }) + // Runs before s.Close, which waits forever on a worker held in the flush hook. + defer releaseFlushes() + + const rows = 3 * budget / rowBytes + added := make(chan error, 1) + go func() { + for i := range rows { + if err := imp.AddNode(budgetRow(i, rowBytes-len("bank/k00000"))); err != nil { + added <- err + return + } + } + added <- nil + }() + + require.Eventually(t, func() bool { return imp.budgetInUse()+rowBytes > budget }, 10*time.Second, time.Millisecond) + select { + case err := <-added: + t.Fatalf("the producer added %d rows of %d bytes into a %d-byte budget with no flush done (err %v)", + rows, rowBytes, budget, err) + default: + } + + releaseFlushes() + select { + case err := <-added: + require.NoError(t, err) + case <-time.After(30 * time.Second): + t.Fatal("AddNode did not return after the flushes were released") + } + require.NoError(t, imp.Close()) + _, pairs := imp.importStats() + require.Equal(t, int64(rows), pairs) +} + +// TestImportWorkerFlushesAtMostAQuarterOfTheDefaultMemTable keeps a worker's batch below the size at which +// Pebble holds the batch itself in its flush queue, which is half a memtable. +func TestImportWorkerFlushesAtMostAQuarterOfTheDefaultMemTable(t *testing.T) { + require.LessOrEqual(t, 4*uint64(workerFlushBytes(importByteBudget)), pebbledb.DefaultConfig().MemTableSize) +} + +func TestImporterFailsWhenItReleasesMoreBytesThanItHolds(t *testing.T) { + s, imp := newKVImporterForTest(t, 1) + defer s.Close() + + imp.releaseBytes(1) + require.ErrorContains(t, imp.Close(), "after a release of 1 bytes") +} + func TestImportSurvivesReopen(t *testing.T) { src := setupTestStore(t) defer src.Close() diff --git a/sei-db/state_db/sc/flatkv/importer.go b/sei-db/state_db/sc/flatkv/importer.go index d579f20bc5..c63d8c9e18 100644 --- a/sei-db/state_db/sc/flatkv/importer.go +++ b/sei-db/state_db/sc/flatkv/importer.go @@ -21,10 +21,30 @@ const ( importBatchSize = 20000 ingestChanSize = 1 << 16 // 64K buffered main channel workerChanSize = 1024 // per-DB worker channel + // importByteBudget limits the key and value bytes that an import holds in memory before they are flushed. + importByteBudget = 256 << 20 ) var _ types.Importer = (*KVImporter)(nil) +// importByteBudgetForTest, when non-zero, replaces importByteBudget for importers built after it is set. +var importByteBudgetForTest atomic.Int64 + +// workerFlushBytes returns the unflushed byte count at which a worker flushes its batch. +func workerFlushBytes(budget int64) int64 { + // Pebble keeps a batch of half a memtable or more on the Go heap until it is written to disk, which is + // after the commit that returns its bytes to the budget. A sixteenth of the default budget is a quarter + // of the default memtable, so a batch is copied into a memtable instead. + return max(1, budget/(4*int64(len(dataDBDirs)))) +} + +// maxNodeBytes returns the largest node an import with budget accepts. +func maxNodeBytes(budget int64) int64 { + // The workers together hold less than budget/4 unflushed, so a node of at most budget/2 that waits for + // budget always waits on bytes that some worker will flush. + return budget / 2 +} + // flushHookForTest, when set by tests in this package, is invoked at the // start of every dbWorker flush. It exists solely for whitebox tests of // the backpressure / fail-fast paths (see importer_test.go) and loads @@ -65,6 +85,10 @@ type dbWorker struct { chunkSize uint32 flushes int64 pairs int64 + bytes int64 + // flushBytes is the unflushed byte count at which the worker flushes, whatever its pair count. + flushBytes int64 + release func(int64) } func newDBWorker( @@ -77,6 +101,8 @@ func newDBWorker( moduleLtHash map[string]*lthash.LtHash, moduleStats map[string]lthash.ModuleStats, chunkSize uint32, + flushBytes int64, + release func(int64), ) *dbWorker { return &dbWorker{ ctx: ctx, @@ -91,12 +117,14 @@ func newDBWorker( pool: pool, moduleOf: moduleOf, chunkSize: chunkSize, + flushBytes: flushBytes, + release: release, } } // run drains the worker channel until closed, flushing whenever the -// buffer reaches importBatchSize. If done fires, the worker abandons -// remaining work and exits immediately. +// buffer reaches importBatchSize pairs or flushBytes bytes. If done fires, +// the worker abandons remaining work and exits immediately. func (w *dbWorker) run(done <-chan struct{}) error { defer func() { if w.batch != nil { @@ -112,11 +140,12 @@ func (w *dbWorker) run(done <-chan struct{}) error { if err := w.batch.Set(kv.Key, kv.Value); err != nil { return fmt.Errorf("%s set: %w", w.dir, err) } + w.bytes += int64(len(kv.Key)) + int64(len(kv.Value)) w.ltMutations = append(w.ltMutations, mutation.Mutation{ Key: kv.Key, NewValue: kv.Value, }) - if len(w.ltMutations) >= importBatchSize { + if len(w.ltMutations) >= importBatchSize || w.bytes >= w.flushBytes { if err := w.flush(); err != nil { return err } @@ -132,6 +161,11 @@ func (w *dbWorker) flush() (err error) { if len(w.ltMutations) == 0 { return nil } + defer func(n int64) { + if w.release != nil { + w.release(n) + } + }(w.bytes) if hook := flushHookForTest.Load(); hook != nil { (*hook)(w.dir) } @@ -183,6 +217,7 @@ func (w *dbWorker) flush() (err error) { } w.batch = w.db.NewBatch() w.ltMutations = w.ltMutations[:0] + w.bytes = 0 return nil } @@ -209,6 +244,11 @@ type KVImporter struct { firstErr atomic.Pointer[error] finishOnce sync.Once finishErr error + + budgetMu sync.Mutex + budgetCond *sync.Cond + budgetUsed int64 + budget int64 } // NewKVImporter builds the import pipeline over dbs, the raw databases the import writes into. The handles @@ -216,6 +256,10 @@ type KVImporter struct { // must be handed the databases explicitly by whoever opened them. When requireAscendingKeys is set, the // import fails unless every physical key is strictly greater than the one before it. func NewKVImporter(store *CommitStore, version int64, dbs rawDBs, requireAscendingKeys bool) types.Importer { + budget := int64(importByteBudget) + if b := importByteBudgetForTest.Load(); b > 0 { + budget = b + } imp := &KVImporter{ store: store, version: version, @@ -223,7 +267,9 @@ func NewKVImporter(store *CommitStore, version int64, dbs rawDBs, requireAscendi ingestCh: make(chan rawKVPair, ingestChanSize), workers: make(map[string]*dbWorker, len(dataDBDirs)), done: make(chan struct{}), + budget: budget, } + imp.budgetCond = sync.NewCond(&imp.budgetMu) for _, dir := range dataDBDirs { w := newDBWorker( @@ -236,6 +282,8 @@ func NewKVImporter(store *CommitStore, version int64, dbs rawDBs, requireAscendi cloneModuleHashes(store.loadedHashes.PerModule[dir]), cloneModuleStats(store.loadedHashes.PerModuleStats[dir]), store.config.HashEngineConfig.ChunkSize, + workerFlushBytes(budget), + imp.releaseBytes, ) imp.workers[dir] = w } @@ -278,6 +326,7 @@ func (imp *KVImporter) dispatch() { } if imp.requireAscendingKeys { if err := checkAscending(prevKey, kv.Key); err != nil { + imp.releaseBytes(int64(len(kv.Key)) + int64(len(kv.Value))) imp.setErr(err) return } @@ -285,12 +334,14 @@ func (imp *KVImporter) dispatch() { } dir, err := routePhysicalKey(kv.Key) if err != nil { + imp.releaseBytes(int64(len(kv.Key)) + int64(len(kv.Value))) imp.setErr(fmt.Errorf("route key: %w", err)) return } select { case imp.workers[dir].ch <- kv: case <-imp.done: + imp.releaseBytes(int64(len(kv.Key)) + int64(len(kv.Value))) return } case <-imp.done: @@ -319,6 +370,11 @@ func checkAscending(prevKey []byte, key []byte) error { func (imp *KVImporter) setErr(err error) { if imp.firstErr.CompareAndSwap(nil, &err) { imp.closeOnce.Do(func() { close(imp.done) }) + // Broadcast under the lock, so a waiter in acquireBytes either sees the error before it waits or + // is already waiting. + imp.budgetMu.Lock() + imp.budgetCond.Broadcast() + imp.budgetMu.Unlock() } } @@ -348,15 +404,52 @@ func (imp *KVImporter) AddNode(node *types.SnapshotNode) error { imp.setErr(err) return err } + size := int64(len(node.Key)) + int64(len(node.Value)) + if err := imp.acquireBytes(size); err != nil { + imp.setErr(err) + return err + } + sent := false select { case imp.ingestCh <- rawKVPair{Key: node.Key, Value: node.Value}: + sent = true case <-imp.done: } + if !sent { + imp.releaseBytes(size) + } // A worker can fail while the send is pending, and select may still pick the send, so the failure is // read again here rather than reporting the node as accepted. return imp.getErr() } +func (imp *KVImporter) acquireBytes(n int64) error { + if limit := maxNodeBytes(imp.budget); n > limit { + return fmt.Errorf("flatkv import: node is %d bytes, more than the %d-byte node limit", n, limit) + } + imp.budgetMu.Lock() + defer imp.budgetMu.Unlock() + for imp.budgetUsed+n > imp.budget { + if err := imp.getErr(); err != nil { + return err + } + imp.budgetCond.Wait() + } + imp.budgetUsed += n + return nil +} + +func (imp *KVImporter) releaseBytes(n int64) { + imp.budgetMu.Lock() + imp.budgetUsed -= n + used := imp.budgetUsed + imp.budgetMu.Unlock() + imp.budgetCond.Broadcast() + if used < 0 { + imp.setErr(fmt.Errorf("flatkv import: %d bytes of the byte budget in use after a release of %d bytes", used, n)) + } +} + // checkNode returns an error unless node is a row this import can store. func (imp *KVImporter) checkNode(node *types.SnapshotNode) error { if node.Height != 0 { From b2967de5acf0e0682f731e29c9d6e29682dae187 Mon Sep 17 00:00:00 2001 From: blindchaser Date: Fri, 13 Nov 2026 11:00:07 -0500 Subject: [PATCH 04/12] flatkv: export and read a snapshot dir without opening a store ExportSnapshotDir streams the live rows of a closed snapshot dir in physical-key order, merging the four data databases. SnapshotDirStats returns what the snapshot metadata records: the row count, the key and value bytes, and the state hash, which is the checksum of the store-wide LtHash. The export counts the rows and bytes it emits, and fails when they differ from the metadata. ReadSnapshotMiscValue reads one non-EVM row. It fails when the dir has no misc database, so a wrong path is not read as a snapshot without the row. The state-sync snapshot tool uses these functions on a staged checkpoint where no CommitStore is open. Co-authored-by: Cursor --- .../state_db/sc/flatkv/import_export_test.go | 133 +++++++++ sei-db/state_db/sc/flatkv/snapshot_read.go | 264 ++++++++++++++++++ .../state_db/sc/flatkv/snapshot_read_test.go | 51 ++++ 3 files changed, 448 insertions(+) create mode 100644 sei-db/state_db/sc/flatkv/snapshot_read.go create mode 100644 sei-db/state_db/sc/flatkv/snapshot_read_test.go diff --git a/sei-db/state_db/sc/flatkv/import_export_test.go b/sei-db/state_db/sc/flatkv/import_export_test.go index b8b12fe234..63eab5334c 100644 --- a/sei-db/state_db/sc/flatkv/import_export_test.go +++ b/sei-db/state_db/sc/flatkv/import_export_test.go @@ -10,6 +10,7 @@ import ( "testing" "time" + "github.com/cockroachdb/pebble/v2" "github.com/stretchr/testify/require" errorutils "github.com/sei-protocol/sei-chain/sei-db/common/errors" @@ -306,6 +307,138 @@ func TestExporterEOAAccountOmitsCodeHash(t *testing.T) { require.Equal(t, &zeroHash, acct.GetCodeHash()) } +func TestExportSnapshotDirMatchesKVExporter(t *testing.T) { + s := setupTestStore(t) + defer s.Close() + + codeAddr := addrN(0x02) + require.NoError(t, s.ApplyChangeSets(s.Version()+1, []*proto.NamedChangeSet{ + {Name: "bank", Changeset: proto.ChangeSet{Pairs: []*proto.KVPair{ + {Key: []byte("balance/alice"), Value: []byte("100")}, + {Key: []byte("supply"), Value: []byte("100")}, + }}}, + {Name: "evm", Changeset: proto.ChangeSet{Pairs: []*proto.KVPair{ + {Key: keys.BuildEVMKey(keys.EVMKeyStorage, ktype.StorageKey(addrN(0x01), slotN(0x01))), Value: padLeft32(0x11)}, + {Key: keys.BuildEVMKey(keys.EVMKeyCode, codeAddr[:]), Value: []byte{0x60, 0x80}}, + }}}, + })) + commitAndCheck(t, s) + require.NoError(t, s.outOfBandSnapshot()) + + exp, err := s.Exporter(1) + require.NoError(t, err) + want := drainExporter(t, exp) + require.NoError(t, exp.Close()) + + var got []*types.SnapshotNode + stats, err := ExportSnapshotDir(t.Context(), filepath.Join(s.flatkvDir(), snapshotName(1)), 1, func(key, value []byte) error { + got = append(got, &types.SnapshotNode{ + Key: bytes.Clone(key), + Value: bytes.Clone(value), + Version: 1, + Height: 0, + }) + return nil + }) + require.NoError(t, err) + var wantBytes int64 + for _, n := range want { + wantBytes += int64(len(n.Key)) + int64(len(n.Value)) + } + require.Equal(t, SnapshotStats{ + RowCount: int64(len(want)), + Bytes: wantBytes, + StateHash: s.maintainedHashes().Global.Checksum(), + }, stats) + require.Equal(t, want, got) +} + +func TestImportedSnapshotRecordsTheStateHashOfItsRows(t *testing.T) { + s := setupTestStore(t) + defer s.Close() + + require.NoError(t, s.ApplyChangeSets(s.Version()+1, []*proto.NamedChangeSet{ + {Name: "bank", Changeset: proto.ChangeSet{Pairs: []*proto.KVPair{ + {Key: []byte("balance/alice"), Value: []byte("100")}, + {Key: []byte("supply"), Value: []byte("100")}, + }}}, + })) + commitAndCheck(t, s) + require.NoError(t, s.outOfBandSnapshot()) + src := filepath.Join(s.flatkvDir(), snapshotName(1)) + exported, err := SnapshotDirStats(src, 1) + require.NoError(t, err) + + reimport := func(t *testing.T, change func(key, value []byte)) SnapshotStats { + t.Helper() + dst := setupTestStore(t) + imp, err := dst.Importer(1) + require.NoError(t, err) + _, err = ExportSnapshotDir(t.Context(), src, 1, func(key, value []byte) error { + key, value = bytes.Clone(key), bytes.Clone(value) + change(key, value) + return imp.AddNode(&types.SnapshotNode{Key: key, Value: value, Version: 1}) + }) + require.NoError(t, err) + require.NoError(t, imp.Close()) + require.NoError(t, dst.Close()) + stats, err := SnapshotDirStats(filepath.Join(dst.flatkvDir(), snapshotName(1)), 1) + require.NoError(t, err) + return stats + } + + require.Equal(t, exported, reimport(t, func(_, _ []byte) {})) + + changed := reimport(t, func(key, value []byte) { + if bytes.HasSuffix(key, []byte("balance/alice")) { + value[len(value)-1] ^= 1 + } + }) + require.Equal(t, exported.RowCount, changed.RowCount) + require.Equal(t, exported.Bytes, changed.Bytes) + require.NotEqual(t, exported.StateHash, changed.StateHash) +} + +func TestExportSnapshotDirRefusesRowsThatDisagreeWithMetadata(t *testing.T) { + s := setupTestStore(t) + defer s.Close() + + require.NoError(t, s.ApplyChangeSets(s.Version()+1, []*proto.NamedChangeSet{ + {Name: "bank", Changeset: proto.ChangeSet{Pairs: []*proto.KVPair{ + {Key: []byte("balance/alice"), Value: []byte("100")}, + }}}, + })) + commitAndCheck(t, s) + require.NoError(t, s.outOfBandSnapshot()) + + dir := filepath.Join(s.flatkvDir(), snapshotName(1)) + db, err := pebble.Open(filepath.Join(dir, miscDBDir), &pebble.Options{FormatMajorVersion: pebble.FormatVirtualSSTables}) + require.NoError(t, err) + require.NoError(t, db.Set([]byte("bank/unrecorded"), []byte("1"), pebble.Sync)) + require.NoError(t, db.Close()) + + _, err = ExportSnapshotDir(t.Context(), dir, 1, func(_, _ []byte) error { return nil }) + require.ErrorContains(t, err, "its metadata records") +} + +func TestExportSnapshotDirRefusesVersionMismatch(t *testing.T) { + s := setupTestStore(t) + defer s.Close() + + require.NoError(t, s.ApplyChangeSets(s.Version()+1, []*proto.NamedChangeSet{ + {Name: "bank", Changeset: proto.ChangeSet{Pairs: []*proto.KVPair{ + {Key: []byte("balance/alice"), Value: []byte("100")}, + }}}, + })) + commitAndCheck(t, s) + require.NoError(t, s.outOfBandSnapshot()) + + _, err := ExportSnapshotDir(t.Context(), filepath.Join(s.flatkvDir(), snapshotName(1)), 2, func(_, _ []byte) error { + return nil + }) + require.ErrorContains(t, err, "records version 1, want 2") +} + // withImportByteBudget makes importers built during the test use budget. func withImportByteBudget(t *testing.T, budget int64) { t.Helper() diff --git a/sei-db/state_db/sc/flatkv/snapshot_read.go b/sei-db/state_db/sc/flatkv/snapshot_read.go new file mode 100644 index 0000000000..02e176b4ee --- /dev/null +++ b/sei-db/state_db/sc/flatkv/snapshot_read.go @@ -0,0 +1,264 @@ +package flatkv + +import ( + "bytes" + "context" + "errors" + "fmt" + "path/filepath" + + "github.com/cockroachdb/pebble/v2" + + "github.com/sei-protocol/sei-chain/sei-db/common/keys" + "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv/ktype" + "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv/lthash" + "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv/vtype" +) + +// SnapshotStats is the metadata a FlatKV snapshot records about its rows. +type SnapshotStats struct { + RowCount int64 + Bytes int64 + // StateHash is the checksum of the store-wide LtHash, which the app hash holds as its state hash. + StateHash [32]byte +} + +// SnapshotDirStats returns the stats that the metadata of snapshot dir records. +func SnapshotDirStats(dir string, version int64) (SnapshotStats, error) { + dbs, err := openSnapshotDataDBs(dir) + if err != nil { + return SnapshotStats{}, err + } + defer closeSnapshotDataDBs(dbs) + return snapshotStats(dbs, version) +} + +// ExportSnapshotDir emits the live non-metadata rows of snapshot dir in ascending physical-key order, and +// returns the stats that the snapshot metadata records. It returns an error if the count or key+value bytes +// of the emitted rows differ from those stats. emit must not keep key or value after it returns. +func ExportSnapshotDir(ctx context.Context, dir string, version int64, emit func(key, value []byte) error) (SnapshotStats, error) { + dbs, err := openSnapshotDataDBs(dir) + if err != nil { + return SnapshotStats{}, err + } + defer closeSnapshotDataDBs(dbs) + + recorded, err := snapshotStats(dbs, version) + if err != nil { + return SnapshotStats{}, err + } + + cursors, err := openSnapshotCursors(dbs) + if err != nil { + return SnapshotStats{}, err + } + defer closeSnapshotCursors(cursors) + + var emittedRows, emittedBytes int64 + for { + if err := ctx.Err(); err != nil { + return SnapshotStats{}, err + } + next := minSnapshotCursor(cursors) + if next == nil { + break + } + key := next.iter.Key() + value, err := next.iter.ValueAndErr() + if err != nil { + return SnapshotStats{}, fmt.Errorf("read %s value %x: %w", next.name, key, err) + } + emittedRows++ + emittedBytes += int64(len(key)) + int64(len(value)) + if err := emit(key, value); err != nil { + return SnapshotStats{}, err + } + next.iter.Next() + if err := next.err(); err != nil { + return SnapshotStats{}, err + } + next.skipMeta() + } + if emittedRows != recorded.RowCount || emittedBytes != recorded.Bytes { + return SnapshotStats{}, fmt.Errorf("%s holds %d rows and %d bytes, its metadata records %d rows and %d bytes", + dir, emittedRows, emittedBytes, recorded.RowCount, recorded.Bytes) + } + return recorded, nil +} + +type snapshotDataDB struct { + name string + db *pebble.DB +} + +func openSnapshotDataDBs(dir string) ([]snapshotDataDB, error) { + dbs := make([]snapshotDataDB, 0, len(dataDBDirs)) + for _, name := range dataDBDirs { + db, err := openSnapshotPebble(filepath.Join(dir, name)) + if err != nil { + closeSnapshotDataDBs(dbs) + return nil, fmt.Errorf("open %s database: %w", name, err) + } + dbs = append(dbs, snapshotDataDB{name: name, db: db}) + } + return dbs, nil +} + +func closeSnapshotDataDBs(dbs []snapshotDataDB) { + for _, db := range dbs { + _ = db.db.Close() + } +} + +func snapshotStats(dbs []snapshotDataDB, version int64) (SnapshotStats, error) { + var stats SnapshotStats + perDB := make(map[string]*lthash.LtHash, len(dbs)) + for _, db := range dbs { + meta, err := readSnapshotLocalMeta(db) + if err != nil { + return SnapshotStats{}, err + } + if meta.CommittedVersion != version { + return SnapshotStats{}, fmt.Errorf("%s records version %d, want %d", db.name, meta.CommittedVersion, version) + } + if meta.LtHash == nil { + return SnapshotStats{}, fmt.Errorf("%s records no LtHash", db.name) + } + if err := validatePerModuleMetadata(db.name, meta); err != nil { + return SnapshotStats{}, err + } + perDB[db.name] = meta.LtHash + for _, moduleStats := range meta.ModuleStats { + stats.RowCount += moduleStats.KeyCount + stats.Bytes += moduleStats.Bytes + } + } + stats.StateHash = lthash.SumDBHashes(dataDBDirs, perDB).Checksum() + return stats, nil +} + +func readSnapshotLocalMeta(db snapshotDataDB) (*LocalMeta, error) { + iter, err := db.db.NewIter(&pebble.IterOptions{ + LowerBound: ktype.MetaKeyPrefixBytes, + UpperBound: ktype.PrefixEnd(ktype.MetaKeyPrefixBytes), + }) + if err != nil { + return nil, fmt.Errorf("open metadata iterator: %w", err) + } + defer func() { _ = iter.Close() }() + + var decoder LocalMetaDecoder + for valid := iter.First(); valid; valid = iter.Next() { + value, err := iter.ValueAndErr() + if err != nil { + return nil, fmt.Errorf("read metadata value %x: %w", iter.Key(), err) + } + decoder.Add(iter.Key(), value) + } + if err := iter.Error(); err != nil { + return nil, fmt.Errorf("iterate metadata: %w", err) + } + return decoder.Finish() +} + +type snapshotCursor struct { + name string + iter *pebble.Iterator +} + +func openSnapshotCursors(dbs []snapshotDataDB) ([]*snapshotCursor, error) { + cursors := make([]*snapshotCursor, 0, len(dbs)) + for _, db := range dbs { + iter, err := db.db.NewIter(nil) + if err != nil { + closeSnapshotCursors(cursors) + return nil, fmt.Errorf("open %s iterator: %w", db.name, err) + } + c := &snapshotCursor{name: db.name, iter: iter} + iter.First() + if err := c.err(); err != nil { + _ = iter.Close() + closeSnapshotCursors(cursors) + return nil, err + } + c.skipMeta() + if err := c.err(); err != nil { + _ = iter.Close() + closeSnapshotCursors(cursors) + return nil, err + } + cursors = append(cursors, c) + } + return cursors, nil +} + +func closeSnapshotCursors(cursors []*snapshotCursor) { + for _, c := range cursors { + _ = c.iter.Close() + } +} + +func (c *snapshotCursor) skipMeta() { + for c.iter.Valid() && ktype.IsMetaKey(c.iter.Key()) { + c.iter.Next() + } +} + +func (c *snapshotCursor) err() error { + if err := c.iter.Error(); err != nil { + return fmt.Errorf("iterate %s database: %w", c.name, err) + } + return nil +} + +func minSnapshotCursor(cursors []*snapshotCursor) *snapshotCursor { + var min *snapshotCursor + for _, c := range cursors { + if !c.iter.Valid() { + continue + } + if min == nil || bytes.Compare(c.iter.Key(), min.iter.Key()) < 0 { + min = c + } + } + return min +} + +// ReadSnapshotMiscValue returns the value of key in the non-EVM module moduleName in the snapshot +// directory dir, and whether the snapshot holds that row. It opens the snapshot's misc database +// read-only and does not change its data files. It returns an error if dir has no misc database. +func ReadSnapshotMiscValue(dir, moduleName string, key []byte) ([]byte, bool, error) { + if moduleName == keys.EVMStoreKey { + return nil, false, fmt.Errorf("module %q is not a misc module", moduleName) + } + dbDir := filepath.Join(dir, miscDBDir) + db, err := openSnapshotPebble(dbDir) + if err != nil { + return nil, false, fmt.Errorf("open %q read-only: %w", dbDir, err) + } + defer func() { _ = db.Close() }() + + raw, closer, err := db.Get(ktype.BuildPhysicalKey(moduleName, key)) + if errors.Is(err, pebble.ErrNotFound) { + return nil, false, nil + } + if err != nil { + return nil, false, fmt.Errorf("read %s/%x in %q: %w", moduleName, key, dbDir, err) + } + defer func() { _ = closer.Close() }() + row, err := vtype.DeserializeMiscData(raw) + if err != nil { + return nil, false, fmt.Errorf("decode %s/%x in %q: %w", moduleName, key, dbDir, err) + } + if row.IsDelete() { + return nil, false, nil + } + return row.GetValue(), true, nil +} + +func openSnapshotPebble(dbDir string) (*pebble.DB, error) { + return pebble.Open(dbDir, &pebble.Options{ + ReadOnly: true, + FormatMajorVersion: pebble.FormatVirtualSSTables, + }) +} diff --git a/sei-db/state_db/sc/flatkv/snapshot_read_test.go b/sei-db/state_db/sc/flatkv/snapshot_read_test.go new file mode 100644 index 0000000000..15a65ad6d9 --- /dev/null +++ b/sei-db/state_db/sc/flatkv/snapshot_read_test.go @@ -0,0 +1,51 @@ +package flatkv + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/sei-protocol/sei-chain/sei-db/common/keys" + "github.com/sei-protocol/sei-chain/sei-db/proto" +) + +func TestReadSnapshotMiscValue(t *testing.T) { + s := setupSnapshottingStore(t, 10) + defer func() { require.NoError(t, s.Close()) }() + commitToEveryDB(t, s, 1) + commitToEveryDB(t, s, 2) + require.NoError(t, s.ApplyChangeSets(s.Version()+1, []*proto.NamedChangeSet{ + {Name: "bank", Changeset: proto.ChangeSet{Pairs: []*proto.KVPair{ + {Key: []byte{'b', 1}, Delete: true}, + {Key: []byte{'e'}, Value: []byte{}}, + }}}, + })) + h := commitAndCheck(t, s) + dir := filepath.Join(t.TempDir(), "linked") + require.NoError(t, HardLinkSnapshot(s.flatkvDir(), h, dir)) + + got, found, err := ReadSnapshotMiscValue(dir, "bank", []byte{'b', 2}) + require.NoError(t, err) + require.True(t, found) + require.Equal(t, []byte{2}, got) + + got, found, err = ReadSnapshotMiscValue(dir, "bank", []byte{'e'}) + require.NoError(t, err) + require.True(t, found, "an empty value is a row") + require.Empty(t, got) + + for name, key := range map[string][]byte{"deleted": {'b', 1}, "never written": {'b', 9}} { + _, found, err := ReadSnapshotMiscValue(dir, "bank", key) + require.NoError(t, err, name) + require.False(t, found, name) + } + + _, _, err = ReadSnapshotMiscValue(dir, keys.EVMStoreKey, []byte{1}) + require.Error(t, err) + + require.NoError(t, os.RemoveAll(filepath.Join(dir, miscDBDir))) + _, _, err = ReadSnapshotMiscValue(dir, "bank", []byte{'b', 2}) + require.ErrorContains(t, err, "read-only") +} From 691faa4554cf6f64d271b5c05a9031c84dec055b Mon Sep 17 00:00:00 2001 From: blindchaser Date: Tue, 10 Nov 2026 17:02:52 -0500 Subject: [PATCH 05/12] autobahn: list every store dir in the layout, and require the layout to cover them The layout now lists the block DB, receipt store, state store and app-hash vault dirs beside FlatKV, so state sync can require them empty on restore. It takes the storage config and the genesis doc, because the storage config requires the initial height. The new test opens the storage manager the way node startup does, and fails on any entry under the persistent state dir that the layout does not cover. Co-authored-by: Cursor --- sei-tendermint/node/autobahn_layout.go | 28 +++++--- sei-tendermint/node/autobahn_layout_test.go | 73 +++++++++++++++++++-- sei-tendermint/node/setup.go | 22 +++++-- 3 files changed, 107 insertions(+), 16 deletions(-) diff --git a/sei-tendermint/node/autobahn_layout.go b/sei-tendermint/node/autobahn_layout.go index 7021fff844..2ccb81c76c 100644 --- a/sei-tendermint/node/autobahn_layout.go +++ b/sei-tendermint/node/autobahn_layout.go @@ -1,35 +1,47 @@ package node import ( + gigaconfig "github.com/sei-protocol/sei-chain/giga/config" dbutils "github.com/sei-protocol/sei-chain/sei-db/common/utils" "github.com/sei-protocol/sei-chain/sei-tendermint/config" "github.com/sei-protocol/sei-chain/sei-tendermint/internal/p2p" + "github.com/sei-protocol/sei-chain/sei-tendermint/types" ) -// AutobahnLayout lists the paths under a node's Autobahn persistent state dir that state -// sync reads and writes. The node creates every other store under PersistentStateDir on -// start. +// AutobahnLayout lists the on-disk paths of a node's Autobahn state that state +// sync reads, writes or requires to be empty. It does not list the validator +// consensus files under PersistentStateDir (inner_a.pb, inner_b.pb, blocks/ +// and commitqcs/). type AutobahnLayout struct { PersistentStateDir string FlatKVDir string + BlockDBDirs []string + ReceiptStoreDir string + StateStoreDir string + HashVaultDir string RestoreMarkerFile string StateSyncProofDir string } -// ResolveAutobahnLayout returns the Autobahn layout of the node that conf configures, with -// the paths node startup opens. It creates the persistent state dir if it does not exist. -func ResolveAutobahnLayout(conf *config.Config) (AutobahnLayout, error) { +// ResolveAutobahnLayout returns the Autobahn layout of the node that conf, storage and +// genDoc configure, with the paths node startup opens. It creates the persistent state dir +// if it does not exist. +func ResolveAutobahnLayout(conf *config.Config, storage gigaconfig.StorageConfig, genDoc *types.GenesisDoc) (AutobahnLayout, error) { fc, _, err := loadAutobahnCommittee(conf.AutobahnConfigFile) if err != nil { return AutobahnLayout{}, err } - psd, err := resolvePersistentStateDir(conf.RootDir, fc.PersistentStateDir) + psd, storageConfig, err := autobahnStorageConfig(conf.RootDir, conf.Mode, fc, storage, genDoc) if err != nil { return AutobahnLayout{}, err } return AutobahnLayout{ PersistentStateDir: psd, - FlatKVDir: dbutils.GetFlatKVPath(psd), + FlatKVDir: storageConfig.FlatKVConfig.DataDir, + BlockDBDirs: storageConfig.BlockDBConfig.Litt.Paths, + ReceiptStoreDir: storageConfig.ReceiptDBConfig.DBDirectory, + StateStoreDir: storageConfig.SSConfig.EVMDBDirectory, + HashVaultDir: storageConfig.AppHashBuilderConfig.HashVaultConfig.Path, RestoreMarkerFile: p2p.RestoreMarkerFile(psd), StateSyncProofDir: dbutils.GetStateSyncProofPath(psd), }, nil diff --git a/sei-tendermint/node/autobahn_layout_test.go b/sei-tendermint/node/autobahn_layout_test.go index 392e6082d1..0ca0071e84 100644 --- a/sei-tendermint/node/autobahn_layout_test.go +++ b/sei-tendermint/node/autobahn_layout_test.go @@ -1,10 +1,14 @@ package node import ( + "io/fs" "os" "path/filepath" + "slices" + "strings" "testing" + gigaconfig "github.com/sei-protocol/sei-chain/giga/config" seidbconfig "github.com/sei-protocol/sei-chain/sei-db/config" "github.com/sei-protocol/sei-chain/sei-tendermint/config" "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils" @@ -28,7 +32,7 @@ func layoutConfig(t *testing.T, psd string) *config.Config { // relative persistent_state_dir. func TestResolveAutobahnLayout(t *testing.T) { conf := layoutConfig(t, filepath.Join("data", "autobahn")) - layout, err := ResolveAutobahnLayout(conf) + layout, err := ResolveAutobahnLayout(conf, gigaconfig.DefaultConfig.Storage, genesisAt(1)) require.NoError(t, err) psd := filepath.Join(conf.RootDir, "data", "autobahn") @@ -40,13 +44,17 @@ func TestResolveAutobahnLayout(t *testing.T) { require.NoError(t, err) require.Equal(t, want.FlatKVConfig.DataDir, layout.FlatKVDir) require.Equal(t, filepath.Join(psd, "data", "state_commit", "flatkv"), layout.FlatKVDir) + require.Equal(t, []string{filepath.Join(psd, "blockdb")}, layout.BlockDBDirs) + require.Equal(t, want.ReceiptDBConfig.DBDirectory, layout.ReceiptStoreDir) + require.Equal(t, want.SSConfig.EVMDBDirectory, layout.StateStoreDir) + require.Equal(t, want.AppHashBuilderConfig.HashVaultConfig.Path, layout.HashVaultDir) require.Equal(t, filepath.Join(psd, "state-sync-restored"), layout.RestoreMarkerFile) require.Equal(t, filepath.Join(psd, "data", "state_commit", "state_sync_proofs"), layout.StateSyncProofDir) } func TestResolveAutobahnLayoutAbsolutePersistentStateDir(t *testing.T) { psd := t.TempDir() - layout, err := ResolveAutobahnLayout(layoutConfig(t, psd)) + layout, err := ResolveAutobahnLayout(layoutConfig(t, psd), gigaconfig.DefaultConfig.Storage, genesisAt(1)) require.NoError(t, err) require.Equal(t, psd, layout.PersistentStateDir) } @@ -55,12 +63,69 @@ func TestResolveAutobahnLayoutLegacyFlatKVDir(t *testing.T) { psd := t.TempDir() legacy := filepath.Join(psd, "data", "flatkv") require.NoError(t, os.MkdirAll(legacy, 0o750)) - layout, err := ResolveAutobahnLayout(layoutConfig(t, psd)) + layout, err := ResolveAutobahnLayout(layoutConfig(t, psd), gigaconfig.DefaultConfig.Storage, genesisAt(1)) require.NoError(t, err) require.Equal(t, legacy, layout.FlatKVDir) } func TestResolveAutobahnLayoutRequiresAutobahnConfig(t *testing.T) { - _, err := ResolveAutobahnLayout(&config.Config{BaseConfig: config.BaseConfig{RootDir: t.TempDir()}}) + _, err := ResolveAutobahnLayout(&config.Config{BaseConfig: config.BaseConfig{RootDir: t.TempDir()}}, gigaconfig.DefaultConfig.Storage, genesisAt(1)) require.Error(t, err) } + +// TestAutobahnLayoutCoversWhatAFullnodeStoresOpen requires every entry that a fullnode's storage +// manager creates under the persistent state dir to be a layout path, inside one, or a parent of +// one. A new store under that dir fails here until state sync decides whether restore must require +// it empty or create must pack it. +func TestAutobahnLayoutCoversWhatAFullnodeStoresOpen(t *testing.T) { + conf := layoutConfig(t, filepath.Join("data", "autobahn")) + storage := gigaconfig.DefaultConfig.Storage + storage.Receipts = true + layout, err := ResolveAutobahnLayout(conf, storage, genesisAt(1)) + require.NoError(t, err) + + fc, _, err := loadAutobahnCommittee(conf.AutobahnConfigFile) + require.NoError(t, err) + manager, err := openAutobahnStorageManager(t.Context(), conf.RootDir, conf.Mode, fc, storage, genesisAt(1)) + require.NoError(t, err) + require.NoError(t, manager.Close()) + + storeDirs := append([]string{layout.FlatKVDir, layout.ReceiptStoreDir, layout.StateStoreDir}, layout.BlockDBDirs...) + for _, dir := range storeDirs { + info, err := os.Stat(dir) + require.NoError(t, err, "the storage manager did not create %s", dir) + require.True(t, info.IsDir()) + } + dirs := append(storeDirs, layout.HashVaultDir, layout.StateSyncProofDir) + files := []string{layout.RestoreMarkerFile} + isParent := func(path string) bool { + for _, p := range append(slices.Clone(dirs), files...) { + if strings.HasPrefix(p, path+string(filepath.Separator)) { + return true + } + } + return false + } + + var uncovered []string + err = filepath.WalkDir(layout.PersistentStateDir, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + switch { + case path == layout.PersistentStateDir || slices.Contains(files, path): + return nil + case slices.Contains(dirs, path): + return fs.SkipDir + case d.IsDir() && isParent(path): + return nil + } + uncovered = append(uncovered, path) + if d.IsDir() { + return fs.SkipDir + } + return nil + }) + require.NoError(t, err) + require.Empty(t, uncovered, "add these paths to AutobahnLayout, or state here why state sync ignores them") +} diff --git a/sei-tendermint/node/setup.go b/sei-tendermint/node/setup.go index 432ce549ea..adeddc8deb 100644 --- a/sei-tendermint/node/setup.go +++ b/sei-tendermint/node/setup.go @@ -412,20 +412,34 @@ func openAutobahnStorageManager( storage gigaconfig.StorageConfig, genDoc *types.GenesisDoc, ) (*bootstrap.GigaStorageManager, error) { - directory, err := resolvePersistentStateDir(rootDir, fc.PersistentStateDir) + _, storageConfig, err := autobahnStorageConfig(rootDir, nodeMode, fc, storage, genDoc) if err != nil { return nil, err } + return bootstrap.NewGigaStorageManager(ctx, storageConfig) +} + +func autobahnStorageConfig( + rootDir string, + nodeMode string, + fc *config.AutobahnFileConfig, + storage gigaconfig.StorageConfig, + genDoc *types.GenesisDoc, +) (string, *seidbconfig.GigaStorageConfig, error) { + directory, err := resolvePersistentStateDir(rootDir, fc.PersistentStateDir) + if err != nil { + return "", nil, err + } storageConfig, err := buildGigaStorageConfig(directory, nodeMode, storage, genDoc) if err != nil { - return nil, fmt.Errorf("build Autobahn storage config: %w", err) + return "", nil, fmt.Errorf("build Autobahn storage config: %w", err) } blockConfig, err := fc.BlockDB.LittBlockConfig(filepath.Join(directory, "blockdb")) if err != nil { - return nil, fmt.Errorf("build Autobahn block DB config: %w", err) + return "", nil, fmt.Errorf("build Autobahn block DB config: %w", err) } storageConfig.BlockDBConfig = &blockConfig - return bootstrap.NewGigaStorageManager(ctx, storageConfig) + return directory, storageConfig, nil } // buildGigaStorageConfig returns the store configs for directory with the [giga.storage] From 8b561ae8f586c98a3eb17f7d89d6b0a4ac0010eb Mon Sep 17 00:00:00 2001 From: blindchaser Date: Fri, 13 Nov 2026 11:00:55 -0500 Subject: [PATCH 06/12] autobahn: prepare a restored node's epoch registry from a checkpoint PrepareEpochRegistry reads the epoch_registry row from a staged FlatKV checkpoint, the PersistedEpochRegistry that consensus commits in EpochState, and writes it as an epoch registry snapshot in a staging dir. The registry must open from genesis the same way node startup opens it, through p2p.OpenEpochRegistry, which BuildDataState now also uses. It must hold the epoch of the checkpoint height and the epoch after it, and a height at the last road of an epoch is refused. With no row the registry holds the genesis epochs, and an empty row is an error. The genesis check binds a row to this chain only while the row holds epoch 0 or 1. InstallEpochRegistry moves the staged snapshot to the layout's EpochRegistryDir. The layout also names the state sync proof file that a restore installs. Co-authored-by: Cursor --- .../autobahn/data/restore_epoch_test.go | 39 ++++ .../internal/autobahn/epoch/seed_test.go | 97 +++++++++ .../internal/autobahn/epoch/snapshot.go | 35 ++- .../internal/autobahn/epoch/testonly.go | 12 + .../internal/evmonlyapp/app_test.go | 30 +++ .../internal/evmonlyapp/epoch_state.go | 16 ++ .../internal/p2p/giga_router_common.go | 41 +++- sei-tendermint/node/autobahn_layout.go | 14 +- sei-tendermint/node/autobahn_layout_test.go | 21 +- sei-tendermint/node/epoch_seed.go | 99 +++++++++ sei-tendermint/node/epoch_seed_test.go | 205 ++++++++++++++++++ sei-tendermint/node/testonly.go | 16 ++ 12 files changed, 606 insertions(+), 19 deletions(-) create mode 100644 sei-tendermint/internal/autobahn/data/restore_epoch_test.go create mode 100644 sei-tendermint/internal/autobahn/epoch/seed_test.go create mode 100644 sei-tendermint/node/epoch_seed.go create mode 100644 sei-tendermint/node/epoch_seed_test.go create mode 100644 sei-tendermint/node/testonly.go diff --git a/sei-tendermint/internal/autobahn/data/restore_epoch_test.go b/sei-tendermint/internal/autobahn/data/restore_epoch_test.go new file mode 100644 index 0000000000..8b77b053e5 --- /dev/null +++ b/sei-tendermint/internal/autobahn/data/restore_epoch_test.go @@ -0,0 +1,39 @@ +package data + +import ( + "testing" + "time" + + "github.com/sei-protocol/sei-chain/sei-tendermint/autobahn/types" + "github.com/sei-protocol/sei-chain/sei-tendermint/internal/autobahn/epoch" + "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils" + "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils/require" +) + +// restoredRegistry opens a registry with the genesis of source from a snapshot restored from +// source's epoch_registry row, as a state sync restore leaves it. +func restoredRegistry(t *testing.T, source *epoch.Registry, genesis *types.Epoch) *epoch.Registry { + t.Helper() + dir := t.TempDir() + require.NoError(t, epoch.RestoreSnapshot(dir, utils.Some(source.MarshalSnapshot()))) + return utils.OrPanic1(epoch.NewRegistry(genesis.Committee(), genesis.RoadRange().First, time.Time{}, utils.Some(dir))) +} + +func TestRestoredStateAnchorsOnTheEpochOfTheRestoredRoad(t *testing.T) { + rng := utils.TestRng() + const x = types.EpochIndex(3) + source, keys := epoch.GenRegistryThrough(rng, 3, x+1) + genesis := source.MustEpoch(0) + require.NoError(t, source.PruneBefore(x-1)) + registry := restoredRegistry(t, source, genesis) + + h := registry.LastRoad(x-1) + 1 + qc, blocks := commitQCAtRoad(utils.OrPanic1(registry.EpochAt(h)), keys, h) + store := newTestStore(t) + writeToBlockStore(t, store, []*types.FullCommitQC{qc}, [][]*types.Block{blocks}) + state := newTestState(t, &Config{Registry: registry}, store) + + anchor := state.Anchor().Load() + require.True(t, epoch.EpochEqual(source.MustEpoch(x), anchor.Epochs[0])) + require.True(t, epoch.EpochEqual(source.MustEpoch(x+1), anchor.Epochs[1])) +} diff --git a/sei-tendermint/internal/autobahn/epoch/seed_test.go b/sei-tendermint/internal/autobahn/epoch/seed_test.go new file mode 100644 index 0000000000..1cef9464cb --- /dev/null +++ b/sei-tendermint/internal/autobahn/epoch/seed_test.go @@ -0,0 +1,97 @@ +package epoch + +import ( + "errors" + "testing" + "time" + + "github.com/sei-protocol/sei-chain/sei-tendermint/autobahn/types" + "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils" + "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils/require" +) + +// openGenesis opens a registry under dir with genesis as its epoch 0. +func openGenesis(genesis *types.Epoch, dir string) (*Registry, error) { + return NewRegistry(genesis.Committee(), genesis.RoadRange().First, time.Time{}, utils.Some(dir)) +} + +// openGenesisOf opens a registry under dir with the genesis of source. +func openGenesisOf(source *Registry, dir string) (*Registry, error) { + return openGenesis(source.MustEpoch(0), dir) +} + +func TestRestoreSnapshotOpensWithSourceEpochs(t *testing.T) { + rng := utils.TestRng() + for _, current := range []types.EpochIndex{1, 2, 5} { + source, keys := GenRegistryThrough(rng, 3, current) + genesis := source.MustEpoch(0) + require.NoError(t, source.TestPushEpoch(current-1, map[types.PublicKey]uint64{keys[0].Public(): 5})) + require.NoError(t, source.PruneBefore(current-1)) + dir := t.TempDir() + + require.NoError(t, RestoreSnapshot(dir, utils.Some(source.MarshalSnapshot()))) + registry, err := openGenesis(genesis, dir) + require.NoError(t, err) + + for idx := current - 1; idx <= current+1; idx++ { + require.True(t, EpochEqual(source.MustEpoch(idx), utils.OrPanic1(registry.Epoch(idx)))) + } + if current >= 2 { + _, err := registry.Epoch(current - 2) + require.True(t, errors.Is(err, types.ErrPruned)) + } + _, err = registry.Epoch(current + 2) + require.True(t, errors.Is(err, types.ErrNotFound)) + } +} + +func TestRestoreSnapshotWithoutRowOpensGenesis(t *testing.T) { + rng := utils.TestRng() + source, _ := GenRegistryThrough(rng, 3, 3) + dir := t.TempDir() + require.NoError(t, RestoreSnapshot(dir, utils.Some(source.MarshalSnapshot()))) + + require.NoError(t, RestoreSnapshot(dir, utils.None[[]byte]())) + registry, err := openGenesisOf(source, dir) + require.NoError(t, err) + require.True(t, EpochEqual(source.MustEpoch(0), utils.OrPanic1(registry.Epoch(0)))) + require.True(t, EpochEqual(source.MustEpoch(1), utils.OrPanic1(registry.Epoch(1)))) + _, err = registry.Epoch(2) + require.True(t, errors.Is(err, types.ErrNotFound)) +} + +func TestRestoreSnapshotWithGenesisEpochsOfAnotherChainDoesNotOpen(t *testing.T) { + rng := utils.TestRng() + source, _ := GenRegistryThrough(rng, 3, 2) + other, _ := GenRegistryThrough(rng, 3, 2) + dir := t.TempDir() + + require.NoError(t, RestoreSnapshot(dir, utils.Some(source.MarshalSnapshot()))) + _, err := openGenesisOf(other, dir) + require.Error(t, err) +} + +func TestRestoreSnapshotPrunedPastGenesisOpensOnAnotherChainWithTheSameFirstRoad(t *testing.T) { + rng := utils.TestRng() + source, _ := GenRegistryThrough(rng, 3, 3) + firstRoad := source.MustEpoch(0).RoadRange().First + require.NoError(t, source.PruneBefore(2)) + otherCommittee, _ := types.GenCommittee(rng, 3) + dir := t.TempDir() + + require.NoError(t, RestoreSnapshot(dir, utils.Some(source.MarshalSnapshot()))) + registry, err := NewRegistry(otherCommittee, firstRoad, time.Time{}, utils.Some(dir)) + require.NoError(t, err) + require.True(t, EpochEqual(source.MustEpoch(3), utils.OrPanic1(registry.Epoch(3)))) +} + +func TestRestoreSnapshotRejectsMalformedRow(t *testing.T) { + for name, raw := range map[string][]byte{ + "not protobuf": {0xff, 0x01}, + "no live epochs": {}, + } { + t.Run(name, func(t *testing.T) { + require.Error(t, RestoreSnapshot(t.TempDir(), utils.Some(raw))) + }) + } +} diff --git a/sei-tendermint/internal/autobahn/epoch/snapshot.go b/sei-tendermint/internal/autobahn/epoch/snapshot.go index c3444174b6..9bcb31696e 100644 --- a/sei-tendermint/internal/autobahn/epoch/snapshot.go +++ b/sei-tendermint/internal/autobahn/epoch/snapshot.go @@ -12,6 +12,7 @@ import ( "github.com/sei-protocol/sei-chain/sei-tendermint/internal/autobahn/pb" "github.com/sei-protocol/sei-chain/sei-tendermint/internal/protoutils" "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils" + "google.golang.org/protobuf/proto" ) const ( @@ -19,6 +20,38 @@ const ( epochSnapshotPrefix = "registry" ) +// SnapshotDir returns the dir under stateDir that holds the epoch registry snapshot. +func SnapshotDir(stateDir string) string { + return filepath.Join(stateDir, epochSnapshotDir) +} + +// RestoreSnapshot replaces the epoch registry snapshot under stateDir with raw, the protobuf +// encoding of a PersistedEpochRegistry. With no raw it removes the snapshot, so the registry +// opens with the genesis epochs. +func RestoreSnapshot(stateDir string, raw utils.Option[[]byte]) error { + dir := SnapshotDir(stateDir) + if err := os.RemoveAll(dir); err != nil { + return fmt.Errorf("remove epoch snapshot dir %s: %w", dir, err) + } + encoded, ok := raw.Get() + if !ok { + return nil + } + var snapshot pb.PersistedEpochRegistry + if err := proto.Unmarshal(encoded, &snapshot); err != nil { + return fmt.Errorf("decode epoch registry: %w", err) + } + state, err := persistedRegistryConv.DecodeReq(&snapshot) + if err != nil { + return fmt.Errorf("decode epoch registry: %w", err) + } + persister, _, err := openEpochSnapshot(utils.Some(stateDir)) + if err != nil { + return err + } + return persister.Persist(persistedRegistryConv.Encode(state)) +} + func openEpochSnapshot( stateDir utils.Option[string], ) (persist.Persister[*pb.PersistedEpochRegistry], utils.Option[*pb.PersistedEpochRegistry], error) { @@ -26,7 +59,7 @@ func openEpochSnapshot( if !ok { return persist.NewPersister[*pb.PersistedEpochRegistry](utils.None[string](), epochSnapshotPrefix) } - dir := filepath.Join(root, epochSnapshotDir) + dir := SnapshotDir(root) if err := os.MkdirAll(dir, 0700); err != nil { return nil, utils.None[*pb.PersistedEpochRegistry](), fmt.Errorf("create epoch snapshot dir %s: %w", dir, err) } diff --git a/sei-tendermint/internal/autobahn/epoch/testonly.go b/sei-tendermint/internal/autobahn/epoch/testonly.go index 574ed343e5..a8cfcb1fa0 100644 --- a/sei-tendermint/internal/autobahn/epoch/testonly.go +++ b/sei-tendermint/internal/autobahn/epoch/testonly.go @@ -5,6 +5,7 @@ import ( "github.com/sei-protocol/sei-chain/sei-tendermint/autobahn/types" "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils" + "google.golang.org/protobuf/proto" ) // GenRegistry generates a random Registry of the given committee size. @@ -48,6 +49,17 @@ func (r *Registry) TestPushEpoch(endEpoch types.EpochIndex, weights map[types.Pu return r.TryPushEpoch(r.LastRoad(endEpoch), weights) } +// MarshalSnapshot returns the protobuf encoding of the registry's persisted snapshot, the +// value that the epoch_registry state row holds. +func (r *Registry) MarshalSnapshot() []byte { + return utils.OrPanic1(proto.Marshal(persistedRegistryConv.Encode(r.stateRecv.Load()))) +} + +// EpochEqual reports whether a and b have the same index, roads and committee. +func EpochEqual(a, b *types.Epoch) bool { + return a.EpochIndex() == b.EpochIndex() && a.RoadRange() == b.RoadRange() && a.Committee().Equal(b.Committee()) +} + // MustEpoch returns the registered epoch at i. Panics if it is missing. func (r *Registry) MustEpoch(i types.EpochIndex) *types.Epoch { ep, err := r.Epoch(i) diff --git a/sei-tendermint/internal/evmonlyapp/app_test.go b/sei-tendermint/internal/evmonlyapp/app_test.go index 21f053ab27..5c89847c61 100644 --- a/sei-tendermint/internal/evmonlyapp/app_test.go +++ b/sei-tendermint/internal/evmonlyapp/app_test.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "math/big" + "path/filepath" "sync/atomic" "testing" "time" @@ -29,6 +30,7 @@ import ( seidbconfig "github.com/sei-protocol/sei-chain/sei-db/config" "github.com/sei-protocol/sei-chain/sei-db/ledger_db/receipt" gigatypes "github.com/sei-protocol/sei-chain/sei-db/state_db/giga/types" + "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv" "github.com/sei-protocol/sei-chain/sei-tendermint/internal/proxy" "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils" "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils/require" @@ -186,6 +188,34 @@ func TestEVMOnlyAppWithoutEpochStateWritesNoEpochStateRows(t *testing.T) { require.False(t, found) } +func TestReadCheckpointEpochRegistryRowReadsTheCheckpointRow(t *testing.T) { + storageConfig, err := seidbconfig.AutobahnStorageConfig(t.TempDir()) + require.NoError(t, err) + storageConfig.CheckpointConfig = seidbconfig.CheckpointConfig{BlockInterval: 1} + app := newEVMOnlyTestAppWithStorage(t, nil, gigaconfig.DefaultConfig.Execution, storageConfig).(*evmOnlyApplication) + _, err = app.InitChain(evmOnlyTestInitChain()) + require.NoError(t, err) + checkpointAt := func(height int64) string { + t.Helper() + require.NoError(t, app.AwaitCommits()) + require.NoError(t, app.storage.StateDB().SC().FlushSnapshots()) + dir := filepath.Join(t.TempDir(), "checkpoint") + require.NoError(t, flatkv.HardLinkSnapshot(storageConfig.FlatKVConfig.DataDir, height, dir)) + return dir + } + + finalizeAndCommitEVMOnlyTestBlock(t, app, evmOnlyTestBlock(1)) + row, err := ReadCheckpointEpochRegistryRow(checkpointAt(1)) + require.NoError(t, err) + require.False(t, row.IsPresent()) + + state := proxy.EpochState{EpochRegistry: []byte("registry"), NextEpoch: []byte("next-epoch")} + finalizeAndCommitEVMOnlyTestBlock(t, app, evmOnlyTestEpochBlock(2, state)) + row, err = ReadCheckpointEpochRegistryRow(checkpointAt(2)) + require.NoError(t, err) + require.Equal(t, state.EpochRegistry, row.OrPanic("epoch_registry row")) +} + func TestEVMOnlyAppEpochStateDoesNotCarryOver(t *testing.T) { app := newInitializedEVMOnlyTestApp(t).(*evmOnlyApplication) encoded := installRecordingEVMExecutor(app) diff --git a/sei-tendermint/internal/evmonlyapp/epoch_state.go b/sei-tendermint/internal/evmonlyapp/epoch_state.go index 179c3b9f18..d3d1f72def 100644 --- a/sei-tendermint/internal/evmonlyapp/epoch_state.go +++ b/sei-tendermint/internal/evmonlyapp/epoch_state.go @@ -1,10 +1,13 @@ package evmonlyapp import ( + "fmt" "slices" gigatypes "github.com/sei-protocol/sei-chain/sei-db/state_db/giga/types" + "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv" "github.com/sei-protocol/sei-chain/sei-tendermint/internal/proxy" + "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils" ) const ( @@ -27,3 +30,16 @@ func epochStateMiscChange(key string, value []byte) gigatypes.MiscChange { Value: slices.Clone(value), } } + +// ReadCheckpointEpochRegistryRow returns the epoch_registry row in the FlatKV checkpoint in dir, +// as consensus passed it in EpochState.EpochRegistry, or None if the checkpoint holds no row. +func ReadCheckpointEpochRegistryRow(dir string) (utils.Option[[]byte], error) { + raw, found, err := flatkv.ReadSnapshotMiscValue(dir, epochStateModule, []byte(epochStateEpochRegistryKey)) + if err != nil { + return utils.None[[]byte](), fmt.Errorf("read the epoch_registry row: %w", err) + } + if !found { + return utils.None[[]byte](), nil + } + return utils.Some(raw), nil +} diff --git a/sei-tendermint/internal/p2p/giga_router_common.go b/sei-tendermint/internal/p2p/giga_router_common.go index f2fec84dc0..86755e08f5 100644 --- a/sei-tendermint/internal/p2p/giga_router_common.go +++ b/sei-tendermint/internal/p2p/giga_router_common.go @@ -4,8 +4,10 @@ import ( "context" "errors" "fmt" + "maps" "net/url" "path/filepath" + "slices" "sort" "github.com/ethereum/go-ethereum/common" @@ -88,24 +90,41 @@ func BuildDataState(cfg *GigaRouterCommonConfig, blockStore *ablockstore.Store) if cfg.AppHashBuilder == nil { return nil, errors.New("GigaRouterCommonConfig.AppHashBuilder is required") } - firstRoad := atypes.RoadIndex(cfg.GenDoc.InitialHeight) // nolint:gosec // verified to be positive. + registry, err := OpenEpochRegistry(cfg.GenDoc, slices.Collect(maps.Keys(cfg.ValidatorAddrs)), cfg.PersistentStateDir) + if err != nil { + return nil, err + } + ds, err := data.NewState(&data.Config{Registry: registry}, blockStore) + if err != nil { + return nil, fmt.Errorf("data.NewState: %w", err) + } + return ds, nil +} + +// OpenEpochRegistry opens the epoch registry under persistentStateDir, with the genesis +// validators and the road origin and timestamp of genDoc. +func OpenEpochRegistry( + genDoc *types.GenesisDoc, + validators []atypes.PublicKey, + persistentStateDir string, +) (*epoch.Registry, error) { + if genDoc.InitialHeight < 1 { + return nil, fmt.Errorf("GenDoc.InitialHeight = %v, want >=1", genDoc.InitialHeight) + } + firstRoad := atypes.RoadIndex(genDoc.InitialHeight) // nolint:gosec // verified to be positive. genesisWeights := map[atypes.PublicKey]uint64{} - for k := range cfg.ValidatorAddrs { + for _, k := range validators { genesisWeights[k] = 1 } genesisCommittee, err := atypes.NewCommittee(genesisWeights) if err != nil { return nil, fmt.Errorf("genesis committee: %w", err) } - registry, err := epoch.NewRegistry(genesisCommittee, firstRoad, cfg.GenDoc.GenesisTime, utils.Some(cfg.PersistentStateDir)) + registry, err := epoch.NewRegistry(genesisCommittee, firstRoad, genDoc.GenesisTime, utils.Some(persistentStateDir)) if err != nil { return nil, fmt.Errorf("epoch.NewRegistry(): %w", err) } - ds, err := data.NewState(&data.Config{Registry: registry}, blockStore) - if err != nil { - return nil, fmt.Errorf("data.NewState: %w", err) - } - return ds, nil + return registry, nil } func (r *gigaRouterCommon) LastCommittedBlockNumber() int64 { @@ -259,6 +278,12 @@ func RestoreMarkerFile(psd string) string { return filepath.Join(psd, "state-sync-restored") } +// RestoreStateSyncProofFile returns the path of the state sync proof file that a state +// sync restore installs in the persistent state dir psd. +func RestoreStateSyncProofFile(psd string) string { + return filepath.Join(psd, "state-sync-proof") +} + // dialAndRunConn dials a peer, handshakes as a SeiGiga connection, // registers the rpc client in poolOut, and runs runClient for the // connection's lifetime. It verifies the p2p node key against the selected diff --git a/sei-tendermint/node/autobahn_layout.go b/sei-tendermint/node/autobahn_layout.go index 2ccb81c76c..90fda98e36 100644 --- a/sei-tendermint/node/autobahn_layout.go +++ b/sei-tendermint/node/autobahn_layout.go @@ -4,6 +4,7 @@ import ( gigaconfig "github.com/sei-protocol/sei-chain/giga/config" dbutils "github.com/sei-protocol/sei-chain/sei-db/common/utils" "github.com/sei-protocol/sei-chain/sei-tendermint/config" + "github.com/sei-protocol/sei-chain/sei-tendermint/internal/autobahn/epoch" "github.com/sei-protocol/sei-chain/sei-tendermint/internal/p2p" "github.com/sei-protocol/sei-chain/sei-tendermint/types" ) @@ -18,9 +19,14 @@ type AutobahnLayout struct { BlockDBDirs []string ReceiptStoreDir string StateStoreDir string - HashVaultDir string + AppHashVaultDir string RestoreMarkerFile string - StateSyncProofDir string + // RestoreProofFile is the state sync proof that restore installs for the seed to read. + RestoreProofFile string + // StateSyncProofDir holds the state sync proof files that a running node writes next to its + // FlatKV checkpoints. + StateSyncProofDir string + EpochRegistryDir string } // ResolveAutobahnLayout returns the Autobahn layout of the node that conf, storage and @@ -41,8 +47,10 @@ func ResolveAutobahnLayout(conf *config.Config, storage gigaconfig.StorageConfig BlockDBDirs: storageConfig.BlockDBConfig.Litt.Paths, ReceiptStoreDir: storageConfig.ReceiptDBConfig.DBDirectory, StateStoreDir: storageConfig.SSConfig.EVMDBDirectory, - HashVaultDir: storageConfig.AppHashBuilderConfig.HashVaultConfig.Path, + AppHashVaultDir: storageConfig.AppHashBuilderConfig.HashVaultConfig.Path, RestoreMarkerFile: p2p.RestoreMarkerFile(psd), + RestoreProofFile: p2p.RestoreStateSyncProofFile(psd), StateSyncProofDir: dbutils.GetStateSyncProofPath(psd), + EpochRegistryDir: epoch.SnapshotDir(psd), }, nil } diff --git a/sei-tendermint/node/autobahn_layout_test.go b/sei-tendermint/node/autobahn_layout_test.go index 0ca0071e84..b8dd6978a5 100644 --- a/sei-tendermint/node/autobahn_layout_test.go +++ b/sei-tendermint/node/autobahn_layout_test.go @@ -11,6 +11,7 @@ import ( gigaconfig "github.com/sei-protocol/sei-chain/giga/config" seidbconfig "github.com/sei-protocol/sei-chain/sei-db/config" "github.com/sei-protocol/sei-chain/sei-tendermint/config" + "github.com/sei-protocol/sei-chain/sei-tendermint/internal/p2p" "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils" "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils/require" ) @@ -47,9 +48,11 @@ func TestResolveAutobahnLayout(t *testing.T) { require.Equal(t, []string{filepath.Join(psd, "blockdb")}, layout.BlockDBDirs) require.Equal(t, want.ReceiptDBConfig.DBDirectory, layout.ReceiptStoreDir) require.Equal(t, want.SSConfig.EVMDBDirectory, layout.StateStoreDir) - require.Equal(t, want.AppHashBuilderConfig.HashVaultConfig.Path, layout.HashVaultDir) + require.Equal(t, want.AppHashBuilderConfig.HashVaultConfig.Path, layout.AppHashVaultDir) require.Equal(t, filepath.Join(psd, "state-sync-restored"), layout.RestoreMarkerFile) + require.Equal(t, filepath.Join(psd, "state-sync-proof"), layout.RestoreProofFile) require.Equal(t, filepath.Join(psd, "data", "state_commit", "state_sync_proofs"), layout.StateSyncProofDir) + require.Equal(t, filepath.Join(psd, "epochs"), layout.EpochRegistryDir) } func TestResolveAutobahnLayoutAbsolutePersistentStateDir(t *testing.T) { @@ -74,8 +77,8 @@ func TestResolveAutobahnLayoutRequiresAutobahnConfig(t *testing.T) { } // TestAutobahnLayoutCoversWhatAFullnodeStoresOpen requires every entry that a fullnode's storage -// manager creates under the persistent state dir to be a layout path, inside one, or a parent of -// one. A new store under that dir fails here until state sync decides whether restore must require +// manager and epoch registry create under the persistent state dir to be a layout path, inside one, +// or a parent of one. A new store under that dir fails here until state sync decides whether restore must require // it empty or create must pack it. func TestAutobahnLayoutCoversWhatAFullnodeStoresOpen(t *testing.T) { conf := layoutConfig(t, filepath.Join("data", "autobahn")) @@ -89,15 +92,19 @@ func TestAutobahnLayoutCoversWhatAFullnodeStoresOpen(t *testing.T) { manager, err := openAutobahnStorageManager(t.Context(), conf.RootDir, conf.Mode, fc, storage, genesisAt(1)) require.NoError(t, err) require.NoError(t, manager.Close()) + genesis, err := ResolveEpochGenesis(conf, genesisAt(1)) + require.NoError(t, err) + _, err = p2p.OpenEpochRegistry(genesis.GenDoc, genesis.Validators, layout.PersistentStateDir) + require.NoError(t, err) - storeDirs := append([]string{layout.FlatKVDir, layout.ReceiptStoreDir, layout.StateStoreDir}, layout.BlockDBDirs...) + storeDirs := append([]string{layout.FlatKVDir, layout.ReceiptStoreDir, layout.StateStoreDir, layout.AppHashVaultDir, layout.EpochRegistryDir}, layout.BlockDBDirs...) for _, dir := range storeDirs { info, err := os.Stat(dir) - require.NoError(t, err, "the storage manager did not create %s", dir) + require.NoError(t, err, "the node did not create %s", dir) require.True(t, info.IsDir()) } - dirs := append(storeDirs, layout.HashVaultDir, layout.StateSyncProofDir) - files := []string{layout.RestoreMarkerFile} + dirs := append(storeDirs, layout.StateSyncProofDir) + files := []string{layout.RestoreMarkerFile, layout.RestoreProofFile} isParent := func(path string) bool { for _, p := range append(slices.Clone(dirs), files...) { if strings.HasPrefix(p, path+string(filepath.Separator)) { diff --git a/sei-tendermint/node/epoch_seed.go b/sei-tendermint/node/epoch_seed.go new file mode 100644 index 0000000000..d0d9c2c3f5 --- /dev/null +++ b/sei-tendermint/node/epoch_seed.go @@ -0,0 +1,99 @@ +package node + +import ( + "errors" + "fmt" + "maps" + "os" + "path/filepath" + "slices" + + dbutils "github.com/sei-protocol/sei-chain/sei-db/common/utils" + atypes "github.com/sei-protocol/sei-chain/sei-tendermint/autobahn/types" + "github.com/sei-protocol/sei-chain/sei-tendermint/config" + "github.com/sei-protocol/sei-chain/sei-tendermint/internal/autobahn/epoch" + "github.com/sei-protocol/sei-chain/sei-tendermint/internal/evmonlyapp" + "github.com/sei-protocol/sei-chain/sei-tendermint/internal/p2p" + "github.com/sei-protocol/sei-chain/sei-tendermint/types" +) + +var ( + // ErrEmptyEpochRegistryRow is returned when a checkpoint holds an empty epoch_registry row. + ErrEmptyEpochRegistryRow = errors.New("the checkpoint has an empty epoch_registry row: the chain does not populate it") + // ErrClosingRoad is returned for a checkpoint at the last road of an epoch. + ErrClosingRoad = errors.New("the checkpoint height is the last road of an epoch") +) + +// EpochGenesis is what a node's epoch registry opens with: the genesis doc and the +// genesis validators. +type EpochGenesis struct { + GenDoc *types.GenesisDoc + Validators []atypes.PublicKey +} + +// ResolveEpochGenesis returns the epoch genesis of the node that conf and genDoc configure. +func ResolveEpochGenesis(conf *config.Config, genDoc *types.GenesisDoc) (EpochGenesis, error) { + _, validators, err := loadAutobahnCommittee(conf.AutobahnConfigFile) + if err != nil { + return EpochGenesis{}, err + } + return EpochGenesis{GenDoc: genDoc, Validators: slices.Collect(maps.Keys(validators))}, nil +} + +// PrepareEpochRegistry writes under stagingDir the epoch registry snapshot of the epoch_registry +// row in the FlatKV checkpoint in checkpointDir, the state after road height. With no row the +// snapshot holds only the genesis epochs. It returns an error if the row is empty or does not +// decode, if the registry does not open from genesis with the snapshot, if the registry does not +// hold the epoch of height and the epoch after it, or if height is the last road of an epoch. +// +// The registry check binds the row to this chain only while it holds epoch 0 or 1. A row whose +// epochs all follow epoch 1 opens on any chain with the same initial height. +func PrepareEpochRegistry(genesis EpochGenesis, checkpointDir string, height int64, stagingDir string) error { + if height < 1 { + return fmt.Errorf("checkpoint height %d is not positive", height) + } + row, err := evmonlyapp.ReadCheckpointEpochRegistryRow(checkpointDir) + if err != nil { + return err + } + if raw, ok := row.Get(); ok && len(raw) == 0 { + return ErrEmptyEpochRegistryRow + } + if err := epoch.RestoreSnapshot(stagingDir, row); err != nil { + return fmt.Errorf("seed epoch snapshot: %w", err) + } + registry, err := p2p.OpenEpochRegistry(genesis.GenDoc, genesis.Validators, stagingDir) + if err != nil { + return fmt.Errorf("open the seeded epoch registry: %w", err) + } + return requireEpochsForRoad(registry, atypes.RoadIndex(height)) +} + +// requireEpochsForRoad returns an error unless registry holds the epoch of road and the epoch +// after it, and road is not the last road of its epoch. +func requireEpochsForRoad(registry *epoch.Registry, road atypes.RoadIndex) error { + if closing, ok := registry.ClosingEpoch(road).Get(); ok { + return fmt.Errorf("road %d ends epoch %d: %w", road, closing, ErrClosingRoad) + } + current, err := registry.EpochAt(road) + if err != nil { + return fmt.Errorf("the seeded epoch registry has no epoch for road %d: %w", road, err) + } + if _, err := registry.Epoch(current.EpochIndex() + 1); err != nil { + return fmt.Errorf("the seeded epoch registry has no epoch after road %d: %w", road, err) + } + return nil +} + +// InstallEpochRegistry replaces the epoch registry snapshot of layout with the one that +// PrepareEpochRegistry wrote under stagingDir. stagingDir must be on the filesystem of the +// persistent state dir. +func InstallEpochRegistry(layout AutobahnLayout, stagingDir string) error { + if err := os.RemoveAll(layout.EpochRegistryDir); err != nil { + return fmt.Errorf("remove the epoch registry snapshot: %w", err) + } + if err := os.Rename(epoch.SnapshotDir(stagingDir), layout.EpochRegistryDir); err != nil && !errors.Is(err, os.ErrNotExist) { + return fmt.Errorf("install the epoch registry snapshot: %w", err) + } + return dbutils.SyncDir(filepath.Dir(layout.EpochRegistryDir)) +} diff --git a/sei-tendermint/node/epoch_seed_test.go b/sei-tendermint/node/epoch_seed_test.go new file mode 100644 index 0000000000..bfe88123f9 --- /dev/null +++ b/sei-tendermint/node/epoch_seed_test.go @@ -0,0 +1,205 @@ +package node + +import ( + "path/filepath" + "testing" + + "github.com/sei-protocol/sei-chain/sei-db/proto" + "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv" + flatkvconfig "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv/config" + atypes "github.com/sei-protocol/sei-chain/sei-tendermint/autobahn/types" + "github.com/sei-protocol/sei-chain/sei-tendermint/internal/autobahn/epoch" + "github.com/sei-protocol/sei-chain/sei-tendermint/internal/p2p" + "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils" + "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils/require" + "github.com/sei-protocol/sei-chain/sei-tendermint/types" +) + +// testEpochChain is a chain whose epoch registry holds epochs 0 through 3. +type testEpochChain struct { + genesis EpochGenesis + registry *epoch.Registry +} + +func newTestEpochChain(t *testing.T, rng utils.Rng) testEpochChain { + t.Helper() + keys := utils.GenSliceN(rng, 3, atypes.GenSecretKey) + validators := make([]atypes.PublicKey, 0, len(keys)) + for _, k := range keys { + validators = append(validators, k.Public()) + } + genesis := EpochGenesis{GenDoc: &types.GenesisDoc{InitialHeight: 7}, Validators: validators} + registry, err := p2p.OpenEpochRegistry(genesis.GenDoc, validators, t.TempDir()) + require.NoError(t, err) + require.NoError(t, registry.TestPushEpoch(0, map[atypes.PublicKey]uint64{keys[0].Public(): 3, keys[1].Public(): 4})) + require.NoError(t, registry.TestPushEpoch(1, map[atypes.PublicKey]uint64{keys[2].Public(): 5})) + return testEpochChain{genesis: genesis, registry: registry} +} + +// firstHeightOf returns the first road height of epoch idx. +func (c testEpochChain) firstHeightOf(idx atypes.EpochIndex) int64 { + return int64(c.registry.MustEpoch(idx).RoadRange().First) //nolint:gosec // test roads fit in an int64. +} + +// committedCheckpoint returns a FlatKV checkpoint dir whose state holds row as its epoch_registry +// row, or no row when row is None. +func committedCheckpoint(t *testing.T, row utils.Option[[]byte]) string { + t.Helper() + cfg := flatkvconfig.DefaultTestConfig(t) + cfg.DataDir = filepath.Join(t.TempDir(), "flatkv") + cfg.SnapshotInterval = 1 + wal, err := flatkv.OpenStateWAL(cfg) + require.NoError(t, err) + store, err := flatkv.NewCommitStore(t.Context(), cfg, wal) + require.NoError(t, err) + require.NoError(t, store.LoadLatest()) + changeSets := []*proto.NamedChangeSet{{Name: "bank", Changeset: proto.ChangeSet{Pairs: []*proto.KVPair{{Key: []byte{1}, Value: []byte{1}}}}}} + if raw, ok := row.Get(); ok { + changeSets = append(changeSets, &proto.NamedChangeSet{ + Name: "autobahn", + Changeset: proto.ChangeSet{Pairs: []*proto.KVPair{{Key: []byte("epoch_registry"), Value: raw}}}, + }) + } + require.NoError(t, store.ApplyChangeSets(1, changeSets)) + _, err = store.Commit(1) + require.NoError(t, err) + require.NoError(t, store.FlushSnapshots()) + dir := filepath.Join(t.TempDir(), "checkpoint") + require.NoError(t, flatkv.HardLinkSnapshot(cfg.DataDir, 1, dir)) + require.NoError(t, store.Close()) + return dir +} + +// seedLayout returns a layout whose epoch registry is seeded from checkpoint at height. +func seedLayout(t *testing.T, genesis EpochGenesis, checkpoint string, height int64) (AutobahnLayout, error) { + t.Helper() + psd := t.TempDir() + layout := AutobahnLayout{PersistentStateDir: psd, EpochRegistryDir: epoch.SnapshotDir(psd)} + staging := filepath.Join(t.TempDir(), "staging") + if err := PrepareEpochRegistry(genesis, checkpoint, height, staging); err != nil { + return layout, err + } + return layout, InstallEpochRegistry(layout, staging) +} + +func openLayoutRegistry(t *testing.T, genesis EpochGenesis, layout AutobahnLayout) *epoch.Registry { + t.Helper() + registry, err := p2p.OpenEpochRegistry(genesis.GenDoc, genesis.Validators, layout.PersistentStateDir) + require.NoError(t, err) + return registry +} + +func TestSeedEpochRegistryFromEpochRegistryRow(t *testing.T) { + rng := utils.TestRng() + chain := newTestEpochChain(t, rng) + checkpoint := committedCheckpoint(t, utils.Some(chain.registry.MarshalSnapshot())) + + layout, err := seedLayout(t, chain.genesis, checkpoint, chain.firstHeightOf(2)) + require.NoError(t, err) + + registry := openLayoutRegistry(t, chain.genesis, layout) + for idx := atypes.EpochIndex(0); idx <= 3; idx++ { + require.True(t, epoch.EpochEqual(chain.registry.MustEpoch(idx), registry.MustEpoch(idx))) + } +} + +func TestInstallEpochRegistryWithoutRowReplacesTheOldSnapshotWithGenesis(t *testing.T) { + rng := utils.TestRng() + chain := newTestEpochChain(t, rng) + psd := t.TempDir() + layout := AutobahnLayout{PersistentStateDir: psd, EpochRegistryDir: epoch.SnapshotDir(psd)} + require.NoError(t, epoch.RestoreSnapshot(psd, utils.Some(chain.registry.MarshalSnapshot()))) + staging := filepath.Join(t.TempDir(), "staging") + + require.NoError(t, PrepareEpochRegistry(chain.genesis, committedCheckpoint(t, utils.None[[]byte]()), chain.firstHeightOf(0), staging)) + require.True(t, epoch.EpochEqual(chain.registry.MustEpoch(3), openLayoutRegistry(t, chain.genesis, layout).MustEpoch(3))) + require.NoError(t, InstallEpochRegistry(layout, staging)) + + registry := openLayoutRegistry(t, chain.genesis, layout) + require.True(t, epoch.EpochEqual(chain.registry.MustEpoch(1), registry.MustEpoch(1))) + _, err := registry.Epoch(2) + require.ErrorIs(t, err, atypes.ErrNotFound) +} + +func TestPrepareEpochRegistryRefusesAHeightWithoutTheNextEpoch(t *testing.T) { + rng := utils.TestRng() + chain := newTestEpochChain(t, rng) + for name, tc := range map[string]struct { + row utils.Option[[]byte] + height int64 + }{ + "row through epoch 3, height in epoch 3": {utils.Some(chain.registry.MarshalSnapshot()), chain.firstHeightOf(3)}, + "no row, height in epoch 1": {utils.None[[]byte](), chain.firstHeightOf(1)}, + } { + t.Run(name, func(t *testing.T) { + _, err := seedLayout(t, chain.genesis, committedCheckpoint(t, tc.row), tc.height) + require.ErrorIs(t, err, atypes.ErrNotFound) + }) + } +} + +func TestPrepareEpochRegistryRefusesAHeightInAPrunedEpoch(t *testing.T) { + rng := utils.TestRng() + chain := newTestEpochChain(t, rng) + height := chain.firstHeightOf(1) + require.NoError(t, chain.registry.PruneBefore(2)) + + _, err := seedLayout(t, chain.genesis, committedCheckpoint(t, utils.Some(chain.registry.MarshalSnapshot())), height) + require.ErrorIs(t, err, atypes.ErrPruned) +} + +func TestPrepareEpochRegistryRefusesTheLastRoadOfAnEpoch(t *testing.T) { + rng := utils.TestRng() + chain := newTestEpochChain(t, rng) + checkpoint := committedCheckpoint(t, utils.Some(chain.registry.MarshalSnapshot())) + + _, err := seedLayout(t, chain.genesis, checkpoint, chain.firstHeightOf(2)-1) + require.ErrorIs(t, err, ErrClosingRoad) +} + +func TestPrepareEpochRegistryRefusesARowWithTheGenesisEpochsOfAnotherChain(t *testing.T) { + rng := utils.TestRng() + chain := newTestEpochChain(t, rng) + other := newTestEpochChain(t, rng) + + _, err := seedLayout(t, chain.genesis, committedCheckpoint(t, utils.Some(other.registry.MarshalSnapshot())), chain.firstHeightOf(2)) + require.Error(t, err) +} + +func TestPrepareEpochRegistryAcceptsARowOfAnotherChainPrunedPastGenesis(t *testing.T) { + rng := utils.TestRng() + chain := newTestEpochChain(t, rng) + other := newTestEpochChain(t, rng) + height := other.firstHeightOf(2) + require.NoError(t, other.registry.PruneBefore(2)) + + layout, err := seedLayout(t, chain.genesis, committedCheckpoint(t, utils.Some(other.registry.MarshalSnapshot())), height) + require.NoError(t, err) + require.True(t, epoch.EpochEqual(other.registry.MustEpoch(3), openLayoutRegistry(t, chain.genesis, layout).MustEpoch(3))) +} + +func TestPrepareEpochRegistryRejectsMalformedRow(t *testing.T) { + rng := utils.TestRng() + chain := newTestEpochChain(t, rng) + + _, err := seedLayout(t, chain.genesis, committedCheckpoint(t, utils.Some([]byte{0xff, 0x01})), chain.firstHeightOf(0)) + require.Error(t, err) +} + +func TestPrepareEpochRegistryRejectsEmptyRow(t *testing.T) { + rng := utils.TestRng() + chain := newTestEpochChain(t, rng) + + _, err := seedLayout(t, chain.genesis, committedCheckpoint(t, utils.Some([]byte{})), chain.firstHeightOf(0)) + require.ErrorIs(t, err, ErrEmptyEpochRegistryRow) +} + +func TestResolveEpochGenesisReadsTheGenesisValidators(t *testing.T) { + conf := layoutConfig(t, t.TempDir()) + genDoc := genesisAt(1) + genesis, err := ResolveEpochGenesis(conf, genDoc) + require.NoError(t, err) + require.Equal(t, genDoc, genesis.GenDoc) + validator := makeValidator([]byte("layout-validator"), []byte("layout-node"), "localhost:26660") + require.Equal(t, []atypes.PublicKey{validator.ValidatorKey}, genesis.Validators) +} diff --git a/sei-tendermint/node/testonly.go b/sei-tendermint/node/testonly.go new file mode 100644 index 0000000000..5d95f3487b --- /dev/null +++ b/sei-tendermint/node/testonly.go @@ -0,0 +1,16 @@ +package node + +import ( + "github.com/sei-protocol/sei-chain/sei-tendermint/internal/p2p" +) + +// GenesisEpochRegistryRowForTesting returns the epoch_registry row of a chain whose epoch +// registry holds only the genesis epochs of genesis. It writes registry files under scratchDir. +// Intended for use in tests only. +func GenesisEpochRegistryRowForTesting(genesis EpochGenesis, scratchDir string) ([]byte, error) { + registry, err := p2p.OpenEpochRegistry(genesis.GenDoc, genesis.Validators, scratchDir) + if err != nil { + return nil, err + } + return registry.MarshalSnapshot(), nil +} From c685ed9dda6741b2cb65063b34a118c916efa0f1 Mon Sep 17 00:00:00 2001 From: blindchaser Date: Mon, 16 Nov 2026 15:30:43 -0500 Subject: [PATCH 07/12] flatkv: open Pebble read-only through one helper readCommittedVersion and the snapshot dir readers held the same read-only pebble.Open. Both now call openPebbleReadOnly, which returns the Pebble error unchanged, so the error text and the ErrDBDoesNotExist check in readCommittedVersion are the same as before. Co-authored-by: Cursor --- sei-db/state_db/sc/flatkv/snapshot_read.go | 8 +++++--- sei-db/state_db/sc/flatkv/store_meta.go | 5 +---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/sei-db/state_db/sc/flatkv/snapshot_read.go b/sei-db/state_db/sc/flatkv/snapshot_read.go index 02e176b4ee..64b4172645 100644 --- a/sei-db/state_db/sc/flatkv/snapshot_read.go +++ b/sei-db/state_db/sc/flatkv/snapshot_read.go @@ -94,7 +94,7 @@ type snapshotDataDB struct { func openSnapshotDataDBs(dir string) ([]snapshotDataDB, error) { dbs := make([]snapshotDataDB, 0, len(dataDBDirs)) for _, name := range dataDBDirs { - db, err := openSnapshotPebble(filepath.Join(dir, name)) + db, err := openPebbleReadOnly(filepath.Join(dir, name)) if err != nil { closeSnapshotDataDBs(dbs) return nil, fmt.Errorf("open %s database: %w", name, err) @@ -232,7 +232,7 @@ func ReadSnapshotMiscValue(dir, moduleName string, key []byte) ([]byte, bool, er return nil, false, fmt.Errorf("module %q is not a misc module", moduleName) } dbDir := filepath.Join(dir, miscDBDir) - db, err := openSnapshotPebble(dbDir) + db, err := openPebbleReadOnly(dbDir) if err != nil { return nil, false, fmt.Errorf("open %q read-only: %w", dbDir, err) } @@ -256,7 +256,9 @@ func ReadSnapshotMiscValue(dir, moduleName string, key []byte) ([]byte, bool, er return row.GetValue(), true, nil } -func openSnapshotPebble(dbDir string) (*pebble.DB, error) { +// openPebbleReadOnly opens the Pebble DB at dbDir read-only and returns the Pebble error unchanged. It +// modifies none of the database's files, and creates only a LOCK file when there is none. +func openPebbleReadOnly(dbDir string) (*pebble.DB, error) { return pebble.Open(dbDir, &pebble.Options{ ReadOnly: true, FormatMajorVersion: pebble.FormatVirtualSSTables, diff --git a/sei-db/state_db/sc/flatkv/store_meta.go b/sei-db/state_db/sc/flatkv/store_meta.go index 3b7bdc4eb4..1f7a8444f2 100644 --- a/sei-db/state_db/sc/flatkv/store_meta.go +++ b/sei-db/state_db/sc/flatkv/store_meta.go @@ -376,10 +376,7 @@ func readCommittedVersion(dbDir string) (int64, error) { } return 0, fmt.Errorf("stat %q: %w", dbDir, err) } - db, err := pebble.Open(dbDir, &pebble.Options{ - ReadOnly: true, - FormatMajorVersion: pebble.FormatVirtualSSTables, - }) + db, err := openPebbleReadOnly(dbDir) if err != nil { // The directory can exist while the database in it does not: createWorkingDir makes an empty one // for every data DB the snapshot it clones from does not have, and only the store's own open From 290f08f35c62bde609c1e652e523366f5b6f6d3c Mon Sep 17 00:00:00 2001 From: blindchaser Date: Mon, 16 Nov 2026 15:45:31 -0500 Subject: [PATCH 08/12] flatkv: clear flushed rows before an import worker reuses its buffer A flush returns its rows' bytes to the import byte budget, but resetting ltMutations to length zero kept the rows reachable through the backing array. A byte-triggered flush can be shorter than the batch before it, so the next batch did not overwrite every slot, and rows that the budget no longer counted stayed in memory. Co-authored-by: Cursor --- .../state_db/sc/flatkv/import_export_test.go | 28 +++++++++++++++++++ sei-db/state_db/sc/flatkv/importer.go | 3 ++ 2 files changed, 31 insertions(+) diff --git a/sei-db/state_db/sc/flatkv/import_export_test.go b/sei-db/state_db/sc/flatkv/import_export_test.go index 63eab5334c..f9773b1c50 100644 --- a/sei-db/state_db/sc/flatkv/import_export_test.go +++ b/sei-db/state_db/sc/flatkv/import_export_test.go @@ -563,6 +563,34 @@ func TestImporterFailsWhenItReleasesMoreBytesThanItHolds(t *testing.T) { require.ErrorContains(t, imp.Close(), "after a release of 1 bytes") } +// TestImportWorkerFlushKeepsNoFlushedRows makes a worker flush a long batch and then a shorter one. The bytes of +// a flushed row return to the budget, so a row that the worker still references afterwards is memory that the +// budget does not count. +func TestImportWorkerFlushKeepsNoFlushedRows(t *testing.T) { + withImportByteBudget(t, 4096) + s, imp := newKVImporterForTest(t, 1) + defer s.Close() + + big := int(workerFlushBytes(4096)) + row := 0 + for _, smallRows := range []int{10, 3} { + for range smallRows { + require.NoError(t, imp.AddNode(budgetRow(row, 1))) + row++ + } + require.NoError(t, imp.AddNode(budgetRow(row, big))) + row++ + } + require.NoError(t, imp.Close()) + + w := imp.workers[miscDBDir] + require.Equal(t, int64(2), w.flushes) + for i, m := range w.ltMutations[:cap(w.ltMutations)] { + require.Nil(t, m.Key, "slot %d still holds a flushed key", i) + require.Nil(t, m.NewValue, "slot %d still holds a flushed value", i) + } +} + func TestImportSurvivesReopen(t *testing.T) { src := setupTestStore(t) defer src.Close() diff --git a/sei-db/state_db/sc/flatkv/importer.go b/sei-db/state_db/sc/flatkv/importer.go index c63d8c9e18..5fbe21b396 100644 --- a/sei-db/state_db/sc/flatkv/importer.go +++ b/sei-db/state_db/sc/flatkv/importer.go @@ -216,6 +216,9 @@ func (w *dbWorker) flush() (err error) { return fmt.Errorf("%s close batch: %w", w.dir, closeErr) } w.batch = w.db.NewBatch() + // The deferred release returns these rows' bytes to the budget, so the reused backing array must not keep + // them reachable: a later, shorter batch would not overwrite every slot. + clear(w.ltMutations) w.ltMutations = w.ltMutations[:0] w.bytes = 0 return nil From 988890c0d86c7539ac49c025da942546df7b584f Mon Sep 17 00:00:00 2001 From: blindchaser Date: Mon, 16 Nov 2026 17:43:16 -0500 Subject: [PATCH 09/12] chore(node): address review of the epoch registry seed State the crash contract of InstallEpochRegistry, return the genesis validators in key order, and rename the test for a row pruned past genesis so that its name states the limit. Co-authored-by: Cursor --- sei-tendermint/node/autobahn_layout_test.go | 2 +- sei-tendermint/node/epoch_seed.go | 8 +++++--- sei-tendermint/node/epoch_seed_test.go | 3 ++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/sei-tendermint/node/autobahn_layout_test.go b/sei-tendermint/node/autobahn_layout_test.go index b8dd6978a5..fb60b2e082 100644 --- a/sei-tendermint/node/autobahn_layout_test.go +++ b/sei-tendermint/node/autobahn_layout_test.go @@ -103,7 +103,7 @@ func TestAutobahnLayoutCoversWhatAFullnodeStoresOpen(t *testing.T) { require.NoError(t, err, "the node did not create %s", dir) require.True(t, info.IsDir()) } - dirs := append(storeDirs, layout.StateSyncProofDir) + dirs := slices.Concat(storeDirs, []string{layout.StateSyncProofDir}) files := []string{layout.RestoreMarkerFile, layout.RestoreProofFile} isParent := func(path string) bool { for _, p := range append(slices.Clone(dirs), files...) { diff --git a/sei-tendermint/node/epoch_seed.go b/sei-tendermint/node/epoch_seed.go index d0d9c2c3f5..b2229613d9 100644 --- a/sei-tendermint/node/epoch_seed.go +++ b/sei-tendermint/node/epoch_seed.go @@ -31,13 +31,14 @@ type EpochGenesis struct { Validators []atypes.PublicKey } -// ResolveEpochGenesis returns the epoch genesis of the node that conf and genDoc configure. +// ResolveEpochGenesis returns the epoch genesis of the node that conf and genDoc configure, with +// the genesis validators in key order. func ResolveEpochGenesis(conf *config.Config, genDoc *types.GenesisDoc) (EpochGenesis, error) { _, validators, err := loadAutobahnCommittee(conf.AutobahnConfigFile) if err != nil { return EpochGenesis{}, err } - return EpochGenesis{GenDoc: genDoc, Validators: slices.Collect(maps.Keys(validators))}, nil + return EpochGenesis{GenDoc: genDoc, Validators: slices.SortedFunc(maps.Keys(validators), atypes.PublicKey.Compare)}, nil } // PrepareEpochRegistry writes under stagingDir the epoch registry snapshot of the epoch_registry @@ -87,7 +88,8 @@ func requireEpochsForRoad(registry *epoch.Registry, road atypes.RoadIndex) error // InstallEpochRegistry replaces the epoch registry snapshot of layout with the one that // PrepareEpochRegistry wrote under stagingDir. stagingDir must be on the filesystem of the -// persistent state dir. +// persistent state dir. It is not atomic: after a crash, the caller must run it again before +// the node starts. func InstallEpochRegistry(layout AutobahnLayout, stagingDir string) error { if err := os.RemoveAll(layout.EpochRegistryDir); err != nil { return fmt.Errorf("remove the epoch registry snapshot: %w", err) diff --git a/sei-tendermint/node/epoch_seed_test.go b/sei-tendermint/node/epoch_seed_test.go index bfe88123f9..083a213864 100644 --- a/sei-tendermint/node/epoch_seed_test.go +++ b/sei-tendermint/node/epoch_seed_test.go @@ -166,7 +166,8 @@ func TestPrepareEpochRegistryRefusesARowWithTheGenesisEpochsOfAnotherChain(t *te require.Error(t, err) } -func TestPrepareEpochRegistryAcceptsARowOfAnotherChainPrunedPastGenesis(t *testing.T) { +// A row pruned past genesis is not bound to this chain here; AppQC(H+1) catches it. +func TestPrepareEpochRegistryCannotBindARowPrunedPastGenesis(t *testing.T) { rng := utils.TestRng() chain := newTestEpochChain(t, rng) other := newTestEpochChain(t, rng) From 148e4a945ae6e2b2a04820cbb0792a03df289e75 Mon Sep 17 00:00:00 2001 From: blindchaser Date: Mon, 16 Nov 2026 18:37:20 -0500 Subject: [PATCH 10/12] fix(node): keep the installed epoch registry when no snapshot is staged InstallEpochRegistry removed the installed snapshot before it checked the staged one, and ignored a missing source, so a second call with the same staging dir left only the genesis epochs and reported success. It now refuses a missing staged snapshot before it removes anything. Co-authored-by: Cursor --- sei-tendermint/node/epoch_seed.go | 15 ++++++++++----- sei-tendermint/node/epoch_seed_test.go | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/sei-tendermint/node/epoch_seed.go b/sei-tendermint/node/epoch_seed.go index b2229613d9..8aa37da3e9 100644 --- a/sei-tendermint/node/epoch_seed.go +++ b/sei-tendermint/node/epoch_seed.go @@ -86,15 +86,20 @@ func requireEpochsForRoad(registry *epoch.Registry, road atypes.RoadIndex) error return nil } -// InstallEpochRegistry replaces the epoch registry snapshot of layout with the one that -// PrepareEpochRegistry wrote under stagingDir. stagingDir must be on the filesystem of the -// persistent state dir. It is not atomic: after a crash, the caller must run it again before -// the node starts. +// InstallEpochRegistry moves the epoch registry snapshot that PrepareEpochRegistry wrote under +// stagingDir into layout, replacing the one there. stagingDir must be on the filesystem of the +// persistent state dir. It returns an error, and keeps the installed snapshot, when stagingDir +// holds no prepared snapshot. It is not atomic: after a crash, the caller must run +// PrepareEpochRegistry and InstallEpochRegistry again before the node starts. func InstallEpochRegistry(layout AutobahnLayout, stagingDir string) error { + staged := epoch.SnapshotDir(stagingDir) + if _, err := os.Stat(staged); err != nil { + return fmt.Errorf("find the prepared epoch registry snapshot: %w", err) + } if err := os.RemoveAll(layout.EpochRegistryDir); err != nil { return fmt.Errorf("remove the epoch registry snapshot: %w", err) } - if err := os.Rename(epoch.SnapshotDir(stagingDir), layout.EpochRegistryDir); err != nil && !errors.Is(err, os.ErrNotExist) { + if err := os.Rename(staged, layout.EpochRegistryDir); err != nil { return fmt.Errorf("install the epoch registry snapshot: %w", err) } return dbutils.SyncDir(filepath.Dir(layout.EpochRegistryDir)) diff --git a/sei-tendermint/node/epoch_seed_test.go b/sei-tendermint/node/epoch_seed_test.go index 083a213864..4f1c76301c 100644 --- a/sei-tendermint/node/epoch_seed_test.go +++ b/sei-tendermint/node/epoch_seed_test.go @@ -1,6 +1,7 @@ package node import ( + "os" "path/filepath" "testing" @@ -103,6 +104,20 @@ func TestSeedEpochRegistryFromEpochRegistryRow(t *testing.T) { } } +func TestInstallEpochRegistryRefusesASecondInstallFromTheSameStaging(t *testing.T) { + rng := utils.TestRng() + chain := newTestEpochChain(t, rng) + checkpoint := committedCheckpoint(t, utils.Some(chain.registry.MarshalSnapshot())) + psd := t.TempDir() + layout := AutobahnLayout{PersistentStateDir: psd, EpochRegistryDir: epoch.SnapshotDir(psd)} + staging := filepath.Join(t.TempDir(), "staging") + require.NoError(t, PrepareEpochRegistry(chain.genesis, checkpoint, chain.firstHeightOf(2), staging)) + require.NoError(t, InstallEpochRegistry(layout, staging)) + + require.ErrorIs(t, InstallEpochRegistry(layout, staging), os.ErrNotExist) + require.True(t, epoch.EpochEqual(chain.registry.MustEpoch(3), openLayoutRegistry(t, chain.genesis, layout).MustEpoch(3))) +} + func TestInstallEpochRegistryWithoutRowReplacesTheOldSnapshotWithGenesis(t *testing.T) { rng := utils.TestRng() chain := newTestEpochChain(t, rng) From eb3d31f7e8f0eaba87616e16460e2ba6db9b64ac Mon Sep 17 00:00:00 2001 From: blindchaser Date: Mon, 16 Nov 2026 19:28:35 -0500 Subject: [PATCH 11/12] docs(node): name the statesync export and import commands in layout comments Co-authored-by: Cursor --- sei-tendermint/node/autobahn_layout.go | 3 ++- sei-tendermint/node/autobahn_layout_test.go | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/sei-tendermint/node/autobahn_layout.go b/sei-tendermint/node/autobahn_layout.go index 90fda98e36..d3c190f9d4 100644 --- a/sei-tendermint/node/autobahn_layout.go +++ b/sei-tendermint/node/autobahn_layout.go @@ -21,7 +21,8 @@ type AutobahnLayout struct { StateStoreDir string AppHashVaultDir string RestoreMarkerFile string - // RestoreProofFile is the state sync proof that restore installs for the seed to read. + // RestoreProofFile is the state sync proof that `seid statesync import` installs for the seed + // to read. RestoreProofFile string // StateSyncProofDir holds the state sync proof files that a running node writes next to its // FlatKV checkpoints. diff --git a/sei-tendermint/node/autobahn_layout_test.go b/sei-tendermint/node/autobahn_layout_test.go index fb60b2e082..23a895c227 100644 --- a/sei-tendermint/node/autobahn_layout_test.go +++ b/sei-tendermint/node/autobahn_layout_test.go @@ -78,8 +78,8 @@ func TestResolveAutobahnLayoutRequiresAutobahnConfig(t *testing.T) { // TestAutobahnLayoutCoversWhatAFullnodeStoresOpen requires every entry that a fullnode's storage // manager and epoch registry create under the persistent state dir to be a layout path, inside one, -// or a parent of one. A new store under that dir fails here until state sync decides whether restore must require -// it empty or create must pack it. +// or a parent of one. A new store under that dir fails here until state sync decides whether import must require +// it empty or export must pack it. func TestAutobahnLayoutCoversWhatAFullnodeStoresOpen(t *testing.T) { conf := layoutConfig(t, filepath.Join("data", "autobahn")) storage := gigaconfig.DefaultConfig.Storage From 06bbf5aa0158a23c1b919a198abd8c24c31ac53d Mon Sep 17 00:00:00 2001 From: blindchaser Date: Mon, 16 Nov 2026 23:19:11 -0500 Subject: [PATCH 12/12] refactor(epoch): restore the epoch registry from the epoch_registry value Add epoch.NewRegistryFromSnapshot, which opens a registry from the FlatKV epoch_registry value with the same checks as NewRegistry and persists it when a state dir is set. Remove epoch.RestoreSnapshot. node.OpenCheckpointEpochs opens the checkpoint registry in memory, and InstallEpochRegistry writes it into the persistent state dir with the new constructor. There is no staging dir and no rename, and an install can run again with the same CheckpointEpochs. Co-authored-by: Cursor --- .../autobahn/data/restore_epoch_test.go | 5 +- .../internal/autobahn/epoch/registry.go | 77 +++++++++++++---- .../internal/autobahn/epoch/seed_test.go | 84 +++++++++++-------- .../internal/autobahn/epoch/snapshot.go | 28 ------- .../internal/p2p/giga_router_common.go | 55 ++++++++++-- sei-tendermint/node/epoch_seed.go | 57 +++++++------ sei-tendermint/node/epoch_seed_test.go | 47 ++++++----- 7 files changed, 216 insertions(+), 137 deletions(-) diff --git a/sei-tendermint/internal/autobahn/data/restore_epoch_test.go b/sei-tendermint/internal/autobahn/data/restore_epoch_test.go index 8b77b053e5..0196f43398 100644 --- a/sei-tendermint/internal/autobahn/data/restore_epoch_test.go +++ b/sei-tendermint/internal/autobahn/data/restore_epoch_test.go @@ -10,12 +10,13 @@ import ( "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils/require" ) -// restoredRegistry opens a registry with the genesis of source from a snapshot restored from +// restoredRegistry opens a registry with the genesis of source from a state dir restored from // source's epoch_registry row, as a state sync restore leaves it. func restoredRegistry(t *testing.T, source *epoch.Registry, genesis *types.Epoch) *epoch.Registry { t.Helper() dir := t.TempDir() - require.NoError(t, epoch.RestoreSnapshot(dir, utils.Some(source.MarshalSnapshot()))) + _, err := epoch.NewRegistryFromSnapshot(genesis.Committee(), genesis.RoadRange().First, time.Time{}, source.MarshalSnapshot(), utils.Some(dir)) + require.NoError(t, err) return utils.OrPanic1(epoch.NewRegistry(genesis.Committee(), genesis.RoadRange().First, time.Time{}, utils.Some(dir))) } diff --git a/sei-tendermint/internal/autobahn/epoch/registry.go b/sei-tendermint/internal/autobahn/epoch/registry.go index e349f43738..44d2d4d97d 100644 --- a/sei-tendermint/internal/autobahn/epoch/registry.go +++ b/sei-tendermint/internal/autobahn/epoch/registry.go @@ -9,6 +9,7 @@ import ( "github.com/sei-protocol/sei-chain/sei-tendermint/internal/autobahn/consensus/persist" "github.com/sei-protocol/sei-chain/sei-tendermint/internal/autobahn/pb" "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils" + "google.golang.org/protobuf/proto" ) // epochLength is the number of road indices per epoch. @@ -46,27 +47,75 @@ func NewRegistry( if state, err = persistedRegistryConv.DecodeReq(raw); err != nil { return nil, fmt.Errorf("restore epoch snapshot: %w", err) } - for idx := state.liveRange.First; idx < state.liveRange.Next; idx++ { - epoch := state.live[idx] - if got, want := epoch.RoadRange(), r.roadRange(idx); got != want { - return nil, fmt.Errorf("restore epoch snapshot: epoch %d roads %v, want %v", idx, got, want) - } - if state.liveRange.First < idx { - if err := checkDerived(state.live[idx-1], epoch); err != nil { - return nil, fmt.Errorf("restore epoch snapshot: epoch %d: %w", idx, err) - } - } + if err := r.checkRestored(state, committee); err != nil { + return nil, fmt.Errorf("restore epoch snapshot: %w", err) } - for idx := range types.EpochIndex(2) { - if epoch, ok := state.live[idx]; ok && !epoch.Committee().Equal(committee) { - return nil, fmt.Errorf("restore epoch snapshot: epoch %d does not match genesis", idx) + } + r.publish(state) + return r, nil +} + +// NewRegistryFromSnapshot opens a registry whose state is raw, the protobuf encoding of the +// PersistedEpochRegistry that an app state holds as its epoch_registry value. It checks raw as +// NewRegistry checks a persisted snapshot. With stateDir set, it persists raw there as the newest +// snapshot; stateDir None disables persistence. +func NewRegistryFromSnapshot( + committee *types.Committee, + firstRoad types.RoadIndex, + genesisTimestamp time.Time, + raw []byte, + stateDir utils.Option[string], +) (*Registry, error) { + var snapshot pb.PersistedEpochRegistry + if err := proto.Unmarshal(raw, &snapshot); err != nil { + return nil, fmt.Errorf("decode epoch registry: %w", err) + } + state, err := persistedRegistryConv.DecodeReq(&snapshot) + if err != nil { + return nil, fmt.Errorf("decode epoch registry: %w", err) + } + r := &Registry{firstRoad: firstRoad, genesisTimestamp: genesisTimestamp} + if err := r.checkRestored(state, committee); err != nil { + return nil, fmt.Errorf("epoch registry: %w", err) + } + if r.persister, _, err = openEpochSnapshot(stateDir); err != nil { + return nil, err + } + if err := r.persister.Persist(persistedRegistryConv.Encode(state)); err != nil { + return nil, fmt.Errorf("persist epoch registry: %w", err) + } + r.publish(state) + return r, nil +} + +// checkRestored returns an error unless the live epochs of state have the roads of r, each +// committee derives from the one before, and the epochs 0 and 1 that state holds have the genesis +// committee. +func (r *Registry) checkRestored(state *persistedRegistry, committee *types.Committee) error { + for idx := state.liveRange.First; idx < state.liveRange.Next; idx++ { + epoch := state.live[idx] + if got, want := epoch.RoadRange(), r.roadRange(idx); got != want { + return fmt.Errorf("epoch %d roads %v, want %v", idx, got, want) + } + if state.liveRange.First < idx { + if err := checkDerived(state.live[idx-1], epoch); err != nil { + return fmt.Errorf("epoch %d: %w", idx, err) } } } + for idx := range types.EpochIndex(2) { + if epoch, ok := state.live[idx]; ok && !epoch.Committee().Equal(committee) { + return fmt.Errorf("epoch %d does not match genesis", idx) + } + } + return nil +} + +// publish makes state the current state of r. +func (r *Registry) publish(state *persistedRegistry) { stateSend := new(utils.NewAtomicSend(state)) r.state = utils.NewMutex(stateSend) r.stateRecv = stateSend.Subscribe() - return r, nil } func (r *Registry) roadRange(idx types.EpochIndex) types.RoadRange { diff --git a/sei-tendermint/internal/autobahn/epoch/seed_test.go b/sei-tendermint/internal/autobahn/epoch/seed_test.go index 1cef9464cb..5a0454f951 100644 --- a/sei-tendermint/internal/autobahn/epoch/seed_test.go +++ b/sei-tendermint/internal/autobahn/epoch/seed_test.go @@ -2,6 +2,7 @@ package epoch import ( "errors" + "os" "testing" "time" @@ -10,88 +11,99 @@ import ( "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils/require" ) +// fromSnapshot opens a registry from raw with genesis as its epoch 0. +func fromSnapshot(genesis *types.Epoch, raw []byte, stateDir utils.Option[string]) (*Registry, error) { + return NewRegistryFromSnapshot(genesis.Committee(), genesis.RoadRange().First, time.Time{}, raw, stateDir) +} + // openGenesis opens a registry under dir with genesis as its epoch 0. func openGenesis(genesis *types.Epoch, dir string) (*Registry, error) { return NewRegistry(genesis.Committee(), genesis.RoadRange().First, time.Time{}, utils.Some(dir)) } -// openGenesisOf opens a registry under dir with the genesis of source. -func openGenesisOf(source *Registry, dir string) (*Registry, error) { - return openGenesis(source.MustEpoch(0), dir) +// requireSourceEpochs requires registry to hold the live epochs of source, which are current-1 +// through current+1. +func requireSourceEpochs(t *testing.T, source, registry *Registry, current types.EpochIndex) { + t.Helper() + for idx := current - 1; idx <= current+1; idx++ { + require.True(t, EpochEqual(source.MustEpoch(idx), utils.OrPanic1(registry.Epoch(idx)))) + } + if current >= 2 { + _, err := registry.Epoch(current - 2) + require.True(t, errors.Is(err, types.ErrPruned)) + } + _, err := registry.Epoch(current + 2) + require.True(t, errors.Is(err, types.ErrNotFound)) } -func TestRestoreSnapshotOpensWithSourceEpochs(t *testing.T) { +func TestNewRegistryFromSnapshotOpensWithSourceEpochs(t *testing.T) { rng := utils.TestRng() for _, current := range []types.EpochIndex{1, 2, 5} { source, keys := GenRegistryThrough(rng, 3, current) genesis := source.MustEpoch(0) require.NoError(t, source.TestPushEpoch(current-1, map[types.PublicKey]uint64{keys[0].Public(): 5})) require.NoError(t, source.PruneBefore(current-1)) - dir := t.TempDir() - require.NoError(t, RestoreSnapshot(dir, utils.Some(source.MarshalSnapshot()))) - registry, err := openGenesis(genesis, dir) + registry, err := fromSnapshot(genesis, source.MarshalSnapshot(), utils.None[string]()) require.NoError(t, err) - - for idx := current - 1; idx <= current+1; idx++ { - require.True(t, EpochEqual(source.MustEpoch(idx), utils.OrPanic1(registry.Epoch(idx)))) - } - if current >= 2 { - _, err := registry.Epoch(current - 2) - require.True(t, errors.Is(err, types.ErrPruned)) - } - _, err = registry.Epoch(current + 2) - require.True(t, errors.Is(err, types.ErrNotFound)) + requireSourceEpochs(t, source, registry, current) } } -func TestRestoreSnapshotWithoutRowOpensGenesis(t *testing.T) { +func TestNewRegistryFromSnapshotPersistsTheSnapshotForNewRegistry(t *testing.T) { rng := utils.TestRng() - source, _ := GenRegistryThrough(rng, 3, 3) + source, keys := GenRegistryThrough(rng, 3, 5) + genesis := source.MustEpoch(0) + require.NoError(t, source.TestPushEpoch(4, map[types.PublicKey]uint64{keys[0].Public(): 5})) + require.NoError(t, source.PruneBefore(4)) dir := t.TempDir() - require.NoError(t, RestoreSnapshot(dir, utils.Some(source.MarshalSnapshot()))) + older, err := openGenesis(genesis, dir) + require.NoError(t, err) + require.NoError(t, older.TestPushEpoch(0, map[types.PublicKey]uint64{keys[1].Public(): 5})) - require.NoError(t, RestoreSnapshot(dir, utils.None[[]byte]())) - registry, err := openGenesisOf(source, dir) + _, err = fromSnapshot(genesis, source.MarshalSnapshot(), utils.Some(dir)) require.NoError(t, err) - require.True(t, EpochEqual(source.MustEpoch(0), utils.OrPanic1(registry.Epoch(0)))) - require.True(t, EpochEqual(source.MustEpoch(1), utils.OrPanic1(registry.Epoch(1)))) - _, err = registry.Epoch(2) - require.True(t, errors.Is(err, types.ErrNotFound)) + registry, err := openGenesis(genesis, dir) + require.NoError(t, err) + requireSourceEpochs(t, source, registry, 5) } -func TestRestoreSnapshotWithGenesisEpochsOfAnotherChainDoesNotOpen(t *testing.T) { +func TestNewRegistryFromSnapshotRejectsTheGenesisEpochsOfAnotherChain(t *testing.T) { rng := utils.TestRng() source, _ := GenRegistryThrough(rng, 3, 2) other, _ := GenRegistryThrough(rng, 3, 2) - dir := t.TempDir() - require.NoError(t, RestoreSnapshot(dir, utils.Some(source.MarshalSnapshot()))) - _, err := openGenesisOf(other, dir) + _, err := fromSnapshot(other.MustEpoch(0), source.MarshalSnapshot(), utils.None[string]()) require.Error(t, err) } -func TestRestoreSnapshotPrunedPastGenesisOpensOnAnotherChainWithTheSameFirstRoad(t *testing.T) { +func TestNewRegistryFromSnapshotPrunedPastGenesisOpensOnAnotherChainWithTheSameFirstRoad(t *testing.T) { rng := utils.TestRng() source, _ := GenRegistryThrough(rng, 3, 3) firstRoad := source.MustEpoch(0).RoadRange().First require.NoError(t, source.PruneBefore(2)) otherCommittee, _ := types.GenCommittee(rng, 3) - dir := t.TempDir() - require.NoError(t, RestoreSnapshot(dir, utils.Some(source.MarshalSnapshot()))) - registry, err := NewRegistry(otherCommittee, firstRoad, time.Time{}, utils.Some(dir)) + registry, err := NewRegistryFromSnapshot( + otherCommittee, firstRoad, time.Time{}, source.MarshalSnapshot(), utils.None[string](), + ) require.NoError(t, err) require.True(t, EpochEqual(source.MustEpoch(3), utils.OrPanic1(registry.Epoch(3)))) } -func TestRestoreSnapshotRejectsMalformedRow(t *testing.T) { +func TestNewRegistryFromSnapshotRejectsMalformedValueWithoutWritingTheStateDir(t *testing.T) { + rng := utils.TestRng() + source, _ := GenRegistryThrough(rng, 3, 1) for name, raw := range map[string][]byte{ "not protobuf": {0xff, 0x01}, "no live epochs": {}, } { t.Run(name, func(t *testing.T) { - require.Error(t, RestoreSnapshot(t.TempDir(), utils.Some(raw))) + dir := t.TempDir() + _, err := fromSnapshot(source.MustEpoch(0), raw, utils.Some(dir)) + require.Error(t, err) + _, err = os.Stat(SnapshotDir(dir)) + require.True(t, errors.Is(err, os.ErrNotExist)) }) } } diff --git a/sei-tendermint/internal/autobahn/epoch/snapshot.go b/sei-tendermint/internal/autobahn/epoch/snapshot.go index 9bcb31696e..714b115c0a 100644 --- a/sei-tendermint/internal/autobahn/epoch/snapshot.go +++ b/sei-tendermint/internal/autobahn/epoch/snapshot.go @@ -12,7 +12,6 @@ import ( "github.com/sei-protocol/sei-chain/sei-tendermint/internal/autobahn/pb" "github.com/sei-protocol/sei-chain/sei-tendermint/internal/protoutils" "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils" - "google.golang.org/protobuf/proto" ) const ( @@ -25,33 +24,6 @@ func SnapshotDir(stateDir string) string { return filepath.Join(stateDir, epochSnapshotDir) } -// RestoreSnapshot replaces the epoch registry snapshot under stateDir with raw, the protobuf -// encoding of a PersistedEpochRegistry. With no raw it removes the snapshot, so the registry -// opens with the genesis epochs. -func RestoreSnapshot(stateDir string, raw utils.Option[[]byte]) error { - dir := SnapshotDir(stateDir) - if err := os.RemoveAll(dir); err != nil { - return fmt.Errorf("remove epoch snapshot dir %s: %w", dir, err) - } - encoded, ok := raw.Get() - if !ok { - return nil - } - var snapshot pb.PersistedEpochRegistry - if err := proto.Unmarshal(encoded, &snapshot); err != nil { - return fmt.Errorf("decode epoch registry: %w", err) - } - state, err := persistedRegistryConv.DecodeReq(&snapshot) - if err != nil { - return fmt.Errorf("decode epoch registry: %w", err) - } - persister, _, err := openEpochSnapshot(utils.Some(stateDir)) - if err != nil { - return err - } - return persister.Persist(persistedRegistryConv.Encode(state)) -} - func openEpochSnapshot( stateDir utils.Option[string], ) (persist.Persister[*pb.PersistedEpochRegistry], utils.Option[*pb.PersistedEpochRegistry], error) { diff --git a/sei-tendermint/internal/p2p/giga_router_common.go b/sei-tendermint/internal/p2p/giga_router_common.go index f304e12d31..8abf397bc3 100644 --- a/sei-tendermint/internal/p2p/giga_router_common.go +++ b/sei-tendermint/internal/p2p/giga_router_common.go @@ -108,23 +108,62 @@ func OpenEpochRegistry( validators []atypes.PublicKey, persistentStateDir string, ) (*epoch.Registry, error) { + committee, firstRoad, err := genesisEpochs(genDoc, validators) + if err != nil { + return nil, err + } + registry, err := epoch.NewRegistry(committee, firstRoad, genDoc.GenesisTime, utils.Some(persistentStateDir)) + if err != nil { + return nil, fmt.Errorf("epoch.NewRegistry(): %w", err) + } + return registry, nil +} + +// RestoreEpochRegistry opens the epoch registry from row, the epoch_registry value of an app +// state, with the genesis validators and the road origin and timestamp of genDoc. With +// persistentStateDir set, it persists row under that dir. With no row, it opens the registry +// that persistentStateDir holds, or the genesis epochs. +func RestoreEpochRegistry( + genDoc *types.GenesisDoc, + validators []atypes.PublicKey, + row utils.Option[[]byte], + persistentStateDir utils.Option[string], +) (*epoch.Registry, error) { + committee, firstRoad, err := genesisEpochs(genDoc, validators) + if err != nil { + return nil, err + } + raw, ok := row.Get() + if !ok { + registry, err := epoch.NewRegistry(committee, firstRoad, genDoc.GenesisTime, persistentStateDir) + if err != nil { + return nil, fmt.Errorf("epoch.NewRegistry(): %w", err) + } + return registry, nil + } + registry, err := epoch.NewRegistryFromSnapshot(committee, firstRoad, genDoc.GenesisTime, raw, persistentStateDir) + if err != nil { + return nil, fmt.Errorf("epoch.NewRegistryFromSnapshot(): %w", err) + } + return registry, nil +} + +// genesisEpochs returns the genesis committee, with weight 1 for each of validators, and the +// first road of genDoc. +func genesisEpochs(genDoc *types.GenesisDoc, validators []atypes.PublicKey) (*atypes.Committee, atypes.RoadIndex, error) { if genDoc.InitialHeight < 1 { - return nil, fmt.Errorf("GenDoc.InitialHeight = %v, want >=1", genDoc.InitialHeight) + return nil, 0, fmt.Errorf("GenDoc.InitialHeight = %v, want >=1", genDoc.InitialHeight) } firstRoad := atypes.RoadIndex(genDoc.InitialHeight) // nolint:gosec // verified to be positive. genesisWeights := map[atypes.PublicKey]uint64{} for _, k := range validators { genesisWeights[k] = 1 } - genesisCommittee, err := atypes.NewCommittee(genesisWeights) + committee, err := atypes.NewCommittee(genesisWeights) if err != nil { - return nil, fmt.Errorf("genesis committee: %w", err) + return nil, 0, fmt.Errorf("genesis committee: %w", err) } - registry, err := epoch.NewRegistry(genesisCommittee, firstRoad, genDoc.GenesisTime, utils.Some(persistentStateDir)) - if err != nil { - return nil, fmt.Errorf("epoch.NewRegistry(): %w", err) - } - return registry, nil + return committee, firstRoad, nil } func (r *gigaRouterCommon) LastCommittedBlockNumber() int64 { diff --git a/sei-tendermint/node/epoch_seed.go b/sei-tendermint/node/epoch_seed.go index 8aa37da3e9..cee5be1b95 100644 --- a/sei-tendermint/node/epoch_seed.go +++ b/sei-tendermint/node/epoch_seed.go @@ -14,6 +14,7 @@ import ( "github.com/sei-protocol/sei-chain/sei-tendermint/internal/autobahn/epoch" "github.com/sei-protocol/sei-chain/sei-tendermint/internal/evmonlyapp" "github.com/sei-protocol/sei-chain/sei-tendermint/internal/p2p" + "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils" "github.com/sei-protocol/sei-chain/sei-tendermint/types" ) @@ -41,33 +42,40 @@ func ResolveEpochGenesis(conf *config.Config, genDoc *types.GenesisDoc) (EpochGe return EpochGenesis{GenDoc: genDoc, Validators: slices.SortedFunc(maps.Keys(validators), atypes.PublicKey.Compare)}, nil } -// PrepareEpochRegistry writes under stagingDir the epoch registry snapshot of the epoch_registry -// row in the FlatKV checkpoint in checkpointDir, the state after road height. With no row the -// snapshot holds only the genesis epochs. It returns an error if the row is empty or does not -// decode, if the registry does not open from genesis with the snapshot, if the registry does not -// hold the epoch of height and the epoch after it, or if height is the last road of an epoch. +// CheckpointEpochs is the epoch registry of a FlatKV checkpoint, open in memory. +type CheckpointEpochs struct { + genesis EpochGenesis + row utils.Option[[]byte] + registry *epoch.Registry +} + +// OpenCheckpointEpochs opens in memory the epoch registry of the epoch_registry row in the FlatKV +// checkpoint in checkpointDir, the state after road height. With no row the registry holds only +// the genesis epochs. It returns an error if the row is empty or does not decode, if the registry +// does not open from genesis with the row, if the registry does not hold the epoch of height and +// the epoch after it, or if height is the last road of an epoch. // // The registry check binds the row to this chain only while it holds epoch 0 or 1. A row whose // epochs all follow epoch 1 opens on any chain with the same initial height. -func PrepareEpochRegistry(genesis EpochGenesis, checkpointDir string, height int64, stagingDir string) error { +func OpenCheckpointEpochs(genesis EpochGenesis, checkpointDir string, height int64) (CheckpointEpochs, error) { if height < 1 { - return fmt.Errorf("checkpoint height %d is not positive", height) + return CheckpointEpochs{}, fmt.Errorf("checkpoint height %d is not positive", height) } row, err := evmonlyapp.ReadCheckpointEpochRegistryRow(checkpointDir) if err != nil { - return err + return CheckpointEpochs{}, err } if raw, ok := row.Get(); ok && len(raw) == 0 { - return ErrEmptyEpochRegistryRow - } - if err := epoch.RestoreSnapshot(stagingDir, row); err != nil { - return fmt.Errorf("seed epoch snapshot: %w", err) + return CheckpointEpochs{}, ErrEmptyEpochRegistryRow } - registry, err := p2p.OpenEpochRegistry(genesis.GenDoc, genesis.Validators, stagingDir) + registry, err := p2p.RestoreEpochRegistry(genesis.GenDoc, genesis.Validators, row, utils.None[string]()) if err != nil { - return fmt.Errorf("open the seeded epoch registry: %w", err) + return CheckpointEpochs{}, fmt.Errorf("open the checkpoint epoch registry: %w", err) } - return requireEpochsForRoad(registry, atypes.RoadIndex(height)) + if err := requireEpochsForRoad(registry, atypes.RoadIndex(height)); err != nil { + return CheckpointEpochs{}, err + } + return CheckpointEpochs{genesis: genesis, row: row, registry: registry}, nil } // requireEpochsForRoad returns an error unless registry holds the epoch of road and the epoch @@ -86,21 +94,16 @@ func requireEpochsForRoad(registry *epoch.Registry, road atypes.RoadIndex) error return nil } -// InstallEpochRegistry moves the epoch registry snapshot that PrepareEpochRegistry wrote under -// stagingDir into layout, replacing the one there. stagingDir must be on the filesystem of the -// persistent state dir. It returns an error, and keeps the installed snapshot, when stagingDir -// holds no prepared snapshot. It is not atomic: after a crash, the caller must run -// PrepareEpochRegistry and InstallEpochRegistry again before the node starts. -func InstallEpochRegistry(layout AutobahnLayout, stagingDir string) error { - staged := epoch.SnapshotDir(stagingDir) - if _, err := os.Stat(staged); err != nil { - return fmt.Errorf("find the prepared epoch registry snapshot: %w", err) - } +// InstallEpochRegistry replaces the epoch registry in layout with the one that epochs holds. It +// is not atomic: after a crash, the caller must run it again before the node starts. It may run +// any number of times with the same epochs. +func InstallEpochRegistry(layout AutobahnLayout, epochs CheckpointEpochs) error { if err := os.RemoveAll(layout.EpochRegistryDir); err != nil { return fmt.Errorf("remove the epoch registry snapshot: %w", err) } - if err := os.Rename(staged, layout.EpochRegistryDir); err != nil { - return fmt.Errorf("install the epoch registry snapshot: %w", err) + genesis := epochs.genesis + if _, err := p2p.RestoreEpochRegistry(genesis.GenDoc, genesis.Validators, epochs.row, utils.Some(layout.PersistentStateDir)); err != nil { + return fmt.Errorf("install the epoch registry: %w", err) } return dbutils.SyncDir(filepath.Dir(layout.EpochRegistryDir)) } diff --git a/sei-tendermint/node/epoch_seed_test.go b/sei-tendermint/node/epoch_seed_test.go index 4f1c76301c..58e5c38068 100644 --- a/sei-tendermint/node/epoch_seed_test.go +++ b/sei-tendermint/node/epoch_seed_test.go @@ -1,7 +1,6 @@ package node import ( - "os" "path/filepath" "testing" @@ -76,11 +75,11 @@ func seedLayout(t *testing.T, genesis EpochGenesis, checkpoint string, height in t.Helper() psd := t.TempDir() layout := AutobahnLayout{PersistentStateDir: psd, EpochRegistryDir: epoch.SnapshotDir(psd)} - staging := filepath.Join(t.TempDir(), "staging") - if err := PrepareEpochRegistry(genesis, checkpoint, height, staging); err != nil { + epochs, err := OpenCheckpointEpochs(genesis, checkpoint, height) + if err != nil { return layout, err } - return layout, InstallEpochRegistry(layout, staging) + return layout, InstallEpochRegistry(layout, epochs) } func openLayoutRegistry(t *testing.T, genesis EpochGenesis, layout AutobahnLayout) *epoch.Registry { @@ -104,18 +103,21 @@ func TestSeedEpochRegistryFromEpochRegistryRow(t *testing.T) { } } -func TestInstallEpochRegistryRefusesASecondInstallFromTheSameStaging(t *testing.T) { +func TestInstallEpochRegistryTwiceKeepsTheRegistry(t *testing.T) { rng := utils.TestRng() chain := newTestEpochChain(t, rng) checkpoint := committedCheckpoint(t, utils.Some(chain.registry.MarshalSnapshot())) psd := t.TempDir() layout := AutobahnLayout{PersistentStateDir: psd, EpochRegistryDir: epoch.SnapshotDir(psd)} - staging := filepath.Join(t.TempDir(), "staging") - require.NoError(t, PrepareEpochRegistry(chain.genesis, checkpoint, chain.firstHeightOf(2), staging)) - require.NoError(t, InstallEpochRegistry(layout, staging)) + epochs, err := OpenCheckpointEpochs(chain.genesis, checkpoint, chain.firstHeightOf(2)) + require.NoError(t, err) + require.NoError(t, InstallEpochRegistry(layout, epochs)) - require.ErrorIs(t, InstallEpochRegistry(layout, staging), os.ErrNotExist) - require.True(t, epoch.EpochEqual(chain.registry.MustEpoch(3), openLayoutRegistry(t, chain.genesis, layout).MustEpoch(3))) + require.NoError(t, InstallEpochRegistry(layout, epochs)) + registry := openLayoutRegistry(t, chain.genesis, layout) + for idx := atypes.EpochIndex(0); idx <= 3; idx++ { + require.True(t, epoch.EpochEqual(chain.registry.MustEpoch(idx), registry.MustEpoch(idx))) + } } func TestInstallEpochRegistryWithoutRowReplacesTheOldSnapshotWithGenesis(t *testing.T) { @@ -123,20 +125,21 @@ func TestInstallEpochRegistryWithoutRowReplacesTheOldSnapshotWithGenesis(t *test chain := newTestEpochChain(t, rng) psd := t.TempDir() layout := AutobahnLayout{PersistentStateDir: psd, EpochRegistryDir: epoch.SnapshotDir(psd)} - require.NoError(t, epoch.RestoreSnapshot(psd, utils.Some(chain.registry.MarshalSnapshot()))) - staging := filepath.Join(t.TempDir(), "staging") + _, err := p2p.RestoreEpochRegistry(chain.genesis.GenDoc, chain.genesis.Validators, utils.Some(chain.registry.MarshalSnapshot()), utils.Some(psd)) + require.NoError(t, err) - require.NoError(t, PrepareEpochRegistry(chain.genesis, committedCheckpoint(t, utils.None[[]byte]()), chain.firstHeightOf(0), staging)) + epochs, err := OpenCheckpointEpochs(chain.genesis, committedCheckpoint(t, utils.None[[]byte]()), chain.firstHeightOf(0)) + require.NoError(t, err) require.True(t, epoch.EpochEqual(chain.registry.MustEpoch(3), openLayoutRegistry(t, chain.genesis, layout).MustEpoch(3))) - require.NoError(t, InstallEpochRegistry(layout, staging)) + require.NoError(t, InstallEpochRegistry(layout, epochs)) registry := openLayoutRegistry(t, chain.genesis, layout) require.True(t, epoch.EpochEqual(chain.registry.MustEpoch(1), registry.MustEpoch(1))) - _, err := registry.Epoch(2) + _, err = registry.Epoch(2) require.ErrorIs(t, err, atypes.ErrNotFound) } -func TestPrepareEpochRegistryRefusesAHeightWithoutTheNextEpoch(t *testing.T) { +func TestOpenCheckpointEpochsRefusesAHeightWithoutTheNextEpoch(t *testing.T) { rng := utils.TestRng() chain := newTestEpochChain(t, rng) for name, tc := range map[string]struct { @@ -153,7 +156,7 @@ func TestPrepareEpochRegistryRefusesAHeightWithoutTheNextEpoch(t *testing.T) { } } -func TestPrepareEpochRegistryRefusesAHeightInAPrunedEpoch(t *testing.T) { +func TestOpenCheckpointEpochsRefusesAHeightInAPrunedEpoch(t *testing.T) { rng := utils.TestRng() chain := newTestEpochChain(t, rng) height := chain.firstHeightOf(1) @@ -163,7 +166,7 @@ func TestPrepareEpochRegistryRefusesAHeightInAPrunedEpoch(t *testing.T) { require.ErrorIs(t, err, atypes.ErrPruned) } -func TestPrepareEpochRegistryRefusesTheLastRoadOfAnEpoch(t *testing.T) { +func TestOpenCheckpointEpochsRefusesTheLastRoadOfAnEpoch(t *testing.T) { rng := utils.TestRng() chain := newTestEpochChain(t, rng) checkpoint := committedCheckpoint(t, utils.Some(chain.registry.MarshalSnapshot())) @@ -172,7 +175,7 @@ func TestPrepareEpochRegistryRefusesTheLastRoadOfAnEpoch(t *testing.T) { require.ErrorIs(t, err, ErrClosingRoad) } -func TestPrepareEpochRegistryRefusesARowWithTheGenesisEpochsOfAnotherChain(t *testing.T) { +func TestOpenCheckpointEpochsRefusesARowWithTheGenesisEpochsOfAnotherChain(t *testing.T) { rng := utils.TestRng() chain := newTestEpochChain(t, rng) other := newTestEpochChain(t, rng) @@ -182,7 +185,7 @@ func TestPrepareEpochRegistryRefusesARowWithTheGenesisEpochsOfAnotherChain(t *te } // A row pruned past genesis is not bound to this chain here; AppQC(H+1) catches it. -func TestPrepareEpochRegistryCannotBindARowPrunedPastGenesis(t *testing.T) { +func TestOpenCheckpointEpochsCannotBindARowPrunedPastGenesis(t *testing.T) { rng := utils.TestRng() chain := newTestEpochChain(t, rng) other := newTestEpochChain(t, rng) @@ -194,7 +197,7 @@ func TestPrepareEpochRegistryCannotBindARowPrunedPastGenesis(t *testing.T) { require.True(t, epoch.EpochEqual(other.registry.MustEpoch(3), openLayoutRegistry(t, chain.genesis, layout).MustEpoch(3))) } -func TestPrepareEpochRegistryRejectsMalformedRow(t *testing.T) { +func TestOpenCheckpointEpochsRejectsMalformedRow(t *testing.T) { rng := utils.TestRng() chain := newTestEpochChain(t, rng) @@ -202,7 +205,7 @@ func TestPrepareEpochRegistryRejectsMalformedRow(t *testing.T) { require.Error(t, err) } -func TestPrepareEpochRegistryRejectsEmptyRow(t *testing.T) { +func TestOpenCheckpointEpochsRejectsEmptyRow(t *testing.T) { rng := utils.TestRng() chain := newTestEpochChain(t, rng)