diff --git a/packages/cosmic-swingset/src/launch-chain.js b/packages/cosmic-swingset/src/launch-chain.js index 1f8d5108c3b..f8ef579a9da 100644 --- a/packages/cosmic-swingset/src/launch-chain.js +++ b/packages/cosmic-swingset/src/launch-chain.js @@ -87,19 +87,35 @@ const parseUpgradePlanInfo = (upgradePlan, prefix = '') => { */ /** - * @typedef {'leftover' | 'forced' | 'high-priority' | 'timer' | 'queued' | 'cleanup'} CrankerPhase - * - leftover: work from a previous block - * - forced: work that claims the entirety of the current block - * - high-priority: queued work the precedes timer advancement - * - intermission: needed to note state exports and update consistency hashes - * - queued: queued work the follows timer advancement - * - cleanup: for dealing with data from terminated vats + * The phase associated with a controller run. + * - Leftover: work from a previous block + * - Forced: work that claims the entirety of the current block + * - Priority: queued work that precedes timer device advancement (e.g., oracle price updates) + * - Timer: work prompted by timer advancement to the new external time + * - Inbound: queued work that follows timer advancement (e.g., normal messages) + * - Cleanup: for dealing with data from terminated vats + * + * @enum {(typeof CrankerPhase)[keyof typeof CrankerPhase]} CrankerPhase */ +const CrankerPhase = /** @type {const} */ ({ + Leftover: 'leftover', + Forced: 'forced', + Priority: 'priority', + Timer: 'timer', + Inbound: 'inbound', + Cleanup: 'cleanup', +}); -/** @typedef {Extract} InboundQueueName */ - -/** @type {CrankerPhase} */ -const CLEANUP = 'cleanup'; +/** + * Some phases correspond with inbound message queues. + * + * @enum {(typeof InboundQueueName)[keyof typeof InboundQueueName]} InboundQueueName + */ +const InboundQueueName = /** @type {const} */ ({ + Forced: CrankerPhase.Forced, + Priority: CrankerPhase.Priority, + Inbound: CrankerPhase.Inbound, +}); /** * @typedef {(phase: CrankerPhase) => Promise} Cranker runs the kernel @@ -468,9 +484,9 @@ export async function launch({ : 0; const initialQueueLengths = /** @type {Record} */ ({ - queued: actionQueue.size(), - 'high-priority': highPriorityQueue.size(), - forced: runThisBlock.size(), + [InboundQueueName.Forced]: runThisBlock.size(), + [InboundQueueName.Priority]: highPriorityQueue.size(), + [InboundQueueName.Inbound]: actionQueue.size(), }); const { crankScheduler, inboundQueueMetrics } = exportKernelStats({ controller, @@ -488,7 +504,7 @@ export async function launch({ function makeRunSwingset(blockHeight, runPolicy) { let runNum = 0; async function runSwingset(phase) { - if (phase === CLEANUP) { + if (phase === CrankerPhase.Cleanup) { const allowCleanup = runPolicy.startCleanup(); if (!allowCleanup) return false; } @@ -535,7 +551,7 @@ export async function launch({ const runPolicy = computronCounter(params, true); const runSwingset = makeRunSwingset(blockHeight, runPolicy); - await runSwingset('forced'); + await runSwingset(CrankerPhase.Forced); } async function saveChainState() { @@ -766,13 +782,13 @@ export async function launch({ */ async function processBlockActions(runSwingset, blockHeight, blockTime) { // First, complete leftover work, if any - let keepGoing = await runSwingset('leftover'); + let keepGoing = await runSwingset(CrankerPhase.Leftover); if (!keepGoing) return; // Then, if we have anything in the special runThisBlock queue, process // it and do no further work. if (runThisBlock.size()) { - await processActions(runThisBlock, runSwingset, 'forced'); + await processActions(runThisBlock, runSwingset, CrankerPhase.Forced); return; } @@ -780,7 +796,7 @@ export async function launch({ keepGoing = await processActions( highPriorityQueue, runSwingset, - 'high-priority', + CrankerPhase.Priority, ); if (!keepGoing) return; @@ -800,14 +816,14 @@ export async function launch({ // We must run the kernel even if nothing was added since the kernel // only notes state exports and updates consistency hashes when attempting // to perform a crank. - keepGoing = await runSwingset('timer'); + keepGoing = await runSwingset(CrankerPhase.Timer); if (!keepGoing) return; // Finally, process as much as we can from the actionQueue. - await processActions(actionQueue, runSwingset, 'queued'); + await processActions(actionQueue, runSwingset, CrankerPhase.Inbound); // Cleanup after terminated vats as allowed. - await runSwingset('cleanup'); + await runSwingset(CrankerPhase.Cleanup); } async function endBlock(blockHeight, blockTime, params) { @@ -818,9 +834,9 @@ export async function launch({ // First, record new actions (bridge/mailbox/etc events that cosmos // added up for delivery to swingset) into our inboundQueue metrics const newLengths = /** @type {Record} */ ({ - queued: actionQueue.size(), - 'high-priority': highPriorityQueue.size(), - forced: runThisBlock.size(), + [InboundQueueName.Forced]: runThisBlock.size(), + [InboundQueueName.Priority]: highPriorityQueue.size(), + [InboundQueueName.Inbound]: actionQueue.size(), }); inboundQueueMetrics.updateLengths(newLengths);