reth_transaction_pool/pool/
events.rs

1use crate::{traits::PropagateKind, PoolTransaction, SubPool, ValidPoolTransaction};
2use alloy_primitives::{TxHash, B256};
3use std::sync::Arc;
4
5use crate::pool::QueuedReason;
6#[cfg(feature = "serde")]
7use serde::{Deserialize, Serialize};
8
9/// An event that happened to a transaction and contains its full body where possible.
10#[derive(Debug)]
11pub enum FullTransactionEvent<T: PoolTransaction> {
12    /// Transaction has been added to the pending pool.
13    Pending(TxHash),
14    /// Transaction has been added to the queued pool.
15    ///
16    /// If applicable, attached the specific reason why this was queued.
17    Queued(TxHash, Option<QueuedReason>),
18    /// Transaction has been included in the block belonging to this hash.
19    Mined {
20        /// The hash of the mined transaction.
21        tx_hash: TxHash,
22        /// The hash of the mined block that contains the transaction.
23        block_hash: B256,
24    },
25    /// Transaction has been replaced by the transaction belonging to the hash.
26    ///
27    /// E.g. same (sender + nonce) pair
28    Replaced {
29        /// The transaction that was replaced.
30        transaction: Arc<ValidPoolTransaction<T>>,
31        /// The transaction that replaced the event subject.
32        replaced_by: TxHash,
33    },
34    /// Transaction was dropped due to configured limits.
35    Discarded(TxHash),
36    /// Transaction became invalid indefinitely.
37    Invalid(TxHash),
38    /// Transaction was propagated to peers.
39    Propagated(Arc<Vec<PropagateKind>>),
40}
41
42impl<T: PoolTransaction> Clone for FullTransactionEvent<T> {
43    fn clone(&self) -> Self {
44        match self {
45            Self::Pending(hash) => Self::Pending(*hash),
46            Self::Queued(hash, reason) => Self::Queued(*hash, reason.clone()),
47            Self::Mined { tx_hash, block_hash } => {
48                Self::Mined { tx_hash: *tx_hash, block_hash: *block_hash }
49            }
50            Self::Replaced { transaction, replaced_by } => {
51                Self::Replaced { transaction: Arc::clone(transaction), replaced_by: *replaced_by }
52            }
53            Self::Discarded(hash) => Self::Discarded(*hash),
54            Self::Invalid(hash) => Self::Invalid(*hash),
55            Self::Propagated(propagated) => Self::Propagated(Arc::clone(propagated)),
56        }
57    }
58}
59
60/// Various events that describe status changes of a transaction.
61#[derive(Debug, Clone, Eq, PartialEq)]
62#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
63pub enum TransactionEvent {
64    /// Transaction has been added to the pending pool.
65    Pending,
66    /// Transaction has been added to the queued pool.
67    Queued,
68    /// Transaction has been included in the block belonging to this hash.
69    Mined(B256),
70    /// Transaction has been replaced by the transaction belonging to the hash.
71    ///
72    /// E.g. same (sender + nonce) pair
73    Replaced(TxHash),
74    /// Transaction was dropped due to configured limits.
75    Discarded,
76    /// Transaction became invalid indefinitely.
77    Invalid,
78    /// Transaction was propagated to peers.
79    Propagated(Arc<Vec<PropagateKind>>),
80}
81
82impl TransactionEvent {
83    /// Returns `true` if the event is final and no more events are expected for this transaction
84    /// hash.
85    pub const fn is_final(&self) -> bool {
86        matches!(self, Self::Replaced(_) | Self::Mined(_) | Self::Discarded | Self::Invalid)
87    }
88}
89
90/// Represents a new transaction
91#[derive(Debug)]
92pub struct NewTransactionEvent<T: PoolTransaction> {
93    /// The pool which the transaction was moved to.
94    pub subpool: SubPool,
95    /// Actual transaction
96    pub transaction: Arc<ValidPoolTransaction<T>>,
97}
98
99impl<T: PoolTransaction> NewTransactionEvent<T> {
100    /// Creates a new event for a pending transaction.
101    pub const fn pending(transaction: Arc<ValidPoolTransaction<T>>) -> Self {
102        Self { subpool: SubPool::Pending, transaction }
103    }
104}
105
106impl<T: PoolTransaction> Clone for NewTransactionEvent<T> {
107    fn clone(&self) -> Self {
108        Self { subpool: self.subpool, transaction: self.transaction.clone() }
109    }
110}