Refactor merkle block test

Refactored the unit test as suggested in
https://github.com/rust-bitcoin/rust-bitcoin/pull/2859#issuecomment-2161710169
This commit is contained in:
Jamil Lambert, PhD 2024-06-13 16:08:38 +01:00
parent c4c1252a9e
commit 778a44dd64
1 changed files with 16 additions and 7 deletions

View File

@ -825,16 +825,25 @@ mod tests {
1b01e32f570200000002252bf9d75c4f481ebb6278d708257d1f12beb6dd30301d26c623f789b2ba6fc0e2d3\ 1b01e32f570200000002252bf9d75c4f481ebb6278d708257d1f12beb6dd30301d26c623f789b2ba6fc0e2d3\
2adb5f8ca820731dff234a84e78ec30bce4ec69dbd562d0b2b8266bf4e5a0105").unwrap(); 2adb5f8ca820731dff234a84e78ec30bce4ec69dbd562d0b2b8266bf4e5a0105").unwrap();
let mb: MerkleBlock = encode::deserialize(&mb_bytes).unwrap(); let mb: MerkleBlock = encode::deserialize(&mb_bytes).unwrap();
// Authenticate and extract matched transaction ids // Authenticate and extract matched transaction ids
let mut matches: Vec<Txid> = vec![]; let mut matches: Vec<Txid> = vec![];
let mut index: Vec<u32> = vec![]; let mut index: Vec<u32> = vec![];
assert!(mb.extract_matches(&mut matches, &mut index).is_ok()); assert!(mb.extract_matches(&mut matches, &mut index).is_ok());
assert_eq!(1, matches.len());
assert_eq!( // The matches and index vectors are coupled, should be the same length.
"5a4ebf66822b0b2d56bd9dc64ece0bc38ee7844a23ff1d7320a88c5fdb2ad3e2".parse::<Txid>().unwrap(), assert_eq!(matches.len(), index.len());
matches[0]
); // There should only be one match.
assert_eq!(1, index.len()); assert_eq!(matches.len(), 1);
assert_eq!(1, index[0]);
// The match should come from index 1.
assert_eq!(index[0], 1);
// And we know the txid we want.
let want = "5a4ebf66822b0b2d56bd9dc64ece0bc38ee7844a23ff1d7320a88c5fdb2ad3e2"
.parse::<Txid>()
.expect("failed to parse txid");
assert_eq!(matches[0], want);
} }
} }