zebra_node_services/mempool/mempool_change.rs
1//! Defines the [`MempoolChange`] and [`MempoolChangeKind`] types used by the mempool change broadcast channel.
2
3use std::collections::HashSet;
4
5use tokio::sync::broadcast;
6use zebra_chain::transaction::UnminedTxId;
7
8/// A newtype around [`broadcast::Sender<MempoolChange>`] used to
9/// subscribe to the channel without an active receiver.
10#[derive(Clone, Debug)]
11pub struct MempoolTxSubscriber(broadcast::Sender<MempoolChange>);
12
13impl MempoolTxSubscriber {
14 /// Creates a new [`MempoolTxSubscriber`].
15 pub fn new(sender: broadcast::Sender<MempoolChange>) -> Self {
16 Self(sender)
17 }
18
19 /// Subscribes to the channel, returning a [`broadcast::Receiver`].
20 pub fn subscribe(&self) -> broadcast::Receiver<MempoolChange> {
21 self.0.subscribe()
22 }
23}
24
25/// Represents a kind of change in the mempool's verified set of transactions
26#[derive(Debug, Copy, Clone, Eq, PartialEq)]
27pub enum MempoolChangeKind {
28 /// Transactions were added to the mempool.
29 Added,
30 /// Transactions were invalidated or could not be verified and were rejected from the mempool.
31 Invalidated,
32 /// Transactions were mined onto the best chain and removed from the mempool.
33 Mined,
34}
35
36/// Represents a change in the mempool's verified set of transactions
37#[derive(Debug, Clone, Eq, PartialEq)]
38pub struct MempoolChange {
39 /// The kind of change that occurred in the mempool.
40 pub kind: MempoolChangeKind,
41 /// The set of [`UnminedTxId`]s of transactions that were affected by the change.
42 pub tx_ids: HashSet<UnminedTxId>,
43}
44
45impl MempoolChange {
46 /// Creates a new [`MempoolChange`] with the specified kind and transaction IDs.
47 pub fn new(kind: MempoolChangeKind, tx_ids: HashSet<UnminedTxId>) -> Self {
48 Self { kind, tx_ids }
49 }
50
51 /// Returns the kind of change that occurred in the mempool.
52 pub fn kind(&self) -> MempoolChangeKind {
53 self.kind
54 }
55
56 /// Returns true if the kind of change that occurred in the mempool is [`MempoolChangeKind::Added`].
57 pub fn is_added(&self) -> bool {
58 self.kind == MempoolChangeKind::Added
59 }
60
61 /// Consumes self and returns the set of [`UnminedTxId`]s of transactions that were affected by the change.
62 pub fn into_tx_ids(self) -> HashSet<UnminedTxId> {
63 self.tx_ids
64 }
65
66 /// Returns a reference to the set of [`UnminedTxId`]s of transactions that were affected by the change.
67 pub fn tx_ids(&self) -> &HashSet<UnminedTxId> {
68 &self.tx_ids
69 }
70
71 /// Creates a new [`MempoolChange`] indicating that transactions were added to the mempool.
72 pub fn added(tx_ids: HashSet<UnminedTxId>) -> Self {
73 Self::new(MempoolChangeKind::Added, tx_ids)
74 }
75
76 /// Creates a new [`MempoolChange`] indicating that transactions were invalidated or rejected from the mempool.
77 pub fn invalidated(tx_ids: HashSet<UnminedTxId>) -> Self {
78 Self::new(MempoolChangeKind::Invalidated, tx_ids)
79 }
80
81 /// Creates a new [`MempoolChange`] indicating that transactions were mined onto the best chain and
82 /// removed from the mempool.
83 pub fn mined(tx_ids: HashSet<UnminedTxId>) -> Self {
84 Self::new(MempoolChangeKind::Mined, tx_ids)
85 }
86}