diff --git a/packages/SwingSet/test/test-state.js b/packages/SwingSet/test/test-state.js index e5946c367e5a..72932c659aa6 100644 --- a/packages/SwingSet/test/test-state.js +++ b/packages/SwingSet/test/test-state.js @@ -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 => { diff --git a/packages/swing-store/src/hasher.js b/packages/swing-store/src/hasher.js index 4907745a94a7..17153494b35c 100644 --- a/packages/swing-store/src/hasher.js +++ b/packages/swing-store/src/hasher.js @@ -1,4 +1,4 @@ -import { assert } from '@agoric/assert'; +import { Fail } from '@agoric/assert'; import { createHash } from 'crypto'; @@ -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'); } diff --git a/packages/swing-store/src/snapStore.js b/packages/swing-store/src/snapStore.js index dc2c8fe340ac..d0bea79d2a66 100644 --- a/packages/swing-store/src/snapStore.js +++ b/packages/swing-store/src/snapStore.js @@ -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'; @@ -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()); @@ -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(); @@ -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); @@ -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, diff --git a/packages/swing-store/src/transcriptStore.js b/packages/swing-store/src/transcriptStore.js index 720c3ef1852d..68527e381e54 100644 --- a/packages/swing-store/src/transcriptStore.js +++ b/packages/swing-store/src/transcriptStore.js @@ -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'; /** @@ -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`; } /** @@ -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; } @@ -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)}}`; @@ -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(); @@ -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, diff --git a/packages/swing-store/test/test-state.js b/packages/swing-store/test/test-state.js index 68534a200f38..fc53d03efea2 100644 --- a/packages/swing-store/test/test-state.js +++ b/packages/swing-store/test/test-state.js @@ -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'); @@ -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();