Skip to content

Commit

Permalink
feat(vats): assign client properties on a per-address basis
Browse files Browse the repository at this point in the history
  • Loading branch information
dckc authored and michaelfig committed Jan 16, 2022
1 parent af1b920 commit 48194ed
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 45 deletions.
44 changes: 7 additions & 37 deletions packages/vats/src/bootstrap-behaviors-sim.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// @ts-check
import { E, Far } from '@agoric/far';
import { makeNotifierKit } from '@agoric/notifier';
import { bootstrapManifest } from './bootstrap-behaviors.js';
import {
bootstrapManifest,
installClientEgress,
} from './bootstrap-behaviors.js';

export const simBootstrapManifest = harden({
behaviors: { installSimEgress: true, ...bootstrapManifest.behaviors },
Expand All @@ -27,45 +28,14 @@ export const simBootstrapManifest = harden({
* },
* workspace: Record<string, ERef<unknown>>,
* }} powers
*
* @typedef {{ getChainBundle: () => unknown }} ChainBundler
*/
const installSimEgress = async ({ vatParameters, vats, workspace }) => {
const PROVISIONER_INDEX = 1;

const { argv } = vatParameters;
const addRemote = async addr => {
const { transmitter, setReceiver } = await E(vats.vattp).addRemote(addr);
await E(vats.comms).addRemote(addr, transmitter, setReceiver);
};

// 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),
getChainBundleNotifier: () => notifier,
});

await Promise.all(
/** @type { string[] } */ (argv.hardcodedClientAddresses).map(
async addr => {
await addRemote(addr);
await E(vats.comms).addEgress(addr, PROVISIONER_INDEX, chainProvider);
},
return Promise.all(
/** @type { string[] } */ (argv.hardcodedClientAddresses).map(addr =>
installClientEgress(addr, { vats, workspace }),
),
);

workspace.allClients = harden({
assign: newProperties => {
bundle = { ...bundle, ...newProperties };
updater.updateState(bundle);
},
});
};

harden({ installSimEgress });
Expand Down
74 changes: 66 additions & 8 deletions packages/vats/src/bootstrap-behaviors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// @ts-check
import { E } from '@agoric/far';
import { E, Far } from '@agoric/far';
import { AssetKind } from '@agoric/ertp';
import { makeNotifierKit } from '@agoric/notifier';

const { entries, fromEntries } = Object;

// TODO: phase out ./issuers.js
export const CENTRAL_ISSUER_NAME = 'RUN';
Expand Down Expand Up @@ -35,7 +38,7 @@ export const bootstrapManifest = harden({
workspace: true,
},
makeBoard: {
workspace: { vatAdminSvc: true, allClients: true },
workspace: { vatAdminSvc: true, client: true },
},
},
});
Expand Down Expand Up @@ -87,26 +90,81 @@ const buildZoe = async ({ workspace }) => {
);

workspace.zoe = zoe;
E(workspace.allClients).assign({ zoe });
E(workspace.client).assignBundle({ zoe: _addr => zoe });
};

/**
* @param {{
* workspace: { vatAdminSvc: VatAdminSvc, allClients: Record<string, unknown> }
* workspace: { vatAdminSvc: VatAdminSvc, client: Record<string, unknown> }
* }} powers
*/
const makeBoard = async ({ workspace: { vatAdminSvc, allClients } }) => {
const makeBoard = async ({ workspace: { vatAdminSvc, client } }) => {
const { root } = await E(vatAdminSvc).createVatByName('board');

const board = E(root).getBoard();
E(allClients).assign({ board });
return E(client).assignBundle({ board: _addr => board });
};

/* TODO
agoricNames,
namesByAddress,
myAddressNameAdmin,
*/
const callProperties = (obj, ...args) =>
fromEntries(entries(obj).map(([k, fn]) => [k, fn(...args)]));

/**
* @param { string } addr
* @param {{
* vats: {
* vattp: VattpVat,
* comms: CommsVatRoot,
* },
* workspace: Record<string, ERef<unknown>>,
* }} powers
*
* @typedef {{ getChainBundle: () => unknown }} ChainBundler
*/
const installClientEgress = async (addr, { vats, workspace }) => {
const PROVISIONER_INDEX = 1;

// TODO: get rid of echoObj?
let bundle = harden({
echoer: Far('echoObj', { echo: message => message }),
// TODO: echo: Far('echoFn', message => message),
});
const { notifier, updater } = makeNotifierKit({ bundle });

harden({ connectVattpWithMailbox, makeVatAdminService, buildZoe, makeBoard });
export { connectVattpWithMailbox, makeVatAdminService, buildZoe, makeBoard };
const chainProvider = Far('chainProvider', {
getChainBundle: () =>
notifier.getUpdateSince().then(({ value: { bundle: b } }) => b),
getChainConfigNotifier: () => notifier,
});

const { transmitter, setReceiver } = await E(vats.vattp).addRemote(addr);
await E(vats.comms).addRemote(addr, transmitter, setReceiver);
await E(vats.comms).addEgress(addr, PROVISIONER_INDEX, chainProvider);

workspace.client = harden({
assignBundle: newPropertyMakers => {
const newProperties = callProperties(newPropertyMakers, addr);
bundle = { ...bundle, ...newProperties };
updater.updateState({ bundle });
},
});
};

harden({
connectVattpWithMailbox,
makeVatAdminService,
buildZoe,
makeBoard,
installClientEgress,
});
export {
connectVattpWithMailbox,
makeVatAdminService,
buildZoe,
makeBoard,
installClientEgress,
};

0 comments on commit 48194ed

Please sign in to comment.