Skip to content

Commit

Permalink
chore: add error handling for bad timers and deadlines when making se…
Browse files Browse the repository at this point in the history
…ats (#1564)

* chore: add error handling for bad timers and deadlines when making seats
  • Loading branch information
katelynsills authored Aug 20, 2020
1 parent 941cc1f commit c57e46b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/zoe/src/contractFacet/exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,22 @@ export const makeExitObj = (proposal, zoeSeatAdmin, zcfSeatAdmin) => {

if (exitKind === 'afterDeadline') {
// Automatically exit the seat after deadline.
E(proposal.exit.afterDeadline.timer).setWakeup(
proposal.exit.afterDeadline.deadline,
harden({
wake: exitFn,
}),
);
E(proposal.exit.afterDeadline.timer)
.setWakeup(
proposal.exit.afterDeadline.deadline,
harden({
wake: exitFn,
}),
)
.catch(reason => {
console.error(
`The seat could not be made with the provided timer ${proposal.exit.afterDeadline.timer} and deadline ${proposal.exit.afterDeadline.deadline}`,
);
console.error(reason);
zcfSeatAdmin.updateHasExited();
E(zoeSeatAdmin).kickOut(reason);
throw reason;
});
} else if (exitKind === 'onDemand') {
// Allow the user to exit their seat on demand. Note: we must wrap
// it in an object to send it back to Zoe because our marshalling layer
Expand Down
17 changes: 17 additions & 0 deletions packages/zoe/test/swingsetTests/zoe/test-zoe.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,20 @@ test('zoe - sellTickets - valid inputs', async t => {
const dump = await main(['sellTicketsOk', startingValues]);
t.deepEquals(dump.log, expectedSellTicketsOkLog);
});

const expectedBadTimerLog = [
'=> alice, bob, carol and dave are set up',
'=> alice.doBadTimer called',
'is a zoe invitation: true',
'aliceMoolaPurse: balance {"brand":{},"value":3}',
'aliceSimoleanPurse: balance {"brand":{},"value":0}',
];
test('zoe - bad timer', async t => {
t.plan(1);
const startingValues = [
[3, 0, 0],
[0, 0, 0],
];
const dump = await main(['badTimer', startingValues]);
t.deepEquals(dump.log, expectedBadTimerLog);
});
45 changes: 45 additions & 0 deletions packages/zoe/test/swingsetTests/zoe/vat-alice.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,48 @@ const build = async (log, zoe, issuers, payments, installations, timer) => {
log('alice earned: ', currentPurseBalance);
};

const doBadTimer = async () => {
log(`=> alice.doBadTimer called`);
const installation = installations.coveredCall;
const issuerKeywordRecord = harden({
UnderlyingAsset: moolaIssuer,
StrikePrice: simoleanIssuer,
});
const { creatorInvitation: writeCallInvitation } = await E(
zoe,
).startInstance(installation, issuerKeywordRecord);

const proposal = harden({
give: { UnderlyingAsset: moola(3) },
want: { StrikePrice: simoleans(7) },
exit: { afterDeadline: { timer: {}, deadline: 1 } },
});

const paymentKeywordRecord = { UnderlyingAsset: moolaPayment };

const seatP = await E(zoe).offer(
writeCallInvitation,
proposal,
paymentKeywordRecord,
);

// Bad timer error is logged but does not cause seat methods to
// throw.
const callOption = await E(seatP).getOfferResult();
const invitationIssuer = await E(zoe).getInvitationIssuer();
const isInvitation = await E(invitationIssuer).isLive(callOption);
log(`is a zoe invitation: ${isInvitation}`);

// The seat is automatically exited
const moolaPayout = await E(seatP).getPayout('UnderlyingAsset');
const simoleanPayout = await E(seatP).getPayout('StrikePrice');
await E(moolaPurseP).deposit(moolaPayout);
await E(simoleanPurseP).deposit(simoleanPayout);

await showPurseBalance(moolaPurseP, 'aliceMoolaPurse', log);
await showPurseBalance(simoleanPurseP, 'aliceSimoleanPurse', log);
};

return harden({
startTest: async (testName, bobP, carolP, daveP) => {
switch (testName) {
Expand Down Expand Up @@ -446,6 +488,9 @@ const build = async (log, zoe, issuers, payments, installations, timer) => {
case 'sellTicketsOk': {
return doSellTickets(bobP, carolP, daveP);
}
case 'badTimer': {
return doBadTimer();
}
default: {
throw new Error(`testName ${testName} not recognized`);
}
Expand Down

0 comments on commit c57e46b

Please sign in to comment.