Skip to content

Commit

Permalink
Merge pull request #8712 from Agoric/gibson-2024-01-cleanup-corepropo…
Browse files Browse the repository at this point in the history
…salbehavior

chore(deploy-script-support): Sync code patterns across files
  • Loading branch information
mergify[bot] authored and mhofman committed Jan 19, 2024
2 parents 180be0e + 59a4a8f commit 23dc128
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 54 deletions.
35 changes: 19 additions & 16 deletions packages/deploy-script-support/src/coreProposalBehavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ export const permits = {
* @param {object} opts
* @param {import('./externalTypes.js').ManifestBundleRef} opts.manifestBundleRef
* @param {[methodName: string, ...args: unknown[]]} opts.getManifestCall
* @param {Record<string, Record<string, unknown>>} [opts.overrideManifest]
* @param {Record<string, Record<string, unknown>>} [opts.customManifest]
* @param {typeof import('@endo/far').E} opts.E
* @param {(...args: unknown[]) => void} [opts.log]
* @param {(ref: import('./externalTypes.js').ManifestBundleRef) => Promise<import('@agoric/zoe/src/zoeService/utils.js').Installation<unknown>>} [opts.restoreRef]
* @param {(ref: import('./externalTypes.js').ManifestBundleRef) => Promise<import('@agoric/zoe/src/zoeService/utils.js').Installation<unknown>>} [opts.customRestoreRef]
* @returns {(vatPowers: unknown) => Promise<unknown>}
*/
export const makeCoreProposalBehavior = ({
manifestBundleRef,
getManifestCall: [manifestGetterName, ...manifestGetterArgs],
overrideManifest,
customManifest,
E,
log = console.info,
restoreRef: overrideRestoreRef,
customRestoreRef,
}) => {
const { entries, fromEntries } = Object;

Expand Down Expand Up @@ -127,7 +127,7 @@ export const makeCoreProposalBehavior = ({
manifestGetterName,
bundleExports: Object.keys(proposalNS),
});
const restoreRef = overrideRestoreRef || makeRestoreRef(vatAdminSvc, zoe);
const restoreRef = customRestoreRef || makeRestoreRef(vatAdminSvc, zoe);
const {
manifest,
options: rawOptions,
Expand All @@ -143,20 +143,23 @@ export const makeCoreProposalBehavior = ({
);

// Publish the installations for our dependencies.
const installAdmin = E(agoricNamesAdmin).lookupAdmin('installation');
await Promise.all(
entries(installations || {}).map(([key, value]) => {
produceInstallations[key].resolve(value);
return E(installAdmin).update(key, value);
}),
);
const installationEntries = entries(installations || {});
if (installationEntries.length > 0) {
const installAdmin = E(agoricNamesAdmin).lookupAdmin('installation');
await Promise.all(
installationEntries.map(([key, value]) => {
produceInstallations[key].resolve(value);
return E(installAdmin).update(key, value);
}),
);
}

// Evaluate the manifest.
return runModuleBehaviors({
// Remember that `powers` may be arbitrarily broad.
allPowers: powers,
behaviors: proposalNS,
manifest: overrideManifest || manifest,
manifest: customManifest || manifest,
makeConfig: (name, _permit) => {
log('coreProposal:', name);
return { options };
Expand All @@ -168,14 +171,14 @@ export const makeCoreProposalBehavior = ({
};

export const makeEnactCoreProposalsFromBundleRef =
({ makeCoreProposalArgs, E }) =>
({ metadataRecords, E }) =>
async powers => {
await Promise.all(
makeCoreProposalArgs.map(async ({ ref, call, overrideManifest }) => {
metadataRecords.map(async ({ ref, call, customManifest }) => {
const coreProposalBehavior = makeCoreProposalBehavior({
manifestBundleRef: ref,
getManifestCall: call,
overrideManifest,
customManifest,
E,
});
return coreProposalBehavior(powers);
Expand Down
59 changes: 31 additions & 28 deletions packages/deploy-script-support/src/extract-proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
* @typedef {string | { module: string, entrypoint: string, args?: Array<unknown> }} ConfigProposal
*/

const { details: X, Fail } = assert;
const { Fail } = assert;

const req = createRequire(import.meta.url);

Expand All @@ -24,8 +24,8 @@ const req = createRequire(import.meta.url);
* @typedef {string} FilePath
*/
const pathResolve = (...paths) => {
const fileName = paths.pop();
assert(fileName, '>=1 paths required');
const fileName = /** @type {string} */ (paths.pop());
fileName || Fail`base name required`;
try {
return req.resolve(fileName, {
paths,
Expand Down Expand Up @@ -132,11 +132,8 @@ export const extractCoreProposalBundles = async (
absolutePaths.bundle = absoluteBundle;
const oldSource = bundleToSource.get(absoluteBundle);
if (oldSource) {
assert.equal(
oldSource,
absoluteSrc,
X`${bundlePath} already installed from ${oldSource}, now ${absoluteSrc}`,
);
oldSource === absoluteSrc ||
Fail`${bundlePath} already installed from ${oldSource}, now ${absoluteSrc}`;
} else {
bundleToSource.set(absoluteBundle, absoluteSrc);
}
Expand Down Expand Up @@ -197,21 +194,20 @@ export const extractCoreProposalBundles = async (
harden(proposal),
);

const behaviorSource = pathResolve(initDir, sourceSpec);
const behaviors = await import(behaviorSource);
const [exportedGetManifest, ...manifestArgs] = getManifestCall;
assert(
exportedGetManifest in behaviors,
`behavior ${behaviorSource} missing ${exportedGetManifest}`,
const proposalSource = pathResolve(initDir, sourceSpec);
const proposalNS = await import(proposalSource);
const [manifestGetterName, ...manifestGetterArgs] = getManifestCall;
manifestGetterName in proposalNS ||
Fail`proposal ${proposalSource} missing export ${manifestGetterName}`;
const { manifest: customManifest } = await proposalNS[manifestGetterName](
harden({ restoreRef: () => null }),
...manifestGetterArgs,
);
const { manifest: overrideManifest } = await behaviors[
exportedGetManifest
](harden({ restoreRef: () => null }), ...manifestArgs);

const behaviorBundleHandle = {};
const specEntry = await handleToBundleSpec(
behaviorBundleHandle,
behaviorSource,
proposalSource,
thisProposalSequence,
'behaviors',
);
Expand All @@ -220,14 +216,14 @@ export const extractCoreProposalBundles = async (
bundleHandleToAbsolutePaths.set(
behaviorBundleHandle,
harden({
source: behaviorSource,
source: proposalSource,
}),
);

return harden({
ref: behaviorBundleHandle,
call: getManifestCall,
overrideManifest,
customManifest,
bundleSpecs: bundleSpecEntries,
});
}),
Expand All @@ -240,22 +236,29 @@ export const extractCoreProposalBundles = async (
harden(bundles);

// Extract the manifest references and calls.
const makeCPArgs = extracted.map(({ ref, call, overrideManifest }) => ({
const metadataRecords = extracted.map(({ ref, call, customManifest }) => ({
ref,
call,
overrideManifest,
customManifest,
}));
harden(makeCPArgs);
harden(metadataRecords);

const code = `\
// This is generated by @agoric/cosmic-swingset/src/extract-proposal.js - DO NOT EDIT
// This is generated by @agoric/deploy-script-support/src/extract-proposal.js - DO NOT EDIT
/* eslint-disable */
const makeCoreProposalArgs = harden(${stringify(makeCPArgs, true)});
const makeCoreProposalBehavior = ${makeCoreProposalBehavior};
const metadataRecords = harden(${stringify(metadataRecords, true)});
(${makeEnactCoreProposals})({ makeCoreProposalArgs, E });
// Make an enactCoreProposals function and "export" it by way of script completion value.
// It is constructed by an IIFE to ensure the absence of global bindings for
// makeCoreProposalBehavior and makeEnactCoreProposals (the latter referencing the former),
// which may not be necessary but preserves behavior pre-dating
// https://github.com/Agoric/agoric-sdk/pull/8712 .
const enactCoreProposals = ((
makeCoreProposalBehavior = ${makeCoreProposalBehavior},
makeEnactCoreProposals = ${makeEnactCoreProposals},
) => makeEnactCoreProposals({ metadataRecords, E }))();
enactCoreProposals;
`;

// console.debug('created bundles from proposals:', coreProposals, bundles);
Expand Down
24 changes: 14 additions & 10 deletions packages/deploy-script-support/src/writeCoreProposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ export const makeWriteCoreProposal = (
const mergeProposalPermit = async (proposal, additionalPermits) => {
const {
sourceSpec,
getManifestCall: [exportedGetManifest, ...manifestArgs],
getManifestCall: [manifestGetterName, ...manifestGetterArgs],
} = proposal;

const manifestNs = await import(pathResolve(sourceSpec));
const proposalNS = await import(pathResolve(sourceSpec));

// We only care about the manifest, not any restoreRef calls.
const { manifest } = await manifestNs[exportedGetManifest](
{ restoreRef: x => `restoreRef:${x}` },
...manifestArgs,
const { manifest } = await proposalNS[manifestGetterName](
harden({ restoreRef: x => `restoreRef:${x}` }),
...manifestGetterArgs,
);

const mergedPermits = mergePermits(manifest);
Expand Down Expand Up @@ -100,7 +100,7 @@ export const makeWriteCoreProposal = (
// console.log('created', { filePrefix, sourceSpec, getManifestCall });

// Extract the top-level permit.
const { permits: proposalPermit, manifest: overrideManifest } =
const { permits: proposalPermit, manifest: customManifest } =
await mergeProposalPermit(proposal, permits);

// Get an install
Expand All @@ -113,10 +113,14 @@ export const makeWriteCoreProposal = (
const manifestBundleRef = ${stringify(manifestBundleRef)};
const getManifestCall = harden(${stringify(getManifestCall, true)});
const overrideManifest = ${stringify(overrideManifest, true)};
// Make the behavior the completion value.
(${makeCoreProposalBehavior})({ manifestBundleRef, getManifestCall, overrideManifest, E });
const customManifest = ${stringify(customManifest, true)};
// Make a behavior function and "export" it by way of script completion value.
// It is constructed by an anonymous invocation to ensure the absence of a global binding
// for makeCoreProposalBehavior, which may not be necessary but preserves behavior pre-dating
// https://github.com/Agoric/agoric-sdk/pull/8712 .
const behavior = (${makeCoreProposalBehavior})({ manifestBundleRef, getManifestCall, customManifest, E });
behavior;
`;

const trimmed = defangAndTrim(code);
Expand Down

0 comments on commit 23dc128

Please sign in to comment.