@@ -1176,30 +1176,84 @@ impl Node {
1176
1176
}
1177
1177
1178
1178
/// Close a previously opened channel.
1179
+ ///
1180
+ /// Will attempt to close a channel coopertively. If this fails, users might need to resort to
1181
+ /// [`Node::force_close_channel`].
1179
1182
pub fn close_channel (
1180
1183
& self , user_channel_id : & UserChannelId , counterparty_node_id : PublicKey ,
1184
+ ) -> Result < ( ) , Error > {
1185
+ self . close_channel_internal ( user_channel_id, counterparty_node_id, false )
1186
+ }
1187
+
1188
+ /// Force-close a previously opened channel.
1189
+ ///
1190
+ /// Will force-close the channel, potentially broadcasting our latest state. Note that in
1191
+ /// contrast to cooperative closure, force-closing will have the channel funds time-locked,
1192
+ /// i.e., they will only be available after the counterparty had time to contest our claim.
1193
+ /// Force-closing channels also more costly in terms of on-chain fees. So cooperative closure
1194
+ /// should always be preferred (and tried first).
1195
+ ///
1196
+ /// Broadcasting the closing transactions will be omitted for Anchor channels if we trust the
1197
+ /// counterparty to broadcast for us (see [`AnchorChannelsConfig::trusted_peers_no_reserve`]
1198
+ /// for more information).
1199
+ pub fn force_close_channel (
1200
+ & self , user_channel_id : & UserChannelId , counterparty_node_id : PublicKey ,
1201
+ ) -> Result < ( ) , Error > {
1202
+ self . close_channel_internal ( user_channel_id, counterparty_node_id, true )
1203
+ }
1204
+
1205
+ fn close_channel_internal (
1206
+ & self , user_channel_id : & UserChannelId , counterparty_node_id : PublicKey , force : bool ,
1181
1207
) -> Result < ( ) , Error > {
1182
1208
let open_channels =
1183
1209
self . channel_manager . list_channels_with_counterparty ( & counterparty_node_id) ;
1184
1210
if let Some ( channel_details) =
1185
1211
open_channels. iter ( ) . find ( |c| c. user_channel_id == user_channel_id. 0 )
1186
1212
{
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 ) ,
1213
+ if force {
1214
+ if self . config . anchor_channels_config . as_ref ( ) . map_or ( false , |acc| {
1215
+ acc. trusted_peers_no_reserve . contains ( & counterparty_node_id)
1216
+ } ) {
1217
+ self . channel_manager
1218
+ . force_close_without_broadcasting_txn (
1219
+ & channel_details. channel_id ,
1220
+ & counterparty_node_id,
1221
+ )
1222
+ . map_err ( |e| {
1223
+ log_error ! (
1224
+ self . logger,
1225
+ "Failed to force-close channel to trusted peer: {:?}" ,
1226
+ e
1227
+ ) ;
1228
+ Error :: ChannelClosingFailed
1229
+ } ) ?;
1230
+ } else {
1231
+ self . channel_manager
1232
+ . force_close_broadcasting_latest_txn (
1233
+ & channel_details. channel_id ,
1234
+ & counterparty_node_id,
1235
+ )
1236
+ . map_err ( |e| {
1237
+ log_error ! ( self . logger, "Failed to force-close channel: {:?}" , e) ;
1238
+ Error :: ChannelClosingFailed
1239
+ } ) ?;
1240
+ }
1241
+ } else {
1242
+ self . channel_manager
1243
+ . close_channel ( & channel_details. channel_id , & counterparty_node_id)
1244
+ . map_err ( |e| {
1245
+ log_error ! ( self . logger, "Failed to close channel: {:?}" , e) ;
1246
+ Error :: ChannelClosingFailed
1247
+ } ) ?;
1248
+ }
1249
+
1250
+ // Check if this was the last open channel, if so, forget the peer.
1251
+ if open_channels. len ( ) == 1 {
1252
+ self . peer_store . remove_peer ( & counterparty_node_id) ?;
1199
1253
}
1200
- } else {
1201
- Ok ( ( ) )
1202
1254
}
1255
+
1256
+ Ok ( ( ) )
1203
1257
}
1204
1258
1205
1259
/// Update the config for a previously opened channel.
0 commit comments