[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.
This commit is contained in:
志宇 2023-05-03 11:43:16 +08:00
parent c61995ca97
commit e413d3e424
No known key found for this signature in database
GPG Key ID: F6345C9837C2BDE8
2 changed files with 10 additions and 11 deletions

View File

@ -624,11 +624,9 @@ impl<A: Anchor> TxGraph<A> {
chain_tip: BlockId, chain_tip: BlockId,
txid: Txid, txid: Txid,
) -> Result<Option<ObservedAs<&A>>, C::Error> { ) -> Result<Option<ObservedAs<&A>>, C::Error> {
let (tx_node, anchors, &last_seen) = match self.txs.get(&txid) { let (tx_node, anchors, last_seen) = match self.txs.get(&txid) {
Some((tx, anchors, last_seen)) if !(anchors.is_empty() && *last_seen == 0) => { Some(v) => v,
(tx, anchors, last_seen) None => return Ok(None),
}
_ => return Ok(None),
}; };
for anchor in anchors { for anchor in anchors {
@ -657,12 +655,12 @@ impl<A: Anchor> TxGraph<A> {
return Ok(None); return Ok(None);
} }
} }
if conflicting_tx.last_seen_unconfirmed > last_seen { if conflicting_tx.last_seen_unconfirmed > *last_seen {
return Ok(None); 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`. /// Get the position of the transaction in `chain` with tip `chain_tip`.

View File

@ -717,10 +717,11 @@ fn test_chain_spends() {
ObservedAs::Confirmed(&local_chain.get_block(95).expect("block expected")) 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. // Even if unconfirmed tx has a last_seen of 0, it can still be part of a chain spend.
assert!(graph assert_eq!(
.get_chain_spend(&local_chain, tip, OutPoint::new(tx_0.txid(), 1)) graph.get_chain_spend(&local_chain, tip, OutPoint::new(tx_0.txid(), 1)),
.is_none()); Some((ObservedAs::Unconfirmed(0), tx_2.txid())),
);
// Mark the unconfirmed as seen and check correct ObservedAs status is returned. // Mark the unconfirmed as seen and check correct ObservedAs status is returned.
let _ = graph.insert_seen_at(tx_2.txid(), 1234567); let _ = graph.insert_seen_at(tx_2.txid(), 1234567);