Skip to content

Commit afba663

Browse files
committed
Merge #553: Backport recursion fix to 7.x
dd6996c bump 7.0.1 to 7.0.2 (Andrew Poelstra) 7a8e0ce concrete: fix infinite recursion in Policy (Andrew Poelstra) 31f2064 semantic: fix todo and infinite recursion in Policy (Andrew Poelstra) Pull request description: Backport of #546. ACKs for top commit: sanket1729: utACK dd6996c Tree-SHA512: 83c3d2a56569361a58a1b41d8c133bf6527ab020281686e99b9b63c7c7b03eba66aa95862c65788dd11f0663df9e11c76024e310be0f531272e8d474464d052c
2 parents 5dbfc26 + dd6996c commit afba663

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "miniscript"
3-
version = "7.0.1"
3+
version = "7.0.2"
44
authors = ["Andrew Poelstra <[email protected]>, Sanket Kanjalkar <[email protected]>"]
55
repository = "https://github.com/apoelstra/miniscript"
66
description = "Miniscript: a subset of Bitcoin Script designed for analysis"

src/policy/concrete.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
146146
Pk: 'a,
147147
Pk::Hash: 'a,
148148
{
149+
self.real_for_each_key(&mut pred)
150+
}
151+
}
152+
153+
impl<Pk: MiniscriptKey> Policy<Pk> {
154+
fn real_for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, pred: &mut F) -> bool {
149155
match *self {
150156
Policy::Unsatisfiable | Policy::Trivial => true,
151157
Policy::Key(ref pk) => pred(ForEach::Key(pk)),
@@ -156,9 +162,9 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
156162
| Policy::After(..)
157163
| Policy::Older(..) => true,
158164
Policy::Threshold(_, ref subs) | Policy::And(ref subs) => {
159-
subs.iter().all(|sub| sub.for_each_key(&mut pred))
165+
subs.iter().all(|sub| sub.real_for_each_key(&mut *pred))
160166
}
161-
Policy::Or(ref subs) => subs.iter().all(|(_, sub)| sub.for_each_key(&mut pred)),
167+
Policy::Or(ref subs) => subs.iter().all(|(_, sub)| sub.real_for_each_key(&mut *pred)),
162168
}
163169
}
164170
}
@@ -654,3 +660,19 @@ where
654660
Policy::from_tree_prob(top, false).map(|(_, result)| result)
655661
}
656662
}
663+
664+
#[cfg(test)]
665+
mod tests {
666+
use super::*;
667+
use std::str::FromStr;
668+
669+
#[test]
670+
fn for_each_key() {
671+
let liquid_pol = Policy::<String>::from_str(
672+
"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();
673+
let mut count = 0;
674+
assert!(liquid_pol.for_each_key(|_| { count +=1; true }));
675+
assert_eq!(count, 17);
676+
}
677+
}
678+

src/policy/semantic.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ pub enum Policy<Pk: MiniscriptKey> {
5959

6060
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
6161
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool
62+
where
63+
Pk: 'a,
64+
Pk::Hash: 'a,
65+
{
66+
self.real_for_each_key(&mut pred)
67+
}
68+
}
69+
70+
impl<Pk: MiniscriptKey> Policy<Pk> {
71+
fn real_for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, pred: &mut F) -> bool
6272
where
6373
Pk: 'a,
6474
Pk::Hash: 'a,
@@ -72,12 +82,10 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
7282
| Policy::Hash160(..)
7383
| Policy::After(..)
7484
| Policy::Older(..) => true,
75-
Policy::Threshold(_, ref subs) => subs.iter().all(|sub| sub.for_each_key(&mut pred)),
85+
Policy::Threshold(_, ref subs) => subs.iter().all(|sub| sub.real_for_each_key(&mut *pred)),
7686
}
7787
}
78-
}
7988

80-
impl<Pk: MiniscriptKey> Policy<Pk> {
8189
/// Convert a policy using one kind of public key to another
8290
/// type of public key
8391
///
@@ -855,4 +863,13 @@ mod tests {
855863
assert!(auth_alice.entails(htlc_pol.clone()).unwrap());
856864
assert!(htlc_pol.entails(control_alice).unwrap());
857865
}
866+
867+
#[test]
868+
fn for_each_key() {
869+
let liquid_pol = StringPolicy::from_str(
870+
"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();
871+
let mut count = 0;
872+
assert!(liquid_pol.for_each_key(|_| { count +=1; true }));
873+
assert_eq!(count, 17);
874+
}
858875
}

0 commit comments

Comments
 (0)