Skip to content

Commit 31f2064

Browse files
committed
semantic: fix todo and infinite recursion in Policy
1 parent 5dbfc26 commit 31f2064

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/policy/semantic.rs

Lines changed: 20 additions & 3 deletions
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)