Skip to content

Commit 4fd9281

Browse files
Add Taproot compiler private-version
1 parent ea76eb1 commit 4fd9281

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

src/policy/concrete.rs

Lines changed: 16 additions & 15 deletions
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> {
@@ -263,7 +250,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
263250
/// the probabilitity of satisfaction for the respective branch in the TapTree.
264251
// TODO: We might require other compile errors for Taproot.
265252
#[cfg(feature = "compiler")]
266-
pub fn compile_tr(&self, unspendable_key: Option<Pk>) -> Result<Descriptor<Pk>, Error> {
253+
pub fn compile_tr_private(&self, unspendable_key: Option<Pk>) -> Result<Descriptor<Pk>, Error> {
267254
self.is_valid()?; // Check for validity
268255
match self.is_safe_nonmalleable() {
269256
(false, _) => Err(Error::from(CompilerError::TopLevelNonSafe)),
@@ -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)

src/policy/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,9 @@ mod tests {
378378
let unspendable_key: String = "UNSPENDABLE".to_string();
379379
{
380380
let policy: Concrete<String> = policy_str!("thresh(2,pk(A),pk(B),pk(C),pk(D))");
381-
let descriptor = policy.compile_tr(Some(unspendable_key.clone())).unwrap();
381+
let descriptor = policy
382+
.compile_tr_private(Some(unspendable_key.clone()))
383+
.unwrap();
382384

383385
let ms_compilation: Miniscript<String, Tap> = ms_str!("multi_a(2,A,B,C,D)");
384386
let tree: TapTree<String> = TapTree::Leaf(Arc::new(ms_compilation));
@@ -390,7 +392,9 @@ mod tests {
390392
// Trivial multi-node compilation
391393
{
392394
let policy: Concrete<String> = policy_str!("or(and(pk(A),pk(B)),and(pk(C),pk(D)))");
393-
let descriptor = policy.compile_tr(Some(unspendable_key.clone())).unwrap();
395+
let descriptor = policy
396+
.compile_tr_private(Some(unspendable_key.clone()))
397+
.unwrap();
394398

395399
let left_ms_compilation: Arc<Miniscript<String, Tap>> =
396400
Arc::new(ms_str!("and_v(v:pk(C),pk(D))"));
@@ -407,7 +411,7 @@ mod tests {
407411
{
408412
// Invalid policy compilation (Duplicate PubKeys)
409413
let policy: Concrete<String> = policy_str!("or(and(pk(A),pk(B)),and(pk(A),pk(D)))");
410-
let descriptor = policy.compile_tr(Some(unspendable_key.clone()));
414+
let descriptor = policy.compile_tr_private(Some(unspendable_key.clone()));
411415

412416
assert_eq!(
413417
descriptor.unwrap_err().to_string(),
@@ -444,7 +448,9 @@ mod tests {
444448
node_policies[6]
445449
)
446450
);
447-
let descriptor = policy.compile_tr(Some(unspendable_key.clone())).unwrap();
451+
let descriptor = policy
452+
.compile_tr_private(Some(unspendable_key.clone()))
453+
.unwrap();
448454

449455
let mut sorted_policy_prob = node_policies
450456
.iter()

0 commit comments

Comments
 (0)