Skip to content

Commit e5c72dc

Browse files
committed
feat: add metrics
1 parent 4c05ff8 commit e5c72dc

File tree

2 files changed

+93
-20
lines changed

2 files changed

+93
-20
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use alloy_consensus::transaction::TxHashRef;
2-
use alloy_consensus::{Eip658Value, Transaction, conditional::BlockConditionalAttributes};
1+
use alloy_consensus::{
2+
Eip658Value, Transaction, conditional::BlockConditionalAttributes, transaction::TxHashRef,
3+
};
34
use alloy_eips::Typed2718;
45
use alloy_evm::Database;
56
use alloy_op_evm::block::receipt_builder::OpReceiptBuilder;

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

Lines changed: 90 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@ where
862862
simulation_state.transition_state = simulation_transition_state;
863863

864864
// Refresh pool txs
865+
let best_txs_start_time = Instant::now();
865866
best_txs.refresh_iterator(
866867
BestPayloadTransactions::new(
867868
self.pool
@@ -870,7 +871,15 @@ where
870871
),
871872
ctx.flashblock_index(),
872873
);
874+
let transaction_pool_fetch_time = best_txs_start_time.elapsed();
875+
ctx.metrics
876+
.transaction_pool_fetch_duration
877+
.record(transaction_pool_fetch_time);
878+
ctx.metrics
879+
.transaction_pool_fetch_gauge
880+
.set(transaction_pool_fetch_time);
873881

882+
let tx_execution_start_time = Instant::now();
874883
ctx.simulate_best_transactions(
875884
&mut simulation_info,
876885
&mut simulation_state,
@@ -879,6 +888,13 @@ where
879888
target_da_for_batch,
880889
)
881890
.wrap_err("failed to execute best transactions")?;
891+
let payload_transaction_simulation_time = tx_execution_start_time.elapsed();
892+
ctx.metrics
893+
.payload_transaction_simulation_duration
894+
.record(payload_transaction_simulation_time);
895+
ctx.metrics
896+
.payload_transaction_simulation_gauge
897+
.set(payload_transaction_simulation_time);
882898

883899
// Try early return condition
884900
if block_cancel.is_cancelled() {
@@ -909,24 +925,39 @@ where
909925
}
910926

911927
// build block and return new best
912-
build_block(
928+
let total_block_built_duration = Instant::now();
929+
let build_result = build_block(
913930
&mut simulation_state,
914931
ctx,
915932
&mut simulation_info,
916933
ctx.extra_ctx.calculate_state_root || ctx.attributes().no_tx_pool,
917-
)
918-
.map(|(payload, mut fb)| {
919-
fb.index = ctx.flashblock_index();
920-
fb.base = None;
921-
Some((
922-
payload,
923-
fb,
924-
simulation_info,
925-
simulation_state.cache,
926-
simulation_state.transition_state,
927-
))
928-
})
929-
.wrap_err("failed to build payload")
934+
);
935+
let total_block_built_duration = total_block_built_duration.elapsed();
936+
ctx.metrics
937+
.total_block_built_duration
938+
.record(total_block_built_duration);
939+
ctx.metrics
940+
.total_block_built_gauge
941+
.set(total_block_built_duration);
942+
943+
match build_result {
944+
Err(err) => {
945+
ctx.metrics.invalid_built_blocks_count.increment(1);
946+
Err(err).wrap_err("failed to build payload")
947+
}
948+
Ok((payload, mut fb)) => {
949+
fb.index = ctx.flashblock_index();
950+
fb.base = None;
951+
952+
Ok(Some((
953+
payload,
954+
fb,
955+
simulation_info,
956+
simulation_state.cache,
957+
simulation_state.transition_state,
958+
)))
959+
}
960+
}
930961
}
931962

932963
#[expect(clippy::too_many_arguments)]
@@ -942,13 +973,27 @@ where
942973
best_txs: &mut NextBestFlashblocksTxs<Pool>,
943974
block_cancel: &CancellationToken,
944975
best_payload: &BlockCell<OpBuiltPayload>,
945-
_span: &tracing::Span,
976+
span: &tracing::Span,
946977
) -> eyre::Result<Option<FlashblocksExtraCtx>> {
947978
// 1. --- Prepare shared context ---
948979

949-
// Add top of block builder txns
980+
let flashblock_index = ctx.flashblock_index();
950981
let mut target_gas_for_batch = ctx.extra_ctx.target_gas_for_batch;
951982
let mut target_da_for_batch = ctx.extra_ctx.target_da_for_batch;
983+
info!(
984+
target: "payload_builder",
985+
block_number = ctx.block_number(),
986+
flashblock_index,
987+
target_gas = target_gas_for_batch,
988+
gas_used = info.cumulative_gas_used,
989+
target_da = target_da_for_batch,
990+
da_used = info.cumulative_da_bytes_used,
991+
block_gas_used = ctx.block_gas_limit(),
992+
"Building flashblock",
993+
);
994+
let flashblock_build_start_time = Instant::now();
995+
996+
// Add top of block builder txns
952997
let builder_txs = self
953998
.builder_tx
954999
.add_builder_txs(&state_provider, info, ctx, state, true)
@@ -972,6 +1017,13 @@ where
9721017
// If main token got canceled in here that means we received get_payload, and we should drop everything and not update best_payload
9731018
// To ensure that we will return same blocks as rollup-boost (to leverage caches)
9741019
if block_cancel.is_cancelled() {
1020+
self.record_flashblocks_metrics(
1021+
ctx,
1022+
info,
1023+
ctx.target_flashblock_count(),
1024+
span,
1025+
"Payload building complete, channel closed or job cancelled",
1026+
);
9751027
return Ok(None);
9761028
}
9771029
// interval end: abort worker and publish current best immediately (below)
@@ -1009,7 +1061,7 @@ where
10091061
state.transition_state = transition_state;
10101062

10111063
// Send payloads
1012-
let _flashblock_byte_size = self
1064+
let flashblock_byte_size = self
10131065
.ws_pub
10141066
.publish(&fb_payload)
10151067
.wrap_err("failed to publish flashblock via websocket")?;
@@ -1028,9 +1080,19 @@ where
10281080
.iter()
10291081
.map(|tx| tx.tx_hash())
10301082
.collect::<Vec<_>>();
1031-
// warn: it also marks the top of blocks builder_txs
10321083
best_txs.mark_commited(batch_new_transactions);
10331084

1085+
// Record flashblock build duration
1086+
ctx.metrics
1087+
.flashblock_build_duration
1088+
.record(flashblock_build_start_time.elapsed());
1089+
ctx.metrics
1090+
.flashblock_byte_size_histogram
1091+
.record(flashblock_byte_size as f64);
1092+
ctx.metrics
1093+
.flashblock_num_tx_histogram
1094+
.record(info.executed_transactions.len() as f64);
1095+
10341096
// Update context for next iteration
10351097
let target_gas_for_batch = ctx.extra_ctx.target_gas_for_batch + ctx.extra_ctx.gas_per_batch;
10361098
let target_da_for_batch = ctx
@@ -1043,6 +1105,16 @@ where
10431105
.extra_ctx
10441106
.clone()
10451107
.next(target_gas_for_batch, target_da_for_batch);
1108+
1109+
info!(
1110+
target: "payload_builder",
1111+
message = "Flashblock built",
1112+
flashblock_index,
1113+
current_gas = info.cumulative_gas_used,
1114+
current_da = info.cumulative_da_bytes_used,
1115+
target_flashblocks = ctx.target_flashblock_count(),
1116+
);
1117+
10461118
Ok(Some(next_extra_ctx))
10471119
}
10481120

0 commit comments

Comments
 (0)