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>>> + 'static,
29{
30    fn best_transactions(&self, parent_hash: B256) -> Option<Transactions<N>> {
31        let block_info = self.0.block_info();
32        if block_info.last_seen_block_hash != parent_hash {
33            return None
34        }
35
36        let mut best = self.0.best_transactions_with_attributes(BestTransactionsAttributes::new(
37            block_info.pending_basefee,
38            block_info.pending_blob_fee.map(|fee| u64::try_from(fee).unwrap_or(u64::MAX)),
39        ));
40        best.allow_updates_out_of_order();
41        best.skip_blobs();
42
43        Some(Box::new(best.map(|transaction| Transaction {
44            hash: *transaction.hash(),
45            sender: transaction.sender(),
46            transaction: transaction.transaction.clone_into_consensus(),
47        })))
48    }
49}