Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4383 durable zoe #5879

Merged
merged 5 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/zoe/src/zoeService/escrowStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import { AmountMath } from '@agoric/ertp';
import { E } from '@endo/eventual-send';
import { makeWeakStore } from '@agoric/store';
import { assert, details as X, q } from '@agoric/assert';
import { objectMap } from '@agoric/vat-data';
import { objectMap, provideDurableWeakMapStore } from '@agoric/vat-data';

import './types.js';
import './internal-types.js';
Expand All @@ -15,10 +14,12 @@ import { arrayToObj } from '../objArrayConversion.js';
/**
* Store the pool purses whose purpose is to escrow assets, with one
* purse per brand.
*
* @param {import('@agoric/vat-data').Baggage} baggage
*/
export const makeEscrowStorage = () => {
export const makeEscrowStorage = baggage => {
/** @type {WeakStore<Brand, ERef<Purse>>} */
const brandToPurse = makeWeakStore('brand');
const brandToPurse = provideDurableWeakMapStore(baggage, 'brandToPurse');

/** @type {CreatePurse} */
const createPurse = (issuer, brand) => {
Expand Down
31 changes: 14 additions & 17 deletions packages/zoe/src/zoeService/installationStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import { assert, details as X } from '@agoric/assert';
import { E } from '@endo/eventual-send';
import { makeWeakStore } from '@agoric/store';
import {
defineDurableKind,
makeScalarBigMapStore,
provideKindHandle,
provideDurableWeakMapStore,
vivifyKind,
} from '@agoric/vat-data';

/** @typedef { import('@agoric/swingset-vat').BundleID} BundleID */
Expand All @@ -21,28 +20,26 @@ export const makeInstallationStorage = (
zoeBaggage = makeScalarBigMapStore('zoe baggage', { durable: true }),
) => {
/** @type {WeakStore<Installation, { bundleCap: BundleCap, bundleID: BundleID }>} */
const installationsBundleCap = makeWeakStore('installationsBundleCap');
/** @type {WeakStore<Installation, SourceBundle>} */
const installationsBundle = makeWeakStore('installationsBundle');

const bundleIDInstallationKindHandle = provideKindHandle(
const installationsBundleCap = provideDurableWeakMapStore(
zoeBaggage,
'BundleIDInstallation',
'installationsBundleCap',
);

const bundleInstallationKindHandle = provideKindHandle(
/** @type {WeakStore<Installation, SourceBundle>} */
const installationsBundle = provideDurableWeakMapStore(
zoeBaggage,
'BundleInstallation',
'installationsBundle',
);

const makeBundleIDInstallation = defineDurableKind(
bundleIDInstallationKindHandle,
const makeBundleIDInstallation = vivifyKind(
zoeBaggage,
'BundleIDInstallation',
() => ({}),
{ getBundle: _context => assert.fail('bundleID-based Installation') },
);

const makeBundleInstallation = defineDurableKind(
bundleInstallationKindHandle,
const makeBundleInstallation = vivifyKind(
zoeBaggage,
'BundleInstallation',
bundle => ({ bundle }),
{ getBundle: ({ state: { bundle } }) => bundle },
);
Expand All @@ -68,7 +65,7 @@ export const makeInstallationStorage = (
/** @type {Installation} */
// @ts-expect-error cast
const installation = makeBundleIDInstallation();
installationsBundleCap.init(installation, { bundleCap, bundleID });
installationsBundleCap.init(installation, harden({ bundleCap, bundleID }));
return installation;
};

Expand Down
8 changes: 5 additions & 3 deletions packages/zoe/src/zoeService/makeInvitation.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// @ts-check
import { assert, details as X } from '@agoric/assert';
import { AmountMath, makeIssuerKit, AssetKind } from '@agoric/ertp';
import { AmountMath, makeDurableIssuerKit, AssetKind } from '@agoric/ertp';
import { InvitationElementShape } from '../typeGuards.js';

/**
* @param {import('@agoric/vat-data').Baggage} baggage
* @param {ShutdownWithFailure | undefined} shutdownZoeVat
*/
export const createInvitationKit = (shutdownZoeVat = undefined) => {
const invitationKit = makeIssuerKit(
export const vivifyInvitationKit = (baggage, shutdownZoeVat = undefined) => {
const invitationKit = makeDurableIssuerKit(
baggage,
'Zoe Invitation',
AssetKind.SET,
undefined,
Expand Down
17 changes: 11 additions & 6 deletions packages/zoe/src/zoeService/zoeStorageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { AssetKind, makeIssuerKit } from '@agoric/ertp';
import { Far } from '@endo/marshal';
import {
makeScalarBigMapStore,
makeScalarBigWeakMapStore,
provideDurableWeakMapStore,
} from '@agoric/vat-data';

import { provideIssuerStorage } from '../issuerStorage.js';
import { makeAndStoreInstanceRecord } from '../instanceRecordStorage.js';
import { makeIssuerRecord } from '../issuerRecord.js';
import { makeEscrowStorage } from './escrowStorage.js';
import { createInvitationKit } from './makeInvitation.js';
import { vivifyInvitationKit } from './makeInvitation.js';
import { makeInstanceAdminStorage } from './instanceAdminStorage.js';
import { makeInstallationStorage } from './installationStorage.js';

Expand Down Expand Up @@ -58,7 +58,7 @@ export const makeZoeStorageManager = (
// EscrowStorage holds the purses that Zoe uses for escrow. This
// object should be closely held and tracked: all of the digital
// assets that users escrow are contained within these purses.
const escrowStorage = makeEscrowStorage();
const escrowStorage = makeEscrowStorage(zoeBaggage);

// Add a purse for escrowing user funds (not for fees). Create the
// local, non-remote escrow purse for the fee mint immediately.
Expand All @@ -71,8 +71,10 @@ export const makeZoeStorageManager = (
// In order to participate in a contract, users must have
// invitations, which are ERTP payments made by Zoe. This code
// contains the mint capability for invitations.
const { setupMakeInvitation, invitationIssuer } =
createInvitationKit(shutdownZoeVat);
const { setupMakeInvitation, invitationIssuer } = vivifyInvitationKit(
zoeBaggage,
shutdownZoeVat,
);

// Every new instance of a contract creates a corresponding
// "zoeInstanceAdmin" - an admin facet within the Zoe Service for
Expand All @@ -99,7 +101,10 @@ export const makeZoeStorageManager = (
getBundleIDFromInstallation,
} = makeInstallationStorage(getBundleCapForID, zoeBaggage);

const proposalSchemas = makeScalarBigWeakMapStore('proposal schemas');
const proposalSchemas = provideDurableWeakMapStore(
zoeBaggage,
'proposal schemas',
);

const getProposalSchemaForInvitation = invitationHandle => {
if (proposalSchemas.has(invitationHandle)) {
Expand Down
11 changes: 8 additions & 3 deletions packages/zoe/test/unitTests/zoe/test-escrowStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';
import { AmountMath, makeIssuerKit, AssetKind } from '@agoric/ertp';

import { E } from '@endo/eventual-send';
import { makeScalarBigMapStore } from '@agoric/vat-data';
import { makeEscrowStorage } from '../../../src/zoeService/escrowStorage.js';
import {
assertAmountsEqual,
Expand All @@ -13,7 +14,7 @@ import {

test('makeEscrowStorage', async t => {
const { createPurse, makeLocalPurse, withdrawPayments, depositPayments } =
makeEscrowStorage();
makeEscrowStorage(makeScalarBigMapStore('zoe baggage', { durable: true }));

const currencyKit = makeIssuerKit(
'currency',
Expand Down Expand Up @@ -132,7 +133,9 @@ const setupPurses = async createPurse => {
};

test('payments without matching give keywords', async t => {
const { createPurse, depositPayments } = makeEscrowStorage();
const { createPurse, depositPayments } = makeEscrowStorage(
makeScalarBigMapStore('zoe baggage', { durable: true }),
);

const { ticketKit, currencyKit } = await setupPurses(createPurse);

Expand Down Expand Up @@ -167,7 +170,9 @@ test('payments without matching give keywords', async t => {
});

test(`give keywords without matching payments`, async t => {
const { createPurse, depositPayments } = makeEscrowStorage();
const { createPurse, depositPayments } = makeEscrowStorage(
makeScalarBigMapStore('zoe baggage', { durable: true }),
);

const { ticketKit, currencyKit } = await setupPurses(createPurse);

Expand Down
12 changes: 6 additions & 6 deletions packages/zoe/test/unitTests/zoe/test-installationStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('install, unwrap installations', async t => {
const installation = await installBundle(fakeBundle);
const unwrapped = await unwrapInstallation(installation);
t.is(unwrapped.installation, installation);
t.is(unwrapped.bundle, fakeBundle);
t.deepEqual(unwrapped.bundle, fakeBundle);
});

test('install, unwrap installation of bundlecap', async t => {
Expand All @@ -37,7 +37,7 @@ test('unwrap promise for installation', async t => {
const installation = await installBundle(fakeBundle);
const unwrapped = await unwrapInstallation(Promise.resolve(installation));
t.is(unwrapped.installation, installation);
t.is(unwrapped.bundle, fakeBundle);
t.deepEqual(unwrapped.bundle, fakeBundle);
});

test('install several', async t => {
Expand All @@ -48,12 +48,12 @@ test('install several', async t => {
const installation1 = await installBundle(fakeBundle1);
const unwrapped1 = await unwrapInstallation(installation1);
t.is(unwrapped1.installation, installation1);
t.is(unwrapped1.bundle, fakeBundle1);
t.deepEqual(unwrapped1.bundle, fakeBundle1);

const installation2 = await installBundle(fakeBundle2);
const unwrapped2 = await unwrapInstallation(installation2);
t.is(unwrapped2.installation, installation2);
t.is(unwrapped2.bundle, fakeBundle2);
t.deepEqual(unwrapped2.bundle, fakeBundle2);
});

test('install same twice', async t => {
Expand All @@ -66,7 +66,7 @@ test('install same twice', async t => {
const installation1 = await installBundle(fakeBundle1);
const unwrapped1 = await unwrapInstallation(installation1);
t.is(unwrapped1.installation, installation1);
t.is(unwrapped1.bundle, fakeBundle1);
t.deepEqual(unwrapped1.bundle, fakeBundle1);

// If the same bundle is installed twice, the bundle is the same,
// but the installation is different. Zoe does not currently care about
Expand All @@ -75,7 +75,7 @@ test('install same twice', async t => {
const unwrapped2 = await unwrapInstallation(installation2);
t.is(unwrapped2.installation, installation2);
t.not(installation2, installation1);
t.is(unwrapped2.bundle, fakeBundle1);
t.deepEqual(unwrapped2.bundle, fakeBundle1);

// same for bundleIDs
const installation3 = await installBundleID('id');
Expand Down
21 changes: 15 additions & 6 deletions packages/zoe/test/unitTests/zoe/test-makeInvitation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';
import { AmountMath } from '@agoric/ertp';
import { E } from '@endo/eventual-send';
import { Far } from '@endo/marshal';
import { makeScalarBigWeakMapStore } from '@agoric/vat-data';
import {
makeScalarBigWeakMapStore,
makeScalarBigMapStore,
} from '@agoric/vat-data';

import { createInvitationKit } from '../../../src/zoeService/makeInvitation.js';
import { vivifyInvitationKit } from '../../../src/zoeService/makeInvitation.js';

const proposalSchemas = makeScalarBigWeakMapStore('proposal schemas');

test('createInvitationKit', async t => {
const { setupMakeInvitation, invitationIssuer } = createInvitationKit();
test('vivifyInvitationKit', async t => {
const { setupMakeInvitation, invitationIssuer } = vivifyInvitationKit(
makeScalarBigMapStore('zoe baggage', { durable: true }),
);

const mockInstance = Far('mockInstance', {});
const mockInstallation = Far('mockInstallation', {});
Expand Down Expand Up @@ -58,7 +63,9 @@ test('createInvitationKit', async t => {
});

test('description is omitted, wrongly', async t => {
const { setupMakeInvitation } = createInvitationKit();
const { setupMakeInvitation } = vivifyInvitationKit(
makeScalarBigMapStore('zoe baggage', { durable: true }),
);

const mockInstance = Far('mockInstance', {});
const mockInstallation = Far('mockInstallation', {});
Expand Down Expand Up @@ -89,7 +96,9 @@ test('description is omitted, wrongly', async t => {
});

test('customProperties ok to omit', async t => {
const { setupMakeInvitation, invitationIssuer } = createInvitationKit();
const { setupMakeInvitation, invitationIssuer } = vivifyInvitationKit(
makeScalarBigMapStore('zoe baggage', { durable: true }),
);

const mockInstance = Far('mockInstance', {});
const mockInstallation = Far('mockInstallation', {});
Expand Down