Skip to content

Commit

Permalink
chore: refactor based on review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
FUDCo committed Oct 5, 2023
1 parent 57a8ba1 commit 4ef2be2
Show file tree
Hide file tree
Showing 19 changed files with 139 additions and 222 deletions.
1 change: 1 addition & 0 deletions packages/SwingSet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@endo/patterns": "^0.2.5",
"@endo/promise-kit": "^0.2.59",
"@endo/ses-ava": "^0.2.43",
"@endo/stream": "^0.3.28",
"@endo/zip": "^0.2.34",
"ansi-styles": "^6.2.1",
"anylogger": "^0.21.0",
Expand Down
108 changes: 108 additions & 0 deletions packages/SwingSet/tools/run-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* eslint-disable @jessie.js/safe-await-separator */
import { Fail } from '@agoric/assert';
import { kunser } from '@agoric/kmarshal';
import { makeQueue } from '@endo/stream';
import type { E } from '@endo/eventual-send';

import type { SwingsetController } from '../src/controller/controller.js';

const sink = () => {};

export const makeRunUtils = (
controller: SwingsetController,
log = (..._) => {},
) => {
let cranksRun = 0;

const mutex = makeQueue();

mutex.put(controller.run());

const runThunk = async <T extends () => any>(
thunk: T,
): Promise<ReturnType<T>> => {
try {
// this promise for the last lock may fail
await mutex.get();
} catch {
// noop because the result will resolve for the previous runMethod return
}

const thunkResult = await thunk();

const result = controller.run().then(cranks => {
cranksRun += cranks;
log(`kernel ran ${cranks} cranks`);
return thunkResult;
});
mutex.put(result.then(sink, sink));
return result;
};

const queueAndRun = async (deliveryThunk, voidResult = false) => {
log('queueAndRun at', cranksRun);

const kpid = await runThunk(deliveryThunk);

if (voidResult) {
return undefined;
}
const status = controller.kpStatus(kpid);
switch (status) {
case 'fulfilled':
return kunser(controller.kpResolution(kpid));
case 'rejected':
throw kunser(controller.kpResolution(kpid));
case 'unresolved':
throw Error(`unresolved value for ${kpid}`);
default:
throw Fail`unknown status ${status}`;
}
};

type EVProxy = typeof E & {
sendOnly: (presence: unknown) => Record<string, (...args: any) => void>;
vat: (name: string) => Record<string, (...args: any) => Promise<any>>;
};
// @ts-expect-error cast, approximate
const EV: EVProxy = presence =>
new Proxy(harden({}), {
get: (_t, method, _rx) =>
harden((...args) =>
queueAndRun(() =>
controller.queueToVatObject(presence, method, args),
),
),
});
EV.vat = vatName =>
new Proxy(harden({}), {
get: (_t, method, _rx) =>
harden((...args) =>
queueAndRun(() => controller.queueToVatRoot(vatName, method, args)),
),
});
// @ts-expect-error xxx
EV.sendOnly = presence =>
new Proxy(harden({}), {
get: (_t, method, _rx) =>
harden((...args) =>
queueAndRun(
() => controller.queueToVatObject(presence, method, args),
true,
),
),
});
// @ts-expect-error xxx
EV.get = presence =>
new Proxy(harden({}), {
get: (_t, pathElement, _rx) =>
queueAndRun(() =>
controller.queueToVatRoot('bootstrap', 'awaitVatObject', [
presence,
[pathElement],
]),
),
});
return harden({ runThunk, EV });
};
export type RunUtils = ReturnType<typeof makeRunUtils>;
1 change: 0 additions & 1 deletion packages/agoric-cli/src/sdk-package-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export default [
"@agoric/swingset-vat",
"@agoric/swingset-xsnap-supervisor",
"@agoric/telemetry",
"@agoric/test-support",
"@agoric/time",
"@agoric/vat-data",
"@agoric/vats",
Expand Down
2 changes: 1 addition & 1 deletion packages/benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
"license": "Apache-2.0",
"dependencies": {
"@agoric/assert": "^0.6.0",
"@agoric/boot": "^0.1.0",
"@agoric/cosmic-swingset": "^0.41.3",
"@agoric/internal": "^0.3.2",
"@agoric/inter-protocol": "^0.16.1",
"@agoric/test-support": "^0.1.0",
"@agoric/vats": "^0.15.1",
"@agoric/zoe": "^0.26.2",
"@endo/init": "^0.5.59"
Expand Down
8 changes: 3 additions & 5 deletions packages/benchmark/src/benchmarkerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import '@agoric/cosmic-swingset/src/launch-chain.js';
import { Fail } from '@agoric/assert';
import { eventLoopIteration } from '@agoric/internal/src/testing-utils.js';
import { makeAgoricNamesRemotesFromFakeStorage } from '@agoric/vats/tools/board-utils.js';
import {
makeSwingsetTestKit,
makeWalletFactoryDriver,
} from '@agoric/test-support';
import { makeSwingsetTestKit } from '@agoric/boot/tools/supports.ts';
import { makeWalletFactoryDriver } from '@agoric/boot/tools/drivers.ts';

// When I was a child my family took a lot of roadtrips around California to go
// camping and backpacking and so on. It was not uncommon in those days (nor is
Expand Down Expand Up @@ -46,7 +44,7 @@ import {
* @typedef {{
* options: Record<string, string>,
* argv: string[],
* actors: Record<string, import('@agoric/test-support').SmartWalletDriver>,
* actors: Record<string, import('@agoric/boot/tools/drivers.ts').SmartWalletDriver>,
* title?: string,
* rounds?: number,
* config?: Record<string, unknown>,
Expand Down
15 changes: 7 additions & 8 deletions packages/boot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@
"author": "Agoric",
"license": "Apache-2.0",
"dependencies": {
"@agoric/assert": "^0.6.0",
"@agoric/builders": "^0.1.0",
"@agoric/cosmic-swingset": "^0.41.3",
"@agoric/ertp": "^0.16.2",
"@agoric/internal": "^0.3.2",
"@agoric/inter-protocol": "^0.16.1",
"@agoric/kmarshal": "^0.1.0",
"@agoric/swing-store": "^0.9.1",
"@agoric/swingset-vat": "^0.32.2",
"@agoric/time": "^0.3.2",
"@agoric/vat-data": "^0.5.2",
"@agoric/vats": "^0.15.1",
"@agoric/vm-config": "^0.1.0",
Expand All @@ -37,19 +44,11 @@
"import-meta-resolve": "^2.2.1"
},
"devDependencies": {
"@agoric/assert": "^0.6.0",
"@agoric/benchmark": "^0.1.0",
"@agoric/cosmic-swingset": "^0.41.3",
"@agoric/deploy-script-support": "^0.10.3",
"@agoric/governance": "^0.10.3",
"@agoric/inter-protocol": "^0.16.1",
"@agoric/kmarshal": "^0.1.0",
"@agoric/store": "^0.9.2",
"@agoric/swing-store": "^0.9.1",
"@agoric/swingset-liveslots": "^0.10.2",
"@agoric/swingset-vat": "^0.32.2",
"@agoric/test-support": "^0.1.0",
"@agoric/time": "^0.3.2",
"ava": "^5.3.0",
"c8": "^7.13.0",
"tsx": "^3.12.8"
Expand Down
6 changes: 2 additions & 4 deletions packages/boot/test/bootstrapTests/bench-vaults-performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ import engineGC from '@agoric/internal/src/lib-nodejs/engine-gc.js';

import { eventLoopIteration } from '@agoric/internal/src/testing-utils.js';
import { makeAgoricNamesRemotesFromFakeStorage } from '@agoric/vats/tools/board-utils.js';
import {
makeSwingsetTestKit,
makeWalletFactoryDriver,
} from '@agoric/test-support';
import { makeSwingsetTestKit } from '../../tools/supports.ts';
import { makeWalletFactoryDriver } from '../../tools/drivers.ts';

/**
* @type {import('ava').TestFn<
Expand Down
4 changes: 2 additions & 2 deletions packages/boot/test/bootstrapTests/liquidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {
AgoricNamesRemotes,
makeAgoricNamesRemotesFromFakeStorage,
} from '@agoric/vats/tools/board-utils.js';
import { makeSwingsetTestKit } from '../../tools/supports.ts';
import {
makeGovernanceDriver,
makePriceFeedDriver,
makeWalletFactoryDriver,
makeSwingsetTestKit,
} from '@agoric/test-support';
} from '../../tools/drivers.ts';

export const scale6 = x => BigInt(Math.round(x * 1_000_000));

Expand Down
2 changes: 1 addition & 1 deletion packages/boot/test/bootstrapTests/test-demo-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { test as anyTest } from '@agoric/zoe/tools/prepare-test-env-ava.js';
import { PowerFlags } from '@agoric/vats/src/walletFlags.js';

import type { TestFn } from 'ava';
import { makeSwingsetTestKit, keyArrayEqual } from '@agoric/test-support';
import { makeSwingsetTestKit, keyArrayEqual } from '../../tools/supports.ts';

const { keys } = Object;

Expand Down
4 changes: 2 additions & 2 deletions packages/boot/test/bootstrapTests/test-vats-restart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import type { EconomyBootstrapSpace } from '@agoric/inter-protocol/src/proposals
import {
makeProposalExtractor,
makeSwingsetTestKit,
makeWalletFactoryDriver,
} from '@agoric/test-support';
} from '../../tools/supports.ts';
import { makeWalletFactoryDriver } from '../../tools/drivers.ts';

const { Fail } = assert;

Expand Down
6 changes: 2 additions & 4 deletions packages/boot/test/bootstrapTests/test-vaults-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import {
} from '@agoric/vats/tools/board-utils.js';
import type { TestFn } from 'ava';
import { ParamChangesOfferArgs } from '@agoric/inter-protocol/src/econCommitteeCharter.js';
import {
makeSwingsetTestKit,
makeWalletFactoryDriver,
} from '@agoric/test-support';
import { makeSwingsetTestKit } from '../../tools/supports.ts';
import { makeWalletFactoryDriver } from '../../tools/drivers.ts';

// presently all these tests use one collateral manager
const collateralBrandKey = 'ATOM';
Expand Down
6 changes: 2 additions & 4 deletions packages/boot/test/bootstrapTests/test-vaults-upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ import { makeAgoricNamesRemotesFromFakeStorage } from '@agoric/vats/tools/board-
import { ExecutionContext, TestFn } from 'ava';
import { FakeStorageKit } from '@agoric/internal/src/storage-test-utils.js';
import { EconomyBootstrapSpace } from '@agoric/inter-protocol/src/proposals/econ-behaviors.js';
import {
makeSwingsetTestKit,
makeWalletFactoryDriver,
} from '@agoric/test-support';
import { makeSwingsetTestKit } from '../../tools/supports.ts';
import { makeWalletFactoryDriver } from '../../tools/drivers.ts';

// presently all these tests use one collateral manager
const collateralBrandKey = 'ATOM';
Expand Down
4 changes: 2 additions & 2 deletions packages/boot/test/bootstrapTests/test-zcf-upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { makeAgoricNamesRemotesFromFakeStorage } from '@agoric/vats/tools/board-
import { TestFn } from 'ava';
import {
matchAmount,
makeZoeDriver,
makeProposalExtractor,
makeSwingsetTestKit,
} from '@agoric/test-support';
} from '../../tools/supports.ts';
import { makeZoeDriver } from '../../tools/drivers.ts';

const filename = new URL(import.meta.url).pathname;
const dirname = path.dirname(filename);
Expand Down
8 changes: 2 additions & 6 deletions packages/boot/test/upgrading/test-upgrade-vats.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ import { test as anyTest } from '@agoric/swingset-vat/tools/prepare-test-env-ava

import { BridgeId } from '@agoric/internal';
import { buildVatController } from '@agoric/swingset-vat';
import { makeRunUtils } from '@agoric/swingset-vat/tools/run-utils.ts';
import { resolve as importMetaResolve } from 'import-meta-resolve';
import {
makeRunUtils,
matchAmount,
matchIter,
matchRef,
} from '@agoric/test-support';
import { matchAmount, matchIter, matchRef } from '../../tools/supports.ts';

/**
* @type {import('ava').TestFn<{}>}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import type { WalletFactoryStartResult } from '@agoric/vats/src/core/startWallet
import type { OfferSpec } from '@agoric/smart-wallet/src/offers.js';
import type { TimerService } from '@agoric/time/src/types.js';
import type { OfferMaker } from '@agoric/smart-wallet/src/types.js';
import type { RunUtils, SwingsetTestKit } from './supports.ts';
import type { RunUtils } from '@agoric/swingset-vat/tools/run-utils.ts';
import type { SwingsetTestKit } from './supports.ts';

export const makeWalletFactoryDriver = async (
runUtils: RunUtils,
Expand Down
Loading

0 comments on commit 4ef2be2

Please sign in to comment.