Skip to content

Commit

Permalink
semantic: Remove recursion in n_keys
Browse files Browse the repository at this point in the history
Done as part of the effort to remove all the recursion crate wide.

Use the `TreeLike` trait to iterate over policy nodes and remove the
recursive call in `semantic::Policy::n_keys`.
  • Loading branch information
tcharding committed Oct 12, 2023
1 parent 760a908 commit dbb4f23
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/policy/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,17 +604,12 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
/// Counts the number of public keys and keyhashes referenced in a policy.
/// Duplicate keys will be double-counted.
pub fn n_keys(&self) -> usize {
match *self {
Policy::Unsatisfiable | Policy::Trivial => 0,
Policy::Key(..) => 1,
Policy::After(..)
| Policy::Older(..)
| Policy::Sha256(..)
| Policy::Hash256(..)
| Policy::Ripemd160(..)
| Policy::Hash160(..) => 0,
Policy::Threshold(_, ref subs) => subs.iter().map(|sub| sub.n_keys()).sum::<usize>(),
}
self.pre_order_iter()
.filter(|policy| match policy {
Policy::Key(..) => true,
_ => false,
})
.count()
}

/// Counts the minimum number of public keys for which signatures could be
Expand Down

0 comments on commit dbb4f23

Please sign in to comment.