From e413d3e42455e26de2a079151aab166af71a3c72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=97=E5=AE=87?= Date: Wed, 3 May 2023 11:43:16 +0800 Subject: [PATCH] [chain_redesign] Change behavior of `try_get_chain_position` `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 | 12 +++++------- crates/chain/tests/test_tx_graph.rs | 9 +++++---- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/crates/chain/src/tx_graph.rs b/crates/chain/src/tx_graph.rs index cee688be..d8b13030 100644 --- a/crates/chain/src/tx_graph.rs +++ b/crates/chain/src/tx_graph.rs @@ -624,11 +624,9 @@ impl TxGraph { chain_tip: BlockId, txid: Txid, ) -> Result>, 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 TxGraph { 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`. diff --git a/crates/chain/tests/test_tx_graph.rs b/crates/chain/tests/test_tx_graph.rs index 41b2ae02..7e8c3ad0 100644 --- a/crates/chain/tests/test_tx_graph.rs +++ b/crates/chain/tests/test_tx_graph.rs @@ -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);