@@ -934,6 +934,9 @@ pub(crate) struct ChannelMonitorImpl<Signer: WriteableEcdsaChannelSigner> {
934
934
/// Ordering of tuple data: (their_per_commitment_point, feerate_per_kw, to_broadcaster_sats,
935
935
/// to_countersignatory_sats)
936
936
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 > ,
937
940
}
938
941
939
942
/// Transaction outputs to watch for on-chain spends.
@@ -1327,6 +1330,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
1327
1330
best_block,
1328
1331
counterparty_node_id : Some ( counterparty_node_id) ,
1329
1332
initial_counterparty_commitment_info : None ,
1333
+ latest_stale_tip : None ,
1330
1334
} )
1331
1335
}
1332
1336
@@ -1855,8 +1859,28 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
1855
1859
spendable_outputs
1856
1860
}
1857
1861
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
+
1858
1873
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
+ }
1860
1884
}
1861
1885
1862
1886
#[ cfg( test) ]
@@ -4725,6 +4749,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
4725
4749
best_block,
4726
4750
counterparty_node_id,
4727
4751
initial_counterparty_commitment_info,
4752
+ latest_stale_tip : None ,
4728
4753
} ) ) )
4729
4754
}
4730
4755
}
0 commit comments