From a1ad58fd3571f753ef03d7844030dcc43101c998 Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Thu, 12 Dec 2024 15:19:25 -0800 Subject: [PATCH] refactor: provideRetiredInstances --- .../src/proposals/add-auction.js | 11 ++++++---- .../src/proposals/deploy-price-feeds.js | 13 +++++------ .../src/proposals/replaceElectorate.js | 11 +++++----- .../inter-protocol/src/proposals/utils.js | 22 ++++++++++++------- 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/packages/inter-protocol/src/proposals/add-auction.js b/packages/inter-protocol/src/proposals/add-auction.js index e2cada43d97..76dd57a0c6b 100644 --- a/packages/inter-protocol/src/proposals/add-auction.js +++ b/packages/inter-protocol/src/proposals/add-auction.js @@ -1,9 +1,9 @@ import { deeplyFulfilledObject, makeTracer } from '@agoric/internal'; import { makeStorageNodeChild } from '@agoric/internal/src/lib-chainStorage.js'; -import { E } from '@endo/far'; import { Stable } from '@agoric/internal/src/tokens.js'; +import { E } from '@endo/far'; import { makeGovernedTerms as makeGovernedATerms } from '../auction/params.js'; -import { parallelCreateMap } from './utils.js'; +import { provideRetiredInstances } from './utils.js'; const trace = makeTracer('NewAuction', true); @@ -83,12 +83,15 @@ export const addAuction = async ( auctioneerInstallationP, ]); - await parallelCreateMap(produceRetiredInstances, 'retiredContractInstances'); + const retiredInstances = await provideRetiredInstances( + retiredContractInstancesP, + produceRetiredInstances, + ); // save the auctioneer instance so we can manage it later const boardID = await E(board).getId(legacyKit.instance); const identifier = `auctioneer-${boardID}`; - await E(retiredContractInstancesP).init(identifier, legacyKit.instance); + retiredInstances.init(identifier, legacyKit.instance); // Each field has an extra layer of type + value: // AuctionStartDelay: { type: 'relativeTime', value: { relValue: 2n, timerBrand: Object [Alleged: timerBrand] {} } } diff --git a/packages/inter-protocol/src/proposals/deploy-price-feeds.js b/packages/inter-protocol/src/proposals/deploy-price-feeds.js index e21fafcfb76..0ecbea49098 100644 --- a/packages/inter-protocol/src/proposals/deploy-price-feeds.js +++ b/packages/inter-protocol/src/proposals/deploy-price-feeds.js @@ -5,7 +5,7 @@ import { E } from '@endo/far'; import { unitAmount } from '@agoric/zoe/src/contractSupport/priceQuote.js'; import { oracleBrandFeedName, - parallelCreateMap, + provideRetiredInstances, reserveThenDeposit, sanitizePathSegment, } from './utils.js'; @@ -109,6 +109,7 @@ const startPriceAggregatorInstance = async ( retiredContractInstances: retiredContractInstancesP, }, instance: { produce: produceInstance }, + produce: { retiredContractInstances: produceRetiredInstances }, }, { AGORIC_INSTANCE_NAME, contractTerms, brandIn, brandOut }, installation, @@ -143,7 +144,10 @@ const startPriceAggregatorInstance = async ( // @ts-expect-error GovernableStartFn vs. fluxAggregatorContract.js start installation, }); - const retiredContractInstances = await retiredContractInstancesP; + const retiredContractInstances = await provideRetiredInstances( + retiredContractInstancesP, + produceRetiredInstances, + ); // save the instance so we can manage it later const retiringInstance = await E(agoricNames).lookup( @@ -240,11 +244,6 @@ export const deployPriceFeeds = async (powers, config) => { priceAggregatorRef.bundleID, ); - await parallelCreateMap( - powers.produce.retiredContractInstances, - 'retiredContractInstances', - ); - const { priceAuthorityAdmin, priceAuthority } = powers.consume; trace({ oracleAddresses }); diff --git a/packages/inter-protocol/src/proposals/replaceElectorate.js b/packages/inter-protocol/src/proposals/replaceElectorate.js index a0c4c1db28d..adde9adf747 100644 --- a/packages/inter-protocol/src/proposals/replaceElectorate.js +++ b/packages/inter-protocol/src/proposals/replaceElectorate.js @@ -15,7 +15,7 @@ import { assertPathSegment, makeStorageNodeChild, } from '@agoric/internal/src/lib-chainStorage.js'; -import { parallelCreateMap, reserveThenDeposit } from './utils.js'; +import { provideRetiredInstances, reserveThenDeposit } from './utils.js'; /** @import {EconomyBootstrapPowers} from './econ-behaviors.js' */ /** @import {EconCharterStartResult} from './econ-behaviors.js' */ @@ -226,14 +226,15 @@ const startNewEconomicCommittee = async ( trace(`committeeName ${committeeName}`); trace(`committeeSize ${committeeSize}`); - await parallelCreateMap(produceRetiredInstances, 'retiredContractInstances'); + const retiredInstances = await provideRetiredInstances( + retiredInstancesP, + produceRetiredInstances, + ); - // get the actual retiredContractInstances - const retiredInstances = await retiredInstancesP; // Record the retired electorate instance so we can manage it later. const economicCommitteeOriginal = await economicCommitteeOriginalP; const boardID = await E(board).getId(economicCommitteeOriginal); - await E(retiredInstances).init( + retiredInstances.init( `economicCommittee-${boardID}`, economicCommitteeOriginal, ); diff --git a/packages/inter-protocol/src/proposals/utils.js b/packages/inter-protocol/src/proposals/utils.js index 07c9a8269d2..165731e4838 100644 --- a/packages/inter-protocol/src/proposals/utils.js +++ b/packages/inter-protocol/src/proposals/utils.js @@ -174,14 +174,20 @@ export const sanitizePathSegment = name => { }; /** - * Idempotently create and store a durable MapStore for the promise space. - * `resolve()` silently fails if called again after a successful call, so it's - * safe for multiple proposals to call in parallel, as long as they all use the - * value from consume rather than the value they produced. + * Idempotently provide an empty MapStore for the `retiredContractInstances` + * value in promise space * - * @param {Producer} producer - * @param {string} [name] + * @param {Promise} consume + * @param {Producer} produce + * @returns {Promise} */ -export const parallelCreateMap = async (producer, name = 'mapStore') => { - await producer.resolve(makeScalarBigMapStore(name, { durable: true })); +export const provideRetiredInstances = async (consume, produce) => { + // Promise space has no way to look for an existing value other than awaiting a promise, + // but it does allow extra production so it's safe to do this redundantly. + produce.resolve( + makeScalarBigMapStore('retiredContractInstances', { + durable: true, + }), + ); + return consume; };