]> Untitled Git - bdk/commitdiff
[chain_redesign] Change behavior of `try_get_chain_position`
author志宇 <hello@evanlinjin.me>
Wed, 3 May 2023 03:43:16 +0000 (11:43 +0800)
committer志宇 <hello@evanlinjin.me>
Wed, 3 May 2023 03:43:16 +0000 (11:43 +0800)
`TxGraph::try_get_chain_position` used to always exclude unconfirmed
transactions with last_seen value of 0. However, what is the point of
including a transaction in the graph if it cannot be part of the chain
history? Additionally, maybe sometimes we don't wish to use the
last_seen field at all.

The new behavior will consider unconfirmed transactions with last_seen
of 0.

crates/chain/src/tx_graph.rs
crates/chain/tests/test_tx_graph.rs

index cee688be77f1c4473ef17d485adbc295ec59b52b..d8b13030305e2b8a138a23552da4110720eddeac 100644 (file)
@@ -624,11 +624,9 @@ impl<A: Anchor> TxGraph<A> {
         chain_tip: BlockId,
         txid: Txid,
     ) -> Result<Option<ObservedAs<&A>>, C::Error> {
-        let (tx_node, anchors, &last_seen) = match self.txs.get(&txid) {
-            Some((tx, anchors, last_seen)) if !(anchors.is_empty() && *last_seen == 0) => {
-                (tx, anchors, last_seen)
-            }
-            _ => return Ok(None),
+        let (tx_node, anchors, last_seen) = match self.txs.get(&txid) {
+            Some(v) => v,
+            None => return Ok(None),
         };
 
         for anchor in anchors {
@@ -657,12 +655,12 @@ impl<A: Anchor> TxGraph<A> {
                     return Ok(None);
                 }
             }
-            if conflicting_tx.last_seen_unconfirmed > last_seen {
+            if conflicting_tx.last_seen_unconfirmed > *last_seen {
                 return Ok(None);
             }
         }
 
-        Ok(Some(ObservedAs::Unconfirmed(last_seen)))
+        Ok(Some(ObservedAs::Unconfirmed(*last_seen)))
     }
 
     /// Get the position of the transaction in `chain` with tip `chain_tip`.
index 41b2ae02fa63288c7724a3ac4fe28e5ee57cce63..7e8c3ad099c3ec901119e5eb174904e58667d2fb 100644 (file)
@@ -717,10 +717,11 @@ fn test_chain_spends() {
         ObservedAs::Confirmed(&local_chain.get_block(95).expect("block expected"))
     );
 
-    // As long the unconfirmed tx isn't marked as seen, chain_spend will return None.
-    assert!(graph
-        .get_chain_spend(&local_chain, tip, OutPoint::new(tx_0.txid(), 1))
-        .is_none());
+    // Even if unconfirmed tx has a last_seen of 0, it can still be part of a chain spend.
+    assert_eq!(
+        graph.get_chain_spend(&local_chain, tip, OutPoint::new(tx_0.txid(), 1)),
+        Some((ObservedAs::Unconfirmed(0), tx_2.txid())),
+    );
 
     // Mark the unconfirmed as seen and check correct ObservedAs status is returned.
     let _ = graph.insert_seen_at(tx_2.txid(), 1234567);