Skip to content

Commit

Permalink
refactor: Cleanup #10348 (#10603)
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Dec 3, 2024
2 parents e37a322 + a0798d2 commit 13796d6
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 42 deletions.
8 changes: 4 additions & 4 deletions packages/SwingSet/tools/run-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { makeQueue } from '@endo/stream';
* @import { RunPolicy } from '../src/types-external.js'
*/

/** @typedef {{ provideRunPolicy: () => RunPolicy | undefined }} RunPolicyMaker */
/** @typedef {{ provideRunPolicy: () => RunPolicy | undefined }} RunHarness */

/**
* @param {import('../src/controller/controller.js').SwingsetController} controller
* @param {RunPolicyMaker} [perfTool]
* @param {RunHarness} [harness]
*/
export const makeRunUtils = (controller, perfTool) => {
export const makeRunUtils = (controller, harness) => {
const mutex = makeQueue();
const logRunFailure = reason =>
console.log('controller.run() failure', reason);
Expand All @@ -31,7 +31,7 @@ export const makeRunUtils = (controller, perfTool) => {
const queueAndRun = async (deliveryThunk, voidResult = false) => {
await mutex.get();
const kpid = await deliveryThunk();
const runPolicy = perfTool && perfTool.provideRunPolicy();
const runPolicy = harness && harness.provideRunPolicy();
const runResultP = controller.run(runPolicy);
mutex.put(runResultP.catch(logRunFailure));
await runResultP;
Expand Down
22 changes: 11 additions & 11 deletions packages/boot/test/bootstrapTests/orchestration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import {
} from './walletFactory.js';
import {
insistManagerType,
makeRunPolicyProvider,
makeSwingsetHarness,
} from '../../tools/supports.js';

const test: TestFn<
WalletFactoryTestContext & {
perfTool?: ReturnType<typeof makeRunPolicyProvider>;
harness?: ReturnType<typeof makeSwingsetHarness>;
}
> = anyTest;

Expand All @@ -40,14 +40,14 @@ const {

test.before(async t => {
insistManagerType(defaultManagerType);
const perfTool =
defaultManagerType === 'xsnap' ? makeRunPolicyProvider() : undefined;
const harness =
defaultManagerType === 'xsnap' ? makeSwingsetHarness() : undefined;
const ctx = await makeWalletFactoryContext(
t,
'@agoric/vm-config/decentral-itest-orchestration-config.json',
{ slogFile, defaultManagerType, perfTool },
{ slogFile, defaultManagerType, harness },
);
t.context = { ...ctx, perfTool };
t.context = { ...ctx, harness };
});
test.after.always(t => t.context.shutdown?.());

Expand Down Expand Up @@ -123,7 +123,7 @@ test.skip('stakeOsmo - queries', async t => {
buildProposal,
evalProposal,
runUtils: { EV },
perfTool,
harness,
} = t.context;
await evalProposal(
buildProposal('@agoric/builders/scripts/orchestration/init-stakeOsmo.js'),
Expand Down Expand Up @@ -162,7 +162,7 @@ test.serial('stakeAtom - smart wallet', async t => {
agoricNamesRemotes,
bridgeUtils: { flushInboundQueue },
readPublished,
perfTool,
harness,
} = t.context;

await evalProposal(
Expand All @@ -173,7 +173,7 @@ test.serial('stakeAtom - smart wallet', async t => {
'agoric1testStakAtom',
);

perfTool?.usePolicy(true);
harness?.useRunPolicy(true);
await wd.sendOffer({
id: 'request-account',
invitationSpec: {
Expand All @@ -183,8 +183,8 @@ test.serial('stakeAtom - smart wallet', async t => {
},
proposal: {},
});
perfTool && t.log('makeAccount computrons', perfTool.totalCount());
perfTool?.usePolicy(false);
harness && t.log('makeAccount computrons', harness.totalComputronCount());
harness?.useRunPolicy(false);

await flushInboundQueue();
t.like(wd.getCurrentWalletRecord(), {
Expand Down
8 changes: 4 additions & 4 deletions packages/boot/test/bootstrapTests/price-feed-replace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ test.serial('setupVaults; run updatePriceFeeds proposals', async t => {
setupVaults,
governanceDriver: gd,
readPublished,
perfTool,
harness,
} = t.context;

await setupVaults(collateralBrandKey, managerIndex, setup);
Expand All @@ -75,10 +75,10 @@ test.serial('setupVaults; run updatePriceFeeds proposals', async t => {
roundId: 1n,
});

perfTool && perfTool.usePolicy(true);
harness && harness.useRunPolicy(true);
await priceFeedDrivers[collateralBrandKey].setPrice(15.99);
perfTool && t.log('setPrice computrons', perfTool.totalCount());
perfTool && perfTool.usePolicy(false);
harness && t.log('setPrice computrons', harness.totalComputronCount());
harness && harness.useRunPolicy(false);

t.like(readPublished('priceFeed.ATOM-USD_price_feed.latestRound'), {
roundId: 2n,
Expand Down
10 changes: 5 additions & 5 deletions packages/boot/tools/liquidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@agoric/vats/tools/board-utils.js';
import { Offers } from '@agoric/inter-protocol/src/clientSupport.js';
import type { ExecutionContext } from 'ava';
import { insistManagerType, makeRunPolicyProvider } from './supports.js';
import { insistManagerType, makeSwingsetHarness } from './supports.js';
import { type SwingsetTestKit, makeSwingsetTestKit } from './supports.js';
import {
type GovernanceDriver,
Expand Down Expand Up @@ -322,12 +322,12 @@ export const makeLiquidationTestContext = async (
SWINGSET_WORKER_TYPE: defaultManagerType = 'local',
} = env;
assertManagerType(defaultManagerType);
const perfTool =
defaultManagerType === 'xsnap' ? makeRunPolicyProvider() : undefined;
const harness =
defaultManagerType === 'xsnap' ? makeSwingsetHarness() : undefined;
const swingsetTestKit = await makeSwingsetTestKit(t.log, undefined, {
slogFile,
defaultManagerType,
perfTool,
harness,
});
console.time('DefaultTestContext');

Expand Down Expand Up @@ -385,7 +385,7 @@ export const makeLiquidationTestContext = async (
refreshAgoricNamesRemotes,
walletFactoryDriver,
governanceDriver,
perfTool,
harness,
};
};

Expand Down
33 changes: 19 additions & 14 deletions packages/boot/tools/supports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { Fail } from '@endo/errors';
import {
makeRunUtils,
type RunUtils,
type RunPolicyMaker,
type RunHarness,
} from '@agoric/swingset-vat/tools/run-utils.js';
import {
boardSlottingMarshaller,
Expand Down Expand Up @@ -83,7 +83,7 @@ type BootstrapEV = EProxy & {

const makeBootstrapRunUtils = makeRunUtils as (
controller: SwingsetController,
perfTool?: RunPolicyMaker,
harness?: RunHarness,
) => Omit<RunUtils, 'EV'> & { EV: BootstrapEV };

const keysToObject = <K extends PropertyKey, V>(
Expand Down Expand Up @@ -315,7 +315,7 @@ export const matchIter = (t: AvaT, iter, valueRef) => {
* @param [options.profileVats]
* @param [options.debugVats]
* @param [options.defaultManagerType]
* @param [options.perfTool]
* @param [options.harness]
*/
export const makeSwingsetTestKit = async (
log: (..._: any[]) => void,
Expand All @@ -329,7 +329,7 @@ export const makeSwingsetTestKit = async (
profileVats = [] as string[],
debugVats = [] as string[],
defaultManagerType = 'local' as ManagerType,
perfTool = undefined as RunPolicyMaker | undefined,
harness = undefined as RunHarness | undefined,
} = {},
) => {
console.time('makeBaseSwingsetTestKit');
Expand Down Expand Up @@ -547,7 +547,7 @@ export const makeSwingsetTestKit = async (

console.timeLog('makeBaseSwingsetTestKit', 'buildSwingset');

const runUtils = makeBootstrapRunUtils(controller, perfTool);
const runUtils = makeBootstrapRunUtils(controller, harness);

const buildProposal = makeProposalExtractor({
childProcess: childProcessAmbient,
Expand Down Expand Up @@ -670,7 +670,12 @@ export const makeSwingsetTestKit = async (
};
export type SwingsetTestKit = Awaited<ReturnType<typeof makeSwingsetTestKit>>;

export const makeRunPolicyProvider = () => {
/**
* Return a harness that can be dynamically configured to provide a computron-
* counting run policy (and queried for the count of computrons recorded since
* the last reset).
*/
export const makeSwingsetHarness = () => {
const c2b = defaultBeansPerXsnapComputron;
const beansPerUnit = {
// see https://cosgov.org/agoric?msgType=parameterChangeProposal&network=main
Expand All @@ -681,24 +686,24 @@ export const makeRunPolicyProvider = () => {

/** @type {ReturnType<typeof computronCounter> | undefined} */
let policy;
let counting = false;
let policyEnabled = false;

const meter = harden({
provideRunPolicy: () => {
if (counting && !policy) {
if (policyEnabled && !policy) {
policy = computronCounter({ beansPerUnit });
}
return policy;
},
/** @param {boolean} x */
usePolicy: x => {
counting = x;
if (!counting) {
/** @param {boolean} forceEnabled */
useRunPolicy: forceEnabled => {
policyEnabled = forceEnabled;
if (!policyEnabled) {
policy = undefined;
}
},
totalCount: () => (policy?.totalBeans() || 0n) / c2b,
resetPolicy: () => (policy = undefined),
totalComputronCount: () => (policy?.totalBeans() || 0n) / c2b,
resetRunPolicy: () => (policy = undefined),
});
return meter;
};
Expand Down
5 changes: 1 addition & 4 deletions packages/cosmic-swingset/src/computron-counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ export function computronCounter(
const defaultCleanupBudget = remainingCleanups.default;
let cleanupStarted = false;
let cleanupDone = false;
const cleanupPossible =
Object.values(remainingCleanups).length > 0
? Object.values(remainingCleanups).some(n => n > 0)
: defaultCleanupBudget > 0;
const cleanupPossible = Object.values(remainingCleanups).some(n => n > 0);
if (!cleanupPossible) cleanupDone = true;
/** @type {() => (false | import('@agoric/swingset-vat').CleanupBudget)} */
const allowCleanup = () =>
Expand Down

0 comments on commit 13796d6

Please sign in to comment.