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:
@@ -438,3 +438,57 @@ func mustRoot(t *testing.T) *identity.Identity {
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
// Reconnecting must transfer only what differs: b holds most of a's listings,
|
||||
// a has since changed one and added another.
|
||||
func TestIncrementalSyncFetchesOnlyTheDifference(t *testing.T) {
|
||||
a := startNode(t, nil, "u1x")
|
||||
b := startNode(t, nil, "u1x")
|
||||
ctx := context.Background()
|
||||
|
||||
var shared []*listing.Listing
|
||||
for i := 0; i < 5; i++ { // page size is 3: two inventory pages
|
||||
l := a.listing(t, "u1xjhx")
|
||||
a.s.Put(ctx, l)
|
||||
b.s.Put(ctx, l)
|
||||
shared = append(shared, l)
|
||||
}
|
||||
update := &listing.Listing{
|
||||
Version: listing.Version, ID: shared[2].ID, Seq: 2,
|
||||
CreatedAt: time.Now().Unix(), ExpiresAt: time.Now().Add(7 * 24 * time.Hour).Unix(),
|
||||
Kind: "rent", Category: "apartment", Geohash: "u1xjhx", Title: "Neu",
|
||||
Price: &listing.Price{AmountCents: 90000, Currency: "EUR", Period: "month"},
|
||||
}
|
||||
if err := update.Seal(a.dev, difficulty); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fresh := a.listing(t, "u1xjhx")
|
||||
a.s.Put(ctx, update)
|
||||
a.s.Put(ctx, fresh)
|
||||
|
||||
connect(t, b, a)
|
||||
eventually(t, "b has the update and the new listing", func() bool {
|
||||
l, err := b.s.Get(ctx, shared[2].Author, shared[2].ID)
|
||||
_, err2 := b.s.Get(ctx, fresh.Author, fresh.ID)
|
||||
return err == nil && l.Title == "Neu" && err2 == nil
|
||||
})
|
||||
da, _ := a.s.ListingDigest(ctx, "u1x")
|
||||
db, _ := b.s.ListingDigest(ctx, "u1x")
|
||||
if da != db {
|
||||
t.Fatal("digests differ after sync")
|
||||
}
|
||||
}
|
||||
|
||||
// The bootstrap node deployed today does not know digests: syncing with it
|
||||
// must still work, the old way.
|
||||
func TestSyncWithANodeThatKnowsNoDigests(t *testing.T) {
|
||||
a := startNode(t, nil, "u1x")
|
||||
a.oldSync = true
|
||||
ctx := context.Background()
|
||||
for i := 0; i < 4; i++ {
|
||||
a.s.Put(ctx, a.listing(t, "u1xjhx"))
|
||||
}
|
||||
b := startNode(t, nil, "u1x")
|
||||
connect(t, b, a)
|
||||
eventually(t, "b caught up from an old node", func() bool { return count(b) == 4 })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user