@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.
- package/README.md +23 -8
- package/dist/bootstrap-peer-store-file.js +2 -2
- package/dist/bootstrap-peer-store.d.ts +32 -12
- package/dist/bootstrap-peer-store.js +41 -15
- package/dist/bootstrap-peer-store.js.map +1 -1
- package/dist/cadre-node.d.ts +883 -102
- package/dist/cadre-node.js +1800 -209
- package/dist/cadre-node.js.map +1 -1
- package/dist/control-authorization.d.ts +2 -2
- package/dist/control-authorization.js +1 -0
- package/dist/control-authorization.js.map +1 -1
- package/dist/control-cohort.d.ts +13 -26
- package/dist/control-cohort.js +0 -26
- package/dist/control-cohort.js.map +1 -1
- package/dist/control-database.d.ts +318 -44
- package/dist/control-database.js +516 -74
- package/dist/control-database.js.map +1 -1
- package/dist/control-read-retry.d.ts +40 -11
- package/dist/control-read-retry.js +48 -15
- package/dist/control-read-retry.js.map +1 -1
- package/dist/control-retry.d.ts +58 -3
- package/dist/control-retry.js +58 -3
- package/dist/control-retry.js.map +1 -1
- package/dist/control-schema.d.ts +1 -1
- package/dist/control-schema.js +133 -21
- package/dist/control-schema.js.map +1 -1
- package/dist/control-write-retry.d.ts +80 -11
- package/dist/control-write-retry.js +168 -29
- package/dist/control-write-retry.js.map +1 -1
- package/dist/enrolled-machine-store-file.d.ts +13 -0
- package/dist/enrolled-machine-store-file.js +41 -0
- package/dist/enrolled-machine-store-file.js.map +1 -0
- package/dist/enrolled-machine-store.d.ts +81 -0
- package/dist/enrolled-machine-store.js +225 -0
- package/dist/enrolled-machine-store.js.map +1 -0
- package/dist/file-durable-slot.js +5 -3
- package/dist/file-durable-slot.js.map +1 -1
- package/dist/index.d.ts +17 -8
- package/dist/index.js +54 -9
- package/dist/index.js.map +1 -1
- package/dist/membership-connection-gater.d.ts +15 -1
- package/dist/membership-connection-gater.js +9 -3
- package/dist/membership-connection-gater.js.map +1 -1
- package/dist/node-local-snapshot.d.ts +21 -7
- package/dist/node-local-snapshot.js +30 -9
- package/dist/node-local-snapshot.js.map +1 -1
- package/dist/peer-dial.d.ts +143 -0
- package/dist/peer-dial.js +176 -0
- package/dist/peer-dial.js.map +1 -0
- package/dist/{strand-backfill.d.ts → peer-join-backfill.d.ts} +49 -20
- package/dist/{strand-backfill.js → peer-join-backfill.js} +133 -52
- package/dist/peer-join-backfill.js.map +1 -0
- package/dist/relay-addrs.d.ts +160 -49
- package/dist/relay-addrs.js +287 -60
- package/dist/relay-addrs.js.map +1 -1
- package/dist/relay-reservation.d.ts +71 -3
- package/dist/relay-reservation.js +116 -8
- package/dist/relay-reservation.js.map +1 -1
- package/dist/seed-bootstrap.d.ts +30 -1
- package/dist/seed-bootstrap.js +62 -17
- package/dist/seed-bootstrap.js.map +1 -1
- package/dist/storage-scope.d.ts +62 -0
- package/dist/storage-scope.js +75 -0
- package/dist/storage-scope.js.map +1 -0
- package/dist/strand-database.d.ts +41 -11
- package/dist/strand-database.js +48 -18
- package/dist/strand-database.js.map +1 -1
- package/dist/strand-first-sync-gate.d.ts +152 -0
- package/dist/strand-first-sync-gate.js +208 -0
- package/dist/strand-first-sync-gate.js.map +1 -0
- package/dist/strand-formation-manager.d.ts +99 -5
- package/dist/strand-formation-manager.js +119 -8
- package/dist/strand-formation-manager.js.map +1 -1
- package/dist/strand-formation-protocol.d.ts +97 -3
- package/dist/strand-formation-protocol.js +108 -2
- package/dist/strand-formation-protocol.js.map +1 -1
- package/dist/strand-instance-manager.d.ts +389 -19
- package/dist/strand-instance-manager.js +678 -65
- package/dist/strand-instance-manager.js.map +1 -1
- package/dist/strand-member-key.d.ts +20 -12
- package/dist/strand-member-key.js +20 -12
- package/dist/strand-member-key.js.map +1 -1
- package/dist/strand-member-registry.js +3 -0
- package/dist/strand-member-registry.js.map +1 -1
- package/dist/strand-membership-reconciler.d.ts +361 -0
- package/dist/strand-membership-reconciler.js +548 -0
- package/dist/strand-membership-reconciler.js.map +1 -0
- package/dist/strand-membership-writer.d.ts +143 -11
- package/dist/strand-membership-writer.js +419 -135
- package/dist/strand-membership-writer.js.map +1 -1
- package/dist/strand-network-config.d.ts +116 -0
- package/dist/strand-network-config.js +192 -0
- package/dist/strand-network-config.js.map +1 -0
- package/dist/strand-revocation-enforcer.d.ts +392 -0
- package/dist/strand-revocation-enforcer.js +533 -0
- package/dist/strand-revocation-enforcer.js.map +1 -0
- package/dist/strand-solicitation.d.ts +17 -1
- package/dist/strand-solicitation.js +13 -2
- package/dist/strand-solicitation.js.map +1 -1
- package/dist/strand-wake-protocol.d.ts +11 -7
- package/dist/strand-wake-protocol.js +20 -52
- package/dist/strand-wake-protocol.js.map +1 -1
- package/dist/strand-watcher.js +12 -1
- package/dist/strand-watcher.js.map +1 -1
- package/dist/timed-step.d.ts +12 -0
- package/dist/timed-step.js +28 -0
- package/dist/timed-step.js.map +1 -0
- package/dist/timeout-scheduler.d.ts +19 -0
- package/dist/timeout-scheduler.js +22 -0
- package/dist/timeout-scheduler.js.map +1 -0
- package/dist/types.d.ts +472 -46
- package/dist/types.js +7 -1
- package/dist/types.js.map +1 -1
- package/package.json +16 -8
- package/src/bootstrap-peer-store-file.ts +2 -2
- package/src/bootstrap-peer-store.ts +69 -27
- package/src/cadre-node.ts +1926 -228
- package/src/control-authorization.ts +2 -1
- package/src/control-cohort.ts +122 -130
- package/src/control-database.ts +586 -74
- package/src/control-read-retry.ts +202 -167
- package/src/control-retry.ts +297 -190
- package/src/control-schema.ts +133 -21
- package/src/control-write-retry.ts +525 -383
- package/src/enrolled-machine-store-file.ts +43 -0
- package/src/enrolled-machine-store.ts +280 -0
- package/src/file-durable-slot.ts +5 -3
- package/src/index.ts +134 -15
- package/src/membership-connection-gater.ts +9 -3
- package/src/node-local-snapshot.ts +38 -9
- package/src/peer-addr-book.ts +251 -250
- package/src/peer-dial.ts +215 -0
- package/src/{strand-backfill.ts → peer-join-backfill.ts} +478 -389
- package/src/relay-addrs.ts +458 -218
- package/src/relay-reservation.ts +951 -828
- package/src/seed-bootstrap.ts +76 -18
- package/src/storage-scope.ts +78 -0
- package/src/strand-database.ts +61 -21
- package/src/strand-first-sync-gate.ts +258 -0
- package/src/strand-formation-manager.ts +162 -8
- package/src/strand-formation-protocol.ts +850 -692
- package/src/strand-instance-manager.ts +877 -74
- package/src/strand-member-key.ts +20 -12
- package/src/strand-member-registry.ts +3 -0
- package/src/strand-membership-reconciler.ts +680 -0
- package/src/strand-membership-writer.ts +550 -190
- package/src/strand-network-config.ts +250 -0
- package/src/strand-revocation-enforcer.ts +653 -0
- package/src/strand-solicitation.ts +499 -471
- package/src/strand-wake-protocol.ts +21 -66
- package/src/strand-watcher.ts +325 -314
- package/src/timed-step.ts +28 -0
- package/src/timeout-scheduler.ts +28 -0
- package/src/types.ts +487 -52
- package/dist/strand-backfill.js.map +0 -1
|
@@ -1,389 +1,478 @@
|
|
|
1
|
-
import debug from 'debug';
|
|
2
|
-
import type { Connection, Libp2p, PeerId } from '@libp2p/interface';
|
|
3
|
-
import type { ActionId, IPeerNetwork } from '@optimystic/db-core';
|
|
4
|
-
import { BlockTransferClient, type BlockCommitProof, type IRawStorage } from '@optimystic/db-p2p';
|
|
5
|
-
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
//
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
//
|
|
24
|
-
//
|
|
25
|
-
//
|
|
26
|
-
//
|
|
27
|
-
//
|
|
28
|
-
//
|
|
29
|
-
//
|
|
30
|
-
//
|
|
31
|
-
//
|
|
32
|
-
//
|
|
33
|
-
//
|
|
34
|
-
//
|
|
35
|
-
//
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
*/
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
*/
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
const
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
1
|
+
import debug from 'debug';
|
|
2
|
+
import type { Connection, Libp2p, PeerId } from '@libp2p/interface';
|
|
3
|
+
import type { ActionId, IPeerNetwork } from '@optimystic/db-core';
|
|
4
|
+
import { BlockTransferClient, type BlockCommitProof, type IRawStorage } from '@optimystic/db-p2p';
|
|
5
|
+
|
|
6
|
+
// Peer-join whole-store block catch-up, shared by the STRAND networks and the CONTROL
|
|
7
|
+
// network. On `connection:open` (debounced) it pushes every committed, materialized block
|
|
8
|
+
// in the local raw store to the new peer, so a machine that joined after blocks were
|
|
9
|
+
// committed still ends up physically holding them. Optimystic has no cohort-join catch-up
|
|
10
|
+
// of its own; a block committed while its writer was alone has exactly one holder forever
|
|
11
|
+
// without this (named collection-header blocks are written once, at collection creation,
|
|
12
|
+
// and their revision never moves again — so no later commit ever carries them anywhere).
|
|
13
|
+
//
|
|
14
|
+
// NOTE: this copies the WHOLE local store to every newly connected peer, which is right
|
|
15
|
+
// while a network is one party's handful of machines (see docs/architecture.md →
|
|
16
|
+
// Replication cluster size — the control network replicates to the whole party by intent,
|
|
17
|
+
// and "On a strand, the machine count is not a party count"). If these meshes ever get
|
|
18
|
+
// large, filter the pushed set by FRET cohort responsibility for each block id instead of
|
|
19
|
+
// pushing everything.
|
|
20
|
+
//
|
|
21
|
+
// NOTE: this lives in cadre-core only because `../optimystic` is read-only from this repo.
|
|
22
|
+
// If Optimystic ever grows a cohort-join catch-up of its own, delete this module rather
|
|
23
|
+
// than running two.
|
|
24
|
+
//
|
|
25
|
+
// Membership — the one place the two networks differ, expressed as the optional
|
|
26
|
+
// `authorizePeer` dep:
|
|
27
|
+
//
|
|
28
|
+
// - STRAND networks pass none. Anything connected on a strand's own libp2p network already
|
|
29
|
+
// receives cohort replicas of new commits, so pushing older blocks to it exposes nothing
|
|
30
|
+
// new; adding a gate would diverge from what ordinary replication already does there. A
|
|
31
|
+
// peer that does NOT speak the strand's block-transfer protocol (a bare circuit relay or
|
|
32
|
+
// bootstrap node the strand node also connects to) cannot receive anything: the protocol
|
|
33
|
+
// id is namespaced per strand, so the dial fails and the push is dropped.
|
|
34
|
+
// - The CONTROL network MUST pass one, because that argument does not carry over: its
|
|
35
|
+
// inbound connection gate deliberately admits non-members in several states (an
|
|
36
|
+
// un-enrolled node taking its seed, an open enrollment window, an outstanding
|
|
37
|
+
// invitation, configured bootstrap/relay peers — see docs/architecture.md, the
|
|
38
|
+
// control-network inbound connection gate). Pushing the whole control store to such a
|
|
39
|
+
// peer would hand a stranger the party's entire membership, addresses and strand list.
|
|
40
|
+
// `CadreNode` passes `isAuthorizedMember`; the gate is consulted at PUSH time, not at
|
|
41
|
+
// schedule time, and fails closed on a thrown check. A denied run is not memoized, so
|
|
42
|
+
// the peer is retried on its next `connection:open` — or sooner, via
|
|
43
|
+
// `scheduleConnectedPeers()` on a membership change (the production join order is
|
|
44
|
+
// connect-then-authorize, so the denial at dial time is the expected first pass).
|
|
45
|
+
//
|
|
46
|
+
// NOTE: a failing dial to a non-speaking peer costs one dial timeout per connection:open
|
|
47
|
+
// from such a peer, and is never memoized (only clean runs are). Bounded today by the
|
|
48
|
+
// debounce plus the unreachable-peer bail in `runCatchUp` — one dial per event, not one
|
|
49
|
+
// per chunk. If a node ever holds many such connections, or the enumeration ahead of the
|
|
50
|
+
// first chunk gets expensive, pre-check `libp2p.peerStore` for this network's
|
|
51
|
+
// block-transfer protocol before enumerating.
|
|
52
|
+
//
|
|
53
|
+
// NOTE: a run that fails is retried only when that peer next opens a connection (or, with
|
|
54
|
+
// an `authorizePeer` gate wired, when `scheduleConnectedPeers()` is driven). Over a stable
|
|
55
|
+
// connection a transient push failure therefore leaves the peer partially copied until the
|
|
56
|
+
// next reconnect (read repair still covers reads meanwhile). If these meshes ever hold
|
|
57
|
+
// long-lived connections where that matters, re-arm the debounce timer with a backoff on a
|
|
58
|
+
// non-clean run instead of waiting for connection:open.
|
|
59
|
+
|
|
60
|
+
const log = debug('sereus:cadre:peer-join-backfill');
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The block-transfer protocol's hard cap on one length-prefixed message. Mirrors
|
|
64
|
+
* `MAX_BLOCK_MESSAGE_BYTES` in `@optimystic/db-p2p`'s `protocol-limits.ts` (8 MiB), which
|
|
65
|
+
* that package does not re-export from its index — keep the two in sync if upstream ever
|
|
66
|
+
* changes it or starts exporting it.
|
|
67
|
+
*/
|
|
68
|
+
export const MAX_BLOCK_MESSAGE_BYTES = 8 * 1024 * 1024;
|
|
69
|
+
|
|
70
|
+
/** Tuning for the per-peer catch-up. Every field optional; defaults in {@link DEFAULT_PEER_JOIN_BACKFILL}. */
|
|
71
|
+
export interface PeerJoinBackfillConfig {
|
|
72
|
+
/** Default true. False disables the catch-up entirely (the pre-existing behaviour). */
|
|
73
|
+
enabled?: boolean;
|
|
74
|
+
/** Settle time after a connection opens before catching that peer up, ms. Default 1000. */
|
|
75
|
+
debounceMs?: number;
|
|
76
|
+
/** Ceiling on blocks copied in one catch-up. Default 10_000. Reaching it is LOGGED, never silent. */
|
|
77
|
+
maxBlocks?: number;
|
|
78
|
+
/** Soft byte budget per push message. Default 1 MiB. Protocol hard cap is {@link MAX_BLOCK_MESSAGE_BYTES} (8 MiB). */
|
|
79
|
+
maxChunkBytes?: number;
|
|
80
|
+
/** Max blocks per push message. Default 64. */
|
|
81
|
+
maxChunkBlocks?: number;
|
|
82
|
+
/** Per-push dial deadline, ms. Default 3000 (matches SpreadOnChurnMonitor). */
|
|
83
|
+
dialTimeoutMs?: number;
|
|
84
|
+
/** Per-push response deadline, ms. Default 10_000 (matches SpreadOnChurnMonitor). */
|
|
85
|
+
responseTimeoutMs?: number;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The resolved defaults every {@link PeerJoinBackfill} starts from. */
|
|
89
|
+
export const DEFAULT_PEER_JOIN_BACKFILL: Required<PeerJoinBackfillConfig> = {
|
|
90
|
+
enabled: true,
|
|
91
|
+
debounceMs: 1000,
|
|
92
|
+
maxBlocks: 10_000,
|
|
93
|
+
maxChunkBytes: 1024 * 1024,
|
|
94
|
+
maxChunkBlocks: 64,
|
|
95
|
+
dialTimeoutMs: 3000,
|
|
96
|
+
responseTimeoutMs: 10_000
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The one capability the catch-up needs from a transfer client. Structural (rather than
|
|
101
|
+
* the concrete `BlockTransferClient`) so unit tests can capture pushes without dialing
|
|
102
|
+
* libp2p — see {@link PeerJoinBackfillDeps.createPushClient}.
|
|
103
|
+
*/
|
|
104
|
+
export type PeerJoinBackfillPushClient = Pick<BlockTransferClient, 'pushBlocks'>;
|
|
105
|
+
|
|
106
|
+
export interface PeerJoinBackfillDeps {
|
|
107
|
+
/** Log tag naming which network this catch-up serves (a strand id, or `control-<partyId>`). */
|
|
108
|
+
label: string;
|
|
109
|
+
/** The libp2p node of the network being caught up — source of connection events and peer ids. */
|
|
110
|
+
libp2p: Libp2p;
|
|
111
|
+
/** `node.keyNetwork`, the IPeerNetwork BlockTransferClient dials through. */
|
|
112
|
+
peerNetwork: IPeerNetwork;
|
|
113
|
+
/** This network's own raw block store — the same instance handed to the libp2p node. */
|
|
114
|
+
storage: IRawStorage;
|
|
115
|
+
/** Must equal the prefix the receiver registered its handler under: `/optimystic/<networkName>`. */
|
|
116
|
+
protocolPrefix: string;
|
|
117
|
+
/**
|
|
118
|
+
* Membership gate, consulted at PUSH time (fails closed on a throw). Absent = push to
|
|
119
|
+
* every connected peer — correct for strand networks, NEVER for the control network;
|
|
120
|
+
* see the module comment for why the strand no-gate argument does not carry over.
|
|
121
|
+
*/
|
|
122
|
+
authorizePeer?: (peerId: string) => Promise<boolean>;
|
|
123
|
+
/**
|
|
124
|
+
* Test seam: build the per-peer push client. Defaults to a real
|
|
125
|
+
* `BlockTransferClient` over {@link peerNetwork} + {@link protocolPrefix}.
|
|
126
|
+
*/
|
|
127
|
+
createPushClient?: (peerId: PeerId) => PeerJoinBackfillPushClient;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** What one peer's catch-up actually did. Returned for tests and logged at the end of each run. */
|
|
131
|
+
export interface PeerJoinBackfillResult {
|
|
132
|
+
/** Blocks offered: had a committed `latest` AND materialized content locally. */
|
|
133
|
+
offered: number;
|
|
134
|
+
/** Blocks the remote reported it persisted. */
|
|
135
|
+
accepted: number;
|
|
136
|
+
/** Block ids the remote reported in `missing` (parse or persist failure on its side). */
|
|
137
|
+
rejected: string[];
|
|
138
|
+
/** Skipped: metadata has no `latest` (pending-only — not yet a durability claim here). */
|
|
139
|
+
uncommitted: number;
|
|
140
|
+
/** Skipped: `latest` exists but no materialized block is stored for that actionId. */
|
|
141
|
+
unmaterialized: number;
|
|
142
|
+
/** Not attempted because `maxBlocks` was reached. */
|
|
143
|
+
capped: number;
|
|
144
|
+
/** Skipped: a single block whose wire size alone exceeds {@link MAX_BLOCK_MESSAGE_BYTES}. */
|
|
145
|
+
oversized: string[];
|
|
146
|
+
/** True when the `authorizePeer` gate refused (or threw) — nothing was pushed, peer not memoized. */
|
|
147
|
+
denied: boolean;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function emptyResult(): PeerJoinBackfillResult {
|
|
151
|
+
return { offered: 0, accepted: 0, rejected: [], uncommitted: 0, unmaterialized: 0, capped: 0, oversized: [], denied: false };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** One in-flight push message being accumulated. */
|
|
155
|
+
interface Chunk {
|
|
156
|
+
ids: string[];
|
|
157
|
+
buffers: Uint8Array[];
|
|
158
|
+
meta: Record<string, { rev: number; actionId: ActionId }>;
|
|
159
|
+
/**
|
|
160
|
+
* The cohort commit proof retained for each block's pushed revision, where one exists.
|
|
161
|
+
*
|
|
162
|
+
* A receiver running the default `requirePushCertificate: true` REJECTS a block pushed
|
|
163
|
+
* without one, so this is what makes peer-join catch-up land at all — a push carrying only
|
|
164
|
+
* `meta` is the legacy shape and is refused. Sparse deliberately: a block whose proof was
|
|
165
|
+
* never retained is still offered with its meta, which is the only thing that lets a
|
|
166
|
+
* receiver migrating with the flag off land the replica at this node's revision rather than
|
|
167
|
+
* a fabricated rev 1. Upstream's rule is the other direction and holds here by construction:
|
|
168
|
+
* a proof is never attached WITHOUT its meta, because both are written from the same
|
|
169
|
+
* `latest` in one place.
|
|
170
|
+
*/
|
|
171
|
+
proofs: Record<string, BlockCommitProof>;
|
|
172
|
+
bytes: number;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Base64 wire size of a raw buffer — `pushBlocks` base64-encodes each block into the JSON
|
|
177
|
+
* request, so the protocol cap must be judged against the encoded size, not the raw bytes.
|
|
178
|
+
*/
|
|
179
|
+
function base64WireBytes(rawBytes: number): number {
|
|
180
|
+
return Math.ceil(rawBytes / 3) * 4;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Copies every block in one network's own raw store to each peer its libp2p node connects
|
|
185
|
+
* to, so a peer that joined the network after blocks were committed still ends up holding
|
|
186
|
+
* them physically (the receiver persists each push via `saveReplicatedBlock`, which is
|
|
187
|
+
* monotonic and idempotent — crossing pushes from both ends cannot regress a revision).
|
|
188
|
+
*
|
|
189
|
+
* Best-effort throughout: nothing here throws into a libp2p event handler or into a
|
|
190
|
+
* runtime bring-up; a failed chunk is logged and the run continues. A peer is marked
|
|
191
|
+
* fully caught up ONLY after a run with no thrown chunk and an empty `missing` list, so
|
|
192
|
+
* the next `connection:open` from a peer whose catch-up failed retries it.
|
|
193
|
+
*/
|
|
194
|
+
export class PeerJoinBackfill {
|
|
195
|
+
private readonly config: Required<PeerJoinBackfillConfig>;
|
|
196
|
+
private readonly createPushClient: (peerId: PeerId) => PeerJoinBackfillPushClient;
|
|
197
|
+
private started = false;
|
|
198
|
+
private stopped = false;
|
|
199
|
+
/** Peers fully caught up this runtime (never retried until the runtime is rebuilt). */
|
|
200
|
+
private readonly done = new Set<string>();
|
|
201
|
+
/** Peers with a catch-up currently running (suppresses concurrent duplicates). */
|
|
202
|
+
private readonly inFlight = new Set<string>();
|
|
203
|
+
/**
|
|
204
|
+
* Peers whose (re-)schedule arrived while their OWN run was in flight, replayed once
|
|
205
|
+
* that run finishes. Load-bearing for the gated path: the gate's authorization check is
|
|
206
|
+
* a control-database read, so the window between it and the run's end is wide enough for
|
|
207
|
+
* the very membership commit that would have authorized the peer to land inside it —
|
|
208
|
+
* dropping that schedule (rather than deferring it) leaves the denied peer waiting for a
|
|
209
|
+
* reconnect, which is exactly what the re-arm exists to avoid.
|
|
210
|
+
*/
|
|
211
|
+
private readonly rearmAfterFlight = new Set<string>();
|
|
212
|
+
/** Pending per-peer debounce timers, cleared on stop. */
|
|
213
|
+
private readonly timers = new Map<string, ReturnType<typeof setTimeout>>();
|
|
214
|
+
private readonly onConnectionOpen: (evt: CustomEvent<Connection>) => void;
|
|
215
|
+
private loggedNoListBlockIds = false;
|
|
216
|
+
|
|
217
|
+
constructor(private readonly deps: PeerJoinBackfillDeps, config?: PeerJoinBackfillConfig) {
|
|
218
|
+
this.config = { ...DEFAULT_PEER_JOIN_BACKFILL, ...config };
|
|
219
|
+
this.createPushClient = deps.createPushClient
|
|
220
|
+
?? ((peerId) => new BlockTransferClient(peerId, deps.peerNetwork, deps.protocolPrefix));
|
|
221
|
+
this.onConnectionOpen = (evt) => this.schedulePeer(evt.detail.remotePeer);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Subscribe to connection:open AND schedule a catch-up for peers already connected. */
|
|
225
|
+
start(): void {
|
|
226
|
+
if (this.started || this.stopped || !this.config.enabled) return;
|
|
227
|
+
this.started = true;
|
|
228
|
+
this.deps.libp2p.addEventListener('connection:open', this.onConnectionOpen);
|
|
229
|
+
// A runtime rebuilt over live connections (resumeStrand) never sees their
|
|
230
|
+
// connection:open, so walk what is already connected once.
|
|
231
|
+
const scheduled = this.scheduleConnectedPeers();
|
|
232
|
+
log('[%s] started (%d peer(s) already connected)', this.deps.label, scheduled);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Unsubscribe, clear timers; in-flight runs observe the stopped flag and bail. */
|
|
236
|
+
stop(): void {
|
|
237
|
+
if (this.stopped) return;
|
|
238
|
+
this.stopped = true;
|
|
239
|
+
if (this.started) {
|
|
240
|
+
this.deps.libp2p.removeEventListener('connection:open', this.onConnectionOpen);
|
|
241
|
+
}
|
|
242
|
+
for (const timer of this.timers.values()) {
|
|
243
|
+
clearTimeout(timer);
|
|
244
|
+
}
|
|
245
|
+
this.timers.clear();
|
|
246
|
+
this.rearmAfterFlight.clear();
|
|
247
|
+
log('[%s] stopped', this.deps.label);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* (Re-)schedule a debounced catch-up for every currently-connected peer not yet caught
|
|
252
|
+
* up. Idempotent and cheap (caught-up peers are skipped before any timer is set, and a
|
|
253
|
+
* peer whose run is in flight is deferred to the end of that run rather than dropped).
|
|
254
|
+
* Driven by {@link start}, and — on a gated network — by the embedder whenever
|
|
255
|
+
* membership changes, so a peer whose first pass was denied (connected before it was
|
|
256
|
+
* authorized: the production join order) is retried without waiting for a reconnect.
|
|
257
|
+
* Returns how many distinct peers were considered (for the start() log) — a peer holding
|
|
258
|
+
* several connections is one peer, and is scheduled once.
|
|
259
|
+
*/
|
|
260
|
+
scheduleConnectedPeers(): number {
|
|
261
|
+
if (this.stopped) return 0;
|
|
262
|
+
const peers = new Set<string>();
|
|
263
|
+
for (const connection of this.deps.libp2p.getConnections()) {
|
|
264
|
+
const key = connection.remotePeer.toString();
|
|
265
|
+
if (peers.has(key)) continue;
|
|
266
|
+
peers.add(key);
|
|
267
|
+
this.schedulePeer(connection.remotePeer);
|
|
268
|
+
}
|
|
269
|
+
return peers.size;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Debounced entry point for connection churn: one run per peer per settle window. */
|
|
273
|
+
private schedulePeer(peerId: PeerId): void {
|
|
274
|
+
const key = peerId.toString();
|
|
275
|
+
if (this.stopped || this.done.has(key)) return;
|
|
276
|
+
if (this.inFlight.has(key)) {
|
|
277
|
+
// Defer rather than drop: the running pass may already be past its gate check (or
|
|
278
|
+
// past the block this schedule was meant to carry), so replay it when that pass ends.
|
|
279
|
+
this.rearmAfterFlight.add(key);
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
const existing = this.timers.get(key);
|
|
283
|
+
if (existing) clearTimeout(existing);
|
|
284
|
+
this.timers.set(key, setTimeout(() => {
|
|
285
|
+
this.timers.delete(key);
|
|
286
|
+
void this.catchUpPeer(peerId);
|
|
287
|
+
}, this.config.debounceMs));
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Run one peer's catch-up now, bypassing the debounce. Never rejects. Returns an
|
|
292
|
+
* all-zero result without pushing when the peer is already caught up, already being
|
|
293
|
+
* caught up, or this backfill is stopped.
|
|
294
|
+
*/
|
|
295
|
+
async catchUpPeer(peerId: PeerId): Promise<PeerJoinBackfillResult> {
|
|
296
|
+
const key = peerId.toString();
|
|
297
|
+
if (this.stopped || this.done.has(key) || this.inFlight.has(key)) {
|
|
298
|
+
return emptyResult();
|
|
299
|
+
}
|
|
300
|
+
this.inFlight.add(key);
|
|
301
|
+
try {
|
|
302
|
+
const { result, clean } = await this.runCatchUp(peerId);
|
|
303
|
+
// NOTE: a run that hit `maxBlocks` is still "clean" and still memoizes the peer, so
|
|
304
|
+
// the tail past the ceiling never reaches it. Deliberate: enumeration is not
|
|
305
|
+
// resumable, so not memoizing would re-push the same prefix on every reconnect
|
|
306
|
+
// without ever advancing. Loud in the log below (capped > 0). If a store can
|
|
307
|
+
// realistically exceed maxBlocks, the fix is a resumable cursor, not either policy.
|
|
308
|
+
if (clean && !this.stopped) {
|
|
309
|
+
this.done.add(key);
|
|
310
|
+
}
|
|
311
|
+
log('[%s] catch-up peer=%s offered=%d accepted=%d rejected=%d uncommitted=%d unmaterialized=%d capped=%d oversized=%d denied=%s done=%s',
|
|
312
|
+
this.deps.label, key, result.offered, result.accepted, result.rejected.length,
|
|
313
|
+
result.uncommitted, result.unmaterialized, result.capped, result.oversized.length, result.denied, clean);
|
|
314
|
+
return result;
|
|
315
|
+
} catch (error) {
|
|
316
|
+
// runCatchUp already contains a per-chunk catch; this guards the enumeration and
|
|
317
|
+
// metadata reads too — a backfill fault must never surface through a libp2p event.
|
|
318
|
+
log('[%s] catch-up peer=%s failed: %o', this.deps.label, key, error);
|
|
319
|
+
return emptyResult();
|
|
320
|
+
} finally {
|
|
321
|
+
this.inFlight.delete(key);
|
|
322
|
+
// Replay a schedule that arrived mid-run. `schedulePeer` re-checks `done`, so a run
|
|
323
|
+
// that finished clean re-arms nothing; a denied or failed one gets its retry.
|
|
324
|
+
if (this.rearmAfterFlight.delete(key)) {
|
|
325
|
+
this.schedulePeer(peerId);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** The actual copy. `clean` = every chunk pushed and the remote persisted every block. */
|
|
331
|
+
private async runCatchUp(peerId: PeerId): Promise<{ result: PeerJoinBackfillResult; clean: boolean }> {
|
|
332
|
+
const result = emptyResult();
|
|
333
|
+
const { storage } = this.deps;
|
|
334
|
+
|
|
335
|
+
// The membership gate, judged at push time (never at schedule time — authorization can
|
|
336
|
+
// change during the debounce window in either direction). Fails CLOSED on a throw: a
|
|
337
|
+
// gate that cannot answer must not leak the store. A denied run is not clean, so the
|
|
338
|
+
// peer is retried later rather than memoized as caught up.
|
|
339
|
+
if (this.deps.authorizePeer) {
|
|
340
|
+
let authorized = false;
|
|
341
|
+
try {
|
|
342
|
+
authorized = await this.deps.authorizePeer(peerId.toString());
|
|
343
|
+
} catch (error) {
|
|
344
|
+
log('[%s] authorizePeer(%s) threw — treating as denied: %o', this.deps.label, peerId.toString(), error);
|
|
345
|
+
}
|
|
346
|
+
if (!authorized) {
|
|
347
|
+
result.denied = true;
|
|
348
|
+
return { result, clean: false };
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (!storage.listBlockIds) {
|
|
353
|
+
if (!this.loggedNoListBlockIds) {
|
|
354
|
+
this.loggedNoListBlockIds = true;
|
|
355
|
+
log('[%s] raw storage does not implement listBlockIds(); backfill is inert', this.deps.label);
|
|
356
|
+
}
|
|
357
|
+
return { result, clean: false };
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const client = this.createPushClient(peerId);
|
|
361
|
+
const encoder = new TextEncoder();
|
|
362
|
+
let chunk: Chunk = { ids: [], buffers: [], meta: {}, proofs: {}, bytes: 0 };
|
|
363
|
+
let chunkFailed = false;
|
|
364
|
+
let anyChunkDelivered = false;
|
|
365
|
+
/**
|
|
366
|
+
* A peer that has answered no push at all, and failed one, is not reachable on this
|
|
367
|
+
* protocol — abandon rather than spend a dial timeout per remaining chunk. Once ONE
|
|
368
|
+
* push has been answered the peer demonstrably speaks it, so a later failure is a
|
|
369
|
+
* transient blip and the rest of the store is still worth pushing.
|
|
370
|
+
*/
|
|
371
|
+
const peerUnreachable = (): boolean => chunkFailed && !anyChunkDelivered;
|
|
372
|
+
|
|
373
|
+
const flush = async (): Promise<void> => {
|
|
374
|
+
if (chunk.ids.length === 0) return;
|
|
375
|
+
const { ids, buffers, meta, proofs } = chunk;
|
|
376
|
+
chunk = { ids: [], buffers: [], meta: {}, proofs: {}, bytes: 0 };
|
|
377
|
+
try {
|
|
378
|
+
// ONE certification value rather than two arguments, so a caller cannot pair a proof
|
|
379
|
+
// for one revision with meta for another — both were written from the same `latest`.
|
|
380
|
+
const response = await client.pushBlocks(ids, buffers, 'replication',
|
|
381
|
+
{ blockMeta: meta, blockProofs: proofs }, {
|
|
382
|
+
dialTimeoutMs: this.config.dialTimeoutMs,
|
|
383
|
+
responseTimeoutMs: this.config.responseTimeoutMs
|
|
384
|
+
});
|
|
385
|
+
anyChunkDelivered = true;
|
|
386
|
+
const missing = new Set(response.missing);
|
|
387
|
+
for (const id of ids) {
|
|
388
|
+
if (missing.has(id)) {
|
|
389
|
+
result.rejected.push(id);
|
|
390
|
+
} else {
|
|
391
|
+
result.accepted += 1;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
if (response.missing.length > 0) {
|
|
395
|
+
log('[%s] peer=%s rejected %d block(s): %o', this.deps.label, peerId.toString(), response.missing.length, response.missing);
|
|
396
|
+
}
|
|
397
|
+
} catch (error) {
|
|
398
|
+
chunkFailed = true;
|
|
399
|
+
log('[%s] push to peer=%s failed for %d block(s): %s', this.deps.label, peerId.toString(), ids.length, (error as Error).message);
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
for await (const blockId of storage.listBlockIds()) {
|
|
404
|
+
if (this.stopped) break;
|
|
405
|
+
if (result.offered >= this.config.maxBlocks) {
|
|
406
|
+
// Past the ceiling: count what is left (id enumeration only — no more reads or
|
|
407
|
+
// pushes) so the cap is loud in the end-of-run log rather than silent.
|
|
408
|
+
result.capped += 1;
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
const metadata = await storage.getMetadata(blockId);
|
|
413
|
+
const latest = metadata?.latest;
|
|
414
|
+
if (!latest) {
|
|
415
|
+
result.uncommitted += 1;
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
// NOTE: a block whose latest revision is a DELETE materializes to nothing (a
|
|
419
|
+
// tombstone), so it is skipped here — a peer that joins after such a delete never
|
|
420
|
+
// receives the tombstone from this path and relies on read repair for it. Fine
|
|
421
|
+
// while nothing deletes whole blocks pre-join; if the whole-store coverage gate in
|
|
422
|
+
// strand-membership-closed-strand-e2e ever reports a tombstone residue, teach this
|
|
423
|
+
// to push the promoted delete transform instead of skipping.
|
|
424
|
+
const block = await storage.getMaterializedBlock(blockId, latest.actionId);
|
|
425
|
+
if (!block) {
|
|
426
|
+
result.unmaterialized += 1;
|
|
427
|
+
log('[%s] block=%s has latest rev %d but no materialized content; skipped', this.deps.label, blockId, latest.rev);
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const buffer = encoder.encode(JSON.stringify(block));
|
|
432
|
+
// NOTE: MAX_BLOCK_MESSAGE_BYTES caps the whole framed request, not one block, and
|
|
433
|
+
// this test ignores the request envelope (ids array, blockMeta, JSON punctuation).
|
|
434
|
+
// A lone block within a few KiB of the cap therefore still ships and is rejected by
|
|
435
|
+
// the receiver's length-prefix decoder — a permanent chunk failure for that peer. No
|
|
436
|
+
// block comes close today; if one ever can, subtract a measured envelope allowance
|
|
437
|
+
// here rather than raising the cap.
|
|
438
|
+
if (base64WireBytes(buffer.length) >= MAX_BLOCK_MESSAGE_BYTES) {
|
|
439
|
+
result.oversized.push(blockId);
|
|
440
|
+
log('[%s] block=%s is %d bytes (%d on the wire) — exceeds the %d-byte protocol cap alone; skipped',
|
|
441
|
+
this.deps.label, blockId, buffer.length, base64WireBytes(buffer.length), MAX_BLOCK_MESSAGE_BYTES);
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// Flush before adding when this block would overflow either budget. A single block
|
|
446
|
+
// larger than maxChunkBytes still ships — alone, in its own chunk.
|
|
447
|
+
if (chunk.ids.length > 0
|
|
448
|
+
&& (chunk.ids.length + 1 > this.config.maxChunkBlocks || chunk.bytes + buffer.length > this.config.maxChunkBytes)) {
|
|
449
|
+
await flush();
|
|
450
|
+
if (this.stopped || peerUnreachable()) break;
|
|
451
|
+
}
|
|
452
|
+
chunk.ids.push(blockId);
|
|
453
|
+
chunk.buffers.push(buffer);
|
|
454
|
+
chunk.meta[blockId] = { rev: latest.rev, actionId: latest.actionId };
|
|
455
|
+
// Read the proof for EXACTLY the revision being pushed, from the same `latest` the meta
|
|
456
|
+
// above was written from. Absent is normal (nothing retained a proof for that revision);
|
|
457
|
+
// the block still ships with its meta and the receiver decides.
|
|
458
|
+
const proof = await storage.getBlockProof(blockId, latest.rev);
|
|
459
|
+
if (proof) {
|
|
460
|
+
chunk.proofs[blockId] = proof;
|
|
461
|
+
}
|
|
462
|
+
chunk.bytes += buffer.length;
|
|
463
|
+
result.offered += 1;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
if (!this.stopped && !peerUnreachable()) {
|
|
467
|
+
await flush();
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
if (result.capped > 0) {
|
|
471
|
+
log('[%s] peer=%s catch-up CAPPED at %d blocks; %d block id(s) not attempted',
|
|
472
|
+
this.deps.label, peerId.toString(), this.config.maxBlocks, result.capped);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const clean = !chunkFailed && result.rejected.length === 0;
|
|
476
|
+
return { result, clean };
|
|
477
|
+
}
|
|
478
|
+
}
|