1use crate::traits::PoolTransaction;
2use alloy_primitives::U256;
3use std::{fmt, marker::PhantomData};
45/// 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.
11Value(T),
12/// Missing priority due to ordering internals.
13None,
14}
1516impl<T: Ord + Clone> From<Option<T>> for Priority<T> {
17fn from(value: Option<T>) -> Self {
18value.map_or(Self::None, Priority::Value)
19 }
20}
2122/// 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.
31type PriorityValue: Ord + Clone + Default + fmt::Debug + Send + Sync;
3233/// The transaction type to determine the priority of.
34type Transaction: PoolTransaction;
3536/// Returns the priority score for the given transaction.
37fn priority(
38&self,
39 transaction: &Self::Transaction,
40 base_fee: u64,
41 ) -> Priority<Self::PriorityValue>;
42}
4344/// 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>);
5152impl<T> TransactionOrderingfor CoinbaseTipOrdering<T>
53where
54T: PoolTransaction + 'static,
55{
56type PriorityValue = U256;
57type Transaction = T;
5859/// 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.
62fn priority(
63&self,
64 transaction: &Self::Transaction,
65 base_fee: u64,
66 ) -> Priority<Self::PriorityValue> {
67transaction.effective_tip_per_gas(base_fee).map(U256::from).into()
68 }
69}
7071impl<T> Defaultfor CoinbaseTipOrdering<T> {
72fn default() -> Self {
73Self(Default::default())
74 }
75}
7677impl<T> Clonefor CoinbaseTipOrdering<T> {
78fn clone(&self) -> Self {
79Self::default()
80 }
81}