[bdk_chain_redesign] Introduce BlockAnchor trait

* Introduce `GraphedTx` struct to access transaction data of graphed
  transactions.
* Ability to insert/access anchors and "seen at" values for graphed
  transactions.
* `Additions` now records changes to anchors and last_seen_at.
This commit is contained in:
志宇
2023-03-24 09:23:36 +08:00
parent 82f9caddab
commit 5ae5fe30eb
21 changed files with 584 additions and 298 deletions

View File

@@ -2,9 +2,12 @@
mod common;
use bdk_chain::{
collections::*,
tx_graph::{Additions, TxGraph},
tx_graph::{Additions, GraphedTx, TxGraph},
BlockId,
};
use bitcoin::{
hashes::Hash, BlockHash, OutPoint, PackedLockTime, Script, Transaction, TxIn, TxOut, Txid,
};
use bitcoin::{hashes::Hash, OutPoint, PackedLockTime, Script, Transaction, TxIn, TxOut, Txid};
use core::iter;
#[test]
@@ -35,7 +38,7 @@ fn insert_txouts() {
)];
let mut graph = {
let mut graph = TxGraph::default();
let mut graph = TxGraph::<(u32, BlockHash)>::default();
for (outpoint, txout) in &original_ops {
assert_eq!(
graph.insert_txout(*outpoint, txout.clone()),
@@ -69,6 +72,7 @@ fn insert_txouts() {
Additions {
tx: [].into(),
txout: update_ops.into(),
..Default::default()
}
);
@@ -90,7 +94,7 @@ fn insert_tx_graph_doesnt_count_coinbase_as_spent() {
output: vec![],
};
let mut graph = TxGraph::default();
let mut graph = TxGraph::<(u32, BlockHash)>::default();
let _ = graph.insert_tx(tx);
assert!(graph.outspends(OutPoint::null()).is_empty());
assert!(graph.tx_outspends(Txid::all_zeros()).next().is_none());
@@ -120,8 +124,8 @@ fn insert_tx_graph_keeps_track_of_spend() {
output: vec![],
};
let mut graph1 = TxGraph::default();
let mut graph2 = TxGraph::default();
let mut graph1 = TxGraph::<(u32, BlockHash)>::default();
let mut graph2 = TxGraph::<(u32, BlockHash)>::default();
// insert in different order
let _ = graph1.insert_tx(tx1.clone());
@@ -149,14 +153,17 @@ fn insert_tx_can_retrieve_full_tx_from_graph() {
output: vec![TxOut::default()],
};
let mut graph = TxGraph::default();
let mut graph = TxGraph::<BlockId>::default();
let _ = graph.insert_tx(tx.clone());
assert_eq!(graph.get_tx(tx.txid()), Some(&tx));
assert_eq!(
graph.get_tx(tx.txid()),
Some(GraphedTx::from_tx(&tx, &BTreeSet::new()))
);
}
#[test]
fn insert_tx_displaces_txouts() {
let mut tx_graph = TxGraph::default();
let mut tx_graph = TxGraph::<(u32, BlockHash)>::default();
let tx = Transaction {
version: 0x01,
lock_time: PackedLockTime(0),
@@ -212,7 +219,7 @@ fn insert_tx_displaces_txouts() {
#[test]
fn insert_txout_does_not_displace_tx() {
let mut tx_graph = TxGraph::default();
let mut tx_graph = TxGraph::<(u32, BlockHash)>::default();
let tx = Transaction {
version: 0x01,
lock_time: PackedLockTime(0),
@@ -268,7 +275,7 @@ fn insert_txout_does_not_displace_tx() {
#[test]
fn test_calculate_fee() {
let mut graph = TxGraph::default();
let mut graph = TxGraph::<(u32, BlockHash)>::default();
let intx1 = Transaction {
version: 0x01,
lock_time: PackedLockTime(0),
@@ -362,7 +369,7 @@ fn test_calculate_fee_on_coinbase() {
output: vec![TxOut::default()],
};
let graph = TxGraph::default();
let graph = TxGraph::<(u32, BlockHash)>::default();
assert_eq!(graph.calculate_fee(&tx), Some(0));
}
@@ -404,7 +411,7 @@ fn test_conflicting_descendants() {
let txid_a = tx_a.txid();
let txid_b = tx_b.txid();
let mut graph = TxGraph::default();
let mut graph = TxGraph::<(u32, BlockHash)>::default();
let _ = graph.insert_tx(tx_a);
let _ = graph.insert_tx(tx_b);
@@ -480,7 +487,7 @@ fn test_descendants_no_repeat() {
})
.collect::<Vec<_>>();
let mut graph = TxGraph::default();
let mut graph = TxGraph::<(u32, BlockHash)>::default();
let mut expected_txids = BTreeSet::new();
// these are NOT descendants of `tx_a`