Skip to content

Commit

Permalink
feat(smart-wallet): use durable zone for vows
Browse files Browse the repository at this point in the history
- refs: #9308

Co-authored-by: Turadg Aleahmad <[email protected]>
  • Loading branch information
0xpatrickdev and turadg committed Jul 5, 2024
1 parent 193c659 commit 1126e9f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 35 deletions.
2 changes: 1 addition & 1 deletion packages/smart-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"devDependencies": {
"@agoric/cosmic-proto": "^0.4.0",
"@agoric/swingset-vat": "^0.32.2",
"@agoric/zone": "^0.2.2",
"@endo/bundle-source": "^3.2.3",
"@endo/captp": "^4.2.0",
"@endo/init": "^1.1.2",
Expand All @@ -36,6 +35,7 @@
"@agoric/vats": "^0.15.1",
"@agoric/vow": "^0.1.0",
"@agoric/zoe": "^0.26.2",
"@agoric/zone": "^0.2.2",
"@endo/eventual-send": "^1.2.2",
"@endo/far": "^1.1.2",
"@endo/marshal": "^1.5.0",
Expand Down
72 changes: 41 additions & 31 deletions packages/smart-wallet/src/offerWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ import {
} from '@agoric/zoe/src/typeGuards.js';
import { AmountShape } from '@agoric/ertp/src/typeGuards.js';
import { deeplyFulfilledObject, objectMap } from '@agoric/internal';
import { heapVowTools } from '@agoric/vow/vat.js';

import { UNPUBLISHED_RESULT } from './offers.js';

const { allVows, asPromise } = heapVowTools;

/**
* @import {OfferSpec} from "./offers.js";
* @import {ContinuingOfferResult} from "./types.js";
* @import {OfferSpec} from './offers.js';
* @import {ContinuingOfferResult} from './types.js';
* @import {Passable} from '@endo/pass-style';
* @import {PromiseWatcher} from '@agoric/swingset-liveslots';
* @import {Baggage} from '@agoric/vat-data';
* @import {Vow, VowTools} from '@agoric/vow';
*/

/**
Expand All @@ -35,17 +34,19 @@ const { allVows, asPromise } = heapVowTools;
* }} OutcomeWatchers
*/

/**
* @param {OutcomeWatchers} watchers
* @param {UserSeat} seat
* @returns {Promise<unknown>} promise for the value of the offer result, or its
* rejection
*/
const watchForOfferResult = ({ resultWatcher }, seat) => {
// NOTE: this enables upgrade of the contract, NOT upgrade of the smart-wallet. See #9308
const p = asPromise(E(seat).getOfferResult());
watchPromise(p, resultWatcher, seat);
return p;
/** @param {VowTools} vowTools */
const makeWatchForOfferResult = ({ watch }) => {
/**
* @param {OutcomeWatchers} watchers
* @param {UserSeat} seat
* @returns {Vow<void>} Vow that resolves when offer result has been processed
* by the resultWatcher
*/
const watchForOfferResult = ({ resultWatcher }, seat) => {
// Offer result may be anything, including a Vow, so watch it durably.
return watch(E(seat).getOfferResult(), resultWatcher, seat);
};
return watchForOfferResult;
};

/**
Expand All @@ -68,19 +69,24 @@ const watchForPayout = ({ paymentWatcher }, seat) => {
return p;
};

/**
* @param {OutcomeWatchers} watchers
* @param {UserSeat} seat
*/
export const watchOfferOutcomes = (watchers, seat) => {
// NOTE: this enables upgrade of the contract, NOT upgrade of the smart-wallet. See #9308
return asPromise(
allVows([
watchForOfferResult(watchers, seat),
watchForNumWants(watchers, seat),
watchForPayout(watchers, seat),
]),
);
/** @param {VowTools} vowTools */
export const makeWatchOfferOutcomes = vowTools => {
const watchForOfferResult = makeWatchForOfferResult(vowTools);
const { asPromise, allVows } = vowTools;
/**
* @param {OutcomeWatchers} watchers
* @param {UserSeat} seat
*/
const watchOfferOutcomes = (watchers, seat) => {
return asPromise(
allVows([
watchForOfferResult(watchers, seat),
watchForNumWants(watchers, seat),
watchForPayout(watchers, seat),
]),
);
};
return watchOfferOutcomes;
};

const offerWatcherGuard = harden({
Expand Down Expand Up @@ -113,9 +119,11 @@ const offerWatcherGuard = harden({
});

/**
* @param {import('@agoric/vat-data').Baggage} baggage
* @param {Baggage} baggage
* @param {VowTools} vowTools
*/
export const prepareOfferWatcher = baggage => {
export const prepareOfferWatcher = (baggage, vowTools) => {
const watchForOfferResult = makeWatchForOfferResult(vowTools);
return prepareExoClassKit(
baggage,
'OfferWatcher',
Expand Down Expand Up @@ -276,6 +284,8 @@ export const prepareOfferWatcher = baggage => {
} else {
facets.helper.handleError(reason);
}
// throw so the vow watcher propagates the rejection
throw reason;
},
},

Expand Down
11 changes: 8 additions & 3 deletions packages/smart-wallet/src/smartWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ import {
AmountKeywordRecordShape,
PaymentPKeywordRecordShape,
} from '@agoric/zoe/src/typeGuards.js';
import { prepareVowTools } from '@agoric/vow';
import { makeDurableZone } from '@agoric/zone/durable.js';

import { makeInvitationsHelper } from './invitations.js';
import { shape } from './typeGuards.js';
import { objectMapStoragePath } from './utils.js';
import { prepareOfferWatcher, watchOfferOutcomes } from './offerWatcher.js';
import { prepareOfferWatcher, makeWatchOfferOutcomes } from './offerWatcher.js';

/** @import {OfferId, OfferStatus} from './offers.js'; */

Expand Down Expand Up @@ -285,7 +287,7 @@ export const prepareSmartWallet = (baggage, shared) => {
secretWalletFactoryKey: M.any(),
}),
);

const zone = makeDurableZone(baggage);
const makeRecorderKit = prepareRecorderKit(baggage, shared.publicMarshaller);

const walletPurses = provide(baggage, BRAND_TO_PURSES_KEY, () => {
Expand All @@ -297,7 +299,10 @@ export const prepareSmartWallet = (baggage, shared) => {
return store;
});

const makeOfferWatcher = prepareOfferWatcher(baggage);
const vowTools = prepareVowTools(zone.subZone('vow'));

const makeOfferWatcher = prepareOfferWatcher(baggage, vowTools);
const watchOfferOutcomes = makeWatchOfferOutcomes(vowTools);

const updateShape = {
value: AmountShape,
Expand Down

0 comments on commit 1126e9f

Please sign in to comment.