Repository navigation
rpcv2: version the on-disk format before the first release freezes it #967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
karthikiyer56
merged 17 commits into
feature/full-history
from
fullhistory-format-versioning
Sep 3, 2026
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7ca4a84
rpcv2: refuse foreign catalogs at open (the storage-format census)
tamirms 95cf0c0
rpcv2: finish the cold formats before they freeze at release one
tamirms 9e765b8
design-docs: the format versioning and upgrades section
tamirms 8c17471
rpcv2: review pass over the versioning branch
tamirms 31c58a0
rpcv2: simplify pass over the versioning branch
tamirms 29e8a49
rpcv2: second review round over the versioning branch
tamirms a6fbffc
rpcv2: complete the newer-engine wrap and reserved-byte coverage
tamirms fcc59b8
catalog: drop the secret-width constant; the field's type is the width
tamirms a054da2
rocksdb: drop the newer-engine error relabeling
tamirms 307a258
bench: drop the help-text warning; off-topic for the versioning PR
tamirms 7cba651
stores: negative tests for the stamp gate and the reserved bytes
tamirms ebfc7f0
census: reject cross-window index coverages; two more format pins
tamirms 9594804
stores/event: stamp-test fixture carries the record checksum
karthikiyer56 934b6fe
rpcv2: review round three, the two open Copilot threads
karthikiyer56 cf4510d
rpcv2: review round three, consolidation nits
karthikiyer56 615c65e
rpcv2: review round three, comment and doc trims
karthikiyer56 e279396
rocksdb: the pin comment names the librocksdb version the repo builds…
karthikiyer56 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package catalog | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/stellar/stellar-rpc/cmd/stellar-rpc/internal/rpcv2/geometry" | ||
| ) | ||
|
|
||
| // ErrForeignCatalog marks a catalog holding entries outside this binary's | ||
| // vocabulary. The daemon refuses to start on it: the entries were either | ||
| // written by a newer stellar-rpc (whose formats this binary cannot read, and | ||
| // whose artifacts the resolver would otherwise overwrite) or corrupted. | ||
| var ErrForeignCatalog = errors.New("catalog: entries unknown to this version of stellar-rpc") | ||
|
|
||
| // censusMaxDetailed caps how many offending entries the refusal error spells | ||
| // out; the total count is always reported. | ||
| const censusMaxDetailed = 10 | ||
|
|
||
| // census validates every key and value in the store against the exact | ||
| // vocabulary this binary writes. It runs once inside Open, before the secret | ||
| // mint, so a foreign catalog is refused before anything writes into it. The | ||
| // scan is read-only and touches only the catalog (never a hot DB or a file). | ||
| // | ||
| // Everything this binary durably writes must parse here; the whitelist spans | ||
| // the geometry key families AND the meta/catalog-secret key, and the value | ||
| // checks are the census's own exact-token comparisons (State/HotState reads | ||
| // elsewhere are raw casts, not validators). | ||
| func (c *Catalog) census() error { | ||
| var ( | ||
| offenders []string | ||
| total int | ||
| ) | ||
| flag := func(key, detail string) { | ||
| total++ | ||
| if len(offenders) < censusMaxDetailed { | ||
| offenders = append(offenders, fmt.Sprintf("%q: %s", key, detail)) | ||
| } | ||
| } | ||
|
|
||
| for e, err := range c.prefixScan("") { | ||
| if err != nil { | ||
| return fmt.Errorf("catalog: census scan: %w", err) | ||
| } | ||
| switch e.Key { | ||
| case geometry.ConfigEarliestLedger: | ||
| if !isCanonicalUint32(e.Value) { | ||
| flag(e.Key, fmt.Sprintf("value %q is not a canonical decimal uint32", e.Value)) | ||
| } | ||
| case catalogSecretStoreKey: | ||
| // Never print the secret: it is the live index-blinding key. The | ||
| // width comes from the field's type, valid before the mint. | ||
| if len(e.Value) != len(c.secret) { | ||
| flag(e.Key, fmt.Sprintf("value is %d bytes, want %d (value redacted)", len(e.Value), len(c.secret))) | ||
| } | ||
| default: | ||
| if detail, ok := c.censusArtifactEntry(e.Key, e.Value); !ok { | ||
| flag(e.Key, detail) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if total == 0 { | ||
| return nil | ||
| } | ||
| return fmt.Errorf("%w — either written by a newer stellar-rpc (deploy that version or newer) "+ | ||
| "or corrupted; %d offending entr%s: %s", | ||
| ErrForeignCatalog, total, plural(total), strings.Join(offenders, "; ")) | ||
| } | ||
|
|
||
| // censusArtifactEntry validates one non-config entry against the three state | ||
| // key families. ok=false returns the reason. | ||
| func (c *Catalog) censusArtifactEntry(key, value string) (string, bool) { | ||
| switch { | ||
| case strings.HasPrefix(key, geometry.HotChunkPrefix): | ||
| if _, ok := geometry.ParseHotChunkKey(key); !ok { | ||
| return "malformed hot-chunk key", false | ||
| } | ||
| if !geometry.IsKnownHotState(geometry.HotState(value)) { | ||
| return fmt.Sprintf("unknown hot state %q", value), false | ||
| } | ||
| case strings.HasPrefix(key, geometry.ChunkPrefix): | ||
| if _, _, ok := geometry.ParseChunkKey(key); !ok { | ||
| return "malformed per-chunk artifact key", false | ||
| } | ||
| if !geometry.IsKnownState(geometry.State(value)) { | ||
| return fmt.Sprintf("unknown artifact state %q", value), false | ||
| } | ||
| case strings.HasPrefix(key, geometry.TxHashIndexPrefix): | ||
| cov, ok := geometry.ParseTxHashIndexKey(key) | ||
| if !ok { | ||
| return "malformed index coverage key", false | ||
| } | ||
| // The builder only ever covers chunks of the key's own index, so a | ||
| // cross-window coverage is something no binary wrote. Left accepted, | ||
| // a corrupt frozen one would suppress legitimate index rebuilds. | ||
| if c.txhashIndex.TxHashIndexID(cov.Lo) != cov.Index || | ||
| c.txhashIndex.TxHashIndexID(cov.Hi) != cov.Index { | ||
| return "index coverage endpoints outside the key's own index", false | ||
| } | ||
| if !geometry.IsKnownState(geometry.State(value)) { | ||
| return fmt.Sprintf("unknown artifact state %q", value), false | ||
| } | ||
| default: | ||
| // Never print an unknown key's value: a newer binary may store key | ||
| // material under a name this binary does not recognize. | ||
| return fmt.Sprintf("unknown key (value redacted, %d bytes)", len(value)), false | ||
| } | ||
| return "", true | ||
| } | ||
|
|
||
| // isCanonicalUint32 reports whether v round-trips through ParseUint and | ||
| // FormatUint byte-identically — the exact form PinEarliestLedger writes. | ||
| func isCanonicalUint32(v string) bool { | ||
| n, err := strconv.ParseUint(v, 10, 32) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| return strconv.FormatUint(n, 10) == v | ||
| } | ||
|
|
||
| func plural(n int) string { | ||
| if n == 1 { | ||
| return "y" | ||
| } | ||
| return "ies" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package catalog | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/stellar/stellar-rpc/cmd/stellar-rpc/internal/rpcv2/chunk" | ||
| "github.com/stellar/stellar-rpc/cmd/stellar-rpc/internal/rpcv2/geometry" | ||
| "github.com/stellar/stellar-rpc/cmd/stellar-rpc/internal/rpcv2/rocksdb" | ||
| ) | ||
|
|
||
| // reopenAfter opens a catalog at path, applies mutate, closes it, and returns | ||
| // the error from reopening — the census's verdict on the mutated store. | ||
| func reopenAfter(t *testing.T, mutate func(c *Catalog)) error { | ||
| t.Helper() | ||
| path := t.TempDir() | ||
| cat, err := openKVAt(t, path) | ||
| require.NoError(t, err) | ||
| mutate(cat) | ||
| require.NoError(t, cat.Close()) | ||
| reopened, err := openKVAt(t, path) | ||
| if reopened != nil { | ||
| t.Cleanup(func() { _ = reopened.Close() }) | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| // TestCensus_AcceptsOwnVocabulary pins that every key and value this binary | ||
| // writes — all three state families in every state, the pin, and the minted | ||
| // secret — reopens cleanly. This is the whole release-1 write surface. | ||
| func TestCensus_AcceptsOwnVocabulary(t *testing.T) { | ||
| err := reopenAfter(t, func(c *Catalog) { | ||
| for i, s := range geometry.AllStates() { | ||
| id := chunk.ID(i) | ||
| for _, kind := range geometry.AllKinds() { | ||
| require.NoError(t, c.put(geometry.ChunkKey(id, kind), string(s))) | ||
| } | ||
| // Coverage endpoints must lie in the key's own index window. | ||
| first := chunk.ID(i) * chunk.ID(geometry.ChunksPerTxhashIndex) | ||
| idxKey := geometry.TxHashIndexKey(geometry.TxHashIndexID(i), first, first+5) | ||
| require.NoError(t, c.put(idxKey, string(s))) | ||
| } | ||
| for i, s := range geometry.AllHotStates() { | ||
| require.NoError(t, c.put(geometry.HotChunkKey(chunk.ID(7+i)), string(s))) | ||
| } | ||
| require.NoError(t, c.PinEarliestLedger(2)) | ||
| }) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| // TestCensus_AcceptsFirstStartResidues pins the crash-shaped first-start | ||
| // catalogs: secret-only (crash after Open, before the pin) and secret+pin. | ||
| func TestCensus_AcceptsFirstStartResidues(t *testing.T) { | ||
| require.NoError(t, reopenAfter(t, func(*Catalog) {}), "secret-only catalog") | ||
| require.NoError(t, reopenAfter(t, func(c *Catalog) { | ||
| require.NoError(t, c.PinEarliestLedger(10002)) | ||
| }), "secret+pin catalog") | ||
| } | ||
|
|
||
| // TestCensus_RefusesForeignEntries walks the refusal matrix: every entry shape | ||
| // a newer binary (or corruption) could leave must fail reopen with | ||
| // ErrForeignCatalog and an actionable message. | ||
| func TestCensus_RefusesForeignEntries(t *testing.T) { | ||
| cases := []struct { | ||
| name, key, value string | ||
| }{ | ||
| {"novel prefix", "format:events", "2"}, | ||
| {"novel meta key", "meta/other", "WOULD-BE-SECRET-BYTES"}, | ||
| {"unknown kind under chunk prefix", "chunk:00000001:bogus", string(geometry.StateFrozen)}, | ||
| {"version suffix on chunk state", geometry.ChunkKey(1, geometry.KindEvents), "frozen@2"}, | ||
| {"version suffix on hot state", geometry.HotChunkKey(3), "ready@2"}, | ||
| {"version suffix on index state", geometry.TxHashIndexKey(0, 0, 9), "frozen@2"}, | ||
| {"unknown hot state", geometry.HotChunkKey(4), "warm"}, | ||
| {"unpadded chunk id", "chunk:123:ledgers", string(geometry.StateFrozen)}, | ||
| {"index lo above hi", "index:00000000:00000005:00000002", string(geometry.StateFrozen)}, | ||
| {"cross-window index coverage", "index:00000001:00000000:00003000", string(geometry.StateFrozen)}, | ||
| {"non-canonical pin", geometry.ConfigEarliestLedger, "007"}, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| err := reopenAfter(t, func(c *Catalog) { | ||
| require.NoError(t, c.put(tc.key, tc.value)) | ||
| }) | ||
| require.ErrorIs(t, err, ErrForeignCatalog) | ||
| require.ErrorContains(t, err, "deploy that version or newer") | ||
| require.ErrorContains(t, err, tc.key) | ||
| require.NotContains(t, err.Error(), "WOULD-BE-SECRET-BYTES", | ||
| "an unknown key's value must never be printed; it may be a newer binary's key material") | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestCensus_CapsDetailButCountsAll pins the refusal shape on a store with | ||
| // more offenders than the detail cap: every offender is counted, only the | ||
| // first censusMaxDetailed are spelled out. | ||
| func TestCensus_CapsDetailButCountsAll(t *testing.T) { | ||
| const n = censusMaxDetailed + 2 | ||
| err := reopenAfter(t, func(c *Catalog) { | ||
| for i := range n { | ||
| require.NoError(t, c.put(fmt.Sprintf("future:%02d", i), "x")) | ||
| } | ||
| }) | ||
| require.ErrorIs(t, err, ErrForeignCatalog) | ||
| require.ErrorContains(t, err, fmt.Sprintf("%d offending entries", n)) | ||
| require.ErrorContains(t, err, fmt.Sprintf("future:%02d", censusMaxDetailed-1)) | ||
| require.NotContains(t, err.Error(), fmt.Sprintf("future:%02d", censusMaxDetailed)) | ||
| } | ||
|
|
||
| // TestCensus_RefusalIsWriteFree pins that a refused Open leaves the store | ||
| // untouched: in particular it must NOT mint a secret into a foreign tree (the | ||
| // census runs before ensureSecret). Simulates a tree whose secret a newer | ||
| // binary relocated: a foreign key present, the secret key absent. | ||
| func TestCensus_RefusalIsWriteFree(t *testing.T) { | ||
| path := t.TempDir() | ||
| cat, err := openKVAt(t, path) | ||
| require.NoError(t, err) | ||
| require.NoError(t, cat.put("future:key", "x")) | ||
| require.NoError(t, cat.del(catalogSecretStoreKey)) | ||
| require.NoError(t, cat.Close()) | ||
|
|
||
| _, err = openKVAt(t, path) | ||
| require.ErrorIs(t, err, ErrForeignCatalog) | ||
|
|
||
| // Inspect the raw store: the secret key must still be absent. | ||
| store, err := rocksdb.New(rocksdb.Config{Path: path, Logger: silentLogger()}) | ||
| require.NoError(t, err) | ||
| t.Cleanup(func() { _ = store.Close() }) | ||
| _, found, err := store.Get("", []byte(catalogSecretStoreKey)) | ||
| require.NoError(t, err) | ||
| require.False(t, found, "the refused Open must not have minted a secret") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.