Artifact 076411422c863006e6f35587ada70e9a7f5ece0f9c6d1d095af945b18c382d33:
- File
internal/sync/check_delta_test.go
— part of check-in
[b43fd24a1f]
at
2026-10-23 05:28:31
on branch feat/horizon-cones
— perf(sync): check received deltas against a cached basis
The real-repository gate found a dense clone of the Fossil repository CPU-bound past an hour, in delta.Checksum and chain expansion. checkDelta rebuilt each received delta's basis from the root of its chain with content.ExpandUnchecked, after walking the chain for availability; a file's history arrives as one chain of hundreds or thousands of deltas, so the cost was quadratic in its depth, as Repo.Verify's was before 1a5abca.
A receive pass now rebuilds the basis through an unchecked content cache (content.NewUncheckedCache): from the deepest node of the chain it holds, keeping each node it rebuilds, and it remembers every artifact a card stored and verified once the card's savepoint commits. A clone keeps one such cache across its rounds and clears it when a round rolls back. A basis the cache holds, or one dangling in the pass, needs no availability walk, and the clone's check for a basis to request uses the same knowledge. The cache holds what each row rebuilds to, unchecked as checkDelta's basis always was, so every verdict is unchanged; a verified delta's target also goes to the receive linker, which no longer expands it again.
TestCorpusClone (LIBFOSSIL_CORPUS) clones a copy of a repository from an in-process server for profiling. (user: [email protected], size: 3145) [annotate] [blame] [check-ins using] [more...]
package sync import ( "testing" "github.com/danmestas/go-libfossil/internal/blob" "github.com/danmestas/go-libfossil/internal/delta" "github.com/danmestas/go-libfossil/internal/hash" ) // TestCheckDelta checks the pre-storage verification every receiver shares // (Section 7.7): a delta against a held basis must give HASH, a delta that // does not apply is a mismatch, a held basis whose bytes are not its name is // still applied and the result checked, a held basis whose bytes cannot be // rebuilt is a mismatch, and a basis that is not held leaves the delta // unchecked. func TestCheckDelta(t *testing.T) { r := setupSyncTestRepo(t) basis := []byte("the basis of a delta: alpha beta gamma delta epsilon\n") target := []byte("the target of a delta: alpha beta gamma delta epsilon zeta\n") _, basisName, err := blob.Store(r.DB(), basis) if err != nil { t.Fatalf("blob.Store: %v", err) } d, err := delta.Create(basis, target) if err != nil { t.Fatalf("delta.Create: %v", err) } // misnamed is a held row holding basis's bytes under another name, so // content.Expand refuses it; unreadable is a held row whose bytes do not // decompress. misnamed := hash.SHA1([]byte("the bytes this row should hold")) unreadable := hash.SHA1([]byte("the bytes this other row should hold")) stored, err := blob.EncodeForStorage(basis, nil) if err != nil { t.Fatalf("blob.EncodeForStorage: %v", err) } for _, row := range []struct { name string bytes []byte }{{misnamed, stored}, {unreadable, []byte("\x00\x00\x00\x09not zlib")}} { if _, err := r.DB().Exec( "INSERT INTO blob(uuid, size, content, rcvid) VALUES(?, ?, ?, 1)", row.name, len(basis), row.bytes); err != nil { t.Fatalf("insert basis %s: %v", row.name, err) } } other := hash.SHA1([]byte("some other content")) unheld := hash.SHA1([]byte("a basis nobody holds")) for _, tt := range []struct { name, uuid, src string payload []byte want deltaVerdict }{ {"result is HASH", hash.SHA1(target), basisName, d, deltaMatch}, {"result is SHA3 HASH", hash.SHA3(target), basisName, d, deltaMatch}, {"result is not HASH", other, basisName, d, deltaMismatch}, {"delta does not apply", other, basisName, []byte("not a delta"), deltaMismatch}, {"misnamed basis, result is HASH", hash.SHA1(target), misnamed, d, deltaMatch}, {"misnamed basis, result is not HASH", other, misnamed, d, deltaMismatch}, {"basis does not reconstruct", other, unreadable, d, deltaMismatch}, {"basis not held", other, unheld, d, deltaUnverifiable}, } { got, rebuilt, err := newReceivePass().checkDelta(r.DB(), tt.uuid, tt.src, tt.payload) if err != nil { t.Fatalf("%s: checkDelta: %v", tt.name, err) } if got != tt.want { t.Errorf("%s: checkDelta = %v, want %v", tt.name, got, tt.want) } // Only a match hands back content, and then the content named HASH. if (rebuilt != nil) != (got == deltaMatch) { t.Errorf("%s: content returned %v for verdict %v", tt.name, rebuilt != nil, got) } if got == deltaMatch && !hashMatches(tt.uuid, rebuilt) { t.Errorf("%s: rebuilt content is not %s", tt.name, tt.uuid) } } }