Skip to content

Commit

Permalink
chore(cosmic-swingset): Rename CrankerPhase values to be single-word …
Browse files Browse the repository at this point in the history
…values of a JSDoc enum
  • Loading branch information
gibson042 committed Jan 29, 2025
1 parent e8b99bc commit 529d2f6
Showing 1 changed file with 41 additions and 25 deletions.
66 changes: 41 additions & 25 deletions packages/cosmic-swingset/src/launch-chain.js
Original file line number Diff line number Diff line change
@@ -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<CrankerPhase, 'queued' | 'high-priority' | 'forced'>} 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<boolean>} Cranker runs the kernel
@@ -468,9 +484,9 @@ export async function launch({
: 0;

const initialQueueLengths = /** @type {Record<InboundQueueName, number>} */ ({
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,21 +782,21 @@ 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;
}

// Then, process as much as we can from the priorityQueue.
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<InboundQueueName, number>} */ ({
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);

0 comments on commit 529d2f6

Please sign in to comment.