Skip to content

Commit b43522f

Browse files
committed
fix: simplify build_candidate
1 parent 017718a commit b43522f

File tree

2 files changed

+55
-79
lines changed

2 files changed

+55
-79
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ where
4646
pub(super) fn mark_commited(&mut self, txs: Vec<TxHash>) {
4747
self.commited_transactions.extend(txs);
4848
}
49-
50-
/// Check if a given transaction is commited
51-
pub(super) fn is_commited(&self, tx: &TxHash) -> bool {
52-
self.commited_transactions.contains(tx)
53-
}
5449
}
5550

5651
impl<T, I> PayloadTransactions for BestFlashblocksTxs<T, I>

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

Lines changed: 55 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use reth_provider::{
3939
use reth_revm::{
4040
State, database::StateProviderDatabase, db::states::bundle_state::BundleRetention,
4141
};
42-
use reth_transaction_pool::{BestTransactions, TransactionPool, ValidPoolTransaction};
42+
use reth_transaction_pool::{BestTransactions, TransactionPool};
4343
use reth_trie::{HashedPostState, updates::TrieUpdates};
4444
use revm::Database;
4545
use rollup_boost::{
@@ -207,22 +207,6 @@ where
207207
}
208208
}
209209

210-
/// The arena in which multiple [`FlashblockCandidate`] are competing for a specific flashblock interval batch.
211-
/// It contains context of the building job, and shared precomputed elements that are required in each candidate.
212-
struct BatchArena {
213-
target_gas_for_batch: u64,
214-
target_da_for_batch: Option<u64>,
215-
}
216-
217-
/// A candidate flashblock payload within a flashblock interval batch building job
218-
struct FlashblockCandidate {
219-
payload: OpBuiltPayload,
220-
fb_payload: FlashblocksPayloadV1,
221-
execution_info: ExecutionInfo<FlashblocksExecutionInfo>,
222-
gas_used: u64,
223-
// todo metrics
224-
}
225-
226210
impl<Pool, Client, BuilderTx> OpPayloadBuilder<Pool, Client, BuilderTx>
227211
where
228212
Pool: PoolBounds,
@@ -834,30 +818,33 @@ where
834818
ctx: &OpPayloadBuilderCtx<FlashblocksExtraCtx>,
835819
state: &mut State<DB>,
836820
state_provider: impl reth::providers::StateProvider + Clone,
837-
best_txs: &NextBestFlashblocksTxs<Pool>,
838-
batch_targets: &BatchArena,
839-
) -> eyre::Result<FlashblockCandidate> {
840-
// Collect candidate best txs without updates
841-
// This is not using best_txn to be able to bound number of txs to execute and collect them, so we need to remove commited txs
842-
let candidate_best_txs: Vec<Arc<ValidPoolTransaction<Pool::Transaction>>> = self
843-
.pool
844-
.best_transactions_with_attributes(ctx.best_transaction_attributes())
845-
.without_updates()
846-
.filter(|tx| best_txs.is_commited(tx.hash()))
847-
.collect();
821+
best_txs: &mut NextBestFlashblocksTxs<Pool>,
822+
target_gas_for_batch: u64,
823+
target_da_for_batch: Option<u64>,
824+
) -> eyre::Result<(
825+
OpBuiltPayload,
826+
FlashblocksPayloadV1,
827+
ExecutionInfo<FlashblocksExecutionInfo>,
828+
)> {
829+
// Update iterator
830+
best_txs.refresh_iterator(
831+
BestPayloadTransactions::new(
832+
self.pool
833+
.best_transactions_with_attributes(ctx.best_transaction_attributes())
834+
.without_updates(),
835+
),
836+
ctx.flashblock_index(),
837+
);
848838

849839
// Initialize empty execution info
850-
let mut batch_info: ExecutionInfo<FlashblocksExecutionInfo> =
851-
ExecutionInfo::with_capacity(candidate_best_txs.len());
852-
853-
let mut candidate_best_txs = BestPayloadTransactions::new(candidate_best_txs.into_iter());
840+
let mut batch_info: ExecutionInfo<FlashblocksExecutionInfo> = ExecutionInfo::default();
854841

855842
ctx.execute_best_transactions(
856843
&mut batch_info,
857844
state, //todo
858-
&mut candidate_best_txs,
859-
batch_targets.target_gas_for_batch,
860-
batch_targets.target_da_for_batch,
845+
best_txs,
846+
target_gas_for_batch,
847+
target_da_for_batch,
861848
)
862849
.wrap_err("failed to execute best transactions")?;
863850

@@ -883,13 +870,7 @@ where
883870
.map(|(payload, mut fb)| {
884871
fb.index = ctx.flashblock_index();
885872
fb.base = None;
886-
let gas_used = fb.diff.gas_used;
887-
FlashblockCandidate {
888-
payload,
889-
fb_payload: fb,
890-
execution_info: batch_info,
891-
gas_used,
892-
}
873+
(payload, fb, batch_info)
893874
})
894875
.wrap_err("failed to build payload")
895876
}
@@ -909,8 +890,7 @@ where
909890
best_payload: &BlockCell<OpBuiltPayload>,
910891
_span: &tracing::Span,
911892
) -> eyre::Result<Option<FlashblocksExtraCtx>> {
912-
// 1. --- Prepare batch arena with shared context ---
913-
let flashblock_index = ctx.flashblock_index();
893+
// 1. --- Prepare shared context ---
914894
let mut target_gas_for_batch = ctx.extra_ctx.target_gas_for_batch;
915895
let mut target_da_for_batch = ctx.extra_ctx.target_da_for_batch;
916896

@@ -931,21 +911,15 @@ where
931911
*da_limit = da_limit.saturating_sub(builder_tx_da_size);
932912
}
933913

934-
// update the batch best txs with a dynamic iterator
935-
best_txs.refresh_iterator(
936-
BestPayloadTransactions::new(
937-
self.pool
938-
.best_transactions_with_attributes(ctx.best_transaction_attributes()),
939-
),
940-
flashblock_index,
941-
);
914+
let target_gas_for_batch = target_gas_for_batch.min(ctx.block_gas_limit());
942915

943-
let batch_targets = BatchArena {
944-
target_gas_for_batch: target_gas_for_batch.min(ctx.block_gas_limit()),
945-
target_da_for_batch,
946-
};
916+
let mut best: Option<(
917+
OpBuiltPayload,
918+
FlashblocksPayloadV1,
919+
ExecutionInfo<FlashblocksExecutionInfo>,
920+
)> = None;
947921

948-
let mut best: Option<FlashblockCandidate> = None;
922+
// 2. --- Build candidates and update best ---
949923
loop {
950924
// If main token got canceled in here that means we received get_payload and we should drop everything and not update best_payload
951925
// To ensure that we will return same blocks as rollup-boost (to leverage caches)
@@ -959,11 +933,18 @@ where
959933

960934
// Build one candidate (blocking here)
961935
// todo: would be best to build async and select on candidate_build/block_cancel/fb_cancel
962-
match self.build_candidate(&*ctx, state, &state_provider, &*best_txs, &batch_targets) {
936+
match self.build_candidate(
937+
&*ctx,
938+
state,
939+
&state_provider,
940+
best_txs,
941+
target_gas_for_batch,
942+
target_da_for_batch,
943+
) {
963944
Ok(candidate) => {
964945
if best
965946
.as_ref()
966-
.is_none_or(|b| candidate.gas_used > b.gas_used)
947+
.is_none_or(|b| candidate.1.diff.gas_used > b.1.diff.gas_used)
967948
{
968949
best = Some(candidate);
969950
}
@@ -974,38 +955,38 @@ where
974955
}
975956
}
976957

977-
let mut best = best.wrap_err("No best flashblock payload")?;
958+
// 3. --- Cancellation token received, send best ---
959+
let (payload, fb_payload, mut execution_info) =
960+
best.wrap_err("No best flashblock payload")?;
978961

979962
// Directly send payloads
980963
let _flashblock_byte_size = self
981964
.ws_pub
982-
.publish(&best.fb_payload)
965+
.publish(&fb_payload)
983966
.wrap_err("failed to publish flashblock via websocket")?;
984-
self.send_payload_to_engine(best.payload.clone());
985-
best_payload.set(best.payload);
967+
self.send_payload_to_engine(payload.clone());
968+
best_payload.set(payload);
986969

987970
// Apply state mutations from best
988-
// todo: update best_txs to take into account candidate_best_txs?
989-
let batch_new_transactions = best
990-
.execution_info
971+
let batch_new_transactions = execution_info
991972
.executed_transactions
992973
.to_vec()
993974
.iter()
994975
.map(|tx| tx.tx_hash())
995976
.collect::<Vec<_>>();
996-
// update batch best txs
977+
// update best txns
997978
best_txs.mark_commited(batch_new_transactions);
998979

999980
// update batch execution info
1000981
info.executed_transactions
1001-
.append(&mut best.execution_info.executed_transactions);
982+
.append(&mut execution_info.executed_transactions);
1002983
info.executed_senders
1003-
.append(&mut best.execution_info.executed_senders);
1004-
info.receipts.append(&mut best.execution_info.receipts);
1005-
info.cumulative_gas_used += best.execution_info.cumulative_gas_used;
1006-
info.cumulative_da_bytes_used += best.execution_info.cumulative_da_bytes_used;
1007-
info.total_fees += best.execution_info.total_fees;
1008-
info.extra.last_flashblock_index = best.execution_info.extra.last_flashblock_index;
984+
.append(&mut execution_info.executed_senders);
985+
info.receipts.append(&mut execution_info.receipts);
986+
info.cumulative_gas_used += execution_info.cumulative_gas_used;
987+
info.cumulative_da_bytes_used += execution_info.cumulative_da_bytes_used;
988+
info.total_fees += execution_info.total_fees;
989+
info.extra.last_flashblock_index = execution_info.extra.last_flashblock_index;
1009990

1010991
// todo state
1011992

0 commit comments

Comments
 (0)