1use crate::eth::{error::PoolError, util::hex_fmt_many};
2use alloy_consensus::{
3 Transaction, Typed2718,
4 crypto::RecoveryError,
5 transaction::{SignerRecoverable, TxHashRef},
6};
7use alloy_network::AnyRpcTransaction;
8use alloy_primitives::{
9 Address, TxHash,
10 map::{HashMap, HashSet},
11};
12use alloy_rlp::Encodable;
13use anvil_core::eth::transaction::PendingTransaction;
14use parking_lot::RwLock;
15use std::{cmp::Ordering, collections::BTreeSet, fmt, str::FromStr, sync::Arc, time::Instant};
16
17pub type TxMarker = Vec<u8>;
19
20type ReplacedTransactions<T> = (Vec<Arc<PoolTransaction<T>>>, Vec<TxHash>);
23
24#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
28pub enum TransactionOrder {
29 Fifo,
34 #[default]
36 Fees,
37}
38
39impl TransactionOrder {
40 pub fn priority<T: Transaction>(&self, tx: &T) -> TransactionPriority {
42 match self {
43 Self::Fifo => TransactionPriority::default(),
44 Self::Fees => TransactionPriority(tx.max_fee_per_gas()),
45 }
46 }
47}
48
49impl FromStr for TransactionOrder {
50 type Err = String;
51
52 fn from_str(s: &str) -> Result<Self, Self::Err> {
53 let s = s.to_lowercase();
54 let order = match s.as_str() {
55 "fees" => Self::Fees,
56 "fifo" => Self::Fifo,
57 _ => return Err(format!("Unknown TransactionOrder: `{s}`")),
58 };
59 Ok(order)
60 }
61}
62
63#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
68pub struct TransactionPriority(pub u128);
69
70#[derive(Clone, PartialEq, Eq)]
72pub struct PoolTransaction<T> {
73 pub pending_transaction: PendingTransaction<T>,
75 pub requires: Vec<TxMarker>,
77 pub provides: Vec<TxMarker>,
79 pub priority: TransactionPriority,
81 pub is_replay: bool,
83}
84
85impl<T> PoolTransaction<T> {
88 pub const fn new(transaction: PendingTransaction<T>) -> Self {
89 Self {
90 pending_transaction: transaction,
91 requires: vec![],
92 provides: vec![],
93 priority: TransactionPriority(0),
94 is_replay: false,
95 }
96 }
97
98 pub const fn with_replay(mut self) -> Self {
100 self.is_replay = true;
101 self
102 }
103
104 pub const fn hash(&self) -> TxHash {
106 *self.pending_transaction.hash()
107 }
108}
109
110impl<T: Transaction> PoolTransaction<T> {
111 pub fn max_fee_per_gas(&self) -> u128 {
113 self.pending_transaction.transaction.max_fee_per_gas()
114 }
115}
116
117impl<T: Typed2718> PoolTransaction<T> {
118 pub fn tx_type(&self) -> u8 {
120 self.pending_transaction.transaction.ty()
121 }
122}
123
124impl<T: fmt::Debug> fmt::Debug for PoolTransaction<T> {
125 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
126 write!(fmt, "Transaction {{ ")?;
127 write!(fmt, "hash: {:?}, ", self.pending_transaction.hash())?;
128 write!(fmt, "requires: [{}], ", hex_fmt_many(self.requires.iter()))?;
129 write!(fmt, "provides: [{}], ", hex_fmt_many(self.provides.iter()))?;
130 write!(fmt, "raw tx: {:?}", self.pending_transaction)?;
131 write!(fmt, "}}")?;
132 Ok(())
133 }
134}
135
136impl<T> TryFrom<AnyRpcTransaction> for PoolTransaction<T>
137where
138 T: SignerRecoverable + TxHashRef + Encodable + TryFrom<AnyRpcTransaction>,
139 <T as TryFrom<AnyRpcTransaction>>::Error: Into<eyre::Error>,
140 RecoveryError: Into<eyre::Error>,
141{
142 type Error = eyre::Error;
143 fn try_from(value: AnyRpcTransaction) -> Result<Self, Self::Error> {
144 let typed_transaction = T::try_from(value).map_err(Into::into)?;
145 let pending_transaction = PendingTransaction::new(typed_transaction)?;
146 Ok(Self {
147 pending_transaction,
148 requires: vec![],
149 provides: vec![],
150 priority: TransactionPriority(0),
151 is_replay: false,
152 })
153 }
154}
155
156#[derive(Clone, Debug)]
160pub struct PendingTransactions<T> {
161 required_markers: HashMap<TxMarker, HashSet<TxHash>>,
163 waiting_markers: HashMap<Vec<TxMarker>, TxHash>,
165 waiting_queue: HashMap<TxHash, PendingPoolTransaction<T>>,
167}
168
169impl<T> Default for PendingTransactions<T> {
170 fn default() -> Self {
171 Self {
172 required_markers: Default::default(),
173 waiting_markers: Default::default(),
174 waiting_queue: Default::default(),
175 }
176 }
177}
178
179impl<T> PendingTransactions<T> {
180 pub fn len(&self) -> usize {
182 self.waiting_queue.len()
183 }
184
185 pub fn is_empty(&self) -> bool {
186 self.waiting_queue.is_empty()
187 }
188
189 pub fn clear(&mut self) {
191 self.required_markers.clear();
192 self.waiting_markers.clear();
193 self.waiting_queue.clear();
194 }
195
196 pub fn transactions(&self) -> impl Iterator<Item = Arc<PoolTransaction<T>>> + '_ {
198 self.waiting_queue.values().map(|tx| tx.transaction.clone())
199 }
200
201 pub fn contains(&self, hash: &TxHash) -> bool {
203 self.waiting_queue.contains_key(hash)
204 }
205
206 pub fn get(&self, hash: &TxHash) -> Option<&PendingPoolTransaction<T>> {
208 self.waiting_queue.get(hash)
209 }
210
211 pub fn mark_and_unlock(
216 &mut self,
217 markers: impl IntoIterator<Item = impl AsRef<TxMarker>>,
218 ) -> Vec<PendingPoolTransaction<T>> {
219 let mut unlocked_ready = Vec::new();
220 for mark in markers {
221 let mark = mark.as_ref();
222 if let Some(tx_hashes) = self.required_markers.remove(mark) {
223 for hash in tx_hashes {
224 let tx = self.waiting_queue.get_mut(&hash).expect("tx is included;");
225 tx.mark(mark);
226
227 if tx.is_ready() {
228 let tx = self.waiting_queue.remove(&hash).expect("tx is included;");
229 self.waiting_markers.remove(&tx.transaction.provides);
230
231 unlocked_ready.push(tx);
232 }
233 }
234 }
235 }
236
237 unlocked_ready
238 }
239
240 pub fn remove(&mut self, hashes: Vec<TxHash>) -> Vec<Arc<PoolTransaction<T>>> {
244 let mut removed = vec![];
245 for hash in hashes {
246 if let Some(waiting_tx) = self.waiting_queue.remove(&hash) {
247 self.waiting_markers.remove(&waiting_tx.transaction.provides);
248 for marker in waiting_tx.missing_markers {
249 let remove = if let Some(required) = self.required_markers.get_mut(&marker) {
250 required.remove(&hash);
251 required.is_empty()
252 } else {
253 false
254 };
255 if remove {
256 self.required_markers.remove(&marker);
257 }
258 }
259 removed.push(waiting_tx.transaction)
260 }
261 }
262 removed
263 }
264}
265
266impl<T: Transaction> PendingTransactions<T> {
267 pub fn add_transaction(&mut self, tx: PendingPoolTransaction<T>) -> Result<(), PoolError> {
269 assert!(!tx.is_ready(), "transaction must not be ready");
270 assert!(
271 !self.waiting_queue.contains_key(&tx.transaction.hash()),
272 "transaction is already added"
273 );
274
275 if let Some(replace) = self
276 .waiting_markers
277 .get(&tx.transaction.provides)
278 .and_then(|hash| self.waiting_queue.get(hash))
279 {
280 if tx.transaction.max_fee_per_gas() <= replace.transaction.max_fee_per_gas() {
282 warn!(target: "txpool", "pending replacement transaction underpriced [{:?}]", tx.transaction.hash());
283 return Err(PoolError::ReplacementUnderpriced(tx.transaction.hash()));
284 }
285 }
286
287 for marker in &tx.missing_markers {
289 self.required_markers.entry(marker.clone()).or_default().insert(tx.transaction.hash());
290 }
291
292 self.waiting_markers.insert(tx.transaction.provides.clone(), tx.transaction.hash());
294 self.waiting_queue.insert(tx.transaction.hash(), tx);
296
297 Ok(())
298 }
299}
300
301#[derive(Clone)]
303pub struct PendingPoolTransaction<T> {
304 pub transaction: Arc<PoolTransaction<T>>,
305 pub missing_markers: HashSet<TxMarker>,
307 pub added_at: Instant,
309}
310
311impl<T> PendingPoolTransaction<T> {
312 pub fn new(transaction: PoolTransaction<T>, provided: &HashMap<TxMarker, TxHash>) -> Self {
317 let missing_markers = transaction
318 .requires
319 .iter()
320 .filter(|marker| {
321 !provided.contains_key(&**marker)
323 })
324 .cloned()
325 .collect();
326
327 Self { transaction: Arc::new(transaction), missing_markers, added_at: Instant::now() }
328 }
329
330 pub fn mark(&mut self, marker: &TxMarker) {
332 self.missing_markers.remove(marker);
333 }
334
335 pub fn is_ready(&self) -> bool {
337 self.missing_markers.is_empty()
338 }
339}
340
341impl<T: fmt::Debug> fmt::Debug for PendingPoolTransaction<T> {
342 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
343 write!(fmt, "PendingTransaction {{ ")?;
344 write!(fmt, "added_at: {:?}, ", self.added_at)?;
345 write!(fmt, "tx: {:?}, ", self.transaction)?;
346 write!(fmt, "missing_markers: {{{}}}", hex_fmt_many(self.missing_markers.iter()))?;
347 write!(fmt, "}}")
348 }
349}
350
351pub struct TransactionsIterator<T> {
352 all: HashMap<TxHash, ReadyTransaction<T>>,
353 awaiting: HashMap<TxHash, (usize, PoolTransactionRef<T>)>,
354 independent: BTreeSet<PoolTransactionRef<T>>,
355 _invalid: HashSet<TxHash>,
356}
357
358impl<T> TransactionsIterator<T> {
359 fn independent_or_awaiting(&mut self, satisfied: usize, tx_ref: PoolTransactionRef<T>) {
362 if satisfied >= tx_ref.transaction.requires.len() {
363 self.independent.insert(tx_ref);
365 } else {
366 self.awaiting.insert(tx_ref.transaction.hash(), (satisfied, tx_ref));
368 }
369 }
370}
371
372impl<T> Iterator for TransactionsIterator<T> {
373 type Item = Arc<PoolTransaction<T>>;
374
375 fn next(&mut self) -> Option<Self::Item> {
376 loop {
377 let best = self.independent.iter().next_back()?.clone();
378 let best = self.independent.take(&best)?;
379 let hash = best.transaction.hash();
380
381 let ready =
382 if let Some(ready) = self.all.get(&hash).cloned() { ready } else { continue };
383
384 for hash in &ready.unlocks {
386 let res = if let Some((mut satisfied, tx_ref)) = self.awaiting.remove(hash) {
388 satisfied += 1;
389 Some((satisfied, tx_ref))
390 } else {
392 self.all
393 .get(hash)
394 .map(|next| (next.requires_offset + 1, next.transaction.clone()))
395 };
396 if let Some((satisfied, tx_ref)) = res {
397 self.independent_or_awaiting(satisfied, tx_ref)
398 }
399 }
400
401 return Some(best.transaction);
402 }
403 }
404}
405
406#[derive(Clone, Debug)]
408pub struct ReadyTransactions<T> {
409 id: u64,
413 provided_markers: HashMap<TxMarker, TxHash>,
415 ready_tx: Arc<RwLock<HashMap<TxHash, ReadyTransaction<T>>>>,
417 independent_transactions: BTreeSet<PoolTransactionRef<T>>,
420}
421
422impl<T> Default for ReadyTransactions<T> {
423 fn default() -> Self {
424 Self {
425 id: 0,
426 provided_markers: Default::default(),
427 ready_tx: Default::default(),
428 independent_transactions: Default::default(),
429 }
430 }
431}
432
433impl<T> ReadyTransactions<T> {
434 pub fn get_transactions(&self) -> TransactionsIterator<T> {
436 TransactionsIterator {
437 all: self.ready_tx.read().clone(),
438 independent: self.independent_transactions.clone(),
439 awaiting: Default::default(),
440 _invalid: Default::default(),
441 }
442 }
443
444 pub fn clear(&mut self) {
446 self.provided_markers.clear();
447 self.ready_tx.write().clear();
448 self.independent_transactions.clear();
449 }
450
451 pub fn contains(&self, hash: &TxHash) -> bool {
453 self.ready_tx.read().contains_key(hash)
454 }
455
456 pub fn len(&self) -> usize {
458 self.ready_tx.read().len()
459 }
460
461 pub fn is_empty(&self) -> bool {
463 self.ready_tx.read().is_empty()
464 }
465
466 pub fn get(&self, hash: &TxHash) -> Option<ReadyTransaction<T>> {
468 self.ready_tx.read().get(hash).cloned()
469 }
470
471 pub const fn provided_markers(&self) -> &HashMap<TxMarker, TxHash> {
472 &self.provided_markers
473 }
474
475 const fn next_id(&mut self) -> u64 {
476 let id = self.id;
477 self.id = self.id.wrapping_add(1);
478 id
479 }
480
481 pub fn clear_transactions(&mut self, tx_hashes: &[TxHash]) -> Vec<Arc<PoolTransaction<T>>> {
484 self.remove_with_markers(tx_hashes.to_vec(), None)
485 }
486
487 pub fn prune_tags(&mut self, marker: TxMarker) -> Vec<Arc<PoolTransaction<T>>> {
492 let mut removed_tx = vec![];
493
494 let mut remove = vec![marker];
496
497 while let Some(marker) = remove.pop() {
498 let res = self
499 .provided_markers
500 .remove(&marker)
501 .and_then(|hash| self.ready_tx.write().remove(&hash));
502
503 if let Some(tx) = res {
504 let unlocks = tx.unlocks;
505 self.independent_transactions.remove(&tx.transaction);
506 let tx = tx.transaction.transaction;
507
508 {
510 let hash = tx.hash();
511 let mut ready = self.ready_tx.write();
512
513 let mut previous_markers = |marker| -> Option<Vec<TxMarker>> {
514 let prev_hash = self.provided_markers.get(marker)?;
515 let tx2 = ready.get_mut(prev_hash)?;
516 if let Some(idx) = tx2.unlocks.iter().position(|i| i == &hash) {
518 tx2.unlocks.swap_remove(idx);
519 }
520 tx2.unlocks.is_empty().then(|| tx2.transaction.transaction.provides.clone())
521 };
522
523 for marker in &tx.requires {
525 if let Some(mut tags_to_remove) = previous_markers(marker) {
526 remove.append(&mut tags_to_remove);
527 }
528 }
529 }
530
531 for hash in unlocks {
533 if let Some(tx) = self.ready_tx.write().get_mut(&hash) {
534 tx.requires_offset += 1;
535 if tx.requires_offset == tx.transaction.transaction.requires.len() {
536 self.independent_transactions.insert(tx.transaction.clone());
537 }
538 }
539 }
540 let current_marker = ▮
542 for marker in &tx.provides {
543 let removed = self.provided_markers.remove(marker);
544 assert_eq!(
545 removed,
546 if current_marker == marker { None } else { Some(tx.hash()) },
547 "The pool contains exactly one transaction providing given tag; the removed transaction
548 claims to provide that tag, so it has to be mapped to it's hash; qed"
549 );
550 }
551 removed_tx.push(tx);
552 }
553 }
554
555 removed_tx
556 }
557
558 pub fn remove_with_markers(
561 &mut self,
562 mut tx_hashes: Vec<TxHash>,
563 marker_filter: Option<HashSet<TxMarker>>,
564 ) -> Vec<Arc<PoolTransaction<T>>> {
565 let mut removed = Vec::new();
566 let mut ready = self.ready_tx.write();
567
568 while let Some(hash) = tx_hashes.pop() {
569 if let Some(mut tx) = ready.remove(&hash) {
570 let invalidated = tx.transaction.transaction.provides.iter().filter(|mark| {
571 marker_filter.as_ref().map(|filter| !filter.contains(&**mark)).unwrap_or(true)
572 });
573
574 let mut removed_some_marks = false;
575 for mark in invalidated {
577 removed_some_marks = true;
578 self.provided_markers.remove(mark);
579 }
580
581 for mark in &tx.transaction.transaction.requires {
583 if let Some(provider_hash) = self.provided_markers.get(mark)
584 && let Some(provider_tx) = ready.get_mut(provider_hash)
585 && let Some(idx) = provider_tx.unlocks.iter().position(|i| i == &hash)
586 {
587 provider_tx.unlocks.swap_remove(idx);
588 }
589 }
590
591 self.independent_transactions.remove(&tx.transaction);
593
594 if removed_some_marks {
595 tx_hashes.append(&mut tx.unlocks);
597 }
598
599 removed.push(tx.transaction.transaction);
601 }
602 }
603
604 removed
605 }
606}
607
608impl<T: Transaction> ReadyTransactions<T> {
609 pub fn add_transaction(
616 &mut self,
617 tx: PendingPoolTransaction<T>,
618 ) -> Result<Vec<Arc<PoolTransaction<T>>>, PoolError> {
619 assert!(tx.is_ready(), "transaction must be ready",);
620 assert!(
621 !self.ready_tx.read().contains_key(&tx.transaction.hash()),
622 "transaction already included"
623 );
624
625 let (replaced_tx, unlocks) = self.replaced_transactions(&tx.transaction)?;
626
627 let id = self.next_id();
628 let hash = tx.transaction.hash();
629
630 let mut independent = true;
631 let mut requires_offset = 0;
632 let mut ready = self.ready_tx.write();
633 for mark in &tx.transaction.requires {
635 if let Some(other) = self.provided_markers.get(mark) {
637 let tx = ready.get_mut(other).expect("hash included;");
638 tx.unlocks.push(hash);
639 independent = false;
641 } else {
642 requires_offset += 1;
643 }
644 }
645
646 for mark in tx.transaction.provides.iter().cloned() {
648 self.provided_markers.insert(mark, hash);
649 }
650
651 let transaction = PoolTransactionRef { id, transaction: tx.transaction };
652
653 if independent {
655 self.independent_transactions.insert(transaction.clone());
656 }
657
658 ready.insert(hash, ReadyTransaction { transaction, unlocks, requires_offset });
660
661 Ok(replaced_tx)
662 }
663
664 fn replaced_transactions(
666 &mut self,
667 tx: &PoolTransaction<T>,
668 ) -> Result<ReplacedTransactions<T>, PoolError> {
669 let remove_hashes: HashSet<_> =
671 tx.provides.iter().filter_map(|mark| self.provided_markers.get(mark)).collect();
672
673 if remove_hashes.is_empty() {
675 return Ok((Vec::new(), Vec::new()));
676 }
677
678 let mut unlocked_tx = Vec::new();
680 {
681 let ready = self.ready_tx.read();
684 for to_remove in remove_hashes.iter().filter_map(|hash| ready.get(*hash)) {
685 if to_remove.provides() == tx.provides {
688 if tx.pending_transaction.transaction.max_fee_per_gas()
690 <= to_remove.max_fee_per_gas()
691 {
692 warn!(target: "txpool", "ready replacement transaction underpriced [{:?}]", tx.hash());
693 return Err(PoolError::ReplacementUnderpriced(tx.hash()));
694 }
695 trace!(target: "txpool", "replacing ready transaction [{:?}] with higher gas price [{:?}]", to_remove.transaction.transaction.hash(), tx.hash());
696 }
697
698 unlocked_tx.extend(to_remove.unlocks.iter().copied())
699 }
700 }
701
702 let remove_hashes = remove_hashes.into_iter().copied().collect::<Vec<_>>();
703
704 let new_provides = tx.provides.iter().cloned().collect::<HashSet<_>>();
705 let removed_tx = self.remove_with_markers(remove_hashes, Some(new_provides));
706
707 Ok((removed_tx, unlocked_tx))
708 }
709}
710
711#[derive(Debug)]
713pub struct PoolTransactionRef<T> {
714 pub transaction: Arc<PoolTransaction<T>>,
716 pub id: u64,
718}
719
720impl<T> Clone for PoolTransactionRef<T> {
721 fn clone(&self) -> Self {
722 Self { transaction: Arc::clone(&self.transaction), id: self.id }
723 }
724}
725
726impl<T> Eq for PoolTransactionRef<T> {}
727
728impl<T> PartialEq<Self> for PoolTransactionRef<T> {
729 fn eq(&self, other: &Self) -> bool {
730 self.cmp(other) == Ordering::Equal
731 }
732}
733
734impl<T> PartialOrd<Self> for PoolTransactionRef<T> {
735 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
736 Some(self.cmp(other))
737 }
738}
739
740impl<T> Ord for PoolTransactionRef<T> {
741 fn cmp(&self, other: &Self) -> Ordering {
742 self.transaction
743 .priority
744 .cmp(&other.transaction.priority)
745 .then_with(|| other.id.cmp(&self.id))
746 }
747}
748
749#[derive(Debug)]
750pub struct ReadyTransaction<T> {
751 pub transaction: PoolTransactionRef<T>,
753 pub unlocks: Vec<TxHash>,
755 pub requires_offset: usize,
757}
758
759impl<T> Clone for ReadyTransaction<T> {
760 fn clone(&self) -> Self {
761 Self {
762 transaction: self.transaction.clone(),
763 unlocks: self.unlocks.clone(),
764 requires_offset: self.requires_offset,
765 }
766 }
767}
768
769impl<T> ReadyTransaction<T> {
770 pub fn provides(&self) -> &[TxMarker] {
771 &self.transaction.transaction.provides
772 }
773}
774
775impl<T: Transaction> ReadyTransaction<T> {
776 pub fn max_fee_per_gas(&self) -> u128 {
777 self.transaction.transaction.max_fee_per_gas()
778 }
779}
780
781pub fn to_marker(nonce: u64, from: Address) -> TxMarker {
783 let mut data = [0u8; 28];
784 data[..8].copy_from_slice(&nonce.to_le_bytes()[..]);
785 data[8..].copy_from_slice(&from.0[..]);
786 data.to_vec()
787}
788
789#[cfg(test)]
790mod tests {
791 use super::*;
792
793 #[test]
794 fn can_id_txs() {
795 let addr = Address::random();
796 assert_eq!(to_marker(1, addr), to_marker(1, addr));
797 assert_ne!(to_marker(2, addr), to_marker(1, addr));
798 }
799}