@@ -662,6 +662,24 @@ where C::Target: chain::Filter,
662
662
}
663
663
}
664
664
}
665
+
666
+ pub fn prune_stale_channel_monitors ( & self , to_prune : Vec < OutPoint > ) {
667
+ let mut monitors = self . monitors . write ( ) . unwrap ( ) ;
668
+ for funding_txo in to_prune {
669
+ let channel_monitor = monitors. get ( & funding_txo) ;
670
+ if let Some ( channel_monitor) = channel_monitor {
671
+ if channel_monitor. monitor . is_stale ( ) {
672
+ log_info ! ( self . logger, "Pruning stale ChannelMonitor for
673
+ channel {}" , log_funding_info!( channel_monitor. monitor) ) ;
674
+ //TODO: save the channel monitor to disk in an archived namespace before removing it
675
+
676
+ self . persister . prune_persisted_channel ( funding_txo) ;
677
+ monitors. remove ( & funding_txo) ;
678
+ }
679
+ }
680
+
681
+ }
682
+ }
665
683
}
666
684
667
685
impl < ChannelSigner : WriteableEcdsaChannelSigner , C : Deref , T : Deref , F : Deref , L : Deref , P : Deref >
@@ -1114,4 +1132,27 @@ mod tests {
1114
1132
core:: mem:: drop( nodes) ;
1115
1133
} ) . is_err( ) ) ;
1116
1134
}
1135
+
1136
+ #[ test]
1137
+ fn prune_stale_channel_monitor ( ) {
1138
+ // Test that we can prune a ChannelMonitor that has no active channel.
1139
+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
1140
+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
1141
+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
1142
+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
1143
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
1144
+ // Get a route for later and rebalance the channel somewhat
1145
+ send_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 10_000_000 ) ;
1146
+ // First route a payment that we will claim on chain and give the recipient the preimage.
1147
+ let ( payment_preimage, payment_hash, ..) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 1_000_000 ) ;
1148
+ nodes[ 1 ] . node . claim_funds ( payment_preimage) ;
1149
+ expect_payment_claimed ! ( nodes[ 1 ] , payment_hash, 1_000_000 ) ;
1150
+ nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
1151
+ let binding = node_cfgs[ 1 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
1152
+ let monitors_b = binding. first ( ) . unwrap ( ) ;
1153
+ let outpoint = monitors_b. 0 . clone ( ) ;
1154
+ dbg ! ( & outpoint) ;
1155
+ // nodes[1].chain_monitor().unwrap().chain_monitor.prune_stale_channel_monitors(vec![outpoint]); // lock order problem
1156
+ assert ! ( false ) ;
1157
+ }
1117
1158
}
0 commit comments