@@ -500,7 +500,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
500500 }
501501
502502 /// Helper function for recursion in `absolute timelocks`
503- pub fn real_absolute_timelocks ( & self ) -> Vec < u32 > {
503+ fn real_absolute_timelocks ( & self ) -> Vec < u32 > {
504504 match * self {
505505 Policy :: Unsatisfiable
506506 | Policy :: Trivial
@@ -546,6 +546,25 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
546546 self . normalized ( )
547547 }
548548
549+ /// Filter a policy by eliminating absolute timelock constraints
550+ /// that are not satisfied at the given age.
551+ pub fn at_height ( mut self , time : u32 ) -> Policy < Pk > {
552+ self = match self {
553+ Policy :: After ( t) => {
554+ if t > time {
555+ Policy :: Unsatisfiable
556+ } else {
557+ Policy :: After ( t)
558+ }
559+ }
560+ Policy :: Threshold ( k, subs) => {
561+ Policy :: Threshold ( k, subs. into_iter ( ) . map ( |sub| sub. at_height ( time) ) . collect ( ) )
562+ }
563+ x => x,
564+ } ;
565+ self . normalized ( )
566+ }
567+
549568 /// Count the number of public keys and keyhashes referenced in a policy.
550569 /// Duplicate keys will be double-counted.
551570 pub fn n_keys ( & self ) -> usize {
@@ -658,6 +677,7 @@ mod tests {
658677 )
659678 ) ;
660679 assert_eq ! ( policy. relative_timelocks( ) , vec![ 1000 ] ) ;
680+ assert_eq ! ( policy. absolute_timelocks( ) , vec![ ] ) ;
661681 assert_eq ! ( policy. clone( ) . at_age( 0 ) , Policy :: KeyHash ( "" . to_owned( ) ) ) ;
662682 assert_eq ! ( policy. clone( ) . at_age( 999 ) , Policy :: KeyHash ( "" . to_owned( ) ) ) ;
663683 assert_eq ! ( policy. clone( ) . at_age( 1000 ) , policy. clone( ) . normalized( ) ) ;
@@ -688,6 +708,17 @@ mod tests {
688708 policy. relative_timelocks( ) ,
689709 vec![ 1000 , 2000 , 10000 ] //sorted and dedup'd
690710 ) ;
711+
712+ let policy = StringPolicy :: from_str ( "after(1000)" ) . unwrap ( ) ;
713+ assert_eq ! ( policy, Policy :: After ( 1000 ) ) ;
714+ assert_eq ! ( policy. absolute_timelocks( ) , vec![ 1000 ] ) ;
715+ assert_eq ! ( policy. relative_timelocks( ) , vec![ ] ) ;
716+ assert_eq ! ( policy. clone( ) . at_height( 0 ) , Policy :: Unsatisfiable ) ;
717+ assert_eq ! ( policy. clone( ) . at_height( 999 ) , Policy :: Unsatisfiable ) ;
718+ assert_eq ! ( policy. clone( ) . at_height( 1000 ) , policy. clone( ) ) ;
719+ assert_eq ! ( policy. clone( ) . at_height( 10000 ) , policy. clone( ) ) ;
720+ assert_eq ! ( policy. n_keys( ) , 0 ) ;
721+ assert_eq ! ( policy. minimum_n_keys( ) , 0 ) ;
691722 }
692723
693724 #[ test]
0 commit comments