Skip to content

Commit 663bc00

Browse files
committed
concrete: fix infinite recursion in Policy
1 parent 520b9db commit 663bc00

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/policy/concrete.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,12 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
668668
where
669669
Pk: 'a,
670670
{
671+
self.real_for_each_key(&mut pred)
672+
}
673+
}
674+
675+
impl<Pk: MiniscriptKey> Policy<Pk> {
676+
fn real_for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: &mut F) -> bool {
671677
match *self {
672678
Policy::Unsatisfiable | Policy::Trivial => true,
673679
Policy::Key(ref pk) => pred(pk),
@@ -678,14 +684,12 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
678684
| Policy::After(..)
679685
| Policy::Older(..) => true,
680686
Policy::Threshold(_, ref subs) | Policy::And(ref subs) => {
681-
subs.iter().all(|sub| sub.for_each_key(&mut pred))
687+
subs.iter().all(|sub| sub.real_for_each_key(&mut *pred))
682688
}
683-
Policy::Or(ref subs) => subs.iter().all(|(_, sub)| sub.for_each_key(&mut pred)),
689+
Policy::Or(ref subs) => subs.iter().all(|(_, sub)| sub.real_for_each_key(&mut *pred)),
684690
}
685691
}
686-
}
687692

688-
impl<Pk: MiniscriptKey> Policy<Pk> {
689693
/// Convert a policy using one kind of public key to another
690694
/// type of public key
691695
///
@@ -1291,7 +1295,7 @@ fn generate_combination<Pk: MiniscriptKey>(
12911295
}
12921296

12931297
#[cfg(all(test, feature = "compiler"))]
1294-
mod tests {
1298+
mod compiler_tests {
12951299
use core::str::FromStr;
12961300

12971301
use sync::Arc;
@@ -1352,3 +1356,18 @@ mod tests {
13521356
assert_eq!(combinations, expected_comb);
13531357
}
13541358
}
1359+
1360+
#[cfg(test)]
1361+
mod tests {
1362+
use super::*;
1363+
use std::str::FromStr;
1364+
1365+
#[test]
1366+
fn for_each_key() {
1367+
let liquid_pol = Policy::<String>::from_str(
1368+
"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();
1369+
let mut count = 0;
1370+
assert!(liquid_pol.for_each_key(|_| { count +=1; true }));
1371+
assert_eq!(count, 17);
1372+
}
1373+
}

0 commit comments

Comments
 (0)