replicagen: a claim generation is never twenty digits
A claim writes g-<gen> under the same location a head.json generation uses, and HA retirement may delete an unnamed g-<20 digits> below its holder's lock generation. ValidClaim now refuses any gen whose ClaimName is Valid, so no claim can own such a prefix. Claim generations are minted as 32 hex characters (16 random bytes, host/claimconstruction.go newID), so none is refused in practice. hostbox.GenerationPrefix refuses the twin table's twenty-digit row.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+35
−6
internal/replicagen/replicagen.go+10 −4internal/replicagen/replicagen_test.go+25 −2
diff --git a/internal/replicagen/replicagen.go b/internal/replicagen/replicagen.go| index a7ce33899..d40b2f536 100644 |
| --- a/internal/replicagen/replicagen.go |
| +++ b/internal/replicagen/replicagen.go |
| @@ -5,8 +5,9 @@ |
| // |
| // Two kinds of name share the shape. A head.json generation is |
| // Name(lockGen), `g-` and twenty digits, and only it or Root passes Valid. |
| -// A claim generation is any single path component but Root; ClaimName |
| -// builds its `g-<gen>` and ValidClaim keeps the claim code's checks. |
| +// A claim generation is any single path component but Root and twenty |
| +// digits; ClaimName builds its `g-<gen>` and ValidClaim keeps the claim |
| +// code's checks, and refuses the twenty digits a Name carries. |
| // |
| // The package imports nothing of the claim code, so lease can use it; |
| // Root equals hostclaim.RootGeneration (a test holds them together). |
| @@ -46,9 +47,14 @@ func Valid(name string) bool { |
| // ValidClaim reports whether gen is a usable claim generation: one |
| // non-empty path component (not "." or "..", no "/", "\" or NUL) that is |
| -// not Root. These are the checks the claim code made before the fold. |
| +// not Root, and whose directory ClaimName(gen) is not a head.json |
| +// generation name (Valid): exactly twenty ASCII digits are refused. The |
| +// two kinds share <loc>/g-<id>/, and HA retirement (lease.RetireGenerations) |
| +// may delete an unnamed g-<20 digits> below its holder's lock generation, |
| +// so a claim must never write one. Claim generations are minted as 32 hex |
| +// characters (16 random bytes), so none is refused in practice. |
| func ValidClaim(gen string) bool { |
| - return gen != "" && gen != "." && gen != ".." && gen != Root && !strings.ContainsAny(gen, "/\\\x00") |
| + return gen != "" && gen != "." && gen != ".." && gen != Root && !strings.ContainsAny(gen, "/\\\x00") && !Valid(ClaimName(gen)) |
| } |
| // ClaimName is a claim generation's directory name, `g-<gen>`. It does not |
diff --git a/internal/replicagen/replicagen_test.go b/internal/replicagen/replicagen_test.go| index 969bd3363..2f0a74f78 100644 |
| --- a/internal/replicagen/replicagen_test.go |
| +++ b/internal/replicagen/replicagen_test.go |
| @@ -47,7 +47,7 @@ func TestValidClaimKeepsTheTwinsChecks(t *testing.T) { |
| for gen, want := range map[string]bool{ |
| "42": true, |
| "cur": true, |
| - "00000000000000000042": true, |
| + "00000000000000000042": false, // a head.json generation's twenty digits |
| "": false, |
| hostclaim.RootGeneration: false, |
| ".": false, |
| @@ -65,6 +65,27 @@ func TestValidClaimKeepsTheTwinsChecks(t *testing.T) { |
| } |
| } |
| +// A claim generation never shares a head.json generation's directory: |
| +// retirement may delete an unnamed g-<20 digits> below its holder's lock |
| +// generation (lease.RetireGenerations), so every twenty-digit claim name, |
| +// below any lock generation, is refused, and the claim code's real names |
| +// (32 hex characters, 16 random bytes) and near misses are not. |
| +func TestValidClaimRefusesHeadGenerationNames(t *testing.T) { |
| + for _, gen := range []string{"00000000000000000000", "00000000000000000002", "00000000000000000004", "99999999999999999999"} { |
| + if replicagen.ValidClaim(gen) { |
| + t.Errorf("ValidClaim(%q) = true: g-%s is a head.json generation", gen, gen) |
| + } |
| + if !replicagen.Valid(replicagen.ClaimName(gen)) { |
| + t.Errorf("ClaimName(%q) is not Valid: the case is not the collision", gen) |
| + } |
| + } |
| + for _, gen := range []string{"0123456789abcdef0123456789abcdef", "01234567890123456789012345678901", "0000000000000000002", "000000000000000000002", "0000000000000000000a"} { |
| + if !replicagen.ValidClaim(gen) { |
| + t.Errorf("ValidClaim(%q) = false, want a usable claim generation", gen) |
| + } |
| + } |
| +} |
| + |
| func TestRootIsTheClaimRoot(t *testing.T) { |
| if replicagen.Root != hostclaim.RootGeneration { |
| t.Fatalf("replicagen.Root = %q, hostclaim.RootGeneration = %q", replicagen.Root, hostclaim.RootGeneration) |
| @@ -98,7 +119,9 @@ func TestPrefixMatchesTheThreeTwins(t *testing.T) { |
| t.Errorf("moves.ReplicaSource(%q, %q) = %q, want %q", tc.location, tc.gen, got, tc.want) |
| } |
| got, err := hostbox.GenerationPrefix(h) |
| - if tc.gen == hostclaim.RootGeneration { |
| + if tc.gen == hostclaim.RootGeneration || !replicagen.ValidClaim(tc.gen) { |
| + // The root, and a twenty-digit name (a head.json generation's, |
| + // never a claim's), are refused. |
| if !errors.Is(err, hostbox.ErrUnsupported) { |
| t.Errorf("hostbox.GenerationPrefix(%q, root) = %q, %v; want ErrUnsupported", tc.location, got, err) |
| } |