Skip to content

Commit 7a8e0ce

Browse files
committed
concrete: fix infinite recursion in Policy
1 parent 31f2064 commit 7a8e0ce

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

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+

0 commit comments

Comments
 (0)