@@ -1176,27 +1176,66 @@ impl Node {
11761176 }
11771177
11781178 /// Close a previously opened channel.
1179+ ///
1180+ /// If `force` is set to `true`, we will force-close the channel, potentially broadcasting our
1181+ /// latest state. Note that in contrast to cooperative closure, force-closing will have the
1182+ /// channel funds time-locked, i.e., they will only be available after the counterparty had
1183+ /// time to contest our claim. Force-closing channels also more costly in terms of on-chain
1184+ /// fees. So cooperative closure should always be preferred (and tried first).
1185+ ///
1186+ /// Broadcasting the closing transactions will be omitted for Anchor channels if we trust the
1187+ /// counterparty to broadcast for us (see [`AnchorChannelsConfig::trusted_peers_no_reserve`]
1188+ /// for more information).
11791189 pub fn close_channel (
1180- & self , user_channel_id : & UserChannelId , counterparty_node_id : PublicKey ,
1190+ & self , user_channel_id : & UserChannelId , counterparty_node_id : PublicKey , force : bool ,
11811191 ) -> Result < ( ) , Error > {
11821192 let open_channels =
11831193 self . channel_manager . list_channels_with_counterparty ( & counterparty_node_id) ;
11841194 if let Some ( channel_details) =
11851195 open_channels. iter ( ) . find ( |c| c. user_channel_id == user_channel_id. 0 )
11861196 {
1187- match self
1188- . channel_manager
1189- . close_channel ( & channel_details. channel_id , & counterparty_node_id)
1190- {
1191- Ok ( _) => {
1192- // Check if this was the last open channel, if so, forget the peer.
1193- if open_channels. len ( ) == 1 {
1194- self . peer_store . remove_peer ( & counterparty_node_id) ?;
1195- }
1196- Ok ( ( ) )
1197- } ,
1198- Err ( _) => Err ( Error :: ChannelClosingFailed ) ,
1197+ if force {
1198+ if self . config . anchor_channels_config . as_ref ( ) . map_or ( false , |acc| {
1199+ acc. trusted_peers_no_reserve . contains ( & counterparty_node_id)
1200+ } ) {
1201+ self . channel_manager
1202+ . force_close_without_broadcasting_txn (
1203+ & channel_details. channel_id ,
1204+ & counterparty_node_id,
1205+ )
1206+ . map_err ( |e| {
1207+ log_error ! (
1208+ self . logger,
1209+ "Failed to force-close channel to trusted peer: {:?}" ,
1210+ e
1211+ ) ;
1212+ Error :: ChannelClosingFailed
1213+ } ) ?;
1214+ } else {
1215+ self . channel_manager
1216+ . force_close_broadcasting_latest_txn (
1217+ & channel_details. channel_id ,
1218+ & counterparty_node_id,
1219+ )
1220+ . map_err ( |e| {
1221+ log_error ! ( self . logger, "Failed to force-close channel: {:?}" , e) ;
1222+ Error :: ChannelClosingFailed
1223+ } ) ?;
1224+ }
1225+ } else {
1226+ self . channel_manager
1227+ . close_channel ( & channel_details. channel_id , & counterparty_node_id)
1228+ . map_err ( |e| {
1229+ log_error ! ( self . logger, "Failed to close channel: {:?}" , e) ;
1230+ Error :: ChannelClosingFailed
1231+ } ) ?;
11991232 }
1233+
1234+ // Check if this was the last open channel, if so, forget the peer.
1235+ if open_channels. len ( ) == 1 {
1236+ self . peer_store . remove_peer ( & counterparty_node_id) ?;
1237+ }
1238+ Ok ( ( ) )
12001239 } else {
12011240 Ok ( ( ) )
12021241 }
0 commit comments