p2p: peer exchange to find a region without the DHT, region-tagged peer cache, faster anti-entropy

A node with too few peers in a followed region asks its connected peers who they know there (sync kind "peers"); a peer answers even from another region, since gossip tells it which topics its connections subscribe to. Peers of the region are dialed, others only while the region has none. The peer cache records the regions a peer was seen in and a restart redials those first. Anti-entropy now runs every 10 minutes (plus or minus a fifth) with 3 peers per round instead of every 30 with one. Simulator: a newcomer whose remembered peers were all in another region got 0 of 114 listings with the seeds down, now 56 of 56; repair median at the same interval 25.9 s to 5.7 s.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
2026-11-19 00:54:48 +01:00
co-authored by Claude Sonnet 5.5
parent 2e105ef353
commit 59973cbf43
8 changed files with 452 additions and 31 deletions
+25 -6
View File
@@ -154,9 +154,21 @@ 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, 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).
repaired by the next reconnect, and by anti-entropy: every 10 minutes (a
fifth more or less, so nodes do not ask together) a node runs this sync again
with up to 3 random connected peers that share a region (a matching digest
makes a round nearly free).
- **Peer exchange** (`internal/p2p/peerexchange.go`): while a followed region
has fewer than 8 peers, a node asks its connected peers every 20 s who they
know there (sync request `{"kind":"peers","region":...}`, answered with up to
20 `{"id","addrs"}` lines). A peer answers even if it follows another region:
gossip tells it which topics its own connections subscribe to, so it names
those first (marked `near`) and then other connected peers. The asker dials
the `near` ones, and the others only while it knows nobody in the region, as a
way on (so discovery stays per region); then it asks the new connections, for
up to 3 rounds and 24 dials. Old nodes
do not know the kind and ignore it. The peer cache records which of the
node's regions a peer was seen in, and a restart redials those first.
- **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
@@ -657,7 +669,7 @@ packet loss looks like above TCP), `-hostile` (an attacker with no validators),
| 60 ms latency, 1130 connections cut in the run | 100% delivered; listings p95 67 ms, messages p95 0.4 s; returning nodes 6 s; all nodes agreed |
| 500 junk, forged, under-worked and wrong-region listings at one region | **0 stored**; honest listings during the attack 100% (p95 31 ms) |
| 400 valid envelopes flooding one listing | queue stayed under the limit (138 of 200 at the owner); the price rose from 6 to 7 bits; a buyer paying its own gateway's price still got through (43 ms) |
| listing only one node's store has (no gossip) | anti-entropy every 5 s delivered all 105: p50 18 s, p95 38 s, max 63 s. At the default 30 min that is a median of about 2 hours |
| listing only one node's store has (no gossip) | anti-entropy every 5 s, one peer per round (the old way): 108 of 109, p50 25.9 s, p95 44 s, max 49.9 s. With 3 peers per round: 109 of 109, p50 5.7 s, p95 10.6 s, max 13.7 s. Scaled to the new default of 10 min that is a median of about 11 minutes (it was about 2 hours at 30 min and one peer) |
| all seeds shut down | the network keeps working (329 of 329 listings); newcomers that remember two live peers of their own region got everything (160 of 160) |
| 300 nodes, 8 regions | 100% delivered (listings p95 75 ms, messages p95 0.8 s, reports p95 0.5 s), all agreed; 1.3 GB heap, 100k goroutines, 137 KB per node |
@@ -738,8 +750,15 @@ Findings (see "What is left" in SPECS.md for the open ones):
serve it. With every seed gone, a newcomer whose remembered peers are all in
another region found nobody (0 of 114 listings), and one that remembers no
live peer found no peer at all. A node that remembers peers of its own
region (the usual case) is fine.
4. **Repair is slow at the default interval** (see the table).
region (the usual case) is fine. Peer exchange (see "Reconnection") now
covers the first case: a newcomer connected to any peer that is connected to
the region finds it through that peer. Re-run with `-kill-seeds` (100 nodes,
seeds down): 6 newcomers whose two remembered peers were all in another
region got 56 of 56 listings, after about 5 s (the first exchange round),
where before they got none. One that remembers no live peer at all still
needs a seed or the DNS name.
4. **Repair at the default interval** takes about 11 minutes to the median
node (see the table); it was about 2 hours.
It shows protocol behaviour and cost, **not** the internet: no real packet
loss, NATs, clock skew or bandwidth limits, and all nodes share one machine, so