From 575a0a20c0960f8b64be278f9d35c8834ad4a1e3 Mon Sep 17 00:00:00 2001 From: "Mark S. Miller" Date: Thu, 6 Jul 2023 19:16:15 -0700 Subject: [PATCH] feat(ERTP): revoke used up payments --- packages/ERTP/package.json | 1 + packages/ERTP/src/payment.js | 17 +++++++++++++++-- packages/ERTP/src/paymentLedger.js | 9 ++++++++- packages/ERTP/test/unitTests/test-recovery.js | 9 +++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/packages/ERTP/package.json b/packages/ERTP/package.json index 301dfb70ad2..9563a2b5b9a 100644 --- a/packages/ERTP/package.json +++ b/packages/ERTP/package.json @@ -53,6 +53,7 @@ "devDependencies": { "@agoric/swingset-vat": "^0.32.2", "@endo/bundle-source": "^3.0.1", + "@endo/exo": "^1.0.1", "@fast-check/ava": "^1.1.5", "ava": "^5.3.0", "tsd": "^0.28.1" diff --git a/packages/ERTP/src/payment.js b/packages/ERTP/src/payment.js index 809be8cc965..0b1c35c2eb3 100644 --- a/packages/ERTP/src/payment.js +++ b/packages/ERTP/src/payment.js @@ -16,9 +16,13 @@ import { prepareExoClass } from '@agoric/vat-data'; * @param {string} name * @param {Brand} brand * @param {InterfaceGuard} PaymentI - * @returns {() => Payment} + * @returns {{ + * makePayment: () => Payment, + * revokePayment: (payment: Payment) => boolean + * }} */ export const preparePaymentKind = (issuerBaggage, name, brand, PaymentI) => { + let revokePayment; const makePayment = prepareExoClass( issuerBaggage, `${name} payment`, @@ -29,7 +33,16 @@ export const preparePaymentKind = (issuerBaggage, name, brand, PaymentI) => { return brand; }, }, + { + receiveRevoker(revoke) { + revokePayment = revoke; + }, + }, ); - return makePayment; + assert(revokePayment !== undefined); + return harden({ + makePayment, + revokePayment, + }); }; harden(preparePaymentKind); diff --git a/packages/ERTP/src/paymentLedger.js b/packages/ERTP/src/paymentLedger.js index b11ecc925a7..65418356ba0 100644 --- a/packages/ERTP/src/paymentLedger.js +++ b/packages/ERTP/src/paymentLedger.js @@ -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 => { @@ -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} */ diff --git a/packages/ERTP/test/unitTests/test-recovery.js b/packages/ERTP/test/unitTests/test-recovery.js index 3ed6ef326a7..aeb13ab746d 100644 --- a/packages/ERTP/test/unitTests/test-recovery.js +++ b/packages/ERTP/test/unitTests/test-recovery.js @@ -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'; @@ -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]"', + }); });