|
| 1 | +use crate::{ |
| 2 | + builder::{ |
| 3 | + hooks::post_seal::{PostSealHook, SealedCandidate, SealedCtx}, |
| 4 | + timing::compute_slot_offset_ms, |
| 5 | + wspub::WebSocketPublisher, |
| 6 | + }, |
| 7 | + metrics::{OpRBuilderMetrics, record_flashblock_publish_timing}, |
| 8 | +}; |
| 9 | +use std::sync::Arc; |
| 10 | +use tracing::{debug, warn}; |
| 11 | + |
| 12 | +/// Publishes the flashblock payload to WebSocket subscribers, record metrics. |
| 13 | +/// |
| 14 | +/// Suppressed when `SealedCtx::no_tx_pool` is true |
| 15 | +pub(in crate::builder) struct WsHook { |
| 16 | + ws_pub: Arc<WebSocketPublisher>, |
| 17 | + metrics: Arc<OpRBuilderMetrics>, |
| 18 | +} |
| 19 | + |
| 20 | +impl std::fmt::Debug for WsHook { |
| 21 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 22 | + f.debug_struct("WsHook").finish_non_exhaustive() |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl WsHook { |
| 27 | + pub(in crate::builder) fn new( |
| 28 | + ws_pub: Arc<WebSocketPublisher>, |
| 29 | + metrics: Arc<OpRBuilderMetrics>, |
| 30 | + ) -> Self { |
| 31 | + Self { ws_pub, metrics } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl PostSealHook for WsHook { |
| 36 | + fn on_sealed(&self, candidate: &SealedCandidate, ctx: &SealedCtx) { |
| 37 | + if ctx.no_tx_pool { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + let byte_size = match self.ws_pub.publish(&candidate.fb_payload) { |
| 42 | + Ok(size) => size, |
| 43 | + Err(e) => { |
| 44 | + warn!( |
| 45 | + target: "payload_builder", |
| 46 | + error = %e, |
| 47 | + flashblock_index = ctx.flashblock_index, |
| 48 | + "Failed to publish flashblock via websocket" |
| 49 | + ); |
| 50 | + return; |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + let slot_offset_ms = compute_slot_offset_ms(ctx.slot_timestamp_secs, ctx.block_time); |
| 55 | + record_flashblock_publish_timing(candidate.fb_payload.index, slot_offset_ms); |
| 56 | + self.metrics |
| 57 | + .flashblock_byte_size_histogram |
| 58 | + .record(byte_size as f64); |
| 59 | + |
| 60 | + if ctx.enable_tx_tracking_debug_logs { |
| 61 | + debug!( |
| 62 | + target: "tx_trace", |
| 63 | + payload_id = %ctx.payload_id, |
| 64 | + block_number = ctx.block_number, |
| 65 | + flashblock_index = candidate.fb_payload.index, |
| 66 | + byte_size, |
| 67 | + total_txs = ctx.executed_tx_count, |
| 68 | + slot_offset_ms, |
| 69 | + stage = "fb_published" |
| 70 | + ); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments