reth_transaction_pool/
ordering.rs

1use crate::traits::PoolTransaction;
2use alloy_primitives::U256;
3use std::{fmt, marker::PhantomData};
4
5/// Priority of the transaction that can be missing.
6///
7/// Transactions with missing priorities are ranked lower.
8#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug)]
9pub enum Priority<T: Ord + Clone> {
10    /// The value of the priority of the transaction.
11    Value(T),
12    /// Missing priority due to ordering internals.
13    None,
14}
15
16impl<T: Ord + Clone> From<Option<T>> for Priority<T> {
17    fn from(value: Option<T>) -> Self {
18        value.map_or(Self::None, Priority::Value)
19    }
20}
21
22/// Transaction ordering trait to determine the order of transactions.
23///
24/// Decides how transactions should be ordered within the pool, depending on a `Priority` value.
25///
26/// The returned priority must reflect [total order](https://en.wikipedia.org/wiki/Total_order).
27pub trait TransactionOrdering: Send + Sync + 'static {
28    /// Priority of a transaction.
29    ///
30    /// Higher is better.
31    type PriorityValue: Ord + Clone + Default + fmt::Debug + Send + Sync;
32
33    /// The transaction type to determine the priority of.
34    type Transaction: PoolTransaction;
35
36    /// Returns the priority score for the given transaction.
37    fn priority(
38        &self,
39        transaction: &Self::Transaction,
40        base_fee: u64,
41    ) -> Priority<Self::PriorityValue>;
42}
43
44/// Default ordering for the pool.
45///
46/// The transactions are ordered by their coinbase tip.
47/// The higher the coinbase tip is, the higher the priority of the transaction.
48#[derive(Debug)]
49#[non_exhaustive]
50pub struct CoinbaseTipOrdering<T>(PhantomData<T>);
51
52impl<T> TransactionOrdering for CoinbaseTipOrdering<T>
53where
54    T: PoolTransaction + 'static,
55{
56    type PriorityValue = U256;
57    type Transaction = T;
58
59    /// Source: <https://github.com/ethereum/go-ethereum/blob/7f756dc1185d7f1eeeacb1d12341606b7135f9ea/core/txpool/legacypool/list.go#L469-L482>.
60    ///
61    /// NOTE: The implementation is incomplete for missing base fee.
62    fn priority(
63        &self,
64        transaction: &Self::Transaction,
65        base_fee: u64,
66    ) -> Priority<Self::PriorityValue> {
67        transaction.effective_tip_per_gas(base_fee).map(U256::from).into()
68    }
69}
70
71impl<T> Default for CoinbaseTipOrdering<T> {
72    fn default() -> Self {
73        Self(Default::default())
74    }
75}
76
77impl<T> Clone for CoinbaseTipOrdering<T> {
78    fn clone(&self) -> Self {
79        Self::default()
80    }
81}