Skip to content

Commit

Permalink
add test for inserting anchor without tx
Browse files Browse the repository at this point in the history
  • Loading branch information
remix7531 committed May 9, 2023
1 parent e3c1370 commit c2d87e4
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 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,57 @@ fn test_additions_last_seen_append() {
);
}
}

#[test]
fn test_inserting_anchor_without_tx() {
let mut graph = TxGraph::<BlockId>::default();
let txs = [common::new_tx(0), common::new_tx(1)];
let chain: LocalChain = (0..=5)
.map(|ht| (ht, BlockHash::hash(format!("Block Hash {}", ht).as_bytes())))
.collect::<BTreeMap<u32, BlockHash>>()
.into();
let tip = chain.tip().unwrap();
let additions = graph.insert_anchor_preview(txs[0].txid(), chain.get_block(0).unwrap().clone());

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

graph.apply_additions(additions);

// We can not directly check whether an Anchor is part of a TxGraph
// so we check if a txid is connected to the correct Anchor / Block.
assert_eq!(
graph
.get_chain_position(&chain, tip, txs[0].txid())
.unwrap(),
Confirmed(&chain.get_block(0).unwrap())
);

// Add transactions
let _ = graph.insert_tx(txs[0].clone());
let _ = graph.insert_tx(txs[1].clone());

// Transaction 0 should have an Anchor
assert_eq!(
graph
.get_chain_position(&chain, tip, txs[0].txid())
.unwrap(),
Confirmed(&chain.get_block(0).unwrap())
);

// Transaction 1 should not have an Anchor
assert_eq!(
graph
.try_get_chain_position(&chain, tip, txs[1].txid())
.unwrap(),
None
);
}

0 comments on commit c2d87e4

Please sign in to comment.