Skip to content

Commit

Permalink
Adds test inserting anchor without tx
Browse files Browse the repository at this point in the history
Test inserting anchors without the corresponding transaction
and testing the recoverability of the transaction graph

Partially fixes "More tests for IndexedTxGraph and TxGraph bitcoindevkit#958"
bitcoindevkit#958
  • Loading branch information
remix7531 committed May 16, 2023
1 parent 34d6087 commit 0536bd5
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/chain/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[allow(unused_macros)]
macro_rules! h {
($index:literal) => {{
($index:expr) => {{
bitcoin::hashes::Hash::hash($index.as_bytes())
}};
}
Expand Down
92 changes: 92 additions & 0 deletions crates/chain/tests/test_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,3 +822,95 @@ fn test_additions_last_seen_append() {
);
}
}

/// Inserting anchors without the corresponding transaction
/// and testing the recoverability of the transaction graph
#[test]
fn test_inserting_anchor_without_tx() {
let mut graph = TxGraph::<ConfirmationHeightAnchor>::default();
let mut graph_recovered = graph.clone();
let txs = (0..5).map(common::new_tx).collect::<Vec<_>>();
let chain: LocalChain = (0..5)
.map(|i| (i, h!(i.to_string())))
.collect::<BTreeMap<u32, BlockHash>>()
.into();
let anchors = (0..5)
.map(|i| ConfirmationHeightAnchor {
anchor_block: BlockId {
height: i,
hash: *chain.blocks().get(&i).unwrap(),
},
confirmation_height: 0,
})
.collect::<Vec<_>>();

let mut anchor_additions = Additions::default();
for i in 0..3 {
let additions = graph.insert_anchor(txs[i].txid(), anchors[i]);
anchor_additions.append(additions);
}

// Additions should only include the Anchor for the Tx-Block 0, 1 and 2
assert_eq!(
anchor_additions,
Additions {
tx: Default::default(),
txout: Default::default(),
anchors: BTreeSet::from([
(anchors[0], txs[0].txid()),
(anchors[1], txs[1].txid()),
(anchors[2], txs[2].txid()),
]),
last_seen: Default::default(),
}
);

// graph should be recoverable from additions
graph_recovered.apply_additions(anchor_additions);
assert_eq!(graph, graph_recovered);

// Check whether Anchors are part of TxGraph
for i in 0..3 {
assert!(graph.all_anchors().contains(&(anchors[i], txs[i].txid())));
}

let mut tx_additions = Additions::default();
for tx in &txs[1..] {
let additions = graph.insert_tx(tx.clone());
assert_eq!(
additions,
Additions {
tx: BTreeSet::from([tx.clone()]),
txout: Default::default(),
anchors: BTreeSet::default(),
last_seen: Default::default(),
}
);
tx_additions.append(additions);
}

// graph should be recoverable from additions
graph_recovered.apply_additions(tx_additions);
assert_eq!(graph, graph_recovered);

// Transactions 0, 1 and 3 should have an Anchor
for i in 0..3 {
assert_eq!(
graph
.get_chain_position(&chain, chain.tip().unwrap(), txs[i].txid())
.unwrap(),
ObservedAs::Confirmed(&anchors[i])
);
}

// Transactions 4 and 5 should not have an Anchor
for tx in txs.iter().take(5).skip(4) {
assert_eq!(
graph
.try_get_chain_position(&chain, chain.tip().unwrap(), tx.txid())
.unwrap()
.unwrap(),
ObservedAs::Unconfirmed(0)
);
}
}

0 comments on commit 0536bd5

Please sign in to comment.