Skip to content

Backport recursion fix to 7.x #553

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

Merged
merged 3 commits into from
Jun 12, 2023
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "miniscript"
version = "7.0.1"
version = "7.0.2"
authors = ["Andrew Poelstra <[email protected]>, Sanket Kanjalkar <[email protected]>"]
repository = "https://github.com/apoelstra/miniscript"
description = "Miniscript: a subset of Bitcoin Script designed for analysis"
Expand Down
26 changes: 24 additions & 2 deletions src/policy/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
Pk: 'a,
Pk::Hash: 'a,
{
self.real_for_each_key(&mut pred)
}
}

impl<Pk: MiniscriptKey> Policy<Pk> {
fn real_for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, pred: &mut F) -> bool {
match *self {
Policy::Unsatisfiable | Policy::Trivial => true,
Policy::Key(ref pk) => pred(ForEach::Key(pk)),
Expand All @@ -156,9 +162,9 @@ 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)),
}
}
}
Expand Down Expand Up @@ -654,3 +660,19 @@ where
Policy::from_tree_prob(top, false).map(|(_, result)| result)
}
}

#[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);
}
}

23 changes: 20 additions & 3 deletions src/policy/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ pub enum Policy<Pk: MiniscriptKey> {

impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool
where
Pk: 'a,
Pk::Hash: 'a,
{
self.real_for_each_key(&mut pred)
}
}

impl<Pk: MiniscriptKey> Policy<Pk> {
fn real_for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, pred: &mut F) -> bool
where
Pk: 'a,
Pk::Hash: 'a,
Expand All @@ -72,12 +82,10 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
| 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 @@ -855,4 +863,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,pkh(A),pkh(B),pkh(C))),thresh(11,pkh(F1),pkh(F2),pkh(F3),pkh(F4),pkh(F5),pkh(F6),pkh(F7),pkh(F8),pkh(F9),pkh(F10),pkh(F11),pkh(F12),pkh(F13),pkh(F14)))").unwrap();
let mut count = 0;
assert!(liquid_pol.for_each_key(|_| { count +=1; true }));
assert_eq!(count, 17);
}
}