Add runnable node, HTTP API, all-devices revocation and reconnect sync

- cmd/node: init (recovery phrase onboarding), run, recover
- api: public reads of canonical signed records, token-protected writes
- identity: RevokeAll voids every device certified before a cutoff, so
  recovery works with only the phrase; store purges by certificate age
- p2p: redial bootstrap peers, sync on every new connection (in-flight
  guard only), validate bootstrap addresses up front
- docs: API, recovery semantics, peer-ID change on recovery

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 18:03:46 +01:00
co-authored by Claude Sonnet 5.5
parent d3efe5a3a7
commit 918e16ed37
18 changed files with 1550 additions and 82 deletions
+75 -10
View File
@@ -6,6 +6,7 @@ import (
"errors"
"io"
"log/slog"
"os"
"testing"
"time"
@@ -19,6 +20,14 @@ import (
const difficulty = 6
// testLogger is silent unless P2P_DEBUG is set.
func testLogger() *slog.Logger {
if os.Getenv("P2P_DEBUG") != "" {
return slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug}))
}
return slog.New(slog.NewTextHandler(io.Discard, nil))
}
type tnode struct {
*Node
s *store.Store
@@ -27,6 +36,11 @@ type tnode struct {
}
func startNode(t *testing.T, root *identity.Identity, regions ...string) *tnode {
t.Helper()
return startNodeWith(t, root, nil, regions...)
}
func startNodeWith(t *testing.T, root *identity.Identity, tweak func(*Config), regions ...string) *tnode {
t.Helper()
if root == nil {
root, _, _ = identity.NewRoot()
@@ -39,15 +53,20 @@ func startNode(t *testing.T, root *identity.Identity, regions ...string) *tnode
if err != nil {
t.Fatal(err)
}
n, err := New(context.Background(), Config{
Device: dev,
Store: s,
ListenAddrs: []string{"/ip4/127.0.0.1/tcp/0"},
Regions: regions,
Difficulty: difficulty,
SyncPageSize: 3,
Logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
})
cfg := Config{
Device: dev,
Store: s,
ListenAddrs: []string{"/ip4/127.0.0.1/tcp/0"},
Regions: regions,
Difficulty: difficulty,
SyncPageSize: 3,
RedialInterval: 200 * time.Millisecond,
Logger: testLogger(),
}
if tweak != nil {
tweak(&cfg)
}
n, err := New(context.Background(), cfg)
if err != nil {
t.Fatal(err)
}
@@ -207,7 +226,7 @@ func TestRevocationPropagatesAndBlocks(t *testing.T) {
late := startNode(t, nil, "u1x")
connect(t, late, b)
eventually(t, "late joiner learns the revocation", func() bool {
ok, _ := late.s.IsRevoked(ctx, a.dev.Cert.Author, a.dev.Cert.Device)
ok, _ := late.s.IsRevoked(ctx, a.dev.Cert.Author, a.dev.Cert.Device, a.dev.Cert.IssuedAt)
return ok
})
}
@@ -255,3 +274,49 @@ func TestValidatorRejectsBadMessages(t *testing.T) {
t.Fatalf("expired listing: %v", r)
}
}
func TestRedialsBootstrapPeerAfterDisconnect(t *testing.T) {
a := startNode(t, nil, "u1x")
b := startNodeWith(t, nil, func(c *Config) { c.Bootstrap = []string{a.Addrs()[0]} }, "u1x")
eventually(t, "b connected to a", func() bool { return b.PeerCount() == 1 })
if err := b.host.Network().ClosePeer(a.ID()); err != nil {
t.Fatal(err)
}
eventually(t, "b redials its bootstrap peer", func() bool { return b.PeerCount() == 1 })
}
func TestEveryNewConnectionCatchesUp(t *testing.T) {
a := startNode(t, nil, "u1x")
b := startNode(t, nil, "u1x")
connect(t, b, a)
eventually(t, "first connection settled", func() bool { return len(b.RegionPeers("u1x")) == 1 })
// The link drops, and a gains a listing that is never gossiped to b.
l := a.listing(t, "u1xjhx") // sealed up front: proof-of-work is slow under -race
if err := b.host.Network().ClosePeer(a.ID()); err != nil {
t.Fatal(err)
}
eventually(t, "link is down", func() bool { return b.PeerCount() == 0 })
if _, err := a.s.Put(context.Background(), l); err != nil {
t.Fatal(err)
}
// A reconnect, however soon after the last sync, must catch up.
connect(t, b, a)
eventually(t, "b catches up after reconnecting", func() bool { return count(b) == 1 })
}
func TestBadBootstrapAddressRejectedUpFront(t *testing.T) {
root, _, _ := identity.NewRoot()
dev, _ := identity.Enroll(root, time.Now().Add(-time.Hour), 24*time.Hour)
s, _ := store.Open(":memory:", store.Options{Difficulty: difficulty})
defer s.Close()
_, err := New(context.Background(), Config{
Device: dev, Store: s, ListenAddrs: []string{"/ip4/127.0.0.1/tcp/0"},
Bootstrap: []string{"not-an-address"},
})
if err == nil {
t.Fatal("malformed bootstrap address accepted")
}
}