Skip to content

Commit

Permalink
feat(cosmic-swingset): Add a config property for exporting storage to…
Browse files Browse the repository at this point in the history
… bootstrap

Fixes #7156
  • Loading branch information
gibson042 committed Apr 24, 2023
1 parent 18a0516 commit 8199ab6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/SwingSet/src/types-external.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ export {};
* @typedef {object} SwingSetOptions
* @property {string} [bootstrap]
* @property {ConfigProposal[]} [coreProposals]
* @property {string[]} [exportStorageSubtrees] chain storage paths
* for which data should be exported into bootstrap vat parameter `chainStorageEntries`
* (e.g., `exportStorageSubtrees: ['a.b']` might result in vatParameters including
* `chainStorageEntries: [ ['a.b', ''], ['a.b.c', '"foo"'], ['a.b.c2', '42'] ]`).
* @property {boolean} [includeDevDependencies] indicates that
* `devDependencies` of the surrounding `package.json` should be accessible to
* bundles.
Expand Down
35 changes: 31 additions & 4 deletions packages/cosmic-swingset/src/launch-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,47 @@ export async function buildSwingset(
return;
}
if (!config) throw Fail`config not yet set`;
const { coreProposals, exportStorageSubtrees } = config;

// XXX `initializeSwingset` does not have a default for `config.bootstrap`;
// should we universally ensure its presence in `config` above?
const bootVat = config.vats[config.bootstrap || 'bootstrap'];

// Find the entrypoints for all the core proposals.
if (config.coreProposals) {
if (coreProposals) {
const { bundles, code } = await extractCoreProposalBundles(
config.coreProposals,
coreProposals,
vatconfig,
);
const bootVat = config.vats[config.bootstrap || 'bootstrap'];
config.bundles = { ...config.bundles, ...bundles };

// Tell the bootstrap code how to run the core proposals.
bootVat.parameters = { ...bootVat.parameters, coreProposalCode: code };
}
config.pinBootstrapRoot = true;

// Extract data from chain storage.
if (exportStorageSubtrees) {
const callChainStorage = (method, path) =>
bridgeOutbound(BRIDGE_ID.STORAGE, { method, args: [path] });
const chainStorageEntries = [];
// Preserve the ordering of each subtree via depth-first traversal.
let pendingEntries = exportStorageSubtrees.map(path => {
const value = callChainStorage('get', path);
return [path, value];
});
while (pendingEntries.length > 0) {
const entry = /** @type {[path: string, value: string]} */ (
pendingEntries.shift()
);
chainStorageEntries.push(entry);
const [path] = entry;
const childEntries = callChainStorage('entries', path);
pendingEntries = [...childEntries, ...pendingEntries];
}
bootVat.parameters = { ...bootVat.parameters, chainStorageEntries };
}

config.pinBootstrapRoot = true;
await initializeSwingset(config, bootstrapArgs, kernelStorage, {
// @ts-expect-error debugPrefix? what's that?
debugPrefix,
Expand Down

0 comments on commit 8199ab6

Please sign in to comment.