Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport recursion fix to 9.x #566

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 9.0.2 - July 13, 2023

- Workaround for recursion bug in rustc https://github.com/rust-bitcoin/rust-miniscript/pull/566

# 9.0.1 - March 8, 2023

- Fixed a typing rule in `multi_a` for taproot miniscript descriptors. Current typing rules
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "miniscript"
version = "9.0.1"
version = "9.0.2"
authors = ["Andrew Poelstra <apoelstra@wpsoftware.net>, Sanket Kanjalkar <sanket1729@gmail.com>"]
license = "CC0-1.0"
homepage = "https://github.com/rust-bitcoin/rust-miniscript/"
Expand Down Expand Up @@ -63,4 +63,4 @@ required-features = ["compiler","std"]

[[example]]
name = "psbt_sign_finalize"
required-features = ["std"]
required-features = ["std"]
29 changes: 24 additions & 5 deletions src/policy/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,12 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
where
Pk: 'a,
{
self.real_for_each_key(&mut pred)
}
}

impl<Pk: MiniscriptKey> Policy<Pk> {
fn real_for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: &mut F) -> bool {
match *self {
Policy::Unsatisfiable | Policy::Trivial => true,
Policy::Key(ref pk) => pred(pk),
Expand All @@ -667,14 +673,12 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
| Policy::After(..)
| Policy::Older(..) => true,
Policy::Threshold(_, ref subs) | Policy::And(ref subs) => {
subs.iter().all(|sub| sub.for_each_key(&mut pred))
subs.iter().all(|sub| sub.real_for_each_key(&mut *pred))
}
Policy::Or(ref subs) => subs.iter().all(|(_, sub)| sub.for_each_key(&mut pred)),
Policy::Or(ref subs) => subs.iter().all(|(_, sub)| sub.real_for_each_key(&mut *pred)),
}
}
}

impl<Pk: MiniscriptKey> Policy<Pk> {
/// Convert a policy using one kind of public key to another
/// type of public key
///
Expand Down Expand Up @@ -1282,7 +1286,7 @@ fn generate_combination<Pk: MiniscriptKey>(
}

#[cfg(all(test, feature = "compiler"))]
mod tests {
mod compiler_tests {
use core::str::FromStr;

use sync::Arc;
Expand Down Expand Up @@ -1343,3 +1347,18 @@ mod tests {
assert_eq!(combinations, expected_comb);
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;

#[test]
fn for_each_key() {
let liquid_pol = Policy::<String>::from_str(
"or(and(older(4096),thresh(2,pk(A),pk(B),pk(C))),thresh(11,pk(F1),pk(F2),pk(F3),pk(F4),pk(F5),pk(F6),pk(F7),pk(F8),pk(F9),pk(F10),pk(F11),pk(F12),pk(F13),pk(F14)))").unwrap();
let mut count = 0;
assert!(liquid_pol.for_each_key(|_| { count +=1; true }));
assert_eq!(count, 17);
}
}
21 changes: 17 additions & 4 deletions src/policy/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,25 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
where
Pk: 'a,
{
self.real_for_each_key(&mut pred)
}
}

impl<Pk: MiniscriptKey> Policy<Pk> {
fn real_for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: &mut F) -> bool {
match *self {
Policy::Unsatisfiable | Policy::Trivial => true,
Policy::Key(ref _pkh) => todo!("Semantic Policy KeyHash must store Pk"),
Policy::Key(ref pk) => pred(pk),
Policy::Sha256(..)
| Policy::Hash256(..)
| Policy::Ripemd160(..)
| Policy::Hash160(..)
| Policy::After(..)
| Policy::Older(..) => true,
Policy::Threshold(_, ref subs) => subs.iter().all(|sub| sub.for_each_key(&mut pred)),
Policy::Threshold(_, ref subs) => subs.iter().all(|sub| sub.real_for_each_key(&mut *pred)),
}
}
}

impl<Pk: MiniscriptKey> Policy<Pk> {
/// Convert a policy using one kind of public key to another
/// type of public key
///
Expand Down Expand Up @@ -994,4 +998,13 @@ mod tests {
assert!(auth_alice.entails(htlc_pol.clone()).unwrap());
assert!(htlc_pol.entails(control_alice).unwrap());
}

#[test]
fn for_each_key() {
let liquid_pol = StringPolicy::from_str(
"or(and(older(4096),thresh(2,pk(A),pk(B),pk(C))),thresh(11,pk(F1),pk(F2),pk(F3),pk(F4),pk(F5),pk(F6),pk(F7),pk(F8),pk(F9),pk(F10),pk(F11),pk(F12),pk(F13),pk(F14)))").unwrap();
let mut count = 0;
assert!(liquid_pol.for_each_key(|_| { count +=1; true }));
assert_eq!(count, 17);
}
}
Loading