Skip to content

Commit

Permalink
Adds includes_tx_with_selected_dependencies test
Browse files Browse the repository at this point in the history
  • Loading branch information
arya2 committed Sep 27, 2024
1 parent fa9d89b commit dbcfafc
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions zebra-rpc/src/methods/get_block_template_rpcs/zip317/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,74 @@ fn excludes_tx_with_unselected_dependencies() {
"should not select any transactions when dependencies are unavailable"
);
}

#[test]
fn includes_tx_with_selected_dependencies() {
let network = Network::Mainnet;
let next_block_height = Height(1_000_000);
let miner_address = transparent::Address::from_pub_key_hash(network.kind(), [0; 20]);
let unmined_txs: Vec<_> = network.unmined_transactions_in_blocks(..).take(3).collect();

let dependent_tx1 = unmined_txs.first().expect("should have 3 txns");
let dependent_tx2 = unmined_txs.get(1).expect("should have 3 txns");
let independent_tx_id = unmined_txs
.get(2)
.expect("should have 3 txns")
.transaction
.id
.mined_id();

let mut mempool_tx_deps = TransactionDependencies::default();
mempool_tx_deps.add(
dependent_tx1.transaction.id.mined_id(),
vec![OutPoint::from_usize(independent_tx_id, 0)],
);
mempool_tx_deps.add(
dependent_tx2.transaction.id.mined_id(),
vec![
OutPoint::from_usize(independent_tx_id, 0),
OutPoint::from_usize(transaction::Hash([0; 32]), 0),
],
);

let like_zcashd = true;
let extra_coinbase_data = Vec::new();

let selected_txs = select_mempool_transactions(
&network,
next_block_height,
&miner_address,
unmined_txs.clone(),
mempool_tx_deps.clone(),
like_zcashd,
extra_coinbase_data,
);

assert_eq!(
selected_txs.len(),
2,
"should select the independent transaction and 1 of the dependent txs, selected: {selected_txs:?}"
);

let selected_tx_by_id = |id| {
selected_txs
.iter()
.find(|(_, tx)| tx.transaction.id.mined_id() == id)
};

let (dependency_depth, _) =
selected_tx_by_id(independent_tx_id).expect("should select the independent tx");

assert_eq!(
*dependency_depth, 0,
"should return a dependency depth of 0 for the independent tx"
);

let (dependency_depth, _) = selected_tx_by_id(dependent_tx1.transaction.id.mined_id())
.expect("should select dependent_tx1");

assert_eq!(
*dependency_depth, 1,
"should return a dependency depth of 1 for the dependent tx"
);
}

0 comments on commit dbcfafc

Please sign in to comment.