Merge rust-bitcoin/rust-bitcoin#1217: Add multithreading for the pmt_tests execution

f924e1451e Enhance pmt_tests execution time (hrouis)

Pull request description:

  An attempt to enhance pmt_tests execution time.
  fixes: https://github.com/rust-bitcoin/rust-bitcoin/issues/1214

  cargo test result :

  > test result: ok. 356 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 5.74s

ACKs for top commit:
  Kixunil:
    ACK f924e1451e
  RCasatta:
    ACK f924e1451e
  tcharding:
    ACK f924e1451e
  apoelstra:
    utACK f924e1451e

Tree-SHA512: d8341778aa7b9e09014d3392962a9bf844fe7870c158d835f667161d6da2e741ba42088c053979fd76154a89996aaa02f8dcb7c1705271d9dff8e10248931459
This commit is contained in:
Andrew Poelstra 2022-09-01 00:06:48 +00:00
commit 75949355a4
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 77 additions and 64 deletions

View File

@ -532,22 +532,36 @@ mod tests {
use crate::util::merkleblock::{MerkleBlock, PartialMerkleTree}; use crate::util::merkleblock::{MerkleBlock, PartialMerkleTree};
use crate::Block; use crate::Block;
#[test] /// accepts `pmt_test_$num`
fn pmt_tests() { fn pmt_test_from_name(name: &str) {
let mut rng = thread_rng(); pmt_test(name[9..].parse().unwrap())
let tx_counts = vec![1, 4, 7, 17, 56, 100, 127, 256, 312, 513, 1000, 4095]; }
for num_tx in tx_counts { macro_rules! pmt_tests {
($($name:ident),* $(,)?) => {
$(
#[test]
fn $name() {
pmt_test_from_name(stringify!($name));
}
)*
}
}
pmt_tests!(pmt_test_1, pmt_test_4, pmt_test_7, pmt_test_17, pmt_test_56, pmt_test_100,
pmt_test_127, pmt_test_256, pmt_test_312, pmt_test_513, pmt_test_1000, pmt_test_4095);
fn pmt_test(tx_count: usize) {
let mut rng = thread_rng();
// Create some fake tx ids // Create some fake tx ids
let txids = (1..num_tx + 1) // change to `1..=num_tx` when min Rust >= 1.26.0 let tx_ids = (1..=tx_count)
.map(|i| Txid::from_hex(&format!("{:064x}", i)).unwrap()) .map(|i| Txid::from_hex(&format!("{:064x}", i)).unwrap())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// Calculate the merkle root and height // Calculate the merkle root and height
let hashes = txids.iter().map(|t| t.as_hash()); let hashes = tx_ids.iter().map(|t| t.as_hash());
let merkle_root_1: TxMerkleNode = bitcoin_merkle_root(hashes).expect("hashes is not empty").into(); let merkle_root_1: TxMerkleNode = bitcoin_merkle_root(hashes).expect("hashes is not empty").into();
let mut height = 1; let mut height = 1;
let mut ntx = num_tx; let mut ntx = tx_count;
while ntx > 1 { while ntx > 1 {
ntx = (ntx + 1) / 2; ntx = (ntx + 1) / 2;
height += 1; height += 1;
@ -555,9 +569,9 @@ mod tests {
// Check with random subsets with inclusion chances 1, 1/2, 1/4, ..., 1/128 // Check with random subsets with inclusion chances 1, 1/2, 1/4, ..., 1/128
for att in 1..15 { for att in 1..15 {
let mut matches = vec![false; num_tx]; let mut matches = vec![false; tx_count];
let mut match_txid1 = vec![]; let mut match_txid1 = vec![];
for j in 0..num_tx { for j in 0..tx_count {
// Generate `att / 2` random bits // Generate `att / 2` random bits
let rand_bits = match att / 2 { let rand_bits = match att / 2 {
0 => 0, 0 => 0,
@ -567,16 +581,16 @@ mod tests {
matches[j] = include; matches[j] = include;
if include { if include {
match_txid1.push(txids[j]); match_txid1.push(tx_ids[j]);
}; };
} }
// Build the partial merkle tree // Build the partial merkle tree
let pmt1 = PartialMerkleTree::from_txids(&txids, &matches); let pmt1 = PartialMerkleTree::from_txids(&tx_ids, &matches);
let serialized = serialize(&pmt1); let serialized = serialize(&pmt1);
// Verify PartialMerkleTree's size guarantees // Verify PartialMerkleTree's size guarantees
let n = min(num_tx, 1 + match_txid1.len() * height); let n = min(tx_count, 1 + match_txid1.len() * height);
assert!(serialized.len() <= 10 + (258 * n + 7) / 8); assert!(serialized.len() <= 10 + (258 * n + 7) / 8);
// Deserialize into a tester copy // Deserialize into a tester copy
@ -609,7 +623,6 @@ mod tests {
} }
} }
} }
}
#[test] #[test]
fn pmt_malleability() { fn pmt_malleability() {