Skip to content

Commit

Permalink
refactor(vat-data): use fewer deprecated things (#6925)
Browse files Browse the repository at this point in the history
  • Loading branch information
erights authored Feb 6, 2023
1 parent 87c3046 commit bcb2ff8
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 90 deletions.
20 changes: 9 additions & 11 deletions packages/inter-protocol/src/econCommitteeCharter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,15 @@ export const INVITATION_MAKERS_DESC = 'charter member invitation';
* @property {Record<string, unknown>} params
* @property {{paramPath: { key: string }}} [path]
*/
const ParamChangesOfferArgsShape = harden(
M.split(
{
deadline: TimestampShape,
instance: InstanceHandleShape,
params: M.recordOf(M.string(), M.any()),
},
M.partial({
path: { paramPath: { key: M.any() } },
}),
),
const ParamChangesOfferArgsShape = M.splitRecord(
{
deadline: TimestampShape,
instance: InstanceHandleShape,
params: M.recordOf(M.string(), M.any()),
},
{
path: { paramPath: { key: M.any() } },
},
);

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/inter-protocol/src/stakeFactory/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
* @typedef AttestationTool
* @property {(amountToLien: Amount<'nat'>) => Promise<Payment>} makeAttestation
* @property {() => Promise<AccountState>} getAccountState
* @property {() => Promise<Invitation>} makeReturnAttInvitation Make an invitation for returning an attestation.
* @property {(lienedAmount: Amount<'nat'>) => Amount<'copyBag'>} wrapLienedAmount
* @property {(attAmount: Amount<'copyBag'>) => Amount<'nat'>} unwrapLienedAmount
* @property {() => Promise<Invitation>} makeReturnAttInvitation Make an invitation for returning an attestation.
*/

/**
Expand Down
43 changes: 41 additions & 2 deletions packages/vat-data/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
/// <reference types="ses"/>
export * from './vat-data-bindings.js';
export * from './kind-utils.js';
export {
M,
makeScalarMapStore,
makeScalarWeakMapStore,
makeScalarSetStore,
makeScalarWeakSetStore,
} from '@agoric/store';
export {
makeKindHandle,
providePromiseWatcher,
watchPromise,
makeScalarBigMapStore,
makeScalarBigWeakMapStore,
makeScalarBigSetStore,
makeScalarBigWeakSetStore,
canBeDurable,
pickFacet,
partialAssign,
provide,
provideDurableMapStore,
provideDurableWeakMapStore,
provideDurableSetStore,
// deprecated
defineKind,
defineKindMulti,
defineDurableKind,
defineDurableKindMulti,
} from './vat-data-bindings.js';
export {
defineVirtualExoClass,
defineVirtualExoClassKit,
Expand All @@ -9,7 +35,20 @@ export {
prepareExoClass,
prepareExoClassKit,
prepareExo,
// deorecated
prepareSingleton,
} from './exo-utils.js';

/** @typedef {import('./types.js').Baggage} Baggage */
/** @typedef {import('./types.js').DurableKindHandle} DurableKindHandle */
/** @template T @typedef {import('./types.js').DefineKindOptions<T>} DefineKindOptions */

// //////////////////////////// deprecated /////////////////////////////////////

export {
// deprecated
ignoreContext,
provideKindHandle,
prepareKind,
prepareKindMulti,
} from './kind-utils.js';
17 changes: 1 addition & 16 deletions packages/vat-data/src/vat-data-bindings.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
/* global globalThis */

import { assert } from '@agoric/assert';
import {
M,
makeScalarMapStore,
makeScalarWeakMapStore,
makeScalarSetStore,
makeScalarWeakSetStore,
provideLazy,
} from '@agoric/store';

export {
M,
makeScalarMapStore,
makeScalarWeakMapStore,
makeScalarSetStore,
makeScalarWeakSetStore,
};
import { provideLazy } from '@agoric/store';

/** @type {import('./types').VatData} */
let VatDataGlobal;
Expand Down
70 changes: 44 additions & 26 deletions packages/zoe/src/contractFacet/zcfSeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
provideDurableMapStore,
provideDurableWeakMapStore,
prepareExoClassKit,
prepareKind,
provide,
prepareExoClass,
} from '@agoric/vat-data';
import { E } from '@endo/eventual-send';
import { AmountMath } from '@agoric/ertp';
Expand All @@ -17,7 +17,7 @@ import { addToAllocation, subtractFromAllocation } from './allocationMath.js';
import { coerceAmountKeywordRecord } from '../cleanProposal.js';
import {
AmountKeywordRecordShape,
ProposalShape,
SeatDataShape,
SeatShape,
} from '../typeGuards.js';

Expand Down Expand Up @@ -135,29 +135,37 @@ export const createSeatManager = (
}
};

const makeZCFSeatInternal = prepareKind(
const ZCFSeatI = M.interface('ZCFSeat', {}, { sloppy: true });

const makeZCFSeatInternal = prepareExoClass(
zcfBaggage,
'zcfSeat',
ZCFSeatI,
proposal => ({ proposal }),
{
getSubscriber: ({ self }) => {
getSubscriber() {
const { self } = this;
return E(zoeInstanceAdmin).getExitSubscriber(
zcfSeatToSeatHandle.get(self),
);
},
getProposal: ({ state }) => state.proposal,
exit: ({ self }, completion) => {
getProposal() {
const { state } = this;
return state.proposal;
},
exit(completion) {
const { self } = this;
assertActive(self);
assertNoStagedAllocation(self);
doExitSeat(self);
E(zoeInstanceAdmin).exitSeat(zcfSeatToSeatHandle.get(self), completion);
},
fail: (
{ self },
fail(
reason = new Error(
'Seat exited with failure. Please check the log for more information.',
),
) => {
) {
const { self } = this;
if (typeof reason === 'string') {
reason = Error(reason);
assert.note(
Expand All @@ -174,9 +182,13 @@ export const createSeatManager = (
}
return reason;
},
hasExited: ({ self }) => hasExited(self),
hasExited() {
const { self } = this;
return hasExited(self);
},

getAmountAllocated: ({ self }, keyword, brand) => {
getAmountAllocated(keyword, brand) {
const { self } = this;
assertActive(self);
const currentAllocation = getCurrentAllocation(self);
if (currentAllocation[keyword] !== undefined) {
Expand All @@ -189,9 +201,16 @@ export const createSeatManager = (
const assetKind = getAssetKindByBrand(brand);
return AmountMath.makeEmpty(brand, assetKind);
},
getCurrentAllocation: ({ self }) => getCurrentAllocation(self),
getStagedAllocation: ({ self }) => getStagedAllocation(self),
isOfferSafe: ({ state, self }, newAllocation) => {
getCurrentAllocation() {
const { self } = this;
return getCurrentAllocation(self);
},
getStagedAllocation() {
const { self } = this;
return getStagedAllocation(self);
},
isOfferSafe(newAllocation) {
const { state, self } = this;
assertActive(self);
const currentAllocation = getCurrentAllocation(self);
const reallocation = harden({
Expand All @@ -201,7 +220,8 @@ export const createSeatManager = (

return isOfferSafe(state.proposal, reallocation);
},
incrementBy: ({ self }, amountKeywordRecord) => {
incrementBy(amountKeywordRecord) {
const { self } = this;
assertActive(self);
amountKeywordRecord = coerceAmountKeywordRecord(
amountKeywordRecord,
Expand All @@ -213,7 +233,8 @@ export const createSeatManager = (
);
return amountKeywordRecord;
},
decrementBy: ({ self }, amountKeywordRecord) => {
decrementBy(amountKeywordRecord) {
const { self } = this;
assertActive(self);
amountKeywordRecord = coerceAmountKeywordRecord(
amountKeywordRecord,
Expand All @@ -228,12 +249,16 @@ export const createSeatManager = (
);
return amountKeywordRecord;
},
clear: ({ self }) => {
clear() {
const { self } = this;
if (zcfSeatToStagedAllocations.has(self)) {
zcfSeatToStagedAllocations.delete(self);
}
},
hasStagedAllocation: ({ self }) => hasStagedAllocation(self),
hasStagedAllocation() {
const { self } = this;
return hasStagedAllocation(self);
},
},
);

Expand All @@ -245,14 +270,7 @@ export const createSeatManager = (

const ZcfSeatManagerIKit = harden({
seatManager: M.interface('ZcfSeatManager', {
makeZCFSeat: M.call(
M.partial({
proposal: ProposalShape,
initialAllocation: AmountKeywordRecordShape,
seatHandle: SeatShape,
offerArgs: M.any(),
}),
).returns(M.remotable('zcfSeat')),
makeZCFSeat: M.call(SeatDataShape).returns(M.remotable('zcfSeat')),
reallocate: M.call(M.remotable('zcfSeat'), M.remotable('zcfSeat'))
.rest(M.arrayOf(M.remotable('zcfSeat')))
.returns(),
Expand Down
10 changes: 6 additions & 4 deletions packages/zoe/src/contractFacet/zcfZygote.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
makeScalarBigMapStore,
provideDurableMapStore,
prepareExo,
prepareKind,
prepareExoClass,
} from '@agoric/vat-data';

import { cleanProposal } from '../cleanProposal.js';
Expand All @@ -27,7 +27,7 @@ import '../internal-types.js';
import './internal-types.js';

import '@agoric/swingset-vat/src/types-ambient.js';
import { InvitationHandleShape } from '../typeGuards.js';
import { HandleOfferI, InvitationHandleShape } from '../typeGuards.js';

const { Fail } = assert;

Expand Down Expand Up @@ -154,12 +154,14 @@ export const makeZCFZygote = async (

// handleOfferObject gives Zoe the ability to notify ZCF when a new seat is
// added in offer(). ZCF responds with the exitObj and offerResult.
const makeHandleOfferObj = prepareKind(
const makeHandleOfferObj = prepareExoClass(
zcfBaggage,
'handleOfferObj',
HandleOfferI,
offerHandlerTaker => ({ offerHandlerTaker }),
{
handleOffer: ({ state }, invitationHandle, seatData) => {
handleOffer(invitationHandle, seatData) {
const { state } = this;
const zcfSeat = seatManager.makeZCFSeat(seatData);
// TODO: provide a details that's a better diagnostic for the
// ephemeral offerHandler that did not survive upgrade.
Expand Down
27 changes: 21 additions & 6 deletions packages/zoe/src/contracts/coveredCall-durable.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mustMatch, M } from '@agoric/store';
import { prepareKind, prepareSingleton } from '@agoric/vat-data';
import { prepareExo, prepareExoClass } from '@agoric/vat-data';
import { swapExact } from '../contractSupport/index.js';
import { isAfterDeadlineExitRule } from '../typeGuards.js';
import { isAfterDeadlineExitRule, OfferHandlerI } from '../typeGuards.js';

const { Fail } = assert;

Expand Down Expand Up @@ -32,12 +32,16 @@ const start = async (zcf, _privateArgs, instanceBaggage) => {
// XXX the exerciseOption offer handler that this makes is an object rather
// than a function for now only because we do not yet support durable
// functions.
const makeExerciser = prepareKind(
const makeExerciser = prepareExoClass(
instanceBaggage,
'makeExerciserKindHandle',
OfferHandlerI,
sellSeat => ({ sellSeat }),
{
handle: ({ state: { sellSeat } }, buySeat) => {
handle(buySeat) {
const {
state: { sellSeat },
} = this;
assert(!sellSeat.hasExited(), sellSeatExpiredMsg);
try {
swapExact(zcf, sellSeat, buySeat);
Expand Down Expand Up @@ -78,9 +82,20 @@ const start = async (zcf, _privateArgs, instanceBaggage) => {
return zcf.makeInvitation(exerciseOption, 'exerciseOption', customProps);
};

const creatorFacet = prepareSingleton(instanceBaggage, 'creatorFacet', {
makeInvitation: () => zcf.makeInvitation(makeOption, 'makeCallOption'),
const CoveredCallCratorFacetI = M.interface('CoveredCallCratorFacet', {
makeInvitation: M.call().returns(M.promise()),
});

const creatorFacet = prepareExo(
instanceBaggage,
'creatorFacet',
CoveredCallCratorFacetI,
{
makeInvitation() {
return zcf.makeInvitation(makeOption, 'makeCallOption');
},
},
);
return harden({ creatorFacet });
};

Expand Down
17 changes: 11 additions & 6 deletions packages/zoe/src/typeGuards.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,16 +347,21 @@ export const AdminFacetI = M.interface('ZcfAdminFacet', {
upgradeContract: M.call(M.string()).optional(M.any()).returns(M.promise()),
});

export const SeatDataShape = harden({
proposal: ProposalShape,
initialAllocation: AmountKeywordRecordShape,
seatHandle: SeatShape,
});
export const SeatDataShape = M.splitRecord(
{
proposal: ProposalShape,
initialAllocation: AmountKeywordRecordShape,
seatHandle: SeatShape,
},
{
offerArgs: M.any(),
},
);

export const HandleOfferI = M.interface('HandleOffer', {
handleOffer: M.call(InvitationHandleShape, SeatDataShape).returns({
offerResultPromise: M.promise(),
exitObj: ExitObjectI,
exitObj: ExitObjectShape,
}),
});

Expand Down
Loading

0 comments on commit bcb2ff8

Please sign in to comment.