Skip to content

Commit

Permalink
fix: kickOut does not throw itself (#1663)
Browse files Browse the repository at this point in the history
* fix: kickOut does not throw itself

* refactor: don't throw if seat that was kickedOut already exited.

* chore: add tests for kicking out multiple seats in one go

* chore: change the tests slightly, remove a console.log that shouldn't be there

* chore: remove t.plan that was there unintentionally from prior tests
  • Loading branch information
katelynsills authored Sep 2, 2020
1 parent e5f5aaf commit 9985dc4
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 22 deletions.
6 changes: 1 addition & 5 deletions packages/zoe/src/contractFacet/contractFacet.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,7 @@ export function buildRootObject() {
const offerHandler = invitationHandleToHandler.get(invitationHandle);
// @ts-ignore
const offerResultP = E(offerHandler)(zcfSeat).catch(reason => {
if (!zcfSeat.hasExited()) {
throw zcfSeat.kickOut(reason);
} else {
throw reason;
}
throw zcfSeat.kickOut(reason);
});
const exitObj = makeExitObj(
seatData.proposal,
Expand Down
9 changes: 5 additions & 4 deletions packages/zoe/src/contractFacet/seat.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ export const makeZcfSeatAdminKit = (
'Kicked out of seat. Please check the log for more information.',
),
) => {
assertExitedFalse();
zcfSeatAdmin.updateHasExited();
E(zoeSeatAdmin).kickOut(harden(reason));
throw reason;
if (!exited) {
zcfSeatAdmin.updateHasExited();
E(zoeSeatAdmin).kickOut(harden(reason));
}
return reason;
},
getNotifier: () => {
return notifier;
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/src/contractFacet/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
/**
* @typedef {Object} ZCFSeat
* @property {() => void} exit
* @property {(reason?: any) => never} kickOut called with the reason this
* @property {(reason?: Error) => Error} kickOut called with the reason this
* seat is being kicked out, where reason is normally an instanceof Error.
* @property {() => Notifier<Allocation>} getNotifier
* @property {() => boolean} hasExited
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/src/contractSupport/zoeHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export const trade = (zcf, keepLeft, tryRight) => {
if (!offerSafeForRight) {
console.log(`offer not safe for right`);
}
return tryRight.seat.kickOut(
throw tryRight.seat.kickOut(
new Error(
`The trade between left ${keepLeft} and right ${tryRight} failed offer safety. Please check the log for more information`,
),
Expand Down
4 changes: 3 additions & 1 deletion packages/zoe/src/contracts/auction/secondPriceLogic.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export const calcWinnerAndClose = (zcf, sellSeat, bidSeats) => {
});

if (activeBidsCount === 0) {
sellSeat.kickOut(new Error(`Could not close auction. No bids were active`));
throw sellSeat.kickOut(
new Error(`Could not close auction. No bids were active`),
);
}

if (activeBidsCount === 1) {
Expand Down
1 change: 0 additions & 1 deletion packages/zoe/src/zoeService/zoe.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@ function makeZoe(vatAdminSvc, zcfBundleName = undefined) {
})
.catch(err => {
console.log(err);
console.log('right here');
});

return userSeat;
Expand Down
41 changes: 37 additions & 4 deletions packages/zoe/test/unitTests/contracts/test-zcf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import '@agoric/install-ses';
// eslint-disable-next-line import/no-extraneous-dependencies
import test from 'ava';

import { E } from '@agoric/eventual-send';
import bundleSource from '@agoric/bundle-source';

// noinspection ES6PreferShortImport
Expand All @@ -12,8 +13,7 @@ import fakeVatAdmin from './fakeVatAdmin';

const contractRoot = `${__dirname}/zcfTesterContract`;

test('zoe - test zcf', async t => {
t.plan(1);
test(`zoe - zcfSeat.kickOut() doesn't throw`, async t => {
const { moolaIssuer, simoleanIssuer } = setup();
const zoe = makeZoe(fakeVatAdmin);

Expand All @@ -27,7 +27,40 @@ test('zoe - test zcf', async t => {
Pixels: moolaIssuer,
Money: simoleanIssuer,
});
await t.notThrowsAsync(() =>
zoe.startInstance(installation, issuerKeywordRecord),

const { creatorFacet } = await E(zoe).startInstance(
installation,
issuerKeywordRecord,
);

// This contract gives ZCF as the contractFacet for testing purposes
/** @type ContractFacet */
const zcf = creatorFacet;

let firstSeat;

const grabSeat = seat => {
firstSeat = seat;
return 'ok';
};

const kickOutSeat = secondSeat => {
firstSeat.kickOut(new Error('kicked out first'));
throw secondSeat.kickOut(new Error('kicked out second'));
};

const invitation1 = await zcf.makeInvitation(grabSeat, 'seat1');
const invitation2 = await zcf.makeInvitation(kickOutSeat, 'seat2');

const userSeat1 = await E(zoe).offer(invitation1);
const userSeat2 = await E(zoe).offer(invitation2);

t.is(await E(userSeat1).getOfferResult(), 'ok', `userSeat1 offer result`);

t.deepEqual(await E(userSeat2).getPayouts(), {});

await t.throwsAsync(E(userSeat2).getOfferResult());
await t.throwsAsync(() => E(userSeat1).tryExit(), {
message: 'seat has been exited',
});
});
9 changes: 4 additions & 5 deletions packages/zoe/test/unitTests/contracts/zcfTesterContract.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// @ts-check

import '../../../exported';

/**
* Tests ZCF
* @type {ContractStartFn}
*/
const start = _zcf => {
// TODO: import tap/tape and do t.is
// TODO: Test ZCF here

return harden({});
const start = zcf => {
return { creatorFacet: zcf };
};

harden(start);
Expand Down

0 comments on commit 9985dc4

Please sign in to comment.