Skip to content

Commit

Permalink
fix(pegasus): only reject transfers waiting for pegRemote
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Feb 20, 2022
1 parent aacf1c3 commit f1801f6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
6 changes: 3 additions & 3 deletions packages/pegasus/src/courier.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const getCourierPK = (key, keyToCourierPK) => {
* @property {ContractFacet} zcf
* @property {ERef<BoardDepositFacet>} board
* @property {ERef<NameHub>} namesByAddress
* @property {Denom} remoteDenom
* @property {Denom} sendDenom
* @property {Brand} localBrand
* @property {(zcfSeat: ZCFSeat, amounts: AmountKeywordRecord) => void} retain
* @property {(zcfSeat: ZCFSeat, amounts: AmountKeywordRecord) => void} redeem
Expand All @@ -47,7 +47,7 @@ export const makeCourierMaker =
zcf,
board,
namesByAddress,
remoteDenom,
sendDenom,
localBrand,
retain,
redeem,
Expand All @@ -59,7 +59,7 @@ export const makeCourierMaker =
const amount = zcfSeat.getAmountAllocated('Transfer', localBrand);
const transferPacket = await E(transferProtocol).makeTransferPacket({
value: amount.value,
remoteDenom,
remoteDenom: sendDenom,
depositAddress,
});

Expand Down
24 changes: 20 additions & 4 deletions packages/pegasus/src/pegasus.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,34 @@ const makePegasus = (zcf, board, namesByAddress) => {

/** @type {PegasusConnectionActions} */
const pegasusConnectionActions = Far('pegasusConnectionActions', {
async rejectStuckTransfers(receiveDenom) {
async rejectTransfersWaitingForPegRemote(remoteDenom) {
checkAbort();
const { receiveDenomToCourierPK } = localDenomState;

const { receiveDenom } = await E(
denomTransformer,
).getDenomsForRemotePeg(
remoteDenom,
localDenomState.localAddr,
localDenomState.remoteAddr,
);
checkAbort();

const { reject, promise } = receiveDenomToCourierPK.get(receiveDenom);

// Only continue if the promise isn't already resolved.
const raceWinner = await Promise.race([promise, null]);
checkAbort();

assert(!raceWinner, X`${receiveDenom} is already resolved`);

// If rejected, the rejection is returned to our caller, so we have
// handled it correctly and that flow doesn't need to trigger an
// additional UnhandledRejectionWarning in our vat.
promise.catch(() => {});
reject(assert.error(X`${receiveDenom} is temporarily unavailable`));

// Allow new transfers to be initiated.
// Allow new transfers to be initiated after this rejection.
receiveDenomToCourierPK.delete(receiveDenom);
},
async pegRemote(
Expand Down Expand Up @@ -165,7 +181,7 @@ const makePegasus = (zcf, board, namesByAddress) => {
localBrand,
board,
namesByAddress,
remoteDenom: sendDenom,
sendDenom,
retain: (zcfSeat, amounts) =>
zcfMint.burnLosses(harden(amounts), zcfSeat),
redeem: (zcfSeat, amounts) => {
Expand Down Expand Up @@ -241,7 +257,7 @@ const makePegasus = (zcf, board, namesByAddress) => {
zcf,
board,
namesByAddress,
remoteDenom: sendDenom,
sendDenom,
localBrand,
retain: (transferSeat, amounts) =>
transferAmountFrom(
Expand Down
4 changes: 2 additions & 2 deletions packages/pegasus/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
*/

/**
* @callback RejectStuckTransfers
* @callback RejectTransfersWaitingForPegRemote
* Abort any in-progress receiveDenom transfers if there has not yet been a
* pegRemote or pegLocal corresponding to it.
*
Expand Down Expand Up @@ -93,7 +93,7 @@
* @typedef {Object} PegasusConnectionActions
* @property {PegLocal} pegLocal
* @property {PegRemote} pegRemote
* @property {RejectStuckTransfers} rejectStuckTransfers
* @property {RejectTransfersWaitingForPegRemote} rejectTransfersWaitingForPegRemote
* @property {(reason?: any) => void} abort
*/

Expand Down
2 changes: 1 addition & 1 deletion packages/pegasus/test/test-peg.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ async function testRemotePeg(t) {

// Wait for the packet to go through.
t.deepEqual(await remoteDenomAit.next(), { done: false, value: 'umuon' });
E(pegConnActions).rejectStuckTransfers('umuon');
E(pegConnActions).rejectTransfersWaitingForPegRemote('umuon');

const sendAckData3 = await sendAckData3P;
const sendAck3 = JSON.parse(sendAckData3);
Expand Down

0 comments on commit f1801f6

Please sign in to comment.