Skip to content

Commit 520b9db

Browse files
committed
semantic: fix todo and infinite recursion in Policy
1 parent 7c28bd3 commit 520b9db

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/policy/semantic.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,25 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
6565
where
6666
Pk: 'a,
6767
{
68+
self.real_for_each_key(&mut pred)
69+
}
70+
}
71+
72+
impl<Pk: MiniscriptKey> Policy<Pk> {
73+
fn real_for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: &mut F) -> bool {
6874
match *self {
6975
Policy::Unsatisfiable | Policy::Trivial => true,
70-
Policy::Key(ref _pkh) => todo!("Semantic Policy KeyHash must store Pk"),
76+
Policy::Key(ref pk) => pred(pk),
7177
Policy::Sha256(..)
7278
| Policy::Hash256(..)
7379
| Policy::Ripemd160(..)
7480
| Policy::Hash160(..)
7581
| Policy::After(..)
7682
| Policy::Older(..) => true,
77-
Policy::Threshold(_, ref subs) => subs.iter().all(|sub| sub.for_each_key(&mut pred)),
83+
Policy::Threshold(_, ref subs) => subs.iter().all(|sub| sub.real_for_each_key(&mut *pred)),
7884
}
7985
}
80-
}
8186

82-
impl<Pk: MiniscriptKey> Policy<Pk> {
8387
/// Convert a policy using one kind of public key to another
8488
/// type of public key
8589
///
@@ -970,4 +974,13 @@ mod tests {
970974
assert!(auth_alice.entails(htlc_pol.clone()).unwrap());
971975
assert!(htlc_pol.entails(control_alice).unwrap());
972976
}
977+
978+
#[test]
979+
fn for_each_key() {
980+
let liquid_pol = StringPolicy::from_str(
981+
"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();
982+
let mut count = 0;
983+
assert!(liquid_pol.for_each_key(|_| { count +=1; true }));
984+
assert_eq!(count, 17);
985+
}
973986
}

0 commit comments

Comments
 (0)