Skip to content

Commit

Permalink
test(boot): thread computron counter thru test context
Browse files Browse the repository at this point in the history
  • Loading branch information
dckc committed Nov 22, 2024
1 parent fe2f37f commit d002646
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/boot/test/bootstrapTests/walletFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import { makeWalletFactoryDriver } from '../../tools/drivers.js';
export const makeWalletFactoryContext = async (
t,
configSpecifier = '@agoric/vm-config/decentral-main-vaults-config.json',
opts = {},
) => {
const swingsetTestKit = await makeSwingsetTestKit(t.log, undefined, {
configSpecifier,
...opts,
});

const { runUtils, storage } = swingsetTestKit;
Expand Down
19 changes: 18 additions & 1 deletion packages/boot/tools/liquidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +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, makePolicyProvider } from './supports.js';
import { type SwingsetTestKit, makeSwingsetTestKit } from './supports.js';
import {
type GovernanceDriver,
Expand Down Expand Up @@ -305,13 +306,28 @@ export const makeLiquidationTestKit = async ({
};
};

// asserts x is type doesn't work when using arrow functions
// https://github.com/microsoft/TypeScript/issues/34523
function assertManagerType(specimen: string): asserts specimen is ManagerType {
insistManagerType(specimen);
}

export const makeLiquidationTestContext = async (
t,
io: { env?: Record<string, string | undefined> } = {},
) => {
const { env = {} } = io;
const {
SLOGFILE: slogFile,
SWINGSET_WORKER_TYPE: defaultManagerType = 'local',
} = env;
assertManagerType(defaultManagerType);
const perfTool =
defaultManagerType === 'xsnap' ? makePolicyProvider() : undefined;
const swingsetTestKit = await makeSwingsetTestKit(t.log, undefined, {
slogFile: env.SLOGFILE,
slogFile,
defaultManagerType,
perfTool,
});
console.time('DefaultTestContext');

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

Expand Down
53 changes: 52 additions & 1 deletion packages/boot/tools/supports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { Fail } from '@endo/errors';
import {
makeRunUtils,
type RunUtils,
type RunPolicyMaker,
} from '@agoric/swingset-vat/tools/run-utils.js';
import {
boardSlottingMarshaller,
Expand All @@ -47,6 +48,11 @@ import type { BridgeHandler, IBCMethod } from '@agoric/vats';
import type { BootstrapRootObject } from '@agoric/vats/src/core/lib-boot.js';
import type { EProxy } from '@endo/eventual-send';
import type { FastUSDCCorePowers } from '@agoric/fast-usdc/src/fast-usdc.start.js';
import {
defaultBeansPerVatCreation,
defaultBeansPerXsnapComputron,
} from '@agoric/cosmic-swingset/src/sim-params.js';
import { computronCounter } from '@agoric/cosmic-swingset/src/computron-counter.js';
import { icaMocks, protoMsgMockMap, protoMsgMocks } from './ibc/mocks.js';

const trace = makeTracer('BSTSupport', false);
Expand Down Expand Up @@ -77,6 +83,7 @@ type BootstrapEV = EProxy & {

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

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

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

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

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

export const makePolicyProvider = () => {
const c2b = defaultBeansPerXsnapComputron;
const mainParams = {
// see https://cosgov.org/agoric?msgType=parameterChangeProposal&network=main
blockComputeLimit: 65_000_000n * c2b,
vatCreation: defaultBeansPerVatCreation,
xsnapComputron: c2b,
};

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

const meter = harden({
provideRunPolicy: () => {
if (counting && !policy) {
policy = computronCounter(mainParams);
}
return policy;
},
/** @param {boolean} x */
usePolicy: x => {
counting = x;
if (!counting) {
policy = undefined;
}
},
totalCount: () => (policy?.totalBeans() || 0n) / c2b,
resetPolicy: () => (policy = undefined),
});
return meter;
};

/**
*
* @param {string} mt
* @returns {asserts mt is ManagerType}
*/
export function insistManagerType(mt) {
assert(['local', 'node-subprocess', 'xsnap', 'xs-worker'].includes(mt));
}

0 comments on commit d002646

Please sign in to comment.