Skip to content

Commit

Permalink
fix: virtualize pool with vpool facet
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Mar 21, 2022
1 parent 45723b4 commit 7164f83
Show file tree
Hide file tree
Showing 9 changed files with 328 additions and 249 deletions.
2 changes: 1 addition & 1 deletion packages/ERTP/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
"@agoric/nat": "^4.1.0",
"@agoric/notifier": "^0.3.35",
"@agoric/store": "^0.6.10",
"@agoric/swingset-vat": "^0.25.1",
"@endo/marshal": "^0.6.3",
"@endo/promise-kit": "^0.2.37"
},
"devDependencies": {
"@agoric/swingset-vat": "^0.25.1",
"@endo/bundle-source": "^2.1.1",
"ava": "^3.12.1",
"fast-check": "^2.21.0"
Expand Down
18 changes: 12 additions & 6 deletions packages/ERTP/src/payment.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
// @ts-check

import { Far } from '@endo/marshal';
import { defineKind } from '@agoric/swingset-vat/src/storeModule.js';

/**
* @template {AssetKind} K
* @param {string} allegedName
* @param {Brand<K>} brand
* @returns {Payment<K>}
* @returns {() => Payment<K>}
*/
export const makePayment = (allegedName, brand) => {
return Far(`${allegedName} payment`, {
getAllegedBrand: () => brand,
});
export const makePaymentMaker = (allegedName, brand) => {
const makePayment = defineKind(
`${allegedName} payment`,
() => ({}),
() => ({
getAllegedBrand: () => brand,
}),
);
return makePayment;
};
harden(makePaymentMaker);
28 changes: 18 additions & 10 deletions packages/ERTP/src/paymentLedger.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { assert, details as X } from '@agoric/assert';
import { E } from '@endo/eventual-send';
import { isPromise } from '@endo/promise-kit';
import { Far, assertCopyArray } from '@endo/marshal';
import { makeWeakStore, fit } from '@agoric/store';

import { fit } from '@agoric/store';
import { makeScalarBigWeakMapStore } from '@agoric/swingset-vat/src/storeModule.js';
import { AmountMath } from './amountMath.js';
import { makePayment } from './payment.js';
import { makePurse } from './purse.js';
import { makePaymentMaker } from './payment.js';
import { makePurseMaker } from './purse.js';

import '@agoric/store/exported.js';

Expand All @@ -31,6 +31,8 @@ export const makePaymentLedger = (
displayInfo,
optShutdownWithFailure = undefined,
) => {
const makePayment = makePaymentMaker(allegedName, brand);

/** @type {ShutdownWithFailure} */
const shutdownLedgerWithFailure = reason => {
// TODO This should also destroy ledger state.
Expand All @@ -47,7 +49,7 @@ export const makePaymentLedger = (
};

/** @type {WeakStore<Payment, Amount>} */
const paymentLedger = makeWeakStore('payment');
const paymentLedger = makeScalarBigWeakMapStore('payment');

/** @type {(left: Amount, right: Amount) => Amount } */
const add = (left, right) => AmountMath.add(left, right, brand);
Expand Down Expand Up @@ -142,7 +144,7 @@ export const makePaymentLedger = (
payments.forEach(payment => paymentLedger.delete(payment));

newPayments = newPaymentBalances.map(balance => {
const newPayment = makePayment(allegedName, brand);
const newPayment = makePayment();
paymentLedger.init(newPayment, balance);
return newPayment;
});
Expand Down Expand Up @@ -257,7 +259,7 @@ export const makePaymentLedger = (
*/
const mintPayment = newAmount => {
newAmount = coerce(newAmount);
const payment = makePayment(allegedName, brand);
const payment = makePayment();
paymentLedger.init(payment, newAmount);
return payment;
};
Expand Down Expand Up @@ -319,7 +321,7 @@ export const makePaymentLedger = (
);
const newPurseBalance = subtract(currentBalance, amount);

const payment = makePayment(allegedName, brand);
const payment = makePayment();
try {
// COMMIT POINT Move the withdrawn assets from this purse into
// payment. Total assets must remain conserved.
Expand All @@ -337,6 +339,13 @@ export const makePaymentLedger = (
withdraw,
};

const makeEmptyPurse = makePurseMaker(
allegedName,
assetKind,
brand,
purseMethods,
);

/** @type {Issuer<K>} */
const issuer = Far(`${allegedName} issuer`, {
isLive,
Expand All @@ -350,8 +359,7 @@ export const makePaymentLedger = (
getAllegedName: () => allegedName,
getAssetKind: () => assetKind,
getDisplayInfo: () => displayInfo,
makeEmptyPurse: () =>
makePurse(allegedName, assetKind, brand, purseMethods),
makeEmptyPurse,
});

/** @type {Mint<K>} */
Expand Down
85 changes: 51 additions & 34 deletions packages/ERTP/src/purse.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,60 @@
import { makeNotifierKit } from '@agoric/notifier';
import { Far } from '@endo/marshal';
import { defineKind } from '@agoric/swingset-vat/src/storeModule.js';
import { AmountMath } from './amountMath.js';

export const makePurse = (allegedName, assetKind, brand, purseMethods) => {
let currentBalance = AmountMath.makeEmpty(brand, assetKind);
export const makePurseMaker = (allegedName, assetKind, brand, purseMethods) => {
const makePurseKit = defineKind(
allegedName,
() => {
const currentBalance = AmountMath.makeEmpty(brand, assetKind);

/** @type {NotifierRecord<Amount>} */
const { notifier: balanceNotifier, updater: balanceUpdater } =
makeNotifierKit(currentBalance);
/** @type {NotifierRecord<Amount>} */
const { notifier: balanceNotifier, updater: balanceUpdater } =
makeNotifierKit(currentBalance);

const updatePurseBalance = newPurseBalance => {
currentBalance = newPurseBalance;
balanceUpdater.updateState(currentBalance);
};

/** @type {Purse} */
const purse = Far(`${allegedName} purse`, {
deposit: (srcPayment, optAmountShape = undefined) => {
// Note COMMIT POINT within deposit.
return purseMethods.deposit(
return {
currentBalance,
updatePurseBalance,
srcPayment,
optAmountShape,
);
balanceNotifier,
balanceUpdater,
};
},
withdraw: amount =>
// Note COMMIT POINT within withdraw.
purseMethods.withdraw(currentBalance, updatePurseBalance, amount),
getCurrentAmount: () => currentBalance,
getCurrentAmountNotifier: () => balanceNotifier,
getAllegedBrand: () => brand,
// eslint-disable-next-line no-use-before-define
getDepositFacet: () => depositFacet,
});

const depositFacet = Far(`${allegedName} depositFacet`, {
receive: purse.deposit,
});
state => {
const { balanceNotifier, balanceUpdater } = state;
const updatePurseBalance = newPurseBalance => {
state.currentBalance = newPurseBalance;
balanceUpdater.updateState(state.currentBalance);
};

return purse;
/** @type {Purse} */
const purse = {
deposit: (srcPayment, optAmountShape = undefined) => {
// Note COMMIT POINT within deposit.
return purseMethods.deposit(
state.currentBalance,
updatePurseBalance,
srcPayment,
optAmountShape,
);
},
withdraw: amount =>
// Note COMMIT POINT within withdraw.
purseMethods.withdraw(
state.currentBalance,
updatePurseBalance,
amount,
),
getCurrentAmount: () => state.currentBalance,
getCurrentAmountNotifier: () => balanceNotifier,
getAllegedBrand: () => brand,
// eslint-disable-next-line no-use-before-define
getDepositFacet: () => depositFacet,
};
const depositFacet = {
receive: purse.deposit,
};
return { purse, depositFacet };
},
);
return () => makePurseKit().purse;
};
harden(makePurseMaker);
2 changes: 1 addition & 1 deletion packages/ERTP/test/unitTests/test-issuerObj.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ test('issuer.combine bad payments', async t => {
await t.throwsAsync(
() => E(issuer).combine(payments),
{
message: '"payment" not found: "[Alleged: other fungible payment]"',
message: /.* "\[Alleged: other fungible payment\]"/,
},
'payment from other mint is not found',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ test('Invitation', async t => {
t.is(paramManager.getAmount('Amt'), drachmaAmount);
const invitationActualAmount =
paramManager.getInvitationAmount('Invite').value;
t.is(invitationActualAmount, invitationAmount.value);
t.deepEqual(invitationActualAmount, invitationAmount.value);
// @ts-ignore invitationActualAmount's type is unknown
t.is(invitationActualAmount[0].description, 'simple');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ test('Invitation', async t => {
t.is(paramManager.getCurrency(), drachmaBrand);
t.is(paramManager.getAmt(), drachmaAmount);
const invitationActualAmount = paramManager.getInvite().value;
t.is(invitationActualAmount, invitationAmount.value);
t.deepEqual(invitationActualAmount, invitationAmount.value);
t.is(invitationActualAmount[0].description, 'simple');

t.is(await paramManager.getInternalParamValue('Invite'), invitation);
Expand Down
Loading

0 comments on commit 7164f83

Please sign in to comment.