@ipab/cli 0.1.0-rc.12 → 0.1.0-rc.13
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/dist/cli-daemon.d.ts +14 -0
- package/dist/cli-daemon.d.ts.map +1 -0
- package/dist/cli-daemon.js +400 -0
- package/dist/cli-daemon.js.map +1 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +1260 -1404
- package/dist/cli.js.map +1 -1
- package/dist/daemon-api.d.ts +80 -0
- package/dist/daemon-api.d.ts.map +1 -0
- package/dist/daemon-api.js +2411 -0
- package/dist/daemon-api.js.map +1 -0
- package/dist/daemon-client.d.ts +322 -0
- package/dist/daemon-client.d.ts.map +1 -0
- package/dist/daemon-client.js +400 -0
- package/dist/daemon-client.js.map +1 -0
- package/dist/daemon-protocol.d.ts +22 -0
- package/dist/daemon-protocol.d.ts.map +1 -0
- package/dist/daemon-protocol.js +39 -0
- package/dist/daemon-protocol.js.map +1 -0
- package/dist/daemon-telemetry.d.ts +86 -0
- package/dist/daemon-telemetry.d.ts.map +1 -0
- package/dist/daemon-telemetry.js +502 -0
- package/dist/daemon-telemetry.js.map +1 -0
- package/dist/daemon-ui.d.ts +68 -0
- package/dist/daemon-ui.d.ts.map +1 -1
- package/dist/daemon-ui.js +283 -4
- package/dist/daemon-ui.js.map +1 -1
- package/dist/daemon.d.ts +10 -0
- package/dist/daemon.d.ts.map +1 -1
- package/dist/daemon.js +289 -38
- package/dist/daemon.js.map +1 -1
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +7 -2
- package/dist/identity.js.map +1 -1
- package/dist/ipc.d.ts +19 -7
- package/dist/ipc.d.ts.map +1 -1
- package/dist/ipc.js +96 -70
- package/dist/ipc.js.map +1 -1
- package/dist/pins.d.ts.map +1 -1
- package/dist/pins.js +18 -3
- package/dist/pins.js.map +1 -1
- package/dist/state-storage.d.ts.map +1 -1
- package/dist/state-storage.js +2 -1
- package/dist/state-storage.js.map +1 -1
- package/dist/state.d.ts +1 -1
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +3 -2
- package/dist/state.js.map +1 -1
- package/dist/swarm-files.d.ts.map +1 -1
- package/dist/swarm-files.js +7 -2
- package/dist/swarm-files.js.map +1 -1
- package/dist/swarm.d.ts +10 -1
- package/dist/swarm.d.ts.map +1 -1
- package/dist/swarm.js +230 -56
- package/dist/swarm.js.map +1 -1
- package/package.json +4 -4
- package/src/cli-daemon.ts +482 -0
- package/src/cli.ts +1689 -1948
- package/src/daemon-api.ts +3148 -0
- package/src/daemon-client.ts +975 -0
- package/src/daemon-protocol.ts +61 -0
- package/src/daemon-telemetry.ts +656 -0
- package/src/daemon-ui.ts +361 -4
- package/src/daemon.ts +337 -43
- package/src/identity.ts +10 -2
- package/src/ipc.ts +113 -79
- package/src/pins.ts +23 -3
- package/src/state-storage.ts +5 -1
- package/src/state.ts +3 -1
- package/src/swarm-files.ts +10 -2
- package/src/swarm.ts +698 -464
package/src/swarm.ts
CHANGED
|
@@ -190,6 +190,8 @@ import {
|
|
|
190
190
|
queryRemotePivotSearch,
|
|
191
191
|
rankPivotCandidates,
|
|
192
192
|
} from "./search-service.js";
|
|
193
|
+
import { DaemonTelemetry, telemetryLogFilename } from "./daemon-telemetry.js";
|
|
194
|
+
import type { DaemonApiHandler } from "./daemon.js";
|
|
193
195
|
import {
|
|
194
196
|
DaemonLogBuffer,
|
|
195
197
|
emptySyncState,
|
|
@@ -316,12 +318,22 @@ interface LegacyMembershipRequest {
|
|
|
316
318
|
peerSignature: string;
|
|
317
319
|
signature: string;
|
|
318
320
|
}
|
|
321
|
+
export interface InProcessKeyedMutex {
|
|
322
|
+
run<T>(key: string, action: () => Promise<T>): Promise<T>;
|
|
323
|
+
}
|
|
324
|
+
|
|
319
325
|
export interface SwarmContext {
|
|
320
326
|
ensureDaemon?: () => Promise<void>;
|
|
321
327
|
daemonReady?: (
|
|
322
328
|
status?: () => DaemonStatusSnapshot,
|
|
323
329
|
onListen?: (info: { port: number; url: string }) => void,
|
|
330
|
+
options?: {
|
|
331
|
+
apiHandler?: DaemonApiHandler;
|
|
332
|
+
onShutdown?: () => void;
|
|
333
|
+
hasActiveWork?: () => boolean;
|
|
334
|
+
},
|
|
324
335
|
) => Promise<() => Promise<void>>;
|
|
336
|
+
inProcessMutex?: InProcessKeyedMutex;
|
|
325
337
|
confirmPublication?: (url: string, root: string) => Promise<void>;
|
|
326
338
|
requestPivotReceipt?: (
|
|
327
339
|
pivot: { member: Member; profile: Profile },
|
|
@@ -461,6 +473,20 @@ export interface Artifact {
|
|
|
461
473
|
previous?: string;
|
|
462
474
|
timestamp: string;
|
|
463
475
|
}
|
|
476
|
+
function withSwarmLock<T>(
|
|
477
|
+
context: SwarmContext,
|
|
478
|
+
lockDir: string,
|
|
479
|
+
action: () => Promise<T>,
|
|
480
|
+
signal?: AbortSignal,
|
|
481
|
+
): Promise<T> {
|
|
482
|
+
if (context.inProcessMutex) {
|
|
483
|
+
return context.inProcessMutex.run(lockDir, async () => {
|
|
484
|
+
signal?.throwIfAborted();
|
|
485
|
+
return action();
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
return withPrivateLock(lockDir, action, signal);
|
|
489
|
+
}
|
|
464
490
|
function withState<T>(
|
|
465
491
|
context: SwarmContext,
|
|
466
492
|
operation: (state: State) => T,
|
|
@@ -1080,7 +1106,8 @@ async function inspection(context: SwarmContext, url: string, refresh = false) {
|
|
|
1080
1106
|
]);
|
|
1081
1107
|
let snapshot: InspectionSnapshot;
|
|
1082
1108
|
if (saved === undefined) {
|
|
1083
|
-
snapshot = await
|
|
1109
|
+
snapshot = await withSwarmLock(
|
|
1110
|
+
context,
|
|
1084
1111
|
await privateDirectory(context.stateDir, "swarm-inspections"),
|
|
1085
1112
|
async () => {
|
|
1086
1113
|
const { config, variable } = await fetchConfig(context, url);
|
|
@@ -1572,7 +1599,7 @@ async function ensureAccountProfile(
|
|
|
1572
1599
|
) {
|
|
1573
1600
|
const identity = await contextIdentity(context);
|
|
1574
1601
|
const dir = await accountProfileDirectory(context, identity.publicKey);
|
|
1575
|
-
return
|
|
1602
|
+
return withSwarmLock(context, dir, async () => {
|
|
1576
1603
|
const existing = await storedAccountProfile(context);
|
|
1577
1604
|
if (
|
|
1578
1605
|
existing &&
|
|
@@ -2165,6 +2192,42 @@ export async function swarmDaemon(
|
|
|
2165
2192
|
}
|
|
2166
2193
|
return secrets;
|
|
2167
2194
|
};
|
|
2195
|
+
const telemetry = new DaemonTelemetry({
|
|
2196
|
+
env: context.env,
|
|
2197
|
+
logPath: join(context.stateDir, "swarm-daemon", telemetryLogFilename),
|
|
2198
|
+
secrets: () => collectSecrets(),
|
|
2199
|
+
});
|
|
2200
|
+
const [
|
|
2201
|
+
{ builtinContent },
|
|
2202
|
+
{ DaemonService, KeyedMutex, createDaemonApiHandler },
|
|
2203
|
+
] = await Promise.all([
|
|
2204
|
+
import("./builtins.js"),
|
|
2205
|
+
import("./daemon-api.js"),
|
|
2206
|
+
]);
|
|
2207
|
+
const ownsDaemonState = !context.state;
|
|
2208
|
+
const builtins = ownsDaemonState ? await builtinContent() : undefined;
|
|
2209
|
+
const daemonState =
|
|
2210
|
+
context.state ??
|
|
2211
|
+
new State(context.stateDir, builtins?.pins ?? [], context.env, (db) =>
|
|
2212
|
+
telemetry.wrapDatabase(db, "state"),
|
|
2213
|
+
);
|
|
2214
|
+
const daemonStore = new IpfsStore({
|
|
2215
|
+
...context.store.configuration,
|
|
2216
|
+
singleProcessCoordination: true,
|
|
2217
|
+
onCoordinationEvent: (event) => {
|
|
2218
|
+
telemetry.recordCoordination(event);
|
|
2219
|
+
},
|
|
2220
|
+
onSqlEvent: (event) => {
|
|
2221
|
+
telemetry.recordSql(event);
|
|
2222
|
+
},
|
|
2223
|
+
});
|
|
2224
|
+
const inProcessMutex = context.inProcessMutex ?? new KeyedMutex();
|
|
2225
|
+
const daemonContext: SwarmContext = {
|
|
2226
|
+
...context,
|
|
2227
|
+
state: daemonState,
|
|
2228
|
+
store: daemonStore,
|
|
2229
|
+
inProcessMutex,
|
|
2230
|
+
};
|
|
2168
2231
|
const redact = (
|
|
2169
2232
|
error: string | null,
|
|
2170
2233
|
session?: NonNullable<ReturnType<typeof sessions.get>>,
|
|
@@ -2176,6 +2239,38 @@ export async function swarmDaemon(
|
|
|
2176
2239
|
logBuffer.push(text, collectSecrets());
|
|
2177
2240
|
context.diagnostic?.(text);
|
|
2178
2241
|
};
|
|
2242
|
+
const dirtyDiscovery = new Set<string>();
|
|
2243
|
+
const wakeState = { dirtyAllDiscovery: false, wakeRequested: false };
|
|
2244
|
+
let wakeSleep: (() => void) | undefined;
|
|
2245
|
+
const wakeDaemonLoop = (url?: string) => {
|
|
2246
|
+
if (url) dirtyDiscovery.add(url);
|
|
2247
|
+
else wakeState.dirtyAllDiscovery = true;
|
|
2248
|
+
wakeState.wakeRequested = true;
|
|
2249
|
+
const resolveWake = wakeSleep;
|
|
2250
|
+
wakeSleep = undefined;
|
|
2251
|
+
resolveWake?.();
|
|
2252
|
+
};
|
|
2253
|
+
const daemonService = await DaemonService.create({
|
|
2254
|
+
stateDir: context.stateDir,
|
|
2255
|
+
env: context.env,
|
|
2256
|
+
state: daemonState,
|
|
2257
|
+
store: daemonStore,
|
|
2258
|
+
telemetry,
|
|
2259
|
+
inProcessMutex,
|
|
2260
|
+
diagnostic: emitDiagnostic,
|
|
2261
|
+
ensureDaemon: () => {
|
|
2262
|
+
wakeDaemonLoop();
|
|
2263
|
+
return Promise.resolve();
|
|
2264
|
+
},
|
|
2265
|
+
onSwarmChange: wakeDaemonLoop,
|
|
2266
|
+
...(context.fetch ? { fetch: context.fetch } : {}),
|
|
2267
|
+
});
|
|
2268
|
+
const shutdownController = new AbortController();
|
|
2269
|
+
const shutdownRequested = () => shutdownController.signal.aborted;
|
|
2270
|
+
const effectiveSignal = AbortSignal.any([
|
|
2271
|
+
signal,
|
|
2272
|
+
shutdownController.signal,
|
|
2273
|
+
]);
|
|
2179
2274
|
const buildStatusSnapshot = (): DaemonStatusSnapshot => {
|
|
2180
2275
|
const now = Date.now();
|
|
2181
2276
|
const urls = [...new Set([...knownAliases.keys(), ...sessions.keys()])];
|
|
@@ -2281,6 +2376,7 @@ export async function swarmDaemon(
|
|
|
2281
2376
|
syncInterval: context.syncInterval ?? 60,
|
|
2282
2377
|
swarms,
|
|
2283
2378
|
logs: logBuffer.entries(),
|
|
2379
|
+
observability: telemetry.snapshot(collectSecrets()),
|
|
2284
2380
|
};
|
|
2285
2381
|
};
|
|
2286
2382
|
const closeReadiness = await context.daemonReady?.(
|
|
@@ -2288,6 +2384,20 @@ export async function swarmDaemon(
|
|
|
2288
2384
|
({ url }) => {
|
|
2289
2385
|
uiUrl = url;
|
|
2290
2386
|
},
|
|
2387
|
+
{
|
|
2388
|
+
apiHandler: createDaemonApiHandler(daemonService),
|
|
2389
|
+
onShutdown: () => {
|
|
2390
|
+
shutdownController.abort(
|
|
2391
|
+
new HostError(
|
|
2392
|
+
"Daemon shutdown requested.",
|
|
2393
|
+
0,
|
|
2394
|
+
"SHUTDOWN",
|
|
2395
|
+
"daemon",
|
|
2396
|
+
),
|
|
2397
|
+
);
|
|
2398
|
+
},
|
|
2399
|
+
hasActiveWork: () => daemonService.hasActiveRuns(),
|
|
2400
|
+
},
|
|
2291
2401
|
);
|
|
2292
2402
|
emitDiagnostic(
|
|
2293
2403
|
`IPAB daemon running in foreground; Ctrl-C stops management and leaves Kubo running.${uiUrl ? ` Status UI: ${uiUrl}` : ""}`,
|
|
@@ -2309,7 +2419,9 @@ export async function swarmDaemon(
|
|
|
2309
2419
|
try {
|
|
2310
2420
|
await session.context.finishRead?.();
|
|
2311
2421
|
} finally {
|
|
2312
|
-
|
|
2422
|
+
if (session.context.store !== daemonStore) {
|
|
2423
|
+
await session.context.store.close();
|
|
2424
|
+
}
|
|
2313
2425
|
}
|
|
2314
2426
|
}
|
|
2315
2427
|
};
|
|
@@ -2379,8 +2491,21 @@ export async function swarmDaemon(
|
|
|
2379
2491
|
await writeNetworkingSnapshot(context.stateDir, url, snapshot);
|
|
2380
2492
|
};
|
|
2381
2493
|
try {
|
|
2382
|
-
while (!
|
|
2383
|
-
|
|
2494
|
+
while (!effectiveSignal.aborted) {
|
|
2495
|
+
if (wakeState.dirtyAllDiscovery) {
|
|
2496
|
+
for (const session of sessions.values()) session.nextDiscovery = 0;
|
|
2497
|
+
wakeState.dirtyAllDiscovery = false;
|
|
2498
|
+
dirtyDiscovery.clear();
|
|
2499
|
+
} else if (dirtyDiscovery.size > 0) {
|
|
2500
|
+
for (const dirtyUrl of dirtyDiscovery) {
|
|
2501
|
+
const session = sessions.get(dirtyUrl);
|
|
2502
|
+
if (session) session.nextDiscovery = 0;
|
|
2503
|
+
}
|
|
2504
|
+
dirtyDiscovery.clear();
|
|
2505
|
+
}
|
|
2506
|
+
const registrations = withState(daemonContext, (state) =>
|
|
2507
|
+
state.swarms(),
|
|
2508
|
+
);
|
|
2384
2509
|
knownAliases = new Map(
|
|
2385
2510
|
registrations.map((entry) => [entry.cid, entry.alias]),
|
|
2386
2511
|
);
|
|
@@ -2392,345 +2517,415 @@ export async function swarmDaemon(
|
|
|
2392
2517
|
await closeSession(session);
|
|
2393
2518
|
sessions.delete(url);
|
|
2394
2519
|
}
|
|
2395
|
-
await boundedMap(registrations, async ({ cid: url }) =>
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
if (session.record?.config && context.pivot === undefined) {
|
|
2421
|
-
const self = session.record.config.members.find(
|
|
2422
|
-
(member) => member.publicKey === session.record!.account,
|
|
2423
|
-
)!;
|
|
2424
|
-
const c = coordinates(url);
|
|
2425
|
-
const profile = parseProfile(
|
|
2426
|
-
(
|
|
2427
|
-
await client(session.context, c.service).read(
|
|
2428
|
-
c.scopeId,
|
|
2429
|
-
self.profileId,
|
|
2430
|
-
{ signal },
|
|
2431
|
-
)
|
|
2432
|
-
).value,
|
|
2433
|
-
self.peerId,
|
|
2434
|
-
);
|
|
2435
|
-
if ((profile.pivot ?? false) !== session.pivot)
|
|
2436
|
-
session.nextDiscovery = 0;
|
|
2437
|
-
}
|
|
2438
|
-
if (Date.now() >= session.nextDiscovery || session.healthy < 1) {
|
|
2439
|
-
const record = await approvedMembership(
|
|
2440
|
-
session.context,
|
|
2441
|
-
url,
|
|
2442
|
-
signal,
|
|
2443
|
-
);
|
|
2444
|
-
const c = coordinates(url);
|
|
2445
|
-
const scope = nodeScope(c.service, record.config!.scopeId);
|
|
2446
|
-
const existing = await readPrivate<unknown>(
|
|
2447
|
-
join(context.stateDir, "swarm-nodes", scope),
|
|
2448
|
-
"node.json",
|
|
2449
|
-
);
|
|
2450
|
-
if (!existing)
|
|
2451
|
-
throw new HostError(
|
|
2452
|
-
"No existing managed node; activate this membership before running daemon.",
|
|
2453
|
-
);
|
|
2454
|
-
await saveLocal(session.context, record);
|
|
2455
|
-
if (
|
|
2456
|
-
!session.record?.config ||
|
|
2457
|
-
networkingBinding(url, record.account, record.config!) !==
|
|
2458
|
-
networkingBinding(
|
|
2459
|
-
url,
|
|
2460
|
-
session.record.account,
|
|
2461
|
-
session.record.config,
|
|
2462
|
-
)
|
|
2463
|
-
) {
|
|
2464
|
-
session.candidates.reconcile(new Map());
|
|
2465
|
-
session.selected.clear();
|
|
2466
|
-
session.verifiedAt = null;
|
|
2467
|
-
delete session.connections;
|
|
2468
|
-
delete session.node;
|
|
2520
|
+
await boundedMap(registrations, async ({ cid: url }) =>
|
|
2521
|
+
telemetry.runWithContext(
|
|
2522
|
+
{ source: "daemon-bg", operation: "swarm-tick", swarm: url },
|
|
2523
|
+
async () => {
|
|
2524
|
+
let session = sessions.get(url);
|
|
2525
|
+
if (!session) {
|
|
2526
|
+
session = {
|
|
2527
|
+
context: {
|
|
2528
|
+
...daemonContext,
|
|
2529
|
+
diagnostic: emitDiagnostic,
|
|
2530
|
+
store: daemonStore.scope(),
|
|
2531
|
+
forceRefresh: true,
|
|
2532
|
+
},
|
|
2533
|
+
candidates: new PivotCandidates(),
|
|
2534
|
+
nextDiscovery: 0,
|
|
2535
|
+
nextSync: 0,
|
|
2536
|
+
healthy: 0,
|
|
2537
|
+
selected: new Set(),
|
|
2538
|
+
verifiedAt: null,
|
|
2539
|
+
updatedAt: null,
|
|
2540
|
+
running: false,
|
|
2541
|
+
error: null,
|
|
2542
|
+
sync: emptySyncState(),
|
|
2543
|
+
};
|
|
2544
|
+
sessions.set(url, session);
|
|
2469
2545
|
}
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
session.context
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
}
|
|
2487
|
-
if (!session.node) return;
|
|
2488
|
-
const currentRecord = session.record!;
|
|
2489
|
-
const currentSelf = currentRecord.config!.members.find(
|
|
2490
|
-
(member) => member.publicKey === currentRecord.account,
|
|
2491
|
-
)!;
|
|
2492
|
-
if (
|
|
2493
|
-
!(await nodeReady(session.node, signal, session.context.fetch))
|
|
2494
|
-
) {
|
|
2495
|
-
if (session.pivotServer) session.pivotServer.listening = false;
|
|
2496
|
-
const currentDesired = memberPeers(
|
|
2497
|
-
currentSelf.peerId,
|
|
2498
|
-
session.candidates.active(),
|
|
2499
|
-
[],
|
|
2500
|
-
);
|
|
2501
|
-
session.node = await startNode(
|
|
2502
|
-
context.stateDir,
|
|
2503
|
-
nodeScope(
|
|
2504
|
-
coordinates(url).service,
|
|
2505
|
-
currentRecord.config!.scopeId,
|
|
2506
|
-
),
|
|
2507
|
-
currentRecord.secret!.swarmKey,
|
|
2508
|
-
currentRecord.config!.epoch,
|
|
2509
|
-
context.listen,
|
|
2510
|
-
{
|
|
2511
|
-
self: currentSelf.peerId,
|
|
2512
|
-
pivot: session.pivot ?? false,
|
|
2513
|
-
peers: currentDesired,
|
|
2514
|
-
signal,
|
|
2515
|
-
},
|
|
2516
|
-
);
|
|
2517
|
-
session.selected = new Set(currentDesired.map(({ ID }) => ID));
|
|
2518
|
-
session.context.node = session.node;
|
|
2519
|
-
emitDiagnostic(
|
|
2520
|
-
`[${url}] Managed Kubo was offline; restarted before probing peers.`,
|
|
2521
|
-
);
|
|
2522
|
-
}
|
|
2523
|
-
let healthy = 0;
|
|
2524
|
-
await boundedMap(
|
|
2525
|
-
[...session.candidates.active()],
|
|
2526
|
-
async ([id, addresses]) => {
|
|
2527
|
-
let success = false;
|
|
2528
|
-
let error: string | null = null;
|
|
2529
|
-
try {
|
|
2530
|
-
success = await probePeer(
|
|
2531
|
-
session.node!,
|
|
2532
|
-
id,
|
|
2533
|
-
addresses,
|
|
2534
|
-
signal,
|
|
2546
|
+
let pivotListenerVerified = false;
|
|
2547
|
+
try {
|
|
2548
|
+
if (session.record?.config && context.pivot === undefined) {
|
|
2549
|
+
const self = session.record.config.members.find(
|
|
2550
|
+
(member) => member.publicKey === session.record!.account,
|
|
2551
|
+
)!;
|
|
2552
|
+
const c = coordinates(url);
|
|
2553
|
+
const profile = parseProfile(
|
|
2554
|
+
(
|
|
2555
|
+
await client(session.context, c.service).read(
|
|
2556
|
+
c.scopeId,
|
|
2557
|
+
self.profileId,
|
|
2558
|
+
{ signal: effectiveSignal },
|
|
2559
|
+
)
|
|
2560
|
+
).value,
|
|
2561
|
+
self.peerId,
|
|
2535
2562
|
);
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2563
|
+
if ((profile.pivot ?? false) !== session.pivot)
|
|
2564
|
+
session.nextDiscovery = 0;
|
|
2565
|
+
}
|
|
2566
|
+
if (
|
|
2567
|
+
Date.now() >= session.nextDiscovery ||
|
|
2568
|
+
session.healthy < 1
|
|
2569
|
+
) {
|
|
2570
|
+
const record = await approvedMembership(
|
|
2571
|
+
session.context,
|
|
2572
|
+
url,
|
|
2573
|
+
effectiveSignal,
|
|
2574
|
+
);
|
|
2575
|
+
const c = coordinates(url);
|
|
2576
|
+
const scope = nodeScope(c.service, record.config!.scopeId);
|
|
2577
|
+
const existing = await readPrivate<unknown>(
|
|
2578
|
+
join(context.stateDir, "swarm-nodes", scope),
|
|
2579
|
+
"node.json",
|
|
2580
|
+
);
|
|
2581
|
+
if (!existing)
|
|
2582
|
+
throw new HostError(
|
|
2583
|
+
"No existing managed node; activate this membership before running daemon.",
|
|
2584
|
+
);
|
|
2585
|
+
await saveLocal(session.context, record);
|
|
2586
|
+
if (
|
|
2587
|
+
!session.record?.config ||
|
|
2588
|
+
networkingBinding(url, record.account, record.config!) !==
|
|
2589
|
+
networkingBinding(
|
|
2590
|
+
url,
|
|
2591
|
+
session.record.account,
|
|
2592
|
+
session.record.config,
|
|
2593
|
+
)
|
|
2594
|
+
) {
|
|
2595
|
+
session.candidates.reconcile(new Map());
|
|
2596
|
+
session.selected.clear();
|
|
2597
|
+
session.verifiedAt = null;
|
|
2598
|
+
delete session.connections;
|
|
2599
|
+
delete session.node;
|
|
2600
|
+
}
|
|
2601
|
+
session.record = record;
|
|
2602
|
+
if (context.pivot === undefined)
|
|
2603
|
+
delete session.context.pivot;
|
|
2604
|
+
const result = await privateSource(
|
|
2605
|
+
session.context,
|
|
2606
|
+
record,
|
|
2607
|
+
effectiveSignal,
|
|
2608
|
+
session.candidates,
|
|
2541
2609
|
);
|
|
2610
|
+
session.node = result.node;
|
|
2611
|
+
session.pivot = result.pivot;
|
|
2612
|
+
session.context.pivot = result.pivot;
|
|
2613
|
+
session.selected = new Set(result.selectedPeers);
|
|
2614
|
+
session.error = result.observationError;
|
|
2615
|
+
if (!session.error) session.verifiedAt = Date.now();
|
|
2616
|
+
session.context.node = result.node;
|
|
2617
|
+
session.nextDiscovery = Date.now() + 120_000;
|
|
2618
|
+
await persist(url, session, true);
|
|
2542
2619
|
}
|
|
2543
|
-
session.
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2620
|
+
if (!session.node) return;
|
|
2621
|
+
const currentRecord = session.record!;
|
|
2622
|
+
const currentSelf = currentRecord.config!.members.find(
|
|
2623
|
+
(member) => member.publicKey === currentRecord.account,
|
|
2624
|
+
)!;
|
|
2625
|
+
if (
|
|
2626
|
+
!(await nodeReady(
|
|
2627
|
+
session.node,
|
|
2628
|
+
effectiveSignal,
|
|
2629
|
+
session.context.fetch,
|
|
2630
|
+
))
|
|
2631
|
+
) {
|
|
2632
|
+
if (session.pivotServer)
|
|
2633
|
+
session.pivotServer.listening = false;
|
|
2634
|
+
const currentDesired = memberPeers(
|
|
2635
|
+
currentSelf.peerId,
|
|
2636
|
+
session.candidates.active(),
|
|
2637
|
+
[],
|
|
2638
|
+
);
|
|
2639
|
+
session.node = await startNode(
|
|
2640
|
+
context.stateDir,
|
|
2641
|
+
nodeScope(
|
|
2642
|
+
coordinates(url).service,
|
|
2643
|
+
currentRecord.config!.scopeId,
|
|
2644
|
+
),
|
|
2645
|
+
currentRecord.secret!.swarmKey,
|
|
2646
|
+
currentRecord.config!.epoch,
|
|
2647
|
+
context.listen,
|
|
2648
|
+
{
|
|
2649
|
+
self: currentSelf.peerId,
|
|
2650
|
+
pivot: session.pivot ?? false,
|
|
2651
|
+
peers: currentDesired,
|
|
2652
|
+
signal: effectiveSignal,
|
|
2653
|
+
},
|
|
2654
|
+
);
|
|
2655
|
+
session.selected = new Set(
|
|
2656
|
+
currentDesired.map(({ ID }) => ID),
|
|
2657
|
+
);
|
|
2658
|
+
session.context.node = session.node;
|
|
2553
2659
|
emitDiagnostic(
|
|
2554
|
-
`[${url}]
|
|
2660
|
+
`[${url}] Managed Kubo was offline; restarted before probing peers.`,
|
|
2555
2661
|
);
|
|
2556
2662
|
}
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
await stopPivotServer(session);
|
|
2612
|
-
const pivotNode = session.node;
|
|
2613
|
-
const mounted = await mountPivotServer(
|
|
2614
|
-
pivotNode,
|
|
2663
|
+
let healthy = 0;
|
|
2664
|
+
await boundedMap(
|
|
2665
|
+
[...session.candidates.active()],
|
|
2666
|
+
async ([id, addresses]) => {
|
|
2667
|
+
let success = false;
|
|
2668
|
+
let error: string | null = null;
|
|
2669
|
+
try {
|
|
2670
|
+
success = await probePeer(
|
|
2671
|
+
session.node!,
|
|
2672
|
+
id,
|
|
2673
|
+
addresses,
|
|
2674
|
+
effectiveSignal,
|
|
2675
|
+
);
|
|
2676
|
+
} catch (cause) {
|
|
2677
|
+
effectiveSignal.throwIfAborted();
|
|
2678
|
+
error = message(cause);
|
|
2679
|
+
emitDiagnostic(
|
|
2680
|
+
`[${url}] Pivot probe ${id}: ${message(cause)}`,
|
|
2681
|
+
);
|
|
2682
|
+
}
|
|
2683
|
+
session.candidates.result(
|
|
2684
|
+
id,
|
|
2685
|
+
success,
|
|
2686
|
+
Date.now(),
|
|
2687
|
+
Math.random(),
|
|
2688
|
+
success ? null : (error ?? "Pivot dial failed."),
|
|
2689
|
+
);
|
|
2690
|
+
if (success) healthy++;
|
|
2691
|
+
else {
|
|
2692
|
+
session.nextDiscovery = 0;
|
|
2693
|
+
emitDiagnostic(
|
|
2694
|
+
`[${url}] Pivot ${id} dial failed; refreshing profile, with cooldown after repeated failure.`,
|
|
2695
|
+
);
|
|
2696
|
+
}
|
|
2697
|
+
},
|
|
2698
|
+
);
|
|
2699
|
+
session.healthy = healthy;
|
|
2700
|
+
// Reconcile active Pivot Bootstrap candidates via live Kubo RPC
|
|
2701
|
+
// without restarting the daemon unless startup-only config changed.
|
|
2702
|
+
const record = session.record!;
|
|
2703
|
+
const self = record.config!.members.find(
|
|
2704
|
+
(member) => member.publicKey === record.account,
|
|
2705
|
+
)!;
|
|
2706
|
+
const desired = memberPeers(
|
|
2707
|
+
self.peerId,
|
|
2708
|
+
session.candidates.active(),
|
|
2709
|
+
[],
|
|
2710
|
+
);
|
|
2711
|
+
session.node = await startNode(
|
|
2712
|
+
context.stateDir,
|
|
2713
|
+
nodeScope(coordinates(url).service, record.config!.scopeId),
|
|
2714
|
+
record.secret!.swarmKey,
|
|
2715
|
+
record.config!.epoch,
|
|
2716
|
+
context.listen,
|
|
2615
2717
|
{
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
requestSignal,
|
|
2621
|
-
),
|
|
2622
|
-
replicate: async (request, requestSignal) => {
|
|
2623
|
-
const requestContext = {
|
|
2624
|
-
...session.context,
|
|
2625
|
-
node: pivotNode,
|
|
2626
|
-
};
|
|
2627
|
-
try {
|
|
2628
|
-
return await handlePivotReplicateRequest(
|
|
2629
|
-
requestContext,
|
|
2630
|
-
url,
|
|
2631
|
-
request,
|
|
2632
|
-
requestSignal,
|
|
2633
|
-
);
|
|
2634
|
-
} finally {
|
|
2635
|
-
session.context.pivotSyncError =
|
|
2636
|
-
requestContext.pivotSyncError ?? null;
|
|
2637
|
-
}
|
|
2638
|
-
},
|
|
2639
|
-
receipt: (receiptRequest, requestSignal) =>
|
|
2640
|
-
handlePivotReceiptRequest(
|
|
2641
|
-
{ ...session.context, node: pivotNode },
|
|
2642
|
-
url,
|
|
2643
|
-
receiptRequest,
|
|
2644
|
-
requestSignal,
|
|
2645
|
-
),
|
|
2646
|
-
search: (params) =>
|
|
2647
|
-
withState(session.context, (state) =>
|
|
2648
|
-
queryLocalSearchIndex(state, url, params),
|
|
2649
|
-
),
|
|
2650
|
-
...(context.diagnostic
|
|
2651
|
-
? { diagnostic: context.diagnostic }
|
|
2652
|
-
: {}),
|
|
2718
|
+
self: self.peerId,
|
|
2719
|
+
pivot: session.pivot ?? false,
|
|
2720
|
+
peers: desired,
|
|
2721
|
+
signal: effectiveSignal,
|
|
2653
2722
|
},
|
|
2654
|
-
session.context.fetch,
|
|
2655
|
-
signal,
|
|
2656
2723
|
);
|
|
2657
|
-
session.
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2724
|
+
session.selected = new Set(desired.map(({ ID }) => ID));
|
|
2725
|
+
session.context.node = session.node;
|
|
2726
|
+
try {
|
|
2727
|
+
const peers = await nodeConnections(
|
|
2728
|
+
session.node,
|
|
2729
|
+
effectiveSignal,
|
|
2730
|
+
);
|
|
2731
|
+
session.connections = {
|
|
2732
|
+
observedAt: Date.now(),
|
|
2733
|
+
error: null,
|
|
2734
|
+
peers,
|
|
2735
|
+
};
|
|
2736
|
+
} catch (cause) {
|
|
2737
|
+
effectiveSignal.throwIfAborted();
|
|
2738
|
+
session.connections = {
|
|
2739
|
+
observedAt: session.connections?.observedAt ?? null,
|
|
2740
|
+
error: message(cause).slice(0, 2048),
|
|
2741
|
+
peers: session.connections?.peers ?? [],
|
|
2742
|
+
};
|
|
2743
|
+
}
|
|
2744
|
+
// Persist connectivity before potentially slow content acquisition.
|
|
2745
|
+
await persist(url, session, true);
|
|
2746
|
+
if (session.pivot) {
|
|
2747
|
+
if (
|
|
2748
|
+
!session.pivotServer ||
|
|
2749
|
+
!session.pivotServer.server.listening ||
|
|
2750
|
+
session.pivotServer.nodeApi !== session.node.api ||
|
|
2751
|
+
session.pivotServer.epoch !== record.config!.epoch ||
|
|
2752
|
+
session.pivotServer.headId !== record.config!.headId
|
|
2753
|
+
) {
|
|
2754
|
+
await stopPivotServer(session);
|
|
2755
|
+
const pivotNode = session.node;
|
|
2756
|
+
const mounted = await mountPivotServer(
|
|
2757
|
+
pivotNode,
|
|
2758
|
+
{
|
|
2759
|
+
status: (requestSignal) =>
|
|
2760
|
+
telemetry.runWithContext(
|
|
2761
|
+
{
|
|
2762
|
+
source: "pivot-rpc",
|
|
2763
|
+
operation: "GET /v1/status",
|
|
2764
|
+
swarm: url,
|
|
2765
|
+
},
|
|
2766
|
+
() =>
|
|
2767
|
+
pivotStatus(
|
|
2768
|
+
{ ...session.context, node: pivotNode },
|
|
2769
|
+
url,
|
|
2770
|
+
requestSignal,
|
|
2771
|
+
),
|
|
2772
|
+
),
|
|
2773
|
+
replicate: (request, requestSignal) =>
|
|
2774
|
+
telemetry.runWithContext(
|
|
2775
|
+
{
|
|
2776
|
+
source: "pivot-rpc",
|
|
2777
|
+
operation: "POST /v1/replicate",
|
|
2778
|
+
swarm: url,
|
|
2779
|
+
},
|
|
2780
|
+
async () => {
|
|
2781
|
+
const requestContext = {
|
|
2782
|
+
...session.context,
|
|
2783
|
+
node: pivotNode,
|
|
2784
|
+
};
|
|
2785
|
+
try {
|
|
2786
|
+
return await handlePivotReplicateRequest(
|
|
2787
|
+
requestContext,
|
|
2788
|
+
url,
|
|
2789
|
+
request,
|
|
2790
|
+
requestSignal,
|
|
2791
|
+
);
|
|
2792
|
+
} finally {
|
|
2793
|
+
session.context.pivotSyncError =
|
|
2794
|
+
requestContext.pivotSyncError ?? null;
|
|
2795
|
+
}
|
|
2796
|
+
},
|
|
2797
|
+
),
|
|
2798
|
+
receipt: (receiptRequest, requestSignal) =>
|
|
2799
|
+
telemetry.runWithContext(
|
|
2800
|
+
{
|
|
2801
|
+
source: "pivot-rpc",
|
|
2802
|
+
operation: "POST /v1/receipt",
|
|
2803
|
+
swarm: url,
|
|
2804
|
+
},
|
|
2805
|
+
() =>
|
|
2806
|
+
handlePivotReceiptRequest(
|
|
2807
|
+
{ ...session.context, node: pivotNode },
|
|
2808
|
+
url,
|
|
2809
|
+
receiptRequest,
|
|
2810
|
+
requestSignal,
|
|
2811
|
+
),
|
|
2812
|
+
),
|
|
2813
|
+
search: (params) =>
|
|
2814
|
+
withState(session.context, (state) =>
|
|
2815
|
+
queryLocalSearchIndex(state, url, params),
|
|
2816
|
+
),
|
|
2817
|
+
...(context.diagnostic
|
|
2818
|
+
? { diagnostic: context.diagnostic }
|
|
2819
|
+
: {}),
|
|
2820
|
+
},
|
|
2821
|
+
session.context.fetch,
|
|
2822
|
+
effectiveSignal,
|
|
2823
|
+
);
|
|
2824
|
+
session.pivotServer = {
|
|
2825
|
+
server: mounted,
|
|
2826
|
+
nodeApi: session.node.api,
|
|
2827
|
+
epoch: record.config!.epoch,
|
|
2828
|
+
headId: record.config!.headId,
|
|
2829
|
+
listening: true,
|
|
2830
|
+
};
|
|
2831
|
+
} else {
|
|
2832
|
+
const remounted = await ensurePivotListener(
|
|
2833
|
+
session.node,
|
|
2834
|
+
session.pivotServer.server.address,
|
|
2835
|
+
effectiveSignal,
|
|
2836
|
+
session.context.fetch,
|
|
2837
|
+
);
|
|
2838
|
+
session.pivotServer.listening = true;
|
|
2839
|
+
if (remounted) {
|
|
2840
|
+
emitDiagnostic(
|
|
2841
|
+
`[${url}] Re-mounted Pivot P2P listener ${pivotReceiptProtocol} -> ${session.pivotServer.server.address}.`,
|
|
2842
|
+
);
|
|
2843
|
+
}
|
|
2844
|
+
}
|
|
2845
|
+
pivotListenerVerified = true;
|
|
2846
|
+
} else {
|
|
2847
|
+
await stopPivotServer(session);
|
|
2848
|
+
}
|
|
2849
|
+
const now = Date.now();
|
|
2850
|
+
const replicationIntervalMs =
|
|
2851
|
+
(context.syncInterval ?? 60) * 1000;
|
|
2852
|
+
const knownRemotePivots = session.candidates.observations();
|
|
2853
|
+
const online = new Set([
|
|
2854
|
+
...[...knownRemotePivots]
|
|
2855
|
+
.filter(([, value]) => value.probe === "success")
|
|
2856
|
+
.map(([peerId]) => peerId),
|
|
2857
|
+
...session.connections.peers.map((peer) => peer.peerId),
|
|
2858
|
+
]);
|
|
2859
|
+
const remoteOnline = [...online].filter(
|
|
2860
|
+
(peerId) => peerId !== session.node!.peerId,
|
|
2670
2861
|
);
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2862
|
+
const onlineRemotePivots = remoteOnline.filter((peerId) =>
|
|
2863
|
+
knownRemotePivots.has(peerId),
|
|
2864
|
+
);
|
|
2865
|
+
const remoteReadinessDue = onlineRemotePivots.some(
|
|
2866
|
+
(peerId) => {
|
|
2867
|
+
const entry = session.context.pivotReadiness?.[peerId];
|
|
2868
|
+
return (
|
|
2869
|
+
!entry ||
|
|
2870
|
+
now - entry.observedAt >=
|
|
2871
|
+
pivotReadinessRefreshIntervalMs
|
|
2872
|
+
);
|
|
2873
|
+
},
|
|
2874
|
+
);
|
|
2875
|
+
const replicationDue = now >= (session.nextReplication ?? 0);
|
|
2876
|
+
if (
|
|
2877
|
+
(onlineRemotePivots.length > 0 &&
|
|
2878
|
+
(remoteReadinessDue || replicationDue)) ||
|
|
2879
|
+
(remoteOnline.length > 0 && replicationDue)
|
|
2880
|
+
) {
|
|
2881
|
+
session.nextReplication = now + replicationIntervalMs;
|
|
2882
|
+
await replicateSwarm(
|
|
2883
|
+
session.context,
|
|
2884
|
+
url,
|
|
2885
|
+
effectiveSignal,
|
|
2886
|
+
{
|
|
2887
|
+
automatic: true,
|
|
2888
|
+
onlinePeers: online,
|
|
2889
|
+
},
|
|
2675
2890
|
);
|
|
2676
2891
|
}
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
session.nextReplication = now + replicationIntervalMs;
|
|
2711
|
-
await replicateSwarm(session.context, url, signal, {
|
|
2712
|
-
automatic: true,
|
|
2713
|
-
onlinePeers: online,
|
|
2714
|
-
});
|
|
2715
|
-
}
|
|
2716
|
-
if (session.pivot) {
|
|
2717
|
-
const pivotSyncDue = now >= (session.nextPivotSync ?? 0);
|
|
2718
|
-
const selfReadiness =
|
|
2719
|
-
session.context.pivotReadiness?.[session.node.peerId];
|
|
2720
|
-
const selfReadinessDue =
|
|
2721
|
-
!selfReadiness ||
|
|
2722
|
-
now - selfReadiness.observedAt >=
|
|
2723
|
-
pivotReadinessRefreshIntervalMs;
|
|
2724
|
-
if (pivotSyncDue) {
|
|
2725
|
-
session.nextPivotSync = now + replicationIntervalMs;
|
|
2726
|
-
try {
|
|
2727
|
-
await syncPivotPublications(session.context, url, signal);
|
|
2728
|
-
} finally {
|
|
2729
|
-
if (!signal.aborted) {
|
|
2892
|
+
if (session.pivot) {
|
|
2893
|
+
const pivotSyncDue = now >= (session.nextPivotSync ?? 0);
|
|
2894
|
+
const selfReadiness =
|
|
2895
|
+
session.context.pivotReadiness?.[session.node.peerId];
|
|
2896
|
+
const selfReadinessDue =
|
|
2897
|
+
!selfReadiness ||
|
|
2898
|
+
now - selfReadiness.observedAt >=
|
|
2899
|
+
pivotReadinessRefreshIntervalMs;
|
|
2900
|
+
if (pivotSyncDue) {
|
|
2901
|
+
session.nextPivotSync = now + replicationIntervalMs;
|
|
2902
|
+
try {
|
|
2903
|
+
await syncPivotPublications(
|
|
2904
|
+
session.context,
|
|
2905
|
+
url,
|
|
2906
|
+
effectiveSignal,
|
|
2907
|
+
);
|
|
2908
|
+
} finally {
|
|
2909
|
+
if (!effectiveSignal.aborted) {
|
|
2910
|
+
const status = await pivotStatus(
|
|
2911
|
+
session.context,
|
|
2912
|
+
url,
|
|
2913
|
+
effectiveSignal,
|
|
2914
|
+
);
|
|
2915
|
+
session.context.pivotReadiness ??= {};
|
|
2916
|
+
session.context.pivotReadiness[session.node.peerId] =
|
|
2917
|
+
{
|
|
2918
|
+
observedAt: Date.now(),
|
|
2919
|
+
status,
|
|
2920
|
+
error: null,
|
|
2921
|
+
};
|
|
2922
|
+
}
|
|
2923
|
+
}
|
|
2924
|
+
} else if (selfReadinessDue) {
|
|
2730
2925
|
const status = await pivotStatus(
|
|
2731
2926
|
session.context,
|
|
2732
2927
|
url,
|
|
2733
|
-
|
|
2928
|
+
effectiveSignal,
|
|
2734
2929
|
);
|
|
2735
2930
|
session.context.pivotReadiness ??= {};
|
|
2736
2931
|
session.context.pivotReadiness[session.node.peerId] = {
|
|
@@ -2740,136 +2935,139 @@ export async function swarmDaemon(
|
|
|
2740
2935
|
};
|
|
2741
2936
|
}
|
|
2742
2937
|
}
|
|
2743
|
-
} else if (selfReadinessDue) {
|
|
2744
|
-
const status = await pivotStatus(
|
|
2745
|
-
session.context,
|
|
2746
|
-
url,
|
|
2747
|
-
signal,
|
|
2748
|
-
);
|
|
2749
|
-
session.context.pivotReadiness ??= {};
|
|
2750
|
-
session.context.pivotReadiness[session.node.peerId] = {
|
|
2751
|
-
observedAt: Date.now(),
|
|
2752
|
-
status,
|
|
2753
|
-
error: null,
|
|
2754
|
-
};
|
|
2755
|
-
}
|
|
2756
|
-
}
|
|
2757
|
-
} catch (cause) {
|
|
2758
|
-
if (signal.aborted) return;
|
|
2759
|
-
if (!pivotListenerVerified && session.pivotServer)
|
|
2760
|
-
session.pivotServer.listening = false;
|
|
2761
|
-
session.error = message(cause);
|
|
2762
|
-
session.nextDiscovery = 0;
|
|
2763
|
-
emitDiagnostic(
|
|
2764
|
-
`[${url}] Daemon refresh failed (last-known local candidates retained): ${message(cause)}`,
|
|
2765
|
-
);
|
|
2766
|
-
if (
|
|
2767
|
-
cause instanceof HostError &&
|
|
2768
|
-
/membership removed|identity was removed|epoch rollback|Pinned Owner changed/.test(
|
|
2769
|
-
cause.message,
|
|
2770
|
-
)
|
|
2771
|
-
) {
|
|
2772
|
-
await stopPivotServer(session);
|
|
2773
|
-
if (session.node) await stopNode(session.node);
|
|
2774
|
-
session.candidates.reconcile(new Map());
|
|
2775
|
-
delete session.node;
|
|
2776
|
-
session.selected.clear();
|
|
2777
|
-
await persist(url, session, false);
|
|
2778
|
-
delete session.record;
|
|
2779
|
-
}
|
|
2780
|
-
}
|
|
2781
|
-
if (
|
|
2782
|
-
synchronize &&
|
|
2783
|
-
session.node &&
|
|
2784
|
-
!signal.aborted &&
|
|
2785
|
-
Date.now() >= session.nextSync
|
|
2786
|
-
) {
|
|
2787
|
-
let deferIsolatedPivot = false;
|
|
2788
|
-
if (
|
|
2789
|
-
session.pivot &&
|
|
2790
|
-
(session.connections?.peers.length ?? 0) === 0 &&
|
|
2791
|
-
session.candidates.active().size === 0 &&
|
|
2792
|
-
session.record?.config
|
|
2793
|
-
) {
|
|
2794
|
-
try {
|
|
2795
|
-
const c = coordinates(url);
|
|
2796
|
-
const head = headValue(
|
|
2797
|
-
await client(session.context, c.service).read(
|
|
2798
|
-
c.scopeId,
|
|
2799
|
-
session.record.config.headId,
|
|
2800
|
-
{ signal },
|
|
2801
|
-
),
|
|
2802
|
-
);
|
|
2803
|
-
deferIsolatedPivot = !(await hasLocalBlock(
|
|
2804
|
-
session.context.store,
|
|
2805
|
-
head.root,
|
|
2806
|
-
));
|
|
2807
2938
|
} catch (cause) {
|
|
2808
|
-
|
|
2939
|
+
if (effectiveSignal.aborted) return;
|
|
2940
|
+
if (!pivotListenerVerified && session.pivotServer)
|
|
2941
|
+
session.pivotServer.listening = false;
|
|
2942
|
+
session.error = message(cause);
|
|
2943
|
+
session.nextDiscovery = 0;
|
|
2809
2944
|
emitDiagnostic(
|
|
2810
|
-
`[${url}]
|
|
2945
|
+
`[${url}] Daemon refresh failed (last-known local candidates retained): ${message(cause)}`,
|
|
2811
2946
|
);
|
|
2947
|
+
if (
|
|
2948
|
+
cause instanceof HostError &&
|
|
2949
|
+
/membership removed|identity was removed|epoch rollback|Pinned Owner changed/.test(
|
|
2950
|
+
cause.message,
|
|
2951
|
+
)
|
|
2952
|
+
) {
|
|
2953
|
+
await stopPivotServer(session);
|
|
2954
|
+
if (session.node) await stopNode(session.node);
|
|
2955
|
+
session.candidates.reconcile(new Map());
|
|
2956
|
+
delete session.node;
|
|
2957
|
+
session.selected.clear();
|
|
2958
|
+
await persist(url, session, false);
|
|
2959
|
+
delete session.record;
|
|
2960
|
+
}
|
|
2812
2961
|
}
|
|
2813
|
-
|
|
2814
|
-
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
session.nextSync
|
|
2818
|
-
)
|
|
2819
|
-
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
}
|
|
2962
|
+
if (
|
|
2963
|
+
synchronize &&
|
|
2964
|
+
session.node &&
|
|
2965
|
+
!effectiveSignal.aborted &&
|
|
2966
|
+
Date.now() >= session.nextSync
|
|
2967
|
+
) {
|
|
2968
|
+
let deferIsolatedPivot = false;
|
|
2969
|
+
if (
|
|
2970
|
+
session.pivot &&
|
|
2971
|
+
(session.connections?.peers.length ?? 0) === 0 &&
|
|
2972
|
+
session.candidates.active().size === 0 &&
|
|
2973
|
+
session.record?.config
|
|
2974
|
+
) {
|
|
2975
|
+
try {
|
|
2976
|
+
const c = coordinates(url);
|
|
2977
|
+
const head = headValue(
|
|
2978
|
+
await client(session.context, c.service).read(
|
|
2979
|
+
c.scopeId,
|
|
2980
|
+
session.record.config.headId,
|
|
2981
|
+
{ signal: effectiveSignal },
|
|
2982
|
+
),
|
|
2983
|
+
);
|
|
2984
|
+
deferIsolatedPivot = !(await hasLocalBlock(
|
|
2985
|
+
session.context.store,
|
|
2986
|
+
head.root,
|
|
2987
|
+
));
|
|
2988
|
+
} catch (cause) {
|
|
2989
|
+
if (shutdownController.signal.aborted) return;
|
|
2990
|
+
signal.throwIfAborted();
|
|
2991
|
+
emitDiagnostic(
|
|
2992
|
+
`[${url}] Isolated Pivot HEAD check failed: ${message(cause)}`,
|
|
2993
|
+
);
|
|
2994
|
+
}
|
|
2995
|
+
}
|
|
2996
|
+
if (deferIsolatedPivot) {
|
|
2997
|
+
session.nextSync = Date.now() + 5_000;
|
|
2998
|
+
session.sync.nextSyncAt = new Date(
|
|
2999
|
+
session.nextSync,
|
|
3000
|
+
).toISOString();
|
|
2840
3001
|
} else {
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
3002
|
+
const attemptAt = new Date().toISOString();
|
|
3003
|
+
session.sync.lastAttemptAt = attemptAt;
|
|
3004
|
+
let reported: DaemonSyncSummary | undefined;
|
|
3005
|
+
try {
|
|
3006
|
+
const summary = await raceAbort(
|
|
3007
|
+
synchronize(session.context, url, (partial) => {
|
|
3008
|
+
reported = partial;
|
|
3009
|
+
}),
|
|
3010
|
+
effectiveSignal,
|
|
3011
|
+
);
|
|
3012
|
+
const applied = summary ?? reported;
|
|
3013
|
+
if (applied) {
|
|
3014
|
+
session.sync = {
|
|
3015
|
+
...session.sync,
|
|
3016
|
+
...applied,
|
|
3017
|
+
lastAttemptAt: attemptAt,
|
|
3018
|
+
lastSuccessAt: new Date().toISOString(),
|
|
3019
|
+
status: "ok",
|
|
3020
|
+
error: null,
|
|
3021
|
+
};
|
|
3022
|
+
} else {
|
|
3023
|
+
session.sync = {
|
|
3024
|
+
...session.sync,
|
|
3025
|
+
lastAttemptAt: attemptAt,
|
|
3026
|
+
lastSuccessAt: new Date().toISOString(),
|
|
3027
|
+
status: "ok",
|
|
3028
|
+
error: null,
|
|
3029
|
+
};
|
|
3030
|
+
}
|
|
3031
|
+
} catch (cause) {
|
|
3032
|
+
if (shutdownController.signal.aborted) return;
|
|
3033
|
+
signal.throwIfAborted();
|
|
3034
|
+
const detail = message(cause);
|
|
3035
|
+
emitDiagnostic(
|
|
3036
|
+
`[${url}] Swarm sync failed; will retry next interval: ${detail}`,
|
|
3037
|
+
);
|
|
3038
|
+
session.sync = {
|
|
3039
|
+
...session.sync,
|
|
3040
|
+
...(reported ?? {}),
|
|
3041
|
+
lastAttemptAt: attemptAt,
|
|
3042
|
+
status: "error",
|
|
3043
|
+
error: redact(detail, session),
|
|
3044
|
+
};
|
|
3045
|
+
} finally {
|
|
3046
|
+
session.nextSync =
|
|
3047
|
+
Date.now() + (context.syncInterval ?? 60) * 1000;
|
|
3048
|
+
session.sync.nextSyncAt = new Date(
|
|
3049
|
+
session.nextSync,
|
|
3050
|
+
).toISOString();
|
|
3051
|
+
}
|
|
2848
3052
|
}
|
|
2849
|
-
} catch (cause) {
|
|
2850
|
-
signal.throwIfAborted();
|
|
2851
|
-
const detail = message(cause);
|
|
2852
|
-
emitDiagnostic(
|
|
2853
|
-
`[${url}] Swarm sync failed; will retry next interval: ${detail}`,
|
|
2854
|
-
);
|
|
2855
|
-
session.sync = {
|
|
2856
|
-
...session.sync,
|
|
2857
|
-
...(reported ?? {}),
|
|
2858
|
-
lastAttemptAt: attemptAt,
|
|
2859
|
-
status: "error",
|
|
2860
|
-
error: redact(detail, session),
|
|
2861
|
-
};
|
|
2862
|
-
} finally {
|
|
2863
|
-
session.nextSync =
|
|
2864
|
-
Date.now() + (context.syncInterval ?? 60) * 1000;
|
|
2865
|
-
session.sync.nextSyncAt = new Date(
|
|
2866
|
-
session.nextSync,
|
|
2867
|
-
).toISOString();
|
|
2868
3053
|
}
|
|
2869
|
-
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
3054
|
+
if (!effectiveSignal.aborted) {
|
|
3055
|
+
await persist(url, session, true);
|
|
3056
|
+
}
|
|
3057
|
+
},
|
|
3058
|
+
),
|
|
3059
|
+
);
|
|
3060
|
+
if (shutdownRequested()) break;
|
|
3061
|
+
signal.throwIfAborted();
|
|
3062
|
+
if (wakeState.wakeRequested) {
|
|
3063
|
+
wakeState.wakeRequested = false;
|
|
3064
|
+
continue;
|
|
3065
|
+
}
|
|
3066
|
+
const sleepController = new AbortController();
|
|
3067
|
+
const onWake = () => {
|
|
3068
|
+
sleepController.abort();
|
|
3069
|
+
};
|
|
3070
|
+
wakeSleep = onWake;
|
|
2873
3071
|
try {
|
|
2874
3072
|
const nextSync = synchronize
|
|
2875
3073
|
? Math.min(
|
|
@@ -2883,23 +3081,48 @@ export async function swarmDaemon(
|
|
|
2883
3081
|
await delay(
|
|
2884
3082
|
Math.min(30_000 + Math.random() * 15_000, nextSync),
|
|
2885
3083
|
undefined,
|
|
2886
|
-
{
|
|
3084
|
+
{
|
|
3085
|
+
signal: AbortSignal.any([
|
|
3086
|
+
effectiveSignal,
|
|
3087
|
+
sleepController.signal,
|
|
3088
|
+
]),
|
|
3089
|
+
},
|
|
2887
3090
|
);
|
|
2888
3091
|
} catch {
|
|
3092
|
+
if (shutdownRequested()) break;
|
|
2889
3093
|
signal.throwIfAborted();
|
|
3094
|
+
} finally {
|
|
3095
|
+
if (wakeSleep === onWake) wakeSleep = undefined;
|
|
3096
|
+
wakeState.wakeRequested = false;
|
|
2890
3097
|
}
|
|
2891
3098
|
}
|
|
2892
3099
|
} finally {
|
|
2893
3100
|
try {
|
|
2894
|
-
|
|
3101
|
+
await closeReadiness?.();
|
|
3102
|
+
} finally {
|
|
3103
|
+
try {
|
|
3104
|
+
for (const [url, session] of sessions) {
|
|
3105
|
+
try {
|
|
3106
|
+
await persist(url, session, false);
|
|
3107
|
+
} finally {
|
|
3108
|
+
await closeSession(session);
|
|
3109
|
+
}
|
|
3110
|
+
}
|
|
3111
|
+
} finally {
|
|
2895
3112
|
try {
|
|
2896
|
-
await
|
|
3113
|
+
await daemonService.close();
|
|
2897
3114
|
} finally {
|
|
2898
|
-
|
|
3115
|
+
try {
|
|
3116
|
+
await telemetry.flush();
|
|
3117
|
+
} finally {
|
|
3118
|
+
try {
|
|
3119
|
+
await daemonStore.close();
|
|
3120
|
+
} finally {
|
|
3121
|
+
if (ownsDaemonState) daemonState.close();
|
|
3122
|
+
}
|
|
3123
|
+
}
|
|
2899
3124
|
}
|
|
2900
3125
|
}
|
|
2901
|
-
} finally {
|
|
2902
|
-
await closeReadiness?.();
|
|
2903
3126
|
}
|
|
2904
3127
|
}
|
|
2905
3128
|
},
|
|
@@ -2907,6 +3130,8 @@ export async function swarmDaemon(
|
|
|
2907
3130
|
1,
|
|
2908
3131
|
);
|
|
2909
3132
|
}
|
|
3133
|
+
const derivedLocalStores = new WeakSet<IpfsStore>();
|
|
3134
|
+
|
|
2910
3135
|
async function activate(context: SwarmContext, record: LocalMembership) {
|
|
2911
3136
|
const { node, peers, source } = await privateSource(context, record);
|
|
2912
3137
|
if (
|
|
@@ -2914,9 +3139,13 @@ async function activate(context: SwarmContext, record: LocalMembership) {
|
|
|
2914
3139
|
context.sourcePeers?.length !== peers.length ||
|
|
2915
3140
|
peers.some((peer, i) => context.sourcePeers?.[i] !== peer)
|
|
2916
3141
|
) {
|
|
2917
|
-
const
|
|
2918
|
-
|
|
3142
|
+
const previous = context.store;
|
|
3143
|
+
const store = previous.withLocalSource(source);
|
|
3144
|
+
derivedLocalStores.add(store);
|
|
2919
3145
|
context.store = store;
|
|
3146
|
+
if (derivedLocalStores.has(previous)) {
|
|
3147
|
+
await previous.close();
|
|
3148
|
+
}
|
|
2920
3149
|
context.sourcePeers = peers;
|
|
2921
3150
|
}
|
|
2922
3151
|
context.node = node;
|
|
@@ -3166,7 +3395,7 @@ export async function writeNamesHead(
|
|
|
3166
3395
|
await directory(context),
|
|
3167
3396
|
`state-pin-${createHash("sha256").update(url).digest("hex")}`,
|
|
3168
3397
|
);
|
|
3169
|
-
return
|
|
3398
|
+
return withSwarmLock(context, lock, async () => {
|
|
3170
3399
|
if (context.confirmPublication) {
|
|
3171
3400
|
try {
|
|
3172
3401
|
await context.confirmPublication(url, root);
|
|
@@ -4789,9 +5018,10 @@ export async function lazyPrivateSource(
|
|
|
4789
5018
|
return source;
|
|
4790
5019
|
};
|
|
4791
5020
|
// Same lock order as Owner rotation: credentials first, then the node.
|
|
4792
|
-
return
|
|
5021
|
+
return withSwarmLock(context, await directory(scoped), authorize, signal);
|
|
4793
5022
|
};
|
|
4794
|
-
const
|
|
5023
|
+
const previous = context.store;
|
|
5024
|
+
const store = previous.withLocalSource({
|
|
4795
5025
|
block: {
|
|
4796
5026
|
async get(cid, options = {}) {
|
|
4797
5027
|
const signal = options.signal
|
|
@@ -4804,8 +5034,11 @@ export async function lazyPrivateSource(
|
|
|
4804
5034
|
},
|
|
4805
5035
|
},
|
|
4806
5036
|
});
|
|
4807
|
-
|
|
5037
|
+
derivedLocalStores.add(store);
|
|
4808
5038
|
context.store = store;
|
|
5039
|
+
if (derivedLocalStores.has(previous)) {
|
|
5040
|
+
await previous.close();
|
|
5041
|
+
}
|
|
4809
5042
|
delete context.node;
|
|
4810
5043
|
delete context.sourcePeers;
|
|
4811
5044
|
context.finishRead = async () => {
|
|
@@ -7641,7 +7874,8 @@ export async function syncPivotReplicationAndIndex(
|
|
|
7641
7874
|
context.stateDir,
|
|
7642
7875
|
`pivot-index-${createHash("sha256").update(configUrl).digest("hex")}`,
|
|
7643
7876
|
);
|
|
7644
|
-
return
|
|
7877
|
+
return withSwarmLock(
|
|
7878
|
+
context,
|
|
7645
7879
|
lock,
|
|
7646
7880
|
() => updatePivotReplicationAndIndex(context, configUrl, header, signal),
|
|
7647
7881
|
signal,
|