From cb4d6d64eef8db1258fc6736f299b05fea731ca2 Mon Sep 17 00:00:00 2001 From: Kris Kowal Date: Sat, 16 Mar 2024 06:38:17 -0700 Subject: [PATCH 1/2] refactor(daemon): Id means formula identifier --- packages/daemon/src/context.js | 38 +- packages/daemon/src/daemon.js | 728 ++++++++++------------ packages/daemon/src/directory.js | 54 +- packages/daemon/src/formula-identifier.js | 22 +- packages/daemon/src/guest.js | 40 +- packages/daemon/src/host.js | 212 +++---- packages/daemon/src/mail.js | 141 ++--- packages/daemon/src/pet-sitter.js | 26 +- packages/daemon/src/pet-store.js | 36 +- packages/daemon/src/types.d.ts | 142 ++--- packages/daemon/src/weak-multimap.js | 22 +- packages/daemon/test/test-endo.js | 6 +- 12 files changed, 667 insertions(+), 800 deletions(-) diff --git a/packages/daemon/src/context.js b/packages/daemon/src/context.js index a591eb428b..13e085f332 100644 --- a/packages/daemon/src/context.js +++ b/packages/daemon/src/context.js @@ -3,13 +3,13 @@ import { makePromiseKit } from '@endo/promise-kit'; export const makeContextMaker = ({ - controllerForFormulaIdentifier, - provideControllerForFormulaIdentifier, + controllerForId, + provideControllerForId, }) => { /** - * @param {string} formulaIdentifier + * @param {string} id */ - const makeContext = formulaIdentifier => { + const makeContext = id => { let done = false; const { promise: cancelled, reject: rejectCancelled } = /** @type {import('@endo/promise-kit').PromiseKit} */ ( @@ -25,14 +25,17 @@ export const makeContextMaker = ({ /** @type {Array<() => void>} */ const hooks = []; + /** + * @param {unknown} reason + */ const cancel = (reason, prefix = '*') => { if (done) return disposed; done = true; rejectCancelled(reason || harden(new Error('Cancelled'))); - console.log(`${prefix} ${formulaIdentifier}`); + console.log(`${prefix} ${id}`); - controllerForFormulaIdentifier.delete(formulaIdentifier); + controllerForId.delete(id); for (const dependentContext of dependents.values()) { dependentContext.cancel(reason, ` ${prefix}`); } @@ -44,18 +47,21 @@ export const makeContextMaker = ({ return disposed; }; - const thatDiesIfThisDies = dependentFormulaIdentifier => { + /** + * @param {string} dependentId + */ + const thatDiesIfThisDies = dependentId => { assert(!done); - const dependentController = provideControllerForFormulaIdentifier( - dependentFormulaIdentifier, - ); - dependents.set(dependentFormulaIdentifier, dependentController.context); + const dependentController = provideControllerForId(dependentId); + dependents.set(dependentId, dependentController.context); }; - const thisDiesIfThatDies = dependencyIdentifier => { - const dependencyController = - provideControllerForFormulaIdentifier(dependencyIdentifier); - dependencyController.context.thatDiesIfThisDies(formulaIdentifier); + /** + * @param {string} dependencyId + */ + const thisDiesIfThatDies = dependencyId => { + const dependencyController = provideControllerForId(dependencyId); + dependencyController.context.thatDiesIfThisDies(id); }; /** @@ -67,7 +73,7 @@ export const makeContextMaker = ({ }; return { - id: formulaIdentifier, + id, cancel, cancelled, disposed, diff --git a/packages/daemon/src/daemon.js b/packages/daemon/src/daemon.js index 7b75f54334..6ebcade973 100644 --- a/packages/daemon/src/daemon.js +++ b/packages/daemon/src/daemon.js @@ -121,7 +121,7 @@ const makeDaemonCore = async ( rootEntropy, cryptoPowers.makeSha512(), ); - const peersFormulaIdentifier = formatId({ + const peersId = formatId({ number: peersFormulaNumber, node: ownNodeIdentifier, }); @@ -132,7 +132,7 @@ const makeDaemonCore = async ( rootEntropy, cryptoPowers.makeSha512(), ); - const leastAuthorityFormulaIdentifier = formatId({ + const leastAuthorityId = formatId({ number: leastAuthorityFormulaNumber, node: ownNodeIdentifier, }); @@ -146,7 +146,7 @@ const makeDaemonCore = async ( rootEntropy, cryptoPowers.makeSha512(), ); - const mainWorkerFormulaIdentifier = formatId({ + const mainWorkerId = formatId({ number: mainWorkerFormulaNumber, node: ownNodeIdentifier, }); @@ -156,8 +156,8 @@ const makeDaemonCore = async ( /** @type {import('./types.js').Builtins} */ const builtins = { - NONE: leastAuthorityFormulaIdentifier, - MAIN: mainWorkerFormulaIdentifier, + NONE: leastAuthorityId, + MAIN: mainWorkerId, }; // Generate platform formulas (without incarnation) @@ -170,20 +170,20 @@ const makeDaemonCore = async ( rootEntropy, cryptoPowers.makeSha512(), ); - const formulaIdentifier = formatId({ + const id = formatId({ number: formulaNumber, node: ownNodeIdentifier, }); await persistencePowers.writeFormula(formulaNumber, formula); - return [specialName, formulaIdentifier]; + return [specialName, id]; }), ), ); /** * The two functions "formulate" and "provide" share a responsibility for - * maintaining the memoization tables "controllerForFormulaIdentifier" and - * "formulaIdentifierForRef". + * maintaining the memoization tables "controllerForId" and + * "idForRef". * "formulate" is used for incarnating and persisting new formulas, whereas * "provide" is used for reincarnating stored formulas. */ @@ -193,17 +193,17 @@ const makeDaemonCore = async ( * reference", and not for "what is my name for this promise". * @type {Map} */ - const controllerForFormulaIdentifier = new Map(); + const controllerForId = new Map(); /** * Reverse look-up, for answering "what is my name for this near or far * reference", and not for "what is my name for this promise". * @type {import('./types.js').WeakMultimap, string>} */ - const formulaIdentifierForRef = makeWeakMultimap(); + const idForRef = makeWeakMultimap(); /** @type {import('./types.js').WeakMultimap, string>['get']} */ - const getFormulaIdentifierForRef = ref => formulaIdentifierForRef.get(ref); + const getIdForRef = ref => idForRef.get(ref); /** * @param {string} workerId512 @@ -280,41 +280,41 @@ const makeDaemonCore = async ( }; /** - * @param {string} workerFormulaIdentifier + * @param {string} workerId * @param {string} source * @param {Array} codeNames - * @param {Array} formulaIdentifiers + * @param {Array} ids * @param {import('./types.js').Context} context */ const makeControllerForEval = async ( - workerFormulaIdentifier, + workerId, source, codeNames, - formulaIdentifiers, + ids, context, ) => { - context.thisDiesIfThatDies(workerFormulaIdentifier); - for (const formulaIdentifier of formulaIdentifiers) { - context.thisDiesIfThatDies(formulaIdentifier); + context.thisDiesIfThatDies(workerId); + for (const id of ids) { + context.thisDiesIfThatDies(id); } const workerController = /** @type {import('./types.js').Controller} */ ( // Behold, recursion: // eslint-disable-next-line no-use-before-define - provideControllerForFormulaIdentifier(workerFormulaIdentifier) + provideControllerForId(workerId) ); const workerDaemonFacet = workerController.internal; assert( workerDaemonFacet, - `panic: No internal bootstrap for worker ${workerFormulaIdentifier}`, + `panic: No internal bootstrap for worker ${workerId}`, ); const endowmentValues = await Promise.all( - formulaIdentifiers.map(formulaIdentifier => + ids.map(id => // Behold, recursion: // eslint-disable-next-line no-use-before-define - provide(formulaIdentifier), + provide(id), ), ); @@ -341,55 +341,51 @@ const makeDaemonCore = async ( * Creates a controller for a `lookup` formula. The external facet is the * resolved value of the lookup. * - * @param {string} hubFormulaIdentifier + * @param {string} hubId * @param {string[]} path * @param {import('./types.js').Context} context */ - const makeControllerForLookup = async ( - hubFormulaIdentifier, - path, - context, - ) => { - context.thisDiesIfThatDies(hubFormulaIdentifier); + const makeControllerForLookup = async (hubId, path, context) => { + context.thisDiesIfThatDies(hubId); // Behold, recursion: // eslint-disable-next-line no-use-before-define - const hub = provide(hubFormulaIdentifier); + const hub = provide(hubId); // @ts-expect-error calling lookup on an unknown object const external = E(hub).lookup(...path); return { external, internal: undefined }; }; /** - * @param {string} workerFormulaIdentifier - * @param {string} guestFormulaIdentifier + * @param {string} workerId + * @param {string} guestId * @param {string} specifier * @param {import('./types.js').Context} context */ const makeControllerForUnconfinedPlugin = async ( - workerFormulaIdentifier, - guestFormulaIdentifier, + workerId, + guestId, specifier, context, ) => { - context.thisDiesIfThatDies(workerFormulaIdentifier); - context.thisDiesIfThatDies(guestFormulaIdentifier); + context.thisDiesIfThatDies(workerId); + context.thisDiesIfThatDies(guestId); const workerController = /** @type {import('./types.js').Controller} */ ( // Behold, recursion: // eslint-disable-next-line no-use-before-define - provideControllerForFormulaIdentifier(workerFormulaIdentifier) + provideControllerForId(workerId) ); const workerDaemonFacet = workerController.internal; assert( workerDaemonFacet, - `panic: No internal bootstrap for worker ${workerFormulaIdentifier}`, + `panic: No internal bootstrap for worker ${workerId}`, ); const guestP = /** @type {Promise} */ ( // Behold, recursion: // eslint-disable-next-line no-use-before-define - provide(guestFormulaIdentifier) + provide(guestId) ); const external = E(workerDaemonFacet).makeUnconfined( specifier, @@ -401,30 +397,30 @@ const makeDaemonCore = async ( }; /** - * @param {string} workerFormulaIdentifier - * @param {string} guestFormulaIdentifier - * @param {string} bundleFormulaIdentifier + * @param {string} workerId + * @param {string} guestId + * @param {string} bundleId * @param {import('./types.js').Context} context */ const makeControllerForSafeBundle = async ( - workerFormulaIdentifier, - guestFormulaIdentifier, - bundleFormulaIdentifier, + workerId, + guestId, + bundleId, context, ) => { - context.thisDiesIfThatDies(workerFormulaIdentifier); - context.thisDiesIfThatDies(guestFormulaIdentifier); + context.thisDiesIfThatDies(workerId); + context.thisDiesIfThatDies(guestId); const workerController = /** @type {import('./types.js').Controller} */ ( // Behold, recursion: // eslint-disable-next-line no-use-before-define - provideControllerForFormulaIdentifier(workerFormulaIdentifier) + provideControllerForId(workerId) ); const workerDaemonFacet = workerController.internal; assert( workerDaemonFacet, - `panic: No internal bootstrap for worker ${workerFormulaIdentifier}`, + `panic: No internal bootstrap for worker ${workerId}`, ); // Behold, recursion: // eslint-disable-next-line no-use-before-define @@ -432,12 +428,12 @@ const makeDaemonCore = async ( /** @type {Promise} */ ( // Behold, recursion: // eslint-disable-next-line no-use-before-define - provide(bundleFormulaIdentifier) + provide(bundleId) ); const guestP = /** @type {Promise} */ ( // Behold, recursion: // eslint-disable-next-line no-use-before-define - provide(guestFormulaIdentifier) + provide(guestId) ); const external = E(workerDaemonFacet).makeBundle( readableBundleP, @@ -449,17 +445,12 @@ const makeDaemonCore = async ( }; /** - * @param {string} formulaIdentifier + * @param {string} id * @param {string} formulaNumber * @param {import('./types.js').Formula} formula * @param {import('./types.js').Context} context */ - const makeControllerForFormula = ( - formulaIdentifier, - formulaNumber, - formula, - context, - ) => { + const makeControllerForFormula = (id, formulaNumber, formula, context) => { if (formula.type === 'eval') { return makeControllerForEval( formula.worker, @@ -492,13 +483,13 @@ const makeDaemonCore = async ( // Behold, recursion: // eslint-disable-next-line no-use-before-define return makeIdentifiedHost( - formulaIdentifier, + id, formula.petStore, formula.inspector, formula.worker, formula.endo, formula.networks, - leastAuthorityFormulaIdentifier, + leastAuthorityId, platformNames, context, ); @@ -506,7 +497,7 @@ const makeDaemonCore = async ( // Behold, recursion: // eslint-disable-next-line no-use-before-define return makeIdentifiedGuestController( - formulaIdentifier, + id, formula.host, formula.petStore, formula.worker, @@ -517,15 +508,15 @@ const makeDaemonCore = async ( return { external: {}, internal: { - targetFormulaIdentifier: formula.target, + targetId: formula.target, }, }; } else if (formula.type === 'endo') { // Gateway is equivalent to E's "nonce locator". It provides a value for // a formula identifier to a remote client. const gateway = Far('Gateway', { - provide: async requestedFormulaIdentifier => { - const { node } = parseId(requestedFormulaIdentifier); + provide: async requestedId => { + const { node } = parseId(requestedId); if (node !== ownNodeIdentifier) { throw new Error( `Gateway can only provide local values. Got request for node ${q( @@ -534,7 +525,7 @@ const makeDaemonCore = async ( ); } // eslint-disable-next-line no-use-before-define - return provide(requestedFormulaIdentifier); + return provide(requestedId); }, }); /** @type {import('./types.js').FarEndoBootstrap} */ @@ -555,7 +546,7 @@ const makeDaemonCore = async ( // Behold, recursion: return /** @type {Promise} */ ( // eslint-disable-next-line no-use-before-define - provide(leastAuthorityFormulaIdentifier) + provide(leastAuthorityId) ); }, gateway: async () => { @@ -568,10 +559,9 @@ const makeDaemonCore = async ( // eslint-disable-next-line no-use-before-define await provide(formula.networks) ); - const networkFormulaIdentifiers = - await networksDirectory.listIdentifiers(); + const networkIds = await networksDirectory.listIdentifiers(); await Promise.allSettled( - networkFormulaIdentifiers.map( + networkIds.map( // Behold, recursion: // eslint-disable-next-line no-use-before-define provide, @@ -592,10 +582,10 @@ const makeDaemonCore = async ( // TODO: merge connection info return; } - const { formulaIdentifier: peerFormulaIdentifier } = + const { id: peerId } = // eslint-disable-next-line no-use-before-define await incarnatePeer(formula.networks, addresses); - await peerPetstore.write(nodeName, peerFormulaIdentifier); + await peerPetstore.write(nodeName, peerId); }, }); return { @@ -658,7 +648,7 @@ const makeDaemonCore = async ( // Behold, forward-reference: // eslint-disable-next-line no-use-before-define return makeIdentifiedDirectory({ - petStoreFormulaIdentifier: formula.petStore, + petStoreId: formula.petStore, context, }); } else if (formula.type === 'peer') { @@ -671,24 +661,18 @@ const makeDaemonCore = async ( }; /** - * @param {string} formulaIdentifier + * @param {string} id * @param {import('./types.js').Context} context */ - const makeControllerForFormulaIdentifier = async ( - formulaIdentifier, - context, - ) => { - const { number: formulaNumber, node: formulaNode } = - parseId(formulaIdentifier); + const makeControllerForId = async (id, context) => { + const { number: formulaNumber, node: formulaNode } = parseId(id); const isRemote = formulaNode !== ownNodeIdentifier; if (isRemote) { // eslint-disable-next-line no-use-before-define - const peerIdentifier = await getPeerFormulaIdentifierForNodeIdentifier( - formulaNode, - ); + const peerIdentifier = await getPeerIdForNodeIdentifier(formulaNode); // Behold, forward reference: // eslint-disable-next-line no-use-before-define - return provideRemoteValue(peerIdentifier, formulaIdentifier); + return provideRemoteValue(peerIdentifier, id); } const formula = await persistencePowers.readFormula(formulaNumber); console.log(`Making ${formula.type} ${formulaNumber}`); @@ -712,28 +696,21 @@ const makeDaemonCore = async ( 'directory', ].includes(formula.type) ) { - assert.Fail`Invalid formula identifier, unrecognized type ${q( - formulaIdentifier, - )}`; + assert.Fail`Invalid formula identifier, unrecognized type ${q(id)}`; } // TODO further validation - return makeControllerForFormula( - formulaIdentifier, - formulaNumber, - formula, - context, - ); + return makeControllerForFormula(id, formulaNumber, formula, context); }; /** @type {import('./types.js').DaemonCore['formulate']} */ const formulate = async (formulaNumber, formula) => { - const formulaIdentifier = formatId({ + const id = formatId({ number: formulaNumber, node: ownNodeIdentifier, }); // Memoize for lookup. - console.log(`Making ${formulaIdentifier}`); + console.log(`Making ${id}`); const { promise: partial, resolve: resolvePartial } = /** @type {import('@endo/promise-kit').PromiseKit>} */ ( makePromiseKit() @@ -741,23 +718,23 @@ const makeDaemonCore = async ( // Behold, recursion: // eslint-disable-next-line no-use-before-define - const context = makeContext(formulaIdentifier); + const context = makeContext(id); partial.catch(context.cancel); const controller = harden({ context, external: E.get(partial).external.then(value => { if (typeof value === 'object' && value !== null) { - formulaIdentifierForRef.add(value, formulaIdentifier); + idForRef.add(value, id); } return value; }), internal: E.get(partial).internal, }); - controllerForFormulaIdentifier.set(formulaIdentifier, controller); + controllerForId.set(id, controller); // The controller _must_ be constructed in the synchronous prelude of this function. const controllerValue = makeControllerForFormula( - formulaIdentifier, + id, formulaNumber, formula, context, @@ -770,14 +747,14 @@ const makeDaemonCore = async ( await written; return harden({ - formulaIdentifier, + id, value: controller.external, }); }; - /** @type {import('./types.js').DaemonCore['provideControllerForFormulaIdentifier']} */ - const provideControllerForFormulaIdentifier = formulaIdentifier => { - let controller = controllerForFormulaIdentifier.get(formulaIdentifier); + /** @type {import('./types.js').DaemonCore['provideControllerForId']} */ + const provideControllerForId = id => { + let controller = controllerForId.get(id); if (controller !== undefined) { return controller; } @@ -789,16 +766,16 @@ const makeDaemonCore = async ( // Behold, recursion: // eslint-disable-next-line no-use-before-define - const context = makeContext(formulaIdentifier); + const context = makeContext(id); partial.catch(context.cancel); controller = harden({ context, external: E.get(partial).external, internal: E.get(partial).internal, }); - controllerForFormulaIdentifier.set(formulaIdentifier, controller); + controllerForId.set(id, controller); - resolve(makeControllerForFormulaIdentifier(formulaIdentifier, context)); + resolve(makeControllerForId(id, context)); return controller; }; @@ -812,71 +789,68 @@ const makeDaemonCore = async ( * @param {string} nodeIdentifier * @returns {Promise} */ - const getPeerFormulaIdentifierForNodeIdentifier = async nodeIdentifier => { + const getPeerIdForNodeIdentifier = async nodeIdentifier => { if (nodeIdentifier === ownNodeIdentifier) { throw new Error(`Cannot get peer formula identifier for self`); } const peerStore = /** @type {import('./types.js').PetStore} */ ( // eslint-disable-next-line no-use-before-define - await provide(peersFormulaIdentifier) + await provide(peersId) ); const nodeName = petStoreNameForNodeIdentifier(nodeIdentifier); - const peerFormulaIdentifier = peerStore.identifyLocal(nodeName); - if (peerFormulaIdentifier === undefined) { + const peerId = peerStore.identifyLocal(nodeName); + if (peerId === undefined) { throw new Error( `No peer found for node identifier ${q(nodeIdentifier)}.`, ); } - return peerFormulaIdentifier; + return peerId; }; /** @type {import('./types.js').DaemonCore['cancelValue']} */ - const cancelValue = async (formulaIdentifier, reason) => { + const cancelValue = async (id, reason) => { await formulaGraphJobs.enqueue(); - const controller = provideControllerForFormulaIdentifier(formulaIdentifier); + const controller = provideControllerForId(id); console.log('Cancelled:'); return controller.context.cancel(reason); }; /** @type {import('./types.js').DaemonCore['provide']} */ - const provide = formulaIdentifier => { + const provide = id => { const controller = /** @type {import('./types.js').Controller<>} */ ( - provideControllerForFormulaIdentifier(formulaIdentifier) + provideControllerForId(id) ); return controller.external.then(value => { // Release the value to the public only after ensuring // we can reverse-lookup its nonce. if (typeof value === 'object' && value !== null) { - formulaIdentifierForRef.add(value, formulaIdentifier); + idForRef.add(value, id); } return value; }); }; - /** @type {import('./types.js').DaemonCore['provideControllerForFormulaIdentifierAndResolveHandle']} */ - const provideControllerForFormulaIdentifierAndResolveHandle = - async formulaIdentifier => { - let currentFormulaIdentifier = formulaIdentifier; - // eslint-disable-next-line no-constant-condition - while (true) { - const controller = provideControllerForFormulaIdentifier( - currentFormulaIdentifier, - ); - // eslint-disable-next-line no-await-in-loop - const internalFacet = await controller.internal; - if (internalFacet === undefined || internalFacet === null) { - return controller; - } - // @ts-expect-error We can't know the type of the internal facet. - if (internalFacet.targetFormulaIdentifier === undefined) { - return controller; - } - const handle = /** @type {import('./types.js').InternalHandle} */ ( - internalFacet - ); - currentFormulaIdentifier = handle.targetFormulaIdentifier; + /** @type {import('./types.js').DaemonCore['provideControllerForIdAndResolveHandle']} */ + const provideControllerForIdAndResolveHandle = async id => { + let currentId = id; + // eslint-disable-next-line no-constant-condition + while (true) { + const controller = provideControllerForId(currentId); + // eslint-disable-next-line no-await-in-loop + const internalFacet = await controller.internal; + if (internalFacet === undefined || internalFacet === null) { + return controller; } - }; + // @ts-expect-error We can't know the type of the internal facet. + if (internalFacet.targetId === undefined) { + return controller; + } + const handle = /** @type {import('./types.js').InternalHandle} */ ( + internalFacet + ); + currentId = handle.targetId; + } + }; /** @type {import('./types.js').DaemonCore['incarnateReadableBlob']} */ const incarnateReadableBlob = async (readerRef, deferredTasks) => { @@ -888,7 +862,7 @@ const makeDaemonCore = async ( }; await deferredTasks.execute({ - readableBlobFormulaIdentifier: formatId({ + readableBlobId: formatId({ number: values.formulaNumber, node: ownNodeIdentifier, }), @@ -914,14 +888,14 @@ const makeDaemonCore = async ( * The returned promise is resolved after the formula is persisted. * * @param {string} formulaNumber - The formula number of the handle to incarnate. - * @param {string} targetFormulaIdentifier - The formula identifier of the handle's target. + * @param {string} targetId - The formula identifier of the handle's target. * @returns {import('./types.js').IncarnateResult} The incarnated handle. */ - const incarnateNumberedHandle = (formulaNumber, targetFormulaIdentifier) => { + const incarnateNumberedHandle = (formulaNumber, targetId) => { /** @type {import('./types.js').HandleFormula} */ const formula = { type: 'handle', - target: targetFormulaIdentifier, + target: targetId, }; return /** @type {import('./types').IncarnateResult} */ ( formulate(formulaNumber, formula) @@ -949,13 +923,14 @@ const makeDaemonCore = async ( * @type {import('./types.js').DaemonCore['incarnateDirectory']} */ const incarnateDirectory = async () => { - const { formulaIdentifier: petStoreFormulaIdentifier } = - await incarnateNumberedPetStore(await randomHex512()); + const { id: petStoreId } = await incarnateNumberedPetStore( + await randomHex512(), + ); const formulaNumber = await randomHex512(); /** @type {import('./types.js').DirectoryFormula} */ const formula = { type: 'directory', - petStore: petStoreFormulaIdentifier, + petStore: petStoreId, }; return /** @type {import('./types').IncarnateResult} */ ( formulate(formulaNumber, formula) @@ -989,7 +964,7 @@ const makeDaemonCore = async ( const formulaNumber = await randomHex512(); await deferredTasks.execute({ - workerFormulaIdentifier: formatId({ + workerId: formatId({ number: formulaNumber, node: ownNodeIdentifier, }), @@ -1004,29 +979,20 @@ const makeDaemonCore = async ( * @type {import('./types.js').DaemonCoreInternal['incarnateHostDependencies']} */ const incarnateHostDependencies = async specifiedIdentifiers => { - const { - specifiedWorkerFormulaIdentifier, - ...remainingSpecifiedIdentifiers - } = specifiedIdentifiers; + const { specifiedWorkerId, ...remainingSpecifiedIdentifiers } = + specifiedIdentifiers; - const storeFormulaIdentifier = ( - await incarnateNumberedPetStore(await randomHex512()) - ).formulaIdentifier; + const storeId = (await incarnateNumberedPetStore(await randomHex512())).id; return harden({ ...remainingSpecifiedIdentifiers, hostFormulaNumber: await randomHex512(), - storeFormulaIdentifier, + storeId, /* eslint-disable no-use-before-define */ - inspectorFormulaIdentifier: ( - await incarnateNumberedPetInspector( - await randomHex512(), - storeFormulaIdentifier, - ) - ).formulaIdentifier, - workerFormulaIdentifier: await provideWorkerFormulaIdentifier( - specifiedWorkerFormulaIdentifier, - ), + inspectorId: ( + await incarnateNumberedPetInspector(await randomHex512(), storeId) + ).id, + workerId: await provideWorkerId(specifiedWorkerId), /* eslint-enable no-use-before-define */ }); }; @@ -1036,11 +1002,11 @@ const makeDaemonCore = async ( /** @type {import('./types.js').HostFormula} */ const formula = { type: 'host', - petStore: identifiers.storeFormulaIdentifier, - inspector: identifiers.inspectorFormulaIdentifier, - worker: identifiers.workerFormulaIdentifier, - endo: identifiers.endoFormulaIdentifier, - networks: identifiers.networksDirectoryFormulaIdentifier, + petStore: identifiers.storeId, + inspector: identifiers.inspectorId, + worker: identifiers.workerId, + endo: identifiers.endoId, + networks: identifiers.networksDirectoryId, }; return /** @type {import('./types').IncarnateResult} */ ( @@ -1050,21 +1016,21 @@ const makeDaemonCore = async ( /** @type {import('./types.js').DaemonCore['incarnateHost']} */ const incarnateHost = async ( - endoFormulaIdentifier, - networksDirectoryFormulaIdentifier, + endoId, + networksDirectoryId, deferredTasks, - specifiedWorkerFormulaIdentifier, + specifiedWorkerId, ) => { return incarnateNumberedHost( await formulaGraphJobs.enqueue(async () => { const identifiers = await incarnateHostDependencies({ - endoFormulaIdentifier, - networksDirectoryFormulaIdentifier, - specifiedWorkerFormulaIdentifier, + endoId, + networksDirectoryId, + specifiedWorkerId, }); await deferredTasks.execute({ - agentFormulaIdentifier: formatId({ + agentId: formatId({ number: identifiers.hostFormulaNumber, node: ownNodeIdentifier, }), @@ -1076,21 +1042,14 @@ const makeDaemonCore = async ( }; /** @type {import('./types.js').DaemonCoreInternal['incarnateGuestDependencies']} */ - const incarnateGuestDependencies = async hostFormulaIdentifier => + const incarnateGuestDependencies = async hostId => harden({ guestFormulaNumber: await randomHex512(), - hostHandleFormulaIdentifier: ( - await incarnateNumberedHandle( - await randomHex512(), - hostFormulaIdentifier, - ) - ).formulaIdentifier, - storeFormulaIdentifier: ( - await incarnateNumberedPetStore(await randomHex512()) - ).formulaIdentifier, - workerFormulaIdentifier: ( - await incarnateNumberedWorker(await randomHex512()) - ).formulaIdentifier, + hostHandleId: ( + await incarnateNumberedHandle(await randomHex512(), hostId) + ).id, + storeId: (await incarnateNumberedPetStore(await randomHex512())).id, + workerId: (await incarnateNumberedWorker(await randomHex512())).id, }); /** @type {import('./types.js').DaemonCoreInternal['incarnateNumberedGuest']} */ @@ -1098,9 +1057,9 @@ const makeDaemonCore = async ( /** @type {import('./types.js').GuestFormula} */ const formula = { type: 'guest', - host: identifiers.hostHandleFormulaIdentifier, - petStore: identifiers.storeFormulaIdentifier, - worker: identifiers.workerFormulaIdentifier, + host: identifiers.hostHandleId, + petStore: identifiers.storeId, + worker: identifiers.workerId, }; return /** @type {import('./types').IncarnateResult} */ ( @@ -1109,15 +1068,13 @@ const makeDaemonCore = async ( }; /** @type {import('./types.js').DaemonCore['incarnateGuest']} */ - const incarnateGuest = async (hostFormulaIdentifier, deferredTasks) => { + const incarnateGuest = async (hostId, deferredTasks) => { return incarnateNumberedGuest( await formulaGraphJobs.enqueue(async () => { - const identifiers = await incarnateGuestDependencies( - hostFormulaIdentifier, - ); + const identifiers = await incarnateGuestDependencies(hostId); await deferredTasks.execute({ - agentFormulaIdentifier: formatId({ + agentId: formatId({ number: identifiers.guestFormulaNumber, node: ownNodeIdentifier, }), @@ -1129,79 +1086,73 @@ const makeDaemonCore = async ( }; /** - * @param {string} [specifiedWorkerFormulaIdentifier] + * @param {string} [specifiedWorkerId] */ - const provideWorkerFormulaIdentifier = - async specifiedWorkerFormulaIdentifier => { - await null; - if (typeof specifiedWorkerFormulaIdentifier === 'string') { - return specifiedWorkerFormulaIdentifier; - } + const provideWorkerId = async specifiedWorkerId => { + await null; + if (typeof specifiedWorkerId === 'string') { + return specifiedWorkerId; + } - const workerFormulaNumber = await randomHex512(); - const workerIncarnation = await incarnateNumberedWorker( - workerFormulaNumber, - ); - return workerIncarnation.formulaIdentifier; - }; + const workerFormulaNumber = await randomHex512(); + const workerIncarnation = await incarnateNumberedWorker( + workerFormulaNumber, + ); + return workerIncarnation.id; + }; /** @type {import('./types.js').DaemonCore['incarnateEval']} */ const incarnateEval = async ( - nameHubFormulaIdentifier, + nameHubId, source, codeNames, - endowmentFormulaIdsOrPaths, + endowmentIdsOrPaths, deferredTasks, - specifiedWorkerFormulaIdentifier, + specifiedWorkerId, ) => { - const { - workerFormulaIdentifier, - endowmentFormulaIdentifiers, - evalFormulaNumber, - } = await formulaGraphJobs.enqueue(async () => { - const ownFormulaNumber = await randomHex512(); - const ownFormulaIdentifier = formatId({ - number: ownFormulaNumber, - node: ownNodeIdentifier, - }); + const { workerId, endowmentIds, evalFormulaNumber } = + await formulaGraphJobs.enqueue(async () => { + const ownFormulaNumber = await randomHex512(); + const ownId = formatId({ + number: ownFormulaNumber, + node: ownNodeIdentifier, + }); - const identifiers = harden({ - workerFormulaIdentifier: await provideWorkerFormulaIdentifier( - specifiedWorkerFormulaIdentifier, - ), - endowmentFormulaIdentifiers: await Promise.all( - endowmentFormulaIdsOrPaths.map(async formulaIdOrPath => { - if (typeof formulaIdOrPath === 'string') { - return formulaIdOrPath; - } - return ( - /* eslint-disable no-use-before-define */ - ( - await incarnateNumberedLookup( - await randomHex512(), - nameHubFormulaIdentifier, - formulaIdOrPath, - ) - ).formulaIdentifier - /* eslint-enable no-use-before-define */ - ); - }), - ), - evalFormulaIdentifier: ownFormulaIdentifier, - evalFormulaNumber: ownFormulaNumber, - }); + const identifiers = harden({ + workerId: await provideWorkerId(specifiedWorkerId), + endowmentIds: await Promise.all( + endowmentIdsOrPaths.map(async formulaIdOrPath => { + if (typeof formulaIdOrPath === 'string') { + return formulaIdOrPath; + } + return ( + /* eslint-disable no-use-before-define */ + ( + await incarnateNumberedLookup( + await randomHex512(), + nameHubId, + formulaIdOrPath, + ) + ).id + /* eslint-enable no-use-before-define */ + ); + }), + ), + evalId: ownId, + evalFormulaNumber: ownFormulaNumber, + }); - await deferredTasks.execute(identifiers); - return identifiers; - }); + await deferredTasks.execute(identifiers); + return identifiers; + }); /** @type {import('./types.js').EvalFormula} */ const formula = { type: 'eval', - worker: workerFormulaIdentifier, + worker: workerId, source, names: codeNames, - values: endowmentFormulaIdentifiers, + values: endowmentIds, }; return /** @type {import('./types.js').IncarnateResult} */ ( formulate(evalFormulaNumber, formula) @@ -1212,21 +1163,17 @@ const makeDaemonCore = async ( * Incarnates a `lookup` formula and synchronously adds it to the formula graph. * The returned promise is resolved after the formula is persisted. * @param {string} formulaNumber - The lookup formula's number. - * @param {string} hubFormulaIdentifier - The formula identifier of the naming + * @param {string} hubId - The formula identifier of the naming * hub to call `lookup` on. A "naming hub" is an objected with a variadic * lookup method. It includes objects such as guests and hosts. * @param {string[]} petNamePath - The pet name path to look up. - * @returns {Promise<{ formulaIdentifier: string, value: import('./types').EndoWorker }>} + * @returns {Promise<{ id: string, value: import('./types').EndoWorker }>} */ - const incarnateNumberedLookup = ( - formulaNumber, - hubFormulaIdentifier, - petNamePath, - ) => { + const incarnateNumberedLookup = (formulaNumber, hubId, petNamePath) => { /** @type {import('./types.js').LookupFormula} */ const formula = { type: 'lookup', - hub: hubFormulaIdentifier, + hub: hubId, path: petNamePath, }; @@ -1236,54 +1183,44 @@ const makeDaemonCore = async ( }; /** - * @param {string} hostFormulaIdentifier - * @param {string} [specifiedPowersFormulaIdentifier] + * @param {string} hostId + * @param {string} [specifiedPowersId] */ - const providePowersFormulaIdentifier = async ( - hostFormulaIdentifier, - specifiedPowersFormulaIdentifier, - ) => { + const providePowersId = async (hostId, specifiedPowersId) => { await null; - if (typeof specifiedPowersFormulaIdentifier === 'string') { - return specifiedPowersFormulaIdentifier; + if (typeof specifiedPowersId === 'string') { + return specifiedPowersId; } - const guestIncarnationData = await incarnateGuestDependencies( - hostFormulaIdentifier, - ); + const guestIncarnationData = await incarnateGuestDependencies(hostId); const guestIncarnation = await incarnateNumberedGuest(guestIncarnationData); - return guestIncarnation.formulaIdentifier; + return guestIncarnation.id; }; /** * Helper for `incarnateUnconfined` and `incarnateBundle`. * @param {'make-bundle' | 'make-unconfined'} formulaType - * @param {string} hostFormulaIdentifier + * @param {string} hostId * @param {import('./types.js').DeferredTasks} deferredTasks - * @param {string} [specifiedWorkerFormulaIdentifier] - * @param {string} [specifiedPowersFormulaIdentifier] + * @param {string} [specifiedWorkerId] + * @param {string} [specifiedPowersId] */ const incarnateCapletDependencies = async ( formulaType, - hostFormulaIdentifier, + hostId, deferredTasks, - specifiedWorkerFormulaIdentifier, - specifiedPowersFormulaIdentifier, + specifiedWorkerId, + specifiedPowersId, ) => { const ownFormulaNumber = await randomHex512(); const identifiers = harden({ - powersFormulaIdentifier: await providePowersFormulaIdentifier( - hostFormulaIdentifier, - specifiedPowersFormulaIdentifier, - ), - capletFormulaIdentifier: formatId({ + powersId: await providePowersId(hostId, specifiedPowersId), + capletId: formatId({ number: ownFormulaNumber, node: ownNodeIdentifier, }), capletFormulaNumber: ownFormulaNumber, - workerFormulaIdentifier: await provideWorkerFormulaIdentifier( - specifiedWorkerFormulaIdentifier, - ), + workerId: await provideWorkerId(specifiedWorkerId), }); await deferredTasks.execute(identifiers); return identifiers; @@ -1291,31 +1228,28 @@ const makeDaemonCore = async ( /** @type {import('./types.js').DaemonCore['incarnateUnconfined']} */ const incarnateUnconfined = async ( - hostFormulaIdentifier, + hostId, specifier, deferredTasks, - specifiedWorkerFormulaIdentifier, - specifiedPowersFormulaIdentifier, + specifiedWorkerId, + specifiedPowersId, ) => { - const { - powersFormulaIdentifier, - capletFormulaNumber, - workerFormulaIdentifier, - } = await formulaGraphJobs.enqueue(() => - incarnateCapletDependencies( - 'make-unconfined', - hostFormulaIdentifier, - deferredTasks, - specifiedWorkerFormulaIdentifier, - specifiedPowersFormulaIdentifier, - ), - ); + const { powersId, capletFormulaNumber, workerId } = + await formulaGraphJobs.enqueue(() => + incarnateCapletDependencies( + 'make-unconfined', + hostId, + deferredTasks, + specifiedWorkerId, + specifiedPowersId, + ), + ); /** @type {import('./types.js').MakeUnconfinedFormula} */ const formula = { type: 'make-unconfined', - worker: workerFormulaIdentifier, - powers: powersFormulaIdentifier, + worker: workerId, + powers: powersId, specifier, }; return formulate(capletFormulaNumber, formula); @@ -1323,48 +1257,42 @@ const makeDaemonCore = async ( /** @type {import('./types.js').DaemonCore['incarnateBundle']} */ const incarnateBundle = async ( - hostFormulaIdentifier, - bundleFormulaIdentifier, + hostId, + bundleId, deferredTasks, - specifiedWorkerFormulaIdentifier, - specifiedPowersFormulaIdentifier, + specifiedWorkerId, + specifiedPowersId, ) => { - const { - powersFormulaIdentifier, - capletFormulaNumber, - workerFormulaIdentifier, - } = await formulaGraphJobs.enqueue(() => - incarnateCapletDependencies( - 'make-bundle', - hostFormulaIdentifier, - deferredTasks, - specifiedWorkerFormulaIdentifier, - specifiedPowersFormulaIdentifier, - ), - ); + const { powersId, capletFormulaNumber, workerId } = + await formulaGraphJobs.enqueue(() => + incarnateCapletDependencies( + 'make-bundle', + hostId, + deferredTasks, + specifiedWorkerId, + specifiedPowersId, + ), + ); /** @type {import('./types.js').MakeBundleFormula} */ const formula = { type: 'make-bundle', - worker: workerFormulaIdentifier, - powers: powersFormulaIdentifier, - bundle: bundleFormulaIdentifier, + worker: workerId, + powers: powersId, + bundle: bundleId, }; return formulate(capletFormulaNumber, formula); }; /** * @param {string} formulaNumber - * @param {string} petStoreFormulaIdentifier + * @param {string} petStoreId */ - const incarnateNumberedPetInspector = ( - formulaNumber, - petStoreFormulaIdentifier, - ) => { + const incarnateNumberedPetInspector = (formulaNumber, petStoreId) => { /** @type {import('./types.js').PetInspectorFormula} */ const formula = { type: 'pet-inspector', - petStore: petStoreFormulaIdentifier, + petStore: petStoreId, }; return /** @type {import('./types').IncarnateResult} */ ( formulate(formulaNumber, formula) @@ -1372,17 +1300,14 @@ const makeDaemonCore = async ( }; /** @type {import('./types.js').DaemonCore['incarnatePeer']} */ - const incarnatePeer = async ( - networksDirectoryFormulaIdentifier, - addresses, - ) => { + const incarnatePeer = async (networksDirectoryId, addresses) => { const formulaNumber = await randomHex512(); // TODO: validate addresses // TODO: mutable state like addresses should not be stored in formula /** @type {import('./types.js').PeerFormula} */ const formula = { type: 'peer', - networks: networksDirectoryFormulaIdentifier, + networks: networksDirectoryId, addresses, }; return /** @type {import('./types').IncarnateResult} */ ( @@ -1404,58 +1329,56 @@ const makeDaemonCore = async ( /** @type {import('./types.js').DaemonCore['incarnateNetworksDirectory']} */ const incarnateNetworksDirectory = async () => { - const { formulaIdentifier, value } = await incarnateDirectory(); + const { id, value } = await incarnateDirectory(); // Make default networks. - const { formulaIdentifier: loopbackNetworkFormulaIdentifier } = - await incarnateLoopbackNetwork(); - await E(value).write(['loop'], loopbackNetworkFormulaIdentifier); - return { formulaIdentifier, value }; + const { id: loopbackNetworkId } = await incarnateLoopbackNetwork(); + await E(value).write(['loop'], loopbackNetworkId); + return { id, value }; }; /** @type {import('./types.js').DaemonCore['incarnateEndoBootstrap']} */ const incarnateEndoBootstrap = async specifiedFormulaNumber => { const identifiers = await formulaGraphJobs.enqueue(async () => { const formulaNumber = await (specifiedFormulaNumber ?? randomHex512()); - const endoFormulaIdentifier = formatId({ + const endoId = formatId({ number: formulaNumber, node: ownNodeIdentifier, }); - const { formulaIdentifier: defaultHostWorkerFormulaIdentifier } = - await incarnateNumberedWorker(await randomHex512()); - const { formulaIdentifier: networksDirectoryFormulaIdentifier } = - await incarnateNetworksDirectory(); - const { formulaIdentifier: newPeersFormulaIdentifier } = - await incarnateNumberedPetStore(peersFormulaNumber); - if (newPeersFormulaIdentifier !== peersFormulaIdentifier) { - assert.Fail`Peers PetStore formula identifier did not match expected value, expected ${peersFormulaIdentifier}, got ${newPeersFormulaIdentifier}`; + const { id: defaultHostWorkerId } = await incarnateNumberedWorker( + await randomHex512(), + ); + const { id: networksDirectoryId } = await incarnateNetworksDirectory(); + const { id: newPeersId } = await incarnateNumberedPetStore( + peersFormulaNumber, + ); + if (newPeersId !== peersId) { + assert.Fail`Peers PetStore formula identifier did not match expected value, expected ${peersId}, got ${newPeersId}`; } // Ensure the default host is incarnated and persisted. - const { formulaIdentifier: defaultHostFormulaIdentifier } = - await incarnateNumberedHost( - await incarnateHostDependencies({ - endoFormulaIdentifier, - networksDirectoryFormulaIdentifier, - specifiedWorkerFormulaIdentifier: - defaultHostWorkerFormulaIdentifier, - }), - ); + const { id: defaultHostId } = await incarnateNumberedHost( + await incarnateHostDependencies({ + endoId, + networksDirectoryId, + specifiedWorkerId: defaultHostWorkerId, + }), + ); return { formulaNumber, - defaultHostFormulaIdentifier, - networksDirectoryFormulaIdentifier, + defaultHostId, + networksDirectoryId, }; }); /** @type {import('./types.js').EndoFormula} */ const formula = { type: 'endo', - networks: identifiers.networksDirectoryFormulaIdentifier, - peers: peersFormulaIdentifier, - host: identifiers.defaultHostFormulaIdentifier, - leastAuthority: leastAuthorityFormulaIdentifier, + networks: identifiers.networksDirectoryId, + peers: peersId, + host: identifiers.defaultHostId, + leastAuthority: leastAuthorityId, }; return /** @type {import('./types').IncarnateResult} */ ( @@ -1464,24 +1387,24 @@ const makeDaemonCore = async ( }; /** - * @param {string} networksDirectoryFormulaIdentifier + * @param {string} networksDirectoryId * @returns {Promise} */ - const getAllNetworks = async networksDirectoryFormulaIdentifier => { + const getAllNetworks = async networksDirectoryId => { const networksDirectory = /** @type {import('./types').EndoDirectory} */ ( // eslint-disable-next-line no-use-before-define - await provide(networksDirectoryFormulaIdentifier) + await provide(networksDirectoryId) ); - const networkFormulaIdentifiers = await networksDirectory.listIdentifiers(); + const networkIds = await networksDirectory.listIdentifiers(); const networks = /** @type {import('./types').EndoNetwork[]} */ ( - await Promise.all(networkFormulaIdentifiers.map(provide)) + await Promise.all(networkIds.map(provide)) ); return networks; }; /** @type {import('./types.js').DaemonCore['getAllNetworkAddresses']} */ - const getAllNetworkAddresses = async networksDirectoryFormulaIdentifier => { - const networks = await getAllNetworks(networksDirectoryFormulaIdentifier); + const getAllNetworkAddresses = async networksDirectoryId => { + const networks = await getAllNetworks(networksDirectoryId); const addresses = ( await Promise.all( networks.map(async network => { @@ -1493,22 +1416,18 @@ const makeDaemonCore = async ( }; /** - * @param {string} networksDirectoryFormulaIdentifier + * @param {string} networksDirectoryId * @param {string[]} addresses * @param {import('./types.js').Context} context * @returns {Promise} */ - const makePeer = async ( - networksDirectoryFormulaIdentifier, - addresses, - context, - ) => { + const makePeer = async (networksDirectoryId, addresses, context) => { // TODO race networks that support protocol for connection // TODO retry, exponential back-off, with full jitter // TODO (in connect implementations) allow for the possibility of // connection loss and invalidate the connection formula and its transitive // dependees when this occurs. - const networks = await getAllNetworks(networksDirectoryFormulaIdentifier); + const networks = await getAllNetworks(networksDirectoryId); // Connect on first support address. for (const address of addresses) { const { protocol } = new URL(address); @@ -1520,9 +1439,9 @@ const makeDaemonCore = async ( makeFarContext(context), ); const external = Promise.resolve({ - provide: remoteFormulaIdentifier => { + provide: remoteId => { return /** @type {Promise} */ ( - E(remoteGateway).provide(remoteFormulaIdentifier) + E(remoteGateway).provide(remoteId) ); }, }); @@ -1542,49 +1461,46 @@ const makeDaemonCore = async ( /** * This is used to provide a value for a formula identifier that is known to * originate from the specified peer. - * @param {string} peerFormulaIdentifier - * @param {string} remoteValueFormulaIdentifier + * @param {string} peerId + * @param {string} remoteValueId * @returns {Promise>} */ - const provideRemoteValue = async ( - peerFormulaIdentifier, - remoteValueFormulaIdentifier, - ) => { + const provideRemoteValue = async (peerId, remoteValueId) => { const peer = /** @type {import('./types.js').EndoPeer} */ ( - await provide(peerFormulaIdentifier) + await provide(peerId) ); - const remoteValueP = peer.provide(remoteValueFormulaIdentifier); + const remoteValueP = peer.provide(remoteValueId); const external = remoteValueP; const internal = Promise.resolve(undefined); return harden({ internal, external }); }; const makeContext = makeContextMaker({ - controllerForFormulaIdentifier, - provideControllerForFormulaIdentifier, + controllerForId, + provideControllerForId, }); const { makeIdentifiedDirectory, makeDirectoryNode } = makeDirectoryMaker({ provide, - getFormulaIdentifierForRef, + getIdForRef, incarnateDirectory, }); const makeMailbox = makeMailboxMaker({ provide, - provideControllerForFormulaIdentifierAndResolveHandle, + provideControllerForIdAndResolveHandle, }); const makeIdentifiedGuestController = makeGuestMaker({ provide, - provideControllerForFormulaIdentifierAndResolveHandle, + provideControllerForIdAndResolveHandle, makeMailbox, makeDirectoryNode, }); const makeIdentifiedHost = makeHostMaker({ provide, - provideControllerForFormulaIdentifier, + provideControllerForId, cancelValue, incarnateWorker, incarnateHost, @@ -1605,12 +1521,12 @@ const makeDaemonCore = async ( * un-nameable values such as the `MAIN` worker. See `KnownEndoInspectors` for * more details. * - * @param {string} petStoreFormulaIdentifier + * @param {string} petStoreId * @returns {Promise} */ - const makePetStoreInspector = async petStoreFormulaIdentifier => { + const makePetStoreInspector = async petStoreId => { const petStore = /** @type {import('./types').PetStore} */ ( - await provide(petStoreFormulaIdentifier) + await provide(petStoreId) ); /** @@ -1619,11 +1535,11 @@ const makeDaemonCore = async ( * inspector for the value of the given pet name. */ const lookup = async petName => { - const formulaIdentifier = petStore.identifyLocal(petName); - if (formulaIdentifier === undefined) { + const id = petStore.identifyLocal(petName); + if (id === undefined) { throw new Error(`Unknown pet name ${petName}`); } - const { number: formulaNumber } = parseId(formulaIdentifier); + const { number: formulaNumber } = parseId(id); // TODO memoize formulas at the root of the // id->formula->controller->incarnation tree. const formula = await persistencePowers.readFormula(formulaNumber); @@ -1711,11 +1627,11 @@ const makeDaemonCore = async ( /** @type {import('./types.js').DaemonCore} */ const daemonCore = { nodeIdentifier: ownNodeIdentifier, - provideControllerForFormulaIdentifier, - provideControllerForFormulaIdentifierAndResolveHandle, + provideControllerForId, + provideControllerForIdAndResolveHandle, provide, formulate, - getFormulaIdentifierForRef, + getIdForRef, getAllNetworkAddresses, cancelValue, makeMailbox, @@ -1760,12 +1676,12 @@ const provideEndoBootstrap = async ( }); const isInitialized = !isNewlyCreated; if (isInitialized) { - const endoFormulaIdentifier = formatId({ + const endoId = formatId({ number: endoFormulaNumber, node: daemonCore.nodeIdentifier, }); return /** @type {Promise} */ ( - daemonCore.provide(endoFormulaIdentifier) + daemonCore.provide(endoId) ); } else { const { value: endoBootstrap } = await daemonCore.incarnateEndoBootstrap( diff --git a/packages/daemon/src/directory.js b/packages/daemon/src/directory.js index 021f4fa833..2df2c5ece4 100644 --- a/packages/daemon/src/directory.js +++ b/packages/daemon/src/directory.js @@ -8,12 +8,12 @@ const { quote: q } = assert; /** * @param {object} args * @param {import('./types.js').DaemonCore['provide']} args.provide - * @param {import('./types.js').DaemonCore['getFormulaIdentifierForRef']} args.getFormulaIdentifierForRef + * @param {import('./types.js').DaemonCore['getIdForRef']} args.getIdForRef * @param {import('./types.js').DaemonCore['incarnateDirectory']} args.incarnateDirectory */ export const makeDirectoryMaker = ({ provide, - getFormulaIdentifierForRef, + getIdForRef, incarnateDirectory, }) => { /** @type {import('./types.js').MakeDirectoryNode} */ @@ -21,13 +21,13 @@ export const makeDirectoryMaker = ({ /** @type {import('./types.js').EndoDirectory['lookup']} */ const lookup = (...petNamePath) => { const [headName, ...tailNames] = petNamePath; - const formulaIdentifier = petStore.identifyLocal(headName); - if (formulaIdentifier === undefined) { + const id = petStore.identifyLocal(headName); + if (id === undefined) { throw new TypeError(`Unknown pet name: ${q(headName)}`); } // Behold, recursion: // eslint-disable-next-line no-use-before-define - const value = provide(formulaIdentifier); + const value = provide(id); return tailNames.reduce( // @ts-expect-error We assume its a NameHub (directory, petName) => E(directory).lookup(petName), @@ -37,11 +37,11 @@ export const makeDirectoryMaker = ({ /** @type {import('./types.js').EndoDirectory['reverseLookup']} */ const reverseLookup = async presence => { - const formulaIdentifier = getFormulaIdentifierForRef(await presence); - if (formulaIdentifier === undefined) { + const id = getIdForRef(await presence); + if (id === undefined) { return harden([]); } - return petStore.reverseIdentify(formulaIdentifier); + return petStore.reverseIdentify(id); }; /** @@ -101,9 +101,9 @@ export const makeDirectoryMaker = ({ const identities = new Set(); await Promise.all( names.map(async name => { - const formulaIdentifier = await identify(...petNamePath, name); - if (formulaIdentifier !== undefined) { - identities.add(formulaIdentifier); + const id = await identify(...petNamePath, name); + if (id !== undefined) { + identities.add(id); } }), ); @@ -148,12 +148,12 @@ export const makeDirectoryMaker = ({ } return; } - const formulaIdentifier = await fromHub.identify(fromName); - if (formulaIdentifier === undefined) { + const id = await fromHub.identify(fromName); + if (id === undefined) { throw new Error(`Unknown name: ${q(fromPath)}`); } const removeP = fromHub.remove(fromName); - const addP = toHub.write([toName], formulaIdentifier); + const addP = toHub.write([toName], id); await Promise.all([addP, removeP]); }; @@ -163,30 +163,29 @@ export const makeDirectoryMaker = ({ fromPath, ); const { hub: toHub, name: toName } = await lookupTailNameHub(toPath); - const formulaIdentifier = await fromHub.identify(fromName); - if (formulaIdentifier === undefined) { + const id = await fromHub.identify(fromName); + if (id === undefined) { throw new Error(`Unknown name: ${q(fromPath)}`); } - await toHub.write([toName], formulaIdentifier); + await toHub.write([toName], id); }; /** @type {import('./types.js').EndoDirectory['makeDirectory']} */ const makeDirectory = async directoryPetName => { - const { value: directory, formulaIdentifier } = - await incarnateDirectory(); - await petStore.write(directoryPetName, formulaIdentifier); + const { value: directory, id } = await incarnateDirectory(); + await petStore.write(directoryPetName, id); return directory; }; /** @type {import('./types.js').EndoDirectory['write']} */ - const write = async (petNamePath, formulaIdentifier) => { + const write = async (petNamePath, id) => { if (petNamePath.length === 1) { const petName = petNamePath[0]; - await petStore.write(petName, formulaIdentifier); + await petStore.write(petName, id); return; } const { hub, name } = await lookupTailNameHub(petNamePath); - await hub.write([name], formulaIdentifier); + await hub.write([name], id); }; /** @type {import('./types.js').EndoDirectory} */ @@ -209,17 +208,14 @@ export const makeDirectoryMaker = ({ /** * @param {object} args - * @param {string} args.petStoreFormulaIdentifier + * @param {string} args.petStoreId * @param {import('./types.js').Context} args.context */ - const makeIdentifiedDirectory = async ({ - petStoreFormulaIdentifier, - context, - }) => { + const makeIdentifiedDirectory = async ({ petStoreId, context }) => { // TODO thread context const petStore = /** @type {import('./types.js').PetStore} */ ( - await provide(petStoreFormulaIdentifier) + await provide(petStoreId) ); const directory = makeDirectoryNode(petStore); diff --git a/packages/daemon/src/formula-identifier.js b/packages/daemon/src/formula-identifier.js index 70ae4ee064..516b6ef379 100644 --- a/packages/daemon/src/formula-identifier.js +++ b/packages/daemon/src/formula-identifier.js @@ -5,13 +5,13 @@ const { quote: q } = assert; const idPattern = /^(?[0-9a-f]{128}):(?[0-9a-f]{128})$/; /** - * @param {string} formulaIdentifier + * @param {string} id * @param {string} [petName] * @returns {void} */ -export const assertValidId = (formulaIdentifier, petName) => { - if (!idPattern.test(formulaIdentifier)) { - let message = `Invalid formula identifier ${q(formulaIdentifier)}`; +export const assertValidId = (id, petName) => { + if (!idPattern.test(id)) { + let message = `Invalid formula identifier ${q(id)}`; if (petName !== undefined) { message += ` for pet name ${q(petName)}`; } @@ -20,19 +20,19 @@ export const assertValidId = (formulaIdentifier, petName) => { }; /** - * @param {string} formulaIdentifier - * @returns {import("./types").FormulaIdentifierRecord} + * @param {string} id + * @returns {import("./types").IdRecord} */ -export const parseId = formulaIdentifier => { - const match = idPattern.exec(formulaIdentifier); +export const parseId = id => { + const match = idPattern.exec(id); if (match === null) { - throw assert.error(`Invalid formula identifier ${q(formulaIdentifier)}`); + throw assert.error(`Invalid formula identifier ${q(id)}`); } const { groups } = match; if (groups === undefined) { throw assert.error( `Programmer invariant failure: expected match groups, formula identifier was ${q( - formulaIdentifier, + id, )}`, ); } @@ -42,7 +42,7 @@ export const parseId = formulaIdentifier => { }; /** - * @param {import("./types").FormulaIdentifierRecord} formulaRecord + * @param {import("./types").IdRecord} formulaRecord * @returns {string} */ export const formatId = ({ number, node }) => { diff --git a/packages/daemon/src/guest.js b/packages/daemon/src/guest.js index d9d838da36..92d16484c2 100644 --- a/packages/daemon/src/guest.js +++ b/packages/daemon/src/guest.js @@ -7,48 +7,44 @@ import { makePetSitter } from './pet-sitter.js'; /** * @param {object} args * @param {import('./types.js').DaemonCore['provide']} args.provide - * @param {import('./types.js').DaemonCore['provideControllerForFormulaIdentifierAndResolveHandle']} args.provideControllerForFormulaIdentifierAndResolveHandle + * @param {import('./types.js').DaemonCore['provideControllerForIdAndResolveHandle']} args.provideControllerForIdAndResolveHandle * @param {import('./types.js').MakeMailbox} args.makeMailbox * @param {import('./types.js').MakeDirectoryNode} args.makeDirectoryNode */ export const makeGuestMaker = ({ provide, - provideControllerForFormulaIdentifierAndResolveHandle, + provideControllerForIdAndResolveHandle, makeMailbox, makeDirectoryNode, }) => { /** - * @param {string} guestFormulaIdentifier - * @param {string} hostHandleFormulaIdentifier - * @param {string} petStoreFormulaIdentifier - * @param {string} mainWorkerFormulaIdentifier + * @param {string} guestId + * @param {string} hostHandleId + * @param {string} petStoreId + * @param {string} mainWorkerId * @param {import('./types.js').Context} context */ const makeIdentifiedGuestController = async ( - guestFormulaIdentifier, - hostHandleFormulaIdentifier, - petStoreFormulaIdentifier, - mainWorkerFormulaIdentifier, + guestId, + hostHandleId, + petStoreId, + mainWorkerId, context, ) => { - context.thisDiesIfThatDies(hostHandleFormulaIdentifier); - context.thisDiesIfThatDies(petStoreFormulaIdentifier); - context.thisDiesIfThatDies(mainWorkerFormulaIdentifier); + context.thisDiesIfThatDies(hostHandleId); + context.thisDiesIfThatDies(petStoreId); + context.thisDiesIfThatDies(mainWorkerId); const basePetStore = /** @type {import('./types.js').PetStore} */ ( - await provide(petStoreFormulaIdentifier) + await provide(petStoreId) ); const specialStore = makePetSitter(basePetStore, { - SELF: guestFormulaIdentifier, - HOST: hostHandleFormulaIdentifier, + SELF: guestId, + HOST: hostHandleId, }); const hostController = /** @type {import('./types.js').EndoHostController} */ - ( - await provideControllerForFormulaIdentifierAndResolveHandle( - hostHandleFormulaIdentifier, - ) - ); + (await provideControllerForIdAndResolveHandle(hostHandleId)); const hostPrivateFacet = await hostController.internal; const { respond: deliverToHost } = hostPrivateFacet; if (deliverToHost === undefined) { @@ -59,7 +55,7 @@ export const makeGuestMaker = ({ const mailbox = makeMailbox({ petStore: specialStore, - selfFormulaIdentifier: guestFormulaIdentifier, + selfId: guestId, context, }); const { petStore } = mailbox; diff --git a/packages/daemon/src/host.js b/packages/daemon/src/host.js index 618106939c..53f1cfc59f 100644 --- a/packages/daemon/src/host.js +++ b/packages/daemon/src/host.js @@ -16,7 +16,7 @@ const assertPowersName = name => { /** * @param {object} args * @param {import('./types.js').DaemonCore['provide']} args.provide - * @param {import('./types.js').DaemonCore['provideControllerForFormulaIdentifier']} args.provideControllerForFormulaIdentifier + * @param {import('./types.js').DaemonCore['provideControllerForId']} args.provideControllerForId * @param {import('./types.js').DaemonCore['cancelValue']} args.cancelValue * @param {import('./types.js').DaemonCore['incarnateWorker']} args.incarnateWorker * @param {import('./types.js').DaemonCore['incarnateHost']} args.incarnateHost @@ -32,7 +32,7 @@ const assertPowersName = name => { */ export const makeHostMaker = ({ provide, - provideControllerForFormulaIdentifier, + provideControllerForId, cancelValue, incarnateWorker, incarnateHost, @@ -47,47 +47,47 @@ export const makeHostMaker = ({ ownNodeIdentifier, }) => { /** - * @param {string} hostFormulaIdentifier - * @param {string} storeFormulaIdentifier - * @param {string} inspectorFormulaIdentifier - * @param {string} mainWorkerFormulaIdentifier - * @param {string} endoFormulaIdentifier - * @param {string} networksDirectoryFormulaIdentifier - * @param {string} leastAuthorityFormulaIdentifier + * @param {string} hostId + * @param {string} storeId + * @param {string} inspectorId + * @param {string} mainWorkerId + * @param {string} endoId + * @param {string} networksDirectoryId + * @param {string} leastAuthorityId * @param {{[name: string]: string}} platformNames * @param {import('./types.js').Context} context */ const makeIdentifiedHost = async ( - hostFormulaIdentifier, - storeFormulaIdentifier, - inspectorFormulaIdentifier, - mainWorkerFormulaIdentifier, - endoFormulaIdentifier, - networksDirectoryFormulaIdentifier, - leastAuthorityFormulaIdentifier, + hostId, + storeId, + inspectorId, + mainWorkerId, + endoId, + networksDirectoryId, + leastAuthorityId, platformNames, context, ) => { - context.thisDiesIfThatDies(storeFormulaIdentifier); - context.thisDiesIfThatDies(mainWorkerFormulaIdentifier); + context.thisDiesIfThatDies(storeId); + context.thisDiesIfThatDies(mainWorkerId); const basePetStore = /** @type {import('./types.js').PetStore} */ ( // Behold, recursion: // eslint-disable-next-line no-use-before-define - await provide(storeFormulaIdentifier) + await provide(storeId) ); const specialStore = makePetSitter(basePetStore, { ...platformNames, - SELF: hostFormulaIdentifier, - ENDO: endoFormulaIdentifier, - NETS: networksDirectoryFormulaIdentifier, - INFO: inspectorFormulaIdentifier, - NONE: leastAuthorityFormulaIdentifier, + SELF: hostId, + ENDO: endoId, + NETS: networksDirectoryId, + INFO: inspectorId, + NONE: leastAuthorityId, }); const mailbox = makeMailbox({ petStore: specialStore, - selfFormulaIdentifier: hostFormulaIdentifier, + selfId: hostId, context, }); const { petStore } = mailbox; @@ -97,7 +97,7 @@ export const makeHostMaker = ({ const getEndoBootstrap = async () => { const endoBootstrap = /** @type {import('./types.js').FarEndoBootstrap} */ ( - await provide(endoFormulaIdentifier) + await provide(endoId) ); return endoBootstrap; }; @@ -113,7 +113,7 @@ export const makeHostMaker = ({ if (petName !== undefined) { assertPetName(petName); tasks.push(identifiers => - petStore.write(petName, identifiers.readableBlobFormulaIdentifier), + petStore.write(petName, identifiers.readableBlobId), ); } @@ -128,14 +128,14 @@ export const makeHostMaker = ({ /** @type {import('./types.js').DeferredTasks} */ const tasks = makeDeferredTasks(); // eslint-disable-next-line no-use-before-define - const workerFormulaIdentifier = tryGetWorkerFormulaIdentifier(workerName); + const workerId = tryGetWorkerId(workerName); // eslint-disable-next-line no-use-before-define - prepareWorkerIncarnation(workerName, workerFormulaIdentifier, tasks.push); + prepareWorkerIncarnation(workerName, workerId, tasks.push); - if (workerFormulaIdentifier !== undefined) { + if (workerId !== undefined) { return /** @type {Promise} */ ( // Behold, recursion: - provide(workerFormulaIdentifier) + provide(workerId) ); } @@ -147,9 +147,9 @@ export const makeHostMaker = ({ * @param {string} workerName * @returns {string | undefined} */ - const tryGetWorkerFormulaIdentifier = workerName => { + const tryGetWorkerId = workerName => { if (workerName === 'MAIN') { - return mainWorkerFormulaIdentifier; + return mainWorkerId; } else if (workerName === 'NEW') { return undefined; } @@ -158,17 +158,13 @@ export const makeHostMaker = ({ /** * @param {string} workerName - * @param {string | undefined} workerFormulaIdentifier - * @param {import('./types.js').DeferredTasks<{ workerFormulaIdentifier: string }>['push']} deferTask + * @param {string | undefined} workerId + * @param {import('./types.js').DeferredTasks<{ workerId: string }>['push']} deferTask */ - const prepareWorkerIncarnation = ( - workerName, - workerFormulaIdentifier, - deferTask, - ) => { - if (workerFormulaIdentifier === undefined) { + const prepareWorkerIncarnation = (workerName, workerId, deferTask) => { + if (workerId === undefined) { deferTask(identifiers => - petStore.write(workerName, identifiers.workerFormulaIdentifier), + petStore.write(workerName, identifiers.workerId), ); } }; @@ -197,8 +193,8 @@ export const makeHostMaker = ({ /** @type {import('./types.js').DeferredTasks} */ const tasks = makeDeferredTasks(); - const workerFormulaIdentifier = tryGetWorkerFormulaIdentifier(workerName); - prepareWorkerIncarnation(workerName, workerFormulaIdentifier, tasks.push); + const workerId = tryGetWorkerId(workerName); + prepareWorkerIncarnation(workerName, workerId, tasks.push); /** @type {(string | string[])[]} */ const endowmentFormulaIdsOrPaths = petNamePaths.map( @@ -209,11 +205,11 @@ export const makeHostMaker = ({ const petNamePath = petNamePathFrom(petNameOrPath); if (petNamePath.length === 1) { - const formulaIdentifier = petStore.identifyLocal(petNamePath[0]); - if (formulaIdentifier === undefined) { + const id = petStore.identifyLocal(petNamePath[0]); + if (id === undefined) { throw new Error(`Unknown pet name ${q(petNamePath[0])}`); } - return formulaIdentifier; + return id; } return petNamePath; @@ -222,17 +218,17 @@ export const makeHostMaker = ({ if (resultName !== undefined) { tasks.push(identifiers => - petStore.write(resultName, identifiers.evalFormulaIdentifier), + petStore.write(resultName, identifiers.evalId), ); } const { value } = await incarnateEval( - hostFormulaIdentifier, + hostId, source, codeNames, endowmentFormulaIdsOrPaths, tasks, - workerFormulaIdentifier, + workerId, ); return value; }; @@ -249,23 +245,23 @@ export const makeHostMaker = ({ /** @type {import('./types.js').DeferredTasks} */ const tasks = makeDeferredTasks(); - const workerFormulaIdentifier = tryGetWorkerFormulaIdentifier(workerName); - prepareWorkerIncarnation(workerName, workerFormulaIdentifier, tasks.push); + const workerId = tryGetWorkerId(workerName); + prepareWorkerIncarnation(workerName, workerId, tasks.push); - const powersFormulaIdentifier = petStore.identifyLocal(powersName); - if (powersFormulaIdentifier === undefined) { + const powersId = petStore.identifyLocal(powersName); + if (powersId === undefined) { tasks.push(identifiers => - petStore.write(powersName, identifiers.powersFormulaIdentifier), + petStore.write(powersName, identifiers.powersId), ); } if (resultName !== undefined) { tasks.push(identifiers => - petStore.write(resultName, identifiers.capletFormulaIdentifier), + petStore.write(resultName, identifiers.capletId), ); } - return { tasks, workerFormulaIdentifier, powersFormulaIdentifier }; + return { tasks, workerId, powersId }; }; /** @type {import('./types.js').EndoHost['makeUnconfined']} */ @@ -275,17 +271,20 @@ export const makeHostMaker = ({ powersName, resultName, ) => { - const { tasks, workerFormulaIdentifier, powersFormulaIdentifier } = - prepareMakeCaplet(powersName, workerName, resultName); + const { tasks, workerId, powersId } = prepareMakeCaplet( + powersName, + workerName, + resultName, + ); // Behold, recursion: // eslint-disable-next-line no-use-before-define const { value } = await incarnateUnconfined( - hostFormulaIdentifier, + hostId, specifier, tasks, - workerFormulaIdentifier, - powersFormulaIdentifier, + workerId, + powersId, ); return value; }; @@ -302,22 +301,25 @@ export const makeHostMaker = ({ powersName, resultName, ) => { - const bundleFormulaIdentifier = petStore.identifyLocal(bundleName); - if (bundleFormulaIdentifier === undefined) { + const bundleId = petStore.identifyLocal(bundleName); + if (bundleId === undefined) { throw new TypeError(`Unknown pet name for bundle: ${q(bundleName)}`); } - const { tasks, workerFormulaIdentifier, powersFormulaIdentifier } = - prepareMakeCaplet(powersName, workerName, resultName); + const { tasks, workerId, powersId } = prepareMakeCaplet( + powersName, + workerName, + resultName, + ); // Behold, recursion: // eslint-disable-next-line no-use-before-define const { value } = await incarnateBundle( - hostFormulaIdentifier, - bundleFormulaIdentifier, + hostId, + bundleId, tasks, - workerFormulaIdentifier, - powersFormulaIdentifier, + workerId, + powersId, ); return value; }; @@ -326,26 +328,21 @@ export const makeHostMaker = ({ * Attempts to introduce the given names to the specified agent. The agent in question * must be incarnated before this function is called. * - * @param {string} formulaIdentifier - The agent's formula identifier. + * @param {string} id - The agent's formula identifier. * @param {Record} introducedNames - The names to introduce. * @returns {Promise} */ - const introduceNamesToAgent = async ( - formulaIdentifier, - introducedNames, - ) => { + const introduceNamesToAgent = async (id, introducedNames) => { /** @type {import('./types.js').Controller} */ - const controller = - provideControllerForFormulaIdentifier(formulaIdentifier); + const controller = provideControllerForId(id); const { petStore: newPetStore } = await controller.internal; await Promise.all( Object.entries(introducedNames).map(async ([parentName, childName]) => { - const introducedFormulaIdentifier = - petStore.identifyLocal(parentName); - if (introducedFormulaIdentifier === undefined) { + const introducedId = petStore.identifyLocal(parentName); + if (introducedId === undefined) { return; } - await newPetStore.write(childName, introducedFormulaIdentifier); + await newPetStore.write(childName, introducedId); }), ); }; @@ -355,12 +352,12 @@ export const makeHostMaker = ({ */ const getNamedAgent = petName => { if (petName !== undefined) { - const formulaIdentifier = petStore.identifyLocal(petName); - if (formulaIdentifier !== undefined) { + const id = petStore.identifyLocal(petName); + if (id !== undefined) { return { - formulaIdentifier, + id, value: /** @type {Promise} */ ( - provideControllerForFormulaIdentifier(formulaIdentifier).external + provideControllerForId(id).external ), }; } @@ -375,9 +372,7 @@ export const makeHostMaker = ({ /** @type {import('./types.js').DeferredTasks} */ const tasks = makeDeferredTasks(); if (petName !== undefined) { - tasks.push(identifiers => - petStore.write(petName, identifiers.agentFormulaIdentifier), - ); + tasks.push(identifiers => petStore.write(petName, identifiers.agentId)); } return tasks; }; @@ -385,24 +380,24 @@ export const makeHostMaker = ({ /** * @param {string} [petName] * @param {import('./types.js').MakeHostOrGuestOptions} [opts] - * @returns {Promise<{formulaIdentifier: string, value: Promise}>} + * @returns {Promise<{id: string, value: Promise}>} */ const makeHost = async (petName, { introducedNames = {} } = {}) => { let host = getNamedAgent(petName); if (host === undefined) { - const { value, formulaIdentifier } = + const { value, id } = // Behold, recursion: await incarnateHost( - endoFormulaIdentifier, - networksDirectoryFormulaIdentifier, + endoId, + networksDirectoryId, getDeferredTasksForAgent(petName), ); - host = { value: Promise.resolve(value), formulaIdentifier }; + host = { value: Promise.resolve(value), id }; } - await introduceNamesToAgent(host.formulaIdentifier, introducedNames); + await introduceNamesToAgent(host.id, introducedNames); - /** @type {{ formulaIdentifier: string, value: Promise }} */ + /** @type {{ id: string, value: Promise }} */ return host; }; @@ -415,23 +410,20 @@ export const makeHostMaker = ({ /** * @param {string} [petName] * @param {import('./types.js').MakeHostOrGuestOptions} [opts] - * @returns {Promise<{formulaIdentifier: string, value: Promise}>} + * @returns {Promise<{id: string, value: Promise}>} */ const makeGuest = async (petName, { introducedNames = {} } = {}) => { let guest = getNamedAgent(petName); if (guest === undefined) { - const { value, formulaIdentifier } = + const { value, id } = // Behold, recursion: - await incarnateGuest( - hostFormulaIdentifier, - getDeferredTasksForAgent(petName), - ); - guest = { value: Promise.resolve(value), formulaIdentifier }; + await incarnateGuest(hostId, getDeferredTasksForAgent(petName)); + guest = { value: Promise.resolve(value), id }; } - await introduceNamesToAgent(guest.formulaIdentifier, introducedNames); + await introduceNamesToAgent(guest.id, introducedNames); - /** @type {{ formulaIdentifier: string, value: Promise }} */ + /** @type {{ id: string, value: Promise }} */ return guest; }; @@ -443,11 +435,11 @@ export const makeHostMaker = ({ /** @type {import('./types.js').EndoHost['cancel']} */ const cancel = async (petName, reason = new Error('Cancelled')) => { - const formulaIdentifier = petStore.identifyLocal(petName); - if (formulaIdentifier === undefined) { + const id = petStore.identifyLocal(petName); + if (id === undefined) { throw new TypeError(`Unknown pet name: ${q(petName)}`); } - return cancelValue(formulaIdentifier, reason); + return cancelValue(id, reason); }; /** @type {import('./types.js').EndoHost['gateway']} */ @@ -464,9 +456,7 @@ export const makeHostMaker = ({ /** @type {import('./types.js').EndoHost['getPeerInfo']} */ const getPeerInfo = async () => { - const addresses = await getAllNetworkAddresses( - networksDirectoryFormulaIdentifier, - ); + const addresses = await getAllNetworkAddresses(networksDirectoryId); const peerInfo = { node: ownNodeIdentifier, addresses, @@ -545,7 +535,7 @@ export const makeHostMaker = ({ }); const internal = harden({ receive, respond, petStore }); - await provide(mainWorkerFormulaIdentifier); + await provide(mainWorkerId); return harden({ external, internal }); }; diff --git a/packages/daemon/src/mail.js b/packages/daemon/src/mail.js index 0bbade95ed..554d0fb9e9 100644 --- a/packages/daemon/src/mail.js +++ b/packages/daemon/src/mail.js @@ -9,16 +9,16 @@ const { quote: q } = assert; /** * @param {object} args * @param {import('./types.js').DaemonCore['provide']} args.provide - * @param {import('./types.js').DaemonCore['provideControllerForFormulaIdentifierAndResolveHandle']} args.provideControllerForFormulaIdentifierAndResolveHandle + * @param {import('./types.js').DaemonCore['provideControllerForIdAndResolveHandle']} args.provideControllerForIdAndResolveHandle * @returns {import('./types.js').MakeMailbox} */ export const makeMailboxMaker = ({ provide, - provideControllerForFormulaIdentifierAndResolveHandle, + provideControllerForIdAndResolveHandle, }) => { /** @type {import('./types.js').MakeMailbox} */ - const makeMailbox = ({ selfFormulaIdentifier, petStore, context }) => { + const makeMailbox = ({ selfId, petStore, context }) => { /** @type {Map>} */ const responses = new Map(); /** @type {Map} */ @@ -38,29 +38,17 @@ export const makeMailboxMaker = ({ const dubMessage = message => { const { type } = message; if (type === 'request') { - const { - who: senderFormulaIdentifier, - dest: recipientFormulaIdentifier, - ...rest - } = message; - const [senderName] = petStore.reverseIdentify(senderFormulaIdentifier); - const [recipientName] = petStore.reverseIdentify( - recipientFormulaIdentifier, - ); + const { who: senderId, dest: recipientId, ...rest } = message; + const [senderName] = petStore.reverseIdentify(senderId); + const [recipientName] = petStore.reverseIdentify(recipientId); if (senderName !== undefined) { return { who: senderName, dest: recipientName, ...rest }; } return undefined; } else if (type === 'package') { - const { - who: senderFormulaIdentifier, - dest: recipientFormulaIdentifier, - ...rest - } = message; - const [senderName] = petStore.reverseIdentify(senderFormulaIdentifier); - const [recipientName] = petStore.reverseIdentify( - recipientFormulaIdentifier, - ); + const { who: senderId, dest: recipientId, ...rest } = message; + const [senderName] = petStore.reverseIdentify(senderId); + const [recipientName] = petStore.reverseIdentify(recipientId); if (senderName !== undefined) { return { who: senderName, dest: recipientName, ...rest }; } @@ -137,7 +125,7 @@ export const makeMailboxMaker = ({ * @param {string} dest * @returns {Promise} */ - const requestFormulaIdentifier = async (what, who, dest) => { + const requestId = async (what, who, dest) => { /** @type {import('@endo/promise-kit/src/types.js').PromiseKit} */ const { promise, resolve } = makePromiseKit(); const settled = promise.then( @@ -159,36 +147,28 @@ export const makeMailboxMaker = ({ const respond = async ( what, responseName, - senderFormulaIdentifier, + senderId, senderPetStore, - recipientFormulaIdentifier = selfFormulaIdentifier, + recipientId = selfId, ) => { if (responseName !== undefined) { /** @type {string | undefined} */ - let formulaIdentifier = senderPetStore.identifyLocal(responseName); - if (formulaIdentifier === undefined) { - formulaIdentifier = await requestFormulaIdentifier( - what, - senderFormulaIdentifier, - recipientFormulaIdentifier, - ); - await senderPetStore.write(responseName, formulaIdentifier); + let id = senderPetStore.identifyLocal(responseName); + if (id === undefined) { + id = await requestId(what, senderId, recipientId); + await senderPetStore.write(responseName, id); } // Behold, recursion: // eslint-disable-next-line no-use-before-define - return provide(formulaIdentifier); + return provide(id); } // The reference is not named nor to be named. - const formulaIdentifier = await requestFormulaIdentifier( - what, - senderFormulaIdentifier, - recipientFormulaIdentifier, - ); + const id = await requestId(what, senderId, recipientId); // TODO: - // context.thisDiesIfThatDies(formulaIdentifier); + // context.thisDiesIfThatDies(id); // Behold, recursion: // eslint-disable-next-line no-use-before-define - return provide(formulaIdentifier); + return provide(id); }; /** @type {import('./types.js').Mail['resolve']} */ @@ -205,13 +185,13 @@ export const makeMailboxMaker = ({ if (resolveRequest === undefined) { throw new Error(`No pending request for number ${messageNumber}`); } - const formulaIdentifier = petStore.identifyLocal(resolutionName); - if (formulaIdentifier === undefined) { + const id = petStore.identifyLocal(resolutionName); + if (id === undefined) { throw new TypeError( `No formula exists for the pet name ${q(resolutionName)}`, ); } - resolveRequest(formulaIdentifier); + resolveRequest(id); }; // TODO test reject @@ -229,32 +209,31 @@ export const makeMailboxMaker = ({ /** @type {import('./types.js').Mail['receive']} */ const receive = ( - senderFormulaIdentifier, + senderId, strings, edgeNames, - formulaIdentifiers, - receiverFormulaIdentifier = selfFormulaIdentifier, + ids, + receiverId = selfId, ) => { deliver({ type: /** @type {const} */ ('package'), strings, names: edgeNames, - formulas: formulaIdentifiers, - who: senderFormulaIdentifier, - dest: receiverFormulaIdentifier, + formulas: ids, + who: senderId, + dest: receiverId, }); }; /** @type {import('./types.js').Mail['send']} */ const send = async (recipientName, strings, edgeNames, petNames) => { - const recipientFormulaIdentifier = petStore.identifyLocal(recipientName); - if (recipientFormulaIdentifier === undefined) { + const recipientId = petStore.identifyLocal(recipientName); + if (recipientId === undefined) { throw new Error(`Unknown pet name for party: ${recipientName}`); } - const recipientController = - await provideControllerForFormulaIdentifierAndResolveHandle( - recipientFormulaIdentifier, - ); + const recipientController = await provideControllerForIdAndResolveHandle( + recipientId, + ); const recipientInternal = await recipientController.internal; if (recipientInternal === undefined || recipientInternal === null) { throw new Error(`Recipient cannot receive messages: ${recipientName}`); @@ -280,28 +259,23 @@ export const makeMailboxMaker = ({ ); } - const formulaIdentifiers = petNames.map(petName => { - const formulaIdentifier = petStore.identifyLocal(petName); - if (formulaIdentifier === undefined) { + const ids = petNames.map(petName => { + const id = petStore.identifyLocal(petName); + if (id === undefined) { throw new Error(`Unknown pet name ${q(petName)}`); } - return formulaIdentifier; + return id; }); // add to recipient mailbox - partyReceive( - selfFormulaIdentifier, - strings, - edgeNames, - formulaIdentifiers, - ); + partyReceive(selfId, strings, edgeNames, ids); // add to own mailbox receive( - selfFormulaIdentifier, + selfId, strings, edgeNames, - formulaIdentifiers, + ids, // Sender expects the handle formula identifier. - recipientFormulaIdentifier, + recipientId, ); }; @@ -344,28 +318,27 @@ export const makeMailboxMaker = ({ `No reference named ${q(edgeName)} in message ${q(messageNumber)}`, ); } - const formulaIdentifier = message.formulas[index]; - if (formulaIdentifier === undefined) { + const id = message.formulas[index]; + if (id === undefined) { throw new Error( `panic: message must contain a formula for every name, including the name ${q( edgeName, )} at ${q(index)}`, ); } - context.thisDiesIfThatDies(formulaIdentifier); - await petStore.write(petName, formulaIdentifier); + context.thisDiesIfThatDies(id); + await petStore.write(petName, id); }; /** @type {import('./types.js').Mail['request']} */ const request = async (recipientName, what, responseName) => { - const recipientFormulaIdentifier = petStore.identifyLocal(recipientName); - if (recipientFormulaIdentifier === undefined) { + const recipientId = petStore.identifyLocal(recipientName); + if (recipientId === undefined) { throw new Error(`Unknown pet name for party: ${recipientName}`); } - const recipientController = - await provideControllerForFormulaIdentifierAndResolveHandle( - recipientFormulaIdentifier, - ); + const recipientController = await provideControllerForIdAndResolveHandle( + recipientId, + ); const recipientInternal = await recipientController.internal; if (recipientInternal === undefined || recipientInternal === null) { throw new Error( @@ -394,17 +367,17 @@ export const makeMailboxMaker = ({ const recipientResponseP = deliverToRecipient( what, responseName, - selfFormulaIdentifier, + selfId, petStore, ); // Send to own inbox. const selfResponseP = respond( what, responseName, - selfFormulaIdentifier, + selfId, petStore, // Sender expects the handle formula identifier. - recipientFormulaIdentifier, + recipientId, ); const newResponseP = Promise.race([recipientResponseP, selfResponseP]); @@ -418,9 +391,9 @@ export const makeMailboxMaker = ({ /** @type {import('./types.js').PetStore['rename']} */ const rename = async (fromName, toName) => { await petStore.rename(fromName, toName); - const formulaIdentifier = responses.get(fromName); - if (formulaIdentifier !== undefined) { - responses.set(toName, formulaIdentifier); + const id = responses.get(fromName); + if (id !== undefined) { + responses.set(toName, id); responses.delete(fromName); } }; diff --git a/packages/daemon/src/pet-sitter.js b/packages/daemon/src/pet-sitter.js index a79beb4768..c5e93e58b6 100644 --- a/packages/daemon/src/pet-sitter.js +++ b/packages/daemon/src/pet-sitter.js @@ -25,14 +25,14 @@ export const makePetSitter = (petStore, specialNames) => { /** * @param {string} petName - * @returns {import('./types.js').FormulaIdentifierRecord} + * @returns {import('./types.js').IdRecord} */ - const formulaIdentifierRecordForName = petName => { - const formulaIdentifier = identifyLocal(petName); - if (formulaIdentifier === undefined) { + const idRecordForName = petName => { + const id = identifyLocal(petName); + if (id === undefined) { throw new Error(`Formula does not exist for pet name ${q(petName)}`); } - return parseId(formulaIdentifier); + return parseId(id); }; /** @type {import('./types.js').PetStore['list']} */ @@ -42,22 +42,20 @@ export const makePetSitter = (petStore, specialNames) => { /** @type {import('./types.js').PetStore['follow']} */ const follow = async function* currentAndSubsequentNames() { for (const name of Object.keys(specialNames).sort()) { - const formulaIdentifierRecord = formulaIdentifierRecordForName(name); - yield /** @type {{ add: string, value: import('./types.js').FormulaIdentifierRecord }} */ ({ + const idRecord = idRecordForName(name); + yield /** @type {{ add: string, value: import('./types.js').IdRecord }} */ ({ add: name, - value: formulaIdentifierRecord, + value: idRecord, }); } yield* petStore.follow(); }; /** @type {import('./types.js').PetStore['reverseIdentify']} */ - const reverseIdentify = formulaIdentifier => { - const names = Array.from(petStore.reverseIdentify(formulaIdentifier)); - for (const [specialName, specialFormulaIdentifier] of Object.entries( - specialNames, - )) { - if (specialFormulaIdentifier === formulaIdentifier) { + const reverseIdentify = id => { + const names = Array.from(petStore.reverseIdentify(id)); + for (const [specialName, specialId] of Object.entries(specialNames)) { + if (specialId === id) { names.push(specialName); } } diff --git a/packages/daemon/src/pet-store.js b/packages/daemon/src/pet-store.js index db1e22a011..f553dba089 100644 --- a/packages/daemon/src/pet-store.js +++ b/packages/daemon/src/pet-store.js @@ -21,8 +21,8 @@ export const makePetStoreMaker = (filePowers, locator) => { /** @type {Map} */ const petNames = new Map(); /** @type {Map>} */ - const formulaIdentifiers = new Map(); - /** @type {import('./types.js').Topic<({ add: string, value: import('./types.js').FormulaIdentifierRecord } | { remove: string })>} */ + const ids = new Map(); + /** @type {import('./types.js').Topic<({ add: string, value: import('./types.js').IdRecord } | { remove: string })>} */ const changesTopic = makeChangeTopic(); /** @param {string} petName */ @@ -42,11 +42,11 @@ export const makePetStoreMaker = (filePowers, locator) => { assertValidName(petName); const formulaIdentifier = await read(petName); petNames.set(petName, formulaIdentifier); - const formulaPetNames = formulaIdentifiers.get(formulaIdentifier); + const formulaPetNames = ids.get(formulaIdentifier); if (formulaPetNames !== undefined) { formulaPetNames.add(petName); } else { - formulaIdentifiers.set(formulaIdentifier, new Set([petName])); + ids.set(formulaIdentifier, new Set([petName])); } }), ); @@ -71,7 +71,7 @@ export const makePetStoreMaker = (filePowers, locator) => { // TODO: Return early if the formula identifier is the same. if (petNames.has(petName)) { // Perform cleanup on the overwritten pet name. - const formulaPetNames = formulaIdentifiers.get(petName); + const formulaPetNames = ids.get(petName); if (formulaPetNames !== undefined) { formulaPetNames.delete(petName); } @@ -81,9 +81,9 @@ export const makePetStoreMaker = (filePowers, locator) => { petNames.set(petName, formulaIdentifier); - const formulaPetNames = formulaIdentifiers.get(formulaIdentifier); + const formulaPetNames = ids.get(formulaIdentifier); if (formulaPetNames === undefined) { - formulaIdentifiers.set(formulaIdentifier, new Set([petName])); + ids.set(formulaIdentifier, new Set([petName])); } else { formulaPetNames.add(petName); } @@ -100,7 +100,7 @@ export const makePetStoreMaker = (filePowers, locator) => { /** * @param {string} petName - * @returns {import('./types.js').FormulaIdentifierRecord} + * @returns {import('./types.js').IdRecord} */ const formulaIdentifierRecordForName = petName => { const formulaIdentifier = petNames.get(petName); @@ -119,7 +119,7 @@ export const makePetStoreMaker = (filePowers, locator) => { const changes = changesTopic.subscribe(); for (const name of [...petNames.keys()].sort()) { const formulaIdentifierRecord = formulaIdentifierRecordForName(name); - yield /** @type {{ add: string, value: import('./types.js').FormulaIdentifierRecord }} */ ({ + yield /** @type {{ add: string, value: import('./types.js').IdRecord }} */ ({ add: name, value: formulaIdentifierRecord, }); @@ -141,7 +141,7 @@ export const makePetStoreMaker = (filePowers, locator) => { const petNamePath = filePowers.joinPath(petNameDirectoryPath, petName); await filePowers.removePath(petNamePath); petNames.delete(petName); - const formulaPetNames = formulaIdentifiers.get(petName); + const formulaPetNames = ids.get(petName); if (formulaPetNames !== undefined) { formulaPetNames.delete(petName); } @@ -158,15 +158,15 @@ export const makePetStoreMaker = (filePowers, locator) => { return; } const formulaIdentifier = petNames.get(fromName); - const overwrittenFormulaIdentifier = petNames.get(toName); + const overwrittenId = petNames.get(toName); if (formulaIdentifier === undefined) { throw new Error( `Formula does not exist for pet name ${JSON.stringify(fromName)}`, ); } assertValidId(formulaIdentifier, fromName); - if (overwrittenFormulaIdentifier !== undefined) { - assertValidId(overwrittenFormulaIdentifier, toName); + if (overwrittenId !== undefined) { + assertValidId(overwrittenId, toName); } const fromPath = filePowers.joinPath(petNameDirectoryPath, fromName); @@ -176,17 +176,15 @@ export const makePetStoreMaker = (filePowers, locator) => { petNames.delete(fromName); // Delete the back-reference for the overwritten pet name if it existed. - if (overwrittenFormulaIdentifier !== undefined) { - const overwrittenFormulaPetNames = formulaIdentifiers.get( - overwrittenFormulaIdentifier, - ); + if (overwrittenId !== undefined) { + const overwrittenFormulaPetNames = ids.get(overwrittenId); if (overwrittenFormulaPetNames !== undefined) { overwrittenFormulaPetNames.delete(toName); } } // Change the back-reference for the old pet name. - const formulaPetNames = formulaIdentifiers.get(formulaIdentifier); + const formulaPetNames = ids.get(formulaIdentifier); if (formulaPetNames !== undefined) { formulaPetNames.delete(fromName); formulaPetNames.add(toName); @@ -204,7 +202,7 @@ export const makePetStoreMaker = (filePowers, locator) => { /** @type {import('./types.js').PetStore['reverseIdentify']} */ const reverseIdentify = formulaIdentifier => { assertValidId(formulaIdentifier); - const formulaPetNames = formulaIdentifiers.get(formulaIdentifier); + const formulaPetNames = ids.get(formulaIdentifier); if (formulaPetNames === undefined) { return harden([]); } diff --git a/packages/daemon/src/types.d.ts b/packages/daemon/src/types.d.ts index b872078d4d..a074ecd3ea 100644 --- a/packages/daemon/src/types.d.ts +++ b/packages/daemon/src/types.d.ts @@ -51,7 +51,7 @@ export type MignonicPowers = { }; }; -type FormulaIdentifierRecord = { +type IdRecord = { number: string; node: string; }; @@ -73,14 +73,14 @@ type WorkerFormula = { }; export type WorkerDeferredTaskParams = { - workerFormulaIdentifier: string; + workerId: string; }; /** * Deferred tasks parameters for `host` and `guest` formulas. */ export type AgentDeferredTaskParams = { - agentFormulaIdentifier: string; + agentId: string; }; type HostFormula = { @@ -113,9 +113,9 @@ type EvalFormula = { }; export type EvalDeferredTaskParams = { - endowmentFormulaIdentifiers: string[]; - evalFormulaIdentifier: string; - workerFormulaIdentifier: string; + endowmentIds: string[]; + evalId: string; + workerId: string; }; type ReadableBlobFormula = { @@ -124,7 +124,7 @@ type ReadableBlobFormula = { }; export type ReadableBlobDeferredTaskParams = { - readableBlobFormulaIdentifier: string; + readableBlobId: string; }; type LookupFormula = { @@ -159,9 +159,9 @@ type MakeBundleFormula = { }; export type MakeCapletDeferredTaskParams = { - capletFormulaIdentifier: string; - powersFormulaIdentifier: string; - workerFormulaIdentifier: string; + capletId: string; + powersId: string; + workerId: string; }; type PeerFormula = { @@ -302,16 +302,16 @@ export interface Context { disposed: Promise; /** - * @param formulaIdentifier - The formula identifier of the value whose + * @param id - The formula identifier of the value whose * cancellation should cause this value to be cancelled. */ - thisDiesIfThatDies: (formulaIdentifier: string) => void; + thisDiesIfThatDies: (id: string) => void; /** - * @param formulaIdentifier - The formula identifier of the value that should + * @param id - The formula identifier of the value that should * be cancelled if this value is cancelled. */ - thatDiesIfThisDies: (formulaIdentifier: string) => void; + thatDiesIfThisDies: (id: string) => void; /** * @param hook - A hook to run when the value is cancelled. @@ -357,13 +357,13 @@ export interface ExternalHandle {} * handle points to. This should not be exposed outside of the endo daemon. */ export interface InternalHandle { - targetFormulaIdentifier: string; + targetId: string; } export type MakeSha512 = () => Sha512; export type PetStoreNameDiff = - | { add: string; value: FormulaIdentifierRecord } + | { add: string; value: IdRecord } | { remove: string }; export interface PetStore { @@ -371,14 +371,14 @@ export interface PetStore { identifyLocal(petName: string): string | undefined; list(): Array; follow(): AsyncGenerator; - write(petName: string, formulaIdentifier: string): Promise; + write(petName: string, id: string): Promise; remove(petName: string): Promise; rename(fromPetName: string, toPetName: string): Promise; /** - * @param formulaIdentifier The formula identifier to look up. + * @param id The formula identifier to look up. * @returns The formula identifier for the given pet name, or `undefined` if the pet name is not found. */ - reverseIdentify(formulaIdentifier: string): Array; + reverseIdentify(id: string): Array; } export interface NameHub { @@ -391,7 +391,7 @@ export interface NameHub { ): AsyncGenerator; lookup(...petNamePath: string[]): Promise; reverseLookup(value: unknown): Array; - write(petNamePath: string[], formulaIdentifier): Promise; + write(petNamePath: string[], id): Promise; remove(...petNamePath: string[]): Promise; move(fromPetName: string[], toPetName: string[]): Promise; copy(fromPetName: string[], toPetName: string[]): Promise; @@ -431,21 +431,21 @@ export interface Mail { respond( what: string, responseName: string, - senderFormulaIdentifier: string, + senderId: string, senderPetStore: PetStore, - recipientFormulaIdentifier?: string, + recipientId?: string, ): Promise; receive( - senderFormulaIdentifier: string, + senderId: string, strings: Array, edgeNames: Array, - formulaIdentifiers: Array, - receiverFormulaIdentifier: string, + ids: Array, + receiverId: string, ): void; } export type MakeMailbox = (args: { - selfFormulaIdentifier: string; + selfId: string; petStore: PetStore; context: Context; }) => Mail; @@ -453,15 +453,15 @@ export type MakeMailbox = (args: { export type RequestFn = ( what: string, responseName: string, - guestFormulaIdentifier: string, + guestId: string, guestPetStore: PetStore, ) => Promise; export type ReceiveFn = ( - senderFormulaIdentifier: string, + senderId: string, strings: Array, edgeNames: Array, - formulaIdentifiers: Array, + ids: Array, ) => void; export interface EndoReadable { @@ -482,13 +482,13 @@ export type MakeHostOrGuestOptions = { }; export interface EndoPeer { - provide: (formulaIdentifier: string) => Promise; + provide: (id: string) => Promise; } export type EndoPeerControllerPartial = ControllerPartial; export type EndoPeerController = Controller; export interface EndoGateway { - provide: (formulaIdentifier: string) => Promise; + provide: (id: string) => Promise; } export interface PeerInfo { @@ -699,12 +699,12 @@ export type DaemonicPowers = { }; type IncarnateResult = Promise<{ - formulaIdentifier: string; + id: string; value: T; }>; export type DeferredTask> = ( - formulaIdentifiers: Readonly, + ids: Readonly, ) => Promise; /** @@ -718,34 +718,34 @@ export type DeferredTasks> = { type IncarnateNumberedGuestParams = { guestFormulaNumber: string; - hostHandleFormulaIdentifier: string; - storeFormulaIdentifier: string; - workerFormulaIdentifier: string; + hostHandleId: string; + storeId: string; + workerId: string; }; type IncarnateHostDependenciesParams = { - endoFormulaIdentifier: string; - networksDirectoryFormulaIdentifier: string; - specifiedWorkerFormulaIdentifier?: string; + endoId: string; + networksDirectoryId: string; + specifiedWorkerId?: string; }; type IncarnateNumberedHostParams = { hostFormulaNumber: string; - workerFormulaIdentifier: string; - storeFormulaIdentifier: string; - inspectorFormulaIdentifier: string; - endoFormulaIdentifier: string; - networksDirectoryFormulaIdentifier: string; + workerId: string; + storeId: string; + inspectorId: string; + endoId: string; + networksDirectoryId: string; }; export interface DaemonCoreInternal { /** * Helper for callers of {@link incarnateNumberedGuest}. - * @param hostFormulaIdentifier - The formula identifier of the host to incarnate a guest for. + * @param hostId - The formula identifier of the host to incarnate a guest for. * @returns The formula identifiers for the guest incarnation's dependencies. */ incarnateGuestDependencies: ( - hostFormulaIdentifier: string, + hostId: string, ) => Promise>; incarnateNumberedGuest: ( identifiers: IncarnateNumberedGuestParams, @@ -765,24 +765,18 @@ export interface DaemonCoreInternal { export interface DaemonCore { nodeIdentifier: string; - provide: (formulaIdentifier: string) => Promise; - provideControllerForFormulaIdentifier: ( - formulaIdentifier: string, - ) => Controller; - provideControllerForFormulaIdentifierAndResolveHandle: ( - formulaIdentifier: string, - ) => Promise; + provide: (id: string) => Promise; + provideControllerForId: (id: string) => Controller; + provideControllerForIdAndResolveHandle: (id: string) => Promise; formulate: ( formulaNumber: string, formula: Formula, ) => Promise<{ - formulaIdentifier: string; + id: string; value: unknown; }>; - getFormulaIdentifierForRef: (ref: unknown) => string | undefined; - getAllNetworkAddresses: ( - networksDirectoryFormulaIdentifier: string, - ) => Promise; + getIdForRef: (ref: unknown) => string | undefined; + getAllNetworkAddresses: (networksDirectoryId: string) => Promise; incarnateEndoBootstrap: ( specifiedFormulaNumber: string, ) => IncarnateResult; @@ -791,13 +785,13 @@ export interface DaemonCore { ) => IncarnateResult; incarnateDirectory: () => IncarnateResult; incarnateHost: ( - endoFormulaIdentifier: string, - networksDirectoryFormulaIdentifier: string, + endoId: string, + networksDirectoryId: string, deferredTasks: DeferredTasks, - specifiedWorkerFormulaIdentifier?: string | undefined, + specifiedWorkerId?: string | undefined, ) => IncarnateResult; incarnateGuest: ( - hostFormulaIdentifier: string, + hostId: string, deferredTasks: DeferredTasks, ) => IncarnateResult; incarnateReadableBlob: ( @@ -805,34 +799,34 @@ export interface DaemonCore { deferredTasks: DeferredTasks, ) => IncarnateResult; incarnateEval: ( - hostFormulaIdentifier: string, + hostId: string, source: string, codeNames: string[], - endowmentFormulaIdsOrPaths: (string | string[])[], + endowmentIdsOrPaths: (string | string[])[], deferredTasks: DeferredTasks, - specifiedWorkerFormulaIdentifier?: string, + specifiedWorkerId?: string, ) => IncarnateResult; incarnateUnconfined: ( - hostFormulaIdentifier: string, + hostId: string, specifier: string, deferredTasks: DeferredTasks, - specifiedWorkerFormulaIdentifier?: string, - specifiedPowersFormulaIdentifier?: string, + specifiedWorkerId?: string, + specifiedPowersId?: string, ) => IncarnateResult; incarnateBundle: ( - hostFormulaIdentifier: string, - bundleFormulaIdentifier: string, + hostId: string, + bundleId: string, deferredTasks: DeferredTasks, - specifiedWorkerFormulaIdentifier?: string, - specifiedPowersFormulaIdentifier?: string, + specifiedWorkerId?: string, + specifiedPowersId?: string, ) => IncarnateResult; incarnatePeer: ( - networksFormulaIdentifier: string, + networksId: string, addresses: Array, ) => IncarnateResult; incarnateNetworksDirectory: () => IncarnateResult; incarnateLoopbackNetwork: () => IncarnateResult; - cancelValue: (formulaIdentifier: string, reason: Error) => Promise; + cancelValue: (id: string, reason: Error) => Promise; makeMailbox: MakeMailbox; makeDirectoryNode: MakeDirectoryNode; } diff --git a/packages/daemon/src/weak-multimap.js b/packages/daemon/src/weak-multimap.js index 5051a03685..568e8e7dbc 100644 --- a/packages/daemon/src/weak-multimap.js +++ b/packages/daemon/src/weak-multimap.js @@ -5,31 +5,31 @@ export const makeWeakMultimap = () => { /** @type {WeakMap>} */ const map = new WeakMap(); return { - add: (ref, formulaIdentifier) => { - let set = map.get(ref); + add: (key, value) => { + let set = map.get(key); if (set === undefined) { set = new Set(); - map.set(ref, set); + map.set(key, set); } - set.add(formulaIdentifier); + set.add(value); }, - delete: (ref, formulaIdentifier) => { - const set = map.get(ref); + delete: (key, value) => { + const set = map.get(key); if (set !== undefined) { - const result = set.delete(formulaIdentifier); + const result = set.delete(value); if (set.size === 0) { - map.delete(ref); + map.delete(key); } return result; } return false; }, - deleteAll: ref => map.delete(ref), + deleteAll: key => map.delete(key), - get: ref => map.get(ref)?.keys().next().value, + get: key => map.get(key)?.keys().next().value, - getAll: ref => Array.from(map.get(ref) ?? []), + getAll: key => Array.from(map.get(key) ?? []), }; }; diff --git a/packages/daemon/test/test-endo.js b/packages/daemon/test/test-endo.js index 0495e40410..ffafba1384 100644 --- a/packages/daemon/test/test-endo.js +++ b/packages/daemon/test/test-endo.js @@ -1328,7 +1328,7 @@ test('guest cannot access host methods', async t => { await t.throwsAsync(() => E(guestsHost).lookup('SELF'), { message: /target has no method "lookup"/u, }); - const revealedTarget = await E.get(guestsHost).targetFormulaIdentifier; + const revealedTarget = await E.get(guestsHost).targetId; t.is(revealedTarget, undefined); }); @@ -1352,11 +1352,11 @@ test('read unknown nodeId', async t => { // write a bogus value for a bogus nodeId const node = await cryptoPowers.randomHex512(); const number = await cryptoPowers.randomHex512(); - const formulaIdentifier = formatId({ + const id = formatId({ node, number, }); - await E(host).write(['abc'], formulaIdentifier); + await E(host).write(['abc'], id); // observe reification failure t.throwsAsync(() => E(host).lookup('abc'), { message: /No peer found for node identifier /u, From eb305bb7b071e6bc69500aa0553198d297e5134c Mon Sep 17 00:00:00 2001 From: Kris Kowal Date: Sat, 16 Mar 2024 07:59:33 -0700 Subject: [PATCH 2/2] refactor(daemon): provideController abbreviation --- packages/daemon/src/context.js | 12 +++++------- packages/daemon/src/daemon.js | 32 ++++++++++++++++---------------- packages/daemon/src/guest.js | 6 +++--- packages/daemon/src/host.js | 10 ++++------ packages/daemon/src/mail.js | 8 ++++---- packages/daemon/src/types.d.ts | 4 ++-- 6 files changed, 34 insertions(+), 38 deletions(-) diff --git a/packages/daemon/src/context.js b/packages/daemon/src/context.js index 13e085f332..85fe1a3e4d 100644 --- a/packages/daemon/src/context.js +++ b/packages/daemon/src/context.js @@ -2,10 +2,7 @@ import { makePromiseKit } from '@endo/promise-kit'; -export const makeContextMaker = ({ - controllerForId, - provideControllerForId, -}) => { +export const makeContextMaker = ({ controllerForId, provideController }) => { /** * @param {string} id */ @@ -26,7 +23,8 @@ export const makeContextMaker = ({ const hooks = []; /** - * @param {unknown} reason + * @param {Error} reason + * @param {string} [prefix] */ const cancel = (reason, prefix = '*') => { if (done) return disposed; @@ -52,7 +50,7 @@ export const makeContextMaker = ({ */ const thatDiesIfThisDies = dependentId => { assert(!done); - const dependentController = provideControllerForId(dependentId); + const dependentController = provideController(dependentId); dependents.set(dependentId, dependentController.context); }; @@ -60,7 +58,7 @@ export const makeContextMaker = ({ * @param {string} dependencyId */ const thisDiesIfThatDies = dependencyId => { - const dependencyController = provideControllerForId(dependencyId); + const dependencyController = provideController(dependencyId); dependencyController.context.thatDiesIfThisDies(id); }; diff --git a/packages/daemon/src/daemon.js b/packages/daemon/src/daemon.js index 6ebcade973..474a1c4180 100644 --- a/packages/daemon/src/daemon.js +++ b/packages/daemon/src/daemon.js @@ -302,7 +302,7 @@ const makeDaemonCore = async ( /** @type {import('./types.js').Controller} */ ( // Behold, recursion: // eslint-disable-next-line no-use-before-define - provideControllerForId(workerId) + provideController(workerId) ); const workerDaemonFacet = workerController.internal; assert( @@ -375,7 +375,7 @@ const makeDaemonCore = async ( /** @type {import('./types.js').Controller} */ ( // Behold, recursion: // eslint-disable-next-line no-use-before-define - provideControllerForId(workerId) + provideController(workerId) ); const workerDaemonFacet = workerController.internal; assert( @@ -415,7 +415,7 @@ const makeDaemonCore = async ( /** @type {import('./types.js').Controller} */ ( // Behold, recursion: // eslint-disable-next-line no-use-before-define - provideControllerForId(workerId) + provideController(workerId) ); const workerDaemonFacet = workerController.internal; assert( @@ -752,8 +752,8 @@ const makeDaemonCore = async ( }); }; - /** @type {import('./types.js').DaemonCore['provideControllerForId']} */ - const provideControllerForId = id => { + /** @type {import('./types.js').DaemonCore['provideController']} */ + const provideController = id => { let controller = controllerForId.get(id); if (controller !== undefined) { return controller; @@ -810,7 +810,7 @@ const makeDaemonCore = async ( /** @type {import('./types.js').DaemonCore['cancelValue']} */ const cancelValue = async (id, reason) => { await formulaGraphJobs.enqueue(); - const controller = provideControllerForId(id); + const controller = provideController(id); console.log('Cancelled:'); return controller.context.cancel(reason); }; @@ -818,7 +818,7 @@ const makeDaemonCore = async ( /** @type {import('./types.js').DaemonCore['provide']} */ const provide = id => { const controller = /** @type {import('./types.js').Controller<>} */ ( - provideControllerForId(id) + provideController(id) ); return controller.external.then(value => { // Release the value to the public only after ensuring @@ -830,12 +830,12 @@ const makeDaemonCore = async ( }); }; - /** @type {import('./types.js').DaemonCore['provideControllerForIdAndResolveHandle']} */ - const provideControllerForIdAndResolveHandle = async id => { + /** @type {import('./types.js').DaemonCore['provideControllerAndResolveHandle']} */ + const provideControllerAndResolveHandle = async id => { let currentId = id; // eslint-disable-next-line no-constant-condition while (true) { - const controller = provideControllerForId(currentId); + const controller = provideController(currentId); // eslint-disable-next-line no-await-in-loop const internalFacet = await controller.internal; if (internalFacet === undefined || internalFacet === null) { @@ -1477,7 +1477,7 @@ const makeDaemonCore = async ( const makeContext = makeContextMaker({ controllerForId, - provideControllerForId, + provideController, }); const { makeIdentifiedDirectory, makeDirectoryNode } = makeDirectoryMaker({ @@ -1488,19 +1488,19 @@ const makeDaemonCore = async ( const makeMailbox = makeMailboxMaker({ provide, - provideControllerForIdAndResolveHandle, + provideControllerAndResolveHandle, }); const makeIdentifiedGuestController = makeGuestMaker({ provide, - provideControllerForIdAndResolveHandle, + provideControllerAndResolveHandle, makeMailbox, makeDirectoryNode, }); const makeIdentifiedHost = makeHostMaker({ provide, - provideControllerForId, + provideController, cancelValue, incarnateWorker, incarnateHost, @@ -1627,8 +1627,8 @@ const makeDaemonCore = async ( /** @type {import('./types.js').DaemonCore} */ const daemonCore = { nodeIdentifier: ownNodeIdentifier, - provideControllerForId, - provideControllerForIdAndResolveHandle, + provideController, + provideControllerAndResolveHandle, provide, formulate, getIdForRef, diff --git a/packages/daemon/src/guest.js b/packages/daemon/src/guest.js index 92d16484c2..3f22c017e3 100644 --- a/packages/daemon/src/guest.js +++ b/packages/daemon/src/guest.js @@ -7,13 +7,13 @@ import { makePetSitter } from './pet-sitter.js'; /** * @param {object} args * @param {import('./types.js').DaemonCore['provide']} args.provide - * @param {import('./types.js').DaemonCore['provideControllerForIdAndResolveHandle']} args.provideControllerForIdAndResolveHandle + * @param {import('./types.js').DaemonCore['provideControllerAndResolveHandle']} args.provideControllerAndResolveHandle * @param {import('./types.js').MakeMailbox} args.makeMailbox * @param {import('./types.js').MakeDirectoryNode} args.makeDirectoryNode */ export const makeGuestMaker = ({ provide, - provideControllerForIdAndResolveHandle, + provideControllerAndResolveHandle, makeMailbox, makeDirectoryNode, }) => { @@ -44,7 +44,7 @@ export const makeGuestMaker = ({ }); const hostController = /** @type {import('./types.js').EndoHostController} */ - (await provideControllerForIdAndResolveHandle(hostHandleId)); + (await provideControllerAndResolveHandle(hostHandleId)); const hostPrivateFacet = await hostController.internal; const { respond: deliverToHost } = hostPrivateFacet; if (deliverToHost === undefined) { diff --git a/packages/daemon/src/host.js b/packages/daemon/src/host.js index 53f1cfc59f..e5f2e11333 100644 --- a/packages/daemon/src/host.js +++ b/packages/daemon/src/host.js @@ -16,7 +16,7 @@ const assertPowersName = name => { /** * @param {object} args * @param {import('./types.js').DaemonCore['provide']} args.provide - * @param {import('./types.js').DaemonCore['provideControllerForId']} args.provideControllerForId + * @param {import('./types.js').DaemonCore['provideController']} args.provideController * @param {import('./types.js').DaemonCore['cancelValue']} args.cancelValue * @param {import('./types.js').DaemonCore['incarnateWorker']} args.incarnateWorker * @param {import('./types.js').DaemonCore['incarnateHost']} args.incarnateHost @@ -32,7 +32,7 @@ const assertPowersName = name => { */ export const makeHostMaker = ({ provide, - provideControllerForId, + provideController, cancelValue, incarnateWorker, incarnateHost, @@ -334,7 +334,7 @@ export const makeHostMaker = ({ */ const introduceNamesToAgent = async (id, introducedNames) => { /** @type {import('./types.js').Controller} */ - const controller = provideControllerForId(id); + const controller = provideController(id); const { petStore: newPetStore } = await controller.internal; await Promise.all( Object.entries(introducedNames).map(async ([parentName, childName]) => { @@ -356,9 +356,7 @@ export const makeHostMaker = ({ if (id !== undefined) { return { id, - value: /** @type {Promise} */ ( - provideControllerForId(id).external - ), + value: /** @type {Promise} */ (provideController(id).external), }; } } diff --git a/packages/daemon/src/mail.js b/packages/daemon/src/mail.js index 554d0fb9e9..e200403702 100644 --- a/packages/daemon/src/mail.js +++ b/packages/daemon/src/mail.js @@ -9,12 +9,12 @@ const { quote: q } = assert; /** * @param {object} args * @param {import('./types.js').DaemonCore['provide']} args.provide - * @param {import('./types.js').DaemonCore['provideControllerForIdAndResolveHandle']} args.provideControllerForIdAndResolveHandle + * @param {import('./types.js').DaemonCore['provideControllerAndResolveHandle']} args.provideControllerAndResolveHandle * @returns {import('./types.js').MakeMailbox} */ export const makeMailboxMaker = ({ provide, - provideControllerForIdAndResolveHandle, + provideControllerAndResolveHandle, }) => { /** @type {import('./types.js').MakeMailbox} */ @@ -231,7 +231,7 @@ export const makeMailboxMaker = ({ if (recipientId === undefined) { throw new Error(`Unknown pet name for party: ${recipientName}`); } - const recipientController = await provideControllerForIdAndResolveHandle( + const recipientController = await provideControllerAndResolveHandle( recipientId, ); const recipientInternal = await recipientController.internal; @@ -336,7 +336,7 @@ export const makeMailboxMaker = ({ if (recipientId === undefined) { throw new Error(`Unknown pet name for party: ${recipientName}`); } - const recipientController = await provideControllerForIdAndResolveHandle( + const recipientController = await provideControllerAndResolveHandle( recipientId, ); const recipientInternal = await recipientController.internal; diff --git a/packages/daemon/src/types.d.ts b/packages/daemon/src/types.d.ts index a074ecd3ea..9f26fcdda7 100644 --- a/packages/daemon/src/types.d.ts +++ b/packages/daemon/src/types.d.ts @@ -766,8 +766,8 @@ export interface DaemonCoreInternal { export interface DaemonCore { nodeIdentifier: string; provide: (id: string) => Promise; - provideControllerForId: (id: string) => Controller; - provideControllerForIdAndResolveHandle: (id: string) => Promise; + provideController: (id: string) => Controller; + provideControllerAndResolveHandle: (id: string) => Promise; formulate: ( formulaNumber: string, formula: Formula,