Skip to content

Commit

Permalink
feat(ERTP): revoke used up payments
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Jul 31, 2023
1 parent 361e46e commit b4a3449
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/ERTP/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
},
"devDependencies": {
"@endo/bundle-source": "^2.5.2",
"@endo/exo": "^0.2.3",
"@fast-check/ava": "^1.1.5",
"ava": "^5.3.0",
"tsd": "^0.28.1"
Expand Down
17 changes: 15 additions & 2 deletions packages/ERTP/src/payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import { prepareExoClass } from '@agoric/vat-data';
* @param {string} name
* @param {Brand<K>} brand
* @param {InterfaceGuard} PaymentI
* @returns {() => Payment<K>}
* @returns {{
* makePayment: () => Payment<K>,
* revokePayment: (payment: Payment<K>) => boolean
* }}
*/
export const preparePaymentKind = (issuerBaggage, name, brand, PaymentI) => {
let revokePayment;
const makePayment = prepareExoClass(
issuerBaggage,
`${name} payment`,
Expand All @@ -24,7 +28,16 @@ export const preparePaymentKind = (issuerBaggage, name, brand, PaymentI) => {
return brand;
},
},
{
getRevoker(revoke) {
revokePayment = revoke;
},
},
);
return makePayment;
assert(revokePayment !== undefined);
return harden({
makePayment,
revokePayment,
});
};
harden(preparePaymentKind);
9 changes: 8 additions & 1 deletion packages/ERTP/src/paymentLedger.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,12 @@ export const preparePaymentLedger = (
amountShape,
);

const makePayment = preparePaymentKind(issuerBaggage, name, brand, PaymentI);
const { makePayment, revokePayment } = preparePaymentKind(
issuerBaggage,
name,
brand,
PaymentI,
);

/** @type {ShutdownWithFailure} */
const shutdownLedgerWithFailure = reason => {
Expand Down Expand Up @@ -198,6 +203,8 @@ export const preparePaymentLedger = (
paymentRecoverySets.delete(payment);
recoverySet.delete(payment);
}
// @ts-expect-error The usual type param confusion
revokePayment(payment);
};

/** @type {(left: Amount, right: Amount) => Amount} */
Expand Down
9 changes: 9 additions & 0 deletions packages/ERTP/test/unitTests/test-recovery.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava.js';
import { GET_INTERFACE_GUARD } from '@endo/exo';
import { getCopySetKeys, keyEQ, makeCopySet } from '@agoric/store';

import { makeIssuerKit, AmountMath } from '../../src/index.js';
Expand Down Expand Up @@ -67,4 +68,12 @@ test('payment recovery from mint recovery set', async t => {
t.throws(() => bobPurse.deposit(payment2), {
message: /was not a live payment for brand/,
});
t.throws(() => payment2.getAllegedBrand(), {
message:
'"In \\"getAllegedBrand\\" method of (precious payment)" may only be applied to a valid instance: "[Alleged: precious payment]"',
});
t.throws(() => payment2[GET_INTERFACE_GUARD](), {
message:
'"In \\"[Symbol(getInterfaceGuard)]\\" method of (precious payment)" may only be applied to a valid instance: "[Alleged: precious payment]"',
});
});

0 comments on commit b4a3449

Please sign in to comment.