Skip to content

Commit 1ab9d04

Browse files
committed
concrete: fix infinite recursion in Policy
1 parent 4f91873 commit 1ab9d04

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
@@ -657,6 +657,12 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
657657
where
658658
Pk: 'a,
659659
{
660+
self.real_for_each_key(&mut pred)
661+
}
662+
}
663+
664+
impl<Pk: MiniscriptKey> Policy<Pk> {
665+
fn real_for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: &mut F) -> bool {
660666
match *self {
661667
Policy::Unsatisfiable | Policy::Trivial => true,
662668
Policy::Key(ref pk) => pred(pk),
@@ -667,14 +673,12 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
667673
| Policy::After(..)
668674
| Policy::Older(..) => true,
669675
Policy::Threshold(_, ref subs) | Policy::And(ref subs) => {
670-
subs.iter().all(|sub| sub.for_each_key(&mut pred))
676+
subs.iter().all(|sub| sub.real_for_each_key(&mut *pred))
671677
}
672-
Policy::Or(ref subs) => subs.iter().all(|(_, sub)| sub.for_each_key(&mut pred)),
678+
Policy::Or(ref subs) => subs.iter().all(|(_, sub)| sub.real_for_each_key(&mut *pred)),
673679
}
674680
}
675-
}
676681

677-
impl<Pk: MiniscriptKey> Policy<Pk> {
678682
/// Convert a policy using one kind of public key to another
679683
/// type of public key
680684
///
@@ -1282,7 +1286,7 @@ fn generate_combination<Pk: MiniscriptKey>(
12821286
}
12831287

12841288
#[cfg(all(test, feature = "compiler"))]
1285-
mod tests {
1289+
mod compiler_tests {
12861290
use core::str::FromStr;
12871291

12881292
use sync::Arc;
@@ -1343,3 +1347,18 @@ mod tests {
13431347
assert_eq!(combinations, expected_comb);
13441348
}
13451349
}
1350+
1351+
#[cfg(test)]
1352+
mod tests {
1353+
use super::*;
1354+
use std::str::FromStr;
1355+
1356+
#[test]
1357+
fn for_each_key() {
1358+
let liquid_pol = Policy::<String>::from_str(
1359+
"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();
1360+
let mut count = 0;
1361+
assert!(liquid_pol.for_each_key(|_| { count +=1; true }));
1362+
assert_eq!(count, 17);
1363+
}
1364+
}

0 commit comments

Comments
 (0)