@@ -1146,27 +1146,66 @@ impl Node {
11461146 }
11471147
11481148 /// Close a previously opened channel.
1149+ ///
1150+ /// If `force` is set to `true`, we will force-close the channel, potentially broadcasting our
1151+ /// latest state. Note that in contrast to cooperative closure, force-closing will have the
1152+ /// channel funds time-locked, i.e., they will only be available after the counterparty had
1153+ /// time to contest our claim. Force-closing channels also more costly in terms of on-chain
1154+ /// fees. So cooperative closure should always be preferred (and tried first).
1155+ ///
1156+ /// Broadcasting the closing transactions will be omitted for Anchor channels if we trust the
1157+ /// counterparty to broadcast for us (see [`AnchorChannelsConfig::trusted_peers_no_reserve`]
1158+ /// for more information).
11491159 pub fn close_channel (
1150- & self , user_channel_id : & UserChannelId , counterparty_node_id : PublicKey ,
1160+ & self , user_channel_id : & UserChannelId , counterparty_node_id : PublicKey , force : bool ,
11511161 ) -> Result < ( ) , Error > {
11521162 let open_channels =
11531163 self . channel_manager . list_channels_with_counterparty ( & counterparty_node_id) ;
11541164 if let Some ( channel_details) =
11551165 open_channels. iter ( ) . find ( |c| c. user_channel_id == user_channel_id. 0 )
11561166 {
1157- match self
1158- . channel_manager
1159- . close_channel ( & channel_details. channel_id , & counterparty_node_id)
1160- {
1161- Ok ( _) => {
1162- // Check if this was the last open channel, if so, forget the peer.
1163- if open_channels. len ( ) == 1 {
1164- self . peer_store . remove_peer ( & counterparty_node_id) ?;
1165- }
1166- Ok ( ( ) )
1167- } ,
1168- Err ( _) => Err ( Error :: ChannelClosingFailed ) ,
1167+ if force {
1168+ if self . config . anchor_channels_config . as_ref ( ) . map_or ( false , |acc| {
1169+ acc. trusted_peers_no_reserve . contains ( & counterparty_node_id)
1170+ } ) {
1171+ self . channel_manager
1172+ . force_close_without_broadcasting_txn (
1173+ & channel_details. channel_id ,
1174+ & counterparty_node_id,
1175+ )
1176+ . map_err ( |e| {
1177+ log_error ! (
1178+ self . logger,
1179+ "Failed to force-close channel to trusted peer: {:?}" ,
1180+ e
1181+ ) ;
1182+ Error :: ChannelClosingFailed
1183+ } ) ?;
1184+ } else {
1185+ self . channel_manager
1186+ . force_close_broadcasting_latest_txn (
1187+ & channel_details. channel_id ,
1188+ & counterparty_node_id,
1189+ )
1190+ . map_err ( |e| {
1191+ log_error ! ( self . logger, "Failed to force-close channel: {:?}" , e) ;
1192+ Error :: ChannelClosingFailed
1193+ } ) ?;
1194+ }
1195+ } else {
1196+ self . channel_manager
1197+ . close_channel ( & channel_details. channel_id , & counterparty_node_id)
1198+ . map_err ( |e| {
1199+ log_error ! ( self . logger, "Failed to close channel: {:?}" , e) ;
1200+ Error :: ChannelClosingFailed
1201+ } ) ?;
11691202 }
1203+
1204+ // Check if this was the last open channel, if so, forget the peer.
1205+ if open_channels. len ( ) == 1 {
1206+ self . peer_store . remove_peer ( & counterparty_node_id) ?;
1207+ }
1208+ Ok ( ( ) )
11701209 } else {
11711210 Ok ( ( ) )
11721211 }
0 commit comments