[bdk_chain_redesign] Fix Anchor definition + docs

Previously, I have misunderstood the definition of anchor. If a tx is
anchored in a block, it does not necessarily mean it is confirmed in
that block. The tx can be confirmed in an ancestor block of the anchor
block.

With this new definition, we need a new trait `ConfirmationHeight` that
has one method `confirmation_height`. This trait can be used to extend
`Anchor` for those implementations that can give us the exact
conirmation height of a tx (which is useful in most cases).

Another change is to add another variant to the `ObservedAs` enum;
`ObservedAs::ConfirmedImplicit(A)`. If a tx does not have an anchor, but
another tx that spends it has an anchor that in in the best chain, we
can assume that tx is also in the best chain. The logic of
`TxGraph::try_get_chain_position` is also changed to reflect this.

Some methods from `IndexedTxGraph` have been moved to `TxGraph` as they
do not require the `Indexer`. Some `TxGraph` methods have been renamed
for clarity and consistency.

Also more docs are added.
This commit is contained in:
志宇
2023-04-17 23:25:57 +08:00
parent 001efdd1cb
commit 81436fcd72
8 changed files with 474 additions and 226 deletions

View File

@@ -149,12 +149,12 @@ fn insert_txouts() {
// Apply addition and check the new graph counts.
graph.apply_additions(additions);
assert_eq!(graph.all_txouts().count(), 4);
assert_eq!(graph.full_transactions().count(), 1);
assert_eq!(graph.partial_transactions().count(), 2);
assert_eq!(graph.full_txs().count(), 1);
assert_eq!(graph.floating_txouts().count(), 3);
// Check TxOuts are fetched correctly from the graph.
assert_eq!(
graph.txouts(h!("tx1")).expect("should exists"),
graph.tx_outputs(h!("tx1")).expect("should exists"),
[
(
1u32,
@@ -175,7 +175,7 @@ fn insert_txouts() {
);
assert_eq!(
graph.txouts(update_txs.txid()).expect("should exists"),
graph.tx_outputs(update_txs.txid()).expect("should exists"),
[(
0u32,
&TxOut {
@@ -201,8 +201,8 @@ fn insert_tx_graph_doesnt_count_coinbase_as_spent() {
let mut graph = TxGraph::<()>::default();
let _ = graph.insert_tx(tx);
assert!(graph.outspends(OutPoint::null()).is_empty());
assert!(graph.tx_outspends(Txid::all_zeros()).next().is_none());
assert!(graph.output_spends(OutPoint::null()).is_empty());
assert!(graph.tx_spends(Txid::all_zeros()).next().is_none());
}
#[test]
@@ -240,10 +240,10 @@ fn insert_tx_graph_keeps_track_of_spend() {
let _ = graph2.insert_tx(tx1);
assert_eq!(
graph1.outspends(op),
graph1.output_spends(op),
&iter::once(tx2.txid()).collect::<HashSet<_>>()
);
assert_eq!(graph2.outspends(op), graph1.outspends(op));
assert_eq!(graph2.output_spends(op), graph1.output_spends(op));
}
#[test]