Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for inserting anchor without tx #961

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
}
remix7531 marked this conversation as resolved.
Show resolved Hide resolved
);

// 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())));
}
remix7531 marked this conversation as resolved.
Show resolved Hide resolved

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(),
}
remix7531 marked this conversation as resolved.
Show resolved Hide resolved
);
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)
);
}
}
remix7531 marked this conversation as resolved.
Show resolved Hide resolved