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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use crate::collections::{HashMap, HashSet, VecDeque};
use crate::tx_graph::{TxAncestors, TxDescendants};
use crate::{Anchor, ChainOracle, TxGraph};
use alloc::boxed::Box;
use alloc::collections::BTreeSet;
use alloc::sync::Arc;
use alloc::vec::Vec;
use bdk_core::BlockId;
use bitcoin::{Transaction, Txid};
type CanonicalMap<A> = HashMap<Txid, (Arc<Transaction>, CanonicalReason<A>)>;
type NotCanonicalSet = HashSet<Txid>;
/// Modifies the canonicalization algorithm.
#[derive(Debug, Default, Clone)]
pub struct CanonicalizationParams {
/// Transactions that will supercede all other transactions.
///
/// In case of conflicting transactions within `assume_canonical`, transactions that appear
/// later in the list (have higher index) have precedence.
pub assume_canonical: Vec<Txid>,
}
/// Iterates over canonical txs.
pub struct CanonicalIter<'g, A, C> {
tx_graph: &'g TxGraph<A>,
chain: &'g C,
chain_tip: BlockId,
unprocessed_assumed_txs: Box<dyn Iterator<Item = (Txid, Arc<Transaction>)> + 'g>,
unprocessed_anchored_txs:
Box<dyn Iterator<Item = (Txid, Arc<Transaction>, &'g BTreeSet<A>)> + 'g>,
unprocessed_seen_txs: Box<dyn Iterator<Item = (Txid, Arc<Transaction>, u64)> + 'g>,
unprocessed_leftover_txs: VecDeque<(Txid, Arc<Transaction>, u32)>,
canonical: CanonicalMap<A>,
not_canonical: NotCanonicalSet,
queue: VecDeque<Txid>,
}
impl<'g, A: Anchor, C: ChainOracle> CanonicalIter<'g, A, C> {
/// Constructs [`CanonicalIter`].
pub fn new(
tx_graph: &'g TxGraph<A>,
chain: &'g C,
chain_tip: BlockId,
params: CanonicalizationParams,
) -> Self {
let anchors = tx_graph.all_anchors();
let unprocessed_assumed_txs = Box::new(
params
.assume_canonical
.into_iter()
.rev()
.filter_map(|txid| Some((txid, tx_graph.get_tx(txid)?))),
);
let unprocessed_anchored_txs = Box::new(
tx_graph
.txids_by_descending_anchor_height()
.filter_map(|(_, txid)| Some((txid, tx_graph.get_tx(txid)?, anchors.get(&txid)?))),
);
let unprocessed_seen_txs = Box::new(
tx_graph
.txids_by_descending_last_seen()
.filter_map(|(last_seen, txid)| Some((txid, tx_graph.get_tx(txid)?, last_seen))),
);
Self {
tx_graph,
chain,
chain_tip,
unprocessed_assumed_txs,
unprocessed_anchored_txs,
unprocessed_seen_txs,
unprocessed_leftover_txs: VecDeque::new(),
canonical: HashMap::new(),
not_canonical: HashSet::new(),
queue: VecDeque::new(),
}
}
/// Whether this transaction is already canonicalized.
fn is_canonicalized(&self, txid: Txid) -> bool {
self.canonical.contains_key(&txid) || self.not_canonical.contains(&txid)
}
/// Mark transaction as canonical if it is anchored in the best chain.
fn scan_anchors(
&mut self,
txid: Txid,
tx: Arc<Transaction>,
anchors: &BTreeSet<A>,
) -> Result<(), C::Error> {
for anchor in anchors {
let in_chain_opt = self
.chain
.is_block_in_chain(anchor.anchor_block(), self.chain_tip)?;
if in_chain_opt == Some(true) {
self.mark_canonical(txid, tx, CanonicalReason::from_anchor(anchor.clone()));
return Ok(());
}
}
// cannot determine
self.unprocessed_leftover_txs.push_back((
txid,
tx,
anchors
.iter()
.last()
.expect(
"tx taken from `unprocessed_txs_with_anchors` so it must atleast have an anchor",
)
.confirmation_height_upper_bound(),
));
Ok(())
}
/// Marks `tx` and it's ancestors as canonical and mark all conflicts of these as
/// `not_canonical`.
///
/// The exception is when it is discovered that `tx` double spends itself (i.e. two of it's
/// inputs conflict with each other), then no changes will be made.
///
/// The logic works by having two loops where one is nested in another.
/// * The outer loop iterates through ancestors of `tx` (including `tx`). We can transitively
/// assume that all ancestors of `tx` are also canonical.
/// * The inner loop loops through conflicts of ancestors of `tx`. Any descendants of conflicts
/// are also conflicts and are transitively considered non-canonical.
///
/// If the inner loop ends up marking `tx` as non-canonical, then we know that it double spends
/// itself.
fn mark_canonical(&mut self, txid: Txid, tx: Arc<Transaction>, reason: CanonicalReason<A>) {
let starting_txid = txid;
let mut is_starting_tx = true;
// We keep track of changes made so far so that we can undo it later in case we detect that
// `tx` double spends itself.
let mut detected_self_double_spend = false;
let mut undo_not_canonical = Vec::<Txid>::new();
// `staged_queue` doubles as the `undo_canonical` data.
let staged_queue = TxAncestors::new_include_root(
self.tx_graph,
tx,
|_: usize, tx: Arc<Transaction>| -> Option<Txid> {
let this_txid = tx.compute_txid();
let this_reason = if is_starting_tx {
is_starting_tx = false;
reason.clone()
} else {
reason.to_transitive(starting_txid)
};
use crate::collections::hash_map::Entry;
let canonical_entry = match self.canonical.entry(this_txid) {
// Already visited tx before, exit early.
Entry::Occupied(_) => return None,
Entry::Vacant(entry) => entry,
};
// Any conflicts with a canonical tx can be added to `not_canonical`. Descendants
// of `not_canonical` txs can also be added to `not_canonical`.
for (_, conflict_txid) in self.tx_graph.direct_conflicts(&tx) {
TxDescendants::new_include_root(
self.tx_graph,
conflict_txid,
|_: usize, txid: Txid| -> Option<()> {
if self.not_canonical.insert(txid) {
undo_not_canonical.push(txid);
Some(())
} else {
None
}
},
)
.run_until_finished()
}
if self.not_canonical.contains(&this_txid) {
// Early exit if self-double-spend is detected.
detected_self_double_spend = true;
return None;
}
canonical_entry.insert((tx, this_reason));
Some(this_txid)
},
)
.collect::<Vec<Txid>>();
if detected_self_double_spend {
for txid in staged_queue {
self.canonical.remove(&txid);
}
for txid in undo_not_canonical {
self.not_canonical.remove(&txid);
}
} else {
self.queue.extend(staged_queue);
}
}
}
impl<A: Anchor, C: ChainOracle> Iterator for CanonicalIter<'_, A, C> {
type Item = Result<(Txid, Arc<Transaction>, CanonicalReason<A>), C::Error>;
fn next(&mut self) -> Option<Self::Item> {
loop {
if let Some(txid) = self.queue.pop_front() {
let (tx, reason) = self
.canonical
.get(&txid)
.cloned()
.expect("reason must exist");
return Some(Ok((txid, tx, reason)));
}
if let Some((txid, tx)) = self.unprocessed_assumed_txs.next() {
if !self.is_canonicalized(txid) {
self.mark_canonical(txid, tx, CanonicalReason::assumed());
}
}
if let Some((txid, tx, anchors)) = self.unprocessed_anchored_txs.next() {
if !self.is_canonicalized(txid) {
if let Err(err) = self.scan_anchors(txid, tx, anchors) {
return Some(Err(err));
}
}
continue;
}
if let Some((txid, tx, last_seen)) = self.unprocessed_seen_txs.next() {
debug_assert!(
!tx.is_coinbase(),
"Coinbase txs must not have `last_seen` (in mempool) value"
);
if !self.is_canonicalized(txid) {
let observed_in = ObservedIn::Mempool(last_seen);
self.mark_canonical(txid, tx, CanonicalReason::from_observed_in(observed_in));
}
continue;
}
if let Some((txid, tx, height)) = self.unprocessed_leftover_txs.pop_front() {
if !self.is_canonicalized(txid) && !tx.is_coinbase() {
let observed_in = ObservedIn::Block(height);
self.mark_canonical(txid, tx, CanonicalReason::from_observed_in(observed_in));
}
continue;
}
return None;
}
}
}
/// Represents when and where a transaction was last observed in.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ObservedIn {
/// The transaction was last observed in a block of height.
Block(u32),
/// The transaction was last observed in the mempool at the given unix timestamp.
Mempool(u64),
}
/// The reason why a transaction is canonical.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CanonicalReason<A> {
/// This transaction is explicitly assumed to be canonical by the caller, superceding all other
/// canonicalization rules.
Assumed {
/// Whether it is a descendant that is assumed to be canonical.
descendant: Option<Txid>,
},
/// This transaction is anchored in the best chain by `A`, and therefore canonical.
Anchor {
/// The anchor that anchored the transaction in the chain.
anchor: A,
/// Whether the anchor is of the transaction's descendant.
descendant: Option<Txid>,
},
/// This transaction does not conflict with any other transaction with a more recent
/// [`ObservedIn`] value or one that is anchored in the best chain.
ObservedIn {
/// The [`ObservedIn`] value of the transaction.
observed_in: ObservedIn,
/// Whether the [`ObservedIn`] value is of the transaction's descendant.
descendant: Option<Txid>,
},
}
impl<A: Clone> CanonicalReason<A> {
/// Constructs a [`CanonicalReason`] for a transaction that is assumed to supercede all other
/// transactions.
pub fn assumed() -> Self {
Self::Assumed { descendant: None }
}
/// Constructs a [`CanonicalReason`] from an `anchor`.
pub fn from_anchor(anchor: A) -> Self {
Self::Anchor {
anchor,
descendant: None,
}
}
/// Constructs a [`CanonicalReason`] from an `observed_in` value.
pub fn from_observed_in(observed_in: ObservedIn) -> Self {
Self::ObservedIn {
observed_in,
descendant: None,
}
}
/// Contruct a new [`CanonicalReason`] from the original which is transitive to `descendant`.
///
/// This signals that either the [`ObservedIn`] or [`Anchor`] value belongs to the transaction's
/// descendant, but is transitively relevant.
pub fn to_transitive(&self, descendant: Txid) -> Self {
match self {
CanonicalReason::Assumed { .. } => Self::Assumed {
descendant: Some(descendant),
},
CanonicalReason::Anchor { anchor, .. } => Self::Anchor {
anchor: anchor.clone(),
descendant: Some(descendant),
},
CanonicalReason::ObservedIn { observed_in, .. } => Self::ObservedIn {
observed_in: *observed_in,
descendant: Some(descendant),
},
}
}
/// This signals that either the [`ObservedIn`] or [`Anchor`] value belongs to the transaction's
/// descendant.
pub fn descendant(&self) -> &Option<Txid> {
match self {
CanonicalReason::Assumed { descendant, .. } => descendant,
CanonicalReason::Anchor { descendant, .. } => descendant,
CanonicalReason::ObservedIn { descendant, .. } => descendant,
}
}
}