Skip to content

Commit 5aafba8

Browse files
committed
Merge #306: Misc: bug fixes
549f6a1 rust 1.59 warnings (sanket1729) 9036cda Implement Tr lifting (sanket1729) b786cf0 Add breaking testcase for tr descriptor parsing (sanket1729) 6d8ed86 Fix tr descriptor parsing bug (sanket1729) c1b516c Fix interpreter inference bug (sanket1729) fbb393e Policy normalize bug (sanket1729) Pull request description: Misc bug fixes ACKs for top commit: apoelstra: ACK 549f6a1 Tree-SHA512: 4b922646979aedb7f2db4974f0caf59b02d37045dd27665d47675cb06323a714fb916e5cfae01d1a061cfa2d124229a2e81bdf33b0c51c00c13854d762f1ee1b
2 parents 8bbcc83 + 549f6a1 commit 5aafba8

File tree

7 files changed

+34
-19
lines changed

7 files changed

+34
-19
lines changed

src/descriptor/mod.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -740,8 +740,14 @@ where
740740

741741
fn from_str(s: &str) -> Result<Descriptor<Pk>, Error> {
742742
let desc_str = verify_checksum(s)?;
743-
let top = expression::Tree::from_str(desc_str)?;
744-
expression::FromTree::from_tree(&top)
743+
// tr tree parsing has special code
744+
if desc_str.starts_with("tr") {
745+
let tr = Tr::from_str(desc_str)?;
746+
Ok(Descriptor::Tr(tr))
747+
} else {
748+
let top = expression::Tree::from_str(desc_str)?;
749+
expression::FromTree::from_tree(&top)
750+
}
745751
}
746752
}
747753

@@ -1252,7 +1258,12 @@ mod tests {
12521258
.unwrap()
12531259
.to_string();
12541260

1255-
assert_eq!(descriptor, "tr(,{pk(),pk()})#7dqr6v8r")
1261+
assert_eq!(descriptor, "tr(,{pk(),pk()})#7dqr6v8r");
1262+
1263+
let descriptor = Descriptor::<String>::from_str("tr(A,{pk(B),pk(C)})")
1264+
.unwrap()
1265+
.to_string();
1266+
assert_eq!(descriptor, "tr(A,{pk(B),pk(C)})#y0uc9t6x");
12561267
}
12571268

12581269
#[test]

src/descriptor/tr.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,13 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for TapTree<Pk> {
546546
impl<Pk: MiniscriptKey> Liftable<Pk> for Tr<Pk> {
547547
fn lift(&self) -> Result<Policy<Pk>, Error> {
548548
match &self.tree {
549-
Some(root) => root.lift(),
549+
Some(root) => Ok(Policy::Threshold(
550+
1,
551+
vec![
552+
Policy::KeyHash(self.internal_key.to_pubkeyhash()),
553+
root.lift()?,
554+
],
555+
)),
550556
None => Ok(Policy::KeyHash(self.internal_key.to_pubkeyhash())),
551557
}
552558
}

src/expression.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn next_expr(sl: &str, delim: char) -> Found {
9292
}
9393
}
9494
} else {
95-
unreachable!("Internal: delimiters in parsing must be '(' or '{'");
95+
unreachable!("{}", "Internal: delimiters in parsing must be '(' or '{'");
9696
}
9797
found
9898
}

src/interpreter/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ impl<'txin> Interpreter<'txin> {
385385
inner::Inner::Script(_, inner::ScriptType::Sh) => true,
386386
inner::Inner::Script(_, inner::ScriptType::Wsh) => false,
387387
inner::Inner::Script(_, inner::ScriptType::ShWsh) => false, // lol "sorta"
388-
inner::Inner::Script(_, inner::ScriptType::Tr) => true,
388+
inner::Inner::Script(_, inner::ScriptType::Tr) => false,
389389
}
390390
}
391391

src/policy/compiler.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,7 @@ mod tests {
12461246
assert!(policy_compile_lift_check("and(pk(A),pk(B))").is_ok());
12471247
assert!(policy_compile_lift_check("or(pk(A),pk(B))").is_ok());
12481248
assert!(policy_compile_lift_check("thresh(2,pk(A),pk(B),pk(C))").is_ok());
1249+
assert!(policy_compile_lift_check("or(thresh(1,pk(A),pk(B)),pk(C))").is_ok());
12491250

12501251
assert_eq!(
12511252
policy_compile_lift_check("thresh(2,after(9),after(9),pk(A))"),

src/policy/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Descriptor<Pk> {
181181
Descriptor::Wpkh(ref wpkh) => wpkh.lift(),
182182
Descriptor::Wsh(ref wsh) => wsh.lift(),
183183
Descriptor::Sh(ref sh) => sh.lift(),
184-
Descriptor::Tr(ref _tr) => unimplemented!(),
184+
Descriptor::Tr(ref tr) => tr.lift(),
185185
}
186186
}
187187
}

src/policy/semantic.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -413,18 +413,15 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
413413
for sub in subs {
414414
match sub {
415415
Policy::Trivial | Policy::Unsatisfiable => {}
416-
Policy::Threshold(1, or_subs) => {
417-
if is_or {
418-
ret_subs.extend(or_subs);
419-
} else {
420-
ret_subs.push(Policy::Threshold(1, or_subs));
421-
}
422-
}
423-
Policy::Threshold(k, and_subs) => {
424-
if k == and_subs.len() && is_and {
425-
ret_subs.extend(and_subs)
426-
} else {
427-
ret_subs.push(Policy::Threshold(k, and_subs));
416+
Policy::Threshold(k, subs) => {
417+
match (is_and, is_or) {
418+
(true, true) => {
419+
// means m = n = 1, thresh(1,X) type thing.
420+
ret_subs.push(Policy::Threshold(k, subs));
421+
}
422+
(true, false) if k == subs.len() => ret_subs.extend(subs), // and case
423+
(false, true) if k == 1 => ret_subs.extend(subs), // or case
424+
_ => ret_subs.push(Policy::Threshold(k, subs)),
428425
}
429426
}
430427
x => ret_subs.push(x),

0 commit comments

Comments
 (0)