@serfab/cadre-core 1.6.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/cadre-node.d.ts +204 -65
  2. package/dist/cadre-node.js +392 -95
  3. package/dist/cadre-node.js.map +1 -1
  4. package/dist/index.d.ts +5 -0
  5. package/dist/index.js +16 -0
  6. package/dist/index.js.map +1 -1
  7. package/dist/joined-strand-store.d.ts +112 -0
  8. package/dist/joined-strand-store.js +263 -0
  9. package/dist/joined-strand-store.js.map +1 -0
  10. package/dist/key-store.d.ts +6 -0
  11. package/dist/key-store.js.map +1 -1
  12. package/dist/node-local-snapshot.d.ts +5 -4
  13. package/dist/node-local-snapshot.js +6 -3
  14. package/dist/node-local-snapshot.js.map +1 -1
  15. package/dist/peer-join-backfill.d.ts +33 -14
  16. package/dist/peer-join-backfill.js +109 -50
  17. package/dist/peer-join-backfill.js.map +1 -1
  18. package/dist/strand-instance-manager.d.ts +39 -0
  19. package/dist/strand-instance-manager.js +74 -1
  20. package/dist/strand-instance-manager.js.map +1 -1
  21. package/dist/strand-peer-book-file.d.ts +12 -0
  22. package/dist/strand-peer-book-file.js +37 -0
  23. package/dist/strand-peer-book-file.js.map +1 -0
  24. package/dist/strand-peer-book-protocol.d.ts +159 -0
  25. package/dist/strand-peer-book-protocol.js +360 -0
  26. package/dist/strand-peer-book-protocol.js.map +1 -0
  27. package/dist/strand-peer-book-swap.d.ts +104 -0
  28. package/dist/strand-peer-book-swap.js +284 -0
  29. package/dist/strand-peer-book-swap.js.map +1 -0
  30. package/dist/strand-peer-book.d.ts +159 -0
  31. package/dist/strand-peer-book.js +354 -0
  32. package/dist/strand-peer-book.js.map +1 -0
  33. package/dist/strand-peer-observer.d.ts +88 -0
  34. package/dist/strand-peer-observer.js +180 -0
  35. package/dist/strand-peer-observer.js.map +1 -0
  36. package/dist/types.d.ts +58 -0
  37. package/dist/types.js.map +1 -1
  38. package/package.json +6 -2
  39. package/src/cadre-node.ts +7776 -7449
  40. package/src/index.ts +67 -0
  41. package/src/joined-strand-store.ts +308 -0
  42. package/src/key-store.ts +6 -0
  43. package/src/node-local-snapshot.ts +293 -289
  44. package/src/peer-join-backfill.ts +112 -51
  45. package/src/strand-instance-manager.ts +100 -1
  46. package/src/strand-peer-book-file.ts +39 -0
  47. package/src/strand-peer-book-protocol.ts +463 -0
  48. package/src/strand-peer-book-swap.ts +338 -0
  49. package/src/strand-peer-book.ts +468 -0
  50. package/src/strand-peer-observer.ts +235 -0
  51. package/src/types.ts +60 -0
@@ -0,0 +1,235 @@
1
+ /**
2
+ * The strand peer book's OBSERVATION writer: on one strand libp2p node, every peer
3
+ * whose identify result names this strand's block-transfer protocol — a strand peer,
4
+ * never the circuit relay or a bootstrap node ({@link speaksBlockTransfer}, the
5
+ * same gate `PeerJoinBackfill` schedules on) — is reported with its dialable
6
+ * addresses, so `CadreNode` can merge an unsigned book entry for it
7
+ * (`strand-peer-book.ts`). That is what lets a restarted machine dial the peers it
8
+ * was talking to, instead of only the ones a formation once carried back.
9
+ *
10
+ * Dialable addresses are the peer's announced `listenAddrs` from the identify
11
+ * result, plus the open connection's `remoteAddr` when it is relayed
12
+ * (`/p2p-circuit`) — a relay-only peer announces its circuit listener, but the
13
+ * address this node actually reached it on is the one proven to work. Every
14
+ * address is bound to the peer id (`withTrailingPeerId`), ordered signaling-first
15
+ * and capped like a formation's list, so the book files it under the right peer.
16
+ *
17
+ * Throttled to one report per peer per {@link STRAND_PEER_OBSERVE_THROTTLE_MS}
18
+ * unless the address set changed, so a flapping relayed connection — identify
19
+ * fires on every reconnect, every 14 s at worst — does not rewrite the node-local
20
+ * slot each time. Best-effort throughout: nothing here throws into a libp2p event
21
+ * handler.
22
+ *
23
+ * Lifecycle: one observer per running strand, armed in
24
+ * `StrandInstanceManager.buildStrandRuntime` right after the libp2p node exists
25
+ * (the bootstrap dials that follow `libp2p.start()` are exactly the peers worth
26
+ * observing) and stopped in `releaseRuntime`, so a hibernation wake re-arms it on
27
+ * the rebuilt node. `start` also walks the peers already connected, because
28
+ * identify may have completed for an early peer before this object existed.
29
+ */
30
+ import debug from 'debug';
31
+ import type { Connection, IdentifyResult, Libp2p, PeerId } from '@libp2p/interface';
32
+ import { multiaddr } from '@multiformats/multiaddr';
33
+ import { speaksBlockTransfer } from './peer-join-backfill.js';
34
+ import { isSignalingAddr, orderSignalingFirst, withTrailingPeerId } from './peer-record.js';
35
+ import { MAX_STRAND_ADDRS } from './strand-formation-protocol.js';
36
+
37
+ const log = debug('sereus:cadre:strand-peer-observer');
38
+
39
+ /** Minimum gap between two reports of ONE peer with an unchanged address set. */
40
+ export const STRAND_PEER_OBSERVE_THROTTLE_MS = 10 * 60 * 1000;
41
+
42
+ /** One strand peer seen live: its strand transport peer id and the addresses it is dialable at. */
43
+ export interface StrandPeerObservation {
44
+ peerId: string;
45
+ /** Bound to `peerId`, signaling-first, at most `MAX_STRAND_ADDRS`. */
46
+ addrs: string[];
47
+ }
48
+
49
+ export interface StrandPeerObserverDeps {
50
+ /** For logs (the strand id). */
51
+ label: string;
52
+ /** The strand's libp2p node. */
53
+ libp2p: Libp2p;
54
+ /** This strand network's `/optimystic/<networkName>` prefix — the block-transfer gate. */
55
+ protocolPrefix: string;
56
+ /** Called for every (throttled) observation; must not throw. */
57
+ onObserved: (observation: StrandPeerObservation) => void;
58
+ }
59
+
60
+ export interface StrandPeerObserverOptions {
61
+ /** Default {@link STRAND_PEER_OBSERVE_THROTTLE_MS}. */
62
+ throttleMs?: number;
63
+ /** Clock, for tests. Default `Date.now`. */
64
+ now?: () => number;
65
+ }
66
+
67
+ interface LastReport {
68
+ at: number;
69
+ /** The address set reported, canonicalised for comparison. */
70
+ key: string;
71
+ }
72
+
73
+ /**
74
+ * Anything that prints as a multiaddr. libp2p's identify result and peer store hand
75
+ * back its nested `@multiformats/multiaddr` copy, a structurally different type from
76
+ * this package's, so addresses are taken by their string form and re-parsed here.
77
+ */
78
+ export interface AddrLike {
79
+ toString(): string;
80
+ }
81
+
82
+ /** One connected peer as the peer store knows it: what identify recorded, plus its open connections. */
83
+ export interface ConnectedIdentifiedPeer {
84
+ peerId: PeerId;
85
+ protocols: string[];
86
+ /** The stored addresses: identify-announced, plus whatever else was merged in. */
87
+ addrs: AddrLike[];
88
+ connections: Connection[];
89
+ }
90
+
91
+ /**
92
+ * Every peer `libp2p` holds a connection to AND has already identified (it is in the
93
+ * peer store), grouped per peer. A connected peer whose identify has not finished is
94
+ * left out: its `peer:identify` event is still to come, and every listener that walks
95
+ * this list at start subscribes to that event too. Shared by the peer-book observer
96
+ * and the book swap (`strand-peer-book-swap.ts`), which both arm right after the
97
+ * node exists and may already have missed an early peer's identify.
98
+ */
99
+ export async function connectedIdentifiedPeers(libp2p: Libp2p, label: string): Promise<ConnectedIdentifiedPeer[]> {
100
+ const byPeer = new Map<string, { peerId: PeerId; connections: Connection[] }>();
101
+ for (const connection of libp2p.getConnections()) {
102
+ const key = connection.remotePeer.toString();
103
+ const group = byPeer.get(key) ?? { peerId: connection.remotePeer, connections: [] };
104
+ group.connections.push(connection);
105
+ byPeer.set(key, group);
106
+ }
107
+ const identified = await Promise.all([...byPeer.values()].map(async ({ peerId, connections }): Promise<ConnectedIdentifiedPeer | undefined> => {
108
+ try {
109
+ const peer = await libp2p.peerStore.get(peerId);
110
+ return { peerId, protocols: peer.protocols, addrs: peer.addresses.map((a) => a.multiaddr), connections };
111
+ } catch (error) {
112
+ // NotFoundError: identify has not finished; the event handler will see it.
113
+ if ((error as Error).name !== 'NotFoundError') {
114
+ log('[%s] peer store read for %s failed — skipping it: %o', label, peerId.toString(), error);
115
+ }
116
+ return undefined;
117
+ }
118
+ }));
119
+ return identified.filter((peer): peer is ConnectedIdentifiedPeer => peer !== undefined);
120
+ }
121
+
122
+ export class StrandPeerObserver {
123
+ private readonly throttleMs: number;
124
+ private readonly now: () => number;
125
+ private readonly lastReports = new Map<string, LastReport>();
126
+ private readonly onPeerIdentify: (evt: CustomEvent<IdentifyResult>) => void;
127
+ private started = false;
128
+ private stopped = false;
129
+
130
+ constructor(private readonly deps: StrandPeerObserverDeps, options?: StrandPeerObserverOptions) {
131
+ this.throttleMs = options?.throttleMs ?? STRAND_PEER_OBSERVE_THROTTLE_MS;
132
+ this.now = options?.now ?? Date.now;
133
+ this.onPeerIdentify = (evt) => {
134
+ const { peerId, protocols, listenAddrs, connection } = evt.detail;
135
+ if (speaksBlockTransfer(protocols, this.deps.protocolPrefix)) {
136
+ this.observe(peerId, listenAddrs, [connection]);
137
+ }
138
+ };
139
+ }
140
+
141
+ /** Subscribe to `peer:identify` and report the peers already connected and identified. */
142
+ start(): void {
143
+ if (this.started || this.stopped) return;
144
+ this.started = true;
145
+ this.deps.libp2p.addEventListener('peer:identify', this.onPeerIdentify);
146
+ void this.observeConnectedPeers().then((seen) => {
147
+ log('[%s] started (%d peer(s) already connected)', this.deps.label, seen);
148
+ });
149
+ }
150
+
151
+ /** Unsubscribe; later identify results are ignored. */
152
+ stop(): void {
153
+ if (this.stopped) return;
154
+ this.stopped = true;
155
+ if (this.started) {
156
+ this.deps.libp2p.removeEventListener('peer:identify', this.onPeerIdentify);
157
+ }
158
+ this.lastReports.clear();
159
+ log('[%s] stopped', this.deps.label);
160
+ }
161
+
162
+ /**
163
+ * Report every connected peer the peer store already knows speaks this strand's
164
+ * protocol, with its stored addresses. A peer not yet in the store (identify has
165
+ * not finished) is left to the `peer:identify` handler. Returns the number of
166
+ * identified peers considered.
167
+ *
168
+ * NOTE: the stored addresses include what `CadreNode.mergeStrandPeerAddrs` merged
169
+ * INTO the peer store from the book, so this walk can re-vouch a dead address the
170
+ * book already held, until the peer's next `peer:identify` replaces the set with
171
+ * what it announces now. Bounded by the address cap; if a dead address ever shows
172
+ * up surviving across resumes, read only the peer store's identify-sourced
173
+ * addresses here rather than shortening the age.
174
+ */
175
+ private async observeConnectedPeers(): Promise<number> {
176
+ const peers = await connectedIdentifiedPeers(this.deps.libp2p, this.deps.label);
177
+ for (const { peerId, protocols, addrs, connections } of peers) {
178
+ if (!this.stopped && speaksBlockTransfer(protocols, this.deps.protocolPrefix)) {
179
+ this.observe(peerId, addrs, connections);
180
+ }
181
+ }
182
+ return peers.length;
183
+ }
184
+
185
+ /** Shape, throttle and report one peer. Never throws. */
186
+ private observe(peerId: PeerId, listenAddrs: AddrLike[], connections: Connection[]): void {
187
+ if (this.stopped) return;
188
+ const id = peerId.toString();
189
+ // Identify is about remotes, so self never arrives here; belt and braces, since
190
+ // a node must never file its own addresses as a peer to dial.
191
+ if (id === this.deps.libp2p.peerId.toString()) return;
192
+ try {
193
+ const addrs = dialableAddrs(id, listenAddrs, connections);
194
+ if (!this.shouldReport(id, addrs)) return;
195
+ log('[%s] observed strand peer %s at %d addr(s)', this.deps.label, id, addrs.length);
196
+ this.deps.onObserved({ peerId: id, addrs });
197
+ } catch (error) {
198
+ log('[%s] observing %s failed (ignored): %o', this.deps.label, id, error);
199
+ }
200
+ }
201
+
202
+ /** One report per peer per throttle window, unless the address set changed. */
203
+ private shouldReport(peerId: string, addrs: string[]): boolean {
204
+ const now = this.now();
205
+ const key = [...addrs].sort().join('\n');
206
+ const last = this.lastReports.get(peerId);
207
+ if (last && last.key === key && now - last.at < this.throttleMs) {
208
+ return false;
209
+ }
210
+ this.lastReports.set(peerId, { at: now, key });
211
+ return true;
212
+ }
213
+ }
214
+
215
+ /**
216
+ * The addresses to file for `peerId`: its announced listen addrs plus every relayed
217
+ * connection addr, each bound to `peerId`, de-duplicated, signaling-first, capped.
218
+ * Re-parsed through this package's own `multiaddr` (see {@link AddrLike}).
219
+ */
220
+ export function dialableAddrs(peerId: string, listenAddrs: AddrLike[], connections: Connection[]): string[] {
221
+ const candidates = [
222
+ ...listenAddrs.map((ma) => ma.toString()),
223
+ ...connections.map((c) => c.remoteAddr.toString()).filter(isSignalingAddr)
224
+ ];
225
+ const bound = new Set<string>();
226
+ for (const candidate of candidates) {
227
+ const addr = withTrailingPeerId(multiaddr(candidate), peerId);
228
+ if (addr === null) {
229
+ log('dropping addr %s — it names a peer other than %s', candidate, peerId);
230
+ continue;
231
+ }
232
+ bound.add(addr.toString());
233
+ }
234
+ return orderSignalingFirst([...bound]).slice(0, MAX_STRAND_ADDRS);
235
+ }
package/src/types.ts CHANGED
@@ -10,7 +10,9 @@ import type { SeedTrustPolicy } from './seed-trust-policy.js';
10
10
  import type { KeyStore, KeyId } from './key-store.js';
11
11
  import type { TrustedOwnerStore, TrustSource } from './trusted-owner-store.js';
12
12
  import type { BootstrapPeerStore } from './bootstrap-peer-store.js';
13
+ import type { StrandPeerBookStore } from './strand-peer-book.js';
13
14
  import type { EnrolledMachineStore } from './enrolled-machine-store.js';
15
+ import type { JoinedStrandStore } from './joined-strand-store.js';
14
16
  import type { PushNotifier } from './push-notifier.js';
15
17
  import type { RevocableTable } from './control-authorization.js';
16
18
  import type { ControlRetryAbandonment } from './control-retry.js';
@@ -947,6 +949,54 @@ export interface CadreNodeConfig {
947
949
  store?: EnrolledMachineStore;
948
950
  };
949
951
 
952
+ /**
953
+ * Node-local record of the strands this node joined from ANOTHER party (see
954
+ * `joined-strand-store.ts`): id, type and the closed strand's read secret, recorded
955
+ * by `formStrand` and by a joining (not `founder: true`) `addStrand` whose row is
956
+ * not in this party's control database, and re-offered on every start as `strand:discovered` — so an embedding
957
+ * app keeps no list of its own.
958
+ *
959
+ * Absent ⇒ a `KeyStoreJoinedStrandStore` over {@link keyStore} when one is
960
+ * configured (the record carries a secret, and the KeyStore is where every platform
961
+ * already keeps secrets), else an in-memory store that warns once and forgets every
962
+ * join on restart. An embedder that passes {@link privateKey} rather than a
963
+ * `keyStore` injects a durable store here, e.g.
964
+ * `new KeyStoreJoinedStrandStore(new FileKeyStore(dir), partyId)`.
965
+ */
966
+ joinedStrands?: {
967
+ /** Its `partyId` must match `controlNetwork.partyId`; start() fails closed on a mismatch. */
968
+ store?: JoinedStrandStore;
969
+ };
970
+
971
+ /**
972
+ * Node-local strand peer book (see `strand-peer-book.ts`): per strand, the strand
973
+ * peers this node has met — the other parties' strand transport peers a formation
974
+ * carried back, and every strand peer observed on a live connection — with their
975
+ * last-known addresses. Read on every launch, hibernation resume and periodic
976
+ * address refresh, so a restarted machine dials the peers it was talking to before
977
+ * anything else, instead of coming back up alone (gotchoices/sereus#18). Sibling
978
+ * of {@link bootstrapPeers} — same NON-replicated, per-party, injected-backend
979
+ * shape — and, like it, nothing here is trust-bearing: dial hints only, each
980
+ * bound to its peer id. Entries age out after 14 days (`STRAND_PEER_MAX_AGE_MS`,
981
+ * overridable per store via `StrandPeerBookOptions.maxAgeMs`).
982
+ *
983
+ * Absent ⇒ an in-memory store is created at start() (ephemeral: the book dies
984
+ * with the process, and a cross-party strand whose only contact was the other
985
+ * party does not re-mesh after a restart until a fresh formation). Every
986
+ * reference embedder injects a durable backend over the same slot kind as its
987
+ * bootstrap-peer store. A new platform needs no new store class: supply a
988
+ * `DurableSlot` and inject `PersistentStrandPeerBookStore.open(slot, partyId)`.
989
+ */
990
+ strandPeers?: {
991
+ /**
992
+ * Injected store instance — e.g. a `FileStrandPeerBookStore` from the
993
+ * Node-only subpath `@serfab/cadre-core/strand-peer-book-file`, persisted in
994
+ * the node's state directory. Its `partyId` must match
995
+ * `controlNetwork.partyId`; start() fails closed on a mismatch.
996
+ */
997
+ store?: StrandPeerBookStore;
998
+ };
999
+
950
1000
  /**
951
1001
  * Platform push-delivery for suspended mobile peers. When present, the node's
952
1002
  * server fan-out can deliver strand-wake data messages over the platform push
@@ -1394,6 +1444,10 @@ export interface CadreNodeEvents {
1394
1444
  * re-admits this party, so a second removal is reported again — and a
1395
1445
  * hibernation wake rebuilds the gate, which may re-emit for a strand still
1396
1446
  * revoked.
1447
+ *
1448
+ * For a strand joined from another party, the node also forgets its remembered
1449
+ * join (see {@link CadreNodeConfig.joinedStrands}): it keeps running this session
1450
+ * and is not re-offered after the next start.
1397
1451
  */
1398
1452
  'strand:revoked': { strandId: string };
1399
1453
  /**
@@ -1427,6 +1481,12 @@ export interface CadreNodeEvents {
1427
1481
  * Carries the full {@link StrandRow} so the app can join without re-querying
1428
1482
  * the control DB.
1429
1483
  *
1484
+ * Also emitted for a strand this node joined from ANOTHER party, which no control
1485
+ * row names: the node remembers every such join (see
1486
+ * {@link CadreNodeConfig.joinedStrands}) and offers it here on each start, as a row
1487
+ * with `FounderOwnerKey: null` carrying the closed strand's `MemberPrivateKey`. That
1488
+ * row is the product of the formation's consent, so claiming it needs no second one.
1489
+ *
1430
1490
  * **Fired once per strand per session, and it can fire before your listener is
1431
1491
  * attached.** The strand watcher's first poll runs inside `CadreNode.start()`
1432
1492
  * (100 ms after the watcher starts), so every strand already stored for this