Skip to content

Commit e413d3e

Browse files
committed
[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.
1 parent c61995c commit e413d3e

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

crates/chain/src/tx_graph.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -624,11 +624,9 @@ impl<A: Anchor> TxGraph<A> {
624624
chain_tip: BlockId,
625625
txid: Txid,
626626
) -> Result<Option<ObservedAs<&A>>, C::Error> {
627-
let (tx_node, anchors, &last_seen) = match self.txs.get(&txid) {
628-
Some((tx, anchors, last_seen)) if !(anchors.is_empty() && *last_seen == 0) => {
629-
(tx, anchors, last_seen)
630-
}
631-
_ => return Ok(None),
627+
let (tx_node, anchors, last_seen) = match self.txs.get(&txid) {
628+
Some(v) => v,
629+
None => return Ok(None),
632630
};
633631

634632
for anchor in anchors {
@@ -657,12 +655,12 @@ impl<A: Anchor> TxGraph<A> {
657655
return Ok(None);
658656
}
659657
}
660-
if conflicting_tx.last_seen_unconfirmed > last_seen {
658+
if conflicting_tx.last_seen_unconfirmed > *last_seen {
661659
return Ok(None);
662660
}
663661
}
664662

665-
Ok(Some(ObservedAs::Unconfirmed(last_seen)))
663+
Ok(Some(ObservedAs::Unconfirmed(*last_seen)))
666664
}
667665

668666
/// Get the position of the transaction in `chain` with tip `chain_tip`.

crates/chain/tests/test_tx_graph.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -717,10 +717,11 @@ fn test_chain_spends() {
717717
ObservedAs::Confirmed(&local_chain.get_block(95).expect("block expected"))
718718
);
719719

720-
// As long the unconfirmed tx isn't marked as seen, chain_spend will return None.
721-
assert!(graph
722-
.get_chain_spend(&local_chain, tip, OutPoint::new(tx_0.txid(), 1))
723-
.is_none());
720+
// Even if unconfirmed tx has a last_seen of 0, it can still be part of a chain spend.
721+
assert_eq!(
722+
graph.get_chain_spend(&local_chain, tip, OutPoint::new(tx_0.txid(), 1)),
723+
Some((ObservedAs::Unconfirmed(0), tx_2.txid())),
724+
);
724725

725726
// Mark the unconfirmed as seen and check correct ObservedAs status is returned.
726727
let _ = graph.insert_seen_at(tx_2.txid(), 1234567);

0 commit comments

Comments
 (0)