@@ -1258,7 +1258,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1258
1258
pub fn close_channel ( & self , channel_id : & [ u8 ; 32 ] ) -> Result < ( ) , APIError > {
1259
1259
let _persistence_guard = PersistenceNotifierGuard :: notify_on_drop ( & self . total_consistency_lock , & self . persistence_notifier ) ;
1260
1260
1261
- let ( mut failed_htlcs, chan_option) = {
1261
+ let mut error_to_handle = None ;
1262
+ let ( counterparty_node_id, shutdown_msg, mut failed_htlcs, chan_option) = {
1262
1263
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1263
1264
let channel_state = & mut * channel_state_lock;
1264
1265
match channel_state. by_id . entry ( channel_id. clone ( ) ) {
@@ -1272,35 +1273,58 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1272
1273
}
1273
1274
} ;
1274
1275
let ( shutdown_msg, monitor_update, failed_htlcs) = chan_entry. get_mut ( ) . get_shutdown ( & self . keys_manager , & their_features) ?;
1275
- channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendShutdown {
1276
- node_id : counterparty_node_id,
1277
- msg : shutdown_msg
1278
- } ) ;
1276
+
1277
+ // Update the monitor with the shutdown script if necessary.
1279
1278
if let Some ( monitor_update) = monitor_update {
1280
- if let Err ( _ ) = self . chain_monitor . update_channel ( chan_entry. get ( ) . get_funding_txo ( ) . unwrap ( ) , monitor_update) {
1279
+ if let Err ( e ) = self . chain_monitor . update_channel ( chan_entry. get ( ) . get_funding_txo ( ) . unwrap ( ) , monitor_update) {
1281
1280
// TODO: How should this be handled?
1282
- unimplemented ! ( ) ;
1281
+ error_to_handle = Some ( handle_monitor_err ! ( self , e , channel_state . short_to_id , chan_entry . get_mut ( ) , RAACommitmentOrder :: CommitmentFirst , false , false , Vec :: new ( ) , Vec :: new ( ) , chan_entry . key ( ) ) ) ;
1283
1282
}
1284
1283
}
1285
- if chan_entry. get ( ) . is_shutdown ( ) {
1284
+
1285
+ // Clean up the channel mappings if necessary.
1286
+ let remove_chan_entry = {
1287
+ if chan_entry. get ( ) . is_shutdown ( ) { true }
1288
+ else if let Some ( ( _, is_permanent) ) = error_to_handle { is_permanent }
1289
+ else { false }
1290
+ } ;
1291
+ let chan_option = if remove_chan_entry {
1286
1292
if let Some ( short_id) = chan_entry. get ( ) . get_short_channel_id ( ) {
1287
1293
channel_state. short_to_id . remove ( & short_id) ;
1288
1294
}
1289
- ( failed_htlcs, Some ( chan_entry. remove_entry ( ) . 1 ) )
1290
- } else { ( failed_htlcs, None ) }
1295
+ Some ( chan_entry. remove_entry ( ) . 1 )
1296
+ } else { None } ;
1297
+
1298
+ ( counterparty_node_id, shutdown_msg, failed_htlcs, chan_option)
1291
1299
} ,
1292
1300
hash_map:: Entry :: Vacant ( _) => return Err ( APIError :: ChannelUnavailable { err : "No such channel" . to_owned ( ) } )
1293
1301
}
1294
1302
} ;
1303
+
1295
1304
for htlc_source in failed_htlcs. drain ( ..) {
1296
1305
self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , htlc_source. 0 , & htlc_source. 1 , HTLCFailReason :: Reason { failure_code : 0x4000 | 8 , data : Vec :: new ( ) } ) ;
1297
1306
}
1307
+
1308
+ // Handle any channel monitor update error, exiting early if it resulted in a force close.
1309
+ if let Some ( ( error, is_permanent) ) = error_to_handle {
1310
+ let _ = handle_error ! ( self , error, counterparty_node_id) ;
1311
+ if is_permanent {
1312
+ return Ok ( ( ) ) ;
1313
+ }
1314
+ }
1315
+
1316
+ // Send the shutdown message since any channel monitor update error would be temporary.
1317
+ let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1318
+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendShutdown {
1319
+ node_id : counterparty_node_id,
1320
+ msg : shutdown_msg
1321
+ } ) ;
1322
+
1298
1323
let chan_update = if let Some ( chan) = chan_option {
1299
1324
self . get_channel_update_for_broadcast ( & chan) . ok ( )
1300
1325
} else { None } ;
1301
1326
1302
1327
if let Some ( update) = chan_update {
1303
- let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1304
1328
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
1305
1329
msg : update
1306
1330
} ) ;
0 commit comments