Skip to content

Commit

Permalink
Add Taproot compiler API
Browse files Browse the repository at this point in the history
  • Loading branch information
SarcasticNastik committed Jun 17, 2022
1 parent 859534b commit 5233c66
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/policy/concrete.rs
Expand Up @@ -194,19 +194,6 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
}
}

/// Compile [`Policy::Or`] and [`Policy::Threshold`] according to odds
#[cfg(feature = "compiler")]
fn compile_tr_policy(&self) -> Result<TapTree<Pk>, Error> {
let leaf_compilations: Vec<_> = self
.to_tapleaf_prob_vec(1.0)
.into_iter()
.filter(|x| x.1 != Policy::Unsatisfiable)
.map(|(prob, ref policy)| (OrdF64(prob), compiler::best_compilation(policy).unwrap()))
.collect();
let taptree = with_huffman_tree::<Pk>(leaf_compilations).unwrap();
Ok(taptree)
}

/// Extract the internal_key from policy tree.
#[cfg(feature = "compiler")]
fn extract_key(self, unspendable_key: Option<Pk>) -> Result<(Pk, Policy<Pk>), Error> {
Expand Down Expand Up @@ -276,7 +263,21 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
internal_key,
match policy {
Policy::Trivial => None,
policy => Some(policy.compile_tr_policy()?),
policy => {
let vec_policies: Vec<_> = policy.to_tapleaf_prob_vec(1.0);
let mut leaf_compilations: Vec<(OrdF64, Miniscript<Pk, Tap>)> = vec![];
for (prob, pol) in vec_policies {
// policy corresponding to the key (replaced by unsatisfiable) is skipped
if pol == Policy::Unsatisfiable {
continue;
}
let compilation = compiler::best_compilation::<Pk, Tap>(&pol)?;
compilation.sanity_check()?;
leaf_compilations.push((OrdF64(prob), compilation));
}
let taptree = with_huffman_tree::<Pk>(leaf_compilations)?;
Some(taptree)
}
},
)?;
Ok(tree)
Expand Down

0 comments on commit 5233c66

Please sign in to comment.