Skip to content

Commit

Permalink
Merge pull request #6918 from Agoric/5285-eschew-weakmap
Browse files Browse the repository at this point in the history
5286 fix heap for getPublicTopics
  • Loading branch information
mergify[bot] authored Feb 4, 2023
2 parents 87023e8 + d44a4b7 commit efd964d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 73 deletions.
24 changes: 12 additions & 12 deletions packages/inter-protocol/src/price/priceAggregatorChainlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { makeScalarBigMapStore } from '@agoric/vat-data';
import {
makeOnewayPriceAuthorityKit,
makePublicTopicProvider,
makeStorageNodePathProvider,
} from '@agoric/zoe/src/contractSupport/index.js';
import { E } from '@endo/eventual-send';
import { Far } from '@endo/marshal';
Expand Down Expand Up @@ -141,7 +141,7 @@ export const start = async (zcf, privateArgs, baggage) => {
const latestRoundStorageNode = E(storageNode).makeChildNode('latestRound');
pipeTopicToStorage(latestRoundSubscriber, latestRoundStorageNode, marshaller);

const providePublicTopic = makePublicTopicProvider();
const memoizedPath = makeStorageNodePathProvider(baggage);

/** @type {MapStore<string, *>} */
const oracles = makeScalarBigMapStore('oracles', {
Expand Down Expand Up @@ -326,16 +326,16 @@ export const start = async (zcf, privateArgs, baggage) => {
},
getPublicTopics() {
return {
quotes: providePublicTopic(
'Quotes from this price aggregator',
quoteSubscriber,
storageNode,
),
latestRound: providePublicTopic(
'Notification of each round',
latestRoundSubscriber,
latestRoundStorageNode,
),
quotes: {
description: 'Quotes from this price aggregator',
subscriber: quoteSubscriber,
storagePath: memoizedPath(storageNode),
},
latestRound: {
description: 'Notification of each round',
subscriber: latestRoundSubscriber,
storagePath: memoizedPath(latestRoundStorageNode),
},
};
},
});
Expand Down
16 changes: 8 additions & 8 deletions packages/inter-protocol/src/vaultFactory/vaultHolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
*/
import { AmountShape } from '@agoric/ertp';
import {
SubscriberShape,
prepareDurablePublishKit,
pipeTopicToStorage,
prepareDurablePublishKit,
SubscriberShape,
TopicsRecordShape,
} from '@agoric/notifier';
import { M, prepareExoClassKit } from '@agoric/vat-data';
import { makePublicTopicProvider } from '@agoric/zoe/src/contractSupport/durability.js';
import { makeStorageNodePathProvider } from '@agoric/zoe/src/contractSupport/durability.js';
import { UnguardedHelperI } from '../typeGuards.js';

const { Fail } = assert;
Expand Down Expand Up @@ -39,7 +39,7 @@ const HolderI = M.interface('holder', {
* @param {ERef<Marshaller>} marshaller
*/
export const prepareVaultHolder = (baggage, marshaller) => {
const providePublicTopic = makePublicTopicProvider();
const memoizedPath = makeStorageNodePathProvider(baggage);

const makeVaultHolderPublishKit = prepareDurablePublishKit(
baggage,
Expand Down Expand Up @@ -91,11 +91,11 @@ export const prepareVaultHolder = (baggage, marshaller) => {
getPublicTopics() {
const { subscriber, storageNode } = this.state;
return harden({
vault: providePublicTopic(
'Vault holder status',
vault: {
description: 'Vault holder status',
subscriber,
storageNode,
),
storagePath: memoizedPath(storageNode),
},
});
},
makeAdjustBalancesInvitation() {
Expand Down
24 changes: 12 additions & 12 deletions packages/smart-wallet/src/smartWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { StorageNodeShape } from '@agoric/notifier/src/typeGuards.js';
import { M, mustMatch } from '@agoric/store';
import { makeScalarBigMapStore, prepareExoClassKit } from '@agoric/vat-data';
import { makePublicTopicProvider } from '@agoric/zoe/src/contractSupport/index.js';
import { makeStorageNodePathProvider } from '@agoric/zoe/src/contractSupport/durability.js';
import { E } from '@endo/far';
import { makeInvitationsHelper } from './invitations.js';
import { makeOfferExecutor } from './offers.js';
Expand Down Expand Up @@ -159,7 +159,7 @@ export const prepareSmartWallet = (baggage, shared) => {
'Smart Wallet publish kit',
);

const providePublicTopic = makePublicTopicProvider();
const memoizedPath = makeStorageNodePathProvider(baggage);

/**
*
Expand Down Expand Up @@ -557,16 +557,16 @@ export const prepareSmartWallet = (baggage, shared) => {
walletStorageNode,
} = this.state;
return harden({
current: providePublicTopic(
'Current state of wallet',
currentPublishKit.subscriber,
currentStorageNode,
),
updates: providePublicTopic(
'Changes to wallet',
updatePublishKit.subscriber,
walletStorageNode,
),
current: {
description: 'Current state of wallet',
subscriber: currentPublishKit.subscriber,
storagePath: memoizedPath(currentStorageNode),
},
updates: {
description: 'Changes to wallet',
subscriber: updatePublishKit.subscriber,
storagePath: memoizedPath(walletStorageNode),
},
});
},
},
Expand Down
58 changes: 17 additions & 41 deletions packages/zoe/src/contractSupport/durability.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { makePublicTopic, SubscriberShape } from '@agoric/notifier';
import { StorageNodeShape } from '@agoric/notifier/src/typeGuards.js';
import { M, mustMatch } from '@agoric/store';
import { makeAtomicProvider } from '@agoric/store/src/stores/store-utils.js';
import {
makeScalarBigMapStore,
provide,
provideDurableMapStore,
provideDurableSetStore,
} from '@agoric/vat-data';
import { E } from '@endo/eventual-send';
import { Far } from '@endo/marshal';

/// <reference types="@agoric/notifier/src/types-ambient.js"/>

/**
* SCALE: Only for low cardinality provisioning. Every value from init() will
* remain in the map for the lifetime of the heap. If a key object is GCed, its
* representative also remains.
*
* @template {{}} E Ephemeral state
* @param {() => E} init
*/
Expand All @@ -37,47 +41,19 @@ harden(makeEphemeraProvider);

/**
*
* @param {import('@agoric/vat-data').Baggage} baggage
*/
export const makePublicTopicProvider = () => {
/** @type {WeakMap<Subscriber<any>, import('@agoric/notifier').PublicTopic<any>>} */
const extant = new WeakMap();

/**
* Provide a PublicTopic for the specified durable subscriber.
* Memoizes the resolution of the promise for the storageNode's path, for the lifetime of the vat.
*
* @template {object} T
* @param {string} description
* @param {Subscriber<T>} durableSubscriber primary key
* @param {ERef<StorageNode>} storageNode
* @returns {import('@agoric/notifier').PublicTopic<T>}
*/
const providePublicTopic = (description, durableSubscriber, storageNode) => {
if (extant.has(durableSubscriber)) {
// @ts-expect-error cast
return extant.get(durableSubscriber);
}
mustMatch(
harden({ description, durableSubscriber, storageNode }),
harden({
description: M.string(),
durableSubscriber: SubscriberShape,
storageNode: M.eref(StorageNodeShape),
}),
);

/** @type {import('@agoric/notifier').PublicTopic<T>} */
const newMeta = makePublicTopic(
description,
durableSubscriber,
storageNode,
);
extant.set(durableSubscriber, newMeta);
return newMeta;
export const makeStorageNodePathProvider = baggage => {
/** @type {import('@agoric/store/src/stores/store-utils.js').AtomicProvider<StorageNode, string>} */
const nodePaths = makeAtomicProvider(
provideDurableMapStore(baggage, 'storage node paths'),
);
/** @param {ERef<StorageNode>} nodeP */
return async nodeP => {
const node = await nodeP;
return nodePaths.provideAsync(node, n => E(n).getPath());
};
return providePublicTopic;
};
harden(makePublicTopicProvider);

/**
* Provide an empty ZCF seat.
Expand Down

0 comments on commit efd964d

Please sign in to comment.