@serfab/cadre-core 1.21.0 → 1.23.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 +294 -294
- package/dist/cadre-node.d.ts +334 -53
- package/dist/cadre-node.js +794 -85
- package/dist/cadre-node.js.map +1 -1
- package/dist/control-authorization.d.ts +32 -2
- package/dist/control-authorization.js +19 -0
- package/dist/control-authorization.js.map +1 -1
- package/dist/control-database.d.ts +236 -23
- package/dist/control-database.js +587 -173
- package/dist/control-database.js.map +1 -1
- package/dist/control-schema.d.ts +3 -3
- package/dist/control-schema.js +71 -7
- package/dist/control-schema.js.map +1 -1
- package/dist/ed25519-key.d.ts +8 -0
- package/dist/ed25519-key.js +11 -1
- package/dist/ed25519-key.js.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +13 -1
- package/dist/index.js.map +1 -1
- package/dist/machine-removal.d.ts +51 -1
- package/dist/machine-removal.js +42 -1
- package/dist/machine-removal.js.map +1 -1
- package/dist/membership-connection-gater.d.ts +8 -0
- package/dist/membership-connection-gater.js +13 -1
- package/dist/membership-connection-gater.js.map +1 -1
- package/dist/membership-view.js +4 -1
- package/dist/membership-view.js.map +1 -1
- package/dist/owner-keys.js +2 -1
- package/dist/owner-keys.js.map +1 -1
- package/dist/owner-signer.js +3 -4
- package/dist/owner-signer.js.map +1 -1
- package/dist/peer-authorization.d.ts +14 -2
- package/dist/peer-authorization.js +24 -1
- package/dist/peer-authorization.js.map +1 -1
- package/dist/sealed-party-key.d.ts +32 -0
- package/dist/sealed-party-key.js +162 -0
- package/dist/sealed-party-key.js.map +1 -0
- package/dist/seed-bootstrap.d.ts +10 -3
- package/dist/seed-bootstrap.js +12 -0
- package/dist/seed-bootstrap.js.map +1 -1
- package/dist/strand-addr-protocol.d.ts +56 -6
- package/dist/strand-addr-protocol.js +57 -5
- package/dist/strand-addr-protocol.js.map +1 -1
- package/dist/strand-commit-durability.d.ts +33 -2
- package/dist/strand-commit-durability.js +44 -4
- package/dist/strand-commit-durability.js.map +1 -1
- package/dist/strand-database.d.ts +35 -5
- package/dist/strand-database.js +34 -10
- package/dist/strand-database.js.map +1 -1
- package/dist/strand-instance-manager.d.ts +200 -18
- package/dist/strand-instance-manager.js +434 -64
- package/dist/strand-instance-manager.js.map +1 -1
- package/dist/strand-membership-reconciler.d.ts +250 -51
- package/dist/strand-membership-reconciler.js +410 -66
- package/dist/strand-membership-reconciler.js.map +1 -1
- package/dist/strand-membership-writer.d.ts +7 -0
- package/dist/strand-membership-writer.js +9 -0
- package/dist/strand-membership-writer.js.map +1 -1
- package/dist/strand-party-key-rotation.d.ts +177 -0
- package/dist/strand-party-key-rotation.js +340 -0
- package/dist/strand-party-key-rotation.js.map +1 -0
- package/dist/strand-party-machines.d.ts +111 -0
- package/dist/strand-party-machines.js +258 -0
- package/dist/strand-party-machines.js.map +1 -0
- package/dist/strand-party-peers.d.ts +101 -0
- package/dist/strand-party-peers.js +49 -0
- package/dist/strand-party-peers.js.map +1 -0
- package/dist/strand-peer-redial.d.ts +34 -7
- package/dist/strand-peer-redial.js +46 -13
- package/dist/strand-peer-redial.js.map +1 -1
- package/dist/strand-revocation-enforcer.d.ts +15 -1
- package/dist/strand-revocation-enforcer.js +16 -9
- package/dist/strand-revocation-enforcer.js.map +1 -1
- package/dist/strand-transport-key.js +3 -0
- package/dist/strand-transport-key.js.map +1 -1
- package/dist/strand-watcher.d.ts +7 -4
- package/dist/strand-watcher.js +12 -7
- package/dist/strand-watcher.js.map +1 -1
- package/dist/types.d.ts +169 -13
- package/dist/types.js.map +1 -1
- package/package.json +9 -8
- package/src/cadre-node.ts +12280 -11528
- package/src/control-authorization.ts +41 -1
- package/src/control-database.ts +6123 -5603
- package/src/control-schema.ts +71 -7
- package/src/ed25519-key.ts +12 -1
- package/src/index.ts +957 -924
- package/src/machine-removal.ts +80 -1
- package/src/membership-connection-gater.ts +13 -1
- package/src/membership-view.ts +4 -1
- package/src/owner-keys.ts +2 -1
- package/src/owner-signer.ts +92 -94
- package/src/peer-authorization.ts +27 -3
- package/src/sealed-party-key.ts +190 -0
- package/src/seed-bootstrap.ts +17 -3
- package/src/strand-addr-protocol.ts +67 -6
- package/src/strand-commit-durability.ts +54 -5
- package/src/strand-database.ts +47 -11
- package/src/strand-instance-manager.ts +524 -65
- package/src/strand-membership-reconciler.ts +1310 -942
- package/src/strand-membership-writer.ts +10 -0
- package/src/strand-party-key-rotation.ts +436 -0
- package/src/strand-party-machines.ts +293 -0
- package/src/strand-party-peers.ts +129 -0
- package/src/strand-peer-redial.ts +61 -14
- package/src/strand-revocation-enforcer.ts +27 -9
- package/src/strand-transport-key.ts +3 -0
- package/src/strand-watcher.ts +12 -7
- package/src/types.ts +3121 -2963
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The party's other machines known to hold one strand, and the offline queue's gate that waits
|
|
3
|
+
* for them (`docs/strands.md` → Waiting for the party's own machines).
|
|
4
|
+
*
|
|
5
|
+
* Why. Optimystic's own reachability gate (`KeyNetworkCohortReachability` in db-p2p) knows only
|
|
6
|
+
* the peers this strand node has itself met and seen serve the network. A sibling of the party
|
|
7
|
+
* that this node has never connected to is not among them, so with the sibling's port blocked
|
|
8
|
+
* the gate answers "genuinely alone" and a queued transaction is committed by this machine
|
|
9
|
+
* alone. The party's control database knows better: every machine that runs the strand has
|
|
10
|
+
* written a `CadreControl.StrandPeer` row for it (`strand-party-peers.ts`), so the machines a
|
|
11
|
+
* queued transaction must not be committed without are the live, trusted machines with such a
|
|
12
|
+
* row.
|
|
13
|
+
*
|
|
14
|
+
* What it does. {@link StrandPartyMachines} keeps the last good read of those machines, read
|
|
15
|
+
* through the launch's `PartyStrandPeerSource` at bring-up and again whenever the control
|
|
16
|
+
* database's party tables change (`StrandInstanceManager.notifyPartyPeersChanged`).
|
|
17
|
+
* {@link cohortGateWaitingForMachines} composes the node's gate with it: a block whose cohort
|
|
18
|
+
* names nobody but this node is unreachable while the party has a machine holding the strand.
|
|
19
|
+
* The composed gate is registered with the strand connection's collection factory
|
|
20
|
+
* (`StrandConnectionOptions.cohortReachability`), where the queue and the offline live-read
|
|
21
|
+
* path both ask it. Only a node offering offline mode builds either.
|
|
22
|
+
*
|
|
23
|
+
* Removal. The same read names the machines removed from the cadre. Optimystic's own gate, which
|
|
24
|
+
* runs first, keeps waiting for a machine its strand node remembers or was seeded with, so
|
|
25
|
+
* `StrandInstanceManager` refuses a removed machine's peer id as a member, and
|
|
26
|
+
* {@link watchRemovedMachinesInSeed} asks for a rebuild when one is in the running node's seed.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import debug from 'debug';
|
|
30
|
+
import type { BlockId, ICohortReachability, IKeyNetwork } from '@optimystic/db-core';
|
|
31
|
+
import { routingKeyForBlock } from '@optimystic/db-core';
|
|
32
|
+
import { describeThrown } from './describe-thrown.js';
|
|
33
|
+
import type { PartyStrandPeer, PartyStrandPeerSource, PartyStrandPeers } from './strand-party-peers.js';
|
|
34
|
+
|
|
35
|
+
const log = debug('sereus:cadre:strand-party-machines');
|
|
36
|
+
|
|
37
|
+
export interface StrandPartyMachinesDeps {
|
|
38
|
+
/** Names the strand in logs. */
|
|
39
|
+
label: string;
|
|
40
|
+
source: Pick<PartyStrandPeerSource, 'read'>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The party's other machines known to hold one strand, from the last good read of the control
|
|
45
|
+
* database. Built per runtime build, before the strand's node, and stopped with the runtime.
|
|
46
|
+
*
|
|
47
|
+
* NOTE: a machine that stops running the strand but stays in the cadre (its filter changed, its
|
|
48
|
+
* app was removed, `stopStrand`) keeps its `StrandPeer` row, so it keeps counting here until it
|
|
49
|
+
* is removed from the cadre, as a peer that never returns keeps counting in Optimystic's own
|
|
50
|
+
* gate. If operators hit this, add a strand-address reply saying the filter excludes the strand,
|
|
51
|
+
* and drop a machine that answers so.
|
|
52
|
+
*
|
|
53
|
+
* NOTE: {@link settled} waits on the first control read, and the gate waits on it, so a stalled
|
|
54
|
+
* read would stall the queue and offline live reads with it. The read is the local one
|
|
55
|
+
* `connectedSiblingTargets`' NOTE in `cadre-node.ts` measured in milliseconds; if a control read
|
|
56
|
+
* ever can stall, bound this one and answer "no machines" past the bound.
|
|
57
|
+
*/
|
|
58
|
+
export class StrandPartyMachines {
|
|
59
|
+
private last: readonly PartyStrandPeer[] = [];
|
|
60
|
+
private lastRemoved: ReadonlySet<string> = new Set();
|
|
61
|
+
private failed = false;
|
|
62
|
+
private inFlight: Promise<void> | null = null;
|
|
63
|
+
/** A refresh asked while a read was in flight: one more read follows, so no change is missed. */
|
|
64
|
+
private again = false;
|
|
65
|
+
private stopped = false;
|
|
66
|
+
/** One entry per `onChange` call, so a listener subscribed twice is two subscriptions. */
|
|
67
|
+
private readonly listeners = new Set<{ readonly listener: () => void }>();
|
|
68
|
+
private readonly firstRead: Promise<void>;
|
|
69
|
+
private settleFirstRead: (() => void) | null = null;
|
|
70
|
+
|
|
71
|
+
constructor(private readonly deps: StrandPartyMachinesDeps) {
|
|
72
|
+
this.firstRead = new Promise<void>((resolve) => {
|
|
73
|
+
this.settleFirstRead = resolve;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Read again now: coalesced, one read in flight. A failed read logs and keeps the last answer. */
|
|
78
|
+
refresh(): void {
|
|
79
|
+
if (this.stopped) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (this.inFlight !== null) {
|
|
83
|
+
this.again = true;
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
this.inFlight = this.read().finally(() => {
|
|
87
|
+
this.inFlight = null;
|
|
88
|
+
this.settleFirstRead?.();
|
|
89
|
+
this.settleFirstRead = null;
|
|
90
|
+
if (this.again) {
|
|
91
|
+
this.again = false;
|
|
92
|
+
this.refresh();
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Settles once the first read has finished, either way. */
|
|
98
|
+
settled(): Promise<void> {
|
|
99
|
+
return this.firstRead;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The last good read's live machines; empty before one. */
|
|
103
|
+
machines(): readonly PartyStrandPeer[] {
|
|
104
|
+
return this.last;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The strand peer ids of machines removed from the cadre, from the last good read; empty before one. */
|
|
108
|
+
removed(): ReadonlySet<string> {
|
|
109
|
+
return this.lastRemoved;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Whether the most recent read failed, so the answer in {@link machines} may be stale. */
|
|
113
|
+
lastReadFailed(): boolean {
|
|
114
|
+
return this.failed;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Called only when the live machines or the removed ids change. Returns an idempotent unsubscribe. */
|
|
118
|
+
onChange(listener: () => void): () => void {
|
|
119
|
+
if (this.stopped) {
|
|
120
|
+
return () => undefined;
|
|
121
|
+
}
|
|
122
|
+
const subscription = { listener };
|
|
123
|
+
this.listeners.add(subscription);
|
|
124
|
+
return () => {
|
|
125
|
+
this.listeners.delete(subscription);
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Drops every listener; a read in flight keeps nothing. */
|
|
130
|
+
stop(): void {
|
|
131
|
+
this.stopped = true;
|
|
132
|
+
this.listeners.clear();
|
|
133
|
+
// A gate waiting on a tracker that never read must not wait forever.
|
|
134
|
+
this.settleFirstRead?.();
|
|
135
|
+
this.settleFirstRead = null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private async read(): Promise<void> {
|
|
139
|
+
let peers: PartyStrandPeers;
|
|
140
|
+
try {
|
|
141
|
+
peers = await this.deps.source.read();
|
|
142
|
+
} catch (error) {
|
|
143
|
+
this.failed = true;
|
|
144
|
+
log('strand %s: the party\'s machines could not be read (keeping %d known): %s',
|
|
145
|
+
this.deps.label, this.last.length, describeThrown(error));
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
this.failed = false;
|
|
149
|
+
if (this.stopped || (sameMachines(this.last, peers.live) && sameIds(this.lastRemoved, peers.removed))) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
this.last = peers.live;
|
|
153
|
+
this.lastRemoved = new Set(peers.removed);
|
|
154
|
+
log('strand %s: the party has %d other machine(s) holding the strand and %d removed', this.deps.label, peers.live.length, peers.removed.length);
|
|
155
|
+
for (const { listener } of [...this.listeners]) {
|
|
156
|
+
try {
|
|
157
|
+
listener();
|
|
158
|
+
} catch (error) {
|
|
159
|
+
log('strand %s: a machines listener threw: %o', this.deps.label, error);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function sameMachines(a: readonly PartyStrandPeer[], b: readonly PartyStrandPeer[]): boolean {
|
|
166
|
+
if (a.length !== b.length) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
const keys = new Set(a.map(machineKey));
|
|
170
|
+
return b.every((machine) => keys.has(machineKey(machine)));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function machineKey(machine: PartyStrandPeer): string {
|
|
174
|
+
return `${machine.peerId}/${machine.strandPeerId}`;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function sameIds(known: ReadonlySet<string>, ids: readonly string[]): boolean {
|
|
178
|
+
return known.size === ids.length && ids.every((id) => known.has(id));
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Call `onFound` when `machines` names a removed machine that is in `seedPeers`, the peers of the
|
|
183
|
+
* launch seed this runtime's node was built with, so that the node is rebuilt from a fresh seed.
|
|
184
|
+
* A rebuild is the only remedy: the node's seed is frozen, and Optimystic reads a refused seed peer
|
|
185
|
+
* that serves the network as not yet heard from for the node's whole life (`heardFromBootstrapPeer`).
|
|
186
|
+
*
|
|
187
|
+
* `announced` is the strand's memory across its runtimes of the ids a rebuild was asked for, so an
|
|
188
|
+
* id still in the rebuilt node's seed asks once and not on every build. Ids this seed does not name
|
|
189
|
+
* are forgotten, so a machine removed, re-added, seeded again and removed again asks again.
|
|
190
|
+
*
|
|
191
|
+
* NOTE: a fresh seed should never name a removed machine: its sources are the answers of live
|
|
192
|
+
* siblings and a formation's addresses, which are another party's. The exception is a removed
|
|
193
|
+
* machine still configured as this node's relay and still answering the strand-addr RPC. The hold
|
|
194
|
+
* then stays and the log says why; the remedy is to take it out of `network.relayAddrs`.
|
|
195
|
+
*/
|
|
196
|
+
export function watchRemovedMachinesInSeed(
|
|
197
|
+
machines: Pick<StrandPartyMachines, 'removed' | 'onChange'>,
|
|
198
|
+
seedPeers: ReadonlySet<string>,
|
|
199
|
+
announced: Set<string>,
|
|
200
|
+
onFound: () => void,
|
|
201
|
+
label: string
|
|
202
|
+
): void {
|
|
203
|
+
for (const peerId of announced) {
|
|
204
|
+
if (!seedPeers.has(peerId)) {
|
|
205
|
+
announced.delete(peerId);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
machines.onChange(() => {
|
|
209
|
+
const inSeed = [...machines.removed()].filter((peerId) => seedPeers.has(peerId));
|
|
210
|
+
const unannounced = inSeed.filter((peerId) => !announced.has(peerId));
|
|
211
|
+
if (unannounced.length > 0) {
|
|
212
|
+
unannounced.forEach((peerId) => announced.add(peerId));
|
|
213
|
+
onFound();
|
|
214
|
+
} else if (inSeed.length > 0) {
|
|
215
|
+
log('strand %s: the seed still names removed machine(s) %o after a rebuild, so the queue stays held', label, inSeed);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** The slice of {@link StrandPartyMachines} the composed gate reads. */
|
|
221
|
+
export type CohortGateMachines = Pick<StrandPartyMachines, 'settled' | 'machines' | 'lastReadFailed' | 'refresh' | 'onChange'>;
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* `inner` (the node's gate), closed also while this node is alone in a block's cohort and the
|
|
225
|
+
* party has machines holding the strand. In order: `inner` closed answers closed and nothing
|
|
226
|
+
* further is read; then the first machine read is awaited, and a failed last read is asked again
|
|
227
|
+
* (the retry rides the asker's own re-check, so no timer here); no machines answers open;
|
|
228
|
+
* otherwise every distinct block's cohort is read from the key network, and one naming nobody
|
|
229
|
+
* but this node, or one whose lookup throws, answers closed.
|
|
230
|
+
*
|
|
231
|
+
* The test is "alone in the cohort", not "a machine is unconnected": with one sibling connected
|
|
232
|
+
* and another blocked the cohort has two members, the commit runs on it, and nothing is held.
|
|
233
|
+
* The same goes for another party's member being connected. `onChange` forwards both the node
|
|
234
|
+
* gate's wakes and a change of the machines.
|
|
235
|
+
*/
|
|
236
|
+
export function cohortGateWaitingForMachines(
|
|
237
|
+
inner: ICohortReachability,
|
|
238
|
+
keyNetwork: Pick<IKeyNetwork, 'findCluster'>,
|
|
239
|
+
selfId: string,
|
|
240
|
+
machines: CohortGateMachines
|
|
241
|
+
): ICohortReachability {
|
|
242
|
+
/** The previous answer, so the log marks each change rather than each ask. */
|
|
243
|
+
let lastAnswer: boolean | undefined;
|
|
244
|
+
const answer = (open: boolean, why: string): boolean => {
|
|
245
|
+
if (open !== lastAnswer) {
|
|
246
|
+
lastAnswer = open;
|
|
247
|
+
log('gate %s: %s', open ? 'open' : 'closed', why);
|
|
248
|
+
}
|
|
249
|
+
return open;
|
|
250
|
+
};
|
|
251
|
+
const aloneInCohort = async (blockId: BlockId): Promise<boolean | 'lookup-failed'> => {
|
|
252
|
+
try {
|
|
253
|
+
const cohort = Object.keys(await keyNetwork.findCluster(routingKeyForBlock(blockId)));
|
|
254
|
+
return !cohort.some((peerId) => peerId !== selfId);
|
|
255
|
+
} catch (error) {
|
|
256
|
+
log('gate: the cohort of block %s could not be read: %s', blockId, describeThrown(error));
|
|
257
|
+
return 'lookup-failed';
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
return {
|
|
261
|
+
async reachable(blockIds) {
|
|
262
|
+
if (!(await inner.reachable(blockIds))) {
|
|
263
|
+
return answer(false, 'the node\'s own gate is closed');
|
|
264
|
+
}
|
|
265
|
+
await machines.settled();
|
|
266
|
+
if (machines.lastReadFailed()) {
|
|
267
|
+
machines.refresh();
|
|
268
|
+
}
|
|
269
|
+
const known = machines.machines();
|
|
270
|
+
if (known.length === 0) {
|
|
271
|
+
return answer(true, 'the party has no other machine holding the strand');
|
|
272
|
+
}
|
|
273
|
+
for (const blockId of new Set(blockIds)) {
|
|
274
|
+
const alone = await aloneInCohort(blockId);
|
|
275
|
+
if (alone === 'lookup-failed') {
|
|
276
|
+
return answer(false, `block ${blockId}'s cohort could not be read`);
|
|
277
|
+
}
|
|
278
|
+
if (alone) {
|
|
279
|
+
return answer(false, `alone in block ${blockId}'s cohort while ${known.length} party machine(s) hold the strand`);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return answer(true, 'another member is in every cohort');
|
|
283
|
+
},
|
|
284
|
+
onChange(listener) {
|
|
285
|
+
const releaseInner = inner.onChange?.(listener);
|
|
286
|
+
const releaseMachines = machines.onChange(listener);
|
|
287
|
+
return () => {
|
|
288
|
+
releaseInner?.();
|
|
289
|
+
releaseMachines();
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A party's machines as one strand sees them: which strand peer ids belong to the party's live
|
|
3
|
+
* machines and which to machines removed from the cadre, projected from the control database.
|
|
4
|
+
* The inputs are the strand's `CadreControl.StrandPeer` rows (each machine's own signed
|
|
5
|
+
* statement of its peer id in the strand), the live `CadrePeer` rows judged by this node's
|
|
6
|
+
* membership predicate, and the `CadrePeer` tombstones.
|
|
7
|
+
*
|
|
8
|
+
* Two readers. The closed-strand membership reconciler (`strand-membership-reconciler.ts`)
|
|
9
|
+
* binds every live sibling under the party's current member key and clears a removed
|
|
10
|
+
* machine's binding, so a sibling that was offline when the party's key changed is already
|
|
11
|
+
* bound when it returns, and a machine wrongly bound under a key is cleared. Who binds whom is
|
|
12
|
+
* stated in `docs/strands.md` → Closed-Strand Member Key Handling. The same loop reads the
|
|
13
|
+
* party's pending key through this seam ({@link PartyStrandPeerSource.pendingKey}): while the
|
|
14
|
+
* party is moving to another key in the strand, a staged invitation is redeemed under that key
|
|
15
|
+
* rather than the current one (`docs/strands.md` → Replacing the party's key). On a node
|
|
16
|
+
* offering offline mode, `strand-party-machines.ts` reads the live machines of every strand,
|
|
17
|
+
* open or closed, as the machines a queued transaction must not be committed without
|
|
18
|
+
* (`docs/strands.md` → Waiting for the party's own machines).
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import type { OpenedPendingPartyKey } from './strand-party-key-rotation.js';
|
|
22
|
+
import type { CadreMachine, RevocationRow, StrandPeerRow } from './types.js';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The party's pending key in one strand, as this machine can read it
|
|
26
|
+
* ({@link PartyStrandPeerSource.pendingKey}). `pending: true` means a `PendingStrandPartyKey`
|
|
27
|
+
* row exists for the strand; its `key` is the row opened under this machine's identity key, or
|
|
28
|
+
* null when the row is sealed to other machines (a machine admitted after the removal) or this
|
|
29
|
+
* node runs on an ephemeral identity and can open nothing.
|
|
30
|
+
*/
|
|
31
|
+
export type PendingPartyKeyState =
|
|
32
|
+
| { pending: false }
|
|
33
|
+
| { pending: true; key: OpenedPendingPartyKey | null };
|
|
34
|
+
|
|
35
|
+
/** One of the party's other machines in one strand, as its `StrandPeer` row states it. */
|
|
36
|
+
export interface PartyStrandPeer {
|
|
37
|
+
/** The machine's control peer id (`CadrePeer.PeerId`, what `listMachines` and `removeMachine` use). */
|
|
38
|
+
readonly peerId: string;
|
|
39
|
+
/** Its peer id in this strand's network. */
|
|
40
|
+
readonly strandPeerId: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The party's machines' peer ids in one strand, as {@link partitionPartyStrandPeers} sorts them. */
|
|
44
|
+
export interface PartyStrandPeers {
|
|
45
|
+
/**
|
|
46
|
+
* The party's live machines other than this one: each has a `StrandPeer` row and a live
|
|
47
|
+
* `CadrePeer` row that passes the membership predicate (`CadreMachine.trusted`, which is false
|
|
48
|
+
* for this node itself). One entry per strand peer id.
|
|
49
|
+
*/
|
|
50
|
+
live: readonly PartyStrandPeer[];
|
|
51
|
+
/**
|
|
52
|
+
* Strand peer ids of machines removed from the cadre: each has a `StrandPeer` row, a
|
|
53
|
+
* `CadrePeer` tombstone naming its peer id, and no live `CadrePeer` row. Never an id that is
|
|
54
|
+
* also in {@link live}: a removed machine can state any id as its own (nothing but the
|
|
55
|
+
* machine itself can derive its id, so nothing checks the statement), and a removed machine's
|
|
56
|
+
* row must never clear a live machine's binding.
|
|
57
|
+
*/
|
|
58
|
+
removed: readonly string[];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The control-database seam of one strand's party machines, scoped to one strand as
|
|
63
|
+
* `PendingMembershipInviteSource` is. `CadreNode` builds one per strand launch
|
|
64
|
+
* (`StartStrandConfig.partyPeers`); the instance manager publishes through it at bring-up and
|
|
65
|
+
* hands it to the reconciler and to the offline-mode machine tracker, each of which reads
|
|
66
|
+
* through it.
|
|
67
|
+
*/
|
|
68
|
+
export interface PartyStrandPeerSource {
|
|
69
|
+
/**
|
|
70
|
+
* Record this machine's own peer id in the strand in the party's control database (its
|
|
71
|
+
* `StrandPeer` row), signed by the machine's identity key. Insert-if-absent, and a no-op once
|
|
72
|
+
* the row is known present. A write deferred while it could fork the party, or refused
|
|
73
|
+
* because this machine's own `CadrePeer` row has not reached this node, is retried by the
|
|
74
|
+
* control-sibling growth drain and by the next call. Never rejects.
|
|
75
|
+
*/
|
|
76
|
+
publishOwn(strandPeerId: string): Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* The party's machines' peer ids in this strand, read now. Rejects when the rows cannot be
|
|
79
|
+
* read (on a party whose store was migrated to the `StrandPeer` table, a read can fail while
|
|
80
|
+
* the other machines are offline: `ControlDatabase.queryStrandPeers`), and never answers
|
|
81
|
+
* "no machines" for a failed read.
|
|
82
|
+
*/
|
|
83
|
+
read(): Promise<PartyStrandPeers>;
|
|
84
|
+
/**
|
|
85
|
+
* Whether the party is moving to another member key in this strand (a
|
|
86
|
+
* `PendingStrandPartyKey` row), and that key opened for this machine when it can be
|
|
87
|
+
* ({@link PendingPartyKeyState}). Rejects when the row cannot be read or does not decode,
|
|
88
|
+
* which a caller treats as `{ pending: true, key: null }`: while a rotation may be pending, a
|
|
89
|
+
* removed machine's binding under the current key is the orphan the rotation is about to
|
|
90
|
+
* leave, and clearing it would hand the removed machine a clean slate; and nothing may be
|
|
91
|
+
* seated under the current key, which the removed machine holds.
|
|
92
|
+
*/
|
|
93
|
+
pendingKey(): Promise<PendingPartyKeyState>;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Sort a strand's `StrandPeer` rows into the party's live siblings and its removed machines
|
|
98
|
+
* ({@link PartyStrandPeers}). A row whose machine has a live `CadrePeer` row is a live sibling
|
|
99
|
+
* when that row passes the membership predicate and is ignored otherwise (this node's own row,
|
|
100
|
+
* or a machine this node's anchor does not trust). A row whose machine has no live `CadrePeer`
|
|
101
|
+
* row is a removed machine only when a `CadrePeer` tombstone names it; otherwise the machine's
|
|
102
|
+
* row has simply not replicated here yet, and the row is ignored.
|
|
103
|
+
*/
|
|
104
|
+
export function partitionPartyStrandPeers(
|
|
105
|
+
rows: readonly StrandPeerRow[],
|
|
106
|
+
machines: readonly CadreMachine[],
|
|
107
|
+
tombstones: readonly RevocationRow[],
|
|
108
|
+
): PartyStrandPeers {
|
|
109
|
+
const liveMachines = new Map(machines.map((machine) => [machine.peerId, machine]));
|
|
110
|
+
const removedMachines = new Set(
|
|
111
|
+
tombstones.filter((tombstone) => tombstone.tableName === 'CadrePeer').map((tombstone) => tombstone.rowKey)
|
|
112
|
+
);
|
|
113
|
+
const live = new Map<string, PartyStrandPeer>();
|
|
114
|
+
const removed = new Set<string>();
|
|
115
|
+
for (const row of rows) {
|
|
116
|
+
const machine = liveMachines.get(row.peerId);
|
|
117
|
+
if (machine !== undefined) {
|
|
118
|
+
if (machine.trusted && !live.has(row.strandPeerId)) {
|
|
119
|
+
live.set(row.strandPeerId, { peerId: row.peerId, strandPeerId: row.strandPeerId });
|
|
120
|
+
}
|
|
121
|
+
} else if (removedMachines.has(row.peerId)) {
|
|
122
|
+
removed.add(row.strandPeerId);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
for (const id of live.keys()) {
|
|
126
|
+
removed.delete(id);
|
|
127
|
+
}
|
|
128
|
+
return { live: [...live.values()], removed: [...removed] };
|
|
129
|
+
}
|
|
@@ -13,7 +13,10 @@
|
|
|
13
13
|
* cut off part-way through the handshake, from both sides, for as long as the link stays that
|
|
14
14
|
* slow. And after a restart nothing else dials a strand peer: a cross-party strand's launch
|
|
15
15
|
* seed is empty, and cadre-core's address passes only fill the address book. Two phones that
|
|
16
|
-
* restarted then never reconnect, and a write either made alone never reaches the other.
|
|
16
|
+
* restarted then never reconnect, and a write either made alone never reaches the other. The
|
|
17
|
+
* same holds for a sibling of the party this node has never connected to (its strand port
|
|
18
|
+
* blocked when the strand launched here): its address reaches the address book through the
|
|
19
|
+
* strand-addr RPC and nothing dials it, so a port that opens later is never reached.
|
|
17
20
|
*
|
|
18
21
|
* What it does. One loop per strand runtime, started in `StrandInstanceManager`'s runtime
|
|
19
22
|
* build and stopped with the runtime (stop, quiesce, failed build), so a hibernating strand
|
|
@@ -30,11 +33,14 @@
|
|
|
30
33
|
*
|
|
31
34
|
* Who is known: every peer the node's FRET table holds a verified signed address record for
|
|
32
35
|
* (`strand-fret-addrs.ts`), plus the launch seed (sibling strand addresses and a formation's
|
|
33
|
-
* carried addresses)
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* peer,
|
|
36
|
+
* carried addresses), plus the party's own machines known to hold the strand
|
|
37
|
+
* ({@link StrandRedialDeps.partyMachines}, from the control database) at whatever addresses
|
|
38
|
+
* the node's address book holds for them, a machine with none skipped. Who counts as
|
|
39
|
+
* connected: a peer FRET labels `'member'` (it has seen it speak this strand's protocols), a
|
|
40
|
+
* seed peer, or a party machine. None ever includes a configured relay, a peer FRET labels
|
|
41
|
+
* `'foreign'`, or a peer the revocation gate refuses. The relay matters most: the strand node
|
|
42
|
+
* is connected to it throughout and FRET files it with a record like any peer, so counting it
|
|
43
|
+
* would leave this loop idle exactly when it is needed.
|
|
38
44
|
*
|
|
39
45
|
* NOTE: this compensates for FRET's 2 000 ms maintenance dial limit, which cannot open a
|
|
40
46
|
* relayed connection on a slow link (upstream half:
|
|
@@ -44,6 +50,8 @@
|
|
|
44
50
|
*/
|
|
45
51
|
|
|
46
52
|
import debug from 'debug';
|
|
53
|
+
import type { PeerId } from '@libp2p/interface';
|
|
54
|
+
import { peerIdFromString } from '@libp2p/peer-id';
|
|
47
55
|
import type { Multiaddr } from '@multiformats/multiaddr';
|
|
48
56
|
import { relayedDialBudgetMs } from './link-budget.js';
|
|
49
57
|
import { CONTROL_COHORT_DIAL_ADDRESS_ATTEMPTS, SelfRelayOnlyError, dialPeerAddrs, type AddrDialer, type PeerDialBudget } from './peer-dial.js';
|
|
@@ -97,6 +105,11 @@ export function strandRedialTimings(linkRoundTripMs?: number): StrandRedialTimin
|
|
|
97
105
|
/** The slice of a strand's libp2p node the loop uses. Structurally satisfied by db-p2p's node. */
|
|
98
106
|
export interface RedialNode extends AddrDialer {
|
|
99
107
|
services?: FretAddrHost['services'];
|
|
108
|
+
/**
|
|
109
|
+
* The node's address book, read for the party's machines ({@link StrandRedialDeps.partyMachines});
|
|
110
|
+
* libp2p's `get` throws a `NotFoundError` for a peer it holds nothing for.
|
|
111
|
+
*/
|
|
112
|
+
peerStore: { get(peerId: PeerId): Promise<{ addresses: ReadonlyArray<{ multiaddr: Multiaddr }> }> };
|
|
100
113
|
getConnections(): ReadonlyArray<{ remotePeer: { toString(): string } }>;
|
|
101
114
|
addEventListener(type: 'peer:disconnect', listener: () => void): void;
|
|
102
115
|
removeEventListener(type: 'peer:disconnect', listener: () => void): void;
|
|
@@ -116,8 +129,16 @@ export interface StrandRedialDeps {
|
|
|
116
129
|
seed: readonly string[];
|
|
117
130
|
/** Peer ids never dialed nor counted as members: the relays the node reserves on. */
|
|
118
131
|
excluded: ReadonlySet<string>;
|
|
119
|
-
/**
|
|
132
|
+
/** Whether a peer, or this node itself, is cut off: the revocation gate's verdict and the party's removed machines. Absent when neither applies. */
|
|
120
133
|
isRevoked?: (peerId: string) => boolean;
|
|
134
|
+
/**
|
|
135
|
+
* The strand peer ids of the party's other machines known to hold the strand
|
|
136
|
+
* (`strand-party-machines.ts`), read at every look. Each is a member whether or not FRET has
|
|
137
|
+
* met it, dialed at the addresses the node's address book holds for it (cadre-core's
|
|
138
|
+
* strand-addr passes fill it) and skipped while it holds none. Absent on a node that tracks
|
|
139
|
+
* no party machines.
|
|
140
|
+
*/
|
|
141
|
+
partyMachines?: () => readonly string[];
|
|
121
142
|
/** Omit for real (unref'd) timers. */
|
|
122
143
|
scheduler?: TimeoutScheduler;
|
|
123
144
|
}
|
|
@@ -207,10 +228,11 @@ class StrandPeerRedialLoop implements StrandPeerRedial {
|
|
|
207
228
|
return 'watch';
|
|
208
229
|
}
|
|
209
230
|
const membership = strandFretMembership(this.deps.node);
|
|
210
|
-
|
|
231
|
+
const partyMachines = new Set(this.deps.partyMachines?.() ?? []);
|
|
232
|
+
if (this.memberConnected(membership, partyMachines)) {
|
|
211
233
|
return 'watch';
|
|
212
234
|
}
|
|
213
|
-
const known = await this.knownMembers(membership);
|
|
235
|
+
const known = await this.knownMembers(membership, partyMachines);
|
|
214
236
|
if (known.size === 0 || this.stopped) {
|
|
215
237
|
return 'watch';
|
|
216
238
|
}
|
|
@@ -255,16 +277,19 @@ class StrandPeerRedialLoop implements StrandPeerRedial {
|
|
|
255
277
|
return peerId !== this.selfId && !this.deps.excluded.has(peerId) && this.deps.isRevoked?.(peerId) !== true;
|
|
256
278
|
}
|
|
257
279
|
|
|
258
|
-
/** Whether an open connection reaches a peer FRET has seen speak this strand's protocols, or a
|
|
259
|
-
private memberConnected(membership: ReadonlyMap<string, FretMembership>): boolean {
|
|
280
|
+
/** Whether an open connection reaches a peer FRET has seen speak this strand's protocols, a seed peer, or a party machine. */
|
|
281
|
+
private memberConnected(membership: ReadonlyMap<string, FretMembership>, partyMachines: ReadonlySet<string>): boolean {
|
|
260
282
|
return this.deps.node.getConnections().some((connection) => {
|
|
261
283
|
const peerId = connection.remotePeer.toString();
|
|
262
|
-
return this.eligible(peerId) && (membership.get(peerId) === 'member' || this.seed.has(peerId));
|
|
284
|
+
return this.eligible(peerId) && (membership.get(peerId) === 'member' || this.seed.has(peerId) || partyMachines.has(peerId));
|
|
263
285
|
});
|
|
264
286
|
}
|
|
265
287
|
|
|
266
|
-
/**
|
|
267
|
-
|
|
288
|
+
/**
|
|
289
|
+
* The members to dial, each with its candidate addresses: verified FRET records first, then the
|
|
290
|
+
* seed's, then the address book's for a party machine neither names.
|
|
291
|
+
*/
|
|
292
|
+
private async knownMembers(membership: ReadonlyMap<string, FretMembership>, partyMachines: ReadonlySet<string>): Promise<Map<string, Multiaddr[]>> {
|
|
268
293
|
const { peers } = await strandFretPeerAddrs(this.deps.node);
|
|
269
294
|
const known = new Map<string, Multiaddr[]>();
|
|
270
295
|
for (const [peerId, addrs] of [...peers, ...this.seed]) {
|
|
@@ -273,9 +298,31 @@ class StrandPeerRedialLoop implements StrandPeerRedial {
|
|
|
273
298
|
}
|
|
274
299
|
known.set(peerId, unionAddrs(known.get(peerId) ?? [], addrs));
|
|
275
300
|
}
|
|
301
|
+
for (const peerId of partyMachines) {
|
|
302
|
+
if (known.has(peerId) || !this.eligible(peerId) || membership.get(peerId) === 'foreign') {
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
const addrs = await this.addressBookAddrs(peerId);
|
|
306
|
+
if (addrs.length > 0) {
|
|
307
|
+
known.set(peerId, addrs);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
276
310
|
return known;
|
|
277
311
|
}
|
|
278
312
|
|
|
313
|
+
/** The address book's addresses for `peerId`: none for a peer it holds nothing for, and none, logged, when it cannot be read. */
|
|
314
|
+
private async addressBookAddrs(peerId: string): Promise<Multiaddr[]> {
|
|
315
|
+
try {
|
|
316
|
+
const peer = await this.deps.node.peerStore.get(peerIdFromString(peerId));
|
|
317
|
+
return peer.addresses.map((address) => address.multiaddr);
|
|
318
|
+
} catch (error) {
|
|
319
|
+
if ((error as { name?: string }).name !== 'NotFoundError') {
|
|
320
|
+
log('strand %s: the address book could not be read for party machine %s: %o', this.deps.label, peerId, error);
|
|
321
|
+
}
|
|
322
|
+
return [];
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
279
326
|
/** Dial every member in `known` at once; resolves to whether any connected. */
|
|
280
327
|
private async dialAny(known: ReadonlyMap<string, Multiaddr[]>): Promise<boolean> {
|
|
281
328
|
const members = [...known];
|
|
@@ -260,6 +260,17 @@ export interface StrandRevocationEnforcerDeps {
|
|
|
260
260
|
* logged and swallowed — an embedder's handler must not derail the sweep.
|
|
261
261
|
*/
|
|
262
262
|
onSelfRevoked?: () => void;
|
|
263
|
+
/**
|
|
264
|
+
* Called ONCE each time this node's own peer id LEAVES the deny set after
|
|
265
|
+
* {@link onSelfRevoked} reported it there — a manager re-admitted the party, or a
|
|
266
|
+
* sibling machine bound this node under the party's current key after a key rotation
|
|
267
|
+
* left its only binding orphaned (`strand-membership-reconciler.ts` → "Binding the
|
|
268
|
+
* party's other machines"). Not fired by a first refresh that finds this node clear.
|
|
269
|
+
* `StrandInstanceManager` wires it to the strand's membership reconciler, which stopped
|
|
270
|
+
* on the revocation and restarts here. A throw is logged and swallowed, as for
|
|
271
|
+
* {@link onSelfRevoked}.
|
|
272
|
+
*/
|
|
273
|
+
onSelfRestored?: () => void;
|
|
263
274
|
/**
|
|
264
275
|
* Subscribe `onChange` to changes in `Strand.Member` and `Strand.MemberPeer`
|
|
265
276
|
* (`StrandDatabase.watchTables`). Absent, or throwing: refreshes come from
|
|
@@ -574,22 +585,29 @@ export class StrandRevocationEnforcer {
|
|
|
574
585
|
* into the revoked state: fired once on entry, and re-armed the moment this
|
|
575
586
|
* node is out of the set again — a manager can re-admit a party it removed
|
|
576
587
|
* (a fresh `Member` row makes the orphaned bindings live again), and a second
|
|
577
|
-
* removal has to be reported like the first.
|
|
588
|
+
* removal has to be reported like the first. Leaving the set after a report is
|
|
589
|
+
* the other edge, {@link StrandRevocationEnforcerDeps.onSelfRestored}.
|
|
578
590
|
*/
|
|
579
591
|
private trackSelfRevoked(selfRevoked: boolean): void {
|
|
580
|
-
if (
|
|
581
|
-
this.selfRevokedSignaled = false;
|
|
592
|
+
if (selfRevoked === this.selfRevokedSignaled) {
|
|
582
593
|
return;
|
|
583
594
|
}
|
|
584
|
-
|
|
585
|
-
|
|
595
|
+
this.selfRevokedSignaled = selfRevoked;
|
|
596
|
+
if (selfRevoked) {
|
|
597
|
+
log('[%s] THIS node is revoked from the strand (removed or left) — signalling', this.deps.label);
|
|
598
|
+
this.signalSelf(this.deps.onSelfRevoked, 'onSelfRevoked');
|
|
599
|
+
} else {
|
|
600
|
+
log('[%s] THIS node is no longer revoked from the strand — signalling', this.deps.label);
|
|
601
|
+
this.signalSelf(this.deps.onSelfRestored, 'onSelfRestored');
|
|
586
602
|
}
|
|
587
|
-
|
|
588
|
-
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/** Fire one of the self-state handlers; a throw is logged and must not derail the sweep. */
|
|
606
|
+
private signalSelf(handler: (() => void) | undefined, name: string): void {
|
|
589
607
|
try {
|
|
590
|
-
|
|
608
|
+
handler?.();
|
|
591
609
|
} catch (error) {
|
|
592
|
-
log('[%s]
|
|
610
|
+
log('[%s] %s handler threw: %o', this.deps.label, name, error);
|
|
593
611
|
}
|
|
594
612
|
}
|
|
595
613
|
}
|
|
@@ -10,6 +10,9 @@ import { ed25519KeyPairFromLibp2p } from './ed25519-key.js';
|
|
|
10
10
|
* peerId churn — a strand node's transport peerId must stay stable across
|
|
11
11
|
* process restarts, or any peer-store entry or `MemberPeer` row that names it
|
|
12
12
|
* goes stale on every boot.
|
|
13
|
+
*
|
|
14
|
+
* NOTE: every `CadreControl.StrandPeer` row names a peer id derived under this tag, and those
|
|
15
|
+
* rows cannot be updated or deleted. If this is ever bumped to `.v2`, every row is stale at once.
|
|
13
16
|
*/
|
|
14
17
|
const STRAND_TRANSPORT_KEY_DOMAIN = 'sereus.strand-transport-key.v1';
|
|
15
18
|
|