Skip to content

Commit 9e062a5

Browse files
committed
test: add suite of scenarios for txs topological order
1 parent 7c3ba87 commit 9e062a5

File tree

3 files changed

+442
-10
lines changed

3 files changed

+442
-10
lines changed

crates/chain/src/canonical_iter.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type NotCanonicalSet = HashSet<Txid>;
1414
/// Modifies the canonicalization algorithm.
1515
#[derive(Debug, Default, Clone)]
1616
pub struct CanonicalizationParams {
17-
/// Transactions that will supercede all other transactions.
17+
/// Transactions that will supersede all other transactions.
1818
///
1919
/// In case of conflicting transactions within `assume_canonical`, transactions that appear
2020
/// later in the list (have higher index) have precedence.
@@ -108,7 +108,7 @@ impl<'g, A: Anchor, C: ChainOracle> CanonicalIter<'g, A, C> {
108108
.iter()
109109
.last()
110110
.expect(
111-
"tx taken from `unprocessed_txs_with_anchors` so it must atleast have an anchor",
111+
"tx taken from `unprocessed_txs_with_anchors` so it must at least have an anchor",
112112
)
113113
.confirmation_height_upper_bound(),
114114
));
@@ -142,8 +142,9 @@ impl<'g, A: Anchor, C: ChainOracle> CanonicalIter<'g, A, C> {
142142
let staged_queue = TxAncestors::new_include_root(
143143
self.tx_graph,
144144
tx,
145-
|_: usize, tx: Arc<Transaction>| -> Option<Txid> {
145+
|depth: usize, tx: Arc<Transaction>| -> Option<Txid> {
146146
let this_txid = tx.compute_txid();
147+
println!("[canonical_iter] depth: {:?} ; txid: {:?}", depth, this_txid);
147148
let this_reason = if is_starting_tx {
148149
is_starting_tx = false;
149150
reason.clone()
@@ -158,6 +159,8 @@ impl<'g, A: Anchor, C: ChainOracle> CanonicalIter<'g, A, C> {
158159
Entry::Vacant(entry) => entry,
159160
};
160161

162+
// TODO: should we also check that it's already been visited by the `self.not_canonical` ?
163+
161164
// Any conflicts with a canonical tx can be added to `not_canonical`. Descendants
162165
// of `not_canonical` txs can also be added to `not_canonical`.
163166
for (_, conflict_txid) in self.tx_graph.direct_conflicts(&tx) {
@@ -166,8 +169,8 @@ impl<'g, A: Anchor, C: ChainOracle> CanonicalIter<'g, A, C> {
166169
conflict_txid,
167170
|_: usize, txid: Txid| -> Option<()> {
168171
if self.not_canonical.insert(txid) {
169-
undo_not_canonical.push(txid);
170-
Some(())
172+
undo_not_canonical.push(txid);
173+
Some(())
171174
} else {
172175
None
173176
}
@@ -181,7 +184,8 @@ impl<'g, A: Anchor, C: ChainOracle> CanonicalIter<'g, A, C> {
181184
detected_self_double_spend = true;
182185
return None;
183186
}
184-
canonical_entry.insert((tx, this_reason));
187+
canonical_entry.insert((tx.clone(), this_reason));
188+
println!("mark_canonical: {:?}", tx.clone().compute_txid());
185189
Some(this_txid)
186190
},
187191
)
@@ -196,6 +200,7 @@ impl<'g, A: Anchor, C: ChainOracle> CanonicalIter<'g, A, C> {
196200
}
197201
} else {
198202
self.queue.extend(staged_queue);
203+
println!("staged_queue: {:?}", self.queue);
199204
}
200205
}
201206
}
@@ -205,7 +210,7 @@ impl<A: Anchor, C: ChainOracle> Iterator for CanonicalIter<'_, A, C> {
205210

206211
fn next(&mut self) -> Option<Self::Item> {
207212
loop {
208-
if let Some(txid) = self.queue.pop_front() {
213+
if let Some(txid) = self.queue.pop_back() {
209214
let (tx, reason) = self
210215
.canonical
211216
.get(&txid)
@@ -222,6 +227,7 @@ impl<A: Anchor, C: ChainOracle> Iterator for CanonicalIter<'_, A, C> {
222227

223228
if let Some((txid, tx, anchors)) = self.unprocessed_anchored_txs.next() {
224229
if !self.is_canonicalized(txid) {
230+
println!("{:?}", txid);
225231
if let Err(err) = self.scan_anchors(txid, tx, anchors) {
226232
return Some(Err(err));
227233
}
@@ -266,7 +272,7 @@ pub enum ObservedIn {
266272
/// The reason why a transaction is canonical.
267273
#[derive(Debug, Clone, PartialEq, Eq)]
268274
pub enum CanonicalReason<A> {
269-
/// This transaction is explicitly assumed to be canonical by the caller, superceding all other
275+
/// This transaction is explicitly assumed to be canonical by the caller, superseding all other
270276
/// canonicalization rules.
271277
Assumed {
272278
/// Whether it is a descendant that is assumed to be canonical.
@@ -312,7 +318,7 @@ impl<A: Clone> CanonicalReason<A> {
312318
}
313319
}
314320

315-
/// Contruct a new [`CanonicalReason`] from the original which is transitive to `descendant`.
321+
/// Construct a new [`CanonicalReason`] from the original which is transitive to `descendant`.
316322
///
317323
/// This signals that either the [`ObservedIn`] or [`Anchor`] value belongs to the transaction's
318324
/// descendant, but is transitively relevant.

crates/chain/src/tx_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,7 @@ where
16411641
F: FnMut(usize, Arc<Transaction>) -> Option<O>,
16421642
{
16431643
/// Creates a `TxAncestors` that includes the starting `Transaction` when iterating.
1644-
pub(crate) fn new_include_root(
1644+
pub fn new_include_root(
16451645
graph: &'g TxGraph<A>,
16461646
tx: impl Into<Arc<Transaction>>,
16471647
filter_map: F,

0 commit comments

Comments
 (0)