Skip to content

Commit 503eb2d

Browse files
author
Solar Mithril
authored
Account for DA and gas limit in flashblocks (#104)
* Account for DA and gas limit in flashblocks * Use fancy staff * thx karim * Use into_bytes
1 parent 0df5b08 commit 503eb2d

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

crates/op-rbuilder/src/builders/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,8 @@ impl OpPayloadBuilderCtx {
560560
}
561561

562562
/// Calculates EIP 2718 builder transaction size
563+
// TODO: this function could be improved, ideally we shouldn't take mut ref to db and maybe
564+
// it's possible to do this without db at all
563565
pub fn estimate_builder_tx_da_size<DB>(
564566
&self,
565567
db: &mut State<DB>,

crates/op-rbuilder/src/builders/flashblocks/payload.rs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ where
189189
.with_bundle_update()
190190
.build();
191191

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+
192202
let mut info = execute_pre_steps(&mut db, &ctx)?;
193203
ctx.metrics
194204
.sequencer_tx_duration
@@ -219,15 +229,22 @@ where
219229
// return early since we don't need to build a block with transactions from the pool
220230
return Ok(());
221231
}
222-
223232
let gas_per_batch = ctx.block_gas_limit() / self.config.flashblocks_per_block();
224233
let mut total_gas_per_batch = gas_per_batch;
225234
let da_per_batch = ctx
226235
.da_config
227236
.max_da_block_size()
228237
.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+
}
229245
let mut total_da_per_batch = da_per_batch;
230246

247+
let last_flashblock = self.config.flashblocks_per_block().saturating_sub(1);
231248
let mut flashblock_count = 0;
232249
// Create a channel to coordinate flashblock building
233250
let (build_tx, mut build_rx) = mpsc::channel(1);
@@ -257,13 +274,6 @@ where
257274
}
258275
});
259276

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-
267277
// Process flashblocks in a blocking loop
268278
loop {
269279
// Block on receiving a message, break on cancellation or closed channel
@@ -310,7 +320,13 @@ where
310320
);
311321
let flashblock_build_start_time = Instant::now();
312322
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+
});
314330
let mut db = State::builder()
315331
.with_database(state)
316332
.with_bundle_update()
@@ -348,10 +364,9 @@ where
348364
}
349365

350366
// 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, || {
353368
ctx.add_builder_tx(&mut info, &mut db, builder_tx_gas, message.clone());
354-
}
369+
});
355370

356371
let total_block_built_duration = Instant::now();
357372
let build_result = build_block(db, &ctx, &mut info);
@@ -669,3 +684,13 @@ where
669684
new_bundle,
670685
))
671686
}
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+
}

crates/op-rbuilder/src/builders/standard/payload.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,12 @@ impl<Txs: PayloadTxsBounds> OpBuilder<'_, Txs> {
361361
let block_da_limit = ctx
362362
.da_config
363363
.max_da_block_size()
364-
.map(|da_size| {
365-
let da_size = da_size.saturating_sub(builder_tx_da_size);
366-
if da_size == 0 {
364+
.map(|da_limit| {
365+
let da_limit = da_limit.saturating_sub(builder_tx_da_size);
366+
if da_limit == 0 {
367367
error!("Builder tx da size subtraction caused max_da_block_size to be 0. No transaction would be included.");
368368
}
369-
da_size
369+
da_limit
370370
});
371371

372372
if !ctx.attributes().no_tx_pool {

0 commit comments

Comments
 (0)