Skip to content

Commit 2c422e6

Browse files
committed
semantic: Remove recursion in real_*_timelocks
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 the `semantic::Policy::real_*_timelocks` functions. Done together because they are basically identical.
1 parent 48a0af8 commit 2c422e6

File tree

1 file changed

+12
-30
lines changed

1 file changed

+12
-30
lines changed

src/policy/semantic.rs

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -479,21 +479,12 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
479479

480480
/// Helper function to do the recursion in `timelocks`.
481481
fn real_relative_timelocks(&self) -> Vec<u32> {
482-
match *self {
483-
Policy::Unsatisfiable
484-
| Policy::Trivial
485-
| Policy::Key(..)
486-
| Policy::Sha256(..)
487-
| Policy::Hash256(..)
488-
| Policy::Ripemd160(..)
489-
| Policy::Hash160(..) => vec![],
490-
Policy::After(..) => vec![],
491-
Policy::Older(t) => vec![t.to_consensus_u32()],
492-
Policy::Threshold(_, ref subs) => subs.iter().fold(vec![], |mut acc, x| {
493-
acc.extend(x.real_relative_timelocks());
494-
acc
495-
}),
496-
}
482+
self.pre_order_iter()
483+
.filter_map(|policy| match policy {
484+
Policy::Older(t) => Some(t.to_consensus_u32()),
485+
_ => None,
486+
})
487+
.collect()
497488
}
498489

499490
/// Returns a list of all relative timelocks, not including 0, which appear
@@ -507,21 +498,12 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
507498

508499
/// Helper function for recursion in `absolute timelocks`
509500
fn real_absolute_timelocks(&self) -> Vec<u32> {
510-
match *self {
511-
Policy::Unsatisfiable
512-
| Policy::Trivial
513-
| Policy::Key(..)
514-
| Policy::Sha256(..)
515-
| Policy::Hash256(..)
516-
| Policy::Ripemd160(..)
517-
| Policy::Hash160(..) => vec![],
518-
Policy::Older(..) => vec![],
519-
Policy::After(t) => vec![t.to_u32()],
520-
Policy::Threshold(_, ref subs) => subs.iter().fold(vec![], |mut acc, x| {
521-
acc.extend(x.real_absolute_timelocks());
522-
acc
523-
}),
524-
}
501+
self.pre_order_iter()
502+
.filter_map(|policy| match policy {
503+
Policy::After(t) => Some(t.to_u32()),
504+
_ => None,
505+
})
506+
.collect()
525507
}
526508

527509
/// Returns a list of all absolute timelocks, not including 0, which appear

0 commit comments

Comments
 (0)