From a54094ea3563ee0a681f95da6b671dad64a76d95 Mon Sep 17 00:00:00 2001 From: Nils Stinnesbeck Date: Wed, 18 Nov 2026 17:33:06 +0100 Subject: [PATCH] p2p: anti-entropy, a periodic sync with one random peer Repairs what gossip lost while two nodes stay connected; cheap because matching digests end the round at once. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw --- docs/PROTOCOL.md | 6 +++-- internal/p2p/node.go | 55 ++++++++++++++++++++++++++++++++++++---- internal/p2p/p2p_test.go | 17 +++++++++++++ 3 files changed, 71 insertions(+), 7 deletions(-) diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.md index 460440e..0ffa63e 100644 --- a/docs/PROTOCOL.md +++ b/docs/PROTOCOL.md @@ -142,7 +142,9 @@ within 30 days. existed) is caught up the original way: kinds `revocations`, `listings` and `messages`, paged by `(created_at, author, id)` and `(created_at, hash)` cursors, which servers still answer. Gossip lost *while connected* is only - repaired by the next reconnect; periodic anti-entropy is future work. + repaired by the next reconnect, and by anti-entropy: every 30 minutes a node + runs this sync again with one random connected peer that shares a region (a + matching digest makes a round nearly free). - **Reconnection**: bootstrap peers are redialed every 20 s when disconnected. - **Re-announce**: a running node re-gossips its own live listings and revocations every 30 minutes, so newcomers hear about them without waiting @@ -493,7 +495,7 @@ Proof-of-work per listing and per revocation, plus subscribable blocklists Abuse reporting for messages, reciprocity rules, anti- discrimination (AGG) content rules, blocklists, peer scoring and rate limits, -periodic anti-entropy while connected, a fallback bootstrap list inside the binary +a fallback bootstrap list inside the binary for networks that block DNS, image hosting, recovering listings onto a fresh machine with an empty database, a helper to turn a place name into a region. diff --git a/internal/p2p/node.go b/internal/p2p/node.go index 9e411c9..1c65919 100644 --- a/internal/p2p/node.go +++ b/internal/p2p/node.go @@ -17,6 +17,7 @@ import ( "errors" "fmt" "log/slog" + "math/rand/v2" "slices" "sync" "time" @@ -89,10 +90,14 @@ type Config struct { // MessageDifficulty is the proof-of-work bits required of message // envelopes; it must match the store's. MessageDifficulty int - SyncPageSize int // records per sync page (default 500) - RedialInterval time.Duration // how often lost bootstrap peers are redialed (default 20s) - Clock func() time.Time // default time.Now - Logger *slog.Logger + SyncPageSize int // records per sync page (default 500) + RedialInterval time.Duration // how often lost bootstrap peers are redialed (default 20s) + // AntiEntropyInterval is how often the node syncs with one random peer + // it shares a region with, to repair what gossip lost while connected + // (default 30 min). + AntiEntropyInterval time.Duration + Clock func() time.Time // default time.Now + Logger *slog.Logger } // Node is a running participant. @@ -140,6 +145,9 @@ func New(ctx context.Context, cfg Config) (*Node, error) { if cfg.RedialInterval <= 0 { cfg.RedialInterval = 20 * time.Second } + if cfg.AntiEntropyInterval <= 0 { + cfg.AntiEntropyInterval = 30 * time.Minute + } if cfg.RelayBootDelay <= 0 { cfg.RelayBootDelay = 10 * time.Second } @@ -256,7 +264,7 @@ func New(ctx context.Context, cfg Config) (*Node, error) { // Finding the first peers can take a while (DNS, unreachable servers), so it // does not hold up starting the node. - n.wg.Add(4) + n.wg.Add(5) go func() { defer n.wg.Done() n.dialBootstrap() @@ -264,6 +272,7 @@ func New(ctx context.Context, cfg Config) (*Node, error) { }() go n.maintain() go n.redial() + go n.antiEntropy() go n.remember() return n, nil } @@ -760,6 +769,42 @@ func (n *Node) redial() { } } +// antiEntropy repairs what gossip lost while the node stayed connected, which +// the sync on a new connection cannot: every interval it syncs with one +// random peer that shares a region. Matching digests make a round nearly free. +func (n *Node) antiEntropy() { + defer n.wg.Done() + t := time.NewTicker(n.cfg.AntiEntropyInterval) + defer t.Stop() + for { + select { + case <-n.ctx.Done(): + return + case <-t.C: + n.antiEntropyOnce() + } + } +} + +func (n *Node) antiEntropyOnce() { + n.mu.Lock() + regions := append([]string(nil), n.subscribed...) + n.mu.Unlock() + seen := map[peer.ID]bool{} + var peers []peer.ID + for _, r := range regions { + for _, p := range n.RegionPeers(r) { + if !seen[p] && n.host.Network().Connectedness(p) == network.Connected { + seen[p] = true + peers = append(peers, p) + } + } + } + if len(peers) > 0 { + n.syncSoon(peers[rand.IntN(len(peers))]) + } +} + // maintain periodically drops expired data. func (n *Node) maintain() { defer n.wg.Done() diff --git a/internal/p2p/p2p_test.go b/internal/p2p/p2p_test.go index 60a515a..fdd21e2 100644 --- a/internal/p2p/p2p_test.go +++ b/internal/p2p/p2p_test.go @@ -492,3 +492,20 @@ func TestSyncWithANodeThatKnowsNoDigests(t *testing.T) { connect(t, b, a) eventually(t, "b caught up from an old node", func() bool { return count(b) == 4 }) } + +// A listing that gossip never delivered is repaired while the link stays up. +func TestAntiEntropyRepairsWhatGossipMissed(t *testing.T) { + a := startNode(t, nil, "u1x") + b := startNodeWith(t, nil, func(c *Config) { c.AntiEntropyInterval = 200 * time.Millisecond }, "u1x") + connect(t, b, a) + eventually(t, "connected and synced", func() bool { return len(b.RegionPeers("u1x")) == 1 }) + + l := a.listing(t, "u1xjhx") + if _, err := a.s.Put(context.Background(), l); err != nil { // stored, not published: gossip never sees it + t.Fatal(err) + } + eventually(t, "b repairs the gap without reconnecting", func() bool { return count(b) == 1 }) + if b.PeerCount() != 1 { + t.Fatal("the link should have stayed up") + } +}