@@ -189,6 +189,16 @@ where
189
189
. with_bundle_update ( )
190
190
. build ( ) ;
191
191
192
+ // We subtract gas limit and da limit for builder transaction from the whole limit
193
+ // TODO: we could optimise this and subtract this only for the last flashblocks
194
+ let message = format ! ( "Block Number: {}" , ctx. block_number( ) ) . into_bytes ( ) ;
195
+ let builder_tx_gas = ctx
196
+ . builder_signer ( )
197
+ . map_or ( 0 , |_| estimate_gas_for_builder_tx ( message. clone ( ) ) ) ;
198
+ let builder_tx_da_size = ctx
199
+ . estimate_builder_tx_da_size ( & mut db, builder_tx_gas, message. clone ( ) )
200
+ . unwrap_or ( 0 ) ;
201
+
192
202
let mut info = execute_pre_steps ( & mut db, & ctx) ?;
193
203
ctx. metrics
194
204
. sequencer_tx_duration
@@ -219,15 +229,22 @@ where
219
229
// return early since we don't need to build a block with transactions from the pool
220
230
return Ok ( ( ) ) ;
221
231
}
222
-
223
232
let gas_per_batch = ctx. block_gas_limit ( ) / self . config . flashblocks_per_block ( ) ;
224
233
let mut total_gas_per_batch = gas_per_batch;
225
234
let da_per_batch = ctx
226
235
. da_config
227
236
. max_da_block_size ( )
228
237
. map ( |da_limit| da_limit / self . config . flashblocks_per_block ( ) ) ;
238
+ // Check that builder tx won't affect fb limit too much
239
+ if let Some ( da_limit) = da_per_batch {
240
+ // We error if we can't insert any tx aside from builder tx in flashblock
241
+ if da_limit / 2 < builder_tx_da_size {
242
+ error ! ( "Builder tx da size subtraction caused max_da_block_size to be 0. No transaction would be included." ) ;
243
+ }
244
+ }
229
245
let mut total_da_per_batch = da_per_batch;
230
246
247
+ let last_flashblock = self . config . flashblocks_per_block ( ) . saturating_sub ( 1 ) ;
231
248
let mut flashblock_count = 0 ;
232
249
// Create a channel to coordinate flashblock building
233
250
let ( build_tx, mut build_rx) = mpsc:: channel ( 1 ) ;
@@ -257,13 +274,6 @@ where
257
274
}
258
275
} ) ;
259
276
260
- let message = format ! ( "Block Number: {}" , ctx. block_number( ) )
261
- . as_bytes ( )
262
- . to_vec ( ) ;
263
- let builder_tx_gas = ctx
264
- . builder_signer ( )
265
- . map_or ( 0 , |_| estimate_gas_for_builder_tx ( message. clone ( ) ) ) ;
266
-
267
277
// Process flashblocks in a blocking loop
268
278
loop {
269
279
// Block on receiving a message, break on cancellation or closed channel
@@ -310,7 +320,13 @@ where
310
320
) ;
311
321
let flashblock_build_start_time = Instant :: now ( ) ;
312
322
let state = StateProviderDatabase :: new ( & state_provider) ;
313
-
323
+ invoke_on_last_flashblock ( flashblock_count, last_flashblock, || {
324
+ total_gas_per_batch -= builder_tx_gas;
325
+ // saturating sub just in case, we will log an error if da_limit too small for builder_tx_da_size
326
+ if let Some ( da_limit) = total_da_per_batch. as_mut ( ) {
327
+ * da_limit = da_limit. saturating_sub ( builder_tx_da_size) ;
328
+ }
329
+ } ) ;
314
330
let mut db = State :: builder ( )
315
331
. with_database ( state)
316
332
. with_bundle_update ( )
@@ -348,10 +364,9 @@ where
348
364
}
349
365
350
366
// If it is the last flashblocks, add the builder txn to the block if enabled
351
- if flashblock_count == self . config . flashblocks_per_block ( ) - 1 {
352
- // TODO: Account for DA size limits
367
+ invoke_on_last_flashblock ( flashblock_count, last_flashblock, || {
353
368
ctx. add_builder_tx ( & mut info, & mut db, builder_tx_gas, message. clone ( ) ) ;
354
- }
369
+ } ) ;
355
370
356
371
let total_block_built_duration = Instant :: now ( ) ;
357
372
let build_result = build_block ( db, & ctx, & mut info) ;
@@ -669,3 +684,13 @@ where
669
684
new_bundle,
670
685
) )
671
686
}
687
+
688
+ pub fn invoke_on_last_flashblock < F : FnOnce ( ) > (
689
+ current_flashblock : u64 ,
690
+ flashblock_limit : u64 ,
691
+ fun : F ,
692
+ ) {
693
+ if current_flashblock == flashblock_limit {
694
+ fun ( )
695
+ }
696
+ }
0 commit comments