Let the owner change regions by place name, without a restart

The p2p node can now add and drop regions while running (SetRegions):
topics, validators, subscriptions and per-region peer discovery start and
stop with them, and a newly followed region catches up by sync.

PUT /api/v1/regions (owner token) applies a change and the node saves it in
regions.json, which wins over --region from then on. The owner's browser
gets a "Regions" page: search a place by name (GeoNames cities, offline,
CC BY 4.0), follow its cell plus any cell within about 40 km.

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-18 10:28:16 +01:00
co-authored by Claude Sonnet 5.5
parent e7bec4fb63
commit fd439710df
24 changed files with 70080 additions and 42 deletions
+61
View File
@@ -324,3 +324,64 @@ func TestBadBootstrapAddressRejectedUpFront(t *testing.T) {
t.Fatal("malformed bootstrap address accepted")
}
}
func TestRegionsChangeWhileRunning(t *testing.T) {
a := startNode(t, nil, "u1x")
b := startNode(t, nil, "u30")
connect(t, b, a)
first := a.listing(t, "u1xjhx")
if err := a.Publish(context.Background(), first); err != nil {
t.Fatal(err)
}
time.Sleep(500 * time.Millisecond)
if count(b) != 0 {
t.Fatal("b received a listing of a region it does not follow")
}
// Following u1x as well: what was published before arrives by sync, what
// is published from now on by gossip.
if err := b.SetRegions([]string{"u30", "u1x", "u1x"}); err != nil {
t.Fatal(err)
}
if got := b.Regions(); len(got) != 2 || got[0] != "u30" || got[1] != "u1x" {
t.Fatalf("regions = %v", got)
}
eventually(t, "b catches up on u1x", func() bool { return count(b) == 1 })
eventually(t, "a sees b on the u1x topic", func() bool { return len(a.RegionPeers("u1x")) == 1 })
if err := a.Publish(context.Background(), a.listing(t, "u1xjhy")); err != nil {
t.Fatal(err)
}
eventually(t, "b receives gossip of the new region", func() bool { return count(b) == 2 })
// Dropping it again: b leaves the topic, nothing more arrives, and what
// was stored stays.
if err := b.SetRegions([]string{"u30"}); err != nil {
t.Fatal(err)
}
if got := b.Regions(); len(got) != 1 || got[0] != "u30" {
t.Fatalf("regions = %v", got)
}
eventually(t, "a no longer sees b on u1x", func() bool { return len(a.RegionPeers("u1x")) == 0 })
if err := a.Publish(context.Background(), a.listing(t, "u1xjhz")); err != nil {
t.Fatal(err)
}
time.Sleep(500 * time.Millisecond)
if count(b) != 2 {
t.Fatalf("b has %d listings, want the 2 it had", count(b))
}
// And back: the region can be followed again.
if err := b.SetRegions([]string{"u30", "u1x"}); err != nil {
t.Fatal(err)
}
eventually(t, "b follows u1x again and catches up", func() bool { return count(b) == 3 })
// Nothing changes on a bad region.
if err := b.SetRegions([]string{"u1x", "no"}); err == nil {
t.Fatal("an invalid region was accepted")
}
if got := b.Regions(); len(got) != 2 {
t.Fatalf("regions changed to %v", got)
}
}