@@ -934,6 +934,9 @@ pub(crate) struct ChannelMonitorImpl<Signer: WriteableEcdsaChannelSigner> {
934934 /// Ordering of tuple data: (their_per_commitment_point, feerate_per_kw, to_broadcaster_sats,
935935 /// to_countersignatory_sats)
936936 initial_counterparty_commitment_info : Option < ( PublicKey , u32 , u64 , u64 ) > ,
937+
938+ /// The latest block height we've seen at the time of checking for stale channels.
939+ latest_stale_tip : Option < u32 > ,
937940}
938941
939942/// Transaction outputs to watch for on-chain spends.
@@ -1327,6 +1330,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
13271330 best_block,
13281331 counterparty_node_id : Some ( counterparty_node_id) ,
13291332 initial_counterparty_commitment_info : None ,
1333+ latest_stale_tip : None ,
13301334 } )
13311335 }
13321336
@@ -1855,8 +1859,28 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
18551859 spendable_outputs
18561860 }
18571861
1862+ fn latest_stale_tip ( & self ) -> Option < u32 > {
1863+ let inner = self . inner . lock ( ) . unwrap ( ) ;
1864+ inner. latest_stale_tip
1865+ }
1866+
1867+ fn set_latest_stale_tip ( & self , latest_stale_tip : Option < u32 > ) {
1868+ let mut inner = self . inner . lock ( ) . unwrap ( ) ;
1869+ inner. latest_stale_tip = latest_stale_tip;
1870+ }
1871+
1872+
18581873 pub ( crate ) fn is_stale ( & self ) -> bool {
1859- self . get_claimable_balances ( ) . is_empty ( )
1874+ if let Some ( latest_stale_tip) = self . latest_stale_tip ( ) {
1875+ let is_below_threshold = self . current_best_block ( ) . height > latest_stale_tip;
1876+ let best_block = self . current_best_block ( ) ;
1877+ self . set_latest_stale_tip ( Some ( best_block. height ) ) ;
1878+ is_below_threshold && self . get_claimable_balances ( ) . is_empty ( )
1879+ } else {
1880+ let best_block = self . current_best_block ( ) ;
1881+ self . set_latest_stale_tip ( Some ( best_block. height ) ) ;
1882+ false
1883+ }
18601884 }
18611885
18621886 #[ cfg( test) ]
@@ -4725,6 +4749,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
47254749 best_block,
47264750 counterparty_node_id,
47274751 initial_counterparty_commitment_info,
4752+ latest_stale_tip : None ,
47284753 } ) ) )
47294754 }
47304755}
0 commit comments