Skip to content

Commit c9c73ef

Browse files
Add Taproot compiler private-version
1 parent afdb725 commit c9c73ef

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
@@ -192,19 +192,6 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
192192
}
193193
}
194194

195-
/// Compile [`Policy::Or`] and [`Policy::Threshold`] according to odds
196-
#[cfg(feature = "compiler")]
197-
fn compile_tr_policy(&self) -> Result<TapTree<Pk>, Error> {
198-
let leaf_compilations: Vec<_> = self
199-
.to_tapleaf_prob_vec(1.0)
200-
.into_iter()
201-
.filter(|x| x.1 != Policy::Unsatisfiable)
202-
.map(|(prob, ref policy)| (OrdF64(prob), compiler::best_compilation(policy).unwrap()))
203-
.collect();
204-
let taptree = with_huffman_tree::<Pk>(leaf_compilations).unwrap();
205-
Ok(taptree)
206-
}
207-
208195
/// Extract the internal_key from policy tree.
209196
#[cfg(feature = "compiler")]
210197
fn extract_key(self, unspendable_key: Option<Pk>) -> Result<(Pk, Policy<Pk>), Error> {
@@ -261,7 +248,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
261248
/// the probabilitity of satisfaction for the respective branch in the TapTree.
262249
// TODO: We might require other compile errors for Taproot.
263250
#[cfg(feature = "compiler")]
264-
pub fn compile_tr(&self, unspendable_key: Option<Pk>) -> Result<Descriptor<Pk>, Error> {
251+
pub fn compile_tr_private(&self, unspendable_key: Option<Pk>) -> Result<Descriptor<Pk>, Error> {
265252
self.is_valid()?; // Check for validity
266253
match self.is_safe_nonmalleable() {
267254
(false, _) => Err(Error::from(CompilerError::TopLevelNonSafe)),
@@ -274,7 +261,21 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
274261
internal_key,
275262
match policy {
276263
Policy::Trivial => None,
277-
policy => Some(policy.compile_tr_policy()?),
264+
policy => {
265+
let vec_policies: Vec<_> = policy.to_tapleaf_prob_vec(1.0);
266+
let mut leaf_compilations: Vec<(OrdF64, Miniscript<Pk, Tap>)> = vec![];
267+
for (prob, pol) in vec_policies {
268+
// policy corresponding to the key (replaced by unsatisfiable) is skipped
269+
if pol == Policy::Unsatisfiable {
270+
continue;
271+
}
272+
let compilation = compiler::best_compilation::<Pk, Tap>(&pol)?;
273+
compilation.sanity_check()?;
274+
leaf_compilations.push((OrdF64(prob), compilation));
275+
}
276+
let taptree = with_huffman_tree::<Pk>(leaf_compilations)?;
277+
Some(taptree)
278+
}
278279
},
279280
)?;
280281
Ok(tree)

src/policy/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,9 @@ mod tests {
374374
let unspendable_key: String = "UNSPENDABLE".to_string();
375375
{
376376
let policy: Concrete<String> = policy_str!("thresh(2,pk(A),pk(B),pk(C),pk(D))");
377-
let descriptor = policy.compile_tr(Some(unspendable_key.clone())).unwrap();
377+
let descriptor = policy
378+
.compile_tr_private(Some(unspendable_key.clone()))
379+
.unwrap();
378380

379381
let ms_compilation: Miniscript<String, Tap> = ms_str!("multi_a(2,A,B,C,D)");
380382
let tree: TapTree<String> = TapTree::Leaf(Arc::new(ms_compilation));
@@ -386,7 +388,9 @@ mod tests {
386388
// Trivial multi-node compilation
387389
{
388390
let policy: Concrete<String> = policy_str!("or(and(pk(A),pk(B)),and(pk(C),pk(D)))");
389-
let descriptor = policy.compile_tr(Some(unspendable_key.clone())).unwrap();
391+
let descriptor = policy
392+
.compile_tr_private(Some(unspendable_key.clone()))
393+
.unwrap();
390394

391395
let left_ms_compilation: Arc<Miniscript<String, Tap>> =
392396
Arc::new(ms_str!("and_v(v:pk(C),pk(D))"));
@@ -403,7 +407,7 @@ mod tests {
403407
{
404408
// Invalid policy compilation (Duplicate PubKeys)
405409
let policy: Concrete<String> = policy_str!("or(and(pk(A),pk(B)),and(pk(A),pk(D)))");
406-
let descriptor = policy.compile_tr(Some(unspendable_key.clone()));
410+
let descriptor = policy.compile_tr_private(Some(unspendable_key.clone()));
407411

408412
assert_eq!(
409413
descriptor.unwrap_err().to_string(),
@@ -440,7 +444,9 @@ mod tests {
440444
node_policies[6]
441445
)
442446
);
443-
let descriptor = policy.compile_tr(Some(unspendable_key.clone())).unwrap();
447+
let descriptor = policy
448+
.compile_tr_private(Some(unspendable_key.clone()))
449+
.unwrap();
444450

445451
let mut sorted_policy_prob = node_policies
446452
.iter()

0 commit comments

Comments
 (0)