Skip to content

Commit

Permalink
refactor: provideRetiredInstances
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg authored and Chris-Hibbert committed Dec 13, 2024
1 parent c883c39 commit 1581127
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
11 changes: 7 additions & 4 deletions packages/inter-protocol/src/proposals/add-auction.js
Original file line number Diff line number Diff line change
@@ -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);

Expand Down Expand Up @@ -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] {} } }
Expand Down
13 changes: 6 additions & 7 deletions packages/inter-protocol/src/proposals/deploy-price-feeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -109,6 +109,7 @@ const startPriceAggregatorInstance = async (
retiredContractInstances: retiredContractInstancesP,
},
instance: { produce: produceInstance },
produce: { retiredContractInstances: produceRetiredInstances },
},
{ AGORIC_INSTANCE_NAME, contractTerms, brandIn, brandOut },
installation,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 });
Expand Down
11 changes: 6 additions & 5 deletions packages/inter-protocol/src/proposals/replaceElectorate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' */
Expand Down Expand Up @@ -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,
);
Expand Down
22 changes: 14 additions & 8 deletions packages/inter-protocol/src/proposals/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<MapStore>} producer
* @param {string} [name]
* @param {Promise<MapStore>} consume
* @param {Producer<MapStore>} produce
* @returns {Promise<MapStore>}
*/
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;
};

0 comments on commit 1581127

Please sign in to comment.