Add bootstrap nodes, DNS-based discovery, a DHT per region and NAT traversal

- Bootstrap: built-in /dnsaddr/allmende.stinnesbeck.com (TXT records at
  _dnsaddr.<name>), resolved at every redial with the last good answer kept;
  --bootstrap takes /p2p/ or /dnsaddr/ entries, --no-default-bootstrap drops
  the built-in one. Stopping never waits for a stuck resolver.
- Remembered peers in peers.json (saved every 5 min and on shutdown, redialled
  at the next start)
- Private Kademlia DHT (/allmende prefix), advertised and looked up per region
  with quick first attempts and a backoff; --no-dht turns it off
- NAT traversal: port mapping, hole punching, AutoRelay through bootstrap nodes
- --public: DHT server and relay for a bootstrap node; prints the TXT records
- Docker, NixOS module (public, defaultBootstrap, QUIC/UDP) and docs updated
- e2e: bootstrap node scenario, seed cache restart, and polling that really polls

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 22:14:34 +01:00
co-authored by Claude Sonnet 5.5
parent a81595e9ee
commit e7bec4fb63
18 changed files with 1281 additions and 69 deletions
+49 -6
View File
@@ -25,8 +25,20 @@ wait_for() { # description, timeout, command...
for _ in $(seq 1 $((t*10))); do if "$@" >/dev/null 2>&1; then return 0; fi; sleep 0.1; done
bad "timed out: $what"
}
# check OP WANT COMMAND...: runs COMMAND each time it is polled and compares its output.
# (Writing test "$(command)" = x inside wait_for would run the command only once.)
check() {
local op=$1 want=$2 got; shift 2
got=$("$@" 2>/dev/null) || return 1
case $op in
eq) [ "$got" = "$want" ] ;;
ge) [ "$got" -ge "$want" ] 2>/dev/null ;;
esac
}
peers() { curl -s http://127.0.0.1:$1/api/v1/status | python3 -c 'import json,sys; print(json.load(sys.stdin)["peers"])'; }
DIFF=10
COMMON="--region u1x --no-mdns --difficulty $DIFF"
# --no-default-bootstrap: these nodes must not look up the real bootstrap name in DNS.
COMMON="--region u1x --no-mdns --no-default-bootstrap --difficulty $DIFF"
echo "== init"
$BIN init --data $A > $W/init-a.txt || bad "init A"
@@ -47,7 +59,7 @@ $BIN run --data $B $COMMON --http 127.0.0.1:18102 --listen /ip4/127.0.0.1/tcp/14
--bootstrap /ip4/127.0.0.1/tcp/14101/p2p/$PEER 2> $W/b.log &
PIDS+=($!)
wait_for "B http" 10 curl -sf http://127.0.0.1:18102/api/v1/status
wait_for "nodes connected" 15 test "$(curl -s http://127.0.0.1:18101/api/v1/status | python3 -c 'import json,sys; print(json.load(sys.stdin)["peers"])')" -ge 1
wait_for "nodes connected" 15 check ge 1 peers 18101
ok "nodes connected"
echo "== publish on A, read on B"
@@ -58,7 +70,7 @@ RESP=$(curl -s -X POST -H "Authorization: Bearer $TOKEN" -d "$DRAFT" http://127.
ID=$(echo "$RESP" | python3 -c 'import json,sys; print(json.load(sys.stdin)["id"])') || bad "create: $RESP"
ok "published listing $ID"
count_b() { curl -s "http://127.0.0.1:18102/api/v1/listings?geohash=u1x" | python3 -c 'import json,sys; print(len(json.load(sys.stdin)))'; }
wait_for "B receives the listing" 15 test "$(count_b)" = 1
wait_for "B receives the listing" 15 check eq 1 count_b
ok "node B has the listing via gossip"
echo "== UI-style edit and renew on A (draft built by the UI's own code)"
@@ -71,7 +83,8 @@ echo "$PUTR" | python3 -c 'import json,sys; l=json.load(sys.stdin); assert l["ti
RENEWED=$(echo "$PUTR" | node $RENEW "" 30) || bad "renew draft"
PUTR=$(curl -s -X PUT -H "Authorization: Bearer $TOKEN" -d "$RENEWED" http://127.0.0.1:18101/api/v1/listings/$ID)
echo "$PUTR" | python3 -c 'import json,sys; l=json.load(sys.stdin); assert l["seq"]==3 and l["expires_at"]-l["created_at"]>29*86400, l' && ok "renew accepted: seq 3, fresh 30-day term" || bad "renew refused: $PUTR"
wait_for "B has the renewed version" 15 test "$(curl -s http://127.0.0.1:18102/api/v1/listings/$AUTHOR0/$ID | python3 -c 'import json,sys; print(json.load(sys.stdin)["seq"])')" = 3
seq_on_b() { curl -s http://127.0.0.1:18102/api/v1/listings/$AUTHOR0/$ID | python3 -c 'import json,sys; print(json.load(sys.stdin)["seq"])'; }
wait_for "B has the renewed version" 15 check eq 3 seq_on_b
ok "node B serves the renewed version"
MOVED=$(echo "$PUTR" | node $RENEW "" 30 | python3 -c 'import json,sys; d=json.load(sys.stdin); d["geohash"]="u28xyz"; print(json.dumps(d))')
curl -s -o /dev/null -w "%{http_code}" -X PUT -H "Authorization: Bearer $TOKEN" -d "$MOVED" http://127.0.0.1:18101/api/v1/listings/$ID | grep -q 400 && ok "moving a listing to another region is refused" || bad "region change was not refused"
@@ -89,7 +102,7 @@ grep -q "Ist die Wohnung noch frei" "$W/a.log" && bad "the buyer's text was prin
ok "the notice does not show what the buyer wrote"
[ "$(grep -c 'msg="new conversation"' "$W/a.log")" = 1 ] || bad "the notice was repeated"
inbox_a() { curl -s -H "Authorization: Bearer $TOKEN" http://127.0.0.1:18101/api/v1/inbox | python3 -c 'import json,sys; print(len(json.load(sys.stdin)))'; }
wait_for "A receives the message" 20 test "$(inbox_a)" = 1
wait_for "A receives the message" 20 check eq 1 inbox_a
ok "owner's node A decrypted it (via gossip through B)"
THREAD=$(curl -s -H "Authorization: Bearer $TOKEN" http://127.0.0.1:18101/api/v1/inbox | python3 -c 'import json,sys; print(json.load(sys.stdin)[0]["thread"])')
curl -s -H "Authorization: Bearer $TOKEN" http://127.0.0.1:18101/api/v1/inbox/$ID/$THREAD | grep -q "Ist die Wohnung noch frei" && ok "owner reads the buyer's text"
@@ -131,6 +144,36 @@ dev_on_b() { curl -s "http://127.0.0.1:18102/api/v1/listings?geohash=u1x" | pyth
import json,sys
l=json.load(sys.stdin)
print(l[0]["device"] if len(l)==1 else "none")'; }
wait_for "B switches to the new device's version" 60 test "$(dev_on_b)" = "$NEW_DEVICE"
wait_for "B switches to the new device's version" 60 check eq "$NEW_DEVICE" dev_on_b
ok "node B now serves the listing re-signed by the new device (old device revoked network-wide)"
echo "== bootstrap node: two nodes that only know it find each other"
C=$W/c; X=$W/x; Y=$W/y
for d in $C $X $Y; do $BIN init --data $d >/dev/null || bad "init $d"; done
$BIN run --data $C $COMMON --public --http 127.0.0.1:18103 --listen /ip4/127.0.0.1/tcp/14103 2> $W/c.log &
PIDS+=($!)
wait_for "bootstrap node http" 10 curl -sf http://127.0.0.1:18103/api/v1/status
PEER_C=$(curl -s http://127.0.0.1:18103/api/v1/status | python3 -c 'import json,sys; print(json.load(sys.stdin)["peer_id"])')
grep -q "no public address" $W/c.log && ok "--public on loopback says it has no public address to publish" || bad "no hint about the missing public address"
$BIN run --data $X $COMMON --http 127.0.0.1:18104 --listen /ip4/127.0.0.1/tcp/14104 \
--bootstrap /ip4/127.0.0.1/tcp/14103/p2p/$PEER_C 2> $W/x.log &
XPID=$!; PIDS+=($XPID)
$BIN run --data $Y $COMMON --http 127.0.0.1:18105 --listen /ip4/127.0.0.1/tcp/14105 \
--bootstrap /ip4/127.0.0.1/tcp/14103/p2p/$PEER_C 2> $W/y.log &
PIDS+=($!)
wait_for "X and Y connected to the bootstrap node" 20 check ge 2 peers 18103
# Each of X and Y has two peers only if they found each other (C is the other one).
wait_for "X finds Y through the DHT" 60 check ge 2 peers 18104
wait_for "Y finds X through the DHT" 20 check ge 2 peers 18105
ok "X and Y found each other through the DHT, introduced by nobody"
echo "== a node remembers its peers: restart X without any bootstrap"
kill $XPID; wait $XPID 2>/dev/null
[ -s $X/peers.json ] && grep -q "$PEER_C" $X/peers.json && ok "peers were saved on shutdown" || bad "peers.json missing or without the bootstrap node"
$BIN run --data $X $COMMON --http 127.0.0.1:18104 --listen /ip4/127.0.0.1/tcp/14104 2> $W/x2.log &
PIDS+=($!)
wait_for "X restarted" 10 curl -sf http://127.0.0.1:18104/api/v1/status
wait_for "X reconnects from its remembered peers" 30 check ge 1 peers 18104
ok "X rejoined the network with no bootstrap configured"
echo "ALL E2E CHECKS PASSED"