p2p: incremental sync with digests, inventory and fetch
Peers compare a short digest of revocations, listings and messages on connect and transfer only what differs. Peers without digests are synced the old way. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
@@ -527,3 +527,94 @@ func TestRevokeAllRecovery(t *testing.T) {
|
||||
t.Fatalf("someone else's listing blocked: %v", o)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDigestsTellEqualSetsFromDifferentOnes(t *testing.T) {
|
||||
a, b := newEnv(t), newEnv(t)
|
||||
l1, l2 := a.listing(t, nil), a.listing(t, nil)
|
||||
a.put(t, l1)
|
||||
a.put(t, l2)
|
||||
b.put(t, l2) // insertion order must not matter
|
||||
b.put(t, l1)
|
||||
|
||||
da, _ := a.s.ListingDigest(ctx, "u1x")
|
||||
db, _ := b.s.ListingDigest(ctx, "u1x")
|
||||
if da != db || da.Count != 2 {
|
||||
t.Fatalf("equal sets, different digests: %+v vs %+v", da, db)
|
||||
}
|
||||
// Another version of one listing changes the digest.
|
||||
a.put(t, a.listing(t, func(l *listing.Listing) { l.ID, l.Seq = l1.ID, 2 }))
|
||||
da2, _ := a.s.ListingDigest(ctx, "u1x")
|
||||
if da2 == db {
|
||||
t.Fatal("a newer version did not change the digest")
|
||||
}
|
||||
// A listing outside the region does not.
|
||||
a.put(t, a.listing(t, func(l *listing.Listing) { l.Geohash = "u30000" }))
|
||||
if d, _ := a.s.ListingDigest(ctx, "u1x"); d != da2 {
|
||||
t.Fatal("a listing of another region changed the digest")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInventoryWantedAndFetch(t *testing.T) {
|
||||
a, b := newEnv(t), newEnv(t)
|
||||
var ls []*listing.Listing
|
||||
for i := 0; i < 5; i++ {
|
||||
l := a.listing(t, nil)
|
||||
a.put(t, l)
|
||||
ls = append(ls, l)
|
||||
}
|
||||
// b holds ls[0], but a is about to get a newer version of it.
|
||||
b.put(t, ls[0])
|
||||
newer := a.listing(t, func(l *listing.Listing) { l.ID, l.Seq = ls[0].ID, 2 })
|
||||
a.put(t, newer)
|
||||
|
||||
var all []Item
|
||||
var after Item
|
||||
for pages := 0; pages < 10; pages++ {
|
||||
page, err := a.s.Inventory(ctx, "u1x", after.Author, after.ID, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(page) == 0 {
|
||||
break
|
||||
}
|
||||
all = append(all, page...)
|
||||
after = page[len(page)-1]
|
||||
}
|
||||
if len(all) != 5 {
|
||||
t.Fatalf("inventory lists %d listings, want 5", len(all))
|
||||
}
|
||||
want, err := b.s.Wanted(ctx, all)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// b lacks four listings and holds an older version of the fifth.
|
||||
if len(want) != 5 {
|
||||
t.Fatalf("wanted %d, want 5", len(want))
|
||||
}
|
||||
b.put(t, ls[2])
|
||||
if want, _ = b.s.Wanted(ctx, all); len(want) != 4 {
|
||||
t.Fatalf("wanted %d after storing one, want 4", len(want))
|
||||
}
|
||||
|
||||
var keys []string
|
||||
for _, it := range want {
|
||||
keys = append(keys, it.Key())
|
||||
}
|
||||
got, err := a.s.ListingsByKey(ctx, append(keys, "garbage", "x/y/z", "ab/cd"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 4 {
|
||||
t.Fatalf("fetched %d, want 4", len(got))
|
||||
}
|
||||
for _, l := range got {
|
||||
b.put(t, l)
|
||||
}
|
||||
if want, _ = b.s.Wanted(ctx, all); len(want) != 0 {
|
||||
t.Fatalf("b still wants %d after fetching", len(want))
|
||||
}
|
||||
da, _ := a.s.ListingDigest(ctx, "u1x")
|
||||
if db, _ := b.s.ListingDigest(ctx, "u1x"); da != db {
|
||||
t.Fatal("digests differ after the transfer")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user