@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
|
@@ -1,436 +1,440 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Party key rotation: the driver that moves a party onto a fresh membership key in one closed
|
|
3
|
-
* strand after a machine was removed from the cadre, so the removed machine is left holding a
|
|
4
|
-
* key that is no longer a member there (docs/
|
|
5
|
-
*
|
|
6
|
-
* ## What it rests on
|
|
7
|
-
*
|
|
8
|
-
* - A `Strand.MemberPeer` binding is signed by the member key, which every machine of the
|
|
9
|
-
* party holds, so any party machine can bind any peer id under the party's key. Which peer
|
|
10
|
-
* ids are the party's remaining machines, and which the removed machine's, is the control
|
|
11
|
-
* record's `StrandPeer` rows joined to the live machines (`strand-party-peers.ts`).
|
|
12
|
-
* - The revoked-peer gate (`strand-revocation-enforcer.ts`) denies a peer id only when it has
|
|
13
|
-
* an orphaned binding and no live one. A remaining machine bound under both the old key K and
|
|
14
|
-
* the new key K' stays served when K leaves; the removed machine, bound under K alone, is
|
|
15
|
-
* denied on the rows alone, with no new gate logic.
|
|
16
|
-
* - `leaveStrand` is self-signed by the departing key, and `removeManager`'s resign branch
|
|
17
|
-
* needs another manager standing. So K can remove itself once K' is a manager, and no second
|
|
18
|
-
* party is involved in a strand the party manages.
|
|
19
|
-
* - The founder bootstrap skips `Member` and `Manager` when either table has rows, so a founder
|
|
20
|
-
* restart after a rotation never tries to re-seat K' as a founding row.
|
|
21
|
-
*
|
|
22
|
-
* ## Where the new key lives before it is current
|
|
23
|
-
*
|
|
24
|
-
* The removal transaction writes a `PendingStrandPartyKey` row per closed strand the party holds
|
|
25
|
-
* a key for, in the same owner approval as the machine's tombstone (`ControlDatabase.removeMachine`),
|
|
26
|
-
* so the removal and the intent to rotate are exactly as durable as each other. That same
|
|
27
|
-
* transaction reaches the removed machine, so the row carries K' public half in the clear and its
|
|
28
|
-
* private half sealed to the identity keys of the machines that remain (`sealed-party-key.ts`);
|
|
29
|
-
* `CadreNode` opens it under this machine's identity key before handing it to the driver
|
|
30
|
-
* ({@link StrandPartyKeyRotationDeps.readPending}), and a machine the key was not sealed to
|
|
31
|
-
* cannot drive. That row is the run's input; the `StrandPartyKey` row stays K until the last step.
|
|
32
|
-
*
|
|
33
|
-
* ## One run
|
|
34
|
-
*
|
|
35
|
-
* Runs on an OWNER machine that runs the strand and can open the pending key (an owner signer is
|
|
36
|
-
* enrolled, the instance is live with a database, the key is sealed to this machine's identity);
|
|
37
|
-
* `CadreNode` decides that and never asks any other machine to drive.
|
|
38
|
-
* Each run reads the pending row and the strand's rows and continues from wherever the state
|
|
39
|
-
* says it is. Every step reads before it writes, and a duplicate-row rejection counts as done,
|
|
40
|
-
* so two owner machines driving one strand converge:
|
|
41
|
-
*
|
|
42
|
-
* 1. **Clean up superseded keys.** A second removal while a rotation is pending replaced the
|
|
43
|
-
* pending key, and the replaced key may already hold a seat (a driver seated it before the
|
|
44
|
-
* replacement). For each public key in `Superseded` that holds a `Member` row: `removeManager`
|
|
45
|
-
* by K if it holds a `Manager` row, then `revokeMember` by K. Its bindings become orphans
|
|
46
|
-
* beside K's, harmless to remaining machines, which are bound under K and then K'. A
|
|
47
|
-
* superseded key that IS the current key is skipped: another driver finished the earlier
|
|
48
|
-
* rotation and this one continues from it.
|
|
49
|
-
* 2. **Seat K'.** `addMemberByManager(K, K')`. When K holds no `Manager` row and K' is not yet a
|
|
50
|
-
* member, the run stops here as `needs-another-party` (one event per pending stamp) and the
|
|
51
|
-
* row stays: K' is seated from outside, by the membership reconciler redeeming the host's fresh
|
|
52
|
-
* invitation under K' (`strand-membership-reconciler.ts` → "Redeeming under the pending key")
|
|
53
|
-
* or by the strand's manager admitting K' by hand, and the reconciler kicks this driver when
|
|
54
|
-
* it sees the seat (docs/strands.md → Replacing the party's key). The run then continues from
|
|
55
|
-
* step 3; step 4 is skipped, since K is no manager, and step 5 is K's self-signed leave alone.
|
|
56
|
-
* 3. **Bind under K'**: this machine's own strand peer id and every live sibling's, each
|
|
57
|
-
* `registerMemberPeer(K', id)` (insert-if-absent). Self-signed by K', so this step also runs
|
|
58
|
-
* after K has left.
|
|
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
|
-
import
|
|
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
|
-
/** The
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
/** The
|
|
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
|
-
break;
|
|
243
|
-
case '
|
|
244
|
-
outcomes.set(strandId, '
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
break;
|
|
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
|
-
|
|
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
|
-
const
|
|
314
|
-
if (
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
if (current ===
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
if (
|
|
323
|
-
return { kind: '
|
|
324
|
-
}
|
|
325
|
-
const
|
|
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
|
-
continue;
|
|
366
|
-
}
|
|
367
|
-
if (
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
const
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Party key rotation: the driver that moves a party onto a fresh membership key in one closed
|
|
3
|
+
* strand after a machine was removed from the cadre, so the removed machine is left holding a
|
|
4
|
+
* key that is no longer a member there (docs/cadre/membership.md → "Removing a machine").
|
|
5
|
+
*
|
|
6
|
+
* ## What it rests on
|
|
7
|
+
*
|
|
8
|
+
* - A `Strand.MemberPeer` binding is signed by the member key, which every machine of the
|
|
9
|
+
* party holds, so any party machine can bind any peer id under the party's key. Which peer
|
|
10
|
+
* ids are the party's remaining machines, and which the removed machine's, is the control
|
|
11
|
+
* record's `StrandPeer` rows joined to the live machines (`strand-party-peers.ts`).
|
|
12
|
+
* - The revoked-peer gate (`strand-revocation-enforcer.ts`) denies a peer id only when it has
|
|
13
|
+
* an orphaned binding and no live one. A remaining machine bound under both the old key K and
|
|
14
|
+
* the new key K' stays served when K leaves; the removed machine, bound under K alone, is
|
|
15
|
+
* denied on the rows alone, with no new gate logic.
|
|
16
|
+
* - `leaveStrand` is self-signed by the departing key, and `removeManager`'s resign branch
|
|
17
|
+
* needs another manager standing. So K can remove itself once K' is a manager, and no second
|
|
18
|
+
* party is involved in a strand the party manages.
|
|
19
|
+
* - The founder bootstrap skips `Member` and `Manager` when either table has rows, so a founder
|
|
20
|
+
* restart after a rotation never tries to re-seat K' as a founding row.
|
|
21
|
+
*
|
|
22
|
+
* ## Where the new key lives before it is current
|
|
23
|
+
*
|
|
24
|
+
* The removal transaction writes a `PendingStrandPartyKey` row per closed strand the party holds
|
|
25
|
+
* a key for, in the same owner approval as the machine's tombstone (`ControlDatabase.removeMachine`),
|
|
26
|
+
* so the removal and the intent to rotate are exactly as durable as each other. That same
|
|
27
|
+
* transaction reaches the removed machine, so the row carries K' public half in the clear and its
|
|
28
|
+
* private half sealed to the identity keys of the machines that remain (`sealed-party-key.ts`);
|
|
29
|
+
* `CadreNode` opens it under this machine's identity key before handing it to the driver
|
|
30
|
+
* ({@link StrandPartyKeyRotationDeps.readPending}), and a machine the key was not sealed to
|
|
31
|
+
* cannot drive. That row is the run's input; the `StrandPartyKey` row stays K until the last step.
|
|
32
|
+
*
|
|
33
|
+
* ## One run
|
|
34
|
+
*
|
|
35
|
+
* Runs on an OWNER machine that runs the strand and can open the pending key (an owner signer is
|
|
36
|
+
* enrolled, the instance is live with a database, the key is sealed to this machine's identity);
|
|
37
|
+
* `CadreNode` decides that and never asks any other machine to drive.
|
|
38
|
+
* Each run reads the pending row and the strand's rows and continues from wherever the state
|
|
39
|
+
* says it is. Every step reads before it writes, and a duplicate-row rejection counts as done,
|
|
40
|
+
* so two owner machines driving one strand converge:
|
|
41
|
+
*
|
|
42
|
+
* 1. **Clean up superseded keys.** A second removal while a rotation is pending replaced the
|
|
43
|
+
* pending key, and the replaced key may already hold a seat (a driver seated it before the
|
|
44
|
+
* replacement). For each public key in `Superseded` that holds a `Member` row: `removeManager`
|
|
45
|
+
* by K if it holds a `Manager` row, then `revokeMember` by K. Its bindings become orphans
|
|
46
|
+
* beside K's, harmless to remaining machines, which are bound under K and then K'. A
|
|
47
|
+
* superseded key that IS the current key is skipped: another driver finished the earlier
|
|
48
|
+
* rotation and this one continues from it.
|
|
49
|
+
* 2. **Seat K'.** `addMemberByManager(K, K')`. When K holds no `Manager` row and K' is not yet a
|
|
50
|
+
* member, the run stops here as `needs-another-party` (one event per pending stamp) and the
|
|
51
|
+
* row stays: K' is seated from outside, by the membership reconciler redeeming the host's fresh
|
|
52
|
+
* invitation under K' (`strand-membership-reconciler.ts` → "Redeeming under the pending key")
|
|
53
|
+
* or by the strand's manager admitting K' by hand, and the reconciler kicks this driver when
|
|
54
|
+
* it sees the seat (docs/strands/keys.md → Replacing the party's key). The run then continues from
|
|
55
|
+
* step 3; step 4 is skipped, since K is no manager, and step 5 is K's self-signed leave alone.
|
|
56
|
+
* 3. **Bind under K'**: this machine's own strand peer id and every live sibling's, each
|
|
57
|
+
* `registerMemberPeer(K', id)` (insert-if-absent). Self-signed by K', so this step also runs
|
|
58
|
+
* after K has left. Every live machine, whether it stores the strand's blocks or runs it as a
|
|
59
|
+
* client: a machine left unbound here is refused by the party's serving machines once K leaves
|
|
60
|
+
* and cannot write its own binding to recover (`docs/strands/membership.md` → "Binding the
|
|
61
|
+
* party's machines"). The one machine this can miss is one whose `StrandPeer` row has not
|
|
62
|
+
* reached this node yet; a sibling's membership loop binds it under K' when the row arrives.
|
|
63
|
+
* 4. **Promote K'**: `addManager(K, K')`, when K is a manager. After the bindings, so a key that
|
|
64
|
+
* was seated but never bound cannot exist beyond step 2.
|
|
65
|
+
* 5. **K steps down**: `removeManager`'s resign branch signed by K (K' now stands, so the sole
|
|
66
|
+
* manager guard does not fire), then `leaveStrand` signed by K. The removed machine's binding
|
|
67
|
+
* under K is now the orphan; remaining machines are live under K'.
|
|
68
|
+
* 6. **Swap the control row**, one transaction and one owner approval across every strand whose
|
|
69
|
+
* steps 1 to 5 completed in this run ({@link StrandPartyKeyRotationDeps.swapKeys}). Deferred
|
|
70
|
+
* while a control write could fork the party, like every background control write; the
|
|
71
|
+
* strands are reported `pending` and the growth drain re-runs them. After the swap the
|
|
72
|
+
* running instance is re-keyed and `strand:party-key-rotated` is emitted.
|
|
73
|
+
*
|
|
74
|
+
* Crash recovery is the same run: pending row present and K' not a member → step 2; K' a member
|
|
75
|
+
* but K still one → steps 3 to 5 as needed; K gone from the strand, control still K → step 6;
|
|
76
|
+
* no pending row → nothing. A run that finds K' already current (another driver swapped) ends.
|
|
77
|
+
*
|
|
78
|
+
* ## Single flight
|
|
79
|
+
*
|
|
80
|
+
* One run per strand at a time: a call that names a strand already being driven waits for that
|
|
81
|
+
* run's outcome rather than starting a second. Strands in one call are prepared one after
|
|
82
|
+
* another and swapped together.
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
import debug from 'debug';
|
|
86
|
+
import type { Database } from '@quereus/quereus';
|
|
87
|
+
import type { Ed25519KeyPair } from './ed25519-key.js';
|
|
88
|
+
import { strandMemberKeyPair } from './strand-member-key.js';
|
|
89
|
+
import {
|
|
90
|
+
addManager,
|
|
91
|
+
addMemberByManager,
|
|
92
|
+
isStrandManager,
|
|
93
|
+
isStrandMember,
|
|
94
|
+
leaveStrand,
|
|
95
|
+
listMemberPeers,
|
|
96
|
+
registerMemberPeer,
|
|
97
|
+
removeManager,
|
|
98
|
+
revokeMember,
|
|
99
|
+
type StrandWriteOptions,
|
|
100
|
+
} from './strand-membership-writer.js';
|
|
101
|
+
import type { StrandKeyRotationOutcome } from './types.js';
|
|
102
|
+
import { describeThrown } from './describe-thrown.js';
|
|
103
|
+
|
|
104
|
+
const log = debug('sereus:cadre:strand-party-key-rotation');
|
|
105
|
+
|
|
106
|
+
/** Every strand write here runs in a transaction of its own, never joining one the app has open. */
|
|
107
|
+
const OWN_TRANSACTION: StrandWriteOptions = { joinOpenTransaction: false };
|
|
108
|
+
|
|
109
|
+
/** What became of one strand in a run ({@link StrandKeyRotationOutcome.outcome}). */
|
|
110
|
+
export type RotationOutcome = StrandKeyRotationOutcome['outcome'];
|
|
111
|
+
|
|
112
|
+
/** One strand's finished steps 1 to 5, ready for the control swap (step 6). */
|
|
113
|
+
export interface StrandPartyKeySwapEntry {
|
|
114
|
+
strandId: string;
|
|
115
|
+
/** The key the control row names now, encoded as `StrandPartyKey.PrivateKey`. */
|
|
116
|
+
oldKey: string;
|
|
117
|
+
/** The key the strand's rows now name as the party's member, same encoding. */
|
|
118
|
+
newKey: string;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The strand's pending row with its key opened for this machine
|
|
123
|
+
* ({@link StrandPartyKeyRotationDeps.readPending}): what `PendingStrandPartyKeyRow` carries sealed
|
|
124
|
+
* (`sealed-party-key.ts`), in the clear, as the driver seats and binds under it.
|
|
125
|
+
*/
|
|
126
|
+
export interface OpenedPendingPartyKey {
|
|
127
|
+
/** The key being moved to, encoded as `StrandPartyKey.PrivateKey`. */
|
|
128
|
+
privateKey: string;
|
|
129
|
+
/** Public keys (base64url) of earlier pending keys the row replaced; a seat any of them holds must be revoked. */
|
|
130
|
+
superseded: readonly string[];
|
|
131
|
+
/** The row's single-use nonce; `needs-another-party` is reported once per stamp. */
|
|
132
|
+
stampId: string;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The seams the driver reads and writes through; `CadreNode` builds one over its control database and strand manager. */
|
|
136
|
+
export interface StrandPartyKeyRotationDeps {
|
|
137
|
+
/**
|
|
138
|
+
* The strand's pending row opened for this machine, or null when no rotation is pending there.
|
|
139
|
+
* Rejects when it cannot be read, or cannot be opened here (sealed to other machines).
|
|
140
|
+
*/
|
|
141
|
+
readPending(strandId: string): Promise<OpenedPendingPartyKey | null>;
|
|
142
|
+
/** The strand's `StrandPartyKey.PrivateKey`, or null when the party holds no key there. */
|
|
143
|
+
readCurrentKey(strandId: string): Promise<string | null>;
|
|
144
|
+
/**
|
|
145
|
+
* The strand peer ids of the party's other live machines in the strand
|
|
146
|
+
* (`PartyStrandPeers.live`), read fresh. Rejects when they cannot be read.
|
|
147
|
+
*/
|
|
148
|
+
readSiblingPeerIds(strandId: string): Promise<readonly string[]>;
|
|
149
|
+
/** The strand's live database, read per step; `undefined` means the strand does not run here now. */
|
|
150
|
+
getDatabase(strandId: string): Database | undefined;
|
|
151
|
+
/** This machine's own peer id in the strand, read per step. */
|
|
152
|
+
getOwnPeerId(strandId: string): string | undefined;
|
|
153
|
+
/**
|
|
154
|
+
* Step 6: the control-row swap for every entry, one transaction and one owner approval.
|
|
155
|
+
* Resolves the strand ids actually swapped (an entry another driver already swapped is
|
|
156
|
+
* skipped), or `'deferred'` when a control write now could fork the party.
|
|
157
|
+
*/
|
|
158
|
+
swapKeys(entries: readonly StrandPartyKeySwapEntry[]): Promise<readonly string[] | 'deferred'>;
|
|
159
|
+
/** After the swap committed: re-key the running instance and tell the app. */
|
|
160
|
+
onRotated(strandId: string, newKey: string): Promise<void>;
|
|
161
|
+
/** Step 2 found no manager seat for the party; once per pending stamp. */
|
|
162
|
+
onBlocked(strandId: string, reason: 'needs-another-party'): void;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** How far one strand got before the swap. */
|
|
166
|
+
type Prepared =
|
|
167
|
+
| { kind: 'ready'; entry: StrandPartyKeySwapEntry }
|
|
168
|
+
| { kind: 'blocked'; stampId: string }
|
|
169
|
+
| { kind: 'nothing' }
|
|
170
|
+
| { kind: 'failed'; error: unknown };
|
|
171
|
+
|
|
172
|
+
/** The rows of one strand a run reads before it writes. */
|
|
173
|
+
interface RotationState {
|
|
174
|
+
oldKey: Ed25519KeyPair;
|
|
175
|
+
newKey: Ed25519KeyPair;
|
|
176
|
+
oldIsMember: boolean;
|
|
177
|
+
oldIsManager: boolean;
|
|
178
|
+
newIsMember: boolean;
|
|
179
|
+
newIsManager: boolean;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export class StrandPartyKeyRotationDriver {
|
|
183
|
+
/** The run in flight per strand; a call naming the strand joins it. */
|
|
184
|
+
private readonly inFlight = new Map<string, Promise<RotationOutcome>>();
|
|
185
|
+
/** The pending stamp each strand's `needs-another-party` was last reported for. */
|
|
186
|
+
private readonly reportedBlocked = new Map<string, string>();
|
|
187
|
+
|
|
188
|
+
constructor(private readonly deps: StrandPartyKeyRotationDeps) {}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Drive every strand in `strandIds` through the steps its state calls for, then swap the
|
|
192
|
+
* control rows of the ones that finished in one transaction. Never rejects: a strand whose
|
|
193
|
+
* step failed is `pending` with a log, and the next trigger re-runs it.
|
|
194
|
+
*/
|
|
195
|
+
async drive(strandIds: readonly string[], cause: string): Promise<StrandKeyRotationOutcome[]> {
|
|
196
|
+
const ids = [...new Set(strandIds)];
|
|
197
|
+
const joined = ids.filter((strandId) => this.inFlight.has(strandId));
|
|
198
|
+
const fresh = ids.filter((strandId) => !this.inFlight.has(strandId));
|
|
199
|
+
const outcomes = new Map<string, RotationOutcome>();
|
|
200
|
+
if (fresh.length > 0) {
|
|
201
|
+
for (const [strandId, outcome] of await this.startRun(fresh, cause)) {
|
|
202
|
+
outcomes.set(strandId, outcome);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (joined.length > 0) {
|
|
206
|
+
// The run in flight read its pending rows before this call was made, so a row written
|
|
207
|
+
// since (a second removal) would otherwise wait for the next trigger: the joined strands
|
|
208
|
+
// are driven again once it ends. A strand with nothing left costs one pending read.
|
|
209
|
+
await Promise.all(joined.map((strandId) => this.inFlight.get(strandId)));
|
|
210
|
+
for (const outcome of await this.drive(joined, cause)) {
|
|
211
|
+
outcomes.set(outcome.strandId, outcome.outcome);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return ids.map((strandId) => ({ strandId, outcome: outcomes.get(strandId) ?? 'pending' }));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Start one run and register it as the run in flight for each of its strands until it ends. `run` never rejects, so neither does the registration. */
|
|
218
|
+
private startRun(strandIds: readonly string[], cause: string): Promise<Map<string, RotationOutcome>> {
|
|
219
|
+
const run = this.run(strandIds, cause);
|
|
220
|
+
for (const strandId of strandIds) {
|
|
221
|
+
const own = run.then((result) => result.get(strandId) ?? 'pending');
|
|
222
|
+
this.inFlight.set(strandId, own);
|
|
223
|
+
void own.finally(() => {
|
|
224
|
+
if (this.inFlight.get(strandId) === own) {
|
|
225
|
+
this.inFlight.delete(strandId);
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
return run;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Steps 1 to 5 per strand, then one swap over the ready ones. */
|
|
233
|
+
private async run(strandIds: readonly string[], cause: string): Promise<Map<string, RotationOutcome>> {
|
|
234
|
+
log('Party key rotation run over %d strand(s) (%s)', strandIds.length, cause);
|
|
235
|
+
const outcomes = new Map<string, RotationOutcome>();
|
|
236
|
+
const ready: StrandPartyKeySwapEntry[] = [];
|
|
237
|
+
for (const strandId of strandIds) {
|
|
238
|
+
const prepared = await this.prepare(strandId);
|
|
239
|
+
switch (prepared.kind) {
|
|
240
|
+
case 'ready':
|
|
241
|
+
ready.push(prepared.entry);
|
|
242
|
+
break;
|
|
243
|
+
case 'blocked':
|
|
244
|
+
outcomes.set(strandId, 'needs-another-party');
|
|
245
|
+
this.reportBlocked(strandId, prepared.stampId);
|
|
246
|
+
break;
|
|
247
|
+
case 'nothing':
|
|
248
|
+
outcomes.set(strandId, 'rotated');
|
|
249
|
+
break;
|
|
250
|
+
case 'failed':
|
|
251
|
+
outcomes.set(strandId, 'pending');
|
|
252
|
+
log('Party key rotation of strand %s left unfinished (retried on the next trigger): %s', strandId, describeThrown(prepared.error));
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
for (const [strandId, outcome] of await this.swap(ready)) {
|
|
257
|
+
outcomes.set(strandId, outcome);
|
|
258
|
+
}
|
|
259
|
+
return outcomes;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** Step 6 for every ready strand, then the re-key of each one swapped. */
|
|
263
|
+
private async swap(ready: readonly StrandPartyKeySwapEntry[]): Promise<Map<string, RotationOutcome>> {
|
|
264
|
+
const outcomes = new Map<string, RotationOutcome>();
|
|
265
|
+
if (ready.length === 0) {
|
|
266
|
+
return outcomes;
|
|
267
|
+
}
|
|
268
|
+
let swapped: readonly string[];
|
|
269
|
+
try {
|
|
270
|
+
const result = await this.deps.swapKeys(ready);
|
|
271
|
+
if (result === 'deferred') {
|
|
272
|
+
log('Party key swap of %d strand(s) deferred: no sibling reachable and the party has other members; re-run on growth', ready.length);
|
|
273
|
+
for (const entry of ready) {
|
|
274
|
+
outcomes.set(entry.strandId, 'pending');
|
|
275
|
+
}
|
|
276
|
+
return outcomes;
|
|
277
|
+
}
|
|
278
|
+
swapped = result;
|
|
279
|
+
} catch (error) {
|
|
280
|
+
log('Party key swap of %d strand(s) failed (retried on the next trigger): %s', ready.length, describeThrown(error));
|
|
281
|
+
for (const entry of ready) {
|
|
282
|
+
outcomes.set(entry.strandId, 'pending');
|
|
283
|
+
}
|
|
284
|
+
return outcomes;
|
|
285
|
+
}
|
|
286
|
+
for (const entry of ready) {
|
|
287
|
+
// An entry the swap skipped names a key already current (another driver swapped it), or a
|
|
288
|
+
// row that moved on; either way the strand's rows are on the new key, so it is rotated.
|
|
289
|
+
outcomes.set(entry.strandId, 'rotated');
|
|
290
|
+
if (swapped.includes(entry.strandId)) {
|
|
291
|
+
await this.notifyRotated(entry);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return outcomes;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* The re-key after a committed swap. A failure is logged and the strand stays `rotated`: the
|
|
299
|
+
* control row names the new key, and the party key watch re-keys the instance from it on its
|
|
300
|
+
* next run (`CadreNode.onPartyKeyTablesChanged`).
|
|
301
|
+
*/
|
|
302
|
+
private async notifyRotated(entry: StrandPartyKeySwapEntry): Promise<void> {
|
|
303
|
+
try {
|
|
304
|
+
await this.deps.onRotated(entry.strandId, entry.newKey);
|
|
305
|
+
} catch (error) {
|
|
306
|
+
log('Party key rotation of strand %s: re-keying the running instance after the swap failed (the party key watch retries it): %s', entry.strandId, describeThrown(error));
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** Steps 1 to 5 for one strand, from the state its rows are in. */
|
|
311
|
+
private async prepare(strandId: string): Promise<Prepared> {
|
|
312
|
+
try {
|
|
313
|
+
const pending = await this.deps.readPending(strandId);
|
|
314
|
+
if (pending === null) {
|
|
315
|
+
return { kind: 'nothing' };
|
|
316
|
+
}
|
|
317
|
+
const current = await this.deps.readCurrentKey(strandId);
|
|
318
|
+
if (current === null) {
|
|
319
|
+
log('Party key rotation of strand %s: the party holds no key there; nothing to rotate', strandId);
|
|
320
|
+
return { kind: 'nothing' };
|
|
321
|
+
}
|
|
322
|
+
if (current === pending.privateKey) {
|
|
323
|
+
return { kind: 'nothing' };
|
|
324
|
+
}
|
|
325
|
+
const db = this.deps.getDatabase(strandId);
|
|
326
|
+
if (db === undefined) {
|
|
327
|
+
return { kind: 'failed', error: new Error('the strand does not run here now') };
|
|
328
|
+
}
|
|
329
|
+
const state = await readRotationState(db, current, pending.privateKey);
|
|
330
|
+
await this.cleanUpSuperseded(db, state, pending.superseded);
|
|
331
|
+
if (!state.newIsMember) {
|
|
332
|
+
if (!state.oldIsManager) {
|
|
333
|
+
return { kind: 'blocked', stampId: pending.stampId };
|
|
334
|
+
}
|
|
335
|
+
await addMemberByManager(db, { managerKeyPair: state.oldKey, memberKey: state.newKey.publicKeyB64 }, OWN_TRANSACTION);
|
|
336
|
+
log('Party key rotation of strand %s: new key seated as member', strandId);
|
|
337
|
+
}
|
|
338
|
+
await this.bindMachines(strandId, db, state.newKey);
|
|
339
|
+
if (state.oldIsManager && !state.newIsManager) {
|
|
340
|
+
await addManager(db, { byManagerKeyPair: state.oldKey, newManagerKey: state.newKey.publicKeyB64 }, OWN_TRANSACTION);
|
|
341
|
+
log('Party key rotation of strand %s: new key promoted to manager', strandId);
|
|
342
|
+
}
|
|
343
|
+
await stepDown(db, state);
|
|
344
|
+
return { kind: 'ready', entry: { strandId, oldKey: current, newKey: pending.privateKey } };
|
|
345
|
+
} catch (error) {
|
|
346
|
+
return { kind: 'failed', error };
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Step 1: a superseded key holding a seat is revoked by K. Skips the current key itself
|
|
352
|
+
* (another driver finished the rotation it was pending for) and, when K holds no manager seat,
|
|
353
|
+
* leaves the seat with a log for the other party's manager.
|
|
354
|
+
*
|
|
355
|
+
* NOTE: a superseded key that holds a seat in a strand the party does not manage can only be
|
|
356
|
+
* revoked by that strand's manager: the row carries superseded PUBLIC halves only, so this
|
|
357
|
+
* driver cannot `leaveStrand` as that key. Fine now, since reaching it takes a second removal
|
|
358
|
+
* between the host's re-admission of the first pending key and the swap that follows it within
|
|
359
|
+
* seconds on the redeeming owner machine. If it is ever hit, seal the superseded private halves
|
|
360
|
+
* into the row too, so the driver can self-leave them.
|
|
361
|
+
*/
|
|
362
|
+
private async cleanUpSuperseded(db: Database, state: RotationState, superseded: readonly string[]): Promise<void> {
|
|
363
|
+
for (const key of superseded) {
|
|
364
|
+
if (key === state.oldKey.publicKeyB64 || key === state.newKey.publicKeyB64 || !await isStrandMember(db, key)) {
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
if (!state.oldIsManager) {
|
|
368
|
+
log('Superseded party key %s still holds a seat and this party has no manager seat to revoke it; left for the other party\'s manager', key);
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
if (await isStrandManager(db, key)) {
|
|
372
|
+
await removeManager(db, { byManagerKeyPair: state.oldKey, targetManagerKey: key }, OWN_TRANSACTION);
|
|
373
|
+
}
|
|
374
|
+
await revokeMember(db, { managerKeyPair: state.oldKey, memberKey: key }, OWN_TRANSACTION);
|
|
375
|
+
log('Superseded party key %s revoked', key);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** Step 3: every remaining machine's peer id bound under the new key; bindings already present are skipped. */
|
|
380
|
+
private async bindMachines(strandId: string, db: Database, newKey: Ed25519KeyPair): Promise<void> {
|
|
381
|
+
const own = this.deps.getOwnPeerId(strandId);
|
|
382
|
+
const ids = new Set(await this.deps.readSiblingPeerIds(strandId));
|
|
383
|
+
if (own !== undefined) {
|
|
384
|
+
ids.add(own);
|
|
385
|
+
}
|
|
386
|
+
const bound = new Set(await listMemberPeers(db, newKey.publicKeyB64));
|
|
387
|
+
for (const peerId of ids) {
|
|
388
|
+
if (!bound.has(peerId)) {
|
|
389
|
+
await registerMemberPeer(db, { memberKeyPair: newKey, peerId }, OWN_TRANSACTION);
|
|
390
|
+
log('Party key rotation of strand %s: %s bound under the new key', strandId, peerId);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
private reportBlocked(strandId: string, stampId: string): void {
|
|
396
|
+
if (this.reportedBlocked.get(strandId) === stampId) {
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
this.reportedBlocked.set(strandId, stampId);
|
|
400
|
+
log('Party key rotation of strand %s blocked: this party holds no manager seat there, so another party\'s manager has to admit the new key', strandId);
|
|
401
|
+
try {
|
|
402
|
+
this.deps.onBlocked(strandId, 'needs-another-party');
|
|
403
|
+
} catch (error) {
|
|
404
|
+
log('Party key rotation of strand %s: the blocked report threw: %s', strandId, describeThrown(error));
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** The rows steps 1 to 5 decide from, read before any write. */
|
|
410
|
+
async function readRotationState(db: Database, currentKey: string, pendingKey: string): Promise<RotationState> {
|
|
411
|
+
const oldKey = strandMemberKeyPair(currentKey);
|
|
412
|
+
const newKey = strandMemberKeyPair(pendingKey);
|
|
413
|
+
return {
|
|
414
|
+
oldKey,
|
|
415
|
+
newKey,
|
|
416
|
+
oldIsMember: await isStrandMember(db, oldKey.publicKeyB64),
|
|
417
|
+
oldIsManager: await isStrandManager(db, oldKey.publicKeyB64),
|
|
418
|
+
newIsMember: await isStrandMember(db, newKey.publicKeyB64),
|
|
419
|
+
newIsManager: await isStrandManager(db, newKey.publicKeyB64),
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Step 5: K resigns its manager seat, then leaves. Both writers are no-ops over an absent row,
|
|
425
|
+
* so a run that finds K already gone writes nothing here. The resign runs only when K' holds a
|
|
426
|
+
* manager seat, so the strand never loses its last manager to this step.
|
|
427
|
+
*/
|
|
428
|
+
async function stepDown(db: Database, state: RotationState): Promise<void> {
|
|
429
|
+
if (!state.oldIsMember) {
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
if (state.oldIsManager) {
|
|
433
|
+
if (!await isStrandManager(db, state.newKey.publicKeyB64)) {
|
|
434
|
+
throw new Error('the new party key holds no manager seat, so the old key cannot resign without emptying the strand\'s managers');
|
|
435
|
+
}
|
|
436
|
+
await removeManager(db, { byManagerKeyPair: state.oldKey, targetManagerKey: state.oldKey.publicKeyB64 }, OWN_TRANSACTION);
|
|
437
|
+
}
|
|
438
|
+
await leaveStrand(db, { memberKeyPair: state.oldKey }, OWN_TRANSACTION);
|
|
439
|
+
log('Old party key %s left the strand', state.oldKey.publicKeyB64);
|
|
440
|
+
}
|