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:
+68
-21
@@ -39,7 +39,6 @@ const (
|
||||
RevocationTopic = "/allmende/1/revocations"
|
||||
|
||||
mdnsService = "allmende"
|
||||
resyncInterval = 5 * time.Minute
|
||||
maintenanceTick = time.Hour
|
||||
)
|
||||
|
||||
@@ -53,10 +52,11 @@ type Config struct {
|
||||
Regions []string // geohash precision-3 cells to replicate
|
||||
MDNS bool // discover peers on the local network
|
||||
|
||||
Difficulty int // required proof-of-work bits
|
||||
SyncPageSize int // records per sync page (default 500)
|
||||
Clock func() time.Time // default time.Now
|
||||
Logger *slog.Logger
|
||||
Difficulty int // required proof-of-work bits
|
||||
SyncPageSize int // records per sync page (default 500)
|
||||
RedialInterval time.Duration // how often lost bootstrap peers are redialed (default 20s)
|
||||
Clock func() time.Time // default time.Now
|
||||
Logger *slog.Logger
|
||||
}
|
||||
|
||||
// Node is a running participant.
|
||||
@@ -75,7 +75,8 @@ type Node struct {
|
||||
mu sync.Mutex
|
||||
topics map[string]*pubsub.Topic
|
||||
subscribed []string
|
||||
synced map[peer.ID]time.Time
|
||||
bootstrap []peer.AddrInfo
|
||||
syncing map[peer.ID]bool
|
||||
revTopic *pubsub.Topic
|
||||
}
|
||||
|
||||
@@ -95,6 +96,17 @@ func New(ctx context.Context, cfg Config) (*Node, error) {
|
||||
if cfg.SyncPageSize <= 0 {
|
||||
cfg.SyncPageSize = 500
|
||||
}
|
||||
if cfg.RedialInterval <= 0 {
|
||||
cfg.RedialInterval = 20 * time.Second
|
||||
}
|
||||
var bootstrap []peer.AddrInfo
|
||||
for _, addr := range cfg.Bootstrap {
|
||||
ai, err := peer.AddrInfoFromString(addr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("p2p: bad bootstrap address %q: %w", addr, err)
|
||||
}
|
||||
bootstrap = append(bootstrap, *ai)
|
||||
}
|
||||
|
||||
priv, err := crypto.UnmarshalEd25519PrivateKey(cfg.Device.Key.PrivateKey())
|
||||
if err != nil {
|
||||
@@ -107,14 +119,15 @@ func New(ctx context.Context, cfg Config) (*Node, error) {
|
||||
|
||||
nctx, cancel := context.WithCancel(ctx)
|
||||
n := &Node{
|
||||
cfg: cfg,
|
||||
host: h,
|
||||
clock: cfg.Clock,
|
||||
log: cfg.Logger,
|
||||
ctx: nctx,
|
||||
cancel: cancel,
|
||||
topics: map[string]*pubsub.Topic{},
|
||||
synced: map[peer.ID]time.Time{},
|
||||
cfg: cfg,
|
||||
host: h,
|
||||
clock: cfg.Clock,
|
||||
log: cfg.Logger,
|
||||
ctx: nctx,
|
||||
cancel: cancel,
|
||||
topics: map[string]*pubsub.Topic{},
|
||||
syncing: map[peer.ID]bool{},
|
||||
bootstrap: bootstrap,
|
||||
}
|
||||
if n.clock == nil {
|
||||
n.clock = time.Now
|
||||
@@ -149,11 +162,7 @@ func New(ctx context.Context, cfg Config) (*Node, error) {
|
||||
ConnectedF: func(_ network.Network, c network.Conn) { n.syncSoon(c.RemotePeer()) },
|
||||
})
|
||||
|
||||
for _, addr := range cfg.Bootstrap {
|
||||
if err := n.Connect(nctx, addr); err != nil {
|
||||
n.log.Warn("bootstrap peer unreachable", "addr", addr, "err", err)
|
||||
}
|
||||
}
|
||||
n.dialBootstrap()
|
||||
if cfg.MDNS {
|
||||
n.mdns = mdns.NewMdnsService(h, mdnsService, &mdnsNotifee{n: n})
|
||||
if err := n.mdns.Start(); err != nil {
|
||||
@@ -162,8 +171,9 @@ func New(ctx context.Context, cfg Config) (*Node, error) {
|
||||
}
|
||||
}
|
||||
|
||||
n.wg.Add(1)
|
||||
n.wg.Add(2)
|
||||
go n.maintain()
|
||||
go n.redial()
|
||||
return n, nil
|
||||
}
|
||||
|
||||
@@ -192,6 +202,9 @@ func (n *Node) Connect(ctx context.Context, addr string) error {
|
||||
return n.host.Connect(ctx, *ai)
|
||||
}
|
||||
|
||||
// PeerCount returns the number of currently connected peers.
|
||||
func (n *Node) PeerCount() int { return len(n.host.Network().Peers()) }
|
||||
|
||||
// RegionPeers lists peers known to be subscribed to the region's topic.
|
||||
func (n *Node) RegionPeers(region string) []peer.ID {
|
||||
return n.ps.ListPeers(listingTopicPrefix + region)
|
||||
@@ -366,7 +379,7 @@ func (n *Node) validateListing(ctx context.Context, data []byte) (*listing.Listi
|
||||
}
|
||||
return nil, pubsub.ValidationReject
|
||||
}
|
||||
if revoked, err := n.cfg.Store.IsRevoked(ctx, l.Author, l.Device); err != nil || revoked {
|
||||
if revoked, err := n.cfg.Store.IsRevoked(ctx, l.Author, l.Device, l.Cert.IssuedAt); err != nil || revoked {
|
||||
return nil, pubsub.ValidationIgnore
|
||||
}
|
||||
return &l, pubsub.ValidationAccept
|
||||
@@ -417,6 +430,40 @@ func (m *mdnsNotifee) HandlePeerFound(pi peer.AddrInfo) {
|
||||
}()
|
||||
}
|
||||
|
||||
// dialBootstrap connects to every configured bootstrap peer we are not
|
||||
// already connected to.
|
||||
func (n *Node) dialBootstrap() {
|
||||
for _, ai := range n.bootstrap {
|
||||
if n.host.Network().Connectedness(ai.ID) == network.Connected {
|
||||
continue
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(n.ctx, 10*time.Second)
|
||||
if err := n.host.Connect(ctx, ai); err != nil {
|
||||
n.log.Warn("bootstrap peer unreachable", "peer", ai.ID, "err", err)
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// redial keeps the node attached to its bootstrap peers across restarts and
|
||||
// network outages on either side.
|
||||
func (n *Node) redial() {
|
||||
defer n.wg.Done()
|
||||
if len(n.bootstrap) == 0 {
|
||||
return
|
||||
}
|
||||
t := time.NewTicker(n.cfg.RedialInterval)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-n.ctx.Done():
|
||||
return
|
||||
case <-t.C:
|
||||
n.dialBootstrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// maintain periodically drops expired data.
|
||||
func (n *Node) maintain() {
|
||||
defer n.wg.Done()
|
||||
|
||||
Reference in New Issue
Block a user