@serfab/cadre-core 1.6.0 → 1.8.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 +8 -6
- package/dist/cadre-node.d.ts +548 -137
- package/dist/cadre-node.js +1208 -383
- 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 +6 -0
- package/dist/control-cohort.js +6 -0
- package/dist/control-cohort.js.map +1 -1
- package/dist/control-database.d.ts +46 -8
- package/dist/control-database.js +97 -18
- package/dist/control-database.js.map +1 -1
- package/dist/control-formation-recorder.d.ts +37 -14
- package/dist/control-formation-recorder.js +60 -10
- package/dist/control-formation-recorder.js.map +1 -1
- package/dist/control-read-retry.d.ts +13 -12
- package/dist/control-read-retry.js +14 -12
- package/dist/control-read-retry.js.map +1 -1
- package/dist/control-schema.d.ts +1 -1
- package/dist/control-schema.js +67 -5
- package/dist/control-schema.js.map +1 -1
- package/dist/control-stream.d.ts +9 -1
- package/dist/control-stream.js +24 -1
- package/dist/control-stream.js.map +1 -1
- package/dist/control-write-retry.d.ts +12 -2
- package/dist/control-write-retry.js +13 -2
- package/dist/control-write-retry.js.map +1 -1
- package/dist/delegate-admission.d.ts +11 -8
- package/dist/delegate-admission.js +19 -11
- package/dist/delegate-admission.js.map +1 -1
- package/dist/diagnostics/connection-path.d.ts +0 -7
- package/dist/diagnostics/connection-path.js +0 -7
- package/dist/diagnostics/connection-path.js.map +1 -1
- package/dist/formation-approval.d.ts +7 -0
- package/dist/formation-approval.js +9 -3
- package/dist/formation-approval.js.map +1 -1
- package/dist/fs-atomic.d.ts +11 -2
- package/dist/fs-atomic.js +13 -3
- package/dist/fs-atomic.js.map +1 -1
- package/dist/hibernation-manager.d.ts +111 -8
- package/dist/hibernation-manager.js +234 -49
- package/dist/hibernation-manager.js.map +1 -1
- package/dist/index.d.ts +13 -6
- package/dist/index.js +27 -5
- package/dist/index.js.map +1 -1
- package/dist/joined-strand-store.d.ts +177 -0
- package/dist/joined-strand-store.js +431 -0
- package/dist/joined-strand-store.js.map +1 -0
- package/dist/key-store-file.js +14 -5
- package/dist/key-store-file.js.map +1 -1
- package/dist/key-store.d.ts +6 -0
- package/dist/key-store.js.map +1 -1
- package/dist/link-budget.d.ts +367 -69
- package/dist/link-budget.js +375 -72
- package/dist/link-budget.js.map +1 -1
- package/dist/membership-connection-gater.d.ts +30 -43
- package/dist/membership-connection-gater.js +31 -42
- package/dist/membership-connection-gater.js.map +1 -1
- package/dist/node-local-snapshot.d.ts +5 -4
- package/dist/node-local-snapshot.js +6 -3
- package/dist/node-local-snapshot.js.map +1 -1
- package/dist/peer-dial.d.ts +24 -4
- package/dist/peer-dial.js +74 -5
- package/dist/peer-dial.js.map +1 -1
- package/dist/peer-join-backfill.d.ts +35 -15
- package/dist/peer-join-backfill.js +115 -55
- package/dist/peer-join-backfill.js.map +1 -1
- package/dist/push-fanout.d.ts +6 -0
- package/dist/push-fanout.js +6 -0
- package/dist/push-fanout.js.map +1 -1
- package/dist/relay-reservation.d.ts +3 -1
- package/dist/relay-reservation.js +3 -1
- package/dist/relay-reservation.js.map +1 -1
- package/dist/relay-server.d.ts +79 -0
- package/dist/relay-server.js +74 -0
- package/dist/relay-server.js.map +1 -0
- package/dist/seed-bootstrap.d.ts +57 -4
- package/dist/seed-bootstrap.js +107 -83
- package/dist/seed-bootstrap.js.map +1 -1
- package/dist/storage-scope.d.ts +33 -17
- package/dist/storage-scope.js +44 -23
- package/dist/storage-scope.js.map +1 -1
- package/dist/strand-addr-protocol.d.ts +58 -21
- package/dist/strand-addr-protocol.js +133 -85
- package/dist/strand-addr-protocol.js.map +1 -1
- package/dist/strand-database.d.ts +20 -2
- package/dist/strand-database.js +42 -5
- package/dist/strand-database.js.map +1 -1
- package/dist/strand-first-sync-gate.d.ts +59 -21
- package/dist/strand-first-sync-gate.js +58 -19
- package/dist/strand-first-sync-gate.js.map +1 -1
- package/dist/strand-formation-deadlines.d.ts +151 -0
- package/dist/strand-formation-deadlines.js +171 -0
- package/dist/strand-formation-deadlines.js.map +1 -0
- package/dist/strand-formation-manager.d.ts +63 -30
- package/dist/strand-formation-manager.js +67 -41
- package/dist/strand-formation-manager.js.map +1 -1
- package/dist/strand-formation-protocol.d.ts +45 -35
- package/dist/strand-formation-protocol.js +59 -122
- package/dist/strand-formation-protocol.js.map +1 -1
- package/dist/strand-instance-manager.d.ts +152 -6
- package/dist/strand-instance-manager.js +331 -42
- package/dist/strand-instance-manager.js.map +1 -1
- package/dist/strand-membership-writer.d.ts +6 -0
- package/dist/strand-membership-writer.js +11 -0
- package/dist/strand-membership-writer.js.map +1 -1
- package/dist/strand-peer-book-file.d.ts +12 -0
- package/dist/strand-peer-book-file.js +37 -0
- package/dist/strand-peer-book-file.js.map +1 -0
- package/dist/strand-peer-book-protocol.d.ts +168 -0
- package/dist/strand-peer-book-protocol.js +356 -0
- package/dist/strand-peer-book-protocol.js.map +1 -0
- package/dist/strand-peer-book-swap.d.ts +104 -0
- package/dist/strand-peer-book-swap.js +284 -0
- package/dist/strand-peer-book-swap.js.map +1 -0
- package/dist/strand-peer-book.d.ts +159 -0
- package/dist/strand-peer-book.js +361 -0
- package/dist/strand-peer-book.js.map +1 -0
- package/dist/strand-peer-observer.d.ts +88 -0
- package/dist/strand-peer-observer.js +180 -0
- package/dist/strand-peer-observer.js.map +1 -0
- package/dist/strand-revocation-enforcer.js +2 -1
- package/dist/strand-revocation-enforcer.js.map +1 -1
- package/dist/strand-solicitation.d.ts +36 -20
- package/dist/strand-solicitation.js +4 -4
- package/dist/strand-solicitation.js.map +1 -1
- package/dist/strand-wake-protocol.d.ts +80 -23
- package/dist/strand-wake-protocol.js +89 -64
- package/dist/strand-wake-protocol.js.map +1 -1
- package/dist/types.d.ts +181 -33
- package/dist/types.js +4 -3
- package/dist/types.js.map +1 -1
- package/package.json +11 -7
- package/src/cadre-node.ts +1316 -398
- package/src/control-authorization.ts +2 -1
- package/src/control-cohort.ts +6 -0
- package/src/control-database.ts +122 -18
- package/src/control-formation-recorder.ts +354 -305
- package/src/control-read-retry.ts +14 -12
- package/src/control-schema.ts +67 -5
- package/src/control-stream.ts +25 -1
- package/src/control-write-retry.ts +13 -2
- package/src/delegate-admission.ts +23 -13
- package/src/diagnostics/connection-path.ts +0 -7
- package/src/formation-approval.ts +610 -604
- package/src/fs-atomic.ts +14 -4
- package/src/hibernation-manager.ts +638 -437
- package/src/index.ts +91 -6
- package/src/joined-strand-store.ts +498 -0
- package/src/key-store-file.ts +14 -5
- package/src/key-store.ts +6 -0
- package/src/link-budget.ts +393 -74
- package/src/membership-connection-gater.ts +31 -45
- package/src/node-local-snapshot.ts +293 -289
- package/src/peer-dial.ts +81 -7
- package/src/peer-join-backfill.ts +120 -57
- package/src/push-fanout.ts +6 -0
- package/src/relay-reservation.ts +3 -1
- package/src/relay-server.ts +98 -0
- package/src/seed-bootstrap.ts +133 -91
- package/src/storage-scope.ts +44 -23
- package/src/strand-addr-protocol.ts +547 -458
- package/src/strand-database.ts +287 -242
- package/src/strand-first-sync-gate.ts +58 -19
- package/src/strand-formation-deadlines.ts +227 -0
- package/src/strand-formation-manager.ts +700 -647
- package/src/strand-formation-protocol.ts +103 -150
- package/src/strand-instance-manager.ts +387 -45
- package/src/strand-membership-writer.ts +12 -0
- package/src/strand-peer-book-file.ts +39 -0
- package/src/strand-peer-book-protocol.ts +466 -0
- package/src/strand-peer-book-swap.ts +338 -0
- package/src/strand-peer-book.ts +475 -0
- package/src/strand-peer-observer.ts +235 -0
- package/src/strand-revocation-enforcer.ts +2 -1
- package/src/strand-solicitation.ts +519 -500
- package/src/strand-wake-protocol.ts +401 -355
- package/src/types.ts +185 -33
package/src/key-store-file.ts
CHANGED
|
@@ -45,19 +45,28 @@ function encodeKeyId(keyId: KeyId): string {
|
|
|
45
45
|
|
|
46
46
|
/**
|
|
47
47
|
* Reverse {@link encodeKeyId}: strip the slot suffix and percent-decode. Returns
|
|
48
|
-
* `undefined` for a `.key` filename this store did not write
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
48
|
+
* `undefined` for a `.key` filename this store did not write, so
|
|
49
|
+
* {@link FileKeyStore.list} can skip it rather than letting one undecodable
|
|
50
|
+
* entry throw and break enumeration of every real slot. That includes a name
|
|
51
|
+
* that decodes but is not the one {@link encodeKeyId} produces for the result
|
|
52
|
+
* (e.g. a slot written before uppercase letters were escaped):
|
|
53
|
+
* {@link FileKeyStore.get} looks only at the canonical name, so listing it
|
|
54
|
+
* would name a key id that reads back as absent.
|
|
52
55
|
*/
|
|
53
56
|
function decodeKeyId(fileName: string): KeyId | undefined {
|
|
54
57
|
const encoded = fileName.slice(0, -SLOT_SUFFIX.length);
|
|
58
|
+
let keyId: KeyId;
|
|
55
59
|
try {
|
|
56
|
-
|
|
60
|
+
keyId = decodeURIComponent(encoded);
|
|
57
61
|
} catch (error) {
|
|
58
62
|
log('FileKeyStore: skipping undecodable slot filename %s: %o', fileName, error);
|
|
59
63
|
return undefined;
|
|
60
64
|
}
|
|
65
|
+
if (encodeKeyId(keyId) !== encoded) {
|
|
66
|
+
log('FileKeyStore: skipping non-canonical slot filename %s', fileName);
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
return keyId;
|
|
61
70
|
}
|
|
62
71
|
|
|
63
72
|
/**
|
package/src/key-store.ts
CHANGED
|
@@ -32,6 +32,12 @@ export const DEFAULT_IDENTITY_KEY_ID: KeyId = 'cadre/identity';
|
|
|
32
32
|
* Key material is raw bytes (e.g. libp2p protobuf-serialized private keys).
|
|
33
33
|
* Text-only backends encode/decode (base64) internally; byte-native backends
|
|
34
34
|
* (LevelDB) store as-is.
|
|
35
|
+
*
|
|
36
|
+
* Not only keys: `KeyStoreJoinedStrandStore` keeps one small UTF-8 JSON record
|
|
37
|
+
* (about 200 bytes, a closed strand's read secret among its fields) per strand
|
|
38
|
+
* this node joined from another party, under ids beginning
|
|
39
|
+
* `cadre/joined-strand/`. {@link list} therefore returns those ids too, and a
|
|
40
|
+
* backend must accept `/` in an id and values of that size.
|
|
35
41
|
*/
|
|
36
42
|
export interface KeyStore {
|
|
37
43
|
/** Resolves to the stored material, or undefined if the slot is empty. */
|
package/src/link-budget.ts
CHANGED
|
@@ -10,13 +10,38 @@
|
|
|
10
10
|
* budget changes ONE declaration, and the next person to add a dial writes down its round-trip
|
|
11
11
|
* count beside the arithmetic.
|
|
12
12
|
*
|
|
13
|
+
* This module is the only place a new deadline over the link should be written, and in
|
|
14
|
+
* cadre-core's source `yarn lint` enforces it (`LINK_DEADLINE_GUARD` in `eslint.config.mjs`): a
|
|
15
|
+
* value named `…TIMEOUT_MS`, `…BUDGET_MS` or `…DEADLINE_MS` (or `…TimeoutMs` and the like) set
|
|
16
|
+
* to a number rather than a derivation fails, unless the line above it gives one of four
|
|
17
|
+
* reasons. `// eslint-disable-next-line no-restricted-syntax -- link-independent: <why>` keeps a
|
|
18
|
+
* deadline that never waits on the link; `-- link-bound, not yet derived: <ticket slug>` marks
|
|
19
|
+
* one that does and names the ticket that owns converting it. Grep for the second to list the
|
|
20
|
+
* deadlines this module does not yet cover. `-- cuts off by design: <why>; see
|
|
21
|
+
* docs/cadre-consistency.md → "Deadlines Over Optimystic's Reads and Commits"` keeps a deadline
|
|
22
|
+
* that does wait on the link, through an Optimystic read or commit, but whose value is what its
|
|
23
|
+
* caller can tolerate: it gives up on purpose and must not grow with the link. That doc section
|
|
24
|
+
* lists every such site. `-- measured, not derived: <where the measurement lives>` keeps a
|
|
25
|
+
* deadline that waits on the link but has no round-trip count to derive from, because it bounds
|
|
26
|
+
* a whole phase of many exchanges whose number depends on the data (a joining machine's first
|
|
27
|
+
* sync): its value is sized from a recorded measurement, which the directive names, and it is
|
|
28
|
+
* re-measured rather than recomputed when a number under it moves.
|
|
29
|
+
*
|
|
30
|
+
* Optimystic's per-peer cohort read deadline is derived here too ({@link cohortReadDeadlineMs}),
|
|
31
|
+
* but declared in another package: `COHORT_READ_DEADLINE_MS` in
|
|
32
|
+
* `quereus-plugin-sereus/src/cluster-size.ts` is the same arithmetic at the default declaration,
|
|
33
|
+
* spelled as a number because that package cannot import this one. `link-budget.spec.ts` pins
|
|
34
|
+
* the two equal.
|
|
35
|
+
*
|
|
13
36
|
* ── The instrument ──
|
|
14
37
|
*
|
|
15
|
-
* Every count and every number below comes from
|
|
38
|
+
* Every count and every number below, except the commit count, comes from
|
|
16
39
|
* `packages/integration-tests/src/scenarios/relayed-dial-cost-by-latency.integration.ts`, which
|
|
17
40
|
* is committed and opt-in (`RELAY_DIAL_COST=1`). **Its doc comment is the single home of the
|
|
18
41
|
* measurement**; this module holds only what the derivation needs. Re-run it before changing
|
|
19
|
-
* anything here.
|
|
42
|
+
* anything here. The commit count ({@link COMMIT_ROUND_TRIPS}) is a whole-operation
|
|
43
|
+
* measurement from `relay-round-trip-measure.integration.ts` instead, and its figures live on
|
|
44
|
+
* the constant.
|
|
20
45
|
*
|
|
21
46
|
* ── The unit ──
|
|
22
47
|
*
|
|
@@ -39,6 +64,15 @@
|
|
|
39
64
|
* | dial the relay itself | 1 | 3 500 ms |
|
|
40
65
|
* | request a reservation on an open relay connection | 1 | 3 500 ms |
|
|
41
66
|
* | negotiate a protocol over an established circuit | 1 | 3 500 ms |
|
|
67
|
+
* | one request and its answer over an open circuit | 2 | 7 000 ms |
|
|
68
|
+
* | open a relayed connection, then negotiate one protocol| 5 | 17 500 ms |
|
|
69
|
+
* | open a relayed connection, then one request on it | 6 | 21 000 ms |
|
|
70
|
+
* | one Optimystic commit over connections already open | 20 | 70 000 ms |
|
|
71
|
+
*
|
|
72
|
+
* The reservation-request row is {@link RELAY_RESERVE_REQUEST_ROUND_TRIPS}, which also sizes how long
|
|
73
|
+
* a party-run relay waits for a stranger's reservation ({@link relayAdmissionReserveDeadlineMs}).
|
|
74
|
+
* The commit row is a measurement of a whole write, not a count of exchanges; its doc comment
|
|
75
|
+
* ({@link COMMIT_ROUND_TRIPS}) holds the figures and says when to re-measure it.
|
|
42
76
|
*
|
|
43
77
|
* **Measured** 2026-09-26, one Windows machine, loopback dedicated relay: a relayed dial took
|
|
44
78
|
* 20-25 ms at no delay, **7 255-7 279 ms at 900 ms one-way**, and **12 066-12 094 ms at 1 500 ms
|
|
@@ -48,42 +82,98 @@
|
|
|
48
82
|
* supports rather than matching it exactly: a declaration equal to the supported round trip
|
|
49
83
|
* would derive a budget marginally BELOW the dial it has to contain.
|
|
50
84
|
*
|
|
51
|
-
* ──
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
85
|
+
* ── The listener's admission decision ──
|
|
86
|
+
*
|
|
87
|
+
* The table is link time only. Every budget that OPENS a connection also adds
|
|
88
|
+
* {@link ADMISSION_DECISION_TIMEOUT_MS} (2 000 ms) per admission decision the machine being
|
|
89
|
+
* called may make on the way, because that machine's gate runs on the dialer's clock (the
|
|
90
|
+
* constant's doc says why). So a relayed dial is budgeted at 14 000 + 2 000 = 16 000 ms at the
|
|
91
|
+
* default declaration, a dial plus one request at 23 000 ms, and a reservation drive, which a
|
|
92
|
+
* party-run relay decides twice, at 18 000 ms. The allowance is flat: it is local decision
|
|
93
|
+
* time, not link time, so it does not scale with the declaration.
|
|
94
|
+
*
|
|
95
|
+
* The measurement above crossed no gate. The instrument is bare libp2p on both sides, with no
|
|
96
|
+
* cadre connection gater, so the 12 094 ms contains no admission decision at all. The
|
|
97
|
+
* allowance is therefore on top of the measured dial rather than already inside it.
|
|
98
|
+
*
|
|
99
|
+
* NOTE: the measured dial also reused a connection to the relay that the dialer already held
|
|
100
|
+
* (the instrument dials the relay first). A relayed dial that has to open that connection itself
|
|
101
|
+
* spends one more link round trip, plus the relay's own admission decision when the relay is a
|
|
102
|
+
* party-run control node, and the count counts neither; at the supported link such a dial fits
|
|
103
|
+
* only while the called machine decides in under about 0.9 s.
|
|
104
|
+
* `bug-relayed-dial-budget-omits-opening-the-relay-connection`.
|
|
105
|
+
*
|
|
106
|
+
* ── Optimystic's deadlines, from the same declaration ──
|
|
107
|
+
*
|
|
108
|
+
* cadre states its declared link to Optimystic on the control node and every strand node
|
|
109
|
+
* (`@optimystic/db-p2p`'s `NodeOptions.linkRoundTripMs`, from 1.8.0), and Optimystic derives
|
|
110
|
+
* its network deadlines from it (`resolveLinkDeadlines` in its `rpc-deadline.ts`), each never
|
|
111
|
+
* below the LAN value it replaced. At the default declaration:
|
|
112
|
+
*
|
|
113
|
+
* | Optimystic deadline | link round trips | at the default | undeclared |
|
|
114
|
+
* | ---------------------------------------------------- | ---------------- | -------------- | ---------- |
|
|
115
|
+
* | libp2p `dialTimeout` and `inboundUpgradeTimeout` | 5 | 17 500 ms | 10 000 ms |
|
|
116
|
+
* | one request's dial, stream negotiation included | 6 | 21 000 ms | 3 000 ms |
|
|
117
|
+
* | one request's response | 3 | 10 500 ms | 10 000 ms |
|
|
118
|
+
* | a rebalance transfer's dial, and again its reply | 6, floor 30 000 | 30 000 ms | 30 000 ms |
|
|
119
|
+
*
|
|
120
|
+
* Its churn and under-replication pushes take the request rows' values. The undeclared column
|
|
121
|
+
* is why cadre always states the value, default included. A fixed 3 000 ms request dial cannot
|
|
122
|
+
* open a relayed connection above 375 ms one-way, and it also bounded the protocol negotiation
|
|
123
|
+
* on a connection that was already open, which is one link round trip: at the supported link
|
|
124
|
+
* every cohort consult was cut off at 3.0 s, before
|
|
125
|
+
* {@link cohortReadDeadlineMs} was reached (measured 2026-09-29, on 1.7.0). From 1.8.0 a cohort
|
|
126
|
+
* consult runs under the per-peer read deadline alone. Optimystic would derive that deadline
|
|
127
|
+
* from the declaration too, at three round trips; cadre states its own, at two
|
|
128
|
+
* ({@link cohortReadDeadlineMs} says why).
|
|
129
|
+
*
|
|
130
|
+
* NOTE: the longer deadlines are paid against a machine that is gone. An Optimystic request
|
|
131
|
+
* to a peer that never completes a connection now waits up to the 21 000 ms dial deadline where
|
|
132
|
+
* it waited 3 000, and one that connects and never answers waits 10 500 where it waited 10 000.
|
|
133
|
+
* Measured 2026-09-30 (`control-write-degraded-cohort-member.integration.ts`): a control write
|
|
134
|
+
* against a connected, silent member failed at 42.2 s, against 40.2 s with no link declared.
|
|
135
|
+
* The dial case was not measured. cadre's cut-off budgets (`CONTROL_WRITE_RETRY_BUDGET_MS`,
|
|
136
|
+
* `CONTROL_READ_RETRY_BUDGET_MS`) are checked between attempts, so one attempt can outlast
|
|
137
|
+
* them by that much. If writes on a fast link are seen waiting out the dial deadline against an
|
|
138
|
+
* unreachable machine, declare that deployment's real round trip instead of the default.
|
|
139
|
+
*
|
|
140
|
+
* libp2p's two limits bound the same relayed dial as cadre's budgets do: the DIALER's
|
|
141
|
+
* `dialTimeout`, which bounds every dial that carries no abort signal of its own, and the
|
|
142
|
+
* LISTENER's `inboundUpgradeTimeout`, which is how long the machine being called lets a
|
|
143
|
+
* half-built connection finish its handshakes. At libp2p's 10 000 ms, no relayed connection
|
|
144
|
+
* could be opened above about 1 250 ms one-way (a 2.5-second link round trip) whatever cadre
|
|
145
|
+
* declared, and the listener's limit is the one that makes that failure silent: at 1 500 ms
|
|
146
|
+
* one-way the dialer's own 12 094 ms dial RESOLVED while the listener had already thrown the
|
|
147
|
+
* connection away at 10 s, so the first stream over it died with `Unexpected EOF - stream
|
|
148
|
+
* closed while reading 0/1 bytes` and the listener reported no peer at all. Optimystic's
|
|
149
|
+
* derivation contains {@link relayedDialBudgetMs} at every declaration: five round trips are at
|
|
150
|
+
* least four plus the {@link ADMISSION_DECISION_TIMEOUT_MS} allowance once the round trip
|
|
151
|
+
* reaches 2 000 ms, and the 10 000 ms floor covers every shorter one. `link-budget.spec.ts` pins
|
|
152
|
+
* that against Optimystic's own derivation, so a change on either side that breaks it fails
|
|
153
|
+
* there rather than silently on a slow link.
|
|
154
|
+
*
|
|
155
|
+
* NOTE: derived from the declaration alone, so a per-field override that raises a cadre dial
|
|
156
|
+
* budget above it (`controlCohort.perAddressDialTimeoutMs`, `strandBackfill.dialTimeoutMs`)
|
|
157
|
+
* does not raise the listener's limit; a dial that outlasts it gets the silent failure again.
|
|
158
|
+
* If such overrides start being used for slow links, raise the declaration instead.
|
|
65
159
|
*
|
|
66
160
|
* ── What still fails at the supported link ──
|
|
67
161
|
*
|
|
68
|
-
* - **Optimystic's own request dials.** `@optimystic/db-p2p`'s RPC clients dial with fixed
|
|
69
|
-
* 3 000 ms deadlines of their own (`DEFAULT_DIAL_TIMEOUT_MS`, the `pushDialTimeoutMs`
|
|
70
|
-
* defaults), which neither limit above reaches because a caller's signal replaces
|
|
71
|
-
* `dialTimeout`. So an Optimystic request that has to OPEN a relayed connection fails above
|
|
72
|
-
* 375 ms one-way. A request over a connection that is already open does not dial, and the
|
|
73
|
-
* connections cadre opens itself are budgeted here. Upstream:
|
|
74
|
-
* `debt-rpc-dial-deadlines-cannot-open-a-slow-relayed-connection` in optimystic.
|
|
75
|
-
* - **Cadre deadlines still typed as milliseconds.** The strand wake, strand-address and seed
|
|
76
|
-
* delivery deadlines, and the cohort read deadline, bound exchanges over the same link but do
|
|
77
|
-
* not derive from it: `tickets/backlog/debt-three-more-dial-deadlines-ignore-the-declared-link`.
|
|
78
162
|
* - **A machine that declares a faster link than its peers.** Every machine is the listener
|
|
79
|
-
* for the others, so its `inboundUpgradeTimeout` —
|
|
80
|
-
* connections other machines open to it. A peer declaring 3 500 ms
|
|
81
|
-
* declared 500 ms gets exactly the silent failure above
|
|
82
|
-
* machine
|
|
163
|
+
* for the others, so its `inboundUpgradeTimeout` — which Optimystic derives from ITS
|
|
164
|
+
* declaration — bounds connections other machines open to it. A peer declaring 3 500 ms
|
|
165
|
+
* dialing a machine that declared 500 ms gets exactly the silent failure above, once its
|
|
166
|
+
* dial outlasts that machine's 10 000 ms floor. Strand formation has the same
|
|
167
|
+
* shape: the joiner derives how long it waits for the host's reply from ITS declaration and
|
|
168
|
+
* the host derives its provisioning budget from its own, so a joiner declaring a faster
|
|
169
|
+
* link than the host gives up on a reply the host is still entitled to send. Declare the
|
|
170
|
+
* same link on every machine of a party.
|
|
171
|
+
* Relay admission too: a party-run relay derives how long a stranger has to ask for a
|
|
172
|
+
* reservation from ITS declaration ({@link relayAdmissionReserveDeadlineMs}), so a relay
|
|
173
|
+
* declaring a faster link than a client closes that client's connection before its request
|
|
174
|
+
* arrives.
|
|
83
175
|
*/
|
|
84
176
|
|
|
85
|
-
import type { Libp2pConnectionTimeouts } from '@optimystic/db-p2p';
|
|
86
|
-
|
|
87
177
|
/**
|
|
88
178
|
* The link round trip cadre assumes when a host declares none, in milliseconds. A deployment
|
|
89
179
|
* that knows its own link states it with `NetworkConfig.linkRoundTripMs` instead.
|
|
@@ -93,18 +183,22 @@ import type { Libp2pConnectionTimeouts } from '@optimystic/db-p2p';
|
|
|
93
183
|
* `docs/architecture.md` → "Relay Integration"). The worst relayed dial measured at that link
|
|
94
184
|
* was 12 094 ms, above the 12 000 ms a declaration of exactly 3 000 would derive, so the
|
|
95
185
|
* declaration rounds the supported link up to the next half second. 3 500 derives 14 000 ms
|
|
96
|
-
* and leaves about 1.9 s over that dial: room for a phone's Noise handshake,
|
|
97
|
-
* loopback measurement on a desktop does not contain.
|
|
98
|
-
*
|
|
186
|
+
* of link time and leaves about 1.9 s over that dial: room for a phone's Noise handshake,
|
|
187
|
+
* which the loopback measurement on a desktop does not contain. The listener's admission
|
|
188
|
+
* decision is not in that 1.9 s; it has its own allowance on top (the module doc's "The
|
|
189
|
+
* listener's admission decision"). (The previous 2 000 followed the same convention for the
|
|
190
|
+
* 1.8 s band sereus supported before, with about 720 ms to spare.)
|
|
99
191
|
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
192
|
+
* One budget outside this module does NOT move with it: `DEFAULT_STRAND_FIRST_SYNC_TIMEOUT_MS`
|
|
193
|
+
* (`strand-first-sync-gate.ts`) bounds a whole first sync, which has no round-trip count, so it
|
|
194
|
+
* is sized from a measurement instead and re-measured when this declaration or the cohort read
|
|
195
|
+
* deadline moves. The per-peer cohort read deadline itself does move with it
|
|
196
|
+
* ({@link cohortReadDeadlineMs}).
|
|
104
197
|
*
|
|
105
198
|
* What 3 500 costs, against 2 000: a peer that is genuinely gone holds each operation longer
|
|
106
|
-
* before it is abandoned and retried — a relayed dial or a reservation drive
|
|
107
|
-
* 8 s, and a control-cohort dial of one peer across all its addresses 56 s
|
|
199
|
+
* before it is abandoned and retried — the link part of a relayed dial or a reservation drive
|
|
200
|
+
* 14 s instead of 8 s, and of a control-cohort dial of one peer across all its addresses 56 s
|
|
201
|
+
* instead of 32 s. The admission allowance adds to both (see {@link relayedDialBudgetMs}).
|
|
108
202
|
* Raise it further for a link slower still; lower it only on a deployment where EVERY machine
|
|
109
203
|
* of the party is that close, for the reason the "What still fails" list gives.
|
|
110
204
|
*/
|
|
@@ -130,14 +224,75 @@ export const RELAYED_DIAL_ROUND_TRIPS = 4;
|
|
|
130
224
|
*/
|
|
131
225
|
export const RELAY_RESERVATION_ROUND_TRIPS = 4;
|
|
132
226
|
|
|
227
|
+
/**
|
|
228
|
+
* Link round trips from the moment a party-run relay admits a stranger's connection for relay
|
|
229
|
+
* use to the moment that stranger's RESERVE request reaches it. libp2p's reservation store
|
|
230
|
+
* (`@libp2p/circuit-relay-v2`'s `addRelay`) opens the hop stream the instant its dial resolves,
|
|
231
|
+
* without waiting for identify, so:
|
|
232
|
+
*
|
|
233
|
+
* 1. The client's last Noise handshake message and its hop-stream open arrive at the relay
|
|
234
|
+
* together. The relay's gate runs on the handshake message (and arms the reserve deadline),
|
|
235
|
+
* then the relay answers the protocol negotiation.
|
|
236
|
+
* 2. The answer reaches the client and the client's RESERVE request reaches the relay: one round
|
|
237
|
+
* trip on the client-to-relay hop, which is the whole link round trip when all of the link's
|
|
238
|
+
* delay is on the client's own hop (a phone on a congested mobile link).
|
|
239
|
+
*
|
|
240
|
+
* The relay's decision on that request is local time, not link time;
|
|
241
|
+
* {@link relayAdmissionReserveDeadlineMs} adds it separately.
|
|
242
|
+
*/
|
|
243
|
+
export const RELAY_RESERVE_REQUEST_ROUND_TRIPS = 1;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Link round trips negotiating one protocol over an ALREADY-OPEN connection costs: the
|
|
247
|
+
* multistream-select exchange that `dialProtocol` / `newStream` runs before the stream is
|
|
248
|
+
* handed back. Measured at 2 one-way delays.
|
|
249
|
+
*/
|
|
250
|
+
export const PROTOCOL_NEGOTIATION_ROUND_TRIPS = 1;
|
|
251
|
+
|
|
133
252
|
/**
|
|
134
253
|
* Link round trips one request-and-answer over an ALREADY-OPEN circuit costs: the protocol
|
|
135
|
-
* negotiation (
|
|
136
|
-
* not include a dial — a caller that may have to open the connection budgets
|
|
254
|
+
* negotiation ({@link PROTOCOL_NEGOTIATION_ROUND_TRIPS}) plus the request and its response (1).
|
|
255
|
+
* It does not include a dial — a caller that may have to open the connection budgets
|
|
137
256
|
* {@link RELAYED_DIAL_ROUND_TRIPS} separately, which is exactly what the two-field shape of
|
|
138
257
|
* Optimystic's `dialTimeoutMs` / `responseTimeoutMs` pair is for.
|
|
139
258
|
*/
|
|
140
|
-
export const CIRCUIT_REQUEST_ROUND_TRIPS =
|
|
259
|
+
export const CIRCUIT_REQUEST_ROUND_TRIPS = PROTOCOL_NEGOTIATION_ROUND_TRIPS + 1;
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Link round trips one Optimystic commit is budgeted for, over connections that are ALREADY
|
|
263
|
+
* open. **Measured, not counted**: a commit is many sequential exchanges whose exact number
|
|
264
|
+
* the transactor decides, so this is a whole-operation figure and it depends on the Optimystic
|
|
265
|
+
* version.
|
|
266
|
+
*
|
|
267
|
+
* Measured 2026-09-23 at optimystic `9e5c1e85` with
|
|
268
|
+
* `packages/integration-tests/src/scenarios/relay-round-trip-measure.integration.ts`,
|
|
269
|
+
* `delayed` configuration (`RELAY_RRT_MEASURE=1 RELAY_RRT_CONFIG=delayed`: a 150 ms one-way
|
|
270
|
+
* delay on one party's link, so a 300 ms link round trip): one strand insert took 3.8–6.1 s
|
|
271
|
+
* (the founder 4.46, 4.43, 3.84 s; the joiner 5.11, 5.09, 6.09 s), which is up to 20.3 link
|
|
272
|
+
* round trips, including about 0.1–0.2 s of local work (the same inserts on an undelayed
|
|
273
|
+
* loopback took 57–165 ms). A static count of the write path agrees on the order: three repo
|
|
274
|
+
* messages through three consensus rounds each, nine sequential requests of a negotiation plus
|
|
275
|
+
* a request, so 18.
|
|
276
|
+
*
|
|
277
|
+
* Re-measured 2026-09-29 on `@optimystic/*` 1.8.0, same scenario and configuration: the founder
|
|
278
|
+
* 3.26-3.94 s and the joiner 4.45-5.17 s over three inserts each, so at most 17.2 link round
|
|
279
|
+
* trips. The count stands.
|
|
280
|
+
*
|
|
281
|
+
* It measures a strand insert; a control-database insert runs the same network transactor
|
|
282
|
+
* against the responder's own party's machines, which the module doc already says must
|
|
283
|
+
* declare the same link. Re-measure with that scenario, rather than recompute, whenever
|
|
284
|
+
* `@optimystic/*` is bumped.
|
|
285
|
+
*/
|
|
286
|
+
export const COMMIT_ROUND_TRIPS = 20;
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Link round trips one request-and-answer costs when the connection may first have to be OPENED,
|
|
290
|
+
* possibly through a relay: {@link RELAYED_DIAL_ROUND_TRIPS} + {@link CIRCUIT_REQUEST_ROUND_TRIPS}.
|
|
291
|
+
* The shape of cadre's own one-frame control protocols (strand wake, strand address, seed
|
|
292
|
+
* delivery), each of which bounds the dial and the exchange with ONE deadline. Link time only:
|
|
293
|
+
* {@link relayedRequestBudgetMs} adds the listener's admission allowance on top.
|
|
294
|
+
*/
|
|
295
|
+
export const RELAYED_REQUEST_ROUND_TRIPS = RELAYED_DIAL_ROUND_TRIPS + CIRCUIT_REQUEST_ROUND_TRIPS;
|
|
141
296
|
|
|
142
297
|
/**
|
|
143
298
|
* What a block-transfer push message's BYTES get to cross, on top of the latency
|
|
@@ -156,6 +311,60 @@ export const CIRCUIT_REQUEST_ROUND_TRIPS = 2;
|
|
|
156
311
|
*/
|
|
157
312
|
export const PUSH_TRANSFER_ALLOWANCE_MS = 6000;
|
|
158
313
|
|
|
314
|
+
/**
|
|
315
|
+
* Deadline for one admission decision, after which the fail-open outcome is
|
|
316
|
+
* used (connection admitted / reservation admitted). Both inbound gaters use
|
|
317
|
+
* it: the control node's membership gate (`membership-connection-gater.ts`),
|
|
318
|
+
* and the closed-strand revoked-peer gate (`strand-revocation-enforcer.ts`).
|
|
319
|
+
* It lives here, not beside the gaters, because the dial budgets below add it
|
|
320
|
+
* and this module imports nothing of cadre-core's own: importing the gater
|
|
321
|
+
* would close the cycle link-budget → membership-connection-gater →
|
|
322
|
+
* seed-bootstrap → link-budget.
|
|
323
|
+
*
|
|
324
|
+
* libp2p awaits `denyInboundEncryptedConnection` inside the inbound upgrade
|
|
325
|
+
* WITHOUT racing its inbound-upgrade timeout signal (unlike the pre-encryption
|
|
326
|
+
* `denyInboundConnection` hook), so a decision that never settles wedges that
|
|
327
|
+
* upgrade forever — the connection-manager's inbound-upgrade slot is taken by
|
|
328
|
+
* `acceptIncomingConnection` and released in the `finally` that never runs. The
|
|
329
|
+
* real policy reads the control DB (`listAuthorizedMembers`), which can pull
|
|
330
|
+
* over the network, so "never settles" is reachable. Bounding it keeps the
|
|
331
|
+
* fail-open contract honest: a slow decision admits rather than silently
|
|
332
|
+
* failing closed (or not at all).
|
|
333
|
+
*
|
|
334
|
+
* The control gate's decision waits on the link, through two live control reads
|
|
335
|
+
* (`Revocation`, then `CadrePeer`) that can consult the cohort, yet this deadline
|
|
336
|
+
* cuts it off on purpose and must not grow with the link:
|
|
337
|
+
*
|
|
338
|
+
* - **Fail-open is the designed outcome.** An expired decision admits a
|
|
339
|
+
* connection and nothing more: the per-protocol stream gates still refuse
|
|
340
|
+
* every members-only protocol from the materialized snapshot, and unplaced
|
|
341
|
+
* relay reservations stay capped (`MAX_UNAUTHORIZED_RELAY_RESERVATIONS`).
|
|
342
|
+
* - **The slow case is bring-up, not steady state.** A membership read consults
|
|
343
|
+
* the cohort only for a block this node does not hold, or before the
|
|
344
|
+
* `Revocation` ledger marker exists; once the marker exists every block the
|
|
345
|
+
* decision reads is held (`control-founding-consult-budget.spec.ts` pins both
|
|
346
|
+
* states). So the gate always fails open only on a node whose membership reads
|
|
347
|
+
* still consult: one consult costs about two link round trips, and asking a
|
|
348
|
+
* silent peer costs the per-peer read deadline ({@link cohortReadDeadlineMs}, 7 s
|
|
349
|
+
* at the default declaration).
|
|
350
|
+
* - **The decision is spent on the dialing machine's clock.** libp2p's listener
|
|
351
|
+
* runs this gate before it answers the multiplexer negotiation the dialer is
|
|
352
|
+
* waiting on (`libp2p/dist/src/upgrader.js`), and the listener's own
|
|
353
|
+
* `inboundUpgradeTimeout` is already running while it does. So
|
|
354
|
+
* {@link relayedDialBudgetMs}, and every budget built on it, adds this
|
|
355
|
+
* deadline once per decision the called machine may make, and the two libp2p
|
|
356
|
+
* limits Optimystic derives from the same declaration contain it (the module
|
|
357
|
+
* doc's "Optimystic's deadlines, from the same declaration").
|
|
358
|
+
* Raising it therefore lengthens every dial budget with it: a slow decision
|
|
359
|
+
* still fits, and a dial to a peer that is gone takes that much longer to
|
|
360
|
+
* give up.
|
|
361
|
+
*
|
|
362
|
+
* `CONTROL_READ_RETRY_BUDGET_MS` must stay below this, which
|
|
363
|
+
* `control-read-retry.spec.ts` pins.
|
|
364
|
+
*/
|
|
365
|
+
// eslint-disable-next-line no-restricted-syntax -- cuts off by design: an expired decision admits a connection only, and the dial budgets add it as a flat allowance; see docs/cadre-consistency.md → "Deadlines Over Optimystic's Reads and Commits"
|
|
366
|
+
export const ADMISSION_DECISION_TIMEOUT_MS = 2_000;
|
|
367
|
+
|
|
159
368
|
/**
|
|
160
369
|
* Resolve a host's declared link round trip, falling back to
|
|
161
370
|
* {@link DECLARED_LINK_ROUND_TRIP_MS}.
|
|
@@ -186,48 +395,77 @@ export function resolveLinkRoundTripMs(linkRoundTripMs?: number): number {
|
|
|
186
395
|
|
|
187
396
|
/**
|
|
188
397
|
* Deadline for OPENING a connection to another machine that may only be reachable through a
|
|
189
|
-
* relay: {@link RELAYED_DIAL_ROUND_TRIPS} at the declared link round trip
|
|
398
|
+
* relay: {@link RELAYED_DIAL_ROUND_TRIPS} at the declared link round trip, plus one
|
|
399
|
+
* {@link ADMISSION_DECISION_TIMEOUT_MS} for the called machine's inbound gate, which runs on
|
|
400
|
+
* this dial's clock. 4 x 3 500 + 2 000 = 16 000 ms at the default declaration.
|
|
401
|
+
*
|
|
402
|
+
* What the allowance costs: a peer that is truly gone holds each dial 2 s longer before it is
|
|
403
|
+
* abandoned — a per-address dial 16 s instead of 14 s, a control-cohort dial of one peer across
|
|
404
|
+
* its 4 addresses 64 s instead of 56 s, and a wake call's two attempts 46 s instead of 42 s. A
|
|
405
|
+
* machine that runs no gate (the dedicated relay container, an open-strand node) gets the same
|
|
406
|
+
* budget; the extra 2 s only lengthens a failure there, never a success.
|
|
407
|
+
*
|
|
408
|
+
* A per-field override of a deadline derived from this one
|
|
409
|
+
* (`network.controlCohort.perAddressDialTimeoutMs`, `strandBackfill.dialTimeoutMs`,
|
|
410
|
+
* `DialWakeOptions.timeoutMs`, …) replaces the whole derived value, allowance included.
|
|
190
411
|
*/
|
|
191
412
|
export function relayedDialBudgetMs(linkRoundTripMs?: number): number {
|
|
192
|
-
return RELAYED_DIAL_ROUND_TRIPS * resolveLinkRoundTripMs(linkRoundTripMs);
|
|
413
|
+
return RELAYED_DIAL_ROUND_TRIPS * resolveLinkRoundTripMs(linkRoundTripMs) + ADMISSION_DECISION_TIMEOUT_MS;
|
|
193
414
|
}
|
|
194
415
|
|
|
195
416
|
/**
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
*
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
* stalls its handshake holds that half-built connection 14 s instead of 10 s.
|
|
213
|
-
*
|
|
214
|
-
* NOTE: derived from the declaration alone, so a per-field override that raises a cadre dial
|
|
215
|
-
* budget above it (`controlCohort.perAddressDialTimeoutMs`, `strandBackfill.dialTimeoutMs`)
|
|
216
|
-
* does not raise these; a dial that outlasts the listener's limit gets the silent failure
|
|
217
|
-
* again. If such overrides start being used for slow links, raise the declaration instead or
|
|
218
|
-
* take the largest configured dial budget here.
|
|
417
|
+
* Deadline for what `dialProtocol` does end to end: OPEN a possibly relayed connection
|
|
418
|
+
* ({@link relayedDialBudgetMs}, admission allowance included) and then NEGOTIATE one protocol
|
|
419
|
+
* on it ({@link PROTOCOL_NEGOTIATION_ROUND_TRIPS} at the declared link round trip). 16 000 +
|
|
420
|
+
* 3 500 = 19 500 ms at the default declaration. The strand formation dial is bounded by this;
|
|
421
|
+
* the one-frame control exchanges bound the negotiation together with their request instead
|
|
422
|
+
* ({@link relayedRequestBudgetMs}).
|
|
423
|
+
*/
|
|
424
|
+
export function relayedStreamOpenBudgetMs(linkRoundTripMs?: number): number {
|
|
425
|
+
return relayedDialBudgetMs(linkRoundTripMs) + PROTOCOL_NEGOTIATION_ROUND_TRIPS * resolveLinkRoundTripMs(linkRoundTripMs);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Deadline for one Optimystic commit over connections that are already open:
|
|
430
|
+
* {@link COMMIT_ROUND_TRIPS} at the declared link round trip, 70 000 ms at the default
|
|
431
|
+
* declaration. No admission allowance: a commit dials nothing, so no called machine decides
|
|
432
|
+
* on its clock.
|
|
219
433
|
*/
|
|
220
|
-
export function
|
|
221
|
-
|
|
222
|
-
return { dialTimeout: budgetMs, inboundUpgradeTimeout: budgetMs };
|
|
434
|
+
export function commitBudgetMs(linkRoundTripMs?: number): number {
|
|
435
|
+
return COMMIT_ROUND_TRIPS * resolveLinkRoundTripMs(linkRoundTripMs);
|
|
223
436
|
}
|
|
224
437
|
|
|
225
438
|
/**
|
|
226
439
|
* Deadline for one whole relay reservation drive: {@link RELAY_RESERVATION_ROUND_TRIPS} at the
|
|
227
|
-
* declared link round trip.
|
|
440
|
+
* declared link round trip, plus two {@link ADMISSION_DECISION_TIMEOUT_MS}.
|
|
441
|
+
* 4 x 3 500 + 2 x 2 000 = 18 000 ms at the default declaration.
|
|
442
|
+
*
|
|
443
|
+
* Two, because a party-run relay is a control node and decides twice on the drive's clock:
|
|
444
|
+
* once for the connection (`denyInboundEncryptedConnection`), then again for the reservation
|
|
445
|
+
* (`denyInboundRelayReservation`), each under the same deadline. The count's slack of 2 round
|
|
446
|
+
* trips over the measured protocol work happens to cover both at the default declaration, but
|
|
447
|
+
* not at a declared round trip under 2 000 ms; counting them makes the budget hold at every
|
|
448
|
+
* declaration.
|
|
228
449
|
*/
|
|
229
450
|
export function relayReservationBudgetMs(linkRoundTripMs?: number): number {
|
|
230
|
-
return RELAY_RESERVATION_ROUND_TRIPS * resolveLinkRoundTripMs(linkRoundTripMs);
|
|
451
|
+
return RELAY_RESERVATION_ROUND_TRIPS * resolveLinkRoundTripMs(linkRoundTripMs) + 2 * ADMISSION_DECISION_TIMEOUT_MS;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* How long a party-run relay keeps a connection it admitted only for relay use
|
|
456
|
+
* (`membership-connection-gater.ts`'s `'admit-for-relay'`) before closing it, unless a
|
|
457
|
+
* reservation is admitted on it first: {@link RELAY_RESERVE_REQUEST_ROUND_TRIPS} at the declared
|
|
458
|
+
* link round trip, plus one {@link ADMISSION_DECISION_TIMEOUT_MS} for the relay's own decision on
|
|
459
|
+
* the reservation (`denyInboundRelayReservation`), which disarms the timer only once it settles.
|
|
460
|
+
* 3 500 + 2 000 = 5 500 ms at the default declaration.
|
|
461
|
+
*
|
|
462
|
+
* The declaration is the RELAY's own, because the relay is the machine that decides.
|
|
463
|
+
*
|
|
464
|
+
* What it costs, against the fixed 5 000 ms it replaced: a stranger that connects and never
|
|
465
|
+
* reserves holds a mute connection 0.5 s longer at the default declaration.
|
|
466
|
+
*/
|
|
467
|
+
export function relayAdmissionReserveDeadlineMs(linkRoundTripMs?: number): number {
|
|
468
|
+
return RELAY_RESERVE_REQUEST_ROUND_TRIPS * resolveLinkRoundTripMs(linkRoundTripMs) + ADMISSION_DECISION_TIMEOUT_MS;
|
|
231
469
|
}
|
|
232
470
|
|
|
233
471
|
/**
|
|
@@ -242,6 +480,87 @@ export function circuitRequestBudgetMs(
|
|
|
242
480
|
return CIRCUIT_REQUEST_ROUND_TRIPS * resolveLinkRoundTripMs(linkRoundTripMs) + transferAllowanceMs;
|
|
243
481
|
}
|
|
244
482
|
|
|
483
|
+
/**
|
|
484
|
+
* Deadline for opening a (possibly relayed) connection and completing one small request on it:
|
|
485
|
+
* {@link relayedDialBudgetMs}, admission allowance included, plus
|
|
486
|
+
* {@link CIRCUIT_REQUEST_ROUND_TRIPS} at the declared link round trip — so
|
|
487
|
+
* {@link RELAYED_REQUEST_ROUND_TRIPS} of link time in all. 16 000 + 2 x 3 500 = 23 000 ms at the
|
|
488
|
+
* default declaration.
|
|
489
|
+
*
|
|
490
|
+
* No transfer allowance, unlike {@link circuitRequestBudgetMs}: the requests this bounds are a few
|
|
491
|
+
* hundred bytes to a few KB, so their bytes cost nothing a round trip does not already cover.
|
|
492
|
+
*/
|
|
493
|
+
export function relayedRequestBudgetMs(linkRoundTripMs?: number): number {
|
|
494
|
+
return relayedDialBudgetMs(linkRoundTripMs) + CIRCUIT_REQUEST_ROUND_TRIPS * resolveLinkRoundTripMs(linkRoundTripMs);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* How long ONE cohort peer gets to answer ONE read-path request — Optimystic's
|
|
499
|
+
* `clusterPolicy.cohortQueryTimeoutMs`, which bounds the latest-revision query a read runs when
|
|
500
|
+
* its local copy may be stale and the archive fetch that follows it.
|
|
501
|
+
*
|
|
502
|
+
* {@link CIRCUIT_REQUEST_ROUND_TRIPS} at the declared link round trip: 2 x 3 500 = 7 000 ms at
|
|
503
|
+
* the default declaration. A read-path request opens a fresh protocol stream over a connection
|
|
504
|
+
* that is already open (`@optimystic/db-p2p`'s `openProtocolStream`), so it costs the protocol
|
|
505
|
+
* negotiation plus the request and its answer, and no dial. No admission allowance either: the
|
|
506
|
+
* called machine decided that when the connection was opened. From `@optimystic/db-p2p` 1.8.0
|
|
507
|
+
* this is the only limit on a consult; before it, a fixed 3 000 ms request dial deadline also
|
|
508
|
+
* bounded the negotiation and cut every consult off first at the supported link.
|
|
509
|
+
*
|
|
510
|
+
* Measured at the supported link on 1.8.0 (2026-09-29, 1 500 ms one-way,
|
|
511
|
+
* `strand-reattach-first-sync-measure.integration.ts`): every consult on a joining machine
|
|
512
|
+
* completed, 6.03-6.05 s after the one before it, which is the two round trips and about 40 ms of
|
|
513
|
+
* work, inside this deadline with about 0.95 s to spare.
|
|
514
|
+
*
|
|
515
|
+
* Stated to Optimystic explicitly rather than left to its own derivation from the same
|
|
516
|
+
* declaration, which counts three round trips (10 500 ms at the default declaration). The
|
|
517
|
+
* headroom over the supported link is already in the declaration (3 500 over 3 000), and the
|
|
518
|
+
* third round trip would lengthen every consult against a peer that is gone, which a joining
|
|
519
|
+
* machine's first sync pays several times over. The plugin's frozen policies have to state it
|
|
520
|
+
* anyway: a host that embeds the plugin without cadre-core declares no link to Optimystic.
|
|
521
|
+
*
|
|
522
|
+
* Optimystic derives its whole-reconcile-pass bound from this value, `max(5 000, 5 x per-peer)`
|
|
523
|
+
* (`db-p2p/src/cluster/cluster-policy.ts`): 35 000 ms at the default declaration.
|
|
524
|
+
*
|
|
525
|
+
* What it costs, against the 5 000 ms that preceded it: a peer that is truly gone holds a read
|
|
526
|
+
* of a block missing locally 7 s instead of 5 s before the read is declined and retried, and a
|
|
527
|
+
* joining machine's first sync runs several such consults. That is why
|
|
528
|
+
* `DEFAULT_STRAND_FIRST_SYNC_TIMEOUT_MS` is re-measured whenever this moves; the bands are on
|
|
529
|
+
* that constant.
|
|
530
|
+
*
|
|
531
|
+
* The plugin declares the same number as `COHORT_READ_DEADLINE_MS` (it cannot import this
|
|
532
|
+
* module), and `link-budget.spec.ts` pins the two equal.
|
|
533
|
+
*/
|
|
534
|
+
export function cohortReadDeadlineMs(linkRoundTripMs?: number): number {
|
|
535
|
+
return CIRCUIT_REQUEST_ROUND_TRIPS * resolveLinkRoundTripMs(linkRoundTripMs);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/** The two `NetworkConfig` fields the per-peer cohort read deadline is settled from. */
|
|
539
|
+
export interface DeclaredReadDeadline {
|
|
540
|
+
/** The host's own per-peer read deadline, which wins over the link derivation. */
|
|
541
|
+
cohortQueryTimeoutMs?: number;
|
|
542
|
+
/** The host's declared link round trip, from which the deadline is derived when no explicit one is set. */
|
|
543
|
+
linkRoundTripMs?: number;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* The per-peer cohort read deadline to declare onto a network's cluster policy, or `undefined`
|
|
548
|
+
* for "declare nothing and take the plugin's frozen policy whole". One helper for both policy
|
|
549
|
+
* construction sites (`CadreNode`'s control policy and `StrandInstanceManager`'s strand policy),
|
|
550
|
+
* because the two networks ride one link and must get the same value.
|
|
551
|
+
*
|
|
552
|
+
* An explicit `cohortQueryTimeoutMs` wins over the derivation, so a host that set the deadline
|
|
553
|
+
* by hand keeps it whatever link it declared. With a declared link and no explicit deadline,
|
|
554
|
+
* the deadline is {@link cohortReadDeadlineMs} at that link. With neither, `undefined`: the
|
|
555
|
+
* plugin's frozen policy already carries `COHORT_READ_DEADLINE_MS`, which equals the derivation
|
|
556
|
+
* at the default declaration, and returning `undefined` lets the builders return that frozen
|
|
557
|
+
* object by identity, which existing specs pin.
|
|
558
|
+
*/
|
|
559
|
+
export function declaredCohortReadDeadlineMs(network?: DeclaredReadDeadline): number | undefined {
|
|
560
|
+
return network?.cohortQueryTimeoutMs
|
|
561
|
+
?? (network?.linkRoundTripMs === undefined ? undefined : cohortReadDeadlineMs(network.linkRoundTripMs));
|
|
562
|
+
}
|
|
563
|
+
|
|
245
564
|
/** The two deadlines one peer-join catch-up push needs, both derived from the declared link. */
|
|
246
565
|
export interface PeerJoinPushBudget {
|
|
247
566
|
/** Opening the connection to the peer being caught up — a relayed dial, in the worst case. */
|