Skip to content

Commit

Permalink
feat: Zoe simpleExchange perf test in swingset-runner
Browse files Browse the repository at this point in the history
  • Loading branch information
FUDCo committed Jun 24, 2020
1 parent 99dd7dd commit 79da155
Show file tree
Hide file tree
Showing 21 changed files with 1,917 additions and 30 deletions.
Empty file.
29 changes: 0 additions & 29 deletions packages/swingset-runner/.eslintrc.js

This file was deleted.

103 changes: 103 additions & 0 deletions packages/swingset-runner/demo/exchangeBenchmark/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import harden from '@agoric/harden';

import produceIssuer from '@agoric/ertp';
import { makePrintLog } from './printLog';

/* eslint-disable import/no-unresolved, import/extensions */
import simpleExchangeBundle from './bundle-simpleExchange';
/* eslint-enable import/no-unresolved, import/extensions */

function setupBasicMints() {
// prettier-ignore
const all = [
produceIssuer('moola'),
produceIssuer('simoleans'),
];
const mints = all.map(objs => objs.mint);
const issuers = all.map(objs => objs.issuer);
const amountMaths = all.map(objs => objs.amountMath);

return harden({
mints,
issuers,
amountMaths,
});
}

function makeVats(E, log, vats, zoe, installations, startingExtents) {
const { mints, issuers, amountMaths } = setupBasicMints();
// prettier-ignore
function makePayments(extents) {
return mints.map((mint, i) => mint.mintPayment(amountMaths[i].make(extents[i])));
}
const [aliceExtents, bobExtents] = startingExtents;

// Setup Alice
const alice = E(vats.alice).build(
zoe,
issuers,
makePayments(aliceExtents),
installations,
);

// Setup Bob
const bob = E(vats.bob).build(
zoe,
issuers,
makePayments(bobExtents),
installations,
);

const result = {
alice,
bob,
};

log(`=> alice and bob are set up`);
return harden(result);
}

function build(E, log) {
let alice;
let bob;
return harden({
async bootstrap(_argv, vats) {
const zoe = await E(vats.zoe).getZoe();

const installations = {
simpleExchange: await E(zoe).install(
simpleExchangeBundle.source,
simpleExchangeBundle.moduleFormat,
),
};

const startingExtents = [
[3, 0], // Alice: 3 moola, no simoleans
[0, 3], // Bob: no moola, 3 simoleans
];

({ alice, bob } = makeVats(
E,
log,
vats,
zoe,
installations,
startingExtents,
));
},
async runBenchmarkRound() {
await E(alice).initiateSimpleExchange(bob);
await E(bob).initiateSimpleExchange(alice);
},
});
}

function setup(syscall, state, helpers) {
return helpers.makeLiveSlots(
syscall,
state,
E => build(E, makePrintLog(helpers.log)),
helpers.vatID,
);
}
export default harden(setup);
116 changes: 116 additions & 0 deletions packages/swingset-runner/demo/exchangeBenchmark/exchanger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import harden from '@agoric/harden';
import { makeGetInstanceHandle } from '@agoric/zoe/src/clientSupport';
import { showPurseBalance, setupPurses } from './helpers';
import { makePrintLog } from './printLog';

async function build(E, log, name, zoe, issuers, payments, installations) {
const { moola, simoleans, purses } = await setupPurses(
zoe,
issuers,
payments,
);
const [moolaPurseP, simoleanPurseP] = purses;
const [moolaIssuer, simoleanIssuer] = issuers;
const issuerKeywordRecord = harden({
Price: simoleanIssuer,
Asset: moolaIssuer,
});
const inviteIssuer = await E(zoe).getInviteIssuer();
const getInstanceHandle = makeGetInstanceHandle(inviteIssuer);
const { simpleExchange } = installations;

async function preReport() {
await showPurseBalance(moolaPurseP, `${name} moola before`, log);
await showPurseBalance(simoleanPurseP, `${name} simoleans before`, log);
}

async function postReport() {
await showPurseBalance(moolaPurseP, `${name} moola after`, log);
await showPurseBalance(simoleanPurseP, `${name} simoleans after`, log);
}

async function receivePayout(payoutP) {
const payout = await payoutP;
const moolaPayout = await payout.Asset;
const simoleanPayout = await payout.Price;

await E(moolaPurseP).deposit(moolaPayout);
await E(simoleanPurseP).deposit(simoleanPayout);
}

async function initiateSimpleExchange(otherP) {
await preReport();

const addOrderInvite = await E(zoe).makeInstance(
simpleExchange,
issuerKeywordRecord,
);
const instanceHandle = await getInstanceHandle(addOrderInvite);
const { publicAPI } = await E(zoe).getInstanceRecord(instanceHandle);

const mySellOrderProposal = harden({
give: { Asset: moola(1) },
want: { Price: simoleans(1) },
exit: { onDemand: null },
});
const paymentKeywordRecord = {
Asset: await E(moolaPurseP).withdraw(moola(1)),
};
const { payout: payoutP, outcome: outcomeP } = await E(zoe).offer(
addOrderInvite,
mySellOrderProposal,
paymentKeywordRecord,
);

log(await outcomeP);

const inviteP = E(publicAPI).makeInvite();
await E(otherP).respondToSimpleExchange(inviteP);

await receivePayout(payoutP);
await postReport();
}

async function respondToSimpleExchange(inviteP) {
await preReport();

const invite = await inviteP;
const exclInvite = await E(inviteIssuer).claim(invite);

const myBuyOrderProposal = harden({
want: { Asset: moola(1) },
give: { Price: simoleans(1) },
exit: { onDemand: null },
});
const paymentKeywordRecord = {
Price: await E(simoleanPurseP).withdraw(simoleans(1)),
};

const { payout: payoutP, outcome: outcomeP } = await E(zoe).offer(
exclInvite,
myBuyOrderProposal,
paymentKeywordRecord,
);

log(await outcomeP);

await receivePayout(payoutP);
await postReport();
}

return harden({
initiateSimpleExchange,
respondToSimpleExchange,
});
}

export default function setup(syscall, state, helpers, name) {
// prettier-ignore
return helpers.makeLiveSlots(
syscall,
state,
E => harden({
build: (...args) => build(E, makePrintLog(helpers.log), name, ...args),
}),
);
}
48 changes: 48 additions & 0 deletions packages/swingset-runner/demo/exchangeBenchmark/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { E } from '@agoric/eventual-send';
import makeAmountMath from '@agoric/ertp/src/amountMath';
import harden from '@agoric/harden';

export async function showPurseBalance(purseP, name, log) {
try {
const amount = await E(purseP).getCurrentAmount();
log(name, ': balance ', amount);
} catch (err) {
console.error(err);
}
}

export function getLocalAmountMath(issuer) {
return Promise.all([
E(issuer).getBrand(),
E(issuer).getMathHelpersName(),
]).then(([brand, mathHelpersName]) => makeAmountMath(brand, mathHelpersName));
}

export async function setupPurses(zoe, issuers, payments) {
const purses = issuers.map(issuer => E(issuer).makeEmptyPurse());
const inviteIssuer = await E(zoe).getInviteIssuer();
const [moolaIssuer, simoleanIssuer] = issuers;

const [moolaPayment, simoleanPayment] = payments;
const [moolaPurseP, simoleanPurseP] = purses;
await E(moolaPurseP).deposit(moolaPayment);
await E(simoleanPurseP).deposit(simoleanPayment);

const moolaAmountMath = await getLocalAmountMath(moolaIssuer);
const simoleanAmountMath = await getLocalAmountMath(simoleanIssuer);

const moola = moolaAmountMath.make;
const simoleans = simoleanAmountMath.make;

return harden({
issuers: harden([moolaIssuer, simoleanIssuer]),
inviteIssuer,
moolaIssuer,
simoleanIssuer,
moolaAmountMath,
simoleanAmountMath,
moola,
simoleans,
purses,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import bundleSource from '@agoric/bundle-source';

import fs from 'fs';

const CONTRACT_FILES = ['simpleExchange'];

const generateBundlesP = Promise.all(
CONTRACT_FILES.map(async contract => {
const { source, moduleFormat } = await bundleSource(
`${__dirname}/../../../zoe/src/contracts/${contract}`,
);
const obj = { source, moduleFormat, contract };
fs.writeFileSync(
`${__dirname}/bundle-${contract}.js`,
`export default ${JSON.stringify(obj)};`,
);
}),
);

generateBundlesP.then(() => console.log('contracts prepared'));
9 changes: 9 additions & 0 deletions packages/swingset-runner/demo/exchangeBenchmark/printLog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function makePrintLog(kernelLog) {
return function printLog(...args) {
kernelLog(...args);
const rendered = args.map(arg =>
typeof arg === 'string' ? arg : JSON.stringify(arg),
);
console.log(rendered.join(''));
};
}
5 changes: 5 additions & 0 deletions packages/swingset-runner/demo/exchangeBenchmark/vat-alice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import commonSetup from './exchanger';

export default function setup(syscall, state, helpers) {
return commonSetup(syscall, state, helpers, 'alice');
}
5 changes: 5 additions & 0 deletions packages/swingset-runner/demo/exchangeBenchmark/vat-bob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import commonSetup from './exchanger';

export default function setup(syscall, state, helpers) {
return commonSetup(syscall, state, helpers, 'bob');
}
26 changes: 26 additions & 0 deletions packages/swingset-runner/demo/exchangeBenchmark/vat-zoe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import harden from '@agoric/harden';

import { makeZoe } from '@agoric/zoe';

const build = (_E, _log) => {
const zoe = makeZoe({ require });
return harden({
getZoe: () => zoe,
});
};

harden(build);

function setup(syscall, state, helpers) {
function log(...args) {
helpers.log(...args);
console.debug(...args);
}
return helpers.makeLiveSlots(
syscall,
state,
E => build(E, log),
helpers.vatID,
);
}
export default harden(setup);
Loading

0 comments on commit 79da155

Please sign in to comment.