@@ -134,24 +134,29 @@ export async function launch(
134
134
135
135
// ////////////////////////////
136
136
// 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 ) {
138
141
let now = Date . now ( ) ;
139
142
const blockStart = now ;
140
- let stepsRemaining = FIXME_MAX_CRANKS_PER_BLOCK ;
141
143
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 ) {
145
146
const crankStart = now ;
146
147
// eslint-disable-next-line no-await-in-loop
147
148
stepped = await controller . step ( ) ;
148
149
now = Date . now ( ) ;
149
150
schedulerCrankTimeHistogram . record ( now - crankStart ) ;
150
- stepsRemaining - = 1 ;
151
+ numCranks + = 1 ;
151
152
}
152
153
schedulerBlockTimeHistogram . record ( ( now - blockStart ) / 1000 ) ;
153
154
}
154
155
156
+ async function endBlock ( _blockHeight , _blockTime ) {
157
+ await crankScheduler ( FIXME_MAX_CRANKS_PER_BLOCK ) ;
158
+ }
159
+
155
160
async function saveChainState ( ) {
156
161
// Save the mailbox state.
157
162
await mailboxStorage . commit ( ) ;
@@ -202,9 +207,11 @@ export async function launch(
202
207
}
203
208
// Run the kernel until there is no more progress possible without inbound
204
209
// messages.
205
- await controller . run ( ) ;
210
+ await crankScheduler ( ) ;
206
211
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.
208
215
savedHeight = 0 ;
209
216
await saveOutsideState ( savedHeight , savedActions , savedChainSends ) ;
210
217
}
0 commit comments