generated from MetaMask/metamask-module-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Handle vat resumption on vat or kernel restart #448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,89 @@ | ||
| import { E } from '@endo/eventual-send'; | ||
| import { Far } from '@endo/marshal'; | ||
| import { makePromiseKit } from '@endo/promise-kit'; | ||
| /** | ||
| * Build function for vats that will run various tests. | ||
| * | ||
| * @param {*} _vatPowers - Special powers granted to this vat (not used here). | ||
| * @param {*} parameters - Initialization parameters from the vat's config object. | ||
| * @param {*} _baggage - Root of vat's persistent state (not used here). | ||
| * @returns {*} The root object for the new vat. | ||
| */ | ||
| export function buildRootObject(_vatPowers, parameters, _baggage) { | ||
| const name = parameters?.name ?? 'anonymous'; | ||
| const test = parameters?.test ?? 'unspecified'; | ||
|
|
||
| /** | ||
| * Print a message to the log. | ||
| * | ||
| * @param {string} message - The message to print. | ||
| */ | ||
| function log(message) { | ||
| console.log(`${name}: ${message}`); | ||
| } | ||
|
|
||
| /** | ||
| * Print a message to the log, tagged as part of the test output. | ||
| * | ||
| * @param {string} message - The message to print. | ||
| */ | ||
| function tlog(message) { | ||
| console.log(`::> ${name}: ${message}`); | ||
| } | ||
|
|
||
| log(`buildRootObject`); | ||
| log(`configuration parameters: ${JSON.stringify(parameters)}`); | ||
|
|
||
| const thing = Far('thing', { | ||
| doSomething() { | ||
| tlog(`thing.doSomething`); | ||
| return `deferred something`; | ||
| }, | ||
| }); | ||
|
|
||
| let resolveDeferred; | ||
|
|
||
| return Far('root', { | ||
| async bootstrap(vats) { | ||
| log(`bootstrap start`); | ||
| tlog(`running test ${test}`); | ||
| const promise1 = E(vats.bob).setup(); | ||
| const promise2 = E(promise1).doSomething(); | ||
| const doneP = promise2.then( | ||
| (res) => { | ||
| tlog(`second result resolved to '${res}'`); | ||
| return 'p2succ'; | ||
| }, | ||
| (rej) => { | ||
| tlog(`second result rejected with '${rej}'`); | ||
| return 'p2fail'; | ||
| }, | ||
| ); | ||
| await E(vats.bob).doResolve(); | ||
| tlog(`invoking loopback`); | ||
| await E(vats.bob).loopback(); | ||
| tlog(`loopback done`); | ||
| return doneP; | ||
| }, | ||
|
|
||
| // This is a hack that effectively does the job of stdout.flush() even | ||
| // though we don't have access to stdout itself here. It makes sure we | ||
| // capture all the log output prior to the return value from `bootstrap` | ||
| // resolving. | ||
| loopback() { | ||
| tlog(`loopback`); | ||
| return undefined; | ||
| }, | ||
|
|
||
| setup() { | ||
| tlog(`setup`); | ||
| const { promise, resolve } = makePromiseKit(); | ||
| resolveDeferred = resolve; | ||
| return promise; | ||
| }, | ||
| doResolve() { | ||
| tlog(`doResolve`); | ||
| resolveDeferred(thing); | ||
| }, | ||
| }); | ||
| } |
This file contains hidden or 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,117 @@ | ||
| /* global harden */ | ||
| import { E } from '@endo/eventual-send'; | ||
| import { Far } from '@endo/marshal'; | ||
|
|
||
| /** | ||
| * Build function for generic test vat. | ||
| * | ||
| * @param {unknown} _vatPowers - Special powers granted to this vat (not used here). | ||
| * @param {unknown} parameters - Initialization parameters from the vat's config object. | ||
| * @param {unknown} baggage - Root of vat's persistent state (not used here). | ||
| * @returns {unknown} The root object for the new vat. | ||
| */ | ||
| export function buildRootObject(_vatPowers, parameters, baggage) { | ||
| const name = parameters?.name ?? 'anonymous'; | ||
|
|
||
| /** | ||
| * Print a message to the log. | ||
| * | ||
| * @param {string} message - The message to print. | ||
| */ | ||
| function log(message) { | ||
| console.log(`${name}: ${message}`); | ||
| } | ||
|
|
||
| /** | ||
| * Print a message to the log, tagged as part of the test output. | ||
| * | ||
| * @param {string} message - The message to print. | ||
| */ | ||
| function tlog(message) { | ||
| console.log(`::> ${name}: ${message}`); | ||
| } | ||
|
|
||
| log(`buildRootObject`); | ||
|
|
||
| let startCount; | ||
| if (baggage.has('name')) { | ||
| const savedName = baggage.get('name'); | ||
| tlog(`saved name is ${savedName}`); | ||
|
|
||
| startCount = baggage.get('startCount') + 1; | ||
| baggage.set('startCount', startCount); | ||
| } else { | ||
| baggage.init('name', name); | ||
| tlog(`saving name`); | ||
|
|
||
| baggage.init('startCount', 1); | ||
| startCount = 1; | ||
| } | ||
| tlog(`start count: ${startCount}`); | ||
|
|
||
| const me = Far('root', { | ||
| async bootstrap(vats) { | ||
| tlog(`bootstrap()`); | ||
| // Explanation for the following bit of gymnastics: we'd like to save | ||
| // `vats` itself in the baggage, but we can't because the entry for our | ||
| // own root is a local reference and thus not durable, and we can't remove | ||
| // this entry from `vats` directly because, being a parameter object, it | ||
| // arrived hardened. So instead we have to copy it sans the unwritable element. | ||
| const writeVats = {}; | ||
| for (const [prop, value] of Object.entries(vats)) { | ||
| if (value !== me) { | ||
| writeVats[prop] = value; | ||
| } | ||
| } | ||
| baggage.init('vats', harden(writeVats)); | ||
|
|
||
| const pIntroB = E(vats.bob).intro(me); | ||
| const pIntroC = E(vats.carol).intro(me); | ||
| const pGreetB = E(vats.bob).greet(`hello from ${name}`); | ||
| const pGreetC = E(vats.carol).greet(`hello from ${name}`); | ||
| const results = await Promise.all([pIntroB, pIntroC, pGreetB, pGreetC]); | ||
| const [, , greetB, greetC] = results; | ||
| tlog(`Bob answers greeting: '${greetB}'`); | ||
| tlog(`Carol answers greeting: '${greetC}'`); | ||
| tlog(`end bootstrap`); | ||
| await E(vats.bob).loopback(); | ||
| return `bootstrap ${name}`; | ||
| }, | ||
| intro(bootVat) { | ||
| tlog(`intro()`); | ||
| baggage.init('bootVat', bootVat); | ||
| }, | ||
| greet(greeting) { | ||
| tlog(`greet('${greeting}')`); | ||
| return `${name} returns your greeting '${greeting}'`; | ||
| }, | ||
| async resume() { | ||
| tlog(`resume()`); | ||
| if (baggage.has('vats')) { | ||
| // I am the bootstrap vat | ||
| tlog(`resumed vat is bootstrap`); | ||
| const vats = baggage.get('vats'); | ||
| const pGreetB = E(vats.bob).greet(`hello again from ${name}`); | ||
| const pGreetC = E(vats.carol).greet(`hello again from ${name}`); | ||
| const [greetB, greetC] = await Promise.all([pGreetB, pGreetC]); | ||
| tlog(`Bob answers greeting: '${greetB}'`); | ||
| tlog(`Carol answers greeting: '${greetC}'`); | ||
| await E(vats.bob).loopback(); | ||
| } | ||
| if (baggage.has('bootVat')) { | ||
| // I am Bob or Carol | ||
| tlog(`resumed vat is not bootstrap`); | ||
| const bootVat = baggage.get('bootVat'); | ||
| const greetBack = await E(bootVat).greet(`hello boot vat from ${name}`); | ||
| tlog(`boot vat returns greeting with '${greetBack}'`); | ||
| await E(bootVat).loopback(); | ||
| } | ||
| tlog(`end resume`); | ||
| return `resume ${name}`; | ||
| }, | ||
| loopback() { | ||
| return undefined; | ||
| }, | ||
| }); | ||
| return me; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.