Skip to content

Commit 956f482

Browse files
committed
Fix compilation of pk()-only policies into tr() descriptors
Before this fix, the added test failed with: `Unexpected("Empty Miniscript compilation")`
1 parent 60ad2c9 commit 956f482

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/policy/concrete.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,13 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
275275
compilation.sanity_check()?;
276276
leaf_compilations.push((OrdF64(prob), compilation));
277277
}
278-
let tap_tree = with_huffman_tree::<Pk>(leaf_compilations)?;
279-
Some(tap_tree)
278+
if !leaf_compilations.is_empty() {
279+
let tap_tree = with_huffman_tree::<Pk>(leaf_compilations)?;
280+
Some(tap_tree)
281+
} else {
282+
// no policies remaining once the extracted key is skipped
283+
None
284+
}
280285
}
281286
},
282287
)?;
@@ -330,8 +335,14 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
330335
)
331336
})
332337
.collect();
333-
let tap_tree = with_huffman_tree::<Pk>(leaf_compilations).unwrap();
334-
Some(tap_tree)
338+
339+
if !leaf_compilations.is_empty() {
340+
let tap_tree = with_huffman_tree::<Pk>(leaf_compilations).unwrap();
341+
Some(tap_tree)
342+
} else {
343+
// no policies remaining once the extracted key is skipped
344+
None
345+
}
335346
}
336347
},
337348
)?;
@@ -1119,6 +1130,14 @@ mod compiler_tests {
11191130
.collect::<Vec<_>>();
11201131
assert_eq!(combinations, expected_comb);
11211132
}
1133+
1134+
#[test]
1135+
fn test_tr_pk_only() {
1136+
let policy: Policy<String> = policy_str!("pk(A)");
1137+
let desc = policy.compile_tr(None).unwrap();
1138+
// pk(A) promoted to the internal key, leaving the script tree empty
1139+
assert_eq!(desc.to_string(), "tr(A)#xyg3grex");
1140+
}
11221141
}
11231142

11241143
#[cfg(test)]

0 commit comments

Comments
 (0)