Skip to main content

reth_transaction_pool/validate/
mod.rs

1//! Transaction validation abstractions.
2
3use crate::{
4    error::InvalidPoolTransactionError,
5    identifier::{SenderId, TransactionId},
6    traits::{PoolTransaction, TransactionOrigin},
7    PriceBumpConfig,
8};
9use alloy_eips::{eip7594::BlobTransactionSidecarVariant, eip7702::SignedAuthorization};
10use alloy_primitives::{Address, TxHash, B256, U256};
11use futures_util::future::Either;
12use reth_primitives_traits::{Block, Recovered, SealedBlock};
13use std::{fmt, fmt::Debug, future::Future, time::Instant};
14
15mod constants;
16mod eth;
17mod task;
18
19pub use eth::*;
20
21pub use task::{TransactionValidationTaskExecutor, ValidationTask};
22
23/// Validation constants.
24pub use constants::{DEFAULT_MAX_TX_INPUT_BYTES, TX_SLOT_BYTE_SIZE};
25
26/// A Result type returned after checking a transaction's validity.
27#[derive(Debug)]
28pub enum TransactionValidationOutcome<T: PoolTransaction> {
29    /// The transaction is considered _currently_ valid and can be inserted into the pool.
30    Valid {
31        /// Balance of the sender at the current point.
32        balance: U256,
33        /// Current nonce of the sender.
34        state_nonce: u64,
35        /// Code hash of the sender.
36        bytecode_hash: Option<B256>,
37        /// The validated transaction.
38        ///
39        /// See also [`ValidTransaction`].
40        ///
41        /// If this is a _new_ EIP-4844 blob transaction, then this must contain the extracted
42        /// sidecar.
43        transaction: ValidTransaction<T>,
44        /// Whether to propagate the transaction to the network.
45        propagate: bool,
46        /// The authorities of EIP-7702 transaction.
47        authorities: Option<Vec<Address>>,
48    },
49    /// The transaction is considered invalid indefinitely: It violates constraints that prevent
50    /// this transaction from ever becoming valid.
51    Invalid(T, InvalidPoolTransactionError),
52    /// An error occurred while trying to validate the transaction
53    Error(TxHash, Box<dyn core::error::Error + Send + Sync>),
54}
55
56impl<T: PoolTransaction> TransactionValidationOutcome<T> {
57    /// Returns the hash of the transactions
58    pub fn tx_hash(&self) -> TxHash {
59        match self {
60            Self::Valid { transaction, .. } => *transaction.hash(),
61            Self::Invalid(transaction, ..) => *transaction.hash(),
62            Self::Error(hash, ..) => *hash,
63        }
64    }
65
66    /// Returns the [`InvalidPoolTransactionError`] if this is an invalid variant.
67    pub const fn as_invalid(&self) -> Option<&InvalidPoolTransactionError> {
68        match self {
69            Self::Invalid(_, err) => Some(err),
70            _ => None,
71        }
72    }
73
74    /// Returns the [`ValidTransaction`] if this is a [`TransactionValidationOutcome::Valid`].
75    pub const fn as_valid_transaction(&self) -> Option<&ValidTransaction<T>> {
76        match self {
77            Self::Valid { transaction, .. } => Some(transaction),
78            _ => None,
79        }
80    }
81
82    /// Returns true if the transaction is valid.
83    pub const fn is_valid(&self) -> bool {
84        matches!(self, Self::Valid { .. })
85    }
86
87    /// Returns true if the transaction is invalid.
88    pub const fn is_invalid(&self) -> bool {
89        matches!(self, Self::Invalid(_, _))
90    }
91
92    /// Returns true if validation resulted in an error.
93    pub const fn is_error(&self) -> bool {
94        matches!(self, Self::Error(_, _))
95    }
96}
97
98/// A wrapper type for a transaction that is valid and has an optional extracted EIP-4844 blob
99/// transaction sidecar.
100///
101/// If this is provided, then the sidecar will be temporarily stored in the blob store until the
102/// transaction is finalized.
103///
104/// Note: Since blob transactions can be re-injected without their sidecar (after reorg), the
105/// validator can omit the sidecar if it is still in the blob store and return a
106/// [`ValidTransaction::Valid`] instead.
107#[derive(Debug)]
108pub enum ValidTransaction<T> {
109    /// A valid transaction without a sidecar.
110    Valid(T),
111    /// A valid transaction for which a sidecar should be stored.
112    ///
113    /// Caution: The [`TransactionValidator`] must ensure that this is only returned for EIP-4844
114    /// transactions.
115    ValidWithSidecar {
116        /// The valid EIP-4844 transaction.
117        transaction: T,
118        /// The extracted sidecar of that transaction
119        sidecar: BlobTransactionSidecarVariant,
120    },
121}
122
123impl<T> ValidTransaction<T> {
124    /// Creates a new valid transaction with an optional sidecar.
125    pub fn new(transaction: T, sidecar: Option<BlobTransactionSidecarVariant>) -> Self {
126        if let Some(sidecar) = sidecar {
127            Self::ValidWithSidecar { transaction, sidecar }
128        } else {
129            Self::Valid(transaction)
130        }
131    }
132}
133
134impl<T: PoolTransaction> ValidTransaction<T> {
135    /// Returns the transaction.
136    #[inline]
137    pub const fn transaction(&self) -> &T {
138        match self {
139            Self::Valid(transaction) | Self::ValidWithSidecar { transaction, .. } => transaction,
140        }
141    }
142
143    /// Consumes the wrapper and returns the transaction.
144    pub fn into_transaction(self) -> T {
145        match self {
146            Self::Valid(transaction) | Self::ValidWithSidecar { transaction, .. } => transaction,
147        }
148    }
149
150    /// Returns the address of that transaction.
151    #[inline]
152    pub(crate) fn sender(&self) -> Address {
153        self.transaction().sender()
154    }
155
156    /// Returns the hash of the transaction.
157    #[inline]
158    pub fn hash(&self) -> &B256 {
159        self.transaction().hash()
160    }
161
162    /// Returns the nonce of the transaction.
163    #[inline]
164    pub fn nonce(&self) -> u64 {
165        self.transaction().nonce()
166    }
167}
168
169/// Provides support for validating transaction at any given state of the chain
170pub trait TransactionValidator: Debug + Send + Sync {
171    /// The transaction type to validate.
172    type Transaction: PoolTransaction;
173
174    /// The block type used for new head block notifications.
175    type Block: Block;
176
177    /// Validates the transaction and returns a [`TransactionValidationOutcome`] describing the
178    /// validity of the given transaction.
179    ///
180    /// This will be used by the transaction-pool to check whether the transaction should be
181    /// inserted into the pool or discarded right away.
182    ///
183    /// Implementers of this trait must ensure that the transaction is well-formed, i.e. that it
184    /// complies at least all static constraints, which includes checking for:
185    ///
186    ///    * chain id
187    ///    * gas limit
188    ///    * max cost
189    ///    * nonce >= next nonce of the sender
190    ///    * ...
191    ///
192    /// See [`InvalidTransactionError`](reth_primitives_traits::transaction::error::InvalidTransactionError) for common
193    /// errors variants.
194    ///
195    /// The transaction pool makes no additional assumptions about the validity of the transaction
196    /// at the time of this call before it inserts it into the pool. However, the validity of
197    /// this transaction is still subject to future (dynamic) changes enforced by the pool, for
198    /// example nonce or balance changes. Hence, any validation checks must be applied in this
199    /// function.
200    ///
201    /// See [`TransactionValidationTaskExecutor`] for a reference implementation.
202    fn validate_transaction(
203        &self,
204        origin: TransactionOrigin,
205        transaction: Self::Transaction,
206    ) -> impl Future<Output = TransactionValidationOutcome<Self::Transaction>> + Send;
207
208    /// Validates a batch of transactions.
209    ///
210    /// Must return all outcomes for the given transactions in the same order.
211    ///
212    /// See also [`Self::validate_transaction`].
213    fn validate_transactions(
214        &self,
215        transactions: impl IntoIterator<Item = (TransactionOrigin, Self::Transaction), IntoIter: Send>
216            + Send,
217    ) -> impl Future<Output = Vec<TransactionValidationOutcome<Self::Transaction>>> + Send {
218        futures_util::future::join_all(
219            transactions.into_iter().map(|(origin, tx)| self.validate_transaction(origin, tx)),
220        )
221    }
222
223    /// Validates a batch of transactions with that given origin.
224    ///
225    /// Must return all outcomes for the given transactions in the same order.
226    ///
227    /// See also [`Self::validate_transaction`].
228    fn validate_transactions_with_origin(
229        &self,
230        origin: TransactionOrigin,
231        transactions: impl IntoIterator<Item = Self::Transaction, IntoIter: Send> + Send,
232    ) -> impl Future<Output = Vec<TransactionValidationOutcome<Self::Transaction>>> + Send {
233        self.validate_transactions(transactions.into_iter().map(move |tx| (origin, tx)))
234    }
235
236    /// Invoked when the head block changes.
237    ///
238    /// This can be used to update fork specific values (timestamp).
239    fn on_new_head_block(&self, _new_tip_block: &SealedBlock<Self::Block>) {}
240}
241
242impl<A, B> TransactionValidator for Either<A, B>
243where
244    A: TransactionValidator,
245    B: TransactionValidator<Transaction = A::Transaction, Block = A::Block>,
246{
247    type Transaction = A::Transaction;
248    type Block = A::Block;
249
250    async fn validate_transaction(
251        &self,
252        origin: TransactionOrigin,
253        transaction: Self::Transaction,
254    ) -> TransactionValidationOutcome<Self::Transaction> {
255        match self {
256            Self::Left(v) => v.validate_transaction(origin, transaction).await,
257            Self::Right(v) => v.validate_transaction(origin, transaction).await,
258        }
259    }
260
261    async fn validate_transactions(
262        &self,
263        transactions: impl IntoIterator<Item = (TransactionOrigin, Self::Transaction), IntoIter: Send>
264            + Send,
265    ) -> Vec<TransactionValidationOutcome<Self::Transaction>> {
266        match self {
267            Self::Left(v) => v.validate_transactions(transactions).await,
268            Self::Right(v) => v.validate_transactions(transactions).await,
269        }
270    }
271
272    fn on_new_head_block(&self, new_tip_block: &SealedBlock<Self::Block>) {
273        match self {
274            Self::Left(v) => v.on_new_head_block(new_tip_block),
275            Self::Right(v) => v.on_new_head_block(new_tip_block),
276        }
277    }
278}
279
280/// A valid transaction in the pool.
281///
282/// This is used as the internal representation of a transaction inside the pool.
283///
284/// For EIP-4844 blob transactions this will _not_ contain the blob sidecar which is stored
285/// separately in the [`BlobStore`](crate::blobstore::BlobStore).
286pub struct ValidPoolTransaction<T: PoolTransaction> {
287    /// The transaction
288    pub transaction: T,
289    /// The identifier for this transaction.
290    pub transaction_id: TransactionId,
291    /// Whether it is allowed to propagate the transaction.
292    pub propagate: bool,
293    /// Timestamp when this was added to the pool.
294    pub timestamp: Instant,
295    /// Where this transaction originated from.
296    pub origin: TransactionOrigin,
297    /// The sender ids of the 7702 transaction authorities.
298    pub authority_ids: Option<Vec<SenderId>>,
299}
300
301// === impl ValidPoolTransaction ===
302
303impl<T: PoolTransaction> ValidPoolTransaction<T> {
304    /// Returns the hash of the transaction.
305    pub fn hash(&self) -> &TxHash {
306        self.transaction.hash()
307    }
308
309    /// Returns the type identifier of the transaction
310    pub fn tx_type(&self) -> u8 {
311        self.transaction.ty()
312    }
313
314    /// Returns the address of the sender
315    pub fn sender(&self) -> Address {
316        self.transaction.sender()
317    }
318
319    /// Returns a reference to the address of the sender
320    pub fn sender_ref(&self) -> &Address {
321        self.transaction.sender_ref()
322    }
323
324    /// Returns the recipient of the transaction if it is not a CREATE transaction.
325    pub fn to(&self) -> Option<Address> {
326        self.transaction.to()
327    }
328
329    /// Returns the internal identifier for the sender of this transaction
330    pub const fn sender_id(&self) -> SenderId {
331        self.transaction_id.sender
332    }
333
334    /// Returns the internal identifier for this transaction.
335    pub const fn id(&self) -> &TransactionId {
336        &self.transaction_id
337    }
338
339    /// Returns the length of the rlp encoded transaction
340    #[inline]
341    pub fn encoded_length(&self) -> usize {
342        self.transaction.encoded_length()
343    }
344
345    /// Returns the nonce set for this transaction.
346    pub fn nonce(&self) -> u64 {
347        self.transaction.nonce()
348    }
349
350    /// Returns the cost that this transaction is allowed to consume:
351    ///
352    /// For EIP-1559 transactions: `max_fee_per_gas * gas_limit + tx_value`.
353    /// For legacy transactions: `gas_price * gas_limit + tx_value`.
354    pub fn cost(&self) -> &U256 {
355        self.transaction.cost()
356    }
357
358    /// Returns the EIP-4844 max blob fee the caller is willing to pay.
359    ///
360    /// For non-EIP-4844 transactions, this returns [None].
361    pub fn max_fee_per_blob_gas(&self) -> Option<u128> {
362        self.transaction.max_fee_per_blob_gas()
363    }
364
365    /// Returns the EIP-1559 Max base fee the caller is willing to pay.
366    ///
367    /// For legacy transactions this is `gas_price`.
368    pub fn max_fee_per_gas(&self) -> u128 {
369        self.transaction.max_fee_per_gas()
370    }
371
372    /// Returns the EIP-1559 Max priority fee the caller is willing to pay, or `None` for
373    /// non-EIP-1559 transactions.
374    pub fn max_priority_fee_per_gas(&self) -> Option<u128> {
375        self.transaction.max_priority_fee_per_gas()
376    }
377
378    /// Returns the effective tip for this transaction.
379    ///
380    /// For EIP-1559 transactions: `min(max_fee_per_gas - base_fee, max_priority_fee_per_gas)`.
381    /// For legacy transactions: `gas_price - base_fee`.
382    pub fn effective_tip_per_gas(&self, base_fee: u64) -> Option<u128> {
383        self.transaction.effective_tip_per_gas(base_fee)
384    }
385
386    /// Returns the max priority fee per gas if the transaction is an EIP-1559 transaction, and
387    /// otherwise returns the gas price.
388    pub fn priority_fee_or_price(&self) -> u128 {
389        self.transaction.priority_fee_or_price()
390    }
391
392    /// Maximum amount of gas that the transaction is allowed to consume.
393    pub fn gas_limit(&self) -> u64 {
394        self.transaction.gas_limit()
395    }
396
397    /// Whether the transaction originated locally.
398    pub const fn is_local(&self) -> bool {
399        self.origin.is_local()
400    }
401
402    /// Whether the transaction is an EIP-4844 blob transaction.
403    #[inline]
404    pub fn is_eip4844(&self) -> bool {
405        self.transaction.is_eip4844()
406    }
407
408    /// The heap allocated size of this transaction.
409    pub(crate) fn size(&self) -> usize {
410        self.transaction.size()
411    }
412
413    /// Returns the [`SignedAuthorization`] list of the transaction.
414    ///
415    /// Returns `None` if this transaction is not EIP-7702.
416    pub fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
417        self.transaction.authorization_list()
418    }
419
420    /// Returns the number of blobs of [`SignedAuthorization`] in this transactions
421    ///
422    /// This is convenience function for `len(authorization_list)`.
423    ///
424    /// Returns `None` for non-eip7702 transactions.
425    pub fn authorization_count(&self) -> Option<u64> {
426        self.transaction.authorization_count()
427    }
428
429    /// EIP-4844 blob transactions and normal transactions are treated as mutually exclusive per
430    /// account.
431    ///
432    /// Returns true if the transaction is an EIP-4844 blob transaction and the other is not, or
433    /// vice versa.
434    #[inline]
435    pub(crate) fn tx_type_conflicts_with(&self, other: &Self) -> bool {
436        self.is_eip4844() != other.is_eip4844()
437    }
438
439    /// Converts to this type into the consensus transaction of the pooled transaction.
440    ///
441    /// Note: this takes `&self` since indented usage is via `Arc<Self>`.
442    pub fn to_consensus(&self) -> Recovered<T::Consensus> {
443        self.transaction.clone_into_consensus()
444    }
445
446    /// Determines whether a candidate transaction (`maybe_replacement`) is underpriced compared to
447    /// an existing transaction in the pool.
448    ///
449    /// A transaction is considered underpriced if it doesn't meet the required fee bump threshold.
450    /// This applies to both standard gas fees and, for blob-carrying transactions (EIP-4844),
451    /// the blob-specific fees.
452    #[inline]
453    pub fn is_underpriced(&self, maybe_replacement: &Self, price_bumps: &PriceBumpConfig) -> bool {
454        // Retrieve the required price bump percentage for this type of transaction.
455        //
456        // The bump is different for EIP-4844 and other transactions. See `PriceBumpConfig`.
457        let price_bump = price_bumps.price_bump(self.tx_type());
458
459        // Check if the max fee per gas is underpriced.
460        if maybe_replacement.max_fee_per_gas() < self.max_fee_per_gas() * (100 + price_bump) / 100 {
461            return true
462        }
463
464        let existing_max_priority_fee_per_gas =
465            self.transaction.max_priority_fee_per_gas().unwrap_or_default();
466        let replacement_max_priority_fee_per_gas =
467            maybe_replacement.transaction.max_priority_fee_per_gas().unwrap_or_default();
468
469        // Check max priority fee per gas (relevant for EIP-1559 transactions only)
470        if existing_max_priority_fee_per_gas != 0 &&
471            replacement_max_priority_fee_per_gas != 0 &&
472            replacement_max_priority_fee_per_gas <
473                existing_max_priority_fee_per_gas * (100 + price_bump) / 100
474        {
475            return true
476        }
477
478        // Check max blob fee per gas
479        if let Some(existing_max_blob_fee_per_gas) = self.transaction.max_fee_per_blob_gas() {
480            // This enforces that blob txs can only be replaced by blob txs
481            let replacement_max_blob_fee_per_gas =
482                maybe_replacement.transaction.max_fee_per_blob_gas().unwrap_or_default();
483            if replacement_max_blob_fee_per_gas <
484                existing_max_blob_fee_per_gas * (100 + price_bump) / 100
485            {
486                return true
487            }
488        }
489
490        false
491    }
492}
493
494#[cfg(test)]
495impl<T: PoolTransaction> Clone for ValidPoolTransaction<T> {
496    fn clone(&self) -> Self {
497        Self {
498            transaction: self.transaction.clone(),
499            transaction_id: self.transaction_id,
500            propagate: self.propagate,
501            timestamp: self.timestamp,
502            origin: self.origin,
503            authority_ids: self.authority_ids.clone(),
504        }
505    }
506}
507
508impl<T: PoolTransaction> fmt::Debug for ValidPoolTransaction<T> {
509    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
510        f.debug_struct("ValidPoolTransaction")
511            .field("id", &self.transaction_id)
512            .field("propagate", &self.propagate)
513            .field("origin", &self.origin)
514            .field("hash", self.transaction.hash())
515            .field("tx", &self.transaction)
516            .finish()
517    }
518}
519
520/// Validation Errors that can occur during transaction validation.
521#[derive(thiserror::Error, Debug)]
522pub enum TransactionValidatorError {
523    /// Failed to communicate with the validation service.
524    #[error("validation service unreachable")]
525    ValidationServiceUnreachable,
526}