@@ -127,6 +127,56 @@ async fn channel_full_cycle_force_close_trusted_no_reserve() {
127127 . await ;
128128}
129129
130+ #[ tokio:: test( flavor = "multi_thread" , worker_threads = 1 ) ]
131+ async fn peer_removed_when_counterparty_force_closes_last_channel ( ) {
132+ // When we open a channel outbound, we persist the counterparty so the background
133+ // reconnection task can reach them. If the counterparty then force-closes what turns out
134+ // to be their last channel with us, the channel is terminal and there is nothing left for
135+ // `channel_reestablish` to recover, so the peer should be dropped from the store rather
136+ // than reconnected to forever.
137+ let ( bitcoind, electrsd) = setup_bitcoind_and_electrsd ( ) ;
138+ let chain_source = random_chain_source ( & bitcoind, & electrsd) ;
139+ let ( node_a, node_b) = setup_two_nodes ( & chain_source, false , true , false ) ;
140+
141+ let address_a = node_a. onchain_payment ( ) . new_address ( ) . unwrap ( ) ;
142+ let premine_amount_sat = 5_000_000 ;
143+ premine_and_distribute_funds (
144+ & bitcoind. client ,
145+ & electrsd. client ,
146+ vec ! [ address_a] ,
147+ Amount :: from_sat ( premine_amount_sat) ,
148+ )
149+ . await ;
150+ node_a. sync_wallets ( ) . unwrap ( ) ;
151+
152+ // node_a opens the channel, so node_a persists node_b in its peer store.
153+ open_channel ( & node_a, & node_b, 4_000_000 , false , & electrsd) . await ;
154+ generate_blocks_and_wait ( & bitcoind. client , & electrsd. client , 6 ) . await ;
155+ node_a. sync_wallets ( ) . unwrap ( ) ;
156+ node_b. sync_wallets ( ) . unwrap ( ) ;
157+
158+ let _user_channel_id_a = expect_channel_ready_event ! ( node_a, node_b. node_id( ) ) ;
159+ let user_channel_id_b = expect_channel_ready_event ! ( node_b, node_a. node_id( ) ) ;
160+
161+ assert ! (
162+ node_a. list_peers( ) . iter( ) . any( |p| p. node_id == node_b. node_id( ) && p. is_persisted) ,
163+ "node_a should persist node_b after opening a channel to it"
164+ ) ;
165+
166+ // The counterparty force-closes their last channel with us.
167+ node_b. force_close_channel ( & user_channel_id_b, node_a. node_id ( ) , None ) . unwrap ( ) ;
168+
169+ expect_event ! ( node_a, ChannelClosed ) ;
170+ expect_event ! ( node_b, ChannelClosed ) ;
171+
172+ // node_a should have dropped node_b from its peer store. We assert on `is_persisted` rather
173+ // than peer presence so a lingering transient TCP connection doesn't mask the removal.
174+ assert ! (
175+ !node_a. list_peers( ) . iter( ) . any( |p| p. node_id == node_b. node_id( ) && p. is_persisted) ,
176+ "node_a should drop node_b from its peer store after node_b force-closed the last channel"
177+ ) ;
178+ }
179+
130180#[ tokio:: test( flavor = "multi_thread" , worker_threads = 1 ) ]
131181async fn channel_full_cycle_0conf ( ) {
132182 let ( bitcoind, electrsd) = setup_bitcoind_and_electrsd ( ) ;
0 commit comments