Skip to content

Commit 849ba74

Browse files
committed
remove SealedCtx in favor of SlotMeta+Payload per hook derivation
1 parent 1cb4ae0 commit 849ba74

6 files changed

Lines changed: 53 additions & 65 deletions

File tree

crates/op-rbuilder/src/builder/hooks/channel.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
1-
use crate::builder::hooks::post_seal::{PostSealHook, SealedCandidate, SealedCtx};
1+
use crate::builder::hooks::post_seal::{PostSealHook, SealedCandidate, SlotMeta};
22
use reth_optimism_node::OpBuiltPayload;
33
use tokio::sync::mpsc;
44
use tracing::warn;
55

66
/// Forwards each sealed candidate over a named mpsc channel.
77
#[derive(Debug)]
8-
pub(in crate::builder) struct ChannelHook {
8+
pub(crate) struct ChannelHook {
99
name: &'static str,
1010
sender: mpsc::Sender<OpBuiltPayload>,
1111
}
1212

1313
impl ChannelHook {
14-
pub(in crate::builder) fn new(
15-
name: &'static str,
16-
sender: mpsc::Sender<OpBuiltPayload>,
17-
) -> Self {
14+
pub(crate) fn new(name: &'static str, sender: mpsc::Sender<OpBuiltPayload>) -> Self {
1815
Self { name, sender }
1916
}
2017
}
2118

2219
impl PostSealHook for ChannelHook {
23-
fn on_sealed(&self, candidate: &SealedCandidate, ctx: &SealedCtx) {
20+
fn on_sealed(&self, candidate: &SealedCandidate, _slot: &SlotMeta) {
2421
if let Err(e) = self.sender.try_send(candidate.payload.clone()) {
2522
warn!(
2623
target: "payload_builder",
2724
channel = self.name,
2825
error = %e,
29-
flashblock_index = ctx.flashblock_index,
26+
flashblock_index = candidate.fb_payload.index,
3027
"Failed to forward sealed payload over channel"
3128
);
3229
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
use crate::{
2-
builder::hooks::post_seal::{PostSealHook, SealedCandidate, SealedCtx},
2+
builder::hooks::post_seal::{PostSealHook, SealedCandidate, SlotMeta},
33
metrics::OpRBuilderMetrics,
44
};
55
use std::sync::Arc;
66

77
/// Records per-flashblock metrics that aren't tied to publication:
88
/// build duration and transaction-count histogram.
99
#[derive(Debug)]
10-
pub(in crate::builder) struct MetricsHook {
10+
pub(crate) struct MetricsHook {
1111
metrics: Arc<OpRBuilderMetrics>,
1212
}
1313

1414
impl MetricsHook {
15-
pub(in crate::builder) fn new(metrics: Arc<OpRBuilderMetrics>) -> Self {
15+
pub(crate) fn new(metrics: Arc<OpRBuilderMetrics>) -> Self {
1616
Self { metrics }
1717
}
1818
}
1919

2020
impl PostSealHook for MetricsHook {
21-
fn on_sealed(&self, _candidate: &SealedCandidate, ctx: &SealedCtx) {
22-
if let Some(duration) = ctx.flashblock_build_duration {
21+
fn on_sealed(&self, candidate: &SealedCandidate, _slot: &SlotMeta) {
22+
if let Some(duration) = candidate.build_duration {
2323
self.metrics.flashblock_build_duration.record(duration);
2424
}
2525
self.metrics
2626
.flashblock_num_tx_histogram
27-
.record(ctx.executed_tx_count as f64);
27+
.record(candidate.payload.block().body().transactions.len() as f64);
2828
}
2929
}

crates/op-rbuilder/src/builder/hooks/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod ws;
1212

1313
pub(super) use channel::ChannelHook;
1414
pub(super) use metrics::MetricsHook;
15-
pub(super) use post_seal::{PostSealHook, SealedCandidate, SealedCtx};
15+
pub(super) use post_seal::{PostSealHook, SealedCandidate, SlotMeta};
1616
pub(super) use ws::WsHook;
1717

1818
/// Dispatch a sealed candidate to every hook in `hooks`.
@@ -22,9 +22,9 @@ pub(super) use ws::WsHook;
2222
pub(super) fn dispatch_post_seal(
2323
hooks: &[Box<dyn PostSealHook>],
2424
candidate: &SealedCandidate,
25-
ctx: &SealedCtx,
25+
slot: &SlotMeta,
2626
) {
2727
for h in hooks {
28-
h.on_sealed(candidate, ctx);
28+
h.on_sealed(candidate, slot);
2929
}
3030
}

crates/op-rbuilder/src/builder/hooks/post_seal.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,31 @@ use reth_payload_builder::PayloadId;
88
///
99
/// `payload` is the full built payload for engine/p2p delivery; `fb_payload`
1010
/// is the slim, serialisable view streamed to flashblocks subscribers.
11+
/// `build_duration` is the wall-clock time spent building this flashblock,
12+
/// or `None` for the fallback candidate (no incremental build step).
1113
#[derive(Debug, Clone)]
12-
pub(in crate::builder) struct SealedCandidate {
14+
pub(crate) struct SealedCandidate {
1315
pub payload: OpBuiltPayload,
1416
pub fb_payload: OpFlashblockPayload,
17+
pub build_duration: Option<Duration>,
1518
}
1619

17-
/// Context describing the slot a sealed candidate belongs to.
18-
///
19-
/// The fields are intentionally limited to data downstream hooks need.
20+
/// Slot-level metadata for a given building slot.
2021
#[derive(Debug, Clone)]
21-
pub(in crate::builder) struct SealedCtx {
22+
pub(crate) struct SlotMeta {
2223
pub payload_id: PayloadId,
23-
pub block_number: u64,
24-
pub flashblock_index: u64,
2524
/// True when the FCU specified `no_tx_pool`.
2625
pub no_tx_pool: bool,
27-
pub executed_tx_count: usize,
2826
/// Slot start timestamp from the payload attributes.
2927
pub slot_timestamp_secs: u64,
3028
pub block_time: Duration,
31-
/// Wall-clock time spent building this flashblock.
32-
/// `None` for the fallback candidate.
33-
pub flashblock_build_duration: Option<Duration>,
34-
pub enable_tx_tracking_debug_logs: bool,
3529
}
3630

3731
/// Hook invoked after a flashblock or fallback candidate has been sealed.
3832
///
3933
/// Implementations should be cheap and non-blocking: dispatch happens on the
4034
/// builder's hot path. Errors are intentionally swallowed at the dispatch site;
4135
/// hooks that want to surface failures should do so via metrics or logs.
42-
pub(in crate::builder) trait PostSealHook:
43-
Send + Sync + std::fmt::Debug
44-
{
45-
fn on_sealed(&self, candidate: &SealedCandidate, ctx: &SealedCtx);
36+
pub(crate) trait PostSealHook: Send + Sync + std::fmt::Debug {
37+
fn on_sealed(&self, candidate: &SealedCandidate, slot: &SlotMeta);
4638
}

crates/op-rbuilder/src/builder/hooks/ws.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
builder::{
3-
hooks::post_seal::{PostSealHook, SealedCandidate, SealedCtx},
3+
hooks::post_seal::{PostSealHook, SealedCandidate, SlotMeta},
44
timing::compute_slot_offset_ms,
55
wspub::WebSocketPublisher,
66
},
@@ -11,10 +11,11 @@ use tracing::{debug, warn};
1111

1212
/// Publishes the flashblock payload to WebSocket subscribers, record metrics.
1313
///
14-
/// Suppressed when `SealedCtx::no_tx_pool` is true
15-
pub(in crate::builder) struct WsHook {
14+
/// Suppressed when `SlotMeta::no_tx_pool` is true.
15+
pub(crate) struct WsHook {
1616
ws_pub: Arc<WebSocketPublisher>,
1717
metrics: Arc<OpRBuilderMetrics>,
18+
enable_tx_tracking_debug_logs: bool,
1819
}
1920

2021
impl std::fmt::Debug for WsHook {
@@ -24,17 +25,22 @@ impl std::fmt::Debug for WsHook {
2425
}
2526

2627
impl WsHook {
27-
pub(in crate::builder) fn new(
28+
pub(crate) fn new(
2829
ws_pub: Arc<WebSocketPublisher>,
2930
metrics: Arc<OpRBuilderMetrics>,
31+
enable_tx_tracking_debug_logs: bool,
3032
) -> Self {
31-
Self { ws_pub, metrics }
33+
Self {
34+
ws_pub,
35+
metrics,
36+
enable_tx_tracking_debug_logs,
37+
}
3238
}
3339
}
3440

3541
impl PostSealHook for WsHook {
36-
fn on_sealed(&self, candidate: &SealedCandidate, ctx: &SealedCtx) {
37-
if ctx.no_tx_pool {
42+
fn on_sealed(&self, candidate: &SealedCandidate, slot: &SlotMeta) {
43+
if slot.no_tx_pool {
3844
return;
3945
}
4046

@@ -44,27 +50,27 @@ impl PostSealHook for WsHook {
4450
warn!(
4551
target: "payload_builder",
4652
error = %e,
47-
flashblock_index = ctx.flashblock_index,
53+
flashblock_index = candidate.fb_payload.index,
4854
"Failed to publish flashblock via websocket"
4955
);
5056
return;
5157
}
5258
};
5359

54-
let slot_offset_ms = compute_slot_offset_ms(ctx.slot_timestamp_secs, ctx.block_time);
60+
let slot_offset_ms = compute_slot_offset_ms(slot.slot_timestamp_secs, slot.block_time);
5561
record_flashblock_publish_timing(candidate.fb_payload.index, slot_offset_ms);
5662
self.metrics
5763
.flashblock_byte_size_histogram
5864
.record(byte_size as f64);
5965

60-
if ctx.enable_tx_tracking_debug_logs {
66+
if self.enable_tx_tracking_debug_logs {
6167
debug!(
6268
target: "tx_trace",
63-
payload_id = %ctx.payload_id,
64-
block_number = ctx.block_number,
69+
payload_id = %slot.payload_id,
70+
block_number = candidate.payload.block().header().number,
6571
flashblock_index = candidate.fb_payload.index,
6672
byte_size,
67-
total_txs = ctx.executed_tx_count,
73+
total_txs = candidate.payload.block().body().transactions.len(),
6874
slot_offset_ms,
6975
stage = "fb_published"
7076
);

crates/op-rbuilder/src/builder/payload.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
context::{OpPayloadBuilderCtx, OpPayloadJobCtx},
99
generator::{BuildArguments, PayloadBuilder},
1010
hooks::{
11-
ChannelHook, MetricsHook, PostSealHook, SealedCandidate, SealedCtx, WsHook,
11+
ChannelHook, MetricsHook, PostSealHook, SealedCandidate, SlotMeta, WsHook,
1212
dispatch_post_seal,
1313
},
1414
timing::FlashblockScheduler,
@@ -113,7 +113,6 @@ struct FlashblockBuildResult {
113113
new_payload: OpBuiltPayload,
114114
fb_payload: OpFlashblockPayload,
115115
build_duration: core::time::Duration,
116-
executed_tx_count: usize,
117116
}
118117

119118
impl FlashblocksState {
@@ -329,7 +328,11 @@ where
329328

330329
let ws_pub = Arc::new(ws_pub);
331330
let post_seal_hooks: Vec<Box<dyn PostSealHook>> = vec![
332-
Box::new(WsHook::new(Arc::clone(&ws_pub), Arc::clone(&metrics))),
331+
Box::new(WsHook::new(
332+
Arc::clone(&ws_pub),
333+
Arc::clone(&metrics),
334+
config.enable_tx_tracking_debug_logs,
335+
)),
333336
Box::new(ChannelHook::new("p2p", built_fb_payload_tx)),
334337
Box::new(ChannelHook::new("engine", built_payload_tx)),
335338
Box::new(MetricsHook::new(Arc::clone(&metrics))),
@@ -510,19 +513,15 @@ where
510513
let candidate = SealedCandidate {
511514
payload: payload.clone(),
512515
fb_payload: fb_payload.clone(),
516+
build_duration: None,
513517
};
514-
let sealed_ctx = SealedCtx {
518+
let slot = SlotMeta {
515519
payload_id: ctx.payload_id(),
516-
block_number: ctx.block_number(),
517-
flashblock_index: fb_payload.index,
518520
no_tx_pool: ctx.attributes().no_tx_pool,
519-
executed_tx_count: info.executed_transactions.len(),
520521
slot_timestamp_secs: config.attributes.timestamp(),
521522
block_time: self.config.block_time,
522-
flashblock_build_duration: None,
523-
enable_tx_tracking_debug_logs: self.config.enable_tx_tracking_debug_logs,
524523
};
525-
dispatch_post_seal(&self.post_seal_hooks, &candidate, &sealed_ctx);
524+
dispatch_post_seal(&self.post_seal_hooks, &candidate, &slot);
526525
best_payload_tx.send_replace(Some(payload));
527526

528527
info!(
@@ -799,25 +798,20 @@ where
799798
new_payload,
800799
fb_payload: built_fb_payload,
801800
build_duration,
802-
executed_tx_count,
803801
} = result;
804802

805803
let candidate = SealedCandidate {
806804
payload: new_payload.clone(),
807805
fb_payload: built_fb_payload,
806+
build_duration: Some(build_duration),
808807
};
809-
let sealed_ctx = SealedCtx {
808+
let slot = SlotMeta {
810809
payload_id: ctx.payload_id(),
811-
block_number: ctx.block_number(),
812-
flashblock_index: candidate.fb_payload.index,
813810
no_tx_pool: ctx.attributes().no_tx_pool,
814-
executed_tx_count,
815811
slot_timestamp_secs: ctx.attributes().timestamp(),
816812
block_time: self.config.block_time,
817-
flashblock_build_duration: Some(build_duration),
818-
enable_tx_tracking_debug_logs: self.config.enable_tx_tracking_debug_logs,
819813
};
820-
dispatch_post_seal(&self.post_seal_hooks, &candidate, &sealed_ctx);
814+
dispatch_post_seal(&self.post_seal_hooks, &candidate, &slot);
821815
best_payload_tx.send_replace(Some(new_payload));
822816
next_fb_state
823817
}
@@ -1091,7 +1085,6 @@ where
10911085
new_payload,
10921086
fb_payload,
10931087
build_duration: flashblock_build_start_time.elapsed(),
1094-
executed_tx_count: info.executed_transactions.len(),
10951088
}))
10961089
}
10971090
}

0 commit comments

Comments
 (0)