Skip to content

Commit 33d42a8

Browse files
committed
feat: use same (instrumented) kernel stepper for bootstrap
This allows us to collect and report on bootstrap crank statistics in the same way as transaction-initiated cranks after bootstrap.
1 parent 1de984e commit 33d42a8

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

packages/cosmic-swingset/lib/launch-chain.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -134,24 +134,29 @@ export async function launch(
134134

135135
// ////////////////////////////
136136
// TODO: This is where we would add the scheduler.
137-
async function endBlock(_blockHeight, _blockTime) {
137+
//
138+
// Note that the "bootstrap until no more progress" state will call this
139+
// function without any arguments.
140+
async function crankScheduler(maximumCranks = Infinity) {
138141
let now = Date.now();
139142
const blockStart = now;
140-
let stepsRemaining = FIXME_MAX_CRANKS_PER_BLOCK;
141143
let stepped = true;
142-
// TODO: how to rewrite without an await in loop?
143-
// (without blowing out the stack or promise chain)
144-
while (stepped && stepsRemaining > 0) {
144+
let numCranks = 0;
145+
while (stepped && numCranks < maximumCranks) {
145146
const crankStart = now;
146147
// eslint-disable-next-line no-await-in-loop
147148
stepped = await controller.step();
148149
now = Date.now();
149150
schedulerCrankTimeHistogram.record(now - crankStart);
150-
stepsRemaining -= 1;
151+
numCranks += 1;
151152
}
152153
schedulerBlockTimeHistogram.record((now - blockStart) / 1000);
153154
}
154155

156+
async function endBlock(_blockHeight, _blockTime) {
157+
await crankScheduler(FIXME_MAX_CRANKS_PER_BLOCK);
158+
}
159+
155160
async function saveChainState() {
156161
// Save the mailbox state.
157162
await mailboxStorage.commit();
@@ -202,9 +207,11 @@ export async function launch(
202207
}
203208
// Run the kernel until there is no more progress possible without inbound
204209
// messages.
205-
await controller.run();
210+
await crankScheduler();
206211

207-
// Commit the results, marking it so that we don't do it again.
212+
// Commit the results, with the savedHeight updated so that we don't do it
213+
// again. All future cranks will be with the scheduler in a normal block
214+
// context.
208215
savedHeight = 0;
209216
await saveOutsideState(savedHeight, savedActions, savedChainSends);
210217
}

0 commit comments

Comments
 (0)