@serfab/cadre-core 1.22.0 → 1.24.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 +216 -294
- package/dist/arachnode-stub.d.ts +12 -4
- package/dist/arachnode-stub.js +15 -8
- package/dist/arachnode-stub.js.map +1 -1
- package/dist/cadre-invite-protocol.js +5 -4
- package/dist/cadre-invite-protocol.js.map +1 -1
- package/dist/cadre-node.d.ts +159 -53
- package/dist/cadre-node.js +276 -89
- package/dist/cadre-node.js.map +1 -1
- package/dist/control-authorization.d.ts +14 -2
- package/dist/control-authorization.js +8 -3
- package/dist/control-authorization.js.map +1 -1
- package/dist/control-cohort.d.ts +27 -13
- package/dist/control-cohort.js +21 -14
- package/dist/control-cohort.js.map +1 -1
- package/dist/control-database.d.ts +18 -16
- package/dist/control-database.js +57 -22
- package/dist/control-database.js.map +1 -1
- package/dist/control-protocol-guard.d.ts +1 -1
- package/dist/control-protocol-guard.js +7 -3
- package/dist/control-protocol-guard.js.map +1 -1
- package/dist/control-read-retry.d.ts +49 -11
- package/dist/control-read-retry.js +56 -15
- package/dist/control-read-retry.js.map +1 -1
- package/dist/control-retry.d.ts +32 -3
- package/dist/control-retry.js +35 -4
- package/dist/control-retry.js.map +1 -1
- package/dist/control-schema-version.d.ts +5 -5
- package/dist/control-schema-version.js +5 -5
- package/dist/control-schema-version.js.map +1 -1
- package/dist/control-schema.d.ts +5 -5
- package/dist/control-schema.js +1032 -1011
- package/dist/control-schema.js.map +1 -1
- package/dist/control-write-retry.d.ts +54 -65
- package/dist/control-write-retry.js +134 -158
- package/dist/control-write-retry.js.map +1 -1
- package/dist/delegate-admission.d.ts +1 -1
- package/dist/delegate-admission.js +1 -1
- package/dist/edge-node/config.d.ts +4 -3
- package/dist/edge-node/config.js +11 -9
- package/dist/edge-node/config.js.map +1 -1
- package/dist/edge-node/dissolve.d.ts +4 -4
- package/dist/edge-node/dissolve.js +4 -4
- package/dist/edge-node/edge-join.d.ts +1 -1
- package/dist/edge-node/edge-join.js +1 -1
- package/dist/edge-node/edge-node.js +2 -2
- package/dist/edge-node/edge-node.js.map +1 -1
- package/dist/edge-node/index.d.ts +1 -0
- package/dist/edge-node/index.js +1 -0
- package/dist/edge-node/index.js.map +1 -1
- package/dist/edge-node/join-credential.d.ts +1 -1
- package/dist/edge-node/join-credential.js +1 -1
- package/dist/edge-node/lazy-raw-storage.js +12 -0
- package/dist/edge-node/lazy-raw-storage.js.map +1 -1
- package/dist/edge-node/listen-addrs.d.ts +21 -0
- package/dist/edge-node/listen-addrs.js +25 -0
- package/dist/edge-node/listen-addrs.js.map +1 -0
- package/dist/edge-node/owner-grant.d.ts +1 -1
- package/dist/edge-node/owner-grant.js +1 -1
- package/dist/edge-node/platform.d.ts +5 -4
- package/dist/edge-node/platform.js.map +1 -1
- package/dist/edge-node/strand-move.d.ts +1 -1
- package/dist/edge-node/strand-move.js +1 -1
- package/dist/edge-node/strands.js +27 -11
- package/dist/edge-node/strands.js.map +1 -1
- package/dist/index.d.ts +7 -5
- package/dist/index.js +18 -7
- package/dist/index.js.map +1 -1
- package/dist/invitation-bootstrap.d.ts +28 -5
- package/dist/invitation-bootstrap.js +41 -8
- package/dist/invitation-bootstrap.js.map +1 -1
- package/dist/link-budget.d.ts +53 -4
- package/dist/link-budget.js +58 -5
- package/dist/link-budget.js.map +1 -1
- package/dist/machine-removal.d.ts +2 -2
- package/dist/machine-removal.js +4 -3
- package/dist/machine-removal.js.map +1 -1
- package/dist/membership-connection-gater.d.ts +2 -2
- package/dist/membership-connection-gater.js +2 -2
- package/dist/membership-view.d.ts +2 -2
- package/dist/membership-view.js +6 -3
- package/dist/membership-view.js.map +1 -1
- package/dist/node-claim-payload.d.ts +15 -7
- package/dist/node-claim-payload.js +25 -9
- package/dist/node-claim-payload.js.map +1 -1
- package/dist/node-class.d.ts +36 -0
- package/dist/node-class.js +26 -0
- package/dist/node-class.js.map +1 -0
- package/dist/node-default-transports.d.ts +50 -3
- package/dist/node-default-transports.js +48 -3
- package/dist/node-default-transports.js.map +1 -1
- package/dist/node-roles.d.ts +118 -0
- package/dist/node-roles.js +142 -0
- package/dist/node-roles.js.map +1 -0
- package/dist/node-transports.d.ts +6 -2
- package/dist/node-transports.js +39 -7
- package/dist/node-transports.js.map +1 -1
- package/dist/owner-anchor-sync.js +1 -1
- package/dist/owner-key-export.js +1 -1
- package/dist/owner-key-flows.d.ts +1 -1
- package/dist/owner-key-store.js +1 -1
- package/dist/owner-signer.js +1 -1
- package/dist/peer-authorization.d.ts +3 -1
- package/dist/peer-authorization.js +3 -1
- package/dist/peer-authorization.js.map +1 -1
- package/dist/peer-dial.d.ts +74 -10
- package/dist/peer-dial.js +89 -17
- package/dist/peer-dial.js.map +1 -1
- package/dist/peer-join-backfill.js +4 -4
- package/dist/peer-join-backfill.js.map +1 -1
- package/dist/peer-record.d.ts +34 -9
- package/dist/peer-record.js +66 -13
- package/dist/peer-record.js.map +1 -1
- package/dist/pending-join-runner.d.ts +1 -1
- package/dist/pending-join-runner.js +1 -1
- package/dist/provider/client.d.ts +6 -5
- package/dist/provider/client.js +3 -3
- package/dist/provider/client.js.map +1 -1
- package/dist/provider/descriptor.d.ts +1 -1
- package/dist/provider/descriptor.js +1 -1
- package/dist/provider/oauth.d.ts +1 -1
- package/dist/provider/oauth.js +1 -1
- package/dist/provider/rent-node.d.ts +4 -3
- package/dist/provider/rent-node.js +3 -3
- package/dist/provider/rent-node.js.map +1 -1
- package/dist/relay-addrs.d.ts +62 -7
- package/dist/relay-addrs.js +74 -11
- package/dist/relay-addrs.js.map +1 -1
- package/dist/relay-reservation.d.ts +1 -1
- package/dist/relay-reservation.js +1 -1
- package/dist/relay-server.d.ts +6 -5
- package/dist/relay-server.js +5 -5
- package/dist/relay-server.js.map +1 -1
- package/dist/schema-verification.d.ts +13 -10
- package/dist/schema-verification.js +32 -24
- package/dist/schema-verification.js.map +1 -1
- package/dist/sealed-party-key.js +1 -1
- package/dist/seed-bootstrap.d.ts +4 -3
- package/dist/seed-bootstrap.js +6 -7
- package/dist/seed-bootstrap.js.map +1 -1
- package/dist/strand-addr-protocol.d.ts +8 -6
- package/dist/strand-addr-protocol.js +12 -10
- package/dist/strand-addr-protocol.js.map +1 -1
- package/dist/strand-app-schema-report.d.ts +16 -2
- package/dist/strand-app-schema-report.js +15 -0
- package/dist/strand-app-schema-report.js.map +1 -1
- package/dist/strand-cohort.d.ts +1 -4
- package/dist/strand-cohort.js.map +1 -1
- package/dist/strand-commit-durability.d.ts +1 -1
- package/dist/strand-database.d.ts +13 -6
- package/dist/strand-database.js +17 -9
- package/dist/strand-database.js.map +1 -1
- package/dist/strand-first-sync-gate.d.ts +3 -2
- package/dist/strand-first-sync-gate.js +3 -2
- package/dist/strand-first-sync-gate.js.map +1 -1
- package/dist/strand-formation-deadlines.d.ts +2 -2
- package/dist/strand-formation-deadlines.js +2 -2
- package/dist/strand-formation-protocol.d.ts +1 -1
- package/dist/strand-formation-protocol.js +51 -16
- package/dist/strand-formation-protocol.js.map +1 -1
- package/dist/strand-instance-manager.d.ts +134 -42
- package/dist/strand-instance-manager.js +331 -76
- package/dist/strand-instance-manager.js.map +1 -1
- package/dist/strand-membership-reconciler.d.ts +8 -3
- package/dist/strand-membership-reconciler.js +8 -3
- package/dist/strand-membership-reconciler.js.map +1 -1
- package/dist/strand-membership-writer.d.ts +2 -2
- package/dist/strand-membership-writer.js +41 -39
- package/dist/strand-membership-writer.js.map +1 -1
- package/dist/strand-move.js +1 -1
- package/dist/strand-network-config.d.ts +15 -5
- package/dist/strand-network-config.js +7 -6
- package/dist/strand-network-config.js.map +1 -1
- package/dist/strand-party-key-rotation.d.ts +7 -3
- package/dist/strand-party-key-rotation.js +7 -3
- package/dist/strand-party-key-rotation.js.map +1 -1
- package/dist/strand-party-machines.d.ts +19 -8
- package/dist/strand-party-machines.js +26 -11
- package/dist/strand-party-machines.js.map +1 -1
- package/dist/strand-party-peers.d.ts +36 -14
- package/dist/strand-party-peers.js +24 -15
- package/dist/strand-party-peers.js.map +1 -1
- package/dist/strand-peer-redial.d.ts +1 -1
- package/dist/strand-peer-redial.js +1 -1
- package/dist/strand-reactivity.d.ts +20 -17
- package/dist/strand-reactivity.js +31 -18
- package/dist/strand-reactivity.js.map +1 -1
- package/dist/strand-revocation-enforcer.d.ts +3 -3
- package/dist/strand-revocation-enforcer.js +3 -3
- package/dist/strand-wake-protocol.d.ts +19 -7
- package/dist/strand-wake-protocol.js +22 -10
- package/dist/strand-wake-protocol.js.map +1 -1
- package/dist/strand-watcher.d.ts +1 -1
- package/dist/strand-watcher.js +1 -1
- package/dist/table-change-watch.js +1 -1
- package/dist/types.d.ts +127 -57
- package/dist/types.js.map +1 -1
- package/dist/webrtc-upgrade.d.ts +254 -0
- package/dist/webrtc-upgrade.js +355 -0
- package/dist/webrtc-upgrade.js.map +1 -0
- package/package.json +9 -9
- package/src/announce-addrs.ts +93 -93
- package/src/arachnode-stub.ts +18 -10
- package/src/bootstrap-peer-store-file.ts +44 -44
- package/src/bootstrap-peer-store.ts +214 -214
- package/src/cadre-invite-protocol.ts +741 -740
- package/src/cadre-node.ts +12462 -12252
- package/src/claim-proof.ts +147 -147
- package/src/control-authorization.ts +290 -276
- package/src/control-cohort.ts +223 -204
- package/src/control-database.ts +72 -28
- package/src/control-formation-recorder.ts +359 -359
- package/src/control-protocol-guard.ts +370 -366
- package/src/control-read-retry.ts +222 -180
- package/src/control-retry.ts +36 -4
- package/src/control-schema-version.ts +301 -301
- package/src/control-schema.ts +1108 -1087
- package/src/control-write-retry.ts +639 -669
- package/src/delegate-admission.ts +351 -351
- package/src/describe-thrown.ts +60 -60
- package/src/ed25519-key.ts +185 -185
- package/src/edge-node/config.ts +13 -10
- package/src/edge-node/dissolve.ts +252 -252
- package/src/edge-node/edge-join.ts +320 -320
- package/src/edge-node/edge-node.ts +1095 -1095
- package/src/edge-node/index.ts +60 -59
- package/src/edge-node/join-credential.ts +16 -16
- package/src/edge-node/lazy-raw-storage.ts +194 -178
- package/src/edge-node/listen-addrs.ts +26 -0
- package/src/edge-node/node-local.ts +84 -84
- package/src/edge-node/options.ts +195 -195
- package/src/edge-node/owner-grant.ts +63 -63
- package/src/edge-node/platform.ts +5 -4
- package/src/edge-node/strand-move.ts +266 -266
- package/src/edge-node/strands.ts +27 -12
- package/src/enrolled-machine-store-file.ts +43 -43
- package/src/enrolled-machine-store.ts +280 -280
- package/src/fetch-impl.ts +23 -23
- package/src/file-durable-slot.ts +64 -64
- package/src/file-safe-name.ts +32 -32
- package/src/formation-approval.ts +604 -604
- package/src/formation-token.ts +63 -63
- package/src/fs-atomic.ts +92 -92
- package/src/index.ts +987 -957
- package/src/invitation-bootstrap.ts +152 -118
- package/src/joined-strand-store.ts +553 -553
- package/src/key-store-file.ts +142 -142
- package/src/link-budget.ts +61 -5
- package/src/machine-removal.ts +211 -210
- package/src/member-redemption.ts +643 -643
- package/src/membership-connection-gater.ts +650 -650
- package/src/membership-view.ts +147 -144
- package/src/node-claim-payload.ts +34 -12
- package/src/node-class.ts +47 -0
- package/src/node-default-transports.ts +65 -3
- package/src/node-roles.ts +220 -0
- package/src/node-transports.ts +86 -49
- package/src/owner-anchor-sync.ts +172 -172
- package/src/owner-grant-protocol.ts +547 -547
- package/src/owner-key-export.ts +348 -348
- package/src/owner-key-flows.ts +109 -109
- package/src/owner-key-store.ts +1 -1
- package/src/owner-keys.ts +179 -179
- package/src/owner-signer.ts +92 -92
- package/src/peer-addr-book.ts +252 -252
- package/src/peer-authorization.ts +584 -582
- package/src/peer-dial.ts +698 -605
- package/src/peer-join-backfill.ts +961 -961
- package/src/peer-record.ts +71 -16
- package/src/pending-join-runner.ts +798 -798
- package/src/primary-lan-address.ts +47 -47
- package/src/provider/client.ts +327 -326
- package/src/provider/descriptor.ts +129 -129
- package/src/provider/index.ts +59 -59
- package/src/provider/oauth.ts +285 -285
- package/src/provider/rent-node.ts +276 -275
- package/src/relay-addrs.ts +90 -12
- package/src/relay-reservation.ts +1 -1
- package/src/relay-server.ts +99 -98
- package/src/schema-verification.ts +38 -23
- package/src/sealed-party-key.ts +190 -190
- package/src/seed-bootstrap.ts +8 -11
- package/src/seed-trust-policy.ts +353 -353
- package/src/self-address-update.ts +19 -19
- package/src/strand-addr-protocol.ts +17 -14
- package/src/strand-app-schema-report.ts +147 -122
- package/src/strand-cohort.ts +1 -5
- package/src/strand-commit-durability.ts +165 -165
- package/src/strand-database.ts +522 -510
- package/src/strand-first-sync-gate.ts +3 -2
- package/src/strand-formation-deadlines.ts +227 -227
- package/src/strand-formation-manager.ts +731 -731
- package/src/strand-formation-protocol.ts +1174 -1138
- package/src/strand-fret-addrs.ts +179 -179
- package/src/strand-instance-manager.ts +381 -85
- package/src/strand-membership-reconciler.ts +1315 -1310
- package/src/strand-membership-writer.ts +2152 -2150
- package/src/strand-move.ts +1 -1
- package/src/strand-network-config.ts +16 -6
- package/src/strand-offline.ts +178 -178
- package/src/strand-party-key-rotation.ts +440 -436
- package/src/strand-party-machines.ts +308 -293
- package/src/strand-party-peers.ts +151 -129
- package/src/strand-peer-redial.ts +1 -1
- package/src/strand-reactivity.ts +36 -19
- package/src/strand-revocation-enforcer.ts +758 -758
- package/src/strand-wake-protocol.ts +406 -394
- package/src/strand-watcher.ts +1 -1
- package/src/table-change-watch.ts +285 -285
- package/src/timed-step.ts +28 -28
- package/src/timeout-scheduler.ts +29 -29
- package/src/trusted-owner-store.ts +242 -242
- package/src/types.ts +3182 -3112
- package/src/webrtc-upgrade.ts +439 -0
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Replace a relayed connection with a direct WebRTC one, in the background, once a peer's
|
|
3
|
+
* addresses are known.
|
|
4
|
+
*
|
|
5
|
+
* Nothing else does this. js-libp2p never upgrades a relayed connection to WebRTC by itself, and
|
|
6
|
+
* DCUtR — its hole-punching service — does not use WebRTC. Since every dial a caller WAITS on
|
|
7
|
+
* goes relayed-first (`docs/network/relay.md` → "Direct connections over WebRTC"), a WebRTC
|
|
8
|
+
* connection no longer opens as a side effect of some dial happening to try the WebRTC address
|
|
9
|
+
* first either, so without this module two machines that reached each other through a relay stay
|
|
10
|
+
* on it for good even when ICE could connect them directly.
|
|
11
|
+
*
|
|
12
|
+
* Direct is worth having when it works. A 1 MB strand write measured 2.4-3.2 s direct against
|
|
13
|
+
* 3.8 s relayed (gotchoices/sereus#61, both on a loopback relay and through `relay.sereus.org`),
|
|
14
|
+
* a phone and a node on one LAN opened directly in about 0.5 s, and the bytes come off the relay.
|
|
15
|
+
*
|
|
16
|
+
* ## The trigger
|
|
17
|
+
*
|
|
18
|
+
* `peer:identify`, not `connection:open`: identify is when the remote's listen addresses are
|
|
19
|
+
* known, and its WebRTC address is the thing being dialed. libp2p dispatches the event for each
|
|
20
|
+
* new connection AND for every identify-push, so a peer that only gains its WebRTC address later
|
|
21
|
+
* (its relay reservation landed after the connection opened) is re-evaluated when it pushes.
|
|
22
|
+
*
|
|
23
|
+
* NOTE: those two are the ONLY re-evaluations — libp2p runs identify per new connection and on a
|
|
24
|
+
* push, never on a timer — so a direct connection that drops while the relayed one beside it
|
|
25
|
+
* survives is not replaced until that peer reconnects or pushes addresses
|
|
26
|
+
* (`docs/network/relay.md` → "Limits and accepted tradeoffs"). Add `connection:close` as a
|
|
27
|
+
* second trigger if peers are seen stuck relayed after a direct connection dropped.
|
|
28
|
+
*
|
|
29
|
+
* It also decides the reach of the whole service, since Optimystic names each node's identify
|
|
30
|
+
* protocol after the network it serves. Two parties' STRAND nodes share one namespace
|
|
31
|
+
* (`/optimystic/strand-<strandId>/id/1.0.0`) and identify each other, which is where this matters
|
|
32
|
+
* most — a strand's peers are other parties' machines, so both sides are often behind NAT. Two
|
|
33
|
+
* parties' CONTROL nodes do not, so on the control network only a party's own machines, and a
|
|
34
|
+
* device redeeming an invitation into the party, are ever candidates.
|
|
35
|
+
*
|
|
36
|
+
* An attempt is made when all of {@link decideWebrtcUpgrade}'s conditions hold. The one that is
|
|
37
|
+
* NOT a condition is worth stating: the relay's own traffic limit. An upgrade over a CAPPED
|
|
38
|
+
* relayed connection — one libp2p marks `limited` — is attempted on the same terms as any other,
|
|
39
|
+
* because there the direct path is the only one Optimystic's database protocols can use at all
|
|
40
|
+
* (`ops/docs/ice-servers.md` → "Why there is no TURN"). The signalling stream `@libp2p/webrtc`
|
|
41
|
+
* opens is declared `runOnLimitedConnection`, so it is allowed on such a connection.
|
|
42
|
+
*
|
|
43
|
+
* ## Which side dials
|
|
44
|
+
*
|
|
45
|
+
* Exactly one, so that two upgrades do not cross: the side with the lower peer id when both
|
|
46
|
+
* advertise a WebRTC address, and otherwise whichever side has a WebRTC address to dial. A
|
|
47
|
+
* dialer needs the WebRTC transport, not a `/webrtc` listener, which is why "this side advertises
|
|
48
|
+
* none" does not stop it.
|
|
49
|
+
*
|
|
50
|
+
* NOTE: the tie-break is judged from each side's CURRENT addresses, so in the window where one
|
|
51
|
+
* side advertises a WebRTC address and the other does not yet, both can decide they are the
|
|
52
|
+
* dialer and two WebRTC connections can open. Harmless — libp2p holds both and new streams take
|
|
53
|
+
* one of them — and it closes as soon as the two sides see the same pair of address sets. If
|
|
54
|
+
* duplicate upgrade connections are ever seen lingering, close the one whose dial this side lost.
|
|
55
|
+
*
|
|
56
|
+
* ## The dial
|
|
57
|
+
*
|
|
58
|
+
* One address per attempt — the first WebRTC address in identify order — through
|
|
59
|
+
* {@link dialAddr}, the queue-free per-address dial, rather than `node.dial`. Three reasons, all
|
|
60
|
+
* of which matter here:
|
|
61
|
+
*
|
|
62
|
+
* - libp2p's dial queue joins any dial to a queued job for the same peer id, whatever address the
|
|
63
|
+
* new dial names. Through the queue an upgrade dial can therefore be answered by someone else's
|
|
64
|
+
* relayed dial of the same peer and report a success that upgraded nothing.
|
|
65
|
+
* - {@link dialAddr} does not hand back an open relayed connection for a WebRTC address, which is
|
|
66
|
+
* the rule libp2p applies too and what lets the attempt replace the link rather than resolve to
|
|
67
|
+
* it. An open DIRECT connection is handed back, which is the correct short-circuit.
|
|
68
|
+
* - It aborts on the node's own `stop`, so a node torn down mid-attempt leaves nothing dialing.
|
|
69
|
+
*
|
|
70
|
+
* The attempt is bounded by {@link WebrtcUpgradeDeps.dialTimeoutMs}, derived from the host's
|
|
71
|
+
* declared link (`link-budget.ts` → `webrtcUpgradeDialBudgetMs`) and deliberately longer than
|
|
72
|
+
* the WebRTC stack's own give-up time: nothing waits on this dial, so cutting off a working
|
|
73
|
+
* attempt would be the only way to lose by it.
|
|
74
|
+
*
|
|
75
|
+
* ## After a success, the relayed connection is left open
|
|
76
|
+
*
|
|
77
|
+
* Closing it would race the streams already on it, and nothing needs it closed: new streams
|
|
78
|
+
* already prefer the direct connection. libp2p's `findExistingConnection` sorts direct
|
|
79
|
+
* connections first, and Optimystic's stream paths try non-limited connections newest first.
|
|
80
|
+
*
|
|
81
|
+
* NOTE: Optimystic groups connections by limited/unlimited rather than by direct/relayed
|
|
82
|
+
* (`../optimystic/packages/db-p2p/src/network/open-protocol-stream.ts`), so if the relayed
|
|
83
|
+
* connection is later re-opened it becomes the newest unlimited one and wins again, and the
|
|
84
|
+
* strand's traffic goes back over the relay while a direct connection sits open beside it. Not
|
|
85
|
+
* reachable from here — the relayed connection that triggered the upgrade is older than the
|
|
86
|
+
* WebRTC one, and that order only inverts if it drops and is re-dialed. If it shows up, ask
|
|
87
|
+
* Optimystic to rank direct connections ahead of unlimited relayed ones rather than closing the
|
|
88
|
+
* relayed connection here.
|
|
89
|
+
*
|
|
90
|
+
* ## Backoff
|
|
91
|
+
*
|
|
92
|
+
* Per remote peer per node, in memory: the ladder {@link nextWebrtcUpgradeBackoffMs} states,
|
|
93
|
+
* cleared by a success. Two networks that
|
|
94
|
+
* allow no direct path between them fail every single time, and each attempt costs relay
|
|
95
|
+
* signalling plus ICE gathering, which wakes a phone's radio. Nothing is persisted: a restart
|
|
96
|
+
* starting over is one attempt per peer, which is what a restart should try anyway.
|
|
97
|
+
*
|
|
98
|
+
* NOTE: the backoff map holds one entry per peer ever attempted, for the life of the node, and
|
|
99
|
+
* is deliberately not cleared on disconnect — clearing it would re-attempt on every reconnect,
|
|
100
|
+
* which is exactly the cost the backoff exists to bound. A party's machines and a strand's
|
|
101
|
+
* members are tens of peers, so the map is small. If a node ever meets peers by the thousand,
|
|
102
|
+
* evict the entries whose wait has expired.
|
|
103
|
+
*/
|
|
104
|
+
|
|
105
|
+
import debug from 'debug';
|
|
106
|
+
import type { Connection, IdentifyResult, Libp2p, PeerId } from '@libp2p/interface';
|
|
107
|
+
import type { Multiaddr } from '@multiformats/multiaddr';
|
|
108
|
+
import { addrKind, dialAddr, withTrailingPeerId, type AddrKind } from './peer-dial.js';
|
|
109
|
+
import { describeThrown } from './describe-thrown.js';
|
|
110
|
+
|
|
111
|
+
const log = debug('sereus:cadre:webrtc-upgrade');
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The protocol `@libp2p/webrtc`'s private-to-private transport registers when it starts, and
|
|
115
|
+
* therefore the signal that a node CAN dial a `/p2p-circuit/webrtc` address: the transport
|
|
116
|
+
* handles the signalling the dial needs, and a node whose embedder passed no WebRTC transport
|
|
117
|
+
* never serves this protocol. Also classified by `control-protocol-guard.ts`, which must know
|
|
118
|
+
* what to do with an inbound stream on it.
|
|
119
|
+
*/
|
|
120
|
+
export const WEBRTC_SIGNALING_PROTOCOL = '/webrtc-signaling/0.0.1';
|
|
121
|
+
|
|
122
|
+
/** Wait after a peer's first failed upgrade attempt, before any further one. */
|
|
123
|
+
export const WEBRTC_UPGRADE_BACKOFF_MS = 60_000;
|
|
124
|
+
|
|
125
|
+
/** Ceiling the per-peer wait doubles up to. */
|
|
126
|
+
export const WEBRTC_UPGRADE_MAX_BACKOFF_MS = 30 * 60_000;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* The wait one peer's NEXT failure puts in force, given the wait its previous failure applied
|
|
130
|
+
* (absent for a peer with no failure on record): {@link WEBRTC_UPGRADE_BACKOFF_MS}, doubling to
|
|
131
|
+
* {@link WEBRTC_UPGRADE_MAX_BACKOFF_MS} and held there.
|
|
132
|
+
*
|
|
133
|
+
* Pure and separate from the map that holds it, for the same reason {@link decideWebrtcUpgrade}
|
|
134
|
+
* is: the ladder is a rule, and `decideWebrtcUpgrade` only ever sees the deadline it produced.
|
|
135
|
+
*/
|
|
136
|
+
export function nextWebrtcUpgradeBackoffMs(previousWaitMs?: number): number {
|
|
137
|
+
return previousWaitMs === undefined
|
|
138
|
+
? WEBRTC_UPGRADE_BACKOFF_MS
|
|
139
|
+
: Math.min(previousWaitMs * 2, WEBRTC_UPGRADE_MAX_BACKOFF_MS);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Why {@link decideWebrtcUpgrade} declined to attempt an upgrade. */
|
|
143
|
+
export type WebrtcUpgradeSkipReason =
|
|
144
|
+
/** This node holds no relay-carried connection to the peer, so there is nothing to upgrade. */
|
|
145
|
+
| 'not-relayed'
|
|
146
|
+
/** A direct connection to the peer is already open — a WebRTC one, or any unrelayed transport. */
|
|
147
|
+
| 'already-direct'
|
|
148
|
+
/** The remote published no WebRTC address, so there is none to dial. */
|
|
149
|
+
| 'no-webrtc-addr'
|
|
150
|
+
/** Both sides advertise one and the remote holds the lower peer id, so the remote dials. */
|
|
151
|
+
| 'remote-dials'
|
|
152
|
+
/** This peer's wait after an earlier failed attempt has not expired. */
|
|
153
|
+
| 'in-backoff';
|
|
154
|
+
|
|
155
|
+
/** Whether to attempt an upgrade, and at which address. */
|
|
156
|
+
export type WebrtcUpgradeDecision =
|
|
157
|
+
| { readonly attempt: true; readonly addr: Multiaddr }
|
|
158
|
+
| { readonly attempt: false; readonly reason: WebrtcUpgradeSkipReason };
|
|
159
|
+
|
|
160
|
+
/** Everything {@link decideWebrtcUpgrade} judges one `peer:identify` from. */
|
|
161
|
+
export interface WebrtcUpgradeInputs {
|
|
162
|
+
/** This node's peer id, for the tie-break. */
|
|
163
|
+
readonly localPeerId: string;
|
|
164
|
+
/** The peer that identified. */
|
|
165
|
+
readonly remotePeerId: string;
|
|
166
|
+
/**
|
|
167
|
+
* The {@link AddrKind} of every OPEN connection this node holds to that peer, which answers
|
|
168
|
+
* both of the first two conditions: there is something to upgrade only if one of them is
|
|
169
|
+
* relay-carried, and it is worth upgrading only if none of them is direct. The connection the
|
|
170
|
+
* identify ran over is one of these, so it needs no field of its own.
|
|
171
|
+
*/
|
|
172
|
+
readonly openConnections: readonly AddrKind[];
|
|
173
|
+
/** The remote's identified listen addresses, in the order it published them. */
|
|
174
|
+
readonly remoteAddrs: readonly Multiaddr[];
|
|
175
|
+
/** Whether this node publishes a WebRTC address of its own (the tie-break's other half). */
|
|
176
|
+
readonly localAdvertisesWebrtc: boolean;
|
|
177
|
+
/** Epoch ms before which this peer must not be attempted again, or absent when it is free. */
|
|
178
|
+
readonly backoffUntilMs?: number;
|
|
179
|
+
readonly nowMs: number;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Whether to dial a WebRTC upgrade for the peer that just identified, and at which address.
|
|
184
|
+
* Pure, so the rules that carry the judgement — the tie-break and the backoff — are testable
|
|
185
|
+
* without a libp2p node.
|
|
186
|
+
*
|
|
187
|
+
* Conditions are checked in the order the module doc describes them, so the reason reported is
|
|
188
|
+
* the first one that applies. Note what is NOT here: whether the relay caps the connection. A
|
|
189
|
+
* capped relayed connection is upgraded on the same terms as any other (see the module doc).
|
|
190
|
+
*/
|
|
191
|
+
export function decideWebrtcUpgrade(inputs: WebrtcUpgradeInputs): WebrtcUpgradeDecision {
|
|
192
|
+
if (!inputs.openConnections.includes('relayed')) {
|
|
193
|
+
return { attempt: false, reason: 'not-relayed' };
|
|
194
|
+
}
|
|
195
|
+
if (inputs.openConnections.some((kind) => kind !== 'relayed')) {
|
|
196
|
+
return { attempt: false, reason: 'already-direct' };
|
|
197
|
+
}
|
|
198
|
+
const addr = inputs.remoteAddrs.find((candidate) => addrKind(candidate) === 'webrtc');
|
|
199
|
+
if (addr === undefined) {
|
|
200
|
+
return { attempt: false, reason: 'no-webrtc-addr' };
|
|
201
|
+
}
|
|
202
|
+
if (inputs.localAdvertisesWebrtc && inputs.localPeerId > inputs.remotePeerId) {
|
|
203
|
+
return { attempt: false, reason: 'remote-dials' };
|
|
204
|
+
}
|
|
205
|
+
if (inputs.backoffUntilMs !== undefined && inputs.nowMs < inputs.backoffUntilMs) {
|
|
206
|
+
return { attempt: false, reason: 'in-backoff' };
|
|
207
|
+
}
|
|
208
|
+
return { attempt: true, addr };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Whether `node` can dial a `/p2p-circuit/webrtc` address at all, judged by the signalling
|
|
213
|
+
* protocol its WebRTC transport registers ({@link WEBRTC_SIGNALING_PROTOCOL}).
|
|
214
|
+
*
|
|
215
|
+
* A node's transport list is fixed when it is built, so this answer cannot change over the
|
|
216
|
+
* node's life and {@link WebrtcUpgradeService.start} reads it once. It must be read after the
|
|
217
|
+
* node has STARTED, which is when the transport registers the protocol — both of cadre-core's
|
|
218
|
+
* install sites run after `createLibp2pNode` has started the node.
|
|
219
|
+
*/
|
|
220
|
+
function canDialWebrtc(node: Libp2p): boolean {
|
|
221
|
+
// Read structurally, like the node slices in `peer-dial.ts` and `relay-reservation.ts`: a node
|
|
222
|
+
// that reports no protocol list is treated as having no WebRTC transport, which leaves the
|
|
223
|
+
// service inert. That is the fail-safe answer, and it is what lets the unit specs build small
|
|
224
|
+
// libp2p doubles without implementing a method only this probe calls.
|
|
225
|
+
const protocols = (node as Partial<Libp2p>).getProtocols?.();
|
|
226
|
+
return protocols?.includes(WEBRTC_SIGNALING_PROTOCOL) === true;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** What {@link WebrtcUpgradeService} needs from its host. */
|
|
230
|
+
export interface WebrtcUpgradeDeps {
|
|
231
|
+
/** Names this node in logs: the control network name, or the strand id. */
|
|
232
|
+
readonly label: string;
|
|
233
|
+
readonly node: Libp2p;
|
|
234
|
+
/**
|
|
235
|
+
* Deadline for one upgrade dial, from `link-budget.ts`'s `webrtcUpgradeDialBudgetMs` at the
|
|
236
|
+
* host's declared link. A backstop, not the normal end of an attempt — see that function.
|
|
237
|
+
*/
|
|
238
|
+
readonly dialTimeoutMs: number;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** One peer's current wait after a failed attempt. */
|
|
242
|
+
interface Backoff {
|
|
243
|
+
/** The wait that was applied, doubled on the next failure. */
|
|
244
|
+
readonly waitMs: number;
|
|
245
|
+
/** Epoch ms the wait expires at. */
|
|
246
|
+
readonly untilMs: number;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Subscribes to `peer:identify` on one libp2p node and dials a background WebRTC upgrade
|
|
251
|
+
* whenever {@link decideWebrtcUpgrade} says to. Installed on every node cadre-core builds — the
|
|
252
|
+
* control node and each strand node — and inert on one that cannot dial WebRTC.
|
|
253
|
+
*
|
|
254
|
+
* One attempt per peer at a time, each abandoned when the service stops.
|
|
255
|
+
*/
|
|
256
|
+
export class WebrtcUpgradeService {
|
|
257
|
+
private listening = false;
|
|
258
|
+
private stopped = false;
|
|
259
|
+
private readonly backoff = new Map<string, Backoff>();
|
|
260
|
+
/** One controller per peer with an attempt in flight; its presence is also the per-peer lock. */
|
|
261
|
+
private readonly inFlight = new Map<string, AbortController>();
|
|
262
|
+
private readonly onIdentify: (evt: CustomEvent<IdentifyResult>) => void;
|
|
263
|
+
|
|
264
|
+
constructor(private readonly deps: WebrtcUpgradeDeps) {
|
|
265
|
+
this.onIdentify = (evt) => {
|
|
266
|
+
this.consider(evt.detail.peerId, evt.detail.listenAddrs);
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Subscribe, unless this node cannot dial WebRTC ({@link canDialWebrtc}) — then the service
|
|
272
|
+
* stays inert and says so once, rather than judging every identify for a dial it could never
|
|
273
|
+
* make. Then judge the peers that are already connected ({@link considerConnectedPeers}).
|
|
274
|
+
*/
|
|
275
|
+
start(): void {
|
|
276
|
+
if (this.listening || this.stopped) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
if (!canDialWebrtc(this.deps.node)) {
|
|
280
|
+
log('[%s] inert: the node serves no %s, so it has no WebRTC transport to dial with',
|
|
281
|
+
this.deps.label, WEBRTC_SIGNALING_PROTOCOL);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
this.listening = true;
|
|
285
|
+
this.deps.node.addEventListener('peer:identify', this.onIdentify);
|
|
286
|
+
// Logged rather than left to float: nothing awaits this pass, so a throw from the node
|
|
287
|
+
// slices it reads would otherwise surface as an unhandled rejection — the same reason
|
|
288
|
+
// {@link consider} catches its own.
|
|
289
|
+
void this.considerConnectedPeers().then(
|
|
290
|
+
(judged) => log('[%s] started (%d peer(s) already connected)', this.deps.label, judged),
|
|
291
|
+
(err) => log('[%s] started, but judging the peers already connected threw: %s',
|
|
292
|
+
this.deps.label, describeThrown(err))
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Judge every peer this node is ALREADY connected to, from the addresses their identify left
|
|
298
|
+
* in the peer store, and return how many were considered (for the start log).
|
|
299
|
+
*
|
|
300
|
+
* The control node needs this: it is armed at the end of `CadreNode.start()`, and building the
|
|
301
|
+
* control database before that connects to every sibling it can reach, so those peers have
|
|
302
|
+
* already identified and the event for them will not come again until one of THEM changes its
|
|
303
|
+
* addresses. Without this pass, a party's two relay-only machines could sit relayed for as long
|
|
304
|
+
* as neither reconnected. Same reasoning as `PeerJoinBackfill.scheduleConnectedPeers`, which
|
|
305
|
+
* reads the same peer-store addresses.
|
|
306
|
+
*/
|
|
307
|
+
private async considerConnectedPeers(): Promise<number> {
|
|
308
|
+
const peers = new Map<string, PeerId>();
|
|
309
|
+
for (const connection of this.deps.node.getConnections()) {
|
|
310
|
+
peers.set(connection.remotePeer.toString(), connection.remotePeer);
|
|
311
|
+
}
|
|
312
|
+
await Promise.all([...peers.values()].map((peerId) => this.considerStored(peerId)));
|
|
313
|
+
return peers.size;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** Judge one already-connected peer from its stored addresses; a peer the store does not know yet is left to its event. */
|
|
317
|
+
private async considerStored(peerId: PeerId): Promise<void> {
|
|
318
|
+
try {
|
|
319
|
+
const stored = await this.deps.node.peerStore.get(peerId);
|
|
320
|
+
this.consider(peerId, stored.addresses.map((address) => address.multiaddr));
|
|
321
|
+
} catch (err) {
|
|
322
|
+
// Normally `NotFoundError`: this peer's identify has not finished, so its own
|
|
323
|
+
// `peer:identify` is still to come and will carry the addresses.
|
|
324
|
+
log('[%s] no stored addresses for %s yet: %s', this.deps.label, peerId.toString(), describeThrown(err));
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** Unsubscribe and abandon every attempt in flight. Idempotent. */
|
|
329
|
+
stop(): void {
|
|
330
|
+
if (this.stopped) {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
this.stopped = true;
|
|
334
|
+
if (this.listening) {
|
|
335
|
+
this.deps.node.removeEventListener('peer:identify', this.onIdentify);
|
|
336
|
+
this.listening = false;
|
|
337
|
+
}
|
|
338
|
+
for (const controller of this.inFlight.values()) {
|
|
339
|
+
controller.abort(new Error(`${this.deps.label}: the WebRTC upgrade service stopped`));
|
|
340
|
+
}
|
|
341
|
+
this.inFlight.clear();
|
|
342
|
+
log('[%s] stopped', this.deps.label);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Judge one peer's addresses against the connections this node holds to it, and start the
|
|
347
|
+
* attempt it calls for. Never throws: it runs inside a libp2p event listener, where a rejection
|
|
348
|
+
* would be an unhandled one, and an upgrade is best-effort.
|
|
349
|
+
*/
|
|
350
|
+
private consider(peerId: PeerId, remoteAddrs: readonly Multiaddr[]): void {
|
|
351
|
+
if (this.stopped) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
const remote = peerId.toString();
|
|
355
|
+
try {
|
|
356
|
+
if (this.inFlight.has(remote)) {
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
const decision = decideWebrtcUpgrade({
|
|
360
|
+
localPeerId: this.deps.node.peerId.toString(),
|
|
361
|
+
remotePeerId: remote,
|
|
362
|
+
openConnections: this.deps.node.getConnections(peerId)
|
|
363
|
+
.filter((connection) => connection.status === 'open')
|
|
364
|
+
.map((connection) => addrKind(connection.remoteAddr)),
|
|
365
|
+
remoteAddrs,
|
|
366
|
+
localAdvertisesWebrtc: this.deps.node.getMultiaddrs().some((addr) => addrKind(addr) === 'webrtc'),
|
|
367
|
+
backoffUntilMs: this.backoff.get(remote)?.untilMs,
|
|
368
|
+
nowMs: Date.now(),
|
|
369
|
+
});
|
|
370
|
+
if (!decision.attempt) {
|
|
371
|
+
log('[%s] no upgrade for %s: %s', this.deps.label, remote, decision.reason);
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
void this.attempt(remote, decision.addr);
|
|
375
|
+
} catch (err) {
|
|
376
|
+
log('[%s] judging an upgrade for %s threw: %s', this.deps.label, remote, describeThrown(err));
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Dial `addr` as an upgrade for `remote`, and record what came of it. A dial that resolves to a
|
|
382
|
+
* relayed connection upgraded nothing — it was served by one, or libp2p's circuit transport
|
|
383
|
+
* answered with one — and is neither a success nor a failure: the backoff is left as it was, so
|
|
384
|
+
* the peer's next identify tries again.
|
|
385
|
+
*/
|
|
386
|
+
private async attempt(remote: string, addr: Multiaddr): Promise<void> {
|
|
387
|
+
const bound = withTrailingPeerId(addr, remote);
|
|
388
|
+
if (bound === null) {
|
|
389
|
+
// identify publishes each listen address with the peer id stripped, so this is an address
|
|
390
|
+
// already ending in a DIFFERENT peer id: it does not reach the peer that published it.
|
|
391
|
+
log('[%s] upgrade address %s does not reach %s', this.deps.label, addr.toString(), remote);
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
const controller = new AbortController();
|
|
395
|
+
this.inFlight.set(remote, controller);
|
|
396
|
+
const label = `[${this.deps.label}] WebRTC upgrade of ${remote}`;
|
|
397
|
+
const expiry = setTimeout(
|
|
398
|
+
() => controller.abort(new Error(`${label}: gave up after ${this.deps.dialTimeoutMs} ms`)),
|
|
399
|
+
this.deps.dialTimeoutMs
|
|
400
|
+
);
|
|
401
|
+
try {
|
|
402
|
+
const connection = await dialAddr(this.deps.node, bound, controller.signal, label);
|
|
403
|
+
this.noteOutcome(remote, connection);
|
|
404
|
+
} catch (err) {
|
|
405
|
+
const wait = this.noteFailure(remote);
|
|
406
|
+
log('%s failed (next attempt in %d ms at the earliest): %s', label, wait, describeThrown(err));
|
|
407
|
+
} finally {
|
|
408
|
+
clearTimeout(expiry);
|
|
409
|
+
this.inFlight.delete(remote);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Clear the peer's backoff on a real upgrade; leave it alone when the dial resolved to a relay,
|
|
415
|
+
* so the peer's next identify tries again rather than waiting out a failure it did not have.
|
|
416
|
+
*
|
|
417
|
+
* The relayed outcome should not happen on the dial path this module uses — {@link dialAddr}
|
|
418
|
+
* dials the one address it is given and refuses to stand an open relayed connection in for a
|
|
419
|
+
* WebRTC address. It is checked because the alternative, `node.dial`, CAN produce it by joining
|
|
420
|
+
* another dial of the same peer (see the module doc), and a false success there would clear a
|
|
421
|
+
* backoff and report an upgrade that never happened.
|
|
422
|
+
*/
|
|
423
|
+
private noteOutcome(remote: string, connection: Connection): void {
|
|
424
|
+
if (addrKind(connection.remoteAddr) === 'relayed') {
|
|
425
|
+
log('[%s] upgrade dial of %s answered with a relayed connection (%s); nothing upgraded',
|
|
426
|
+
this.deps.label, remote, connection.remoteAddr.toString());
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
this.backoff.delete(remote);
|
|
430
|
+
log('[%s] upgraded %s to %s', this.deps.label, remote, connection.remoteAddr.toString());
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/** Start or double this peer's wait, and return the wait now in force. */
|
|
434
|
+
private noteFailure(remote: string): number {
|
|
435
|
+
const waitMs = nextWebrtcUpgradeBackoffMs(this.backoff.get(remote)?.waitMs);
|
|
436
|
+
this.backoff.set(remote, { waitMs, untilMs: Date.now() + waitMs });
|
|
437
|
+
return waitMs;
|
|
438
|
+
}
|
|
439
|
+
}
|