Skip to content

Commit 12d7307

Browse files
Add Taproot compiler API
1 parent 3f15802 commit 12d7307

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/policy/concrete.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,6 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
194194
}
195195
}
196196

197-
/// Compile [`Policy::Or`] and [`Policy::Threshold`] according to odds
198-
#[cfg(feature = "compiler")]
199-
fn compile_tr_policy(&self) -> Result<TapTree<Pk>, Error> {
200-
let leaf_compilations: Vec<_> = self
201-
.to_tapleaf_prob_vec(1.0)
202-
.into_iter()
203-
.filter(|x| x.1 != Policy::Unsatisfiable)
204-
.map(|(prob, ref policy)| (OrdF64(prob), compiler::best_compilation(policy).unwrap()))
205-
.collect();
206-
let taptree = with_huffman_tree::<Pk>(leaf_compilations).unwrap();
207-
Ok(taptree)
208-
}
209-
210197
/// Extract the internal_key from policy tree.
211198
#[cfg(feature = "compiler")]
212199
fn extract_key(self, unspendable_key: Option<Pk>) -> Result<(Pk, Policy<Pk>), Error> {
@@ -276,7 +263,21 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
276263
internal_key,
277264
match policy {
278265
Policy::Trivial => None,
279-
policy => Some(policy.compile_tr_policy()?),
266+
policy => {
267+
let vec_policies: Vec<_> = policy.to_tapleaf_prob_vec(1.0);
268+
let mut leaf_compilations: Vec<(OrdF64, Miniscript<Pk, Tap>)> = vec![];
269+
for (prob, pol) in vec_policies {
270+
// policy corresponding to the key (replaced by unsatisfiable) is skipped
271+
if pol == Policy::Unsatisfiable {
272+
continue;
273+
}
274+
let compilation = compiler::best_compilation::<Pk, Tap>(&pol)?;
275+
compilation.sanity_check()?;
276+
leaf_compilations.push((OrdF64(prob), compilation));
277+
}
278+
let taptree = with_huffman_tree::<Pk>(leaf_compilations)?;
279+
Some(taptree)
280+
}
280281
},
281282
)?;
282283
Ok(tree)

0 commit comments

Comments
 (0)