Merge rust-bitcoin/rust-bitcoin#914: Taproot Huffman tree builder u64->u32 fixes
8dabe3ed64
Taproot Huffman tree builder u64->u32 fixes (Dr Maxim Orlovsky) Pull request description: Follow-up fixes for #909 ACKs for top commit: sanket1729: ACK8dabe3ed64
apoelstra: ACK8dabe3ed64
Tree-SHA512: 0e05b2183c7746ed57b0f585abf769d10b638840e9b8b0de07e718f451f349c15c06e223930fd51b192f1328734fabf242f1fa701518ebc57ceed4b4d85c8dbe
This commit is contained in:
commit
1c923c1da6
|
@ -367,8 +367,8 @@ impl TaprootBuilder {
|
||||||
/// weights of satisfaction for that script. The weights represent the probability of
|
/// weights of satisfaction for that script. The weights represent the probability of
|
||||||
/// each branch being taken. If probabilities/weights for each condition are known,
|
/// each branch being taken. If probabilities/weights for each condition are known,
|
||||||
/// constructing the tree as a Huffman tree is the optimal way to minimize average
|
/// constructing the tree as a Huffman tree is the optimal way to minimize average
|
||||||
/// case satisfaction cost. This function takes input an iterator of tuple(u64, &Script)
|
/// case satisfaction cost. This function takes an iterator of (`u32`, &[`Script`]) tuples
|
||||||
/// where usize represents the satisfaction weights of the branch.
|
/// as an input, where `u32` represents the satisfaction weights of the script branch.
|
||||||
/// For example, [(3, S1), (2, S2), (5, S3)] would construct a TapTree that has optimal
|
/// For example, [(3, S1), (2, S2), (5, S3)] would construct a TapTree that has optimal
|
||||||
/// satisfaction weight when probability for S1 is 30%, S2 is 20% and S3 is 50%.
|
/// satisfaction weight when probability for S1 is 30%, S2 is 20% and S3 is 50%.
|
||||||
///
|
///
|
||||||
|
@ -387,9 +387,9 @@ impl TaprootBuilder {
|
||||||
where
|
where
|
||||||
I: IntoIterator<Item=(u32, Script)>,
|
I: IntoIterator<Item=(u32, Script)>,
|
||||||
{
|
{
|
||||||
let mut node_weights = BinaryHeap::<(Reverse<u64>, NodeInfo)>::new();
|
let mut node_weights = BinaryHeap::<(Reverse<u32>, NodeInfo)>::new();
|
||||||
for (p, leaf) in script_weights {
|
for (p, leaf) in script_weights {
|
||||||
node_weights.push((Reverse(p as u64), NodeInfo::new_leaf_with_ver(leaf, LeafVersion::TapScript)));
|
node_weights.push((Reverse(p), NodeInfo::new_leaf_with_ver(leaf, LeafVersion::TapScript)));
|
||||||
}
|
}
|
||||||
if node_weights.is_empty() {
|
if node_weights.is_empty() {
|
||||||
return Err(TaprootBuilderError::IncompleteTree);
|
return Err(TaprootBuilderError::IncompleteTree);
|
||||||
|
@ -407,7 +407,7 @@ impl TaprootBuilder {
|
||||||
}
|
}
|
||||||
// Every iteration of the loop reduces the node_weights.len() by exactly 1
|
// Every iteration of the loop reduces the node_weights.len() by exactly 1
|
||||||
// Therefore, the loop will eventually terminate with exactly 1 element
|
// Therefore, the loop will eventually terminate with exactly 1 element
|
||||||
debug_assert!(node_weights.len() == 1);
|
debug_assert_eq!(node_weights.len(), 1);
|
||||||
let node = node_weights.pop().expect("huffman tree algorithm is broken").1;
|
let node = node_weights.pop().expect("huffman tree algorithm is broken").1;
|
||||||
Ok(TaprootBuilder{branch: vec![Some(node)]})
|
Ok(TaprootBuilder{branch: vec![Some(node)]})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue