From eb82858881fed04e88288a9ee66ef38db4ad8402 Mon Sep 17 00:00:00 2001 From: Erik Marks Date: Fri, 8 Mar 2024 16:49:06 -0800 Subject: [PATCH] refactor(daemon): Extract caplet incarnation prelude to function --- packages/cli/src/pet-name.js | 4 +- packages/daemon/src/daemon.js | 93 ++++++++++++++++++++--------------- packages/daemon/src/host.js | 2 +- 3 files changed, 57 insertions(+), 42 deletions(-) diff --git a/packages/cli/src/pet-name.js b/packages/cli/src/pet-name.js index fdea2a8214..2e18833364 100644 --- a/packages/cli/src/pet-name.js +++ b/packages/cli/src/pet-name.js @@ -1,3 +1,5 @@ +const { quote: q } = assert; + /** * Splits a dot-delimited pet name path into an array of pet names. * Throws if any of the path segments are empty. @@ -10,7 +12,7 @@ export const parsePetNamePath = petNamePath => { for (const petName of petNames) { if (petName === '') { throw new Error( - `Pet name path "${petNamePath}" contains an empty segment.`, + `Pet name path ${q(petNamePath)} contains an empty segment.`, ); } } diff --git a/packages/daemon/src/daemon.js b/packages/daemon/src/daemon.js index 7eabc55a70..205bc5559b 100644 --- a/packages/daemon/src/daemon.js +++ b/packages/daemon/src/daemon.js @@ -1214,6 +1214,41 @@ const makeDaemonCore = async ( return guestIncarnation.formulaIdentifier; }; + /** + * Helper for `incarnateUnconfined` and `incarnateBundle`. + * @param {'make-bundle' | 'make-unconfined'} formulaType + * @param {string} hostFormulaIdentifier + * @param {import('./types.js').DeferredTasks} deferredTasks + * @param {string} [specifiedWorkerFormulaIdentifier] + * @param {string} [specifiedPowersFormulaIdentifier] + */ + const incarnateCapletDependencies = async ( + formulaType, + hostFormulaIdentifier, + deferredTasks, + specifiedWorkerFormulaIdentifier, + specifiedPowersFormulaIdentifier, + ) => { + const ownFormulaNumber = await randomHex512(); + const identifiers = harden({ + powersFormulaIdentifier: await providePowersFormulaIdentifier( + hostFormulaIdentifier, + specifiedPowersFormulaIdentifier, + ), + capletFormulaIdentifier: serializeFormulaIdentifier({ + type: formulaType, + number: ownFormulaNumber, + node: ownNodeIdentifier, + }), + capletFormulaNumber: ownFormulaNumber, + workerFormulaIdentifier: await provideWorkerFormulaIdentifier( + specifiedWorkerFormulaIdentifier, + ), + }); + await deferredTasks.execute(identifiers); + return identifiers; + }; + /** @type {import('./types.js').DaemonCore['incarnateUnconfined']} */ const incarnateUnconfined = async ( hostFormulaIdentifier, @@ -1226,26 +1261,15 @@ const makeDaemonCore = async ( powersFormulaIdentifier, capletFormulaNumber, workerFormulaIdentifier, - } = await formulaGraphMutex.enqueue(async () => { - const ownFormulaNumber = await randomHex512(); - const identifiers = harden({ - powersFormulaIdentifier: await providePowersFormulaIdentifier( - hostFormulaIdentifier, - specifiedPowersFormulaIdentifier, - ), - capletFormulaIdentifier: serializeFormulaIdentifier({ - type: 'make-unconfined', - number: ownFormulaNumber, - node: ownNodeIdentifier, - }), - capletFormulaNumber: ownFormulaNumber, - workerFormulaIdentifier: await provideWorkerFormulaIdentifier( - specifiedWorkerFormulaIdentifier, - ), - }); - await deferredTasks.execute(identifiers); - return identifiers; - }); + } = await formulaGraphMutex.enqueue(() => + incarnateCapletDependencies( + 'make-unconfined', + hostFormulaIdentifier, + deferredTasks, + specifiedWorkerFormulaIdentifier, + specifiedPowersFormulaIdentifier, + ), + ); /** @type {import('./types.js').MakeUnconfinedFormula} */ const formula = { @@ -1273,26 +1297,15 @@ const makeDaemonCore = async ( powersFormulaIdentifier, capletFormulaNumber, workerFormulaIdentifier, - } = await formulaGraphMutex.enqueue(async () => { - const ownFormulaNumber = await randomHex512(); - const identifiers = harden({ - powersFormulaIdentifier: await providePowersFormulaIdentifier( - hostFormulaIdentifier, - specifiedPowersFormulaIdentifier, - ), - capletFormulaIdentifier: serializeFormulaIdentifier({ - type: 'make-bundle', - number: ownFormulaNumber, - node: ownNodeIdentifier, - }), - capletFormulaNumber: ownFormulaNumber, - workerFormulaIdentifier: await provideWorkerFormulaIdentifier( - specifiedWorkerFormulaIdentifier, - ), - }); - await deferredTasks.execute(identifiers); - return identifiers; - }); + } = await formulaGraphMutex.enqueue(() => + incarnateCapletDependencies( + 'make-bundle', + hostFormulaIdentifier, + deferredTasks, + specifiedWorkerFormulaIdentifier, + specifiedPowersFormulaIdentifier, + ), + ); /** @type {import('./types.js').MakeBundleFormula} */ const formula = { diff --git a/packages/daemon/src/host.js b/packages/daemon/src/host.js index aebd65c5ad..638d05a56a 100644 --- a/packages/daemon/src/host.js +++ b/packages/daemon/src/host.js @@ -412,7 +412,7 @@ export const makeHostMaker = ({ ) => { const bundleFormulaIdentifier = petStore.identifyLocal(bundleName); if (bundleFormulaIdentifier === undefined) { - throw new TypeError(`Unknown pet name for bundle: ${bundleName}`); + throw new TypeError(`Unknown pet name for bundle: ${q(bundleName)}`); } const { tasks, workerFormulaIdentifier, powersFormulaIdentifier } =