[bdk_chain_redesign] TxGraph tweaks

* Rename `TxNode::last_seen` to `last_seen_unconfirmed` and improve docs
* Improve `try_get_chain_position` logic and tweak comments
This commit is contained in:
志宇 2023-03-31 14:15:34 +08:00
parent a63ffe9739
commit 7810059ed0
No known key found for this signature in database
GPG Key ID: F6345C9837C2BDE8

View File

@ -103,8 +103,8 @@ pub struct TxNode<'a, T, A> {
pub tx: &'a T, pub tx: &'a T,
/// The blocks that the transaction is "anchored" in. /// The blocks that the transaction is "anchored" in.
pub anchors: &'a BTreeSet<A>, pub anchors: &'a BTreeSet<A>,
/// The last-seen unix timestamp of the transaction. /// The last-seen unix timestamp of the transaction as unconfirmed.
pub last_seen: u64, pub last_seen_unconfirmed: u64,
} }
impl<'a, T, A> Deref for TxNode<'a, T, A> { impl<'a, T, A> Deref for TxNode<'a, T, A> {
@ -121,7 +121,7 @@ impl<'a, A> TxNode<'a, Transaction, A> {
txid: tx.txid(), txid: tx.txid(),
tx, tx,
anchors, anchors,
last_seen: 0, last_seen_unconfirmed: 0,
} }
} }
} }
@ -168,7 +168,7 @@ impl<A> TxGraph<A> {
txid, txid,
tx, tx,
anchors, anchors,
last_seen: *last_seen, last_seen_unconfirmed: *last_seen,
}), }),
TxNodeInternal::Partial(_) => None, TxNodeInternal::Partial(_) => None,
}) })
@ -190,7 +190,7 @@ impl<A> TxGraph<A> {
txid, txid,
tx, tx,
anchors, anchors,
last_seen: *last_seen, last_seen_unconfirmed: *last_seen,
}), }),
_ => None, _ => None,
} }
@ -290,7 +290,7 @@ impl<A> TxGraph<A> {
txid, txid,
tx: partial, tx: partial,
anchors, anchors,
last_seen: *last_seen, last_seen_unconfirmed: *last_seen,
}), }),
}) })
} }
@ -628,8 +628,8 @@ impl<A: BlockAnchor> TxGraph<A> {
} }
}; };
// [TODO] Is this logic correct? I do not think so, but it should be good enough for now! // If a conflicting tx is in the best chain, or has `last_seen` higher than this tx, then
let mut latest_last_seen = 0_u64; // this tx cannot exist in the best chain
for conflicting_tx in self.walk_conflicts(tx, |_, txid| self.get_tx_node(txid)) { for conflicting_tx in self.walk_conflicts(tx, |_, txid| self.get_tx_node(txid)) {
for block_id in conflicting_tx.anchors.iter().map(A::anchor_block) { for block_id in conflicting_tx.anchors.iter().map(A::anchor_block) {
if chain.is_block_in_best_chain(block_id)? { if chain.is_block_in_best_chain(block_id)? {
@ -637,15 +637,12 @@ impl<A: BlockAnchor> TxGraph<A> {
return Ok(None); return Ok(None);
} }
} }
if conflicting_tx.last_seen > latest_last_seen { if conflicting_tx.last_seen_unconfirmed > last_seen {
latest_last_seen = conflicting_tx.last_seen; return Ok(None);
} }
} }
if last_seen >= latest_last_seen {
Ok(Some(ObservedIn::Mempool(last_seen))) Ok(Some(ObservedIn::Mempool(last_seen)))
} else {
Ok(None)
}
} }
pub fn get_chain_position<C>(&self, chain: C, txid: Txid) -> Option<ObservedIn<&A>> pub fn get_chain_position<C>(&self, chain: C, txid: Txid) -> Option<ObservedIn<&A>>