Skip to main content

reth_node_builder/
txpool_prewarm.rs

1//! Transaction-pool backed candidate source for engine cache prewarming.
2
3use alloy_primitives::B256;
4use reth_engine_tree::tree::{
5    TxPoolPrewarmSource as PrewarmSource, TxPoolPrewarmTransaction as Transaction,
6    TxPoolPrewarmTransactions as Transactions,
7};
8use reth_primitives_traits::{NodePrimitives, TxTy};
9use reth_transaction_pool::{
10    BestTransactions, BestTransactionsAttributes, PoolTransaction, TransactionPool,
11};
12use std::fmt::Debug;
13
14/// [`TransactionPool`]-backed [`PrewarmSource`].
15#[derive(Debug)]
16pub(crate) struct Source<P>(P);
17
18impl<P> Source<P> {
19    /// Creates a new txpool prewarm source.
20    pub(crate) const fn new(pool: P) -> Self {
21        Self(pool)
22    }
23}
24
25impl<N, P> PrewarmSource<N> for Source<P>
26where
27    N: NodePrimitives,
28    P: TransactionPool<Transaction: PoolTransaction<Consensus = TxTy<N>>>
29        + Clone
30        + Send
31        + Sync
32        + Debug
33        + 'static,
34{
35    fn best_transactions(&self, parent_hash: B256) -> Option<Transactions<N>> {
36        let block_info = self.0.block_info();
37        if block_info.last_seen_block_hash != parent_hash {
38            return None
39        }
40
41        let mut best = self.0.best_transactions_with_attributes(BestTransactionsAttributes::new(
42            block_info.pending_basefee,
43            block_info.pending_blob_fee.map(|fee| u64::try_from(fee).unwrap_or(u64::MAX)),
44        ));
45        best.allow_updates_out_of_order();
46        best.skip_blobs();
47
48        Some(Box::new(best.map(|transaction| Transaction {
49            hash: *transaction.hash(),
50            sender: transaction.sender(),
51            transaction: transaction.transaction.clone_into_consensus(),
52        })))
53    }
54}