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