|
| 1 | +//! User facing wallet events. |
| 2 | +
|
| 3 | +use crate::collections::BTreeMap; |
| 4 | +use crate::wallet::ChainPosition::{Confirmed, Unconfirmed}; |
| 5 | +use crate::Wallet; |
| 6 | +use alloc::sync::Arc; |
| 7 | +use alloc::vec::Vec; |
| 8 | +use bitcoin::{Transaction, Txid}; |
| 9 | +use chain::{BlockId, ChainPosition, ConfirmationBlockTime}; |
| 10 | + |
| 11 | +/// Events representing changes to wallet transactions. |
| 12 | +/// |
| 13 | +/// Returned after calling |
| 14 | +/// [`Wallet::apply_update_events`](crate::wallet::Wallet::apply_update_events). |
| 15 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 16 | +#[non_exhaustive] |
| 17 | +pub enum WalletEvent { |
| 18 | + /// The latest chain tip known to the wallet changed. |
| 19 | + ChainTipChanged { |
| 20 | + /// Previous chain tip. |
| 21 | + old_tip: BlockId, |
| 22 | + /// New chain tip. |
| 23 | + new_tip: BlockId, |
| 24 | + }, |
| 25 | + /// A transaction is now confirmed. |
| 26 | + /// |
| 27 | + /// If the transaction was previously unconfirmed `old_block_time` will be `None`. |
| 28 | + /// |
| 29 | + /// If a confirmed transaction is now re-confirmed in a new block `old_block_time` will contain |
| 30 | + /// the block id and the time it was previously confirmed. This can happen after a chain |
| 31 | + /// reorg. |
| 32 | + TxConfirmed { |
| 33 | + /// Transaction id. |
| 34 | + txid: Txid, |
| 35 | + /// Transaction. |
| 36 | + tx: Arc<Transaction>, |
| 37 | + /// Confirmation block time. |
| 38 | + block_time: ConfirmationBlockTime, |
| 39 | + /// Old confirmation block and time if previously confirmed in a different block. |
| 40 | + old_block_time: Option<ConfirmationBlockTime>, |
| 41 | + }, |
| 42 | + /// A transaction is now unconfirmed. |
| 43 | + /// |
| 44 | + /// If the transaction is first seen in the mempool `old_block_time` will be `None`. |
| 45 | + /// |
| 46 | + /// If a previously confirmed transaction is now seen in the mempool `old_block_time` will |
| 47 | + /// contain the block id and the time it was previously confirmed. This can happen after a |
| 48 | + /// chain reorg. |
| 49 | + TxUnconfirmed { |
| 50 | + /// Transaction id. |
| 51 | + txid: Txid, |
| 52 | + /// Transaction. |
| 53 | + tx: Arc<Transaction>, |
| 54 | + /// Old confirmation block and time, if previously confirmed. |
| 55 | + old_block_time: Option<ConfirmationBlockTime>, |
| 56 | + }, |
| 57 | + /// An unconfirmed transaction was replaced. |
| 58 | + /// |
| 59 | + /// This can happen after an RBF is broadcast or if a third party double spends an input of |
| 60 | + /// a received payment transaction before it is confirmed. |
| 61 | + /// |
| 62 | + /// The conflicts field contains the txid and vin (in which it conflicts) of the conflicting |
| 63 | + /// transactions. |
| 64 | + TxReplaced { |
| 65 | + /// Transaction id. |
| 66 | + txid: Txid, |
| 67 | + /// Transaction. |
| 68 | + tx: Arc<Transaction>, |
| 69 | + /// Conflicting transaction ids. |
| 70 | + conflicts: Vec<(usize, Txid)>, |
| 71 | + }, |
| 72 | + /// Unconfirmed transaction dropped. |
| 73 | + /// |
| 74 | + /// The transaction was dropped from the local mempool. This is generally due to the fee rate |
| 75 | + /// being too low. The transaction can still reappear in the mempool in the future resulting in |
| 76 | + /// a [`WalletEvent::TxUnconfirmed`] event. |
| 77 | + TxDropped { |
| 78 | + /// Transaction id. |
| 79 | + txid: Txid, |
| 80 | + /// Transaction. |
| 81 | + tx: Arc<Transaction>, |
| 82 | + }, |
| 83 | +} |
| 84 | + |
| 85 | +/// Generate events by comparing the chain tip and wallet transactions before and after applying |
| 86 | +/// `wallet::Update` to `Wallet`. Any changes are added to the list of returned `WalletEvent`s. |
| 87 | +pub(crate) fn wallet_events( |
| 88 | + wallet: &mut Wallet, |
| 89 | + chain_tip1: BlockId, |
| 90 | + chain_tip2: BlockId, |
| 91 | + wallet_txs1: BTreeMap<Txid, (Arc<Transaction>, ChainPosition<ConfirmationBlockTime>)>, |
| 92 | + wallet_txs2: BTreeMap<Txid, (Arc<Transaction>, ChainPosition<ConfirmationBlockTime>)>, |
| 93 | +) -> Vec<WalletEvent> { |
| 94 | + let mut events: Vec<WalletEvent> = Vec::new(); |
| 95 | + |
| 96 | + // find chain tip change |
| 97 | + if chain_tip1 != chain_tip2 { |
| 98 | + events.push(WalletEvent::ChainTipChanged { |
| 99 | + old_tip: chain_tip1, |
| 100 | + new_tip: chain_tip2, |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + // find transaction canonical status changes |
| 105 | + wallet_txs2.iter().for_each(|(txid2, (tx2, pos2))| { |
| 106 | + if let Some((tx1, pos1)) = wallet_txs1.get(txid2) { |
| 107 | + debug_assert_eq!(tx1.compute_txid(), *txid2); |
| 108 | + match (pos1, pos2) { |
| 109 | + (Unconfirmed { .. }, Confirmed { anchor, .. }) => { |
| 110 | + events.push(WalletEvent::TxConfirmed { |
| 111 | + txid: *txid2, |
| 112 | + tx: tx2.clone(), |
| 113 | + block_time: *anchor, |
| 114 | + old_block_time: None, |
| 115 | + }); |
| 116 | + } |
| 117 | + (Confirmed { anchor, .. }, Unconfirmed { .. }) => { |
| 118 | + events.push(WalletEvent::TxUnconfirmed { |
| 119 | + txid: *txid2, |
| 120 | + tx: tx2.clone(), |
| 121 | + old_block_time: Some(*anchor), |
| 122 | + }); |
| 123 | + } |
| 124 | + ( |
| 125 | + Confirmed { |
| 126 | + anchor: anchor1, .. |
| 127 | + }, |
| 128 | + Confirmed { |
| 129 | + anchor: anchor2, .. |
| 130 | + }, |
| 131 | + ) => { |
| 132 | + if *anchor1 != *anchor2 { |
| 133 | + events.push(WalletEvent::TxConfirmed { |
| 134 | + txid: *txid2, |
| 135 | + tx: tx2.clone(), |
| 136 | + block_time: *anchor2, |
| 137 | + old_block_time: Some(*anchor1), |
| 138 | + }); |
| 139 | + } |
| 140 | + } |
| 141 | + (Unconfirmed { .. }, Unconfirmed { .. }) => { |
| 142 | + // do nothing if still unconfirmed |
| 143 | + } |
| 144 | + } |
| 145 | + } else { |
| 146 | + match pos2 { |
| 147 | + Confirmed { anchor, .. } => { |
| 148 | + events.push(WalletEvent::TxConfirmed { |
| 149 | + txid: *txid2, |
| 150 | + tx: tx2.clone(), |
| 151 | + block_time: *anchor, |
| 152 | + old_block_time: None, |
| 153 | + }); |
| 154 | + } |
| 155 | + Unconfirmed { .. } => { |
| 156 | + events.push(WalletEvent::TxUnconfirmed { |
| 157 | + txid: *txid2, |
| 158 | + tx: tx2.clone(), |
| 159 | + old_block_time: None, |
| 160 | + }); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + }); |
| 165 | + |
| 166 | + // find tx that are no longer canonical |
| 167 | + wallet_txs1.iter().for_each(|(txid1, (tx1, _))| { |
| 168 | + if !wallet_txs2.contains_key(txid1) { |
| 169 | + let conflicts = wallet.tx_graph().direct_conflicts(tx1).collect::<Vec<_>>(); |
| 170 | + if !conflicts.is_empty() { |
| 171 | + events.push(WalletEvent::TxReplaced { |
| 172 | + txid: *txid1, |
| 173 | + tx: tx1.clone(), |
| 174 | + conflicts, |
| 175 | + }); |
| 176 | + } else { |
| 177 | + events.push(WalletEvent::TxDropped { |
| 178 | + txid: *txid1, |
| 179 | + tx: tx1.clone(), |
| 180 | + }); |
| 181 | + } |
| 182 | + } |
| 183 | + }); |
| 184 | + |
| 185 | + events |
| 186 | +} |
0 commit comments