From 1c0265353dcbb88fa5d2c7d80d53ebadee49936d Mon Sep 17 00:00:00 2001 From: Chip Morningstar Date: Thu, 13 Aug 2020 00:15:06 -0700 Subject: [PATCH 1/3] feat: support use of module references in swingset config sourceSpecs --- packages/SwingSet/src/controller.js | 64 ++++++++++++++----- .../demo/exchangeBenchmark/swingset.json | 2 +- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/packages/SwingSet/src/controller.js b/packages/SwingSet/src/controller.js index e3e01947a00..f40358ccfc5 100644 --- a/packages/SwingSet/src/controller.js +++ b/packages/SwingSet/src/controller.js @@ -125,15 +125,48 @@ export function loadBasedir(basedir) { return config; } +/** + * Resolve a pathname found in a config descriptor. First try to resolve it as + * a module path, and then if that doesn't work try to resolve it as an + * ordinary path relative to the directory in which the config file was found. + * + * @param dirname Path to directory containing the config file + * @param specPath Path found in a `sourceSpec` or `bundleSpec` property + * + * @return the absolute path corresponding to `specPath` if it can be + * determined. + */ +function resolveSpecFromConfig(dirname, specPath) { + try { + return require.resolve(specPath); + } catch (e) { + if (e.code === 'MODULE_NOT_FOUND') { + return path.resolve(dirname, specPath); + } else { + throw e; + } + } +} + +/** + * For each entry in a config descriptor (i.e, `vats`, `bundles`, etc), convert + * it to normal form: resolve each pathname to a context-insensitive absolute + * path and make sure it has a `parameters` property if it's supposed to. + * + * @param desc The config descriptor to be normalized. + * @param dirname The pathname of the directory in which the config file was found + * @param expectParameters `true` if the entries should have parameters (for + * example, `true` for `vats` but `false` for bundles). + */ function normalizeConfigDescriptor(desc, dirname, expectParameters) { if (desc) { for (const name of Object.keys(desc)) { const entry = desc[name]; if (entry.sourceSpec) { - entry.sourceSpec = path.resolve(dirname, entry.sourceSpec); + entry.sourceSpec = resolveSpecFromConfig(dirname, entry.sourceSpec); } if (entry.bundleSpec) { - entry.bundleSpec = path.resolve(dirname, entry.bundleSpec); + entry.bundleSpec = resolveSpecFromConfig(dirname, entry.bundleSpec); } if (expectParameters && !entry.parameters) { entry.parameters = {}; @@ -142,6 +175,17 @@ function normalizeConfigDescriptor(desc, dirname, expectParameters) { } } +/** + * Read and parse a swingset config file and return it in normalized form. + * + * @param configPath Path to the config file to be processed + * + * @return the contained config object, in normalized form, or null if the + * requested config file did not exist. + * + * @throws if the file existed but was inaccessible, malformed, or otherwise + * invalid. + */ export function loadSwingsetConfigFile(configPath) { try { const config = JSON.parse(fs.readFileSync(configPath)); @@ -398,21 +442,9 @@ export async function buildVatController( if (!desc.bundleName) { names.push(name); if (desc.sourceSpec) { - const sourceSpec = desc.sourceSpec; - if (!(sourceSpec[0] === '.' || path.isAbsolute(sourceSpec))) { - throw Error( - 'sourceSpec must be relative (./foo) or absolute (/foo) not bare (foo)', - ); - } - presumptiveBundles.push(bundleSource(sourceSpec)); + presumptiveBundles.push(bundleSource(desc.sourceSpec)); } else if (desc.bundleSpec) { - const bundleSpec = desc.bundleSpec; - if (!(bundleSpec[0] === '.' || path.isAbsolute(bundleSpec))) { - throw Error( - 'bundleSpec must be relative (./foo) or absolute (/foo) not bare (foo)', - ); - } - presumptiveBundles.push(fs.readFileSync(bundleSpec)); + presumptiveBundles.push(fs.readFileSync(desc.bundleSpec)); } else if (desc.bundle) { presumptiveBundles.push(desc.bundle); } else { diff --git a/packages/swingset-runner/demo/exchangeBenchmark/swingset.json b/packages/swingset-runner/demo/exchangeBenchmark/swingset.json index 5f36231e2a6..2dee26eda9b 100644 --- a/packages/swingset-runner/demo/exchangeBenchmark/swingset.json +++ b/packages/swingset-runner/demo/exchangeBenchmark/swingset.json @@ -2,7 +2,7 @@ "bootstrap": "bootstrap", "bundles": { "zcf": { - "sourceSpec": "../../../zoe/src/contractFacet.js" + "sourceSpec": "@agoric/zoe/src/contractFacet" } }, "vats": { From 285965047ba92608b39d9fa496b1bf2b952d748d Mon Sep 17 00:00:00 2001 From: Chip Morningstar Date: Thu, 13 Aug 2020 00:15:56 -0700 Subject: [PATCH 2/3] feat: make Zoe in cosmic-swingset work with prebundled zcf --- packages/cosmic-swingset/lib/ag-solo/init-basedir.js | 2 +- packages/cosmic-swingset/lib/ag-solo/vats/chain-config.json | 2 +- packages/cosmic-swingset/lib/ag-solo/vats/solo-config.json | 2 +- packages/cosmic-swingset/lib/ag-solo/vats/vat-zoe.js | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/cosmic-swingset/lib/ag-solo/init-basedir.js b/packages/cosmic-swingset/lib/ag-solo/init-basedir.js index 7224c0d97ed..a67d700140c 100644 --- a/packages/cosmic-swingset/lib/ag-solo/init-basedir.js +++ b/packages/cosmic-swingset/lib/ag-solo/init-basedir.js @@ -71,7 +71,7 @@ export default function initBasedir( const dstVatdir = path.join(basedir, 'vats'); fs.mkdirSync(dstVatdir); fs.readdirSync(srcVatdir) - .filter(name => name.match(/\.js$/)) + .filter(name => name.match(/\.(js|json)$/)) .forEach(name => { fs.copyFileSync(path.join(srcVatdir, name), path.join(dstVatdir, name)); }); diff --git a/packages/cosmic-swingset/lib/ag-solo/vats/chain-config.json b/packages/cosmic-swingset/lib/ag-solo/vats/chain-config.json index ca652836fed..435b6a82dde 100644 --- a/packages/cosmic-swingset/lib/ag-solo/vats/chain-config.json +++ b/packages/cosmic-swingset/lib/ag-solo/vats/chain-config.json @@ -2,7 +2,7 @@ "bootstrap": "bootstrap", "bundles": { "zcf": { - "sourceSpec": "../../../../zoe/src/contractFacet.js" + "sourceSpec": "@agoric/zoe/src/contractFacet" } }, "vats": { diff --git a/packages/cosmic-swingset/lib/ag-solo/vats/solo-config.json b/packages/cosmic-swingset/lib/ag-solo/vats/solo-config.json index ca652836fed..435b6a82dde 100644 --- a/packages/cosmic-swingset/lib/ag-solo/vats/solo-config.json +++ b/packages/cosmic-swingset/lib/ag-solo/vats/solo-config.json @@ -2,7 +2,7 @@ "bootstrap": "bootstrap", "bundles": { "zcf": { - "sourceSpec": "../../../../zoe/src/contractFacet.js" + "sourceSpec": "@agoric/zoe/src/contractFacet" } }, "vats": { diff --git a/packages/cosmic-swingset/lib/ag-solo/vats/vat-zoe.js b/packages/cosmic-swingset/lib/ag-solo/vats/vat-zoe.js index 0c7f6763306..07d3a60c134 100644 --- a/packages/cosmic-swingset/lib/ag-solo/vats/vat-zoe.js +++ b/packages/cosmic-swingset/lib/ag-solo/vats/vat-zoe.js @@ -2,8 +2,8 @@ import { makeZoe } from '@agoric/zoe'; -export function buildRootObject(_vatPowers) { +export function buildRootObject(_vatPowers, vatParameters) { return harden({ - buildZoe: adminVat => makeZoe(adminVat), + buildZoe: adminVat => makeZoe(adminVat, vatParameters.zcfBundleName), }); } From e979475f4b5c77a1e084f82c814f866bf1a01457 Mon Sep 17 00:00:00 2001 From: Chip Morningstar Date: Thu, 13 Aug 2020 11:13:50 -0700 Subject: [PATCH 3/3] fix: handle relative paths more better --- packages/SwingSet/src/controller.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/SwingSet/src/controller.js b/packages/SwingSet/src/controller.js index f40358ccfc5..cae53373544 100644 --- a/packages/SwingSet/src/controller.js +++ b/packages/SwingSet/src/controller.js @@ -138,14 +138,13 @@ export function loadBasedir(basedir) { */ function resolveSpecFromConfig(dirname, specPath) { try { - return require.resolve(specPath); + return require.resolve(specPath, { path: [dirname] }); } catch (e) { - if (e.code === 'MODULE_NOT_FOUND') { - return path.resolve(dirname, specPath); - } else { + if (e.code !== 'MODULE_NOT_FOUND') { throw e; } } + return path.resolve(dirname, specPath); } /**