-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7235 from Agoric/refactor-start-xsnap
refactor startXSnap
- Loading branch information
Showing
13 changed files
with
151 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { Fail } from '@agoric/assert'; | ||
import { type as osType } from 'os'; | ||
import { xsnap, recordXSnap } from '@agoric/xsnap'; | ||
|
||
const NETSTRING_MAX_CHUNK_SIZE = 12_000_000; | ||
|
||
/** | ||
* @param {{ moduleFormat: string, source: string }[]} bundles | ||
* @param {{ | ||
* snapStore?: SnapStore, | ||
* spawn: typeof import('child_process').spawn | ||
* debug?: boolean, | ||
* workerTraceRootPath?: string, | ||
* }} options | ||
*/ | ||
export function makeStartXSnap(bundles, options) { | ||
// our job is to simply curry some authorities and settings into the | ||
// 'startXSnap' function we return | ||
const { snapStore, spawn, debug = false, workerTraceRootPath } = options; | ||
|
||
let doXSnap = xsnap; | ||
|
||
if (workerTraceRootPath) { | ||
let serial = 0; | ||
const makeNextTraceDir = () => { | ||
const rel = `${workerTraceRootPath}/${serial}`; | ||
const workerTrace = path.resolve(rel) + path.sep; | ||
serial += 1; | ||
return workerTrace; | ||
}; | ||
doXSnap = opts => { | ||
const workerTraceDir = makeNextTraceDir(); | ||
console.log('SwingSet xs-worker tracing:', { workerTraceDir }); | ||
fs.mkdirSync(workerTraceDir, { recursive: true }); | ||
return recordXSnap(opts, workerTraceDir, { | ||
writeFileSync: fs.writeFileSync, | ||
}); | ||
}; | ||
} | ||
|
||
/** @type { import('@agoric/xsnap/src/xsnap').XSnapOptions } */ | ||
const xsnapOpts = { | ||
os: osType(), | ||
spawn, | ||
stdout: 'inherit', | ||
stderr: 'inherit', | ||
debug, | ||
netstringMaxChunkSize: NETSTRING_MAX_CHUNK_SIZE, | ||
}; | ||
|
||
/** | ||
* @param {string} vatID | ||
* @param {string} name | ||
* @param {(request: Uint8Array) => Promise<Uint8Array>} handleCommand | ||
* @param {boolean} [metered] | ||
* @param {boolean} [reload] | ||
*/ | ||
async function startXSnap( | ||
vatID, | ||
name, | ||
handleCommand, | ||
metered, | ||
reload = false, | ||
) { | ||
const meterOpts = metered ? {} : { meteringLimit: 0 }; | ||
if (snapStore && reload) { | ||
// console.log('startXSnap from', { snapshotHash }); | ||
return snapStore.loadSnapshot(vatID, async snapshot => { | ||
const xs = doXSnap({ | ||
snapshot, | ||
name, | ||
handleCommand, | ||
...meterOpts, | ||
...xsnapOpts, | ||
}); | ||
await xs.isReady(); | ||
return xs; | ||
}); | ||
} | ||
// console.log('fresh xsnap', { snapStore: snapStore }); | ||
const worker = doXSnap({ handleCommand, name, ...meterOpts, ...xsnapOpts }); | ||
|
||
for (const bundle of bundles) { | ||
bundle.moduleFormat === 'getExport' || | ||
bundle.moduleFormat === 'nestedEvaluate' || | ||
Fail`unexpected: ${bundle.moduleFormat}`; | ||
// eslint-disable-next-line no-await-in-loop, @jessie.js/no-nested-await | ||
await worker.evaluate(`(${bundle.source}\n)()`.trim()); | ||
} | ||
return worker; | ||
} | ||
return startXSnap; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.