@serfab/cadre-core 0.12.0 → 1.0.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 (155) hide show
  1. package/README.md +23 -8
  2. package/dist/bootstrap-peer-store-file.js +2 -2
  3. package/dist/bootstrap-peer-store.d.ts +32 -12
  4. package/dist/bootstrap-peer-store.js +41 -15
  5. package/dist/bootstrap-peer-store.js.map +1 -1
  6. package/dist/cadre-node.d.ts +883 -102
  7. package/dist/cadre-node.js +1800 -209
  8. package/dist/cadre-node.js.map +1 -1
  9. package/dist/control-authorization.d.ts +2 -2
  10. package/dist/control-authorization.js +1 -0
  11. package/dist/control-authorization.js.map +1 -1
  12. package/dist/control-cohort.d.ts +13 -26
  13. package/dist/control-cohort.js +0 -26
  14. package/dist/control-cohort.js.map +1 -1
  15. package/dist/control-database.d.ts +318 -44
  16. package/dist/control-database.js +516 -74
  17. package/dist/control-database.js.map +1 -1
  18. package/dist/control-read-retry.d.ts +40 -11
  19. package/dist/control-read-retry.js +48 -15
  20. package/dist/control-read-retry.js.map +1 -1
  21. package/dist/control-retry.d.ts +58 -3
  22. package/dist/control-retry.js +58 -3
  23. package/dist/control-retry.js.map +1 -1
  24. package/dist/control-schema.d.ts +1 -1
  25. package/dist/control-schema.js +133 -21
  26. package/dist/control-schema.js.map +1 -1
  27. package/dist/control-write-retry.d.ts +80 -11
  28. package/dist/control-write-retry.js +168 -29
  29. package/dist/control-write-retry.js.map +1 -1
  30. package/dist/enrolled-machine-store-file.d.ts +13 -0
  31. package/dist/enrolled-machine-store-file.js +41 -0
  32. package/dist/enrolled-machine-store-file.js.map +1 -0
  33. package/dist/enrolled-machine-store.d.ts +81 -0
  34. package/dist/enrolled-machine-store.js +225 -0
  35. package/dist/enrolled-machine-store.js.map +1 -0
  36. package/dist/file-durable-slot.js +5 -3
  37. package/dist/file-durable-slot.js.map +1 -1
  38. package/dist/index.d.ts +17 -8
  39. package/dist/index.js +54 -9
  40. package/dist/index.js.map +1 -1
  41. package/dist/membership-connection-gater.d.ts +15 -1
  42. package/dist/membership-connection-gater.js +9 -3
  43. package/dist/membership-connection-gater.js.map +1 -1
  44. package/dist/node-local-snapshot.d.ts +21 -7
  45. package/dist/node-local-snapshot.js +30 -9
  46. package/dist/node-local-snapshot.js.map +1 -1
  47. package/dist/peer-dial.d.ts +143 -0
  48. package/dist/peer-dial.js +176 -0
  49. package/dist/peer-dial.js.map +1 -0
  50. package/dist/{strand-backfill.d.ts → peer-join-backfill.d.ts} +49 -20
  51. package/dist/{strand-backfill.js → peer-join-backfill.js} +133 -52
  52. package/dist/peer-join-backfill.js.map +1 -0
  53. package/dist/relay-addrs.d.ts +160 -49
  54. package/dist/relay-addrs.js +287 -60
  55. package/dist/relay-addrs.js.map +1 -1
  56. package/dist/relay-reservation.d.ts +71 -3
  57. package/dist/relay-reservation.js +116 -8
  58. package/dist/relay-reservation.js.map +1 -1
  59. package/dist/seed-bootstrap.d.ts +30 -1
  60. package/dist/seed-bootstrap.js +62 -17
  61. package/dist/seed-bootstrap.js.map +1 -1
  62. package/dist/storage-scope.d.ts +62 -0
  63. package/dist/storage-scope.js +75 -0
  64. package/dist/storage-scope.js.map +1 -0
  65. package/dist/strand-database.d.ts +41 -11
  66. package/dist/strand-database.js +48 -18
  67. package/dist/strand-database.js.map +1 -1
  68. package/dist/strand-first-sync-gate.d.ts +152 -0
  69. package/dist/strand-first-sync-gate.js +208 -0
  70. package/dist/strand-first-sync-gate.js.map +1 -0
  71. package/dist/strand-formation-manager.d.ts +99 -5
  72. package/dist/strand-formation-manager.js +119 -8
  73. package/dist/strand-formation-manager.js.map +1 -1
  74. package/dist/strand-formation-protocol.d.ts +97 -3
  75. package/dist/strand-formation-protocol.js +108 -2
  76. package/dist/strand-formation-protocol.js.map +1 -1
  77. package/dist/strand-instance-manager.d.ts +389 -19
  78. package/dist/strand-instance-manager.js +678 -65
  79. package/dist/strand-instance-manager.js.map +1 -1
  80. package/dist/strand-member-key.d.ts +20 -12
  81. package/dist/strand-member-key.js +20 -12
  82. package/dist/strand-member-key.js.map +1 -1
  83. package/dist/strand-member-registry.js +3 -0
  84. package/dist/strand-member-registry.js.map +1 -1
  85. package/dist/strand-membership-reconciler.d.ts +361 -0
  86. package/dist/strand-membership-reconciler.js +548 -0
  87. package/dist/strand-membership-reconciler.js.map +1 -0
  88. package/dist/strand-membership-writer.d.ts +143 -11
  89. package/dist/strand-membership-writer.js +419 -135
  90. package/dist/strand-membership-writer.js.map +1 -1
  91. package/dist/strand-network-config.d.ts +116 -0
  92. package/dist/strand-network-config.js +192 -0
  93. package/dist/strand-network-config.js.map +1 -0
  94. package/dist/strand-revocation-enforcer.d.ts +392 -0
  95. package/dist/strand-revocation-enforcer.js +533 -0
  96. package/dist/strand-revocation-enforcer.js.map +1 -0
  97. package/dist/strand-solicitation.d.ts +17 -1
  98. package/dist/strand-solicitation.js +13 -2
  99. package/dist/strand-solicitation.js.map +1 -1
  100. package/dist/strand-wake-protocol.d.ts +11 -7
  101. package/dist/strand-wake-protocol.js +20 -52
  102. package/dist/strand-wake-protocol.js.map +1 -1
  103. package/dist/strand-watcher.js +12 -1
  104. package/dist/strand-watcher.js.map +1 -1
  105. package/dist/timed-step.d.ts +12 -0
  106. package/dist/timed-step.js +28 -0
  107. package/dist/timed-step.js.map +1 -0
  108. package/dist/timeout-scheduler.d.ts +19 -0
  109. package/dist/timeout-scheduler.js +22 -0
  110. package/dist/timeout-scheduler.js.map +1 -0
  111. package/dist/types.d.ts +472 -46
  112. package/dist/types.js +7 -1
  113. package/dist/types.js.map +1 -1
  114. package/package.json +16 -8
  115. package/src/bootstrap-peer-store-file.ts +2 -2
  116. package/src/bootstrap-peer-store.ts +69 -27
  117. package/src/cadre-node.ts +1926 -228
  118. package/src/control-authorization.ts +2 -1
  119. package/src/control-cohort.ts +122 -130
  120. package/src/control-database.ts +586 -74
  121. package/src/control-read-retry.ts +202 -167
  122. package/src/control-retry.ts +297 -190
  123. package/src/control-schema.ts +133 -21
  124. package/src/control-write-retry.ts +525 -383
  125. package/src/enrolled-machine-store-file.ts +43 -0
  126. package/src/enrolled-machine-store.ts +280 -0
  127. package/src/file-durable-slot.ts +5 -3
  128. package/src/index.ts +134 -15
  129. package/src/membership-connection-gater.ts +9 -3
  130. package/src/node-local-snapshot.ts +38 -9
  131. package/src/peer-addr-book.ts +251 -250
  132. package/src/peer-dial.ts +215 -0
  133. package/src/{strand-backfill.ts → peer-join-backfill.ts} +478 -389
  134. package/src/relay-addrs.ts +458 -218
  135. package/src/relay-reservation.ts +951 -828
  136. package/src/seed-bootstrap.ts +76 -18
  137. package/src/storage-scope.ts +78 -0
  138. package/src/strand-database.ts +61 -21
  139. package/src/strand-first-sync-gate.ts +258 -0
  140. package/src/strand-formation-manager.ts +162 -8
  141. package/src/strand-formation-protocol.ts +850 -692
  142. package/src/strand-instance-manager.ts +877 -74
  143. package/src/strand-member-key.ts +20 -12
  144. package/src/strand-member-registry.ts +3 -0
  145. package/src/strand-membership-reconciler.ts +680 -0
  146. package/src/strand-membership-writer.ts +550 -190
  147. package/src/strand-network-config.ts +250 -0
  148. package/src/strand-revocation-enforcer.ts +653 -0
  149. package/src/strand-solicitation.ts +499 -471
  150. package/src/strand-wake-protocol.ts +21 -66
  151. package/src/strand-watcher.ts +325 -314
  152. package/src/timed-step.ts +28 -0
  153. package/src/timeout-scheduler.ts +28 -0
  154. package/src/types.ts +487 -52
  155. package/dist/strand-backfill.js.map +0 -1
@@ -1,36 +1,59 @@
1
1
  import debug from 'debug';
2
2
  import { BlockTransferClient } from '@optimystic/db-p2p';
3
- // NOTE: this copies the WHOLE local store to every newly connected strand peer, which is
4
- // right while a strand mesh is one party's handful of machines (see docs/architecture.md →
5
- // Replication cluster size, "On a strand, the machine count is not a party count"). If
6
- // strand meshes ever get large, filter the pushed set by FRET cohort responsibility for
7
- // each block id instead of pushing everything.
3
+ // Peer-join whole-store block catch-up, shared by the STRAND networks and the CONTROL
4
+ // network. On `connection:open` (debounced) it pushes every committed, materialized block
5
+ // in the local raw store to the new peer, so a machine that joined after blocks were
6
+ // committed still ends up physically holding them. Optimystic has no cohort-join catch-up
7
+ // of its own; a block committed while its writer was alone has exactly one holder forever
8
+ // without this (named collection-header blocks are written once, at collection creation,
9
+ // and their revision never moves again — so no later commit ever carries them anywhere).
10
+ //
11
+ // NOTE: this copies the WHOLE local store to every newly connected peer, which is right
12
+ // while a network is one party's handful of machines (see docs/architecture.md →
13
+ // Replication cluster size — the control network replicates to the whole party by intent,
14
+ // and "On a strand, the machine count is not a party count"). If these meshes ever get
15
+ // large, filter the pushed set by FRET cohort responsibility for each block id instead of
16
+ // pushing everything.
8
17
  //
9
18
  // NOTE: this lives in cadre-core only because `../optimystic` is read-only from this repo.
10
19
  // If Optimystic ever grows a cohort-join catch-up of its own, delete this module rather
11
20
  // than running two.
12
21
  //
13
- // Membership: anything connected on the strand's own libp2p network already receives
14
- // cohort replicas of new commits, so pushing older blocks to it exposes nothing new. No
15
- // membership gate here — adding one would diverge from what ordinary replication already
16
- // does on this network. A peer that does NOT speak this strand's block-transfer protocol
17
- // (a bare circuit relay or bootstrap node the strand node also connects to) cannot receive
18
- // anything: the protocol id is namespaced per strand, so the dial fails and the push is
19
- // dropped.
22
+ // Membership — the one place the two networks differ, expressed as the optional
23
+ // `authorizePeer` dep:
24
+ //
25
+ // - STRAND networks pass none. Anything connected on a strand's own libp2p network already
26
+ // receives cohort replicas of new commits, so pushing older blocks to it exposes nothing
27
+ // new; adding a gate would diverge from what ordinary replication already does there. A
28
+ // peer that does NOT speak the strand's block-transfer protocol (a bare circuit relay or
29
+ // bootstrap node the strand node also connects to) cannot receive anything: the protocol
30
+ // id is namespaced per strand, so the dial fails and the push is dropped.
31
+ // - The CONTROL network MUST pass one, because that argument does not carry over: its
32
+ // inbound connection gate deliberately admits non-members in several states (an
33
+ // un-enrolled node taking its seed, an open enrollment window, an outstanding
34
+ // invitation, configured bootstrap/relay peers — see docs/architecture.md, the
35
+ // control-network inbound connection gate). Pushing the whole control store to such a
36
+ // peer would hand a stranger the party's entire membership, addresses and strand list.
37
+ // `CadreNode` passes `isAuthorizedMember`; the gate is consulted at PUSH time, not at
38
+ // schedule time, and fails closed on a thrown check. A denied run is not memoized, so
39
+ // the peer is retried on its next `connection:open` — or sooner, via
40
+ // `scheduleConnectedPeers()` on a membership change (the production join order is
41
+ // connect-then-authorize, so the denial at dial time is the expected first pass).
20
42
  //
21
- // NOTE: that failing case costs one dial timeout per connection:open from such a peer, and
22
- // is never memoized (only clean runs are). Bounded today by the debounce plus the
23
- // unreachable-peer bail in `runCatchUp` — one dial per event, not one per chunk. If a strand
24
- // node ever holds many non-strand connections, or the enumeration ahead of the first chunk
25
- // gets expensive, pre-check `libp2p.peerStore` for this strand's block-transfer protocol
26
- // before enumerating.
43
+ // NOTE: a failing dial to a non-speaking peer costs one dial timeout per connection:open
44
+ // from such a peer, and is never memoized (only clean runs are). Bounded today by the
45
+ // debounce plus the unreachable-peer bail in `runCatchUp` — one dial per event, not one
46
+ // per chunk. If a node ever holds many such connections, or the enumeration ahead of the
47
+ // first chunk gets expensive, pre-check `libp2p.peerStore` for this network's
48
+ // block-transfer protocol before enumerating.
27
49
  //
28
- // NOTE: a run that fails is retried only when that peer next opens a connection. Over a
29
- // stable connection a transient push failure therefore leaves the peer partially copied
30
- // until the next reconnect (read repair still covers reads meanwhile). If strand meshes
31
- // ever hold long-lived connections where that matters, re-arm the debounce timer with a
32
- // backoff on a non-clean run instead of waiting for connection:open.
33
- const log = debug('sereus:cadre:strand-backfill');
50
+ // NOTE: a run that fails is retried only when that peer next opens a connection (or, with
51
+ // an `authorizePeer` gate wired, when `scheduleConnectedPeers()` is driven). Over a stable
52
+ // connection a transient push failure therefore leaves the peer partially copied until the
53
+ // next reconnect (read repair still covers reads meanwhile). If these meshes ever hold
54
+ // long-lived connections where that matters, re-arm the debounce timer with a backoff on a
55
+ // non-clean run instead of waiting for connection:open.
56
+ const log = debug('sereus:cadre:peer-join-backfill');
34
57
  /**
35
58
  * The block-transfer protocol's hard cap on one length-prefixed message. Mirrors
36
59
  * `MAX_BLOCK_MESSAGE_BYTES` in `@optimystic/db-p2p`'s `protocol-limits.ts` (8 MiB), which
@@ -38,8 +61,8 @@ const log = debug('sereus:cadre:strand-backfill');
38
61
  * changes it or starts exporting it.
39
62
  */
40
63
  export const MAX_BLOCK_MESSAGE_BYTES = 8 * 1024 * 1024;
41
- /** The resolved defaults every {@link StrandBackfill} starts from. */
42
- export const DEFAULT_STRAND_BACKFILL = {
64
+ /** The resolved defaults every {@link PeerJoinBackfill} starts from. */
65
+ export const DEFAULT_PEER_JOIN_BACKFILL = {
43
66
  enabled: true,
44
67
  debounceMs: 1000,
45
68
  maxBlocks: 10000,
@@ -49,7 +72,7 @@ export const DEFAULT_STRAND_BACKFILL = {
49
72
  responseTimeoutMs: 10000
50
73
  };
51
74
  function emptyResult() {
52
- return { offered: 0, accepted: 0, rejected: [], uncommitted: 0, unmaterialized: 0, capped: 0, oversized: [] };
75
+ return { offered: 0, accepted: 0, rejected: [], uncommitted: 0, unmaterialized: 0, capped: 0, oversized: [], denied: false };
53
76
  }
54
77
  /**
55
78
  * Base64 wire size of a raw buffer — `pushBlocks` base64-encodes each block into the JSON
@@ -59,17 +82,17 @@ function base64WireBytes(rawBytes) {
59
82
  return Math.ceil(rawBytes / 3) * 4;
60
83
  }
61
84
  /**
62
- * Copies every block in one strand's own raw store to each peer its libp2p node connects
63
- * to, so a peer that joined the strand after blocks were committed still ends up holding
85
+ * Copies every block in one network's own raw store to each peer its libp2p node connects
86
+ * to, so a peer that joined the network after blocks were committed still ends up holding
64
87
  * them physically (the receiver persists each push via `saveReplicatedBlock`, which is
65
88
  * monotonic and idempotent — crossing pushes from both ends cannot regress a revision).
66
89
  *
67
- * Best-effort throughout: nothing here throws into a libp2p event handler or into
68
- * `buildStrandRuntime`; a failed chunk is logged and the run continues. A peer is marked
90
+ * Best-effort throughout: nothing here throws into a libp2p event handler or into a
91
+ * runtime bring-up; a failed chunk is logged and the run continues. A peer is marked
69
92
  * fully caught up ONLY after a run with no thrown chunk and an empty `missing` list, so
70
93
  * the next `connection:open` from a peer whose catch-up failed retries it.
71
94
  */
72
- export class StrandBackfill {
95
+ export class PeerJoinBackfill {
73
96
  constructor(deps, config) {
74
97
  this.deps = deps;
75
98
  this.started = false;
@@ -78,10 +101,19 @@ export class StrandBackfill {
78
101
  this.done = new Set();
79
102
  /** Peers with a catch-up currently running (suppresses concurrent duplicates). */
80
103
  this.inFlight = new Set();
104
+ /**
105
+ * Peers whose (re-)schedule arrived while their OWN run was in flight, replayed once
106
+ * that run finishes. Load-bearing for the gated path: the gate's authorization check is
107
+ * a control-database read, so the window between it and the run's end is wide enough for
108
+ * the very membership commit that would have authorized the peer to land inside it —
109
+ * dropping that schedule (rather than deferring it) leaves the denied peer waiting for a
110
+ * reconnect, which is exactly what the re-arm exists to avoid.
111
+ */
112
+ this.rearmAfterFlight = new Set();
81
113
  /** Pending per-peer debounce timers, cleared on stop. */
82
114
  this.timers = new Map();
83
115
  this.loggedNoListBlockIds = false;
84
- this.config = { ...DEFAULT_STRAND_BACKFILL, ...config };
116
+ this.config = { ...DEFAULT_PEER_JOIN_BACKFILL, ...config };
85
117
  this.createPushClient = deps.createPushClient
86
118
  ?? ((peerId) => new BlockTransferClient(peerId, deps.peerNetwork, deps.protocolPrefix));
87
119
  this.onConnectionOpen = (evt) => this.schedulePeer(evt.detail.remotePeer);
@@ -94,11 +126,8 @@ export class StrandBackfill {
94
126
  this.deps.libp2p.addEventListener('connection:open', this.onConnectionOpen);
95
127
  // A runtime rebuilt over live connections (resumeStrand) never sees their
96
128
  // connection:open, so walk what is already connected once.
97
- const existing = this.deps.libp2p.getConnections();
98
- for (const connection of existing) {
99
- this.schedulePeer(connection.remotePeer);
100
- }
101
- log('[%s] started (%d peer(s) already connected)', this.deps.strandId, existing.length);
129
+ const scheduled = this.scheduleConnectedPeers();
130
+ log('[%s] started (%d peer(s) already connected)', this.deps.label, scheduled);
102
131
  }
103
132
  /** Unsubscribe, clear timers; in-flight runs observe the stopped flag and bail. */
104
133
  stop() {
@@ -112,13 +141,43 @@ export class StrandBackfill {
112
141
  clearTimeout(timer);
113
142
  }
114
143
  this.timers.clear();
115
- log('[%s] stopped', this.deps.strandId);
144
+ this.rearmAfterFlight.clear();
145
+ log('[%s] stopped', this.deps.label);
146
+ }
147
+ /**
148
+ * (Re-)schedule a debounced catch-up for every currently-connected peer not yet caught
149
+ * up. Idempotent and cheap (caught-up peers are skipped before any timer is set, and a
150
+ * peer whose run is in flight is deferred to the end of that run rather than dropped).
151
+ * Driven by {@link start}, and — on a gated network — by the embedder whenever
152
+ * membership changes, so a peer whose first pass was denied (connected before it was
153
+ * authorized: the production join order) is retried without waiting for a reconnect.
154
+ * Returns how many distinct peers were considered (for the start() log) — a peer holding
155
+ * several connections is one peer, and is scheduled once.
156
+ */
157
+ scheduleConnectedPeers() {
158
+ if (this.stopped)
159
+ return 0;
160
+ const peers = new Set();
161
+ for (const connection of this.deps.libp2p.getConnections()) {
162
+ const key = connection.remotePeer.toString();
163
+ if (peers.has(key))
164
+ continue;
165
+ peers.add(key);
166
+ this.schedulePeer(connection.remotePeer);
167
+ }
168
+ return peers.size;
116
169
  }
117
170
  /** Debounced entry point for connection churn: one run per peer per settle window. */
118
171
  schedulePeer(peerId) {
119
172
  const key = peerId.toString();
120
- if (this.stopped || this.done.has(key) || this.inFlight.has(key))
173
+ if (this.stopped || this.done.has(key))
174
+ return;
175
+ if (this.inFlight.has(key)) {
176
+ // Defer rather than drop: the running pass may already be past its gate check (or
177
+ // past the block this schedule was meant to carry), so replay it when that pass ends.
178
+ this.rearmAfterFlight.add(key);
121
179
  return;
180
+ }
122
181
  const existing = this.timers.get(key);
123
182
  if (existing)
124
183
  clearTimeout(existing);
@@ -143,32 +202,54 @@ export class StrandBackfill {
143
202
  // NOTE: a run that hit `maxBlocks` is still "clean" and still memoizes the peer, so
144
203
  // the tail past the ceiling never reaches it. Deliberate: enumeration is not
145
204
  // resumable, so not memoizing would re-push the same prefix on every reconnect
146
- // without ever advancing. Loud in the log below (capped > 0). If a strand's store can
205
+ // without ever advancing. Loud in the log below (capped > 0). If a store can
147
206
  // realistically exceed maxBlocks, the fix is a resumable cursor, not either policy.
148
207
  if (clean && !this.stopped) {
149
208
  this.done.add(key);
150
209
  }
151
- log('[%s] catch-up peer=%s offered=%d accepted=%d rejected=%d uncommitted=%d unmaterialized=%d capped=%d oversized=%d done=%s', this.deps.strandId, key, result.offered, result.accepted, result.rejected.length, result.uncommitted, result.unmaterialized, result.capped, result.oversized.length, clean);
210
+ log('[%s] catch-up peer=%s offered=%d accepted=%d rejected=%d uncommitted=%d unmaterialized=%d capped=%d oversized=%d denied=%s done=%s', this.deps.label, key, result.offered, result.accepted, result.rejected.length, result.uncommitted, result.unmaterialized, result.capped, result.oversized.length, result.denied, clean);
152
211
  return result;
153
212
  }
154
213
  catch (error) {
155
214
  // runCatchUp already contains a per-chunk catch; this guards the enumeration and
156
215
  // metadata reads too — a backfill fault must never surface through a libp2p event.
157
- log('[%s] catch-up peer=%s failed: %o', this.deps.strandId, key, error);
216
+ log('[%s] catch-up peer=%s failed: %o', this.deps.label, key, error);
158
217
  return emptyResult();
159
218
  }
160
219
  finally {
161
220
  this.inFlight.delete(key);
221
+ // Replay a schedule that arrived mid-run. `schedulePeer` re-checks `done`, so a run
222
+ // that finished clean re-arms nothing; a denied or failed one gets its retry.
223
+ if (this.rearmAfterFlight.delete(key)) {
224
+ this.schedulePeer(peerId);
225
+ }
162
226
  }
163
227
  }
164
228
  /** The actual copy. `clean` = every chunk pushed and the remote persisted every block. */
165
229
  async runCatchUp(peerId) {
166
230
  const result = emptyResult();
167
231
  const { storage } = this.deps;
232
+ // The membership gate, judged at push time (never at schedule time — authorization can
233
+ // change during the debounce window in either direction). Fails CLOSED on a throw: a
234
+ // gate that cannot answer must not leak the store. A denied run is not clean, so the
235
+ // peer is retried later rather than memoized as caught up.
236
+ if (this.deps.authorizePeer) {
237
+ let authorized = false;
238
+ try {
239
+ authorized = await this.deps.authorizePeer(peerId.toString());
240
+ }
241
+ catch (error) {
242
+ log('[%s] authorizePeer(%s) threw — treating as denied: %o', this.deps.label, peerId.toString(), error);
243
+ }
244
+ if (!authorized) {
245
+ result.denied = true;
246
+ return { result, clean: false };
247
+ }
248
+ }
168
249
  if (!storage.listBlockIds) {
169
250
  if (!this.loggedNoListBlockIds) {
170
251
  this.loggedNoListBlockIds = true;
171
- log('[%s] raw storage does not implement listBlockIds(); backfill is inert', this.deps.strandId);
252
+ log('[%s] raw storage does not implement listBlockIds(); backfill is inert', this.deps.label);
172
253
  }
173
254
  return { result, clean: false };
174
255
  }
@@ -207,12 +288,12 @@ export class StrandBackfill {
207
288
  }
208
289
  }
209
290
  if (response.missing.length > 0) {
210
- log('[%s] peer=%s rejected %d block(s): %o', this.deps.strandId, peerId.toString(), response.missing.length, response.missing);
291
+ log('[%s] peer=%s rejected %d block(s): %o', this.deps.label, peerId.toString(), response.missing.length, response.missing);
211
292
  }
212
293
  }
213
294
  catch (error) {
214
295
  chunkFailed = true;
215
- log('[%s] push to peer=%s failed for %d block(s): %s', this.deps.strandId, peerId.toString(), ids.length, error.message);
296
+ log('[%s] push to peer=%s failed for %d block(s): %s', this.deps.label, peerId.toString(), ids.length, error.message);
216
297
  }
217
298
  };
218
299
  for await (const blockId of storage.listBlockIds()) {
@@ -239,7 +320,7 @@ export class StrandBackfill {
239
320
  const block = await storage.getMaterializedBlock(blockId, latest.actionId);
240
321
  if (!block) {
241
322
  result.unmaterialized += 1;
242
- log('[%s] block=%s has latest rev %d but no materialized content; skipped', this.deps.strandId, blockId, latest.rev);
323
+ log('[%s] block=%s has latest rev %d but no materialized content; skipped', this.deps.label, blockId, latest.rev);
243
324
  continue;
244
325
  }
245
326
  const buffer = encoder.encode(JSON.stringify(block));
@@ -247,11 +328,11 @@ export class StrandBackfill {
247
328
  // this test ignores the request envelope (ids array, blockMeta, JSON punctuation).
248
329
  // A lone block within a few KiB of the cap therefore still ships and is rejected by
249
330
  // the receiver's length-prefix decoder — a permanent chunk failure for that peer. No
250
- // block in a strand comes close today; if one ever can, subtract a measured envelope
251
- // allowance here rather than raising the cap.
331
+ // block comes close today; if one ever can, subtract a measured envelope allowance
332
+ // here rather than raising the cap.
252
333
  if (base64WireBytes(buffer.length) >= MAX_BLOCK_MESSAGE_BYTES) {
253
334
  result.oversized.push(blockId);
254
- log('[%s] block=%s is %d bytes (%d on the wire) — exceeds the %d-byte protocol cap alone; skipped', this.deps.strandId, blockId, buffer.length, base64WireBytes(buffer.length), MAX_BLOCK_MESSAGE_BYTES);
335
+ log('[%s] block=%s is %d bytes (%d on the wire) — exceeds the %d-byte protocol cap alone; skipped', this.deps.label, blockId, buffer.length, base64WireBytes(buffer.length), MAX_BLOCK_MESSAGE_BYTES);
255
336
  continue;
256
337
  }
257
338
  // Flush before adding when this block would overflow either budget. A single block
@@ -279,10 +360,10 @@ export class StrandBackfill {
279
360
  await flush();
280
361
  }
281
362
  if (result.capped > 0) {
282
- log('[%s] peer=%s catch-up CAPPED at %d blocks; %d block id(s) not attempted', this.deps.strandId, peerId.toString(), this.config.maxBlocks, result.capped);
363
+ log('[%s] peer=%s catch-up CAPPED at %d blocks; %d block id(s) not attempted', this.deps.label, peerId.toString(), this.config.maxBlocks, result.capped);
283
364
  }
284
365
  const clean = !chunkFailed && result.rejected.length === 0;
285
366
  return { result, clean };
286
367
  }
287
368
  }
288
- //# sourceMappingURL=strand-backfill.js.map
369
+ //# sourceMappingURL=peer-join-backfill.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"peer-join-backfill.js","sourceRoot":"","sources":["../src/peer-join-backfill.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAG1B,OAAO,EAAE,mBAAmB,EAA2C,MAAM,oBAAoB,CAAC;AAElG,sFAAsF;AACtF,0FAA0F;AAC1F,qFAAqF;AACrF,0FAA0F;AAC1F,0FAA0F;AAC1F,yFA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AAG,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YACxB,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YAC3B,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,GAAG,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,CAAC;YACrE,wFAAwF;YACxF,yFAAyF;YACzF,gEAAgE;YAChE,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,aAAa,CAAC,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;YAC/D,IAAI,KAAK,EAAE,CAAC;gBACV,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,KAAK,CAAC;YAChC,CAAC;YACD,KAAK,CAAC,KAAK,IAAI,MAAM,CAAC,MAAM,CAAC;YAC7B,MAAM,CAAC,OAAO,IAAI,CAAC,CAAC;QACtB,CAAC;QAED,IAAI,CAAC,IAAI,CAAC,OAAO,IAAI,CAAC,eAAe,EAAE,EAAE,CAAC;YACxC,MAAM,KAAK,EAAE,CAAC;QAChB,CAAC;QAED,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACtB,GAAG,CAAC,yEAAyE,EAC3E,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC,QAAQ,EAAE,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;QAC9E,CAAC;QAED,MAAM,KAAK,GAAG,CAAC,WAAW,IAAI,MAAM,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC,CAAC;QAC3D,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;IAC3B,CAAC;CACF"}
@@ -3,34 +3,47 @@
3
3
  * setting — into the listen entry a node needs before it can hold a circuit-relay
4
4
  * slot, and validate the operator's entries while doing it.
5
5
  *
6
- * `@libp2p/circuit-relay-v2`'s listener branches on the SHAPE of the listen addr,
7
- * and the same `relayAddrs` list resolves to either shape depending on WHO is
8
- * listening ({@link RelayListenRoute}):
9
- *
10
- * | route | listen entry | libp2p behaviour |
11
- * | -------------- | ------------------------------------------- | --------------------------------------------------------- |
12
- * | `'configured'` | `<relayAddr>/p2p-circuit`, one per relay | dial that relay from inside `listen()` and reserve, or throw |
13
- * | `'search'` | a single bare `/p2p-circuit` | register a pending reservation; open no connection |
14
- *
15
- * The CONTROL node takes the `'search'` route, and `CadreNode.start()` drives the
16
- * reservation explicitly (`relay-reservation.ts`) once the control database is up.
17
- * That ordering is the point: a configured circuit listener dials the relay from
18
- * inside `libp2p.start()`, so the control database was being built while a sibling
19
- * relay was already in this node's cohort — and a sibling that has not yet
20
- * replicated this node's `CadrePeer` row correctly refuses its control-DB streams,
21
- * which killed `start()` outright. See `cadre-node.ts` → `start()`.
22
- *
23
- * STRAND nodes keep the `'configured'` route (`strand-instance-manager.ts`): they
24
- * inherit the control node's `NetworkConfig`, nothing drives an explicit
25
- * reservation for them, and their relay connection cannot disturb a control-DB
26
- * bring-up (a strand node's protocol ids are namespaced `/optimystic/strand-<id>/…`,
27
- * so the relay is never in its cohort).
28
- *
29
- * `network.relayAddrs` is FAIL-FAST for the operator on both routes, just from
30
- * different places: a malformed entry throws here at config resolution, and on the
31
- * control node a first reservation attempt that does not land throws out of
32
- * `start()` (`RelayReservationFailedError`). Naming a relay that is down still
33
- * means the node does not come up.
6
+ * `@libp2p/circuit-relay-v2`'s listener branches on the SHAPE of the listen addr:
7
+ *
8
+ * | listen entry | libp2p behaviour |
9
+ * | ----------------------------------- | --------------------------------------------------------------- |
10
+ * | `<relayAddr>/p2p-circuit` | CONFIGURED: dial that relay from inside `listen()` and reserve, or throw |
11
+ * | bare `/p2p-circuit` | SEARCH: register a pending reservation; open no connection |
12
+ *
13
+ * Every cadre-built node takes the SEARCH shape, and something explicit drives the
14
+ * reservation afterwards (`relay-reservation.ts`):
15
+ *
16
+ * - The CONTROL node resolves `relayAddrs` here to ONE bare entry (whichever relay
17
+ * answers first fills it) and `CadreNode.start()` drives the reservation once the
18
+ * control database is up. That ordering is the point: a configured circuit
19
+ * listener dials the relay from inside `libp2p.start()`, so the control database
20
+ * was being built while a sibling relay was already in this node's cohort — and a
21
+ * sibling that has not yet replicated this node's `CadrePeer` row correctly refuses
22
+ * its control-DB streams, which killed `start()` outright. See `cadre-node.ts` →
23
+ * `start()`.
24
+ * - Each STRAND node resolves the same `relayAddrs` to one bare entry PER relay and
25
+ * runs one reservation supervisor per relay (`strand-network-config.ts`,
26
+ * `strand-instance-manager.ts`).
27
+ *
28
+ * The CONFIGURED shape is deliberately not producible from a `NetworkConfig` any
29
+ * more: it loses its address and never recovers when the relay restarts, when either
30
+ * side hangs up, and — with no network event at all — on libp2p's own reservation
31
+ * refresh, which removes the reservation before re-creating it while a configured
32
+ * listener republishes only from inside its own `listen()`. A hand-written
33
+ * `<relay>/p2p-circuit` entry in `network.listenAddrs` is rejected on the control
34
+ * node ({@link resolveListenAddrs}) and folded into the supervised relay set on a
35
+ * strand node.
36
+ *
37
+ * `network.relayAddrs` is FAIL-FAST for the operator by default: a malformed entry
38
+ * throws here at config resolution regardless of posture, and on the control node a
39
+ * first reservation attempt that does not land throws out of `start()`
40
+ * (`RelayReservationFailedError`). Naming a relay that is down still means the control
41
+ * node does not come up. `network.requireRelay: false` softens the second half only —
42
+ * for a node that must still boot with no network (a phone, a browser tab) — logging
43
+ * the failed attempt instead of throwing and leaving the retry supervisor running in
44
+ * the background; the malformed-entry check is unaffected. A strand node is
45
+ * fail-SOFT regardless of `requireRelay` (its supervisor keeps trying while the
46
+ * strand serves).
34
47
  *
35
48
  * The bare `/p2p-circuit` search listener cannot open a connection on its own:
36
49
  * libp2p fills a pending reservation from `RelayDiscovery`, which nominates a peer
@@ -42,18 +55,25 @@
42
55
  import type { NetworkConfig } from './types.js';
43
56
  import type { RelayReservationState } from './relay-reservation.js';
44
57
  /**
45
- * libp2p's relay SEARCH listener: registers one pending reservation and dials
46
- * nothing. One entry covers every configured relay — the pending reservation is
47
- * per-listener, not per-relay, and `relay-reservation.ts` fills it with whichever
48
- * relay answers first.
58
+ * The direct listener a node keeps when it configures `relayAddrs` but no
59
+ * `listenAddrs`. Without this, naming a relay would silently REPLACE the node's
60
+ * direct TCP listener with a circuit-only one. Shared with the strand-node
61
+ * derivation (`strand-network-config.ts`), which applies the same rule.
62
+ *
63
+ * NOTE: mirrors `createLibp2pNode`'s own default listen addr in
64
+ * `../optimystic/packages/db-p2p/src/libp2p-node.ts` (`/ip4/0.0.0.0/tcp/<port>`,
65
+ * and `CadreNode` passes `port: 0`) — that file is the source of truth. If it
66
+ * ever changes its default, this constant has to follow.
49
67
  */
50
- export declare const RELAY_SEARCH_LISTEN_ADDR = "/p2p-circuit";
68
+ export declare const DEFAULT_DIRECT_LISTEN_ADDR = "/ip4/0.0.0.0/tcp/0";
51
69
  /**
52
- * Which listener shape `network.relayAddrs` resolves to for this caller — see the
53
- * table in the module doc. Defaults to `'configured'` everywhere except the
54
- * control node's own libp2p options.
70
+ * libp2p's relay SEARCH listener: registers one pending reservation and dials
71
+ * nothing. The pending reservation is per-LISTENER, not per-relay: the control
72
+ * node binds one entry for its whole relay list (`relay-reservation.ts` fills it
73
+ * with whichever relay answers first), a strand node binds one entry per relay so
74
+ * each relay's supervisor has a slot of its own to fill.
55
75
  */
56
- export type RelayListenRoute = 'configured' | 'search';
76
+ export declare const RELAY_SEARCH_LISTEN_ADDR = "/p2p-circuit";
57
77
  /**
58
78
  * The circuit-listen multiaddr for each configured relay: `<relayAddr>/p2p-circuit`.
59
79
  * An entry that already carries `/p2p-circuit` is passed through unchanged.
@@ -61,28 +81,113 @@ export type RelayListenRoute = 'configured' | 'search';
61
81
  * operator configuration, so a typo must fail loudly at start rather than
62
82
  * silently costing the node its reachability.
63
83
  *
84
+ * No node LISTENS on these any more (see the module doc); they are the validated
85
+ * form of `relayAddrs`, read by `CadreNode.circuitRelayTargets` for the delegate
86
+ * announce and by `strand-network-config.ts` for the per-relay supervisor list.
87
+ *
64
88
  * Deliberately unlike `extractCircuitRelayTargets` (`delegate-admission.ts`), which
65
89
  * SKIPS a bad entry: that one reads addresses discovered at runtime from peers, where
66
90
  * one bad entry must not be fatal.
67
91
  */
68
92
  export declare function relayCircuitAddrs(relayAddrs: readonly string[]): string[];
69
93
  /**
70
- * The listen multiaddrs a node actually binds: configured `listenAddrs` plus the
71
- * relay listen entry `route` calls for, deduplicated, order-stable (configured
72
- * entries first). Returns `undefined` when neither field is set, so callers keep
73
- * omitting `listenAddrs` and inherit db-p2p's default.
74
- *
75
- * Every `relayAddrs` entry is validated on BOTH routes — the `'search'` route
76
- * discards the resolved circuit addrs, but an operator typo must fail at config
77
- * resolution either way.
94
+ * The listen multiaddrs the CONTROL node binds: configured `listenAddrs` plus ONE
95
+ * bare `/p2p-circuit` search entry when `relayAddrs` names any relay, deduplicated,
96
+ * order-stable (configured entries first). Returns `undefined` when neither field
97
+ * is set, so callers keep omitting `listenAddrs` and inherit db-p2p's default.
98
+ *
99
+ * Every `relayAddrs` entry is validated even though the search entry discards the
100
+ * resolved circuit addrs — an operator typo must fail at config resolution. A
101
+ * hand-written `<relay>/p2p-circuit` entry in `listenAddrs` is rejected outright
102
+ * (see {@link rejectConfiguredCircuitListenAddrs}).
103
+ *
104
+ * Strand nodes derive their own listen set from the same config in
105
+ * `strand-network-config.ts` (`strandNodeAddrs`) — one search entry per relay,
106
+ * fixed direct ports made ephemeral.
78
107
  */
79
- export declare function resolveListenAddrs(network: NetworkConfig | undefined, route?: RelayListenRoute): string[] | undefined;
108
+ export declare function resolveListenAddrs(network: NetworkConfig | undefined): string[] | undefined;
109
+ /**
110
+ * The transport-derived `createLibp2pNode` options a resolved listen set implies —
111
+ * see {@link resolveTransportOptions}.
112
+ */
113
+ export interface ListenTransportOptions {
114
+ /**
115
+ * Present when some listen entry names WebSocket. Its VALUE is never bound; it is
116
+ * the switch that makes `@optimystic/db-p2p` add `webSockets()` — see
117
+ * {@link WS_TRANSPORT_SWITCH_PORT}.
118
+ */
119
+ wsPort?: number;
120
+ }
121
+ /**
122
+ * The transport-derived libp2p options `listenAddrs` implies, and the gate that stops a
123
+ * listen address the node has no transport for from being SILENTLY dropped.
124
+ *
125
+ * libp2p's transport manager sorts configured listen addresses by which transport claims
126
+ * them, discards the unclaimed ones, and raises `UnsupportedListenAddressesError` only
127
+ * when EVERY address was discarded. Pair a TCP address with a WebSocket one — which is
128
+ * exactly what the shipped configs do — and the TCP address carries the start while the
129
+ * missing WebSocket listener is never reported. Two halves of `NetworkConfig` that have
130
+ * to agree (`listenAddrs`, and the transports the node will actually have) were never
131
+ * checked against each other; this is that check, and it covers both node kinds because
132
+ * both derive their listen set from the same config here ({@link resolveListenAddrs}) or
133
+ * in `strand-network-config.ts`, and both call this on the result.
134
+ *
135
+ * The two transport classes are handled differently, on purpose:
136
+ *
137
+ * - **WebSocket is DERIVED.** It is the one non-TCP transport the shipped configs need
138
+ * (the React Native reference app's companion drone; `cadre start --ws-port`), and
139
+ * `@optimystic/db-p2p` already knows how to add it. `cadre-core` grows no dependency
140
+ * and decides no transport policy — it flips db-p2p's own switch
141
+ * ({@link WS_TRANSPORT_SWITCH_PORT}).
142
+ * - **Everything else is REFUSED.** Deriving `/quic-v1`, `/webrtc` or `/webtransport`
143
+ * would mean importing transport packages into every `cadre-core` consumer including
144
+ * the React Native and browser bundles (against the cross-platform rule in
145
+ * `AGENTS.md`) and duplicating policy `libp2p-node.ts` owns. So name the address, name
146
+ * the transport it needs, and refuse to start — matching how `network.relayAddrs` and
147
+ * `network.announceAddrs` already treat an operator typo.
148
+ *
149
+ * Returns `{}` unconditionally when `network.transports` is set: a programmatic embedder
150
+ * supplying transport factories owns the policy, and the factories are opaque — nothing
151
+ * can be inferred from them. That is what keeps the RN phone, the web app, and the
152
+ * integration-test harness unaffected.
153
+ *
154
+ * NOTE: pairing this with {@link resolveListenAddrs} is a CONVENTION, not a structure —
155
+ * a third libp2p-node build site could resolve listen addrs and forget to call this,
156
+ * putting the original bug back on that path. Both existing sites are covered
157
+ * (`cadre-node.ts` → `buildControlNodeOptions`, `strand-network-config.ts` →
158
+ * `strandNodeAddrs`), and they are the only two `createLibp2pNode` callers in this repo
159
+ * that derive their listen set from a `NetworkConfig` — the other callers
160
+ * (`integration-tests/src/harness/test-party.ts`, `quereus-plugin-sereus`'s `connect.ts`
161
+ * and `connect-browser.ts`) pass no `listenAddrs` at all, or pass their own transports
162
+ * with it. If a third CONFIG-derived site appears, fold the two functions into one that
163
+ * returns listen addrs and transport options together, so forgetting becomes impossible
164
+ * rather than merely unlikely.
165
+ *
166
+ * @param listenAddrs the ALREADY-resolved listen set — the output of
167
+ * {@link resolveListenAddrs}, or the strand derivation, after any per-node rewriting —
168
+ * so the check reads what this node will actually bind.
169
+ * @throws {UnbindableListenAddressError} when a listen entry names a transport outside
170
+ * {tcp, ws/wss, p2p-circuit}.
171
+ */
172
+ export declare function resolveTransportOptions(network: NetworkConfig | undefined, listenAddrs: readonly string[] | undefined): ListenTransportOptions;
173
+ /**
174
+ * Thrown at config resolution when `network.listenAddrs` names an address none of the
175
+ * default transports can bind. Names each offending address alongside the libp2p
176
+ * transport package it would need — what the operator has without this is a node that
177
+ * starts and silently never listens there.
178
+ */
179
+ export declare class UnbindableListenAddressError extends Error {
180
+ readonly listenAddrs: readonly string[];
181
+ constructor(listenAddrs: readonly string[]);
182
+ }
80
183
  /**
81
184
  * Thrown out of `CadreNode.start()` when the boot-path reservation drive for
82
- * `network.relayAddrs` produces no `/p2p-circuit` address on its FIRST attempt.
185
+ * `network.relayAddrs` produces no `/p2p-circuit` address on its FIRST attempt,
186
+ * UNLESS `network.requireRelay` is explicitly `false` (see `driveControlRelayReservation`,
187
+ * which logs and returns instead on that posture).
83
188
  *
84
- * This is what keeps `network.relayAddrs` fail-fast now that the control node
85
- * takes the `'search'` route: libp2p's own `UnsupportedListenAddressesError` used
189
+ * This is what keeps `network.relayAddrs` fail-fast by default now that the control node
190
+ * listens on the bare search entry: libp2p's own `UnsupportedListenAddressesError` used
86
191
  * to abort start from inside `listen()`, and an operator who names a relay is
87
192
  * telling the node it has no other reachability — coming up undialable is worse
88
193
  * than not coming up. Names the relays and the reservation's own reason, both of
@@ -93,3 +198,9 @@ export declare class RelayReservationFailedError extends Error {
93
198
  readonly state: RelayReservationState;
94
199
  constructor(relayAddrs: readonly string[], state: RelayReservationState);
95
200
  }
201
+ /**
202
+ * `<something>/p2p-circuit` — the configured shape, as opposed to the bare search
203
+ * addr. An unparsable entry is `false`: libp2p reports a bad listen addr itself,
204
+ * and this helper only ever ADDS a denial (or, on the strand path, a supervisor).
205
+ */
206
+ export declare function isConfiguredCircuitListenAddr(listenAddr: string): boolean;