Skip to content

Commit

Permalink
feat(vats): provide home.zoe via makePromiseSpace
Browse files Browse the repository at this point in the history
  • Loading branch information
dckc authored and michaelfig committed Jan 16, 2022
1 parent a438f47 commit a50c727
Showing 1 changed file with 61 additions and 12 deletions.
73 changes: 61 additions & 12 deletions packages/vats/src/bootstrap-core.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @ts-check
import { E, Far } from '@agoric/far';
import { makeNotifierKit } from '@agoric/notifier';
import { makePromiseKit } from '@agoric/promise-kit';

import { feeIssuerConfig } from './bootstrap-zoe-config';

Expand Down Expand Up @@ -53,6 +54,7 @@ const PROVISIONER_INDEX = 1;
* vattp: VattpVat,
* comms: CommsVatRoot,
* },
* workspace: Record<string, ERef<unknown>>,
* }} powers
*
* @typedef {{ getChainBundle: () => unknown }} ChainBundler
Expand All @@ -70,14 +72,12 @@ const installSimEgress = async ({ vatParameters, vats, workspace }) => {
await E(vats.comms).addRemote(addr, transmitter, setReceiver);
};

const { notifier, updater } = makeNotifierKit();

updater.updateState(
harden({
echoer: Far('echoObj', { echo: message => message }),
// TODO: echo: Far('echoFn', message => message),
}),
);
// TODO: chainProvider per address
let bundle = harden({
echoer: Far('echoObj', { echo: message => message }),
// TODO: echo: Far('echoFn', message => message),
});
const { notifier, updater } = makeNotifierKit(bundle);

const chainProvider = Far('chainProvider', {
getChainBundle: () => notifier.getUpdateSince().then(({ value }) => value),
Expand All @@ -93,7 +93,12 @@ const installSimEgress = async ({ vatParameters, vats, workspace }) => {
),
);

// TODO: stuff the notifier in init thingy
workspace.allClients = {
assign: newProperties => {
bundle = { ...bundle, ...newProperties };
updater.updateState(bundle);
},
};
};

/**
Expand All @@ -104,21 +109,61 @@ const installSimEgress = async ({ vatParameters, vats, workspace }) => {
* devices: {
* vatAdmin: unknown,
* },
* workspace: Record<string, ERef<any>>,
* }} powers
*/
const buildZoe = async ({ vats, devices }) => {
const buildZoe = async ({ vats, devices, workspace }) => {
// TODO: what else do we need vatAdminSvc for? can we let it go out of scope?
const vatAdminSvc = E(vats.vatAdmin).createVatAdminService(devices.vatAdmin);

const { root } = await E(vatAdminSvc).createVatByName('zoe');
const { zoeService: _zoe, feeMintAccess: _2 } = await E(root).buildZoe(
const { zoeService: zoe, feeMintAccess: _2 } = await E(root).buildZoe(
vatAdminSvc,
feeIssuerConfig,
);

workspace.zoe = zoe;
E(workspace.allClients).assign({ zoe });
};

const steps = [connectVattpWithMailbox, installSimEgress, buildZoe];

const makePromiseSpace = () => {
/** @type {Map<string, PromiseRecord<unknown>} */
const state = new Map();

const findOrCreateKit = name => {
let kit = state.get(name);
if (kit) {
return kit;
} else {
console.info('workspace: allocating', name);
kit = makePromiseKit();
state.set(name, kit);
return kit;
}
};

const space = new Proxy(
{},
{
get: (_target, name) => {
assert.typeof(name, 'string');
const kit = findOrCreateKit(name);
return kit.promise;
},
set: (_target, name, value) => {
// Note: repeated resolves() are noops.
findOrCreateKit(name).resolve(value);
console.info('workspace: resolved', name);
return true;
},
},
);

return space;
};

/**
* Build root object of the bootstrap vat.
*
Expand All @@ -130,6 +175,8 @@ const steps = [connectVattpWithMailbox, installSimEgress, buildZoe];
* }} vatParameters
*/
export function buildRootObject(vatPowers, vatParameters) {
const workspace = makePromiseSpace();

return Far('bootstrap', {
/**
* Bootstrap vats and devices.
Expand All @@ -146,7 +193,9 @@ export function buildRootObject(vatPowers, vatParameters) {
*/
bootstrap: (vats, devices) =>
Promise.all(
steps.map(step => step({ vatPowers, vatParameters, vats, devices })),
steps.map(step =>
step({ vatPowers, vatParameters, vats, devices, workspace }),
),
),
});
}

0 comments on commit a50c727

Please sign in to comment.