-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Zoe simpleExchange perf test in swingset-runner
- Loading branch information
Showing
21 changed files
with
1,917 additions
and
30 deletions.
There are no files selected for viewing
Empty file.
This file was deleted.
Oops, something went wrong.
103 changes: 103 additions & 0 deletions
103
packages/swingset-runner/demo/exchangeBenchmark/bootstrap.js
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,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
116
packages/swingset-runner/demo/exchangeBenchmark/exchanger.js
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,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
48
packages/swingset-runner/demo/exchangeBenchmark/helpers.js
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,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, | ||
}); | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/swingset-runner/demo/exchangeBenchmark/prepareContracts.js
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,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')); |
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,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('')); | ||
}; | ||
} |
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,5 @@ | ||
import commonSetup from './exchanger'; | ||
|
||
export default function setup(syscall, state, helpers) { | ||
return commonSetup(syscall, state, helpers, 'alice'); | ||
} |
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,5 @@ | ||
import commonSetup from './exchanger'; | ||
|
||
export default function setup(syscall, state, helpers) { | ||
return commonSetup(syscall, state, helpers, 'bob'); | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/swingset-runner/demo/exchangeBenchmark/vat-zoe.js
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,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); |
Oops, something went wrong.