@serfab/cadre-core 0.12.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 (86) hide show
  1. package/README.md +3 -2
  2. package/dist/cadre-node.d.ts +328 -25
  3. package/dist/cadre-node.js +760 -59
  4. package/dist/cadre-node.js.map +1 -1
  5. package/dist/control-database.d.ts +19 -0
  6. package/dist/control-database.js +51 -9
  7. package/dist/control-database.js.map +1 -1
  8. package/dist/control-schema.d.ts +1 -1
  9. package/dist/control-schema.js +30 -2
  10. package/dist/control-schema.js.map +1 -1
  11. package/dist/enrolled-machine-store-file.d.ts +13 -0
  12. package/dist/enrolled-machine-store-file.js +41 -0
  13. package/dist/enrolled-machine-store-file.js.map +1 -0
  14. package/dist/enrolled-machine-store.d.ts +81 -0
  15. package/dist/enrolled-machine-store.js +225 -0
  16. package/dist/enrolled-machine-store.js.map +1 -0
  17. package/dist/file-durable-slot.js +5 -3
  18. package/dist/file-durable-slot.js.map +1 -1
  19. package/dist/index.d.ts +5 -3
  20. package/dist/index.js +23 -5
  21. package/dist/index.js.map +1 -1
  22. package/dist/membership-connection-gater.d.ts +15 -1
  23. package/dist/membership-connection-gater.js +9 -3
  24. package/dist/membership-connection-gater.js.map +1 -1
  25. package/dist/node-local-snapshot.d.ts +6 -0
  26. package/dist/node-local-snapshot.js +7 -1
  27. package/dist/node-local-snapshot.js.map +1 -1
  28. package/dist/{strand-backfill.d.ts → peer-join-backfill.d.ts} +49 -20
  29. package/dist/{strand-backfill.js → peer-join-backfill.js} +133 -52
  30. package/dist/peer-join-backfill.js.map +1 -0
  31. package/dist/relay-addrs.d.ts +73 -0
  32. package/dist/relay-addrs.js +195 -0
  33. package/dist/relay-addrs.js.map +1 -1
  34. package/dist/strand-database.d.ts +12 -0
  35. package/dist/strand-database.js +16 -0
  36. package/dist/strand-database.js.map +1 -1
  37. package/dist/strand-formation-manager.d.ts +12 -0
  38. package/dist/strand-formation-manager.js +10 -3
  39. package/dist/strand-formation-manager.js.map +1 -1
  40. package/dist/strand-formation-protocol.d.ts +76 -2
  41. package/dist/strand-formation-protocol.js +78 -2
  42. package/dist/strand-formation-protocol.js.map +1 -1
  43. package/dist/strand-instance-manager.d.ts +147 -14
  44. package/dist/strand-instance-manager.js +225 -45
  45. package/dist/strand-instance-manager.js.map +1 -1
  46. package/dist/strand-membership-writer.d.ts +12 -0
  47. package/dist/strand-membership-writer.js +23 -1
  48. package/dist/strand-membership-writer.js.map +1 -1
  49. package/dist/strand-network-config.d.ts +84 -0
  50. package/dist/strand-network-config.js +136 -0
  51. package/dist/strand-network-config.js.map +1 -0
  52. package/dist/strand-revocation-enforcer.d.ts +392 -0
  53. package/dist/strand-revocation-enforcer.js +533 -0
  54. package/dist/strand-revocation-enforcer.js.map +1 -0
  55. package/dist/strand-solicitation.d.ts +7 -0
  56. package/dist/strand-solicitation.js +8 -2
  57. package/dist/strand-solicitation.js.map +1 -1
  58. package/dist/strand-watcher.js +6 -1
  59. package/dist/strand-watcher.js.map +1 -1
  60. package/dist/types.d.ts +210 -16
  61. package/dist/types.js +7 -1
  62. package/dist/types.js.map +1 -1
  63. package/package.json +16 -8
  64. package/src/cadre-node.ts +6353 -5603
  65. package/src/control-database.ts +53 -9
  66. package/src/control-schema.ts +30 -2
  67. package/src/enrolled-machine-store-file.ts +43 -0
  68. package/src/enrolled-machine-store.ts +280 -0
  69. package/src/file-durable-slot.ts +5 -3
  70. package/src/index.ts +52 -10
  71. package/src/membership-connection-gater.ts +9 -3
  72. package/src/node-local-snapshot.ts +13 -1
  73. package/src/peer-addr-book.ts +251 -250
  74. package/src/{strand-backfill.ts → peer-join-backfill.ts} +478 -389
  75. package/src/relay-addrs.ts +218 -0
  76. package/src/strand-database.ts +17 -0
  77. package/src/strand-formation-manager.ts +22 -3
  78. package/src/strand-formation-protocol.ts +808 -692
  79. package/src/strand-instance-manager.ts +317 -54
  80. package/src/strand-membership-writer.ts +23 -1
  81. package/src/strand-network-config.ts +172 -0
  82. package/src/strand-revocation-enforcer.ts +653 -0
  83. package/src/strand-solicitation.ts +484 -471
  84. package/src/strand-watcher.ts +319 -314
  85. package/src/types.ts +1554 -1354
  86. package/dist/strand-backfill.js.map +0 -1
@@ -8,8 +8,8 @@ import { BlockTransferClient, type IRawStorage } from '@optimystic/db-p2p';
8
8
  * changes it or starts exporting it.
9
9
  */
10
10
  export declare const MAX_BLOCK_MESSAGE_BYTES: number;
11
- /** Tuning for the per-peer strand catch-up. Every field optional; defaults in {@link DEFAULT_STRAND_BACKFILL}. */
12
- export interface StrandBackfillConfig {
11
+ /** Tuning for the per-peer catch-up. Every field optional; defaults in {@link DEFAULT_PEER_JOIN_BACKFILL}. */
12
+ export interface PeerJoinBackfillConfig {
13
13
  /** Default true. False disables the catch-up entirely (the pre-existing behaviour). */
14
14
  enabled?: boolean;
15
15
  /** Settle time after a connection opens before catching that peer up, ms. Default 1000. */
@@ -25,32 +25,39 @@ export interface StrandBackfillConfig {
25
25
  /** Per-push response deadline, ms. Default 10_000 (matches SpreadOnChurnMonitor). */
26
26
  responseTimeoutMs?: number;
27
27
  }
28
- /** The resolved defaults every {@link StrandBackfill} starts from. */
29
- export declare const DEFAULT_STRAND_BACKFILL: Required<StrandBackfillConfig>;
28
+ /** The resolved defaults every {@link PeerJoinBackfill} starts from. */
29
+ export declare const DEFAULT_PEER_JOIN_BACKFILL: Required<PeerJoinBackfillConfig>;
30
30
  /**
31
31
  * The one capability the catch-up needs from a transfer client. Structural (rather than
32
32
  * the concrete `BlockTransferClient`) so unit tests can capture pushes without dialing
33
- * libp2p — see {@link StrandBackfillDeps.createPushClient}.
33
+ * libp2p — see {@link PeerJoinBackfillDeps.createPushClient}.
34
34
  */
35
- export type StrandBackfillPushClient = Pick<BlockTransferClient, 'pushBlocks'>;
36
- export interface StrandBackfillDeps {
37
- strandId: string;
38
- /** The STRAND's libp2p node (not the control node) — source of connection events and peer ids. */
35
+ export type PeerJoinBackfillPushClient = Pick<BlockTransferClient, 'pushBlocks'>;
36
+ export interface PeerJoinBackfillDeps {
37
+ /** Log tag naming which network this catch-up serves (a strand id, or `control-<partyId>`). */
38
+ label: string;
39
+ /** The libp2p node of the network being caught up — source of connection events and peer ids. */
39
40
  libp2p: Libp2p;
40
41
  /** `node.keyNetwork`, the IPeerNetwork BlockTransferClient dials through. */
41
42
  peerNetwork: IPeerNetwork;
42
- /** This strand's own raw block store — the same instance handed to the libp2p node. */
43
+ /** This network's own raw block store — the same instance handed to the libp2p node. */
43
44
  storage: IRawStorage;
44
- /** Must equal the prefix the receiver registered its handler under: `/optimystic/strand-<strandId>`. */
45
+ /** Must equal the prefix the receiver registered its handler under: `/optimystic/<networkName>`. */
45
46
  protocolPrefix: string;
47
+ /**
48
+ * Membership gate, consulted at PUSH time (fails closed on a throw). Absent = push to
49
+ * every connected peer — correct for strand networks, NEVER for the control network;
50
+ * see the module comment for why the strand no-gate argument does not carry over.
51
+ */
52
+ authorizePeer?: (peerId: string) => Promise<boolean>;
46
53
  /**
47
54
  * Test seam: build the per-peer push client. Defaults to a real
48
55
  * `BlockTransferClient` over {@link peerNetwork} + {@link protocolPrefix}.
49
56
  */
50
- createPushClient?: (peerId: PeerId) => StrandBackfillPushClient;
57
+ createPushClient?: (peerId: PeerId) => PeerJoinBackfillPushClient;
51
58
  }
52
59
  /** What one peer's catch-up actually did. Returned for tests and logged at the end of each run. */
53
- export interface StrandBackfillResult {
60
+ export interface PeerJoinBackfillResult {
54
61
  /** Blocks offered: had a committed `latest` AND materialized content locally. */
55
62
  offered: number;
56
63
  /** Blocks the remote reported it persisted. */
@@ -65,19 +72,21 @@ export interface StrandBackfillResult {
65
72
  capped: number;
66
73
  /** Skipped: a single block whose wire size alone exceeds {@link MAX_BLOCK_MESSAGE_BYTES}. */
67
74
  oversized: string[];
75
+ /** True when the `authorizePeer` gate refused (or threw) — nothing was pushed, peer not memoized. */
76
+ denied: boolean;
68
77
  }
69
78
  /**
70
- * Copies every block in one strand's own raw store to each peer its libp2p node connects
71
- * to, so a peer that joined the strand after blocks were committed still ends up holding
79
+ * Copies every block in one network's own raw store to each peer its libp2p node connects
80
+ * to, so a peer that joined the network after blocks were committed still ends up holding
72
81
  * them physically (the receiver persists each push via `saveReplicatedBlock`, which is
73
82
  * monotonic and idempotent — crossing pushes from both ends cannot regress a revision).
74
83
  *
75
- * Best-effort throughout: nothing here throws into a libp2p event handler or into
76
- * `buildStrandRuntime`; a failed chunk is logged and the run continues. A peer is marked
84
+ * Best-effort throughout: nothing here throws into a libp2p event handler or into a
85
+ * runtime bring-up; a failed chunk is logged and the run continues. A peer is marked
77
86
  * fully caught up ONLY after a run with no thrown chunk and an empty `missing` list, so
78
87
  * the next `connection:open` from a peer whose catch-up failed retries it.
79
88
  */
80
- export declare class StrandBackfill {
89
+ export declare class PeerJoinBackfill {
81
90
  private readonly deps;
82
91
  private readonly config;
83
92
  private readonly createPushClient;
@@ -87,15 +96,35 @@ export declare class StrandBackfill {
87
96
  private readonly done;
88
97
  /** Peers with a catch-up currently running (suppresses concurrent duplicates). */
89
98
  private readonly inFlight;
99
+ /**
100
+ * Peers whose (re-)schedule arrived while their OWN run was in flight, replayed once
101
+ * that run finishes. Load-bearing for the gated path: the gate's authorization check is
102
+ * a control-database read, so the window between it and the run's end is wide enough for
103
+ * the very membership commit that would have authorized the peer to land inside it —
104
+ * dropping that schedule (rather than deferring it) leaves the denied peer waiting for a
105
+ * reconnect, which is exactly what the re-arm exists to avoid.
106
+ */
107
+ private readonly rearmAfterFlight;
90
108
  /** Pending per-peer debounce timers, cleared on stop. */
91
109
  private readonly timers;
92
110
  private readonly onConnectionOpen;
93
111
  private loggedNoListBlockIds;
94
- constructor(deps: StrandBackfillDeps, config?: StrandBackfillConfig);
112
+ constructor(deps: PeerJoinBackfillDeps, config?: PeerJoinBackfillConfig);
95
113
  /** Subscribe to connection:open AND schedule a catch-up for peers already connected. */
96
114
  start(): void;
97
115
  /** Unsubscribe, clear timers; in-flight runs observe the stopped flag and bail. */
98
116
  stop(): void;
117
+ /**
118
+ * (Re-)schedule a debounced catch-up for every currently-connected peer not yet caught
119
+ * up. Idempotent and cheap (caught-up peers are skipped before any timer is set, and a
120
+ * peer whose run is in flight is deferred to the end of that run rather than dropped).
121
+ * Driven by {@link start}, and — on a gated network — by the embedder whenever
122
+ * membership changes, so a peer whose first pass was denied (connected before it was
123
+ * authorized: the production join order) is retried without waiting for a reconnect.
124
+ * Returns how many distinct peers were considered (for the start() log) — a peer holding
125
+ * several connections is one peer, and is scheduled once.
126
+ */
127
+ scheduleConnectedPeers(): number;
99
128
  /** Debounced entry point for connection churn: one run per peer per settle window. */
100
129
  private schedulePeer;
101
130
  /**
@@ -103,7 +132,7 @@ export declare class StrandBackfill {
103
132
  * all-zero result without pushing when the peer is already caught up, already being
104
133
  * caught up, or this backfill is stopped.
105
134
  */
106
- catchUpPeer(peerId: PeerId): Promise<StrandBackfillResult>;
135
+ catchUpPeer(peerId: PeerId): Promise<PeerJoinBackfillResult>;
107
136
  /** The actual copy. `clean` = every chunk pushed and the remote persisted every block. */
108
137
  private runCatchUp;
109
138
  }
@@ -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,yFAAyF;AACzF,yFAAyF;AACzF,EAAE;AACF,wFAAwF;AACxF,iFAAiF;AACjF,0FAA0F;AAC1F,uFAAuF;AACvF,0FAA0F;AAC1F,sBAAsB;AACtB,EAAE;AACF,2FAA2F;AAC3F,wFAAwF;AACxF,oBAAoB;AACpB,EAAE;AACF,gFAAgF;AAChF,uBAAuB;AACvB,EAAE;AACF,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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"}
@@ -77,6 +77,79 @@ export declare function relayCircuitAddrs(relayAddrs: readonly string[]): string
77
77
  * resolution either way.
78
78
  */
79
79
  export declare function resolveListenAddrs(network: NetworkConfig | undefined, route?: RelayListenRoute): string[] | undefined;
80
+ /**
81
+ * The transport-derived `createLibp2pNode` options a resolved listen set implies —
82
+ * see {@link resolveTransportOptions}.
83
+ */
84
+ export interface ListenTransportOptions {
85
+ /**
86
+ * Present when some listen entry names WebSocket. Its VALUE is never bound; it is
87
+ * the switch that makes `@optimystic/db-p2p` add `webSockets()` — see
88
+ * {@link WS_TRANSPORT_SWITCH_PORT}.
89
+ */
90
+ wsPort?: number;
91
+ }
92
+ /**
93
+ * The transport-derived libp2p options `listenAddrs` implies, and the gate that stops a
94
+ * listen address the node has no transport for from being SILENTLY dropped.
95
+ *
96
+ * libp2p's transport manager sorts configured listen addresses by which transport claims
97
+ * them, discards the unclaimed ones, and raises `UnsupportedListenAddressesError` only
98
+ * when EVERY address was discarded. Pair a TCP address with a WebSocket one — which is
99
+ * exactly what the shipped configs do — and the TCP address carries the start while the
100
+ * missing WebSocket listener is never reported. Two halves of `NetworkConfig` that have
101
+ * to agree (`listenAddrs`, and the transports the node will actually have) were never
102
+ * checked against each other; this is that check, and it covers both node kinds because
103
+ * both resolve their listen set through {@link resolveListenAddrs}.
104
+ *
105
+ * The two transport classes are handled differently, on purpose:
106
+ *
107
+ * - **WebSocket is DERIVED.** It is the one non-TCP transport the shipped configs need
108
+ * (the React Native reference app's companion drone; `cadre start --ws-port`), and
109
+ * `@optimystic/db-p2p` already knows how to add it. `cadre-core` grows no dependency
110
+ * and decides no transport policy — it flips db-p2p's own switch
111
+ * ({@link WS_TRANSPORT_SWITCH_PORT}).
112
+ * - **Everything else is REFUSED.** Deriving `/quic-v1`, `/webrtc` or `/webtransport`
113
+ * would mean importing transport packages into every `cadre-core` consumer including
114
+ * the React Native and browser bundles (against the cross-platform rule in
115
+ * `AGENTS.md`) and duplicating policy `libp2p-node.ts` owns. So name the address, name
116
+ * the transport it needs, and refuse to start — matching how `network.relayAddrs` and
117
+ * `network.announceAddrs` already treat an operator typo.
118
+ *
119
+ * Returns `{}` unconditionally when `network.transports` is set: a programmatic embedder
120
+ * supplying transport factories owns the policy, and the factories are opaque — nothing
121
+ * can be inferred from them. That is what keeps the RN phone, the web app, and the
122
+ * integration-test harness unaffected.
123
+ *
124
+ * NOTE: pairing this with {@link resolveListenAddrs} is a CONVENTION, not a structure —
125
+ * a third libp2p-node build site could resolve listen addrs and forget to call this,
126
+ * putting the original bug back on that path. Both existing sites are covered
127
+ * (`cadre-node.ts` → `buildControlNodeOptions`, `strand-network-config.ts` →
128
+ * `strandNodeAddrs`), and they are the only two `createLibp2pNode` callers in this repo
129
+ * that derive their listen set from a `NetworkConfig` — the other callers
130
+ * (`integration-tests/src/harness/test-party.ts`, `quereus-plugin-sereus`'s `connect.ts`
131
+ * and `connect-browser.ts`) pass no `listenAddrs` at all, or pass their own transports
132
+ * with it. If a third CONFIG-derived site appears, fold the two functions into one that
133
+ * returns listen addrs and transport options together, so forgetting becomes impossible
134
+ * rather than merely unlikely.
135
+ *
136
+ * @param listenAddrs the ALREADY-resolved listen set — the output of
137
+ * {@link resolveListenAddrs} after any per-node rewriting — so the check reads what
138
+ * this node will actually bind.
139
+ * @throws {UnbindableListenAddressError} when a listen entry names a transport outside
140
+ * {tcp, ws/wss, p2p-circuit}.
141
+ */
142
+ export declare function resolveTransportOptions(network: NetworkConfig | undefined, listenAddrs: readonly string[] | undefined): ListenTransportOptions;
143
+ /**
144
+ * Thrown at config resolution when `network.listenAddrs` names an address none of the
145
+ * default transports can bind. Names each offending address alongside the libp2p
146
+ * transport package it would need — what the operator has without this is a node that
147
+ * starts and silently never listens there.
148
+ */
149
+ export declare class UnbindableListenAddressError extends Error {
150
+ readonly listenAddrs: readonly string[];
151
+ constructor(listenAddrs: readonly string[]);
152
+ }
80
153
  /**
81
154
  * Thrown out of `CadreNode.start()` when the boot-path reservation drive for
82
155
  * `network.relayAddrs` produces no `/p2p-circuit` address on its FIRST attempt.