Skip to content

Commit

Permalink
Merge rust-bitcoin#677: Fix compilation of pk()-only policies into tr…
Browse files Browse the repository at this point in the history
…() descriptors

ea3c523 Add test case for compilation of pk()-only policies (Nadav Ivgi)
169e489 Fix compilation of pk()-only policies into tr() descriptors (Nadav Ivgi)

Pull request description:

  Before this fix, the added test failed with: `Unexpected("Empty Miniscript compilation")`

ACKs for top commit:
  apoelstra:
    ACK ea3c523 successfully ran local tests; good find!

Tree-SHA512: 3d19d4f23f21d4c811ddb1e0a3cc66f0bc90cffe47f4d5a0659caa44b0d80a0665e71b46b8289f1efba98ba4cf731eac8bc7d1fc7b5a3d701cf5c85b48fc5551
  • Loading branch information
apoelstra committed Sep 17, 2024
2 parents 286fa88 + ea3c523 commit 98104b9
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/policy/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,13 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
.expect("compiler produces sane output");
leaf_compilations.push((OrdF64(prob), compilation));
}
let tap_tree = with_huffman_tree::<Pk>(leaf_compilations).unwrap();
Some(tap_tree)
if !leaf_compilations.is_empty() {
let tap_tree = with_huffman_tree::<Pk>(leaf_compilations).unwrap();
Some(tap_tree)
} else {
// no policies remaining once the extracted key is skipped
None
}
}
},
)
Expand Down Expand Up @@ -303,8 +308,14 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
)
})
.collect();
let tap_tree = with_huffman_tree::<Pk>(leaf_compilations).unwrap();
Some(tap_tree)

if !leaf_compilations.is_empty() {
let tap_tree = with_huffman_tree::<Pk>(leaf_compilations).unwrap();
Some(tap_tree)
} else {
// no policies remaining once the extracted key is skipped
None
}
}
},
)?;
Expand Down Expand Up @@ -1109,6 +1120,14 @@ mod compiler_tests {
.collect::<Vec<_>>();
assert_eq!(combinations, expected_comb);
}

#[test]
fn test_tr_pk_only() {
let policy: Policy<String> = policy_str!("pk(A)");
let desc = policy.compile_tr(None).unwrap();
// pk(A) promoted to the internal key, leaving the script tree empty
assert_eq!(desc.to_string(), "tr(A)#xyg3grex");
}
}

#[cfg(test)]
Expand Down

0 comments on commit 98104b9

Please sign in to comment.