Skip to content

Commit

Permalink
refactor: use Fail template tag in SwingSet (#6619)
Browse files Browse the repository at this point in the history
* refactor: use Fail template tag in SwingSet

* fix: review suggestions
  • Loading branch information
erights authored Dec 7, 2022
1 parent 8e71c07 commit 162fdf2
Show file tree
Hide file tree
Showing 72 changed files with 392 additions and 412 deletions.
6 changes: 3 additions & 3 deletions packages/SwingSet/src/controller/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Worker } from 'worker_threads';
import anylogger from 'anylogger';
import microtime from 'microtime';

import { assert, details as X } from '@agoric/assert';
import { assert, Fail } from '@agoric/assert';
import { importBundle } from '@endo/import-bundle';
import { xsnap, recordXSnap } from '@agoric/xsnap';

Expand Down Expand Up @@ -148,7 +148,7 @@ export function makeStartXSnap(bundles, { snapStore, env, spawn }) {
for (const bundle of bundles) {
bundle.moduleFormat === 'getExport' ||
bundle.moduleFormat === 'nestedEvaluate' ||
assert.fail(X`unexpected: ${bundle.moduleFormat}`);
Fail`unexpected: ${bundle.moduleFormat}`;
// eslint-disable-next-line no-await-in-loop, @jessie.js/no-nested-await
await worker.evaluate(`(${bundle.source}\n)()`.trim());
}
Expand Down Expand Up @@ -246,7 +246,7 @@ export async function makeSwingsetController(
}

function kernelRequire(what) {
assert.fail(X`kernelRequire unprepared to satisfy require(${what})`);
Fail`kernelRequire unprepared to satisfy require(${what})`;
}
writeSlogObject({ type: 'import-kernel-start' });
const kernelNS = await importBundle(kernelBundle, {
Expand Down
12 changes: 5 additions & 7 deletions packages/SwingSet/src/controller/initializeKernel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-use-before-define */

import { makeMarshal, Far } from '@endo/marshal';
import { assert, details as X } from '@agoric/assert';
import { assert, Fail } from '@agoric/assert';
import { createSHA256 } from '../lib-nodejs/hasher.js';
import { assertKnownOptions } from '../lib/assertOptions.js';
import { insistVatID } from '../lib/id.js';
Expand Down Expand Up @@ -65,8 +65,7 @@ export function initializeKernel(config, hostStorage, verbose = false) {
creationOptions = {},
} = config.vats[name];
logStartup(`adding vat '${name}' from bundle ${bundleID}`);

assert(bundleID, X`no bundleID specified for vat ${name}`);
bundleID || Fail`no bundleID specified for vat ${name}`;

// todo: consider having vats indicate 'enablePipelining' by exporting a
// boolean, rather than options= . We'd have to retrieve the flag from
Expand Down Expand Up @@ -112,7 +111,7 @@ export function initializeKernel(config, hostStorage, verbose = false) {
gotVatAdminRootKref = true;
}
}
assert(gotVatAdminRootKref, X`a vat admin vat is required`);
gotVatAdminRootKref || Fail`a vat admin vat is required`;
}

// generate the devices
Expand All @@ -125,8 +124,7 @@ export function initializeKernel(config, hostStorage, verbose = false) {
creationOptions = {},
} = config.devices[name];
logStartup(`adding device '${name}' from bundle ${bundleID}`);

assert(bundleID, X`no bundleID for device ${name}`);
bundleID || Fail`no bundleID for device ${name}`;
creationOptions.deviceParameters = deviceParameters;

const deviceID = kernelKeeper.allocateDeviceIDForNameIfNeeded(name);
Expand All @@ -137,7 +135,7 @@ export function initializeKernel(config, hostStorage, verbose = false) {
haveAdminDevice = true;
}
}
assert(haveAdminDevice, X`a vat admin device is required`);
haveAdminDevice || Fail`a vat admin device is required`;
}

// And enqueue the bootstrap() call. If we're reloading from an
Expand Down
13 changes: 5 additions & 8 deletions packages/SwingSet/src/controller/initializeSwingset.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs';
import path from 'path';

import { resolve as resolveModuleSpecifier } from 'import-meta-resolve';
import { assert, details as X } from '@agoric/assert';
import { assert, Fail } from '@agoric/assert';
import bundleSource from '@endo/bundle-source';

import '../types-ambient.js';
Expand Down Expand Up @@ -238,11 +238,9 @@ export async function loadSwingsetConfigFile(configPath) {
await normalizeConfigDescriptor(config.vats, referrer, true);
await normalizeConfigDescriptor(config.bundles, referrer, false);
// await normalizeConfigDescriptor(config.devices, referrer, true); // TODO: represent devices
assert(config.bootstrap, X`no designated bootstrap vat in ${configPath}`);
config.bootstrap || Fail`no designated bootstrap vat in ${configPath}`;
(config.vats && config.vats[config.bootstrap]) ||
assert.fail(
X`bootstrap vat ${config.bootstrap} not found in ${configPath}`,
);
Fail`bootstrap vat ${config.bootstrap} not found in ${configPath}`;
return config;
} catch (e) {
if (e.code === 'ENOENT') {
Expand Down Expand Up @@ -295,8 +293,7 @@ export async function initializeSwingset(
) {
const kvStore = hostStorage.kvStore;
insistStorageAPI(kvStore);
!swingsetIsInitialized(hostStorage) ||
assert.fail(X`kernel store already initialized`);
!swingsetIsInitialized(hostStorage) || Fail`kernel store already initialized`;

// copy config so we can safely mess with it even if it's shared or hardened
config = JSON.parse(JSON.stringify(config));
Expand Down Expand Up @@ -327,7 +324,7 @@ export async function initializeSwingset(
config.defaultManagerType = 'local';
break;
default:
assert.fail(X`unknown manager type ${defaultManagerType}`);
Fail`unknown manager type ${defaultManagerType}`;
}

const obtainKernelBundles = async () =>
Expand Down
4 changes: 2 additions & 2 deletions packages/SwingSet/src/devices/bridge/device-bridge.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, details as X } from '@agoric/assert';
import { Fail } from '@agoric/assert';
import { Far } from '@endo/marshal';

function sanitize(data) {
Expand Down Expand Up @@ -28,7 +28,7 @@ export function buildRootDeviceNode(tools) {
let { inboundHandler } = getDeviceState() || {};

function inboundCallback(...args) {
assert(inboundHandler, X`inboundHandler not yet registered`);
inboundHandler || Fail`inboundHandler not yet registered`;
const safeArgs = JSON.parse(JSON.stringify(args));
try {
SO(inboundHandler).inbound(...harden(safeArgs));
Expand Down
10 changes: 5 additions & 5 deletions packages/SwingSet/src/devices/command/command.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { makePromiseKit } from '@endo/promise-kit';
import { Nat } from '@agoric/nat';

import { assert, details as X } from '@agoric/assert';
import { Fail } from '@agoric/assert';

export default function buildCommand(broadcastCallback) {
assert(broadcastCallback, X`broadcastCallback must be provided.`);
broadcastCallback || Fail`broadcastCallback must be provided.`;
let inboundCallback;
const srcPath = new URL('device-command.js', import.meta.url).pathname;
let nextCount = 0n;
Expand All @@ -18,7 +18,7 @@ export default function buildCommand(broadcastCallback) {
const count = nextCount;
nextCount += 1n;
responses.set(count, { resolve, reject });
assert(inboundCallback, X`inboundCommand before registerInboundCallback`);
inboundCallback || Fail`inboundCommand before registerInboundCallback`;
try {
inboundCallback(count, JSON.stringify(obj));
} catch (e) {
Expand All @@ -33,7 +33,7 @@ export default function buildCommand(broadcastCallback) {
}

function registerInboundCallback(cb) {
assert(!inboundCallback, X`registerInboundCallback called more than once`);
!inboundCallback || Fail`registerInboundCallback called more than once`;
inboundCallback = cb;
}

Expand All @@ -48,7 +48,7 @@ export default function buildCommand(broadcastCallback) {
}
// TODO this might not qualify as an error, it needs more thought
// See https://github.com/Agoric/agoric-sdk/pull/2406#discussion_r575561554
assert(responses.has(count), X`unknown response index ${count}`);
responses.has(count) || Fail`unknown response index ${count}`;
const { resolve, reject } = responses.get(count);
if (isReject) {
reject(obj);
Expand Down
4 changes: 2 additions & 2 deletions packages/SwingSet/src/devices/command/device-command.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Nat } from '@agoric/nat';
import { Far } from '@endo/marshal';

import { assert, details as X } from '@agoric/assert';
import { Fail } from '@agoric/assert';

export function buildRootDeviceNode(tools) {
const { SO, getDeviceState, setDeviceState, endowments } = tools;
Expand All @@ -20,7 +20,7 @@ export function buildRootDeviceNode(tools) {
SO(inboundHandler).inbound(Nat(count), body);
} catch (e) {
console.error(`error during inboundCallback:`, e);
assert.fail(X`error during inboundCallback: ${e}`);
Fail`error during inboundCallback: ${e}`;
}
});

Expand Down
12 changes: 6 additions & 6 deletions packages/SwingSet/src/devices/lib/deviceTools.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, details as X } from '@agoric/assert';
import { assert, Fail } from '@agoric/assert';
import { makeMarshal, Far } from '@endo/marshal';
import { parseVatSlot } from '../../lib/parseVatSlots.js';

Expand Down Expand Up @@ -47,16 +47,16 @@ export function buildSerializationTools(syscall, deviceName) {
function convertSlotToVal(slot) {
const { type, allocatedByVat } = parseVatSlot(slot);
if (type === 'object') {
assert(!allocatedByVat, X`devices cannot yet allocate objects ${slot}`);
!allocatedByVat || Fail`devices cannot yet allocate objects ${slot}`;
return presenceForSlot(slot);
} else if (type === 'device') {
allocatedByVat ||
assert.fail(X`devices should yet not be given other devices '${slot}'`);
Fail`devices should yet not be given other devices '${slot}'`;
return deviceNodeForSlot(slot);
} else if (type === 'promise') {
assert.fail(X`devices should not yet be given promises '${slot}'`);
throw Fail`devices should not yet be given promises '${slot}'`;
} else {
assert.fail(X`unrecognized slot type '${type}'`);
throw Fail`unrecognized slot type '${type}'`;
}
}

Expand All @@ -69,7 +69,7 @@ export function buildSerializationTools(syscall, deviceName) {
if (devnodeSlot) {
return devnodeSlot;
}
assert.fail(X`unable to convert value ${val}`);
throw Fail`unable to convert value ${val}`;
}

const m = makeMarshal(convertValToSlot, convertSlotToVal, {
Expand Down
6 changes: 3 additions & 3 deletions packages/SwingSet/src/devices/loopbox/device-loopbox.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, details as X } from '@agoric/assert';
import { Fail } from '@agoric/assert';
import { Far } from '@endo/marshal';

export function buildRootDeviceNode(tools) {
Expand All @@ -10,7 +10,7 @@ export function buildRootDeviceNode(tools) {
return Far('sender', {
add(peer, msgnum, body) {
const oldDS = getDeviceState();
assert(oldDS.inboundHandlers[peer], X`unregistered peer '${peer}'`);
oldDS.inboundHandlers[peer] || Fail`unregistered peer '${peer}'`;
const h = oldDS.inboundHandlers[peer];
const newDS = { ...oldDS };
if (deliverMode === 'immediate') {
Expand Down Expand Up @@ -89,7 +89,7 @@ export function buildRootDeviceNode(tools) {
return Far('root', {
registerInboundHandler(name, handler) {
const oldDS = getDeviceState();
assert(!oldDS.inboundHandlers[name], X`already registered`);
!oldDS.inboundHandlers[name] || Fail`already registered`;
const newDS = { ...oldDS };
newDS.inboundHandlers = { ...newDS.inboundHandlers };
newDS.inboundHandlers[name] = handler;
Expand Down
4 changes: 2 additions & 2 deletions packages/SwingSet/src/devices/loopbox/loopbox.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, details as X } from '@agoric/assert';
import { Fail } from '@agoric/assert';

/*
* The "loopbox" is a special device used for unit tests, which glues one
Expand All @@ -19,7 +19,7 @@ import { assert, details as X } from '@agoric/assert';
export function buildLoopbox(deliverMode) {
deliverMode === 'immediate' ||
deliverMode === 'queued' ||
assert.fail(X`deliverMode=${deliverMode}, must be 'immediate' or 'queued'`);
Fail`deliverMode=${deliverMode}, must be 'immediate' or 'queued'`;
const loopboxSrcPath = new URL('device-loopbox.js', import.meta.url).pathname;

let loopboxPassOneMessage;
Expand Down
14 changes: 7 additions & 7 deletions packages/SwingSet/src/devices/mailbox/device-mailbox.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Nat } from '@agoric/nat';
import { Far } from '@endo/marshal';

import { assert, details as X } from '@agoric/assert';
import { assert, Fail } from '@agoric/assert';

export function buildRootDeviceNode(tools) {
const { SO, getDeviceState, setDeviceState, endowments } = tools;
Expand Down Expand Up @@ -36,7 +36,7 @@ export function buildRootDeviceNode(tools) {
// console.debug(`device-mailbox build: inboundHandler is`, inboundHandler);
deliverInboundMessages = (peer, newMessages) => {
inboundHandler ||
assert.fail(X`deliverInboundMessages before registerInboundHandler`);
Fail`deliverInboundMessages before registerInboundHandler`;
try {
SO(inboundHandler).deliverInboundMessages(peer, newMessages);
} catch (e) {
Expand All @@ -45,7 +45,7 @@ export function buildRootDeviceNode(tools) {
};

deliverInboundAck = (peer, ack) => {
assert(inboundHandler, X`deliverInboundAck before registerInboundHandler`);
inboundHandler || Fail`deliverInboundAck before registerInboundHandler`;
try {
SO(inboundHandler).deliverInboundAck(peer, ack);
} catch (e) {
Expand All @@ -56,7 +56,7 @@ export function buildRootDeviceNode(tools) {
// the Root Device Node.
return Far('root', {
registerInboundHandler(handler) {
assert(!inboundHandler, X`already registered`);
!inboundHandler || Fail`already registered`;
inboundHandler = handler;
setDeviceState(harden({ inboundHandler }));
},
Expand All @@ -69,23 +69,23 @@ export function buildRootDeviceNode(tools) {
try {
endowments.add(`${peer}`, Nat(msgnum), `${body}`);
} catch (e) {
assert.fail(X`error in add: ${e}`);
Fail`error in add: ${e}`;
}
},

remove(peer, msgnum) {
try {
endowments.remove(`${peer}`, Nat(msgnum));
} catch (e) {
assert.fail(X`error in remove: ${e}`);
Fail`error in remove: ${e}`;
}
},

ackInbound(peer, msgnum) {
try {
endowments.setAcknum(`${peer}`, Nat(msgnum));
} catch (e) {
assert.fail(X`error in ackInbound: ${e}`);
Fail`error in ackInbound: ${e}`;
}
},
});
Expand Down
6 changes: 3 additions & 3 deletions packages/SwingSet/src/devices/mailbox/mailbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

import { Nat } from '@agoric/nat';

import { assert, details as X } from '@agoric/assert';
import { Fail } from '@agoric/assert';

// This Map-based mailboxState object is a good starting point, but we may
// replace it with one that tracks which parts of the state have been
Expand Down Expand Up @@ -132,7 +132,7 @@ export function buildMailboxStateMap(state = harden(new Map())) {
}

function populateFromData(data) {
assert(!state.size, X`cannot populateFromData: outbox is not empty`);
!state.size || Fail`cannot populateFromData: outbox is not empty`;
for (const peer of Object.getOwnPropertyNames(data)) {
const inout = getOrCreatePeer(peer);
const d = data[peer];
Expand Down Expand Up @@ -185,7 +185,7 @@ export function buildMailbox(state) {
try {
return Boolean(inboundCallback(peer, messages, ack));
} catch (e) {
assert.fail(X`error in inboundCallback: ${e}`);
throw Fail`error in inboundCallback: ${e}`;
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/SwingSet/src/devices/plugin/device-plugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { makeCapTP } from '@endo/captp';
import { Far } from '@endo/marshal';
import { assert, details as X } from '@agoric/assert';
import { assert, details as X, Fail } from '@agoric/assert';

export function buildRootDeviceNode(tools) {
const { SO, getDeviceState, setDeviceState, endowments } = tools;
Expand Down Expand Up @@ -148,7 +148,7 @@ export function buildRootDeviceNode(tools) {
connect,
send,
registerReceiver(receiver) {
assert(!registeredReceiver, X`registered receiver already set`);
!registeredReceiver || Fail`registered receiver already set`;
registeredReceiver = receiver;
saveState();
},
Expand Down
6 changes: 2 additions & 4 deletions packages/SwingSet/src/devices/timer/device-timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

import { Nat } from '@agoric/nat';
import { assert, details as X } from '@agoric/assert';
import { assert, Fail } from '@agoric/assert';
import { Far } from '@endo/marshal';

// Since we use harden when saving the state, we need to copy the arrays so they
Expand Down Expand Up @@ -244,9 +244,7 @@ export function buildRootDeviceNode(tools) {

function updateTime(time) {
time >= lastPolled ||
assert.fail(
X`Time is monotonic. ${time} cannot be less than ${lastPolled}`,
);
Fail`Time is monotonic. ${time} cannot be less than ${lastPolled}`;
lastPolled = time;
saveState();
}
Expand Down
Loading

0 comments on commit 162fdf2

Please sign in to comment.