From 169e4895d916069a0d2e12bf763c7eee0f55a9c4 Mon Sep 17 00:00:00 2001 From: Nadav Ivgi Date: Wed, 1 May 2024 00:01:20 +0300 Subject: [PATCH 1/2] Fix compilation of pk()-only policies into tr() descriptors --- src/policy/concrete.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/policy/concrete.rs b/src/policy/concrete.rs index 8e31d5474..44069fbd2 100644 --- a/src/policy/concrete.rs +++ b/src/policy/concrete.rs @@ -247,8 +247,13 @@ impl Policy { .expect("compiler produces sane output"); leaf_compilations.push((OrdF64(prob), compilation)); } - let tap_tree = with_huffman_tree::(leaf_compilations).unwrap(); - Some(tap_tree) + if !leaf_compilations.is_empty() { + let tap_tree = with_huffman_tree::(leaf_compilations).unwrap(); + Some(tap_tree) + } else { + // no policies remaining once the extracted key is skipped + None + } } }, ) @@ -303,8 +308,14 @@ impl Policy { ) }) .collect(); - let tap_tree = with_huffman_tree::(leaf_compilations).unwrap(); - Some(tap_tree) + + if !leaf_compilations.is_empty() { + let tap_tree = with_huffman_tree::(leaf_compilations).unwrap(); + Some(tap_tree) + } else { + // no policies remaining once the extracted key is skipped + None + } } }, )?; From ea3c523b1c0b52069ca2b80e1b07bb275a3ebe9a Mon Sep 17 00:00:00 2001 From: Nadav Ivgi Date: Wed, 1 May 2024 00:01:41 +0300 Subject: [PATCH 2/2] Add test case for compilation of pk()-only policies Prior to this fix, the test failed with: `Unexpected("Empty Miniscript compilation")` --- src/policy/concrete.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/policy/concrete.rs b/src/policy/concrete.rs index 44069fbd2..578d821b4 100644 --- a/src/policy/concrete.rs +++ b/src/policy/concrete.rs @@ -1120,6 +1120,14 @@ mod compiler_tests { .collect::>(); assert_eq!(combinations, expected_comb); } + + #[test] + fn test_tr_pk_only() { + let policy: Policy = 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)]