-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new iterated Zoe benchmark that uses atomicSwap
- Loading branch information
Showing
7 changed files
with
321 additions
and
0 deletions.
There are no files selected for viewing
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,95 @@ | ||
import { makeIssuerKit } from '@agoric/ertp'; | ||
import { E } from '@agoric/eventual-send'; | ||
import { makePrintLog } from './printLog'; | ||
|
||
/* eslint-disable-next-line import/no-unresolved, import/extensions */ | ||
import atomicSwapBundle from './bundle-atomicSwap'; | ||
|
||
const log = makePrintLog(); | ||
|
||
function setupBasicMints() { | ||
// prettier-ignore | ||
const all = [ | ||
makeIssuerKit('moola'), | ||
makeIssuerKit('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(vats, zoe, installations, startingValues) { | ||
const { mints, issuers, amountMaths } = setupBasicMints(); | ||
// prettier-ignore | ||
function makePayments(values) { | ||
return mints.map((mint, i) => mint.mintPayment(amountMaths[i].make(values[i]))); | ||
} | ||
const [aliceValues, bobValues] = startingValues; | ||
|
||
// Setup Alice | ||
const alice = E(vats.alice).build( | ||
zoe, | ||
issuers, | ||
makePayments(aliceValues), | ||
installations, | ||
); | ||
|
||
// Setup Bob | ||
const bob = E(vats.bob).build( | ||
zoe, | ||
issuers, | ||
makePayments(bobValues), | ||
installations, | ||
); | ||
|
||
const result = { | ||
alice, | ||
bob, | ||
}; | ||
|
||
log(`=> alice and bob are set up`); | ||
return harden(result); | ||
} | ||
|
||
export function buildRootObject(_vatPowers, vatParameters) { | ||
let alice; | ||
let bob; | ||
let round = 0; | ||
return harden({ | ||
async bootstrap(vats, devices) { | ||
const vatAdminSvc = await E(vats.vatAdmin).createVatAdminService( | ||
devices.vatAdmin, | ||
); | ||
const zoe = await E(vats.zoe).buildZoe(vatAdminSvc); | ||
|
||
const installations = { | ||
atomicSwap: await E(zoe).install(atomicSwapBundle.bundle), | ||
}; | ||
|
||
const startingValues = [ | ||
[3, 0], // Alice: 3 moola, no simoleans | ||
[0, 3], // Bob: no moola, 3 simoleans | ||
]; | ||
|
||
({ alice, bob } = makeVats(vats, zoe, installations, startingValues)); | ||
// Zoe appears to do some one-time setup the first time it's used, so this | ||
// is a sacrifical benchmark round to prime the pump. | ||
if (vatParameters.argv[0] === '--prime') { | ||
await E(alice).initiateSwap(bob); | ||
await E(bob).initiateSwap(alice); | ||
} | ||
}, | ||
async runBenchmarkRound() { | ||
round += 1; | ||
await E(alice).initiateSwap(bob); | ||
await E(bob).initiateSwap(alice); | ||
return `round ${round} complete`; | ||
}, | ||
}); | ||
} |
114 changes: 114 additions & 0 deletions
114
packages/swingset-runner/demo/swapBenchmark/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,114 @@ | ||
// @ts-check | ||
|
||
import { E } from '@agoric/eventual-send'; | ||
import { showPurseBalance, setupPurses } from './helpers'; | ||
import { makePrintLog } from './printLog'; | ||
|
||
import '@agoric/zoe/exported'; | ||
|
||
const log = makePrintLog(); | ||
|
||
/** | ||
* @param {string} name | ||
* @param {ZoeService} zoe | ||
* @param {Issuer[]} issuers | ||
* @param {Payment[]} payments | ||
* @param {Record<string,Installation>} installations | ||
*/ | ||
async function build(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 invitationIssuer = await E(zoe).getInvitationIssuer(); | ||
const { atomicSwap } = 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 receivePayouts(payoutsP) { | ||
const payouts = await payoutsP; | ||
const moolaPayout = await payouts.Asset; | ||
const simoleanPayout = await payouts.Price; | ||
|
||
await E(moolaPurseP).deposit(moolaPayout); | ||
await E(simoleanPurseP).deposit(simoleanPayout); | ||
} | ||
|
||
async function initiateSwap(otherP) { | ||
await preReport(); | ||
|
||
const { creatorInvitation: invitation } = await E(zoe).startInstance( | ||
atomicSwap, | ||
issuerKeywordRecord, | ||
); | ||
|
||
const sellProposal = harden({ | ||
give: { Asset: moola(1) }, | ||
want: { Price: simoleans(1) }, | ||
exit: { onDemand: null }, | ||
}); | ||
const paymentKeywordRecord = { | ||
Asset: await E(moolaPurseP).withdraw(moola(1)), | ||
}; | ||
|
||
const seatP = E(zoe).offer(invitation, sellProposal, paymentKeywordRecord); | ||
E(otherP).respondToSwap(E(seatP).getOfferResult()); | ||
const payoutsP = E(seatP).getPayouts(); | ||
|
||
await receivePayouts(payoutsP); | ||
await postReport(); | ||
} | ||
|
||
async function respondToSwap(invitation) { | ||
await preReport(); | ||
|
||
const exclInvitation = await E(invitationIssuer).claim(invitation); | ||
|
||
const buyProposal = harden({ | ||
want: { Asset: moola(1) }, | ||
give: { Price: simoleans(1) }, | ||
exit: { onDemand: null }, | ||
}); | ||
const paymentKeywordRecord = { | ||
Price: await E(simoleanPurseP).withdraw(simoleans(1)), | ||
}; | ||
|
||
const seatP = await E(zoe).offer( | ||
exclInvitation, | ||
buyProposal, | ||
paymentKeywordRecord, | ||
); | ||
await E(seatP).getOfferResult(); | ||
const payoutsP = E(seatP).getPayouts(); | ||
|
||
await receivePayouts(payoutsP); | ||
await postReport(); | ||
} | ||
|
||
return harden({ | ||
initiateSwap, | ||
respondToSwap, | ||
}); | ||
} | ||
|
||
export function buildRootObject(_vatPowers, vatParameters) { | ||
return harden({ | ||
build: (zoe, issuers, payments, installations) => | ||
build(vatParameters.name, zoe, issuers, payments, installations), | ||
}); | ||
} |
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,45 @@ | ||
import { E } from '@agoric/eventual-send'; | ||
import { makeLocalAmountMath } from '@agoric/ertp'; | ||
|
||
import '@agoric/zoe/exported'; | ||
|
||
export async function showPurseBalance(purseP, name, log) { | ||
try { | ||
const amount = await E(purseP).getCurrentAmount(); | ||
log(name, ': balance ', amount); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
|
||
/** | ||
* @param {ZoeService} zoe | ||
* @param {Issuer[]} issuers | ||
* @param {Payment[]} payments | ||
*/ | ||
export async function setupPurses(zoe, issuers, payments) { | ||
const purses = issuers.map(issuer => E(issuer).makeEmptyPurse()); | ||
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 makeLocalAmountMath(moolaIssuer); | ||
const simoleanAmountMath = await makeLocalAmountMath(simoleanIssuer); | ||
|
||
const moola = moolaAmountMath.make; | ||
const simoleans = simoleanAmountMath.make; | ||
|
||
return harden({ | ||
issuers: harden([moolaIssuer, simoleanIssuer]), | ||
moolaIssuer, | ||
simoleanIssuer, | ||
moolaAmountMath, | ||
simoleanAmountMath, | ||
moola, | ||
simoleans, | ||
purses, | ||
}); | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/swingset-runner/demo/swapBenchmark/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,21 @@ | ||
import bundleSource from '@agoric/bundle-source'; | ||
|
||
import fs from 'fs'; | ||
|
||
const CONTRACT_FILES = ['atomicSwap']; | ||
|
||
const generateBundlesP = Promise.all( | ||
CONTRACT_FILES.map(async contract => { | ||
const contractPath = require.resolve( | ||
`@agoric/zoe/src/contracts/${contract}`, | ||
); | ||
const bundle = await bundleSource(contractPath); | ||
const obj = { bundle, 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,8 @@ | ||
export function makePrintLog() { | ||
return function printLog(...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,31 @@ | ||
{ | ||
"bootstrap": "bootstrap", | ||
"bundles": { | ||
"zcf": { | ||
"sourceSpec": "@agoric/zoe/contractFacet" | ||
} | ||
}, | ||
"vats": { | ||
"alice": { | ||
"sourceSpec": "exchanger.js", | ||
"parameters": { | ||
"name": "alice" | ||
} | ||
}, | ||
"bob": { | ||
"sourceSpec": "exchanger.js", | ||
"parameters": { | ||
"name": "bob" | ||
} | ||
}, | ||
"zoe": { | ||
"sourceSpec": "vat-zoe.js", | ||
"parameters": { | ||
"zcfBundleName": "zcf" | ||
} | ||
}, | ||
"bootstrap": { | ||
"sourceSpec": "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,7 @@ | ||
import { makeZoe } from '@agoric/zoe'; | ||
|
||
export function buildRootObject(_vatPowers, vatParameters) { | ||
return harden({ | ||
buildZoe: vatAdminSvc => makeZoe(vatAdminSvc, vatParameters.zcfBundleName), | ||
}); | ||
} |