|
| 1 | +//! Unbroadcasted transaction queue. |
| 2 | +
|
| 3 | +use alloc::vec::Vec; |
| 4 | +use chain::tx_graph; |
| 5 | +use chain::Anchor; |
| 6 | +use chain::TxGraph; |
| 7 | + |
| 8 | +use crate::collections::HashSet; |
| 9 | +use crate::collections::VecDeque; |
| 10 | + |
| 11 | +use bitcoin::Txid; |
| 12 | +use chain::Merge; |
| 13 | + |
| 14 | +/// An ordered unbroadcasted list. |
| 15 | +/// |
| 16 | +/// It is ordered in case of RBF txs. |
| 17 | +#[derive(Debug, Clone, Default)] |
| 18 | +pub struct BroadcastQueue { |
| 19 | + queue: VecDeque<Txid>, |
| 20 | + |
| 21 | + /// Enforces that we do not have duplicates in `queue`. |
| 22 | + dedup: HashSet<Txid>, |
| 23 | +} |
| 24 | + |
| 25 | +/// Represents a single mutation to [`BroadcastQueue`]. |
| 26 | +#[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)] |
| 27 | +pub enum Mutation { |
| 28 | + /// A push to the back of the queue. |
| 29 | + Push(Txid), |
| 30 | + /// |
| 31 | + Remove(Txid), |
| 32 | +} |
| 33 | + |
| 34 | +/// A list of mutations made to [`BroadcastQueue`]. |
| 35 | +#[must_use] |
| 36 | +#[derive(Debug, Clone, Default, PartialEq, serde::Deserialize, serde::Serialize)] |
| 37 | +pub struct ChangeSet { |
| 38 | + /// Mutations. |
| 39 | + pub mutations: Vec<Mutation>, |
| 40 | +} |
| 41 | + |
| 42 | +impl Merge for ChangeSet { |
| 43 | + fn merge(&mut self, other: Self) { |
| 44 | + self.mutations.extend(other.mutations); |
| 45 | + } |
| 46 | + |
| 47 | + fn is_empty(&self) -> bool { |
| 48 | + self.mutations.is_empty() |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl BroadcastQueue { |
| 53 | + /// Construct [`Unbroadcasted`] from the given `changeset`. |
| 54 | + pub fn from_changeset(changeset: ChangeSet) -> Self { |
| 55 | + let mut out = BroadcastQueue::default(); |
| 56 | + out.apply_changeset(changeset); |
| 57 | + out |
| 58 | + } |
| 59 | + |
| 60 | + /// Apply the given `changeset`. |
| 61 | + pub fn apply_changeset(&mut self, changeset: ChangeSet) { |
| 62 | + for mutation in changeset.mutations { |
| 63 | + match mutation { |
| 64 | + Mutation::Push(txid) => self._push(txid), |
| 65 | + Mutation::Remove(txid) => self._remove(txid), |
| 66 | + }; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// Whether the `txid` exists in the queue. |
| 71 | + pub fn contains(&self, txid: Txid) -> bool { |
| 72 | + self.dedup.contains(&txid) |
| 73 | + } |
| 74 | + |
| 75 | + /// Push a `txid` to the queue if it does not already exist. |
| 76 | + /// |
| 77 | + /// # Warning |
| 78 | + /// |
| 79 | + /// This does not get rid of conflicting transactions already in the queue. |
| 80 | + pub fn push(&mut self, txid: Txid) -> ChangeSet { |
| 81 | + let mut changeset = ChangeSet::default(); |
| 82 | + if self._push(txid) { |
| 83 | + changeset.mutations.push(Mutation::Push(txid)); |
| 84 | + } |
| 85 | + changeset |
| 86 | + } |
| 87 | + fn _push(&mut self, txid: Txid) -> bool { |
| 88 | + if self.dedup.insert(txid) { |
| 89 | + self.queue.push_back(txid); |
| 90 | + return true; |
| 91 | + } |
| 92 | + false |
| 93 | + } |
| 94 | + |
| 95 | + /// Push a `txid` to the broadcast queue (if it does not exist already) and displaces all |
| 96 | + /// coflicting txids in the queue. |
| 97 | + pub fn push_and_displace_conflicts<A>(&mut self, tx_graph: &TxGraph<A>, txid: Txid) -> ChangeSet |
| 98 | + where |
| 99 | + A: Anchor, |
| 100 | + { |
| 101 | + let mut changeset = ChangeSet::default(); |
| 102 | + |
| 103 | + let tx = match tx_graph.get_tx(txid) { |
| 104 | + Some(tx) => tx, |
| 105 | + None => { |
| 106 | + debug_assert!( |
| 107 | + !self.dedup.contains(&txid), |
| 108 | + "Cannot have txid in queue which has no corresponding tx in graph" |
| 109 | + ); |
| 110 | + return changeset; |
| 111 | + } |
| 112 | + }; |
| 113 | + |
| 114 | + if self._push(txid) { |
| 115 | + changeset.mutations.push(Mutation::Push(txid)); |
| 116 | + |
| 117 | + for txid in tx_graph.walk_conflicts(&tx, |_, conflict_txid| Some(conflict_txid)) { |
| 118 | + if self._remove(txid) { |
| 119 | + changeset.mutations.push(Mutation::Remove(txid)); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + changeset |
| 125 | + } |
| 126 | + |
| 127 | + /// Returns the next `txid` of the queue to broadcast which has no dependencies to other |
| 128 | + /// transactions in the queue. |
| 129 | + pub fn next_to_broadcast<A>(&self, tx_graph: &TxGraph<A>) -> Option<Txid> |
| 130 | + where |
| 131 | + A: Anchor, |
| 132 | + { |
| 133 | + self.queue.iter().copied().find(|&txid| { |
| 134 | + let tx = match tx_graph.get_tx(txid) { |
| 135 | + Some(tx) => tx, |
| 136 | + None => return false, |
| 137 | + }; |
| 138 | + if tx |
| 139 | + .input |
| 140 | + .iter() |
| 141 | + .any(|txin| self.dedup.contains(&txin.previous_output.txid)) |
| 142 | + { |
| 143 | + return false; |
| 144 | + } |
| 145 | + true |
| 146 | + }) |
| 147 | + } |
| 148 | + |
| 149 | + /// Returns unbroadcasted dependencies of the given `txid`. |
| 150 | + /// |
| 151 | + /// The returned `Vec` is in broadcast order. |
| 152 | + pub fn unbroadcasted_dependencies<A>(&self, tx_graph: &TxGraph<A>, txid: Txid) -> Vec<Txid> |
| 153 | + where |
| 154 | + A: Anchor, |
| 155 | + { |
| 156 | + let tx = match tx_graph.get_tx(txid) { |
| 157 | + Some(tx) => tx, |
| 158 | + None => return Vec::new(), |
| 159 | + }; |
| 160 | + let mut txs = tx_graph |
| 161 | + .walk_ancestors(tx, |_depth, ancestor_tx| { |
| 162 | + let ancestor_txid = ancestor_tx.compute_txid(); |
| 163 | + if self.dedup.contains(&ancestor_txid) { |
| 164 | + Some(ancestor_txid) |
| 165 | + } else { |
| 166 | + None |
| 167 | + } |
| 168 | + }) |
| 169 | + .collect::<Vec<_>>(); |
| 170 | + txs.reverse(); |
| 171 | + txs |
| 172 | + } |
| 173 | + |
| 174 | + /// Untracks and removes a transaction from the broadcast queue. |
| 175 | + /// |
| 176 | + /// Transactions are automatically removed from the queue upon successful broadcast, so calling |
| 177 | + /// this method directly is typically not required. |
| 178 | + pub fn remove(&mut self, txid: Txid) -> ChangeSet { |
| 179 | + let mut changeset = ChangeSet::default(); |
| 180 | + if self._remove(txid) { |
| 181 | + changeset.mutations.push(Mutation::Remove(txid)); |
| 182 | + } |
| 183 | + changeset |
| 184 | + } |
| 185 | + fn _remove(&mut self, txid: Txid) -> bool { |
| 186 | + if self.dedup.remove(&txid) { |
| 187 | + let i = (0..self.queue.len()) |
| 188 | + .zip(self.queue.iter().copied()) |
| 189 | + .find_map(|(i, queue_txid)| if queue_txid == txid { Some(i) } else { None }) |
| 190 | + .expect("must exist in queue to exist in `queue`"); |
| 191 | + let _removed = self.queue.remove(i); |
| 192 | + debug_assert_eq!(_removed, Some(txid)); |
| 193 | + return true; |
| 194 | + } |
| 195 | + false |
| 196 | + } |
| 197 | + |
| 198 | + /// Untracks and removes a transaction and it's descendants from the broadcast queue. |
| 199 | + pub fn remove_and_displace_dependants<A>( |
| 200 | + &mut self, |
| 201 | + tx_graph: &TxGraph<A>, |
| 202 | + txid: Txid, |
| 203 | + ) -> ChangeSet |
| 204 | + where |
| 205 | + A: Anchor, |
| 206 | + { |
| 207 | + let mut changeset = ChangeSet::default(); |
| 208 | + |
| 209 | + if self._remove(txid) { |
| 210 | + changeset.mutations.push(Mutation::Remove(txid)); |
| 211 | + for txid in tx_graph.walk_descendants(txid, |_depth, txid| Some(txid)) { |
| 212 | + if self._remove(txid) { |
| 213 | + changeset.mutations.push(Mutation::Remove(txid)); |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + changeset |
| 218 | + } |
| 219 | + |
| 220 | + /// Untrack transactions that are given anchors and/or mempool timestamps. |
| 221 | + pub fn filter_from_graph_changeset<A>( |
| 222 | + &mut self, |
| 223 | + graph_changeset: &tx_graph::ChangeSet<A>, |
| 224 | + ) -> ChangeSet { |
| 225 | + let mut changeset = ChangeSet::default(); |
| 226 | + let s_txids = graph_changeset.last_seen.keys().copied(); |
| 227 | + let a_txids = graph_changeset.anchors.iter().map(|(_, txid)| *txid); |
| 228 | + let e_txids = graph_changeset.last_evicted.keys().copied(); |
| 229 | + for txid in s_txids.chain(a_txids).chain(e_txids) { |
| 230 | + changeset.merge(self.remove(txid)); |
| 231 | + } |
| 232 | + changeset |
| 233 | + } |
| 234 | + |
| 235 | + /// Txids ordered by precedence. |
| 236 | + /// |
| 237 | + /// Transactions with greater precedence will appear later in this list. |
| 238 | + pub fn txids(&self) -> impl ExactSizeIterator<Item = Txid> + '_ { |
| 239 | + self.queue.iter().copied() |
| 240 | + } |
| 241 | + |
| 242 | + /// Initial changeset. |
| 243 | + pub fn initial_changeset(&self) -> ChangeSet { |
| 244 | + ChangeSet { |
| 245 | + mutations: self.queue.iter().copied().map(Mutation::Push).collect(), |
| 246 | + } |
| 247 | + } |
| 248 | +} |
0 commit comments