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