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
+73
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"log/slog"
"net/http"
@@ -300,3 +301,75 @@ func TestStatus(t *testing.T) {
t.Fatal("status must never expose the admin token")
}
}
// keeper is a RegionKeeper that follows the node's real regions.
type keeper struct {
node *p2p.Node
fail error
}
func (k *keeper) Regions() []string { return k.node.Regions() }
func (k *keeper) SetRegions(r []string) error {
if k.fail != nil {
return k.fail
}
return k.node.SetRegions(r)
}
func TestOwnerChangesTheRegions(t *testing.T) {
k := &keeper{}
f := newFixture(t, token, func(c *Config) {
k.node = c.Node
c.RegionKeeper = k
})
regions := func() []string {
_, body := f.do(t, "GET", "/api/v1/status", "", false)
var st struct {
Regions []string `json:"regions"`
}
_ = json.Unmarshal(body, &st)
return st.Regions
}
if got := regions(); len(got) != 1 || got[0] != "u1x" {
t.Fatalf("regions = %v", got)
}
if code, _ := f.do(t, "PUT", "/api/v1/regions", `{"regions":["u1j"]}`, false); code != 401 {
t.Fatalf("without the token: %d, want 401", code)
}
code, body := f.do(t, "PUT", "/api/v1/regions", `{"regions":[" U1J ","u1x","u1j"]}`, true)
if code != 200 {
t.Fatalf("PUT: %d %s", code, body)
}
if got := regions(); len(got) != 2 || got[0] != "u1x" || got[1] != "u1j" {
t.Fatalf("regions after the change (case and repeats tidied) = %v", got)
}
for name, tc := range map[string]struct{ body, want string }{
"not a region": {`{"regions":["u1j","nope"]}`, "not a region"},
"none": {`{"regions":[]}`, "at least one"},
"unknown field": {`{"regions":["u1j"],"x":1}`, "invalid JSON"},
"too many": {`{"regions":["u00","u01","u02","u03","u04","u05","u06","u07","u08","u09","u0b",` +
`"u0c","u0d","u0e","u0f","u0g","u0h","u0j","u0k","u0m","u0n"]}`, "too many"},
} {
code, body := f.do(t, "PUT", "/api/v1/regions", tc.body, true)
if code != 400 || !strings.Contains(string(body), tc.want) {
t.Errorf("%s: %d %s, want 400 containing %q", name, code, body, tc.want)
}
}
if got := regions(); len(got) != 2 {
t.Fatalf("a refused change must change nothing, regions = %v", got)
}
k.fail = errors.New("disk full")
if code, body := f.do(t, "PUT", "/api/v1/regions", `{"regions":["u1j"]}`, true); code != 500 || !strings.Contains(string(body), "disk full") {
t.Fatalf("a failing keeper: %d %s", code, body)
}
}
func TestRegionsCannotBeChangedWithoutAKeeper(t *testing.T) {
f := newFixture(t, token)
if code, _ := f.do(t, "PUT", "/api/v1/regions", `{"regions":["u1j"]}`, true); code == 200 {
t.Fatal("regions changed on a node that has no keeper")
}
}