Skip to content

Commit

Permalink
refactor: build XS bundles with Kernel bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
dckc committed Jan 23, 2021
1 parent 758751d commit ad730d2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 38 deletions.
22 changes: 5 additions & 17 deletions packages/SwingSet/src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import anylogger from 'anylogger';
import { assert } from '@agoric/assert';
import { isTamed, tameMetering } from '@agoric/tame-metering';
import { importBundle } from '@agoric/import-bundle';
import bundleSource from '@agoric/bundle-source';
import { initSwingStore } from '@agoric/swing-store-simple';
import { makeMeteringTransformer } from '@agoric/transform-metering';
import { makeTransform } from '@agoric/transform-eventual-send';
Expand All @@ -38,20 +37,6 @@ function makeConsole(tag) {
return harden(cons);
}

async function buildXsBundles() {
const zip = (xs, ys) => xs.map((x, i) => [x, ys[i]]);
const { keys, values, fromEntries } = Object;
const allValues = async obj =>
fromEntries(zip(keys(obj), await Promise.all(values(obj))));
const src = rel => bundleSource(require.resolve(rel), 'getExport');
return harden(
await allValues({
lockdown: src('./kernel/vatManager/lockdown-subprocess-xsnap.js'),
supervisor: src('./kernel/vatManager/supervisor-subprocess-xsnap.js'),
}),
);
}

export async function makeSwingsetController(
hostStorage = initSwingStore().storage,
deviceEndowments = {},
Expand Down Expand Up @@ -173,7 +158,6 @@ export async function makeSwingsetController(
return startSubprocessWorker(process.execPath, ['-r', 'esm', supercode]);
}

const { xsnapBundles = await buildXsBundles() } = {}; // @@@ options?
const startXSnap = (name, handleCommand) => {
const worker = xsnap({
os: osType(),
Expand All @@ -185,7 +169,11 @@ export async function makeSwingsetController(
// debug: true,
});

return harden({ worker, bundles: xsnapBundles });
const bundles = {
lockdown: JSON.parse(hostStorage.get('lockdownBundle')),
supervisor: JSON.parse(hostStorage.get('supervisorBundle')),
};
return harden({ worker, bundles });
};

const slogF =
Expand Down
42 changes: 25 additions & 17 deletions packages/SwingSet/src/initializeSwingset.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,33 @@ import { initSwingStore } from '@agoric/swing-store-simple';
import { insistStorageAPI } from './storageAPI';
import { initializeKernel } from './kernel/initializeKernel';

const zip = (xs, ys) => xs.map((x, i) => [x, ys[i]]);
const { keys, values, fromEntries } = Object;
const allValues = async obj =>
fromEntries(zip(keys(obj), await Promise.all(values(obj))));

/**
* Build the kernel source bundles.
*
* Build the source bundles for the kernel and xsnap vat worker.
*/
export async function buildKernelBundles() {
// this takes 2.7s on my computer
const sources = {
kernel: require.resolve('./kernel/kernel.js'),
adminDevice: require.resolve('./kernel/vatAdmin/vatAdmin-src'),
adminVat: require.resolve('./kernel/vatAdmin/vatAdminWrapper'),
comms: require.resolve('./vats/comms'),
vattp: require.resolve('./vats/vat-tp'),
timer: require.resolve('./vats/vat-timerWrapper'),
};
const kernelBundles = {};
for (const name of Object.keys(sources)) {
// this was harder to read with Promise.all
// eslint-disable-next-line no-await-in-loop
kernelBundles[name] = await bundleSource(sources[name]);
}
return harden(kernelBundles);

const src = rel => bundleSource(require.resolve(rel));
const srcGE = rel => bundleSource(require.resolve(rel), 'getExport');

const bundles = await allValues({
kernel: src('./kernel/kernel.js'),
adminDevice: src('./kernel/vatAdmin/vatAdmin-src'),
adminVat: src('./kernel/vatAdmin/vatAdminWrapper'),
comms: src('./vats/comms'),
vattp: src('./vats/vat-tp'),
timer: src('./vats/vat-timerWrapper'),

lockdown: srcGE('./kernel/vatManager/lockdown-subprocess-xsnap.js'),
supervisor: srcGE('./kernel/vatManager/supervisor-subprocess-xsnap.js'),
});

return harden(bundles);
}

function byName(a, b) {
Expand Down Expand Up @@ -246,6 +252,8 @@ export async function initializeSwingset(
const { kernelBundles = await buildKernelBundles() } = initializationOptions;

hostStorage.set('kernelBundle', JSON.stringify(kernelBundles.kernel));
hostStorage.set('lockdownBundle', JSON.stringify(kernelBundles.lockdown));
hostStorage.set('supervisorBundle', JSON.stringify(kernelBundles.supervisor));

if (config.bootstrap && argv) {
if (config.vats[config.bootstrap]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ export function makeXsSubprocessFactory({
superCode.moduleFormat === 'getExport',
details`${it} unexpected: ${superCode.moduleFormat}`,
);
await worker.evaluate(
`(${superCode.source}
)()`.trim(),
);
await worker.evaluate(`(${superCode.source}\n)()`.trim());
}

/** @type { (item: Tagged) => Promise<Tagged> } */
Expand Down

0 comments on commit ad730d2

Please sign in to comment.