|
| 1 | +//! Tracex execution extension wiring. |
| 2 | +
|
| 3 | +use alloy_primitives::TxHash; |
| 4 | +use eyre::Result; |
| 5 | +use futures::StreamExt; |
| 6 | +use reth::{ |
| 7 | + api::{BlockBody, FullNodeComponents}, |
| 8 | + core::primitives::{AlloyBlockHeader, transaction::TxHashRef}, |
| 9 | + transaction_pool::{FullTransactionEvent, TransactionPool}, |
| 10 | +}; |
| 11 | +use reth_exex::{ExExContext, ExExEvent, ExExNotification}; |
| 12 | +use reth_tracing::tracing::debug; |
| 13 | + |
| 14 | +use crate::{ |
| 15 | + events::{Pool, TxEvent}, |
| 16 | + tracker::Tracker, |
| 17 | +}; |
| 18 | + |
| 19 | +/// Execution extension that tracks transaction timing from mempool to inclusion. |
| 20 | +/// |
| 21 | +/// Monitors transaction lifecycle events and records timing metrics. |
| 22 | +pub async fn tracex_exex<Node: FullNodeComponents>( |
| 23 | + mut ctx: ExExContext<Node>, |
| 24 | + enable_logs: bool, |
| 25 | +) -> Result<()> { |
| 26 | + debug!(target: "tracex", "Starting transaction tracking ExEx"); |
| 27 | + let mut tracker = Tracker::new(enable_logs); |
| 28 | + |
| 29 | + // Subscribe to events from the mempool. |
| 30 | + let pool = ctx.pool().clone(); |
| 31 | + let mut all_events_stream = pool.all_transactions_event_listener(); |
| 32 | + |
| 33 | + loop { |
| 34 | + tokio::select! { |
| 35 | + // Track # of transactions dropped and replaced. |
| 36 | + Some(full_event) = all_events_stream.next() => { |
| 37 | + match full_event { |
| 38 | + FullTransactionEvent::Pending(tx_hash) => { |
| 39 | + tracker.transaction_inserted(tx_hash, TxEvent::Pending); |
| 40 | + tracker.transaction_moved(tx_hash, Pool::Pending); |
| 41 | + } |
| 42 | + FullTransactionEvent::Queued(tx_hash, _) => { |
| 43 | + tracker.transaction_inserted(tx_hash, TxEvent::Queued); |
| 44 | + tracker.transaction_moved(tx_hash, Pool::Queued); |
| 45 | + } |
| 46 | + FullTransactionEvent::Discarded(tx_hash) => { |
| 47 | + tracker.transaction_completed(tx_hash, TxEvent::Dropped); |
| 48 | + } |
| 49 | + FullTransactionEvent::Replaced{ transaction, replaced_by } => { |
| 50 | + let tx_hash = transaction.hash(); |
| 51 | + tracker.transaction_replaced(*tx_hash, TxHash::from(replaced_by)); |
| 52 | + } |
| 53 | + _ => { |
| 54 | + // Other events. |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // Use chain notifications to track time to inclusion. |
| 60 | + Some(notification) = ctx.notifications.next() => { |
| 61 | + match notification { |
| 62 | + Ok(ExExNotification::ChainCommitted { new }) => { |
| 63 | + // Process all transactions in committed chain. |
| 64 | + for block in new.blocks().values() { |
| 65 | + for transaction in block.body().transactions() { |
| 66 | + tracker.transaction_completed(*transaction.tx_hash(), TxEvent::BlockInclusion); |
| 67 | + } |
| 68 | + } |
| 69 | + ctx.events.send(ExExEvent::FinishedHeight(new.tip().num_hash()))?; |
| 70 | + } |
| 71 | + Ok(ExExNotification::ChainReorged { old: _, new }) => { |
| 72 | + debug!(target: "tracex", tip = ?new.tip().number(), "Chain reorg detected"); |
| 73 | + for block in new.blocks().values() { |
| 74 | + for transaction in block.body().transactions() { |
| 75 | + tracker.transaction_completed(*transaction.tx_hash(), TxEvent::BlockInclusion); |
| 76 | + } |
| 77 | + } |
| 78 | + ctx.events.send(ExExEvent::FinishedHeight(new.tip().num_hash()))?; |
| 79 | + } |
| 80 | + Ok(ExExNotification::ChainReverted { old }) => { |
| 81 | + debug!(target: "tracex", old_tip = ?old.tip().number(), "Chain reverted"); |
| 82 | + ctx.events.send(ExExEvent::FinishedHeight(old.tip().num_hash()))?; |
| 83 | + } |
| 84 | + Err(e) => { |
| 85 | + debug!(target: "tracex", error = %e, "Notification error"); |
| 86 | + return Err(e); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments