cuprate_txpool/service/
interface.rs

1//! Tx-pool [`service`](super) interface.
2//!
3//! This module contains `cuprate_txpool`'s [`tower::Service`] request and response enums.
4use std::collections::{HashMap, HashSet};
5
6use cuprate_types::TransactionVerificationData;
7
8use crate::{
9    tx::TxEntry,
10    types::{KeyImage, TransactionBlobHash, TransactionHash},
11};
12
13//---------------------------------------------------------------------------------------------------- TxpoolReadRequest
14/// The transaction pool [`tower::Service`] read request type.
15#[derive(Clone)]
16pub enum TxpoolReadRequest {
17    /// Get the blob (raw bytes) of a transaction with the given hash.
18    TxBlob(TransactionHash),
19
20    /// Get the [`TransactionVerificationData`] of a transaction in the tx pool.
21    TxVerificationData(TransactionHash),
22
23    /// Filter (remove) all **known** transactions from the set.
24    ///
25    /// The hash is **not** the transaction hash, it is the hash of the serialized tx-blob.
26    FilterKnownTxBlobHashes(HashSet<TransactionBlobHash>),
27
28    /// Get some transactions for an incoming block.
29    TxsForBlock(Vec<TransactionHash>),
30
31    /// Get information on all transactions in the pool.
32    Backlog,
33
34    /// Get the number of transactions in the pool.
35    Size {
36        /// If this is [`true`], the size returned will
37        /// include private transactions in the pool.
38        include_sensitive_txs: bool,
39    },
40}
41
42//---------------------------------------------------------------------------------------------------- TxpoolReadResponse
43/// The transaction pool [`tower::Service`] read response type.
44#[expect(clippy::large_enum_variant)]
45pub enum TxpoolReadResponse {
46    /// The response for [`TxpoolReadRequest::TxBlob`].
47    TxBlob { tx_blob: Vec<u8>, state_stem: bool },
48
49    /// The response for [`TxpoolReadRequest::TxVerificationData`].
50    TxVerificationData(TransactionVerificationData),
51
52    /// The response for [`TxpoolReadRequest::FilterKnownTxBlobHashes`].
53    FilterKnownTxBlobHashes {
54        /// The blob hashes that are unknown.
55        unknown_blob_hashes: HashSet<TransactionBlobHash>,
56        /// The tx hashes of the blob hashes that were known but were in the stem pool.
57        stem_pool_hashes: Vec<TransactionHash>,
58    },
59
60    /// The response for [`TxpoolReadRequest::TxsForBlock`].
61    TxsForBlock {
62        /// The txs we had in the txpool.
63        txs: HashMap<[u8; 32], TransactionVerificationData>,
64        /// The indexes of the missing txs.
65        missing: Vec<usize>,
66    },
67
68    /// Response to [`TxpoolReadRequest::Backlog`].
69    ///
70    /// The inner [`Vec`] contains information on all
71    /// the transactions currently in the pool.
72    Backlog(Vec<TxEntry>),
73
74    /// Response to [`TxpoolReadRequest::Size`].
75    ///
76    /// The inner value is the amount of
77    /// transactions currently in the pool.
78    Size(usize),
79}
80
81//---------------------------------------------------------------------------------------------------- TxpoolWriteRequest
82/// The transaction pool [`tower::Service`] write request type.
83#[derive(Clone)]
84pub enum TxpoolWriteRequest {
85    /// Add a transaction to the pool.
86    ///
87    /// Returns [`TxpoolWriteResponse::AddTransaction`].
88    AddTransaction {
89        /// The tx to add.
90        tx: Box<TransactionVerificationData>,
91        /// A [`bool`] denoting the routing state of this tx.
92        ///
93        /// [`true`] if this tx is in the stem state.
94        state_stem: bool,
95    },
96
97    /// Remove a transaction with the given hash from the pool.
98    RemoveTransaction(TransactionHash),
99
100    /// Promote a transaction from the stem pool to the fluff pool.
101    /// If the tx is already in the fluff pool this does nothing.
102    Promote(TransactionHash),
103
104    /// Tell the tx-pool about a new block.
105    NewBlock {
106        /// The spent key images in the new block.
107        spent_key_images: Vec<KeyImage>,
108    },
109}
110
111//---------------------------------------------------------------------------------------------------- TxpoolWriteResponse
112/// The transaction pool [`tower::Service`] write response type.
113#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
114pub enum TxpoolWriteResponse {
115    /// Response to:
116    /// - [`TxpoolWriteRequest::RemoveTransaction`]
117    /// - [`TxpoolWriteRequest::Promote`]
118    /// - [`TxpoolWriteRequest::NewBlock`]
119    Ok,
120
121    /// Response to [`TxpoolWriteRequest::AddTransaction`].
122    ///
123    /// If the inner value is [`Some`] the tx was not added to the pool as it double spends a tx with the given hash.
124    AddTransaction(Option<TransactionHash>),
125}