Skip to content

Commit

Permalink
chore: use inline Fail instead of assert
Browse files Browse the repository at this point in the history
  • Loading branch information
FUDCo committed Mar 1, 2023
1 parent ae287c8 commit 291941e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 62 deletions.
16 changes: 9 additions & 7 deletions packages/SwingSet/test/test-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,15 @@ function duplicateKeeper(serialize) {

test('kernelStorage param guards', async t => {
const { kvStore } = buildKeeperStorageInMemory();
const exp = { message: /true must be a string/ };
t.throws(() => kvStore.set('foo', true), exp);
t.throws(() => kvStore.set(true, 'foo'), exp);
t.throws(() => kvStore.has(true), exp);
t.throws(() => kvStore.getNextKey(true), exp);
t.throws(() => kvStore.get(true), exp);
t.throws(() => kvStore.delete(true), exp);
const expv = { message: /value must be a string/ };
const expk = { message: /key must be a string/ };
const exppk = { message: /previousKey must be a string/ };
t.throws(() => kvStore.set('foo', true), expv);
t.throws(() => kvStore.set(true, 'foo'), expk);
t.throws(() => kvStore.has(true), expk);
t.throws(() => kvStore.getNextKey(true), exppk);
t.throws(() => kvStore.get(true), expk);
t.throws(() => kvStore.delete(true), expk);
});

test('kernel state', async t => {
Expand Down
6 changes: 3 additions & 3 deletions packages/swing-store/src/hasher.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert } from '@agoric/assert';
import { Fail } from '@agoric/assert';

import { createHash } from 'crypto';

Expand All @@ -19,12 +19,12 @@ function createSHA256(initial = undefined) {
// eslint-disable-next-line no-use-before-define
const self = harden({ add, finish, sample });
function add(more) {
assert(!done);
!done || Fail`hash already finished`;
hash.update(more);
return self;
}
function finish() {
assert(!done);
!done || Fail`hash already finished`;
done = true;
return hash.digest('hex');
}
Expand Down
34 changes: 15 additions & 19 deletions packages/swing-store/src/snapStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createHash } from 'crypto';
import { finished as finishedCallback, Readable } from 'stream';
import { promisify } from 'util';
import { createGzip, createGunzip } from 'zlib';
import { assert, details as d } from '@agoric/assert';
import { Fail, q } from '@agoric/assert';
import { aggregateTryFinally, PromiseAllOrErrors } from '@agoric/internal';
import { fsStreamReady } from '@agoric/internal/src/fs-stream.js';

Expand Down Expand Up @@ -297,7 +297,7 @@ export function makeSnapStore(
return aggregateTryFinally(
async () => {
const loadInfo = sqlLoadSnapshot.get(vatID);
assert(loadInfo, `no snapshot available for vat ${vatID}`);
loadInfo || Fail`no snapshot available for vat ${q(vatID)}`;
const { hash, compressedSnapshot } = loadInfo;
const gzReader = Readable.from(compressedSnapshot);
cleanup.push(() => gzReader.destroy());
Expand All @@ -322,7 +322,7 @@ export function makeSnapStore(

await Promise.all([finished(gzReader), finished(snapWriter)]);
const h = hashStream.digest('hex');
h === hash || assert.fail(d`actual hash ${h} !== expected ${hash}`);
h === hash || Fail`actual hash ${q(h)} !== expected ${q(hash)}`;
const snapWriterClose = cleanup.pop();
snapWriterClose();

Expand Down Expand Up @@ -457,19 +457,16 @@ export function makeSnapStore(
async function importSnapshot(name, exporter, info) {
const parts = name.split('.');
const [type, vatID, rawEndPos] = parts;
assert(
parts.length === 3 && type === 'snapshot',
`expected snapshot name of the form 'snapshot.{vatID}.{endPos}', saw '${name}'`,
);
assert(
info.vatID === vatID,
`snapshot name says vatID ${vatID}, metadata says ${info.vatID}`,
);
// prettier-ignore
parts.length === 3 && type === 'snapshot' ||
Fail`expected snapshot name of the form 'snapshot.{vatID}.{endPos}', saw '${q(name)}'`;
// prettier-ignore
info.vatID === vatID ||
Fail`snapshot name says vatID ${q(vatID)}, metadata says ${q(info.vatID)}`;
const endPos = Number(rawEndPos);
assert(
info.endPos === endPos,
`snapshot name says endPos ${endPos}, metadata says ${info.endPos}`,
);
// prettier-ignore
info.endPos === endPos ||
Fail`snapshot name says endPos ${q(endPos)}, metadata says ${q(info.endPos)}`;

const artifactChunks = exporter.getArtifact(name);
const inStream = Readable.from(artifactChunks);
Expand All @@ -482,10 +479,9 @@ export function makeSnapStore(
const compressedArtifact = await buffer(gzip);
await finished(inStream);
const hash = hashStream.digest('hex');
assert(
info.hash === hash,
`snapshot ${name} hash is ${hash}, metadata says ${info.hash}`,
);
// prettier-ignore
info.hash === hash ||
Fail`snapshot ${q(name)} hash is ${q(hash)}, metadata says ${q(info.hash)}`;
sqlSaveSnapshot.run(
vatID,
endPos,
Expand Down
53 changes: 22 additions & 31 deletions packages/swing-store/src/transcriptStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import ReadlineTransform from 'readline-transform';
import { Readable } from 'stream';
import { Buffer } from 'buffer';
import { assert, Fail, q } from '@agoric/assert';
import { Fail, q } from '@agoric/assert';
import { createSHA256 } from './hasher.js';

/**
Expand Down Expand Up @@ -34,13 +34,13 @@ function* empty() {
}

/**
* @param {unknown} position
* @param {number} position
* @returns {asserts position is number}
*/

function insistTranscriptPosition(position) {
assert.typeof(position, 'number');
assert(position >= 0);
typeof position === 'number' || Fail`position must be a number`;
position >= 0 || Fail`position must not be negative`;
}

/**
Expand Down Expand Up @@ -170,7 +170,7 @@ export function makeTranscriptStore(db, ensureTxn, noteExport) {
*/
function getCurrentSpanBounds(vatID) {
const bounds = sqlGetCurrentSpanBounds.get(vatID);
assert(bounds, `no current transcript for ${vatID}`);
bounds || Fail`no current transcript for ${q(vatID)}`;
return bounds;
}

Expand Down Expand Up @@ -300,13 +300,10 @@ export function makeTranscriptStore(db, ensureTxn, noteExport) {
if (startPos === undefined) {
({ startPos, endPos } = getCurrentSpanBounds(vatID));
} else {
assert.typeof(startPos, 'number');
insistTranscriptPosition(startPos);
endPos = sqlGetSpanEndPos.get(vatID, startPos);
assert.typeof(
endPos,
'number',
`no transcript span for ${vatID} at ${startPos}`,
);
typeof endPos === 'number' ||
Fail`no transcript span for ${q(vatID)} at ${q(startPos)}`;
}
insistTranscriptPosition(startPos);
startPos <= endPos || Fail`${q(startPos)} <= ${q(endPos)}}`;
Expand Down Expand Up @@ -384,24 +381,20 @@ export function makeTranscriptStore(db, ensureTxn, noteExport) {
async function importSpan(name, exporter, info) {
const parts = name.split('.');
const [type, vatID, rawStartPos, rawEndPos] = parts;
assert(
parts.length === 4 && type === 'transcript',
`expected artifact name of the form 'transcript.{vatID}.{startPos}.{endPos}', saw '${name}'`,
);
assert(
info.vatID === vatID,
`artifact name says vatID ${vatID}, metadata says ${info.vatID}`,
);
// prettier-ignore
parts.length === 4 && type === 'transcript' ||
Fail`expected artifact name of the form 'transcript.{vatID}.{startPos}.{endPos}', saw '${q(name)}'`;
// prettier-ignore
info.vatID === vatID ||
Fail`artifact name says vatID ${q(vatID)}, metadata says ${q(info.vatID)}`;
const startPos = Number(rawStartPos);
assert(
info.startPos === startPos,
`artifact name says startPos ${startPos}, metadata says ${info.startPos}`,
);
// prettier-ignore
info.startPos === startPos ||
Fail`artifact name says startPos ${q(startPos)}, metadata says ${q(info.startPos)}`;
const endPos = Number(rawEndPos);
assert(
info.endPos === endPos,
`artifact name says endPos ${endPos}, metadata says ${info.endPos}`,
);
// prettier-ignore
info.endPos === endPos ||
Fail`artifact name says endPos ${q(endPos)}, metadata says ${q(info.endPos)}`;
const artifactChunks = exporter.getArtifact(name);
const inStream = Readable.from(artifactChunks);
const lineTransform = new ReadlineTransform();
Expand All @@ -413,10 +406,8 @@ export function makeTranscriptStore(db, ensureTxn, noteExport) {
hash = computeItemHash(hash, item);
pos += 1;
}
assert(
info.hash === hash,
`artifact ${name} hash is ${hash}, metadata says ${info.hash}`,
);
info.hash === hash ||
Fail`artifact ${name} hash is ${q(hash)}, metadata says ${q(info.hash)}`;
sqlWriteSpan.run(
info.vatID,
info.startPos,
Expand Down
4 changes: 2 additions & 2 deletions packages/swing-store/test/test-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ async function testTranscriptStore(t, dbDir) {
t.deepEqual(Array.from(reader2), ['oneth', 'twoth', 'threeth', 'fourst']);

t.throws(() => transcriptStore.readSpan('st2', 3), {
message: 'no transcript span for st2 at 3',
message: 'no transcript span for "st2" at 3',
});

const reader1alt = transcriptStore.readSpan('st1');
Expand All @@ -185,7 +185,7 @@ async function testTranscriptStore(t, dbDir) {
t.deepEqual(Array.from(readerEmpty), []);

t.throws(() => transcriptStore.readSpan('nonexistent'), {
message: 'no current transcript for nonexistent',
message: 'no current transcript for "nonexistent"',
});

await commit();
Expand Down

0 comments on commit 291941e

Please sign in to comment.