@serfab/cadre-core 0.11.0 → 0.13.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 (121) hide show
  1. package/README.md +3 -2
  2. package/dist/announce-addrs.d.ts +49 -0
  3. package/dist/announce-addrs.js +78 -0
  4. package/dist/announce-addrs.js.map +1 -0
  5. package/dist/cadre-node.d.ts +607 -53
  6. package/dist/cadre-node.js +1268 -157
  7. package/dist/cadre-node.js.map +1 -1
  8. package/dist/control-database.d.ts +174 -23
  9. package/dist/control-database.js +317 -107
  10. package/dist/control-database.js.map +1 -1
  11. package/dist/control-read-retry.d.ts +103 -0
  12. package/dist/control-read-retry.js +145 -0
  13. package/dist/control-read-retry.js.map +1 -0
  14. package/dist/control-retry.d.ts +101 -0
  15. package/dist/control-retry.js +108 -0
  16. package/dist/control-retry.js.map +1 -0
  17. package/dist/control-schema.d.ts +1 -1
  18. package/dist/control-schema.js +50 -6
  19. package/dist/control-schema.js.map +1 -1
  20. package/dist/control-write-retry.d.ts +59 -35
  21. package/dist/control-write-retry.js +21 -92
  22. package/dist/control-write-retry.js.map +1 -1
  23. package/dist/delegate-admission.d.ts +17 -15
  24. package/dist/delegate-admission.js +17 -15
  25. package/dist/delegate-admission.js.map +1 -1
  26. package/dist/enrolled-machine-store-file.d.ts +13 -0
  27. package/dist/enrolled-machine-store-file.js +41 -0
  28. package/dist/enrolled-machine-store-file.js.map +1 -0
  29. package/dist/enrolled-machine-store.d.ts +81 -0
  30. package/dist/enrolled-machine-store.js +225 -0
  31. package/dist/enrolled-machine-store.js.map +1 -0
  32. package/dist/file-durable-slot.js +5 -3
  33. package/dist/file-durable-slot.js.map +1 -1
  34. package/dist/index.d.ts +10 -6
  35. package/dist/index.js +39 -12
  36. package/dist/index.js.map +1 -1
  37. package/dist/membership-connection-gater.d.ts +226 -26
  38. package/dist/membership-connection-gater.js +316 -30
  39. package/dist/membership-connection-gater.js.map +1 -1
  40. package/dist/node-local-snapshot.d.ts +6 -0
  41. package/dist/node-local-snapshot.js +7 -1
  42. package/dist/node-local-snapshot.js.map +1 -1
  43. package/dist/{strand-backfill.d.ts → peer-join-backfill.d.ts} +49 -20
  44. package/dist/{strand-backfill.js → peer-join-backfill.js} +146 -56
  45. package/dist/peer-join-backfill.js.map +1 -0
  46. package/dist/peer-record.d.ts +26 -0
  47. package/dist/peer-record.js +32 -0
  48. package/dist/peer-record.js.map +1 -1
  49. package/dist/relay-addrs.d.ts +145 -22
  50. package/dist/relay-addrs.js +314 -31
  51. package/dist/relay-addrs.js.map +1 -1
  52. package/dist/relay-reservation.d.ts +20 -15
  53. package/dist/relay-reservation.js +18 -14
  54. package/dist/relay-reservation.js.map +1 -1
  55. package/dist/strand-addr-protocol.js +16 -0
  56. package/dist/strand-addr-protocol.js.map +1 -1
  57. package/dist/strand-database.d.ts +12 -0
  58. package/dist/strand-database.js +16 -0
  59. package/dist/strand-database.js.map +1 -1
  60. package/dist/strand-formation-manager.d.ts +22 -9
  61. package/dist/strand-formation-manager.js +20 -12
  62. package/dist/strand-formation-manager.js.map +1 -1
  63. package/dist/strand-formation-protocol.d.ts +83 -9
  64. package/dist/strand-formation-protocol.js +86 -10
  65. package/dist/strand-formation-protocol.js.map +1 -1
  66. package/dist/strand-instance-manager.d.ts +151 -17
  67. package/dist/strand-instance-manager.js +237 -32
  68. package/dist/strand-instance-manager.js.map +1 -1
  69. package/dist/strand-membership-writer.d.ts +118 -14
  70. package/dist/strand-membership-writer.js +203 -15
  71. package/dist/strand-membership-writer.js.map +1 -1
  72. package/dist/strand-network-config.d.ts +84 -0
  73. package/dist/strand-network-config.js +136 -0
  74. package/dist/strand-network-config.js.map +1 -0
  75. package/dist/strand-revocation-enforcer.d.ts +392 -0
  76. package/dist/strand-revocation-enforcer.js +533 -0
  77. package/dist/strand-revocation-enforcer.js.map +1 -0
  78. package/dist/strand-solicitation.d.ts +10 -3
  79. package/dist/strand-solicitation.js +12 -7
  80. package/dist/strand-solicitation.js.map +1 -1
  81. package/dist/strand-wake-protocol.d.ts +47 -2
  82. package/dist/strand-wake-protocol.js +76 -6
  83. package/dist/strand-wake-protocol.js.map +1 -1
  84. package/dist/strand-watcher.js +6 -1
  85. package/dist/strand-watcher.js.map +1 -1
  86. package/dist/types.d.ts +282 -43
  87. package/dist/types.js +7 -1
  88. package/dist/types.js.map +1 -1
  89. package/package.json +16 -8
  90. package/src/announce-addrs.ts +93 -0
  91. package/src/cadre-node.ts +1366 -171
  92. package/src/control-database.ts +2628 -2406
  93. package/src/control-read-retry.ts +167 -0
  94. package/src/control-retry.ts +190 -0
  95. package/src/control-schema.ts +50 -6
  96. package/src/control-write-retry.ts +30 -130
  97. package/src/delegate-admission.ts +17 -15
  98. package/src/enrolled-machine-store-file.ts +43 -0
  99. package/src/enrolled-machine-store.ts +280 -0
  100. package/src/file-durable-slot.ts +5 -3
  101. package/src/index.ts +477 -407
  102. package/src/membership-connection-gater.ts +539 -204
  103. package/src/node-local-snapshot.ts +13 -1
  104. package/src/peer-addr-book.ts +251 -250
  105. package/src/{strand-backfill.ts → peer-join-backfill.ts} +187 -75
  106. package/src/peer-record.ts +33 -0
  107. package/src/relay-addrs.ts +436 -111
  108. package/src/relay-reservation.ts +828 -823
  109. package/src/strand-addr-protocol.ts +17 -0
  110. package/src/strand-database.ts +17 -0
  111. package/src/strand-formation-manager.ts +511 -491
  112. package/src/strand-formation-protocol.ts +808 -692
  113. package/src/strand-instance-manager.ts +899 -611
  114. package/src/strand-membership-writer.ts +232 -15
  115. package/src/strand-network-config.ts +172 -0
  116. package/src/strand-revocation-enforcer.ts +653 -0
  117. package/src/strand-solicitation.ts +484 -472
  118. package/src/strand-wake-protocol.ts +94 -8
  119. package/src/strand-watcher.ts +319 -314
  120. package/src/types.ts +1554 -1309
  121. 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,38 +202,60 @@ 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
  }
175
256
  const client = this.createPushClient(peerId);
176
257
  const encoder = new TextEncoder();
177
- let chunk = { ids: [], buffers: [], meta: {}, bytes: 0 };
258
+ let chunk = { ids: [], buffers: [], meta: {}, proofs: {}, bytes: 0 };
178
259
  let chunkFailed = false;
179
260
  let anyChunkDelivered = false;
180
261
  /**
@@ -187,10 +268,12 @@ export class StrandBackfill {
187
268
  const flush = async () => {
188
269
  if (chunk.ids.length === 0)
189
270
  return;
190
- const { ids, buffers, meta } = chunk;
191
- chunk = { ids: [], buffers: [], meta: {}, bytes: 0 };
271
+ const { ids, buffers, meta, proofs } = chunk;
272
+ chunk = { ids: [], buffers: [], meta: {}, proofs: {}, bytes: 0 };
192
273
  try {
193
- const response = await client.pushBlocks(ids, buffers, 'replication', meta, {
274
+ // ONE certification value rather than two arguments, so a caller cannot pair a proof
275
+ // for one revision with meta for another — both were written from the same `latest`.
276
+ const response = await client.pushBlocks(ids, buffers, 'replication', { blockMeta: meta, blockProofs: proofs }, {
194
277
  dialTimeoutMs: this.config.dialTimeoutMs,
195
278
  responseTimeoutMs: this.config.responseTimeoutMs
196
279
  });
@@ -205,12 +288,12 @@ export class StrandBackfill {
205
288
  }
206
289
  }
207
290
  if (response.missing.length > 0) {
208
- 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);
209
292
  }
210
293
  }
211
294
  catch (error) {
212
295
  chunkFailed = true;
213
- 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);
214
297
  }
215
298
  };
216
299
  for await (const blockId of storage.listBlockIds()) {
@@ -237,7 +320,7 @@ export class StrandBackfill {
237
320
  const block = await storage.getMaterializedBlock(blockId, latest.actionId);
238
321
  if (!block) {
239
322
  result.unmaterialized += 1;
240
- 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);
241
324
  continue;
242
325
  }
243
326
  const buffer = encoder.encode(JSON.stringify(block));
@@ -245,11 +328,11 @@ export class StrandBackfill {
245
328
  // this test ignores the request envelope (ids array, blockMeta, JSON punctuation).
246
329
  // A lone block within a few KiB of the cap therefore still ships and is rejected by
247
330
  // the receiver's length-prefix decoder — a permanent chunk failure for that peer. No
248
- // block in a strand comes close today; if one ever can, subtract a measured envelope
249
- // 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.
250
333
  if (base64WireBytes(buffer.length) >= MAX_BLOCK_MESSAGE_BYTES) {
251
334
  result.oversized.push(blockId);
252
- 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);
253
336
  continue;
254
337
  }
255
338
  // Flush before adding when this block would overflow either budget. A single block
@@ -263,6 +346,13 @@ export class StrandBackfill {
263
346
  chunk.ids.push(blockId);
264
347
  chunk.buffers.push(buffer);
265
348
  chunk.meta[blockId] = { rev: latest.rev, actionId: latest.actionId };
349
+ // Read the proof for EXACTLY the revision being pushed, from the same `latest` the meta
350
+ // above was written from. Absent is normal (nothing retained a proof for that revision);
351
+ // the block still ships with its meta and the receiver decides.
352
+ const proof = await storage.getBlockProof(blockId, latest.rev);
353
+ if (proof) {
354
+ chunk.proofs[blockId] = proof;
355
+ }
266
356
  chunk.bytes += buffer.length;
267
357
  result.offered += 1;
268
358
  }
@@ -270,10 +360,10 @@ export class StrandBackfill {
270
360
  await flush();
271
361
  }
272
362
  if (result.capped > 0) {
273
- 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);
274
364
  }
275
365
  const clean = !chunkFailed && result.rejected.length === 0;
276
366
  return { result, clean };
277
367
  }
278
368
  }
279
- //# 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,qFAA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oBAAE,MAAM;YAC/C,CAAC;YACD,KAAK,CAAC,GAAG,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"}
@@ -84,6 +84,32 @@ export declare function isSignalingAddr(addr: string): boolean;
84
84
  * cannot express that distinction.
85
85
  */
86
86
  export declare function trailingPeerId(addr: Multiaddr): string | null;
87
+ /**
88
+ * `addr` guaranteed to terminate in `/p2p/<peerId>` — the shape every dial path
89
+ * needs — or `null` when it cannot, because the address already terminates in a
90
+ * DIFFERENT peer id and therefore does not reach `peerId` at all.
91
+ *
92
+ * Three input shapes, matching what a `CadrePeer` record can legitimately carry:
93
+ *
94
+ * - no `/p2p/` component (`/ip4/1.2.3.4/tcp/4001`) — the suffix is appended;
95
+ * - a relay hop with the destination missing (`…/p2p/<relay>/p2p-circuit`) — the
96
+ * last component is the circuit, not a peer id, so the suffix is appended and
97
+ * the address becomes the full `…/p2p/<relay>/p2p-circuit/p2p/<peerId>`;
98
+ * - already terminating in `/p2p/<X>` — returned untouched when `X` is `peerId`,
99
+ * dropped otherwise.
100
+ *
101
+ * This is deliberately the same rule libp2p applies internally in
102
+ * `calculateMultiaddrs` (append when the LAST component is not `p2p`) followed by
103
+ * its wrong-peer-id filter, so a normalized list matches what libp2p would have
104
+ * built anyway. Normalizing before the dial is what keeps a list from mixing
105
+ * suffixed and unsuffixed entries, which `getPeerAddress` rejects outright
106
+ * ("Multiaddrs must all have the same peer id or have no peer id") — taking the
107
+ * whole peer down with it, not just the odd address.
108
+ *
109
+ * Never use before signature verification: verification runs against the
110
+ * original on-record strings.
111
+ */
112
+ export declare function withTrailingPeerId(addr: Multiaddr, peerId: string): Multiaddr | null;
87
113
  /**
88
114
  * Return `addrs` with signaling (`/p2p-circuit`) addrs first, otherwise stable.
89
115
  * Used to present the WebRTC dial input ahead of direct addrs. Does NOT mutate
@@ -104,6 +104,38 @@ export function trailingPeerId(addr) {
104
104
  const p2p = addr.getComponents().filter((c) => c.name === 'p2p');
105
105
  return p2p.length > 0 ? (p2p[p2p.length - 1].value ?? null) : null;
106
106
  }
107
+ /**
108
+ * `addr` guaranteed to terminate in `/p2p/<peerId>` — the shape every dial path
109
+ * needs — or `null` when it cannot, because the address already terminates in a
110
+ * DIFFERENT peer id and therefore does not reach `peerId` at all.
111
+ *
112
+ * Three input shapes, matching what a `CadrePeer` record can legitimately carry:
113
+ *
114
+ * - no `/p2p/` component (`/ip4/1.2.3.4/tcp/4001`) — the suffix is appended;
115
+ * - a relay hop with the destination missing (`…/p2p/<relay>/p2p-circuit`) — the
116
+ * last component is the circuit, not a peer id, so the suffix is appended and
117
+ * the address becomes the full `…/p2p/<relay>/p2p-circuit/p2p/<peerId>`;
118
+ * - already terminating in `/p2p/<X>` — returned untouched when `X` is `peerId`,
119
+ * dropped otherwise.
120
+ *
121
+ * This is deliberately the same rule libp2p applies internally in
122
+ * `calculateMultiaddrs` (append when the LAST component is not `p2p`) followed by
123
+ * its wrong-peer-id filter, so a normalized list matches what libp2p would have
124
+ * built anyway. Normalizing before the dial is what keeps a list from mixing
125
+ * suffixed and unsuffixed entries, which `getPeerAddress` rejects outright
126
+ * ("Multiaddrs must all have the same peer id or have no peer id") — taking the
127
+ * whole peer down with it, not just the odd address.
128
+ *
129
+ * Never use before signature verification: verification runs against the
130
+ * original on-record strings.
131
+ */
132
+ export function withTrailingPeerId(addr, peerId) {
133
+ const components = addr.getComponents();
134
+ if (components[components.length - 1]?.name !== 'p2p') {
135
+ return addr.encapsulate(`/p2p/${peerId}`);
136
+ }
137
+ return trailingPeerId(addr) === peerId ? addr : null;
138
+ }
107
139
  /**
108
140
  * Return `addrs` with signaling (`/p2p-circuit`) addrs first, otherwise stable.
109
141
  * Used to present the WebRTC dial input ahead of direct addrs. Does NOT mutate
@@ -1 +1 @@
1
- {"version":3,"file":"peer-record.js","sourceRoot":"","sources":["../src/peer-record.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAGH,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAC;AACzE,OAAO,EAAE,0BAA0B,EAAE,MAAM,4BAA4B,CAAC;AAGxE;;;;;GAKG;AACH,MAAM,CAAC,MAAM,8BAA8B,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAE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