reth_transaction_pool/traits.rs
1//! Transaction Pool Traits and Types
2//!
3//! This module defines the core abstractions for transaction pool implementations,
4//! handling the complexity of different transaction representations across the
5//! network, mempool, and the chain itself.
6//!
7//! ## Key Concepts
8//!
9//! ### Transaction Representations
10//!
11//! Transactions exist in different formats throughout their lifecycle:
12//!
13//! 1. **Consensus Format** ([`PoolTransaction::Consensus`])
14//! - The canonical format stored in blocks
15//! - Minimal size for efficient storage
16//! - Example: EIP-4844 transactions store only blob hashes: ([`TransactionSigned::Eip4844`])
17//!
18//! 2. **Pooled Format** ([`PoolTransaction::Pooled`])
19//! - Extended format for network propagation
20//! - Includes additional validation data
21//! - Example: EIP-4844 transactions include full blob sidecars: ([`PooledTransactionVariant`])
22//!
23//! ### Type Relationships
24//!
25//! ```text
26//! NodePrimitives::SignedTx ←── NetworkPrimitives::BroadcastedTransaction
27//! │ │
28//! │ (consensus format) │ (announced to peers)
29//! │ │
30//! └──────────┐ ┌────────────────┘
31//! ▼ ▼
32//! PoolTransaction::Consensus
33//! │ ▲
34//! │ │ from pooled (always succeeds)
35//! │ │
36//! ▼ │ try_from consensus (may fail)
37//! PoolTransaction::Pooled ←──→ NetworkPrimitives::PooledTransaction
38//! (sent on request)
39//! ```
40//!
41//! ### Special Cases
42//!
43//! #### EIP-4844 Blob Transactions
44//! - Consensus format: Only blob hashes (32 bytes each)
45//! - Pooled format: Full blobs + commitments + proofs (large data per blob)
46//! - Network behavior: Not broadcast automatically, only sent on explicit request
47//!
48//! #### Optimism Deposit Transactions
49//! - Only exist in consensus format
50//! - Never enter the mempool (system transactions)
51//! - Conversion from consensus to pooled always fails
52
53use crate::{
54 blobstore::{BlobStore, BlobStoreError},
55 error::{InvalidPoolTransactionError, PoolError, PoolResult, RawPoolTransactionError},
56 pool::{
57 state::SubPool, BestTransactionFilter, NewTransactionEvent, TransactionEvents,
58 TransactionListenerKind,
59 },
60 validate::{TransactionValidationOutcome, TransactionValidator, ValidPoolTransaction},
61 AddedTransactionOutcome, AllTransactionsEvents,
62};
63use alloy_consensus::{error::ValueError, transaction::TxHashRef, BlockHeader, Signed, Typed2718};
64use alloy_eips::{
65 eip2718::{Decodable2718, Encodable2718, WithEncoded},
66 eip2930::AccessList,
67 eip4844::{
68 env_settings::KzgSettings, BlobAndProofV1, BlobAndProofV2, BlobCellsAndProofsV1,
69 BlobTransactionValidationError,
70 },
71 eip7594::BlobTransactionSidecarVariant,
72 eip7702::SignedAuthorization,
73};
74use alloy_primitives::{
75 map::{AddressSet, B256Map},
76 Address, Bytes, TxHash, TxKind, B128, B256, U256,
77};
78use futures_util::{ready, Stream};
79use reth_eth_wire_types::HandleMempoolData;
80use reth_ethereum_primitives::{PooledTransactionVariant, TransactionSigned};
81use reth_execution_types::ChangedAccount;
82use reth_primitives_traits::{Block, InMemorySize, Recovered, SealedBlock, SignedTransaction};
83use serde::{Deserialize, Serialize};
84use std::{
85 fmt,
86 fmt::Debug,
87 future::Future,
88 pin::Pin,
89 sync::Arc,
90 task::{Context, Poll},
91};
92use tokio::sync::mpsc::Receiver;
93
94/// The `PeerId` type.
95pub type PeerId = alloy_primitives::B512;
96
97/// Helper type alias to access [`PoolTransaction`] for a given [`TransactionPool`].
98pub type PoolTx<P> = <P as TransactionPool>::Transaction;
99/// Helper type alias to access [`PoolTransaction::Consensus`] for a given [`TransactionPool`].
100pub type PoolConsensusTx<P> = <<P as TransactionPool>::Transaction as PoolTransaction>::Consensus;
101
102/// Helper type alias to access [`PoolTransaction::Pooled`] for a given [`TransactionPool`].
103pub type PoolPooledTx<P> = <<P as TransactionPool>::Transaction as PoolTransaction>::Pooled;
104
105/// General purpose abstraction of a transaction-pool.
106///
107/// This is intended to be used by API-consumers such as RPC that need inject new incoming,
108/// unverified transactions. And by block production that needs to get transactions to execute in a
109/// new block.
110///
111/// Note: This requires `Clone` for convenience, since it is assumed that this will be implemented
112/// for a wrapped `Arc` type, see also [`Pool`](crate::Pool).
113#[auto_impl::auto_impl(&, Arc)]
114pub trait TransactionPool: Clone + Debug + Send + Sync {
115 /// The transaction type of the pool
116 type Transaction: EthPoolTransaction;
117
118 /// Returns stats about the pool and all sub-pools.
119 fn pool_size(&self) -> PoolSize;
120
121 /// Returns the block the pool is currently tracking.
122 ///
123 /// This tracks the block that the pool has last seen.
124 fn block_info(&self) -> BlockInfo;
125
126 /// Imports an _external_ transaction.
127 ///
128 /// This is intended to be used by the network to insert incoming transactions received over the
129 /// p2p network.
130 ///
131 /// Consumer: P2P
132 fn add_external_transaction(
133 &self,
134 transaction: Self::Transaction,
135 ) -> impl Future<Output = PoolResult<AddedTransactionOutcome>> + Send {
136 self.add_transaction(TransactionOrigin::External, transaction)
137 }
138
139 /// Imports all _external_ transactions
140 ///
141 /// Consumer: Utility
142 fn add_external_transactions(
143 &self,
144 transactions: Vec<Self::Transaction>,
145 ) -> impl Future<Output = Vec<PoolResult<AddedTransactionOutcome>>> + Send {
146 self.add_transactions(TransactionOrigin::External, transactions)
147 }
148
149 /// Adds an _unvalidated_ transaction into the pool and subscribe to state changes.
150 ///
151 /// This is the same as [`TransactionPool::add_transaction`] but returns an event stream for the
152 /// given transaction.
153 ///
154 /// Consumer: Custom
155 fn add_transaction_and_subscribe(
156 &self,
157 origin: TransactionOrigin,
158 transaction: Self::Transaction,
159 ) -> impl Future<Output = PoolResult<TransactionEvents>> + Send;
160
161 /// Adds an _unvalidated_ transaction into the pool.
162 ///
163 /// Consumer: RPC
164 fn add_transaction(
165 &self,
166 origin: TransactionOrigin,
167 transaction: Self::Transaction,
168 ) -> impl Future<Output = PoolResult<AddedTransactionOutcome>> + Send;
169
170 /// Adds the given _unvalidated_ transactions into the pool.
171 ///
172 /// All transactions will use the same `origin`.
173 ///
174 /// Returns a list of results.
175 ///
176 /// Consumer: RPC
177 fn add_transactions(
178 &self,
179 origin: TransactionOrigin,
180 transactions: Vec<Self::Transaction>,
181 ) -> impl Future<Output = Vec<PoolResult<AddedTransactionOutcome>>> + Send;
182
183 /// Adds the given _unvalidated_ transactions into the pool.
184 ///
185 /// Each transaction is paired with its own [`TransactionOrigin`].
186 ///
187 /// Returns a list of results.
188 ///
189 /// Consumer: RPC
190 fn add_transactions_with_origins(
191 &self,
192 transactions: Vec<(TransactionOrigin, Self::Transaction)>,
193 ) -> impl Future<Output = Vec<PoolResult<AddedTransactionOutcome>>> + Send;
194
195 /// Submit a consensus transaction directly to the pool
196 fn add_consensus_transaction(
197 &self,
198 tx: Recovered<<Self::Transaction as PoolTransaction>::Consensus>,
199 origin: TransactionOrigin,
200 ) -> impl Future<Output = PoolResult<AddedTransactionOutcome>> + Send {
201 async move {
202 let tx_hash = *tx.tx_hash();
203
204 let pool_transaction = match Self::Transaction::try_from_consensus(tx) {
205 Ok(tx) => tx,
206 Err(e) => return Err(PoolError::other(tx_hash, e.to_string())),
207 };
208
209 self.add_transaction(origin, pool_transaction).await
210 }
211 }
212
213 /// Submit a consensus transaction and subscribe to event stream
214 fn add_consensus_transaction_and_subscribe(
215 &self,
216 tx: Recovered<<Self::Transaction as PoolTransaction>::Consensus>,
217 origin: TransactionOrigin,
218 ) -> impl Future<Output = PoolResult<TransactionEvents>> + Send {
219 async move {
220 let tx_hash = *tx.tx_hash();
221
222 let pool_transaction = match Self::Transaction::try_from_consensus(tx) {
223 Ok(tx) => tx,
224 Err(e) => return Err(PoolError::other(tx_hash, e.to_string())),
225 };
226
227 self.add_transaction_and_subscribe(origin, pool_transaction).await
228 }
229 }
230
231 /// Returns a new transaction change event stream for the given transaction.
232 ///
233 /// Returns `None` if the transaction is not in the pool.
234 fn transaction_event_listener(&self, tx_hash: TxHash) -> Option<TransactionEvents>;
235
236 /// Returns a new transaction change event stream for _all_ transactions in the pool.
237 fn all_transactions_event_listener(&self) -> AllTransactionsEvents<Self::Transaction>;
238
239 /// Returns a new Stream that yields transactions hashes for new __pending__ transactions
240 /// inserted into the pool that are allowed to be propagated.
241 ///
242 /// Note: This is intended for networking and will __only__ yield transactions that are allowed
243 /// to be propagated over the network, see also [`TransactionListenerKind`].
244 ///
245 /// Consumer: RPC/P2P
246 fn pending_transactions_listener(&self) -> Receiver<TxHash> {
247 self.pending_transactions_listener_for(TransactionListenerKind::PropagateOnly)
248 }
249
250 /// Returns a new [Receiver] that yields transactions hashes for new __pending__ transactions
251 /// inserted into the pending pool depending on the given [`TransactionListenerKind`] argument.
252 fn pending_transactions_listener_for(&self, kind: TransactionListenerKind) -> Receiver<TxHash>;
253
254 /// Returns a new stream that yields new valid transactions added to the pool.
255 fn new_transactions_listener(&self) -> Receiver<NewTransactionEvent<Self::Transaction>> {
256 self.new_transactions_listener_for(TransactionListenerKind::PropagateOnly)
257 }
258
259 /// Returns a new [Receiver] that yields blob "sidecars" (blobs w/ assoc. kzg
260 /// commitments/proofs) for eip-4844 transactions inserted into the pool
261 fn blob_transaction_sidecars_listener(&self) -> Receiver<NewBlobSidecar>;
262
263 /// Returns a new stream that yields new valid transactions added to the pool
264 /// depending on the given [`TransactionListenerKind`] argument.
265 fn new_transactions_listener_for(
266 &self,
267 kind: TransactionListenerKind,
268 ) -> Receiver<NewTransactionEvent<Self::Transaction>>;
269
270 /// Returns a new Stream that yields new transactions added to the pending sub-pool.
271 ///
272 /// This is a convenience wrapper around [`Self::new_transactions_listener`] that filters for
273 /// [`SubPool::Pending`](crate::SubPool).
274 fn new_pending_pool_transactions_listener(
275 &self,
276 ) -> NewSubpoolTransactionStream<Self::Transaction> {
277 NewSubpoolTransactionStream::new(
278 self.new_transactions_listener_for(TransactionListenerKind::PropagateOnly),
279 SubPool::Pending,
280 )
281 }
282
283 /// Returns a new Stream that yields new transactions added to the basefee sub-pool.
284 ///
285 /// This is a convenience wrapper around [`Self::new_transactions_listener`] that filters for
286 /// [`SubPool::BaseFee`](crate::SubPool).
287 fn new_basefee_pool_transactions_listener(
288 &self,
289 ) -> NewSubpoolTransactionStream<Self::Transaction> {
290 NewSubpoolTransactionStream::new(self.new_transactions_listener(), SubPool::BaseFee)
291 }
292
293 /// Returns a new Stream that yields new transactions added to the queued-pool.
294 ///
295 /// This is a convenience wrapper around [`Self::new_transactions_listener`] that filters for
296 /// [`SubPool::Queued`](crate::SubPool).
297 fn new_queued_transactions_listener(&self) -> NewSubpoolTransactionStream<Self::Transaction> {
298 NewSubpoolTransactionStream::new(self.new_transactions_listener(), SubPool::Queued)
299 }
300
301 /// Returns a new Stream that yields new transactions added to the blob sub-pool.
302 ///
303 /// This is a convenience wrapper around [`Self::new_transactions_listener`] that filters for
304 /// [`SubPool::Blob`](crate::SubPool).
305 fn new_blob_pool_transactions_listener(
306 &self,
307 ) -> NewSubpoolTransactionStream<Self::Transaction> {
308 NewSubpoolTransactionStream::new(self.new_transactions_listener(), SubPool::Blob)
309 }
310
311 /// Returns the _hashes_ of all transactions in the pool that are allowed to be propagated.
312 ///
313 /// This excludes hashes that aren't allowed to be propagated.
314 ///
315 /// Note: This returns a `Vec` but should guarantee that all hashes are unique.
316 ///
317 /// Consumer: P2P
318 fn pooled_transaction_hashes(&self) -> Vec<TxHash>;
319
320 /// Returns only the first `max` hashes of transactions in the pool.
321 ///
322 /// Consumer: P2P
323 fn pooled_transaction_hashes_max(&self, max: usize) -> Vec<TxHash>;
324
325 /// Returns the _full_ transaction objects all transactions in the pool that are allowed to be
326 /// propagated.
327 ///
328 /// This is intended to be used by the network for the initial exchange of pooled transaction
329 /// _hashes_
330 ///
331 /// Note: This returns a `Vec` but should guarantee that all transactions are unique.
332 ///
333 /// Caution: In case of blob transactions, this does not include the sidecar.
334 ///
335 /// Consumer: P2P
336 fn pooled_transactions(&self) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
337
338 /// Returns only the first `max` transactions in the pool.
339 ///
340 /// Consumer: P2P
341 fn pooled_transactions_max(
342 &self,
343 max: usize,
344 ) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
345
346 /// Returns converted [`PooledTransactionVariant`] for the given transaction hashes that are
347 /// allowed to be propagated.
348 ///
349 /// This adheres to the expected behavior of
350 /// [`GetPooledTransactions`](https://github.com/ethereum/devp2p/blob/master/caps/eth.md#getpooledtransactions-0x09):
351 ///
352 /// The transactions must be in same order as in the request, but it is OK to skip transactions
353 /// which are not available.
354 ///
355 /// If the transaction is a blob transaction, the sidecar will be included.
356 ///
357 /// Consumer: P2P
358 fn get_pooled_transaction_elements(
359 &self,
360 tx_hashes: Vec<TxHash>,
361 limit: GetPooledTransactionLimit,
362 ) -> Vec<<Self::Transaction as PoolTransaction>::Pooled>;
363
364 /// Extends the given vector with pooled transactions for the given hashes that are allowed to
365 /// be propagated.
366 ///
367 /// This adheres to the expected behavior of [`Self::get_pooled_transaction_elements`].
368 ///
369 /// Consumer: P2P
370 fn append_pooled_transaction_elements(
371 &self,
372 tx_hashes: &[TxHash],
373 limit: GetPooledTransactionLimit,
374 out: &mut Vec<<Self::Transaction as PoolTransaction>::Pooled>,
375 ) {
376 out.extend(self.get_pooled_transaction_elements(tx_hashes.to_vec(), limit));
377 }
378
379 /// Returns the pooled transaction variant for the given transaction hash.
380 ///
381 /// This adheres to the expected behavior of
382 /// [`GetPooledTransactions`](https://github.com/ethereum/devp2p/blob/master/caps/eth.md#getpooledtransactions-0x09):
383 ///
384 /// If the transaction is a blob transaction, the sidecar will be included.
385 ///
386 /// It is expected that this variant represents the valid p2p format for full transactions.
387 /// E.g. for EIP-4844 transactions this is the consensus transaction format with the blob
388 /// sidecar.
389 ///
390 /// Consumer: P2P
391 fn get_pooled_transaction_element(
392 &self,
393 tx_hash: TxHash,
394 ) -> Option<Recovered<<Self::Transaction as PoolTransaction>::Pooled>>;
395
396 /// Returns an iterator that yields transactions that are ready for block production.
397 ///
398 /// Consumer: Block production
399 fn best_transactions(
400 &self,
401 ) -> Box<dyn BestTransactions<Item = Arc<ValidPoolTransaction<Self::Transaction>>>>;
402
403 /// Returns an iterator that yields transactions that are ready for block production with the
404 /// given base fee and optional blob fee attributes.
405 ///
406 /// Consumer: Block production
407 fn best_transactions_with_attributes(
408 &self,
409 best_transactions_attributes: BestTransactionsAttributes,
410 ) -> Box<dyn BestTransactions<Item = Arc<ValidPoolTransaction<Self::Transaction>>>>;
411
412 /// Returns all transactions that can be included in the next block.
413 ///
414 /// This is primarily used for the `txpool_` RPC namespace:
415 /// <https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-txpool> which distinguishes
416 /// between `pending` and `queued` transactions, where `pending` are transactions ready for
417 /// inclusion in the next block and `queued` are transactions that are ready for inclusion in
418 /// future blocks.
419 ///
420 /// Consumer: RPC
421 fn pending_transactions(&self) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
422
423 /// Returns a pending transaction if it exists and is ready for immediate execution
424 /// (i.e., has the lowest nonce among the sender's pending transactions).
425 fn get_pending_transaction_by_sender_and_nonce(
426 &self,
427 sender: Address,
428 nonce: u64,
429 ) -> Option<Arc<ValidPoolTransaction<Self::Transaction>>> {
430 self.best_transactions().find(|tx| tx.sender() == sender && tx.nonce() == nonce)
431 }
432
433 /// Returns first `max` transactions that can be included in the next block.
434 /// See <https://github.com/paradigmxyz/reth/issues/12767#issuecomment-2493223579>
435 ///
436 /// Consumer: Block production
437 fn pending_transactions_max(
438 &self,
439 max: usize,
440 ) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
441
442 /// Returns all transactions that can be included in _future_ blocks.
443 ///
444 /// This and [`Self::pending_transactions`] are mutually exclusive.
445 ///
446 /// Consumer: RPC
447 fn queued_transactions(&self) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
448
449 /// Returns the number of transactions that are ready for inclusion in the next block and the
450 /// number of transactions that are ready for inclusion in future blocks: `(pending, queued)`.
451 fn pending_and_queued_txn_count(&self) -> (usize, usize);
452
453 /// Returns all transactions that are currently in the pool grouped by whether they are ready
454 /// for inclusion in the next block or not.
455 ///
456 /// This is primarily used for the `txpool_` namespace: <https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-txpool>
457 ///
458 /// Consumer: RPC
459 fn all_transactions(&self) -> AllPoolTransactions<Self::Transaction>;
460
461 /// Returns the _hashes_ of all transactions regardless of whether they can be propagated or
462 /// not.
463 ///
464 /// Unlike [`Self::pooled_transaction_hashes`] this doesn't consider whether the transaction can
465 /// be propagated or not.
466 ///
467 /// Note: This returns a `Vec` but should guarantee that all hashes are unique.
468 ///
469 /// Consumer: Utility
470 fn all_transaction_hashes(&self) -> Vec<TxHash>;
471
472 /// Removes a single transaction corresponding to the given hash.
473 ///
474 /// Note: This removes the transaction as if it got discarded (_not_ mined).
475 ///
476 /// Returns the removed transaction if it was found in the pool.
477 ///
478 /// Consumer: Utility
479 fn remove_transaction(
480 &self,
481 hash: TxHash,
482 ) -> Option<Arc<ValidPoolTransaction<Self::Transaction>>> {
483 self.remove_transactions(vec![hash]).pop()
484 }
485
486 /// Removes all transactions corresponding to the given hashes.
487 ///
488 /// Note: This removes the transactions as if they got discarded (_not_ mined).
489 ///
490 /// Consumer: Utility
491 fn remove_transactions(
492 &self,
493 hashes: Vec<TxHash>,
494 ) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
495
496 /// Removes all transactions corresponding to the given hashes.
497 ///
498 /// Also removes all _dependent_ transactions.
499 ///
500 /// Consumer: Utility
501 fn remove_transactions_and_descendants(
502 &self,
503 hashes: Vec<TxHash>,
504 ) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
505
506 /// Removes all transactions from the given sender
507 ///
508 /// Consumer: Utility
509 fn remove_transactions_by_sender(
510 &self,
511 sender: Address,
512 ) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
513
514 /// Prunes a single transaction from the pool.
515 ///
516 /// This is similar to [`Self::remove_transaction`] but treats the transaction as _mined_
517 /// rather than discarded. The key difference is that pruning does **not** park descendant
518 /// transactions: their nonce requirements are considered satisfied, so they remain in whatever
519 /// sub-pool they currently occupy and can be included in the next block.
520 ///
521 /// In contrast, [`Self::remove_transaction`] treats the removal as a discard, which
522 /// introduces a nonce gap and moves all descendant transactions to the queued (parked)
523 /// sub-pool.
524 ///
525 /// Returns the pruned transaction if it existed in the pool.
526 ///
527 /// Consumer: Utility
528 fn prune_transaction(
529 &self,
530 hash: TxHash,
531 ) -> Option<Arc<ValidPoolTransaction<Self::Transaction>>> {
532 self.prune_transactions(vec![hash]).pop()
533 }
534
535 /// Prunes all transactions corresponding to the given hashes from the pool.
536 ///
537 /// This behaves like [`Self::prune_transaction`] but for multiple transactions at once.
538 /// Each transaction is removed as if it was mined: descendant transactions are **not** parked
539 /// and their nonce requirements are considered satisfied.
540 ///
541 /// This is useful for scenarios like Flashblocks where transactions are committed across
542 /// multiple partial blocks without a canonical state update: previously committed transactions
543 /// can be pruned so that the best-transactions iterator yields their descendants in the
544 /// correct priority order.
545 ///
546 /// Consumer: Utility
547 fn prune_transactions(
548 &self,
549 hashes: Vec<TxHash>,
550 ) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
551
552 /// Retains only those hashes that are unknown to the pool.
553 ///
554 /// In other words, removes all transactions from the given set that are currently present in
555 /// the pool.
556 ///
557 /// Consumer: P2P
558 fn retain_unknown<A>(&self, announcement: &mut A)
559 where
560 A: HandleMempoolData;
561
562 /// Retains only those hashes that are known to the pool.
563 ///
564 /// In other words, removes all transactions from the given set that are not currently present
565 /// in the pool.
566 ///
567 /// Consumer: P2P
568 fn retain_contains<A>(&self, announcement: &mut A)
569 where
570 A: HandleMempoolData;
571
572 /// Returns if the transaction for the given hash is already included in this pool.
573 fn contains(&self, tx_hash: &TxHash) -> bool {
574 self.get(tx_hash).is_some()
575 }
576
577 /// Returns the transaction for the given hash.
578 fn get(&self, tx_hash: &TxHash) -> Option<Arc<ValidPoolTransaction<Self::Transaction>>>;
579
580 /// Returns all transaction objects for the given hashes.
581 ///
582 /// Caution: In case of blob transactions, this does not include the sidecar.
583 fn get_all(&self, txs: Vec<TxHash>) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
584
585 /// Notify the pool about transactions that are propagated to peers.
586 ///
587 /// Consumer: P2P
588 fn on_propagated(&self, txs: PropagatedTransactions);
589
590 /// Returns all transactions sent by a given user
591 fn get_transactions_by_sender(
592 &self,
593 sender: Address,
594 ) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
595
596 /// Returns all pending transactions filtered by predicate
597 fn get_pending_transactions_with_predicate(
598 &self,
599 predicate: impl FnMut(&ValidPoolTransaction<Self::Transaction>) -> bool,
600 ) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
601
602 /// Returns all pending transactions sent by a given user
603 fn get_pending_transactions_by_sender(
604 &self,
605 sender: Address,
606 ) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
607
608 /// Returns all queued transactions sent by a given user
609 fn get_queued_transactions_by_sender(
610 &self,
611 sender: Address,
612 ) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
613
614 /// Returns the highest transaction sent by a given user
615 fn get_highest_transaction_by_sender(
616 &self,
617 sender: Address,
618 ) -> Option<Arc<ValidPoolTransaction<Self::Transaction>>>;
619
620 /// Returns the transaction with the highest nonce that is executable given the on chain nonce.
621 /// In other words the highest non nonce gapped transaction.
622 ///
623 /// Note: The next pending pooled transaction must have the on chain nonce.
624 ///
625 /// For example, for a given on chain nonce of `5`, the next transaction must have that nonce.
626 /// If the pool contains txs `[5,6,7]` this returns tx `7`.
627 /// If the pool contains txs `[6,7]` this returns `None` because the next valid nonce (5) is
628 /// missing, which means txs `[6,7]` are nonce gapped.
629 fn get_highest_consecutive_transaction_by_sender(
630 &self,
631 sender: Address,
632 on_chain_nonce: u64,
633 ) -> Option<Arc<ValidPoolTransaction<Self::Transaction>>>;
634
635 /// Returns a transaction sent by a given user and a nonce
636 fn get_transaction_by_sender_and_nonce(
637 &self,
638 sender: Address,
639 nonce: u64,
640 ) -> Option<Arc<ValidPoolTransaction<Self::Transaction>>>;
641
642 /// Returns all transactions that where submitted with the given [`TransactionOrigin`]
643 fn get_transactions_by_origin(
644 &self,
645 origin: TransactionOrigin,
646 ) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
647
648 /// Returns all pending transactions filtered by [`TransactionOrigin`]
649 fn get_pending_transactions_by_origin(
650 &self,
651 origin: TransactionOrigin,
652 ) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
653
654 /// Returns all transactions that where submitted as [`TransactionOrigin::Local`]
655 fn get_local_transactions(&self) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>> {
656 self.get_transactions_by_origin(TransactionOrigin::Local)
657 }
658
659 /// Returns all transactions that where submitted as [`TransactionOrigin::Private`]
660 fn get_private_transactions(&self) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>> {
661 self.get_transactions_by_origin(TransactionOrigin::Private)
662 }
663
664 /// Returns all transactions that where submitted as [`TransactionOrigin::External`]
665 fn get_external_transactions(&self) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>> {
666 self.get_transactions_by_origin(TransactionOrigin::External)
667 }
668
669 /// Returns all pending transactions that where submitted as [`TransactionOrigin::Local`]
670 fn get_local_pending_transactions(&self) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>> {
671 self.get_pending_transactions_by_origin(TransactionOrigin::Local)
672 }
673
674 /// Returns all pending transactions that where submitted as [`TransactionOrigin::Private`]
675 fn get_private_pending_transactions(
676 &self,
677 ) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>> {
678 self.get_pending_transactions_by_origin(TransactionOrigin::Private)
679 }
680
681 /// Returns all pending transactions that where submitted as [`TransactionOrigin::External`]
682 fn get_external_pending_transactions(
683 &self,
684 ) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>> {
685 self.get_pending_transactions_by_origin(TransactionOrigin::External)
686 }
687
688 /// Returns a set of all senders of transactions in the pool
689 fn unique_senders(&self) -> AddressSet;
690
691 /// Returns the [`BlobTransactionSidecarVariant`] for the given transaction hash if it exists in
692 /// the blob store.
693 fn get_blob(
694 &self,
695 tx_hash: TxHash,
696 ) -> Result<Option<Arc<BlobTransactionSidecarVariant>>, BlobStoreError>;
697
698 /// Returns all [`BlobTransactionSidecarVariant`] for the given transaction hashes if they
699 /// exists in the blob store.
700 ///
701 /// This only returns the blobs that were found in the store.
702 /// If there's no blob it will not be returned.
703 fn get_all_blobs(
704 &self,
705 tx_hashes: Vec<TxHash>,
706 ) -> Result<Vec<(TxHash, Arc<BlobTransactionSidecarVariant>)>, BlobStoreError>;
707
708 /// Returns the exact [`BlobTransactionSidecarVariant`] for the given transaction hashes in the
709 /// order they were requested.
710 ///
711 /// Returns an error if any of the blobs are not found in the blob store.
712 fn get_all_blobs_exact(
713 &self,
714 tx_hashes: Vec<TxHash>,
715 ) -> Result<Vec<Arc<BlobTransactionSidecarVariant>>, BlobStoreError>;
716
717 /// Return the [`BlobAndProofV1`]s for a list of blob versioned hashes.
718 fn get_blobs_for_versioned_hashes_v1(
719 &self,
720 versioned_hashes: &[B256],
721 ) -> Result<Vec<Option<BlobAndProofV1>>, BlobStoreError>;
722
723 /// Return the [`BlobAndProofV2`]s for a list of blob versioned hashes.
724 /// Blobs and proofs are returned only if they are present for _all_ of the requested versioned
725 /// hashes.
726 fn get_blobs_for_versioned_hashes_v2(
727 &self,
728 versioned_hashes: &[B256],
729 ) -> Result<Option<Vec<BlobAndProofV2>>, BlobStoreError>;
730
731 /// Return the [`BlobAndProofV2`]s for a list of blob versioned hashes.
732 ///
733 /// The response is always the same length as the request. Missing or older-version blobs are
734 /// returned as `None` elements.
735 fn get_blobs_for_versioned_hashes_v3(
736 &self,
737 versioned_hashes: &[B256],
738 ) -> Result<Vec<Option<BlobAndProofV2>>, BlobStoreError>;
739
740 /// Return the [`BlobCellsAndProofsV1`]s for a list of blob versioned hashes and requested cell
741 /// indices.
742 ///
743 /// The response is always the same length as the request. Missing or older-version blobs are
744 /// returned as `None` elements.
745 fn get_blobs_for_versioned_hashes_v4(
746 &self,
747 versioned_hashes: &[B256],
748 indices_bitarray: B128,
749 ) -> Result<Vec<Option<BlobCellsAndProofsV1>>, BlobStoreError>;
750
751 /// Returns the blob store used by the pool.
752 fn blob_store(&self) -> Box<dyn BlobStore>;
753}
754
755/// Extension for [`TransactionPool`] trait that allows to set the current block info.
756#[auto_impl::auto_impl(&, Arc)]
757pub trait TransactionPoolExt: TransactionPool {
758 /// The block type used for chain tip updates.
759 type Block: Block;
760
761 /// Sets the current block info for the pool.
762 fn set_block_info(&self, info: BlockInfo);
763
764 /// Event listener for when the pool needs to be updated.
765 ///
766 /// Implementers need to update the pool accordingly:
767 ///
768 /// ## Fee changes
769 ///
770 /// The [`CanonicalStateUpdate`] includes the base and blob fee of the pending block, which
771 /// affects the dynamic fee requirement of pending transactions in the pool.
772 ///
773 /// ## EIP-4844 Blob transactions
774 ///
775 /// Mined blob transactions need to be removed from the pool, but from the pool only. The blob
776 /// sidecar must not be removed from the blob store. Only after a blob transaction is
777 /// finalized, its sidecar is removed from the blob store. This ensures that in case of a reorg,
778 /// the sidecar is still available.
779 fn on_canonical_state_change(&self, update: CanonicalStateUpdate<'_, Self::Block>);
780
781 /// Updates the accounts in the pool
782 fn update_accounts(&self, accounts: Vec<ChangedAccount>);
783
784 /// Deletes the blob sidecar for the given transaction from the blob store
785 fn delete_blob(&self, tx: B256);
786
787 /// Deletes multiple blob sidecars from the blob store
788 fn delete_blobs(&self, txs: Vec<B256>);
789
790 /// Maintenance function to cleanup blobs that are no longer needed.
791 fn cleanup_blobs(&self);
792}
793
794/// Extension for [`TransactionPool`] that exposes the pool's underlying [`TransactionValidator`].
795///
796/// This is implemented by pools that validate transactions through a single validator before
797/// insertion (e.g. [`Pool`](crate::Pool)). It lets consumers and wrapper pools reach the validator
798/// directly, for example to validate a transaction without inserting it into the pool.
799pub trait ValidatingPool: TransactionPool {
800 /// The validator used to validate transactions before they are inserted into the pool.
801 type Validator: TransactionValidator<Transaction = Self::Transaction>;
802
803 /// Returns a reference to the pool's transaction validator.
804 fn validator(&self) -> &Self::Validator;
805
806 /// Validates the given transaction without inserting it into the pool.
807 ///
808 /// This is a convenience wrapper around [`TransactionValidator::validate_transaction`].
809 fn validate(
810 &self,
811 origin: TransactionOrigin,
812 transaction: Self::Transaction,
813 ) -> impl Future<Output = TransactionValidationOutcome<Self::Transaction>> + Send {
814 self.validator().validate_transaction(origin, transaction)
815 }
816}
817
818/// A Helper type that bundles all transactions in the pool.
819#[derive(Debug, Clone)]
820pub struct AllPoolTransactions<T: PoolTransaction> {
821 /// Transactions that are ready for inclusion in the next block.
822 pub pending: Vec<Arc<ValidPoolTransaction<T>>>,
823 /// Transactions that are ready for inclusion in _future_ blocks, but are currently parked,
824 /// because they depend on other transactions that are not yet included in the pool (nonce gap)
825 /// or otherwise blocked.
826 pub queued: Vec<Arc<ValidPoolTransaction<T>>>,
827}
828
829// === impl AllPoolTransactions ===
830
831impl<T: PoolTransaction> AllPoolTransactions<T> {
832 /// Returns the combined number of all transactions.
833 pub const fn count(&self) -> usize {
834 self.pending.len() + self.queued.len()
835 }
836
837 /// Returns an iterator over all pending and queued transactions.
838 pub fn iter(&self) -> impl Iterator<Item = &Arc<ValidPoolTransaction<T>>> + '_ {
839 self.pending.iter().chain(self.queued.iter())
840 }
841
842 /// Returns an iterator over all pending [`Recovered`] transactions.
843 pub fn pending_recovered(&self) -> impl Iterator<Item = Recovered<T::Consensus>> + '_ {
844 self.pending.iter().map(|tx| tx.to_consensus())
845 }
846
847 /// Returns an iterator over all queued [`Recovered`] transactions.
848 pub fn queued_recovered(&self) -> impl Iterator<Item = Recovered<T::Consensus>> + '_ {
849 self.queued.iter().map(|tx| tx.to_consensus())
850 }
851
852 /// Returns an iterator over all transactions, both pending and queued.
853 pub fn all(&self) -> impl Iterator<Item = Recovered<T::Consensus>> + '_ {
854 self.pending.iter().chain(self.queued.iter()).map(|tx| tx.to_consensus())
855 }
856}
857
858impl<T: PoolTransaction> Default for AllPoolTransactions<T> {
859 fn default() -> Self {
860 Self { pending: Default::default(), queued: Default::default() }
861 }
862}
863
864impl<T: PoolTransaction> IntoIterator for AllPoolTransactions<T> {
865 type Item = Arc<ValidPoolTransaction<T>>;
866 type IntoIter = std::iter::Chain<
867 std::vec::IntoIter<Arc<ValidPoolTransaction<T>>>,
868 std::vec::IntoIter<Arc<ValidPoolTransaction<T>>>,
869 >;
870
871 fn into_iter(self) -> Self::IntoIter {
872 self.pending.into_iter().chain(self.queued)
873 }
874}
875
876/// Represents transactions that were propagated over the network.
877#[derive(Debug, Clone, Eq, PartialEq, Default)]
878pub struct PropagatedTransactions(pub B256Map<Vec<PropagateKind>>);
879
880impl PropagatedTransactions {
881 /// Records a propagation of a transaction to a peer.
882 pub fn record(&mut self, hash: TxHash, kind: PropagateKind) {
883 self.0.entry(hash).or_default().push(kind);
884 }
885
886 /// Returns the number of distinct transactions that were propagated.
887 pub fn len(&self) -> usize {
888 self.0.len()
889 }
890
891 /// Returns true if no transactions were propagated.
892 pub fn is_empty(&self) -> bool {
893 self.0.is_empty()
894 }
895
896 /// Returns the propagation info for a specific transaction.
897 pub fn get(&self, hash: &TxHash) -> Option<&[PropagateKind]> {
898 self.0.get(hash).map(Vec::as_slice)
899 }
900}
901
902impl IntoIterator for PropagatedTransactions {
903 type Item = (TxHash, Vec<PropagateKind>);
904 type IntoIter = alloy_primitives::map::hash_map::IntoIter<TxHash, Vec<PropagateKind>>;
905
906 fn into_iter(self) -> Self::IntoIter {
907 self.0.into_iter()
908 }
909}
910
911/// Represents how a transaction was propagated over the network.
912#[derive(Debug, Copy, Clone, Eq, PartialEq)]
913#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
914pub enum PropagateKind {
915 /// The full transaction object was sent to the peer.
916 ///
917 /// This is equivalent to the `Transaction` message
918 Full(PeerId),
919 /// Only the Hash was propagated to the peer.
920 Hash(PeerId),
921}
922
923// === impl PropagateKind ===
924
925impl PropagateKind {
926 /// Returns the peer the transaction was sent to
927 pub const fn peer(&self) -> &PeerId {
928 match self {
929 Self::Full(peer) | Self::Hash(peer) => peer,
930 }
931 }
932
933 /// Returns true if the transaction was sent as a full transaction
934 pub const fn is_full(&self) -> bool {
935 matches!(self, Self::Full(_))
936 }
937
938 /// Returns true if the transaction was sent as a hash
939 pub const fn is_hash(&self) -> bool {
940 matches!(self, Self::Hash(_))
941 }
942}
943
944impl From<PropagateKind> for PeerId {
945 fn from(value: PropagateKind) -> Self {
946 match value {
947 PropagateKind::Full(peer) | PropagateKind::Hash(peer) => peer,
948 }
949 }
950}
951
952/// This type represents a new blob sidecar that has been stored in the transaction pool's
953/// blobstore; it includes the `TransactionHash` of the blob transaction along with the assoc.
954/// sidecar (blobs, commitments, proofs)
955#[derive(Debug, Clone)]
956pub struct NewBlobSidecar {
957 /// hash of the EIP-4844 transaction.
958 pub tx_hash: TxHash,
959 /// the blob transaction sidecar.
960 pub sidecar: Arc<BlobTransactionSidecarVariant>,
961}
962
963/// Where the transaction originates from.
964///
965/// Depending on where the transaction was picked up, it affects how the transaction is handled
966/// internally, e.g. limits for simultaneous transaction of one sender.
967#[derive(Debug, Copy, Clone, PartialEq, Eq, Default, Deserialize, Serialize)]
968pub enum TransactionOrigin {
969 /// Transaction is coming from a local source.
970 #[default]
971 Local,
972 /// Transaction has been received externally.
973 ///
974 /// This is usually considered an "untrusted" source, for example received from another in the
975 /// network.
976 External,
977 /// Transaction is originated locally and is intended to remain private.
978 ///
979 /// This type of transaction should not be propagated to the network. It's meant for
980 /// private usage within the local node only.
981 Private,
982}
983
984// === impl TransactionOrigin ===
985
986impl TransactionOrigin {
987 /// Whether the transaction originates from a local source.
988 pub const fn is_local(&self) -> bool {
989 matches!(self, Self::Local)
990 }
991
992 /// Whether the transaction originates from an external source.
993 pub const fn is_external(&self) -> bool {
994 matches!(self, Self::External)
995 }
996 /// Whether the transaction originates from a private source.
997 pub const fn is_private(&self) -> bool {
998 matches!(self, Self::Private)
999 }
1000}
1001
1002/// Represents the kind of update to the canonical state.
1003#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1004pub enum PoolUpdateKind {
1005 /// The update was due to a block commit.
1006 Commit,
1007 /// The update was due to a reorganization.
1008 Reorg,
1009}
1010
1011/// Represents changes after a new canonical block or range of canonical blocks was added to the
1012/// chain.
1013///
1014/// It is expected that this is only used if the added blocks are canonical to the pool's last known
1015/// block hash. In other words, the first added block of the range must be the child of the last
1016/// known block hash.
1017///
1018/// This is used to update the pool state accordingly.
1019#[derive(Clone, Debug)]
1020pub struct CanonicalStateUpdate<'a, B: Block> {
1021 /// Hash of the tip block.
1022 pub new_tip: &'a SealedBlock<B>,
1023 /// EIP-1559 Base fee of the _next_ (pending) block
1024 ///
1025 /// The base fee of a block depends on the utilization of the last block and its base fee.
1026 pub pending_block_base_fee: u64,
1027 /// EIP-4844 blob fee of the _next_ (pending) block
1028 ///
1029 /// Only after Cancun
1030 pub pending_block_blob_fee: Option<u128>,
1031 /// A set of changed accounts across a range of blocks.
1032 pub changed_accounts: Vec<ChangedAccount>,
1033 /// All mined transactions in the block range.
1034 pub mined_transactions: Vec<B256>,
1035 /// The kind of update to the canonical state.
1036 pub update_kind: PoolUpdateKind,
1037}
1038
1039impl<B> CanonicalStateUpdate<'_, B>
1040where
1041 B: Block,
1042{
1043 /// Returns the number of the tip block.
1044 pub fn number(&self) -> u64 {
1045 self.new_tip.number()
1046 }
1047
1048 /// Returns the hash of the tip block.
1049 pub fn hash(&self) -> B256 {
1050 self.new_tip.hash()
1051 }
1052
1053 /// Timestamp of the latest chain update
1054 pub fn timestamp(&self) -> u64 {
1055 self.new_tip.timestamp()
1056 }
1057
1058 /// Returns the block info for the tip block.
1059 pub fn block_info(&self) -> BlockInfo {
1060 BlockInfo {
1061 block_gas_limit: self.new_tip.gas_limit(),
1062 last_seen_block_hash: self.hash(),
1063 last_seen_block_number: self.number(),
1064 pending_basefee: self.pending_block_base_fee,
1065 pending_blob_fee: self.pending_block_blob_fee,
1066 }
1067 }
1068}
1069
1070impl<B> fmt::Display for CanonicalStateUpdate<'_, B>
1071where
1072 B: Block,
1073{
1074 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1075 f.debug_struct("CanonicalStateUpdate")
1076 .field("hash", &self.hash())
1077 .field("number", &self.number())
1078 .field("pending_block_base_fee", &self.pending_block_base_fee)
1079 .field("pending_block_blob_fee", &self.pending_block_blob_fee)
1080 .field("changed_accounts", &self.changed_accounts.len())
1081 .field("mined_transactions", &self.mined_transactions.len())
1082 .finish()
1083 }
1084}
1085
1086/// Alias to restrict the [`BestTransactions`] items to the pool's transaction type.
1087pub type BestTransactionsFor<Pool> = Box<
1088 dyn BestTransactions<Item = Arc<ValidPoolTransaction<<Pool as TransactionPool>::Transaction>>>,
1089>;
1090
1091/// An `Iterator` that only returns transactions that are ready to be executed.
1092///
1093/// This makes no assumptions about the order of the transactions, but expects that _all_
1094/// transactions are valid (no nonce gaps.) for the tracked state of the pool.
1095///
1096/// Note: this iterator will always return the best transaction that it currently knows.
1097/// There is no guarantee transactions will be returned sequentially in decreasing
1098/// priority order.
1099pub trait BestTransactions: Iterator + Send {
1100 /// Mark the transaction as invalid.
1101 ///
1102 /// Implementers must ensure all subsequent transaction _don't_ depend on this transaction.
1103 /// In other words, this must remove the given transaction _and_ drain all transaction that
1104 /// depend on it.
1105 fn mark_invalid(&mut self, transaction: &Self::Item, kind: InvalidPoolTransactionError);
1106
1107 /// An iterator may be able to receive additional pending transactions that weren't present it
1108 /// the pool when it was created.
1109 ///
1110 /// This ensures that iterator will return the best transaction that it currently knows and not
1111 /// listen to pool updates.
1112 fn no_updates(&mut self);
1113
1114 /// Convenience function for [`Self::no_updates`] that returns the iterator again.
1115 fn without_updates(mut self) -> Self
1116 where
1117 Self: Sized,
1118 {
1119 self.no_updates();
1120 self
1121 }
1122
1123 /// Skip all blob transactions.
1124 ///
1125 /// There's only limited blob space available in a block, once exhausted, EIP-4844 transactions
1126 /// can no longer be included.
1127 ///
1128 /// If called then the iterator will no longer yield blob transactions.
1129 ///
1130 /// Note: this will also exclude any transactions that depend on blob transactions.
1131 fn skip_blobs(&mut self) {
1132 self.set_skip_blobs(true);
1133 }
1134
1135 /// Controls whether the iterator skips blob transactions or not.
1136 ///
1137 /// If set to true, no blob transactions will be returned.
1138 fn set_skip_blobs(&mut self, skip_blobs: bool);
1139
1140 /// Convenience function for [`Self::skip_blobs`] that returns the iterator again.
1141 fn without_blobs(mut self) -> Self
1142 where
1143 Self: Sized,
1144 {
1145 self.skip_blobs();
1146 self
1147 }
1148
1149 /// Creates an iterator which uses a closure to determine whether a transaction should be
1150 /// returned by the iterator.
1151 ///
1152 /// All items the closure returns false for are marked as invalid via [`Self::mark_invalid`] and
1153 /// descendant transactions will be skipped.
1154 fn filter_transactions<P>(self, predicate: P) -> BestTransactionFilter<Self, P>
1155 where
1156 P: FnMut(&Self::Item) -> bool,
1157 Self: Sized,
1158 {
1159 BestTransactionFilter::new(self, predicate)
1160 }
1161}
1162
1163impl<T> BestTransactions for Box<T>
1164where
1165 T: BestTransactions + ?Sized,
1166{
1167 fn mark_invalid(&mut self, transaction: &Self::Item, kind: InvalidPoolTransactionError) {
1168 (**self).mark_invalid(transaction, kind)
1169 }
1170
1171 fn no_updates(&mut self) {
1172 (**self).no_updates();
1173 }
1174
1175 fn skip_blobs(&mut self) {
1176 (**self).skip_blobs();
1177 }
1178
1179 fn set_skip_blobs(&mut self, skip_blobs: bool) {
1180 (**self).set_skip_blobs(skip_blobs);
1181 }
1182}
1183
1184/// A no-op implementation that yields no transactions.
1185impl<T> BestTransactions for std::iter::Empty<T> {
1186 fn mark_invalid(&mut self, _tx: &T, _kind: InvalidPoolTransactionError) {}
1187
1188 fn no_updates(&mut self) {}
1189
1190 fn skip_blobs(&mut self) {}
1191
1192 fn set_skip_blobs(&mut self, _skip_blobs: bool) {}
1193}
1194
1195/// A filter that allows to check if a transaction satisfies a set of conditions
1196pub trait TransactionFilter {
1197 /// The type of the transaction to check.
1198 type Transaction;
1199
1200 /// Returns true if the transaction satisfies the conditions.
1201 fn is_valid(&self, transaction: &Self::Transaction) -> bool;
1202}
1203
1204/// A no-op implementation of [`TransactionFilter`] which
1205/// marks all transactions as valid.
1206#[derive(Debug, Clone)]
1207pub struct NoopTransactionFilter<T>(std::marker::PhantomData<T>);
1208
1209// We can't derive Default because this forces T to be
1210// Default as well, which isn't necessary.
1211impl<T> Default for NoopTransactionFilter<T> {
1212 fn default() -> Self {
1213 Self(std::marker::PhantomData)
1214 }
1215}
1216
1217impl<T> TransactionFilter for NoopTransactionFilter<T> {
1218 type Transaction = T;
1219
1220 fn is_valid(&self, _transaction: &Self::Transaction) -> bool {
1221 true
1222 }
1223}
1224
1225/// A Helper type that bundles the best transactions attributes together.
1226#[derive(Debug, Copy, Clone, PartialEq, Eq)]
1227pub struct BestTransactionsAttributes {
1228 /// The base fee attribute for best transactions.
1229 pub basefee: u64,
1230 /// The blob fee attribute for best transactions.
1231 pub blob_fee: Option<u64>,
1232}
1233
1234// === impl BestTransactionsAttributes ===
1235
1236impl BestTransactionsAttributes {
1237 /// Creates a new `BestTransactionsAttributes` with the given basefee and blob fee.
1238 pub const fn new(basefee: u64, blob_fee: Option<u64>) -> Self {
1239 Self { basefee, blob_fee }
1240 }
1241
1242 /// Creates a new `BestTransactionsAttributes` with the given basefee.
1243 pub const fn base_fee(basefee: u64) -> Self {
1244 Self::new(basefee, None)
1245 }
1246
1247 /// Sets the given blob fee.
1248 pub const fn with_blob_fee(mut self, blob_fee: u64) -> Self {
1249 self.blob_fee = Some(blob_fee);
1250 self
1251 }
1252}
1253
1254/// Trait for transaction types stored in the transaction pool.
1255///
1256/// This trait represents the actual transaction object stored in the mempool, which includes not
1257/// only the transaction data itself but also additional metadata needed for efficient pool
1258/// operations. Implementations typically cache values that are frequently accessed during
1259/// transaction ordering, validation, and eviction.
1260///
1261/// ## Key Responsibilities
1262///
1263/// 1. **Metadata Caching**: Store computed values like address, cost and encoded size
1264/// 2. **Representation Conversion**: Handle conversions between consensus and pooled
1265/// representations
1266/// 3. **Validation Support**: Provide methods for pool-specific validation rules
1267///
1268/// ## Cached Metadata
1269///
1270/// Implementations should cache frequently accessed values to avoid recomputation:
1271/// - **Address**: Recovered sender address of the transaction
1272/// - **Cost**: Max amount spendable (gas × price + value + blob costs)
1273/// - **Size**: RLP encoded length for mempool size limits
1274///
1275/// See [`EthPooledTransaction`] for a reference implementation.
1276///
1277/// ## Transaction Representations
1278///
1279/// This trait abstracts over the different representations a transaction can have:
1280///
1281/// 1. **Consensus representation** (`Consensus` associated type): The canonical form included in
1282/// blocks
1283/// - Compact representation without networking metadata
1284/// - For EIP-4844: includes only blob hashes, not the actual blobs
1285/// - Used for block execution and state transitions
1286///
1287/// 2. **Pooled representation** (`Pooled` associated type): The form used for network propagation
1288/// - May include additional data for validation
1289/// - For EIP-4844: includes full blob sidecars (blobs, commitments, proofs)
1290/// - Used for mempool validation and p2p gossiping
1291///
1292/// ## Why Two Representations?
1293///
1294/// This distinction is necessary because:
1295///
1296/// - **EIP-4844 blob transactions**: Require large blob sidecars for validation that would bloat
1297/// blocks if included. Only blob hashes are stored on-chain.
1298///
1299/// - **Network efficiency**: Blob transactions are not broadcast to all peers automatically but
1300/// must be explicitly requested to reduce bandwidth usage.
1301///
1302/// - **Special transactions**: Some transactions (like OP deposit transactions) exist only in
1303/// consensus format and are never in the mempool.
1304///
1305/// ## Conversion Rules
1306///
1307/// - `Consensus` → `Pooled`: May fail for transactions that cannot be pooled (e.g., OP deposit
1308/// transactions, blob transactions without sidecars)
1309/// - `Pooled` → `Consensus`: Always succeeds (pooled is a superset)
1310pub trait PoolTransaction:
1311 alloy_consensus::Transaction + InMemorySize + Debug + Send + Sync + Clone
1312{
1313 /// Associated error type for the `try_from_consensus` method.
1314 type TryFromConsensusError: fmt::Display;
1315
1316 /// Associated type representing the raw consensus variant of the transaction.
1317 type Consensus: SignedTransaction + From<Self::Pooled>;
1318
1319 /// Associated type representing the recovered pooled variant of the transaction.
1320 type Pooled: TryFrom<Self::Consensus, Error = Self::TryFromConsensusError> + SignedTransaction;
1321
1322 /// Define a method to convert from the `Consensus` type to `Self`
1323 ///
1324 /// This conversion may fail for transactions that are valid for inclusion in blocks
1325 /// but cannot exist in the transaction pool. Examples include:
1326 ///
1327 /// - **OP Deposit transactions**: These are special system transactions that are directly
1328 /// included in blocks by the sequencer/validator and never enter the mempool
1329 /// - **Blob transactions without sidecars**: After being included in a block, the sidecar data
1330 /// is pruned, making the consensus transaction unpoolable
1331 fn try_from_consensus(
1332 tx: Recovered<Self::Consensus>,
1333 ) -> Result<Self, Self::TryFromConsensusError> {
1334 let (tx, signer) = tx.into_parts();
1335 Ok(Self::from_pooled(Recovered::new_unchecked(tx.try_into()?, signer)))
1336 }
1337
1338 /// Clone the transaction into a consensus variant.
1339 ///
1340 /// This method is preferred when the [`PoolTransaction`] already wraps the consensus variant.
1341 fn clone_into_consensus(&self) -> Recovered<Self::Consensus> {
1342 self.clone().into_consensus()
1343 }
1344
1345 /// Returns a reference to the consensus transaction with the recovered sender.
1346 fn consensus_ref(&self) -> Recovered<&Self::Consensus>;
1347
1348 /// Define a method to convert from the `Self` type to `Consensus`
1349 fn into_consensus(self) -> Recovered<Self::Consensus>;
1350
1351 /// Converts the transaction into consensus format while preserving the EIP-2718 encoded bytes.
1352 /// This is used to optimize transaction execution by reusing cached encoded bytes instead of
1353 /// re-encoding the transaction. The cached bytes are particularly useful in payload building
1354 /// where the same transaction may be executed multiple times.
1355 fn into_consensus_with2718(self) -> WithEncoded<Recovered<Self::Consensus>> {
1356 self.into_consensus().into_encoded()
1357 }
1358
1359 /// Define a method to convert from the `Pooled` type to `Self`
1360 fn from_pooled(pooled: Recovered<Self::Pooled>) -> Self;
1361
1362 /// Decodes and recovers a raw transaction into this pool transaction type.
1363 ///
1364 /// Implementations can override this to avoid constructing the pooled transaction as an
1365 /// intermediate value when the raw representation can be converted directly into `Self`.
1366 fn recover_raw_transaction(data: &[u8]) -> Result<Self, RawPoolTransactionError> {
1367 if data.is_empty() {
1368 return Err(RawPoolTransactionError::EmptyRawTransactionData)
1369 }
1370
1371 let transaction = Self::Pooled::decode_2718_exact(data)
1372 .map_err(|_| RawPoolTransactionError::FailedToDecodeSignedTransaction)?;
1373
1374 transaction
1375 .try_into_recovered()
1376 .map(Self::from_pooled)
1377 .map_err(|_| RawPoolTransactionError::InvalidTransactionSignature)
1378 }
1379
1380 /// Tries to convert the `Consensus` type into the `Pooled` type.
1381 fn try_into_pooled(self) -> Result<Recovered<Self::Pooled>, Self::TryFromConsensusError> {
1382 let consensus = self.into_consensus();
1383 let (tx, signer) = consensus.into_parts();
1384 Ok(Recovered::new_unchecked(tx.try_into()?, signer))
1385 }
1386
1387 /// Clones the consensus transactions and tries to convert the `Consensus` type into the
1388 /// `Pooled` type.
1389 fn clone_into_pooled(&self) -> Result<Recovered<Self::Pooled>, Self::TryFromConsensusError> {
1390 let consensus = self.clone_into_consensus();
1391 let (tx, signer) = consensus.into_parts();
1392 Ok(Recovered::new_unchecked(tx.try_into()?, signer))
1393 }
1394
1395 /// Converts the `Pooled` type into the `Consensus` type.
1396 fn pooled_into_consensus(tx: Self::Pooled) -> Self::Consensus {
1397 tx.into()
1398 }
1399
1400 /// Hash of the transaction.
1401 fn hash(&self) -> &TxHash;
1402
1403 /// The Sender of the transaction.
1404 fn sender(&self) -> Address;
1405
1406 /// Reference to the Sender of the transaction.
1407 fn sender_ref(&self) -> &Address;
1408
1409 /// Returns the cost that this transaction is allowed to consume:
1410 ///
1411 /// For EIP-1559 transactions: `max_fee_per_gas * gas_limit + tx_value`.
1412 /// For legacy transactions: `gas_price * gas_limit + tx_value`.
1413 /// For EIP-4844 blob transactions: `max_fee_per_gas * gas_limit + tx_value +
1414 /// max_blob_fee_per_gas * blob_gas_used`.
1415 fn cost(&self) -> &U256;
1416
1417 /// Returns the length of the rlp encoded transaction object
1418 ///
1419 /// Note: Implementations should cache this value.
1420 fn encoded_length(&self) -> usize;
1421
1422 /// Ensures that the transaction's code size does not exceed the provided `max_init_code_size`.
1423 ///
1424 /// This is specifically relevant for contract creation transactions ([`TxKind::Create`]),
1425 /// where the input data contains the initialization code. If the input code size exceeds
1426 /// the configured limit, an [`InvalidPoolTransactionError::ExceedsMaxInitCodeSize`] error is
1427 /// returned.
1428 fn ensure_max_init_code_size(
1429 &self,
1430 max_init_code_size: usize,
1431 ) -> Result<(), InvalidPoolTransactionError> {
1432 let input_len = self.input().len();
1433 if self.is_create() && input_len > max_init_code_size {
1434 Err(InvalidPoolTransactionError::ExceedsMaxInitCodeSize(input_len, max_init_code_size))
1435 } else {
1436 Ok(())
1437 }
1438 }
1439
1440 /// Allows to communicate to the pool that the transaction doesn't require a nonce check.
1441 fn requires_nonce_check(&self) -> bool {
1442 true
1443 }
1444}
1445
1446/// Super trait for transactions that can be converted to and from Eth transactions intended for the
1447/// ethereum style pool.
1448///
1449/// This extends the [`PoolTransaction`] trait with additional methods that are specific to the
1450/// Ethereum pool.
1451pub trait EthPoolTransaction: PoolTransaction {
1452 /// Extracts the blob sidecar from the transaction.
1453 fn take_blob(&mut self) -> EthBlobTransactionSidecar;
1454
1455 /// A specialization for the EIP-4844 transaction type.
1456 /// Tries to reattach the blob sidecar to the transaction.
1457 ///
1458 /// This returns an option, but callers should ensure that the transaction is an EIP-4844
1459 /// transaction: [`Typed2718::is_eip4844`].
1460 fn try_into_pooled_eip4844(
1461 self,
1462 sidecar: Arc<BlobTransactionSidecarVariant>,
1463 ) -> Option<Recovered<Self::Pooled>>;
1464
1465 /// Tries to convert the `Consensus` type with a blob sidecar into the `Pooled` type.
1466 ///
1467 /// Returns `None` if passed transaction is not a blob transaction.
1468 fn try_from_eip4844(
1469 tx: Recovered<Self::Consensus>,
1470 sidecar: BlobTransactionSidecarVariant,
1471 ) -> Option<Self>;
1472
1473 /// Validates the blob sidecar of the transaction with the given settings.
1474 fn validate_blob(
1475 &self,
1476 blob: &BlobTransactionSidecarVariant,
1477 settings: &KzgSettings,
1478 ) -> Result<(), BlobTransactionValidationError>;
1479}
1480
1481/// The default [`PoolTransaction`] for the [Pool](crate::Pool) for Ethereum.
1482///
1483/// This type wraps a consensus transaction with additional cached data that's
1484/// frequently accessed by the pool for transaction ordering and validation:
1485///
1486/// - `cost`: Pre-calculated max cost (gas * price + value + blob costs)
1487/// - `encoded_length`: Cached RLP encoding length for size limits
1488/// - `blob_sidecar`: Blob data state (None/Missing/Present)
1489///
1490/// This avoids recalculating these values repeatedly during pool operations.
1491#[derive(Debug, Clone, PartialEq, Eq)]
1492pub struct EthPooledTransaction<T = TransactionSigned> {
1493 /// `EcRecovered` transaction, the consensus format.
1494 pub transaction: Recovered<T>,
1495
1496 /// For EIP-1559 transactions: `max_fee_per_gas * gas_limit + tx_value`.
1497 /// For legacy transactions: `gas_price * gas_limit + tx_value`.
1498 /// For EIP-4844 blob transactions: `max_fee_per_gas * gas_limit + tx_value +
1499 /// max_blob_fee_per_gas * blob_gas_used`.
1500 pub cost: U256,
1501
1502 /// This is the RLP length of the transaction, computed when the transaction is added to the
1503 /// pool.
1504 pub encoded_length: usize,
1505
1506 /// The blob side car for this transaction
1507 pub blob_sidecar: EthBlobTransactionSidecar,
1508}
1509
1510impl<T: SignedTransaction> EthPooledTransaction<T> {
1511 /// Create new instance of [Self].
1512 ///
1513 /// Caution: In case of blob transactions, this marks the blob sidecar as
1514 /// [`EthBlobTransactionSidecar::Missing`]
1515 pub fn new(transaction: Recovered<T>, encoded_length: usize) -> Self {
1516 let mut blob_sidecar = EthBlobTransactionSidecar::None;
1517
1518 let gas_cost = U256::from(transaction.max_fee_per_gas())
1519 .saturating_mul(U256::from(transaction.gas_limit()));
1520
1521 let mut cost = gas_cost.saturating_add(transaction.value());
1522
1523 if let (Some(blob_gas_used), Some(max_fee_per_blob_gas)) =
1524 (transaction.blob_gas_used(), transaction.max_fee_per_blob_gas())
1525 {
1526 // Add max blob cost using saturating math to avoid overflow
1527 cost = cost.saturating_add(U256::from(
1528 max_fee_per_blob_gas.saturating_mul(blob_gas_used as u128),
1529 ));
1530
1531 // because the blob sidecar is not included in this transaction variant, mark it as
1532 // missing
1533 blob_sidecar = EthBlobTransactionSidecar::Missing;
1534 }
1535
1536 Self { transaction, cost, encoded_length, blob_sidecar }
1537 }
1538
1539 /// Return the reference to the underlying transaction.
1540 pub const fn transaction(&self) -> &Recovered<T> {
1541 &self.transaction
1542 }
1543}
1544
1545impl PoolTransaction for EthPooledTransaction {
1546 type TryFromConsensusError = ValueError<TransactionSigned>;
1547
1548 type Consensus = TransactionSigned;
1549
1550 type Pooled = PooledTransactionVariant;
1551
1552 fn clone_into_consensus(&self) -> Recovered<Self::Consensus> {
1553 self.transaction().clone()
1554 }
1555
1556 fn consensus_ref(&self) -> Recovered<&Self::Consensus> {
1557 Recovered::new_unchecked(&*self.transaction, self.transaction.signer())
1558 }
1559
1560 fn into_consensus(self) -> Recovered<Self::Consensus> {
1561 self.transaction
1562 }
1563
1564 fn from_pooled(tx: Recovered<Self::Pooled>) -> Self {
1565 let encoded_length = tx.encode_2718_len();
1566 let (tx, signer) = tx.into_parts();
1567 match tx {
1568 PooledTransactionVariant::Eip4844(tx) => {
1569 // include the blob sidecar
1570 let (tx, sig, hash) = tx.into_parts();
1571 let (tx, blob) = tx.into_parts();
1572 let tx = Signed::new_unchecked(tx, sig, hash);
1573 let tx = TransactionSigned::from(tx);
1574 let tx = Recovered::new_unchecked(tx, signer);
1575 let mut pooled = Self::new(tx, encoded_length);
1576 pooled.blob_sidecar = EthBlobTransactionSidecar::Present(blob);
1577 pooled
1578 }
1579 tx => {
1580 // no blob sidecar
1581 let tx = Recovered::new_unchecked(tx.into(), signer);
1582 Self::new(tx, encoded_length)
1583 }
1584 }
1585 }
1586
1587 /// Returns hash of the transaction.
1588 fn hash(&self) -> &TxHash {
1589 self.transaction.tx_hash()
1590 }
1591
1592 /// Returns the Sender of the transaction.
1593 fn sender(&self) -> Address {
1594 self.transaction.signer()
1595 }
1596
1597 /// Returns a reference to the Sender of the transaction.
1598 fn sender_ref(&self) -> &Address {
1599 self.transaction.signer_ref()
1600 }
1601
1602 /// Returns the cost that this transaction is allowed to consume:
1603 ///
1604 /// For EIP-1559 transactions: `max_fee_per_gas * gas_limit + tx_value`.
1605 /// For legacy transactions: `gas_price * gas_limit + tx_value`.
1606 /// For EIP-4844 blob transactions: `max_fee_per_gas * gas_limit + tx_value +
1607 /// max_blob_fee_per_gas * blob_gas_used`.
1608 fn cost(&self) -> &U256 {
1609 &self.cost
1610 }
1611
1612 /// Returns the length of the rlp encoded object
1613 fn encoded_length(&self) -> usize {
1614 self.encoded_length
1615 }
1616}
1617
1618impl<T: Typed2718> Typed2718 for EthPooledTransaction<T> {
1619 fn ty(&self) -> u8 {
1620 self.transaction.ty()
1621 }
1622}
1623
1624impl<T: InMemorySize> InMemorySize for EthPooledTransaction<T> {
1625 fn size(&self) -> usize {
1626 self.transaction.size()
1627 }
1628}
1629
1630impl<T: alloy_consensus::Transaction> alloy_consensus::Transaction for EthPooledTransaction<T> {
1631 fn chain_id(&self) -> Option<alloy_primitives::ChainId> {
1632 self.transaction.chain_id()
1633 }
1634
1635 fn nonce(&self) -> u64 {
1636 self.transaction.nonce()
1637 }
1638
1639 fn gas_limit(&self) -> u64 {
1640 self.transaction.gas_limit()
1641 }
1642
1643 fn gas_price(&self) -> Option<u128> {
1644 self.transaction.gas_price()
1645 }
1646
1647 fn max_fee_per_gas(&self) -> u128 {
1648 self.transaction.max_fee_per_gas()
1649 }
1650
1651 fn max_priority_fee_per_gas(&self) -> Option<u128> {
1652 self.transaction.max_priority_fee_per_gas()
1653 }
1654
1655 fn max_fee_per_blob_gas(&self) -> Option<u128> {
1656 self.transaction.max_fee_per_blob_gas()
1657 }
1658
1659 fn priority_fee_or_price(&self) -> u128 {
1660 self.transaction.priority_fee_or_price()
1661 }
1662
1663 fn effective_gas_price(&self, base_fee: Option<u64>) -> u128 {
1664 self.transaction.effective_gas_price(base_fee)
1665 }
1666
1667 fn is_dynamic_fee(&self) -> bool {
1668 self.transaction.is_dynamic_fee()
1669 }
1670
1671 fn kind(&self) -> TxKind {
1672 self.transaction.kind()
1673 }
1674
1675 fn is_create(&self) -> bool {
1676 self.transaction.is_create()
1677 }
1678
1679 fn value(&self) -> U256 {
1680 self.transaction.value()
1681 }
1682
1683 fn input(&self) -> &Bytes {
1684 self.transaction.input()
1685 }
1686
1687 fn access_list(&self) -> Option<&AccessList> {
1688 self.transaction.access_list()
1689 }
1690
1691 fn blob_versioned_hashes(&self) -> Option<&[B256]> {
1692 self.transaction.blob_versioned_hashes()
1693 }
1694
1695 fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
1696 self.transaction.authorization_list()
1697 }
1698}
1699
1700impl EthPoolTransaction for EthPooledTransaction {
1701 fn take_blob(&mut self) -> EthBlobTransactionSidecar {
1702 if self.is_eip4844() {
1703 std::mem::replace(&mut self.blob_sidecar, EthBlobTransactionSidecar::Missing)
1704 } else {
1705 EthBlobTransactionSidecar::None
1706 }
1707 }
1708
1709 fn try_into_pooled_eip4844(
1710 self,
1711 sidecar: Arc<BlobTransactionSidecarVariant>,
1712 ) -> Option<Recovered<Self::Pooled>> {
1713 let (signed_transaction, signer) = self.into_consensus().into_parts();
1714 let pooled_transaction =
1715 signed_transaction.try_into_pooled_eip4844(Arc::unwrap_or_clone(sidecar)).ok()?;
1716
1717 Some(Recovered::new_unchecked(pooled_transaction, signer))
1718 }
1719
1720 fn try_from_eip4844(
1721 tx: Recovered<Self::Consensus>,
1722 sidecar: BlobTransactionSidecarVariant,
1723 ) -> Option<Self> {
1724 let (tx, signer) = tx.into_parts();
1725 tx.try_into_pooled_eip4844(sidecar)
1726 .ok()
1727 .map(|tx| tx.with_signer(signer))
1728 .map(Self::from_pooled)
1729 }
1730
1731 fn validate_blob(
1732 &self,
1733 sidecar: &BlobTransactionSidecarVariant,
1734 settings: &KzgSettings,
1735 ) -> Result<(), BlobTransactionValidationError> {
1736 match self.transaction.inner().as_eip4844() {
1737 Some(tx) => tx.tx().validate_blob(sidecar, settings),
1738 _ => Err(BlobTransactionValidationError::NotBlobTransaction(self.ty())),
1739 }
1740 }
1741}
1742
1743/// Represents the blob sidecar of the [`EthPooledTransaction`].
1744///
1745/// EIP-4844 blob transactions require additional data (blobs, commitments, proofs)
1746/// for validation that is not included in the consensus format. This enum tracks
1747/// the sidecar state throughout the transaction's lifecycle in the pool.
1748#[derive(Debug, Clone, PartialEq, Eq)]
1749pub enum EthBlobTransactionSidecar {
1750 /// This transaction does not have a blob sidecar
1751 /// (applies to all non-EIP-4844 transaction types)
1752 None,
1753 /// This transaction has a blob sidecar (EIP-4844) but it is missing.
1754 ///
1755 /// This can happen when:
1756 /// - The sidecar was extracted after the transaction was added to the pool
1757 /// - The transaction was re-injected after a reorg without its sidecar
1758 /// - The transaction was recovered from the consensus format (e.g., from a block)
1759 Missing,
1760 /// The EIP-4844 transaction was received from the network with its complete sidecar.
1761 ///
1762 /// This sidecar contains:
1763 /// - The actual blob data (large data per blob)
1764 /// - KZG commitments for each blob
1765 /// - KZG proofs for validation
1766 ///
1767 /// The sidecar is required for validating the transaction but is not included
1768 /// in blocks (only the blob hashes are included in the consensus format).
1769 Present(BlobTransactionSidecarVariant),
1770}
1771
1772impl EthBlobTransactionSidecar {
1773 /// Returns the blob sidecar if it is present
1774 pub const fn maybe_sidecar(&self) -> Option<&BlobTransactionSidecarVariant> {
1775 match self {
1776 Self::Present(sidecar) => Some(sidecar),
1777 _ => None,
1778 }
1779 }
1780}
1781
1782/// Represents the current status of the pool.
1783#[derive(Debug, Clone, Copy, Default)]
1784pub struct PoolSize {
1785 /// Number of transactions in the _pending_ sub-pool.
1786 pub pending: usize,
1787 /// Reported size of transactions in the _pending_ sub-pool.
1788 pub pending_size: usize,
1789 /// Number of transactions in the _blob_ pool.
1790 pub blob: usize,
1791 /// Reported size of transactions in the _blob_ pool.
1792 pub blob_size: usize,
1793 /// Number of transactions in the _basefee_ pool.
1794 pub basefee: usize,
1795 /// Reported size of transactions in the _basefee_ sub-pool.
1796 pub basefee_size: usize,
1797 /// Number of transactions in the _queued_ sub-pool.
1798 pub queued: usize,
1799 /// Reported size of transactions in the _queued_ sub-pool.
1800 pub queued_size: usize,
1801 /// Number of all transactions of all sub-pools
1802 ///
1803 /// Note: this is the sum of ```pending + basefee + queued + blob```
1804 pub total: usize,
1805}
1806
1807// === impl PoolSize ===
1808
1809impl PoolSize {
1810 /// Asserts that the invariants of the pool size are met.
1811 #[cfg(test)]
1812 pub(crate) fn assert_invariants(&self) {
1813 assert_eq!(self.total, self.pending + self.basefee + self.queued + self.blob);
1814 }
1815}
1816
1817/// Represents the current status of the pool.
1818#[derive(Default, Debug, Clone, Copy, Eq, PartialEq)]
1819pub struct BlockInfo {
1820 /// Hash for the currently tracked block.
1821 pub last_seen_block_hash: B256,
1822 /// Currently tracked block.
1823 pub last_seen_block_number: u64,
1824 /// Current block gas limit for the latest block.
1825 pub block_gas_limit: u64,
1826 /// Currently enforced base fee: the threshold for the basefee sub-pool.
1827 ///
1828 /// Note: this is the derived base fee of the _next_ block that builds on the block the pool is
1829 /// currently tracking.
1830 pub pending_basefee: u64,
1831 /// Currently enforced blob fee: the threshold for eip-4844 blob transactions.
1832 ///
1833 /// Note: this is the derived blob fee of the _next_ block that builds on the block the pool is
1834 /// currently tracking
1835 pub pending_blob_fee: Option<u128>,
1836}
1837
1838/// The limit to enforce for [`TransactionPool::get_pooled_transaction_elements`].
1839#[derive(Debug, Clone, Copy, Eq, PartialEq)]
1840pub enum GetPooledTransactionLimit {
1841 /// No limit, return all transactions.
1842 None,
1843 /// Enforce a size limit on the returned transactions, for example 2MB
1844 ResponseSizeSoftLimit(usize),
1845}
1846
1847impl GetPooledTransactionLimit {
1848 /// Returns true if the given size exceeds the limit.
1849 #[inline]
1850 pub const fn exceeds(&self, size: usize) -> bool {
1851 match self {
1852 Self::None => false,
1853 Self::ResponseSizeSoftLimit(limit) => size > *limit,
1854 }
1855 }
1856}
1857
1858/// A Stream that yields full transactions the subpool
1859#[must_use = "streams do nothing unless polled"]
1860#[derive(Debug)]
1861pub struct NewSubpoolTransactionStream<Tx: PoolTransaction> {
1862 st: Receiver<NewTransactionEvent<Tx>>,
1863 subpool: SubPool,
1864}
1865
1866// === impl NewSubpoolTransactionStream ===
1867
1868impl<Tx: PoolTransaction> NewSubpoolTransactionStream<Tx> {
1869 /// Create a new stream that yields full transactions from the subpool
1870 pub const fn new(st: Receiver<NewTransactionEvent<Tx>>, subpool: SubPool) -> Self {
1871 Self { st, subpool }
1872 }
1873
1874 /// Tries to receive the next value for this stream.
1875 pub fn try_recv(
1876 &mut self,
1877 ) -> Result<NewTransactionEvent<Tx>, tokio::sync::mpsc::error::TryRecvError> {
1878 loop {
1879 let event = self.st.try_recv()?;
1880 if event.subpool == self.subpool {
1881 return Ok(event)
1882 }
1883 }
1884 }
1885}
1886
1887impl<Tx: PoolTransaction> Stream for NewSubpoolTransactionStream<Tx> {
1888 type Item = NewTransactionEvent<Tx>;
1889
1890 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
1891 loop {
1892 match ready!(self.st.poll_recv(cx)) {
1893 Some(event) => {
1894 if event.subpool == self.subpool {
1895 return Poll::Ready(Some(event))
1896 }
1897 }
1898 None => return Poll::Ready(None),
1899 }
1900 }
1901 }
1902}
1903
1904#[cfg(test)]
1905mod tests {
1906 use super::*;
1907 use alloy_consensus::{
1908 EthereumTxEnvelope, SignableTransaction, TxEip1559, TxEip2930, TxEip4844, TxEip7702,
1909 TxEnvelope, TxLegacy,
1910 };
1911 use alloy_eips::eip4844::DATA_GAS_PER_BLOB;
1912 use alloy_primitives::Signature;
1913
1914 #[test]
1915 fn test_pool_size_invariants() {
1916 let pool_size = PoolSize {
1917 pending: 10,
1918 pending_size: 1000,
1919 blob: 5,
1920 blob_size: 500,
1921 basefee: 8,
1922 basefee_size: 800,
1923 queued: 7,
1924 queued_size: 700,
1925 total: 10 + 5 + 8 + 7, // Correct total
1926 };
1927
1928 // Call the assert_invariants method to check if the invariants are correct
1929 pool_size.assert_invariants();
1930 }
1931
1932 #[test]
1933 #[should_panic]
1934 fn test_pool_size_invariants_fail() {
1935 let pool_size = PoolSize {
1936 pending: 10,
1937 pending_size: 1000,
1938 blob: 5,
1939 blob_size: 500,
1940 basefee: 8,
1941 basefee_size: 800,
1942 queued: 7,
1943 queued_size: 700,
1944 total: 10 + 5 + 8, // Incorrect total
1945 };
1946
1947 // Call the assert_invariants method, which should panic
1948 pool_size.assert_invariants();
1949 }
1950
1951 #[test]
1952 fn test_eth_pooled_transaction_new_legacy() {
1953 // Create a legacy transaction with specific parameters
1954 let tx = TxEnvelope::Legacy(
1955 TxLegacy {
1956 gas_price: 10,
1957 gas_limit: 1000,
1958 value: U256::from(100),
1959 ..Default::default()
1960 }
1961 .into_signed(Signature::test_signature()),
1962 );
1963 let transaction = Recovered::new_unchecked(tx, Default::default());
1964 let pooled_tx = EthPooledTransaction::new(transaction.clone(), 200);
1965
1966 // Check that the pooled transaction is created correctly
1967 assert_eq!(pooled_tx.transaction, transaction);
1968 assert_eq!(pooled_tx.encoded_length, 200);
1969 assert_eq!(pooled_tx.blob_sidecar, EthBlobTransactionSidecar::None);
1970 assert_eq!(pooled_tx.cost, U256::from(100) + U256::from(10 * 1000));
1971 }
1972
1973 #[test]
1974 fn test_eth_pooled_transaction_new_eip2930() {
1975 // Create an EIP-2930 transaction with specific parameters
1976 let tx = TxEnvelope::Eip2930(
1977 TxEip2930 {
1978 gas_price: 10,
1979 gas_limit: 1000,
1980 value: U256::from(100),
1981 ..Default::default()
1982 }
1983 .into_signed(Signature::test_signature()),
1984 );
1985 let transaction = Recovered::new_unchecked(tx, Default::default());
1986 let pooled_tx = EthPooledTransaction::new(transaction.clone(), 200);
1987 let expected_cost = U256::from(100) + (U256::from(10 * 1000));
1988
1989 assert_eq!(pooled_tx.transaction, transaction);
1990 assert_eq!(pooled_tx.encoded_length, 200);
1991 assert_eq!(pooled_tx.blob_sidecar, EthBlobTransactionSidecar::None);
1992 assert_eq!(pooled_tx.cost, expected_cost);
1993 }
1994
1995 #[test]
1996 fn test_eth_pooled_transaction_new_eip1559() {
1997 // Create an EIP-1559 transaction with specific parameters
1998 let tx = TxEnvelope::Eip1559(
1999 TxEip1559 {
2000 max_fee_per_gas: 10,
2001 gas_limit: 1000,
2002 value: U256::from(100),
2003 ..Default::default()
2004 }
2005 .into_signed(Signature::test_signature()),
2006 );
2007 let transaction = Recovered::new_unchecked(tx, Default::default());
2008 let pooled_tx = EthPooledTransaction::new(transaction.clone(), 200);
2009
2010 // Check that the pooled transaction is created correctly
2011 assert_eq!(pooled_tx.transaction, transaction);
2012 assert_eq!(pooled_tx.encoded_length, 200);
2013 assert_eq!(pooled_tx.blob_sidecar, EthBlobTransactionSidecar::None);
2014 assert_eq!(pooled_tx.cost, U256::from(100) + U256::from(10 * 1000));
2015 }
2016
2017 #[test]
2018 fn test_eth_pooled_transaction_new_eip4844() {
2019 // Create an EIP-4844 transaction with specific parameters
2020 let tx = EthereumTxEnvelope::Eip4844(
2021 TxEip4844 {
2022 max_fee_per_gas: 10,
2023 gas_limit: 1000,
2024 value: U256::from(100),
2025 max_fee_per_blob_gas: 5,
2026 blob_versioned_hashes: vec![B256::default()],
2027 ..Default::default()
2028 }
2029 .into_signed(Signature::test_signature()),
2030 );
2031 let transaction = Recovered::new_unchecked(tx, Default::default());
2032 let pooled_tx = EthPooledTransaction::new(transaction.clone(), 300);
2033
2034 // Check that the pooled transaction is created correctly
2035 assert_eq!(pooled_tx.transaction, transaction);
2036 assert_eq!(pooled_tx.encoded_length, 300);
2037 assert_eq!(pooled_tx.blob_sidecar, EthBlobTransactionSidecar::Missing);
2038 let expected_cost =
2039 U256::from(100) + U256::from(10 * 1000) + U256::from(5 * DATA_GAS_PER_BLOB);
2040 assert_eq!(pooled_tx.cost, expected_cost);
2041 }
2042
2043 #[test]
2044 fn test_eth_pooled_transaction_new_eip7702() {
2045 // Init an EIP-7702 transaction with specific parameters
2046 let tx = EthereumTxEnvelope::<TxEip4844>::Eip7702(
2047 TxEip7702 {
2048 max_fee_per_gas: 10,
2049 gas_limit: 1000,
2050 value: U256::from(100),
2051 ..Default::default()
2052 }
2053 .into_signed(Signature::test_signature()),
2054 );
2055 let transaction = Recovered::new_unchecked(tx, Default::default());
2056 let pooled_tx = EthPooledTransaction::new(transaction.clone(), 200);
2057
2058 // Check that the pooled transaction is created correctly
2059 assert_eq!(pooled_tx.transaction, transaction);
2060 assert_eq!(pooled_tx.encoded_length, 200);
2061 assert_eq!(pooled_tx.blob_sidecar, EthBlobTransactionSidecar::None);
2062 assert_eq!(pooled_tx.cost, U256::from(100) + U256::from(10 * 1000));
2063 }
2064
2065 #[test]
2066 fn test_pooled_transaction_limit() {
2067 // No limit should never exceed
2068 let limit_none = GetPooledTransactionLimit::None;
2069 // Any size should return false
2070 assert!(!limit_none.exceeds(1000));
2071
2072 // Size limit of 2MB (2 * 1024 * 1024 bytes)
2073 let size_limit_2mb = GetPooledTransactionLimit::ResponseSizeSoftLimit(2 * 1024 * 1024);
2074
2075 // Test with size below the limit
2076 // 1MB is below 2MB, should return false
2077 assert!(!size_limit_2mb.exceeds(1024 * 1024));
2078
2079 // Test with size exactly at the limit
2080 // 2MB equals the limit, should return false
2081 assert!(!size_limit_2mb.exceeds(2 * 1024 * 1024));
2082
2083 // Test with size exceeding the limit
2084 // 3MB is above the 2MB limit, should return true
2085 assert!(size_limit_2mb.exceeds(3 * 1024 * 1024));
2086 }
2087}