@@ -1385,6 +1385,14 @@ macro_rules! handle_error {
1385
1385
}
1386
1386
}
1387
1387
1388
+ macro_rules! update_maps_on_chan_removal {
1389
+ ( $short_to_id: expr, $channel: expr) => {
1390
+ if let Some ( short_id) = $channel. get_short_channel_id( ) {
1391
+ $short_to_id. remove( & short_id) ;
1392
+ }
1393
+ }
1394
+ }
1395
+
1388
1396
/// Returns (boolean indicating if we should remove the Channel object from memory, a mapped error)
1389
1397
macro_rules! convert_chan_err {
1390
1398
( $self: ident, $err: expr, $short_to_id: expr, $channel: expr, $channel_id: expr) => {
@@ -1397,18 +1405,14 @@ macro_rules! convert_chan_err {
1397
1405
} ,
1398
1406
ChannelError :: Close ( msg) => {
1399
1407
log_error!( $self. logger, "Closing channel {} due to close-required error: {}" , log_bytes!( $channel_id[ ..] ) , msg) ;
1400
- if let Some ( short_id) = $channel. get_short_channel_id( ) {
1401
- $short_to_id. remove( & short_id) ;
1402
- }
1408
+ update_maps_on_chan_removal!( $short_to_id, $channel) ;
1403
1409
let shutdown_res = $channel. force_shutdown( true ) ;
1404
1410
( true , MsgHandleErrInternal :: from_finish_shutdown( msg, * $channel_id, $channel. get_user_id( ) ,
1405
1411
shutdown_res, $self. get_channel_update_for_broadcast( & $channel) . ok( ) ) )
1406
1412
} ,
1407
1413
ChannelError :: CloseDelayBroadcast ( msg) => {
1408
1414
log_error!( $self. logger, "Channel {} need to be shutdown but closing transactions not broadcast due to {}" , log_bytes!( $channel_id[ ..] ) , msg) ;
1409
- if let Some ( short_id) = $channel. get_short_channel_id( ) {
1410
- $short_to_id. remove( & short_id) ;
1411
- }
1415
+ update_maps_on_chan_removal!( $short_to_id, $channel) ;
1412
1416
let shutdown_res = $channel. force_shutdown( false ) ;
1413
1417
( true , MsgHandleErrInternal :: from_finish_shutdown( msg, * $channel_id, $channel. get_user_id( ) ,
1414
1418
shutdown_res, $self. get_channel_update_for_broadcast( & $channel) . ok( ) ) )
@@ -1451,9 +1455,7 @@ macro_rules! remove_channel {
1451
1455
( $channel_state: expr, $entry: expr) => {
1452
1456
{
1453
1457
let channel = $entry. remove_entry( ) . 1 ;
1454
- if let Some ( short_id) = channel. get_short_channel_id( ) {
1455
- $channel_state. short_to_id. remove( & short_id) ;
1456
- }
1458
+ update_maps_on_chan_removal!( $channel_state. short_to_id, channel) ;
1457
1459
channel
1458
1460
}
1459
1461
}
@@ -2029,17 +2031,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2029
2031
return Err ( APIError :: ChannelUnavailable { err : "No such channel" . to_owned ( ) } ) ;
2030
2032
}
2031
2033
}
2032
- if let Some ( short_id) = chan. get ( ) . get_short_channel_id ( ) {
2033
- channel_state. short_to_id . remove ( & short_id) ;
2034
- }
2035
2034
if peer_node_id. is_some ( ) {
2036
2035
if let Some ( peer_msg) = peer_msg {
2037
2036
self . issue_channel_close_events ( chan. get ( ) , ClosureReason :: CounterpartyForceClosed { peer_msg : peer_msg. to_string ( ) } ) ;
2038
2037
}
2039
2038
} else {
2040
2039
self . issue_channel_close_events ( chan. get ( ) , ClosureReason :: HolderForceClosed ) ;
2041
2040
}
2042
- chan . remove_entry ( ) . 1
2041
+ remove_channel ! ( channel_state , chan )
2043
2042
} else {
2044
2043
return Err ( APIError :: ChannelUnavailable { err : "No such channel" . to_owned ( ) } ) ;
2045
2044
}
@@ -3180,12 +3179,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3180
3179
}
3181
3180
ChannelError :: Close ( msg) => {
3182
3181
log_trace ! ( self . logger, "Closing channel {} due to Close-required error: {}" , log_bytes!( chan. key( ) [ ..] ) , msg) ;
3183
- let ( channel_id, mut channel) = chan. remove_entry ( ) ;
3184
- if let Some ( short_id) = channel. get_short_channel_id ( ) {
3185
- channel_state. short_to_id . remove ( & short_id) ;
3186
- }
3182
+ let mut channel = remove_channel ! ( channel_state, chan) ;
3187
3183
// ChannelClosed event is generated by handle_error for us.
3188
- Err ( MsgHandleErrInternal :: from_finish_shutdown ( msg, channel_id, channel. get_user_id ( ) , channel. force_shutdown ( true ) , self . get_channel_update_for_broadcast ( & channel) . ok ( ) ) )
3184
+ Err ( MsgHandleErrInternal :: from_finish_shutdown ( msg, channel . channel_id ( ) , channel. get_user_id ( ) , channel. force_shutdown ( true ) , self . get_channel_update_for_broadcast ( & channel) . ok ( ) ) )
3189
3185
} ,
3190
3186
ChannelError :: CloseDelayBroadcast ( _) => { panic ! ( "Wait is only generated on receipt of channel_reestablish, which is handled by try_chan_entry, we don't bother to support it here" ) ; }
3191
3187
} ;
@@ -4446,10 +4442,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
4446
4442
// also implies there are no pending HTLCs left on the channel, so we can
4447
4443
// fully delete it from tracking (the channel monitor is still around to
4448
4444
// watch for old state broadcasts)!
4449
- if let Some ( short_id) = chan_entry. get ( ) . get_short_channel_id ( ) {
4450
- channel_state. short_to_id . remove ( & short_id) ;
4451
- }
4452
- ( tx, Some ( chan_entry. remove_entry ( ) . 1 ) )
4445
+ ( tx, Some ( remove_channel ! ( channel_state, chan_entry) ) )
4453
4446
} else { ( tx, None ) }
4454
4447
} ,
4455
4448
hash_map:: Entry :: Vacant ( _) => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" . to_owned ( ) , msg. channel_id ) )
@@ -4885,12 +4878,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
4885
4878
let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
4886
4879
let channel_state = & mut * channel_lock;
4887
4880
let by_id = & mut channel_state. by_id ;
4888
- let short_to_id = & mut channel_state. short_to_id ;
4889
4881
let pending_msg_events = & mut channel_state. pending_msg_events ;
4890
- if let Some ( mut chan) = by_id. remove ( & funding_outpoint. to_channel_id ( ) ) {
4891
- if let Some ( short_id) = chan. get_short_channel_id ( ) {
4892
- short_to_id. remove ( & short_id) ;
4893
- }
4882
+ if let hash_map:: Entry :: Occupied ( chan_entry) = by_id. entry ( funding_outpoint. to_channel_id ( ) ) {
4883
+ let mut chan = remove_channel ! ( channel_state, chan_entry) ;
4894
4884
failed_channels. push ( chan. force_shutdown ( false ) ) ;
4895
4885
if let Ok ( update) = self . get_channel_update_for_broadcast ( & chan) {
4896
4886
pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
@@ -5019,10 +5009,6 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
5019
5009
if let Some ( tx) = tx_opt {
5020
5010
// We're done with this channel. We got a closing_signed and sent back
5021
5011
// a closing_signed with a closing transaction to broadcast.
5022
- if let Some ( short_id) = chan. get_short_channel_id ( ) {
5023
- short_to_id. remove ( & short_id) ;
5024
- }
5025
-
5026
5012
if let Ok ( update) = self . get_channel_update_for_broadcast ( & chan) {
5027
5013
pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
5028
5014
msg : update
@@ -5033,6 +5019,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
5033
5019
5034
5020
log_info ! ( self . logger, "Broadcasting {}" , log_tx!( tx) ) ;
5035
5021
self . tx_broadcaster . broadcast_transaction ( & tx) ;
5022
+ update_maps_on_chan_removal ! ( short_to_id, chan) ;
5036
5023
false
5037
5024
} else { true }
5038
5025
} ,
@@ -5550,9 +5537,7 @@ where
5550
5537
}
5551
5538
}
5552
5539
} else if let Err ( reason) = res {
5553
- if let Some ( short_id) = channel. get_short_channel_id ( ) {
5554
- short_to_id. remove ( & short_id) ;
5555
- }
5540
+ update_maps_on_chan_removal ! ( short_to_id, channel) ;
5556
5541
// It looks like our counterparty went on-chain or funding transaction was
5557
5542
// reorged out of the main chain. Close the channel.
5558
5543
failed_channels. push ( channel. force_shutdown ( true ) ) ;
@@ -5748,9 +5733,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
5748
5733
log_debug ! ( self . logger, "Failing all channels with {} due to no_connection_possible" , log_pubkey!( counterparty_node_id) ) ;
5749
5734
channel_state. by_id . retain ( |_, chan| {
5750
5735
if chan. get_counterparty_node_id ( ) == * counterparty_node_id {
5751
- if let Some ( short_id) = chan. get_short_channel_id ( ) {
5752
- short_to_id. remove ( & short_id) ;
5753
- }
5736
+ update_maps_on_chan_removal ! ( short_to_id, chan) ;
5754
5737
failed_channels. push ( chan. force_shutdown ( true ) ) ;
5755
5738
if let Ok ( update) = self . get_channel_update_for_broadcast ( & chan) {
5756
5739
pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
@@ -5769,9 +5752,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
5769
5752
if chan. get_counterparty_node_id ( ) == * counterparty_node_id {
5770
5753
chan. remove_uncommitted_htlcs_and_mark_paused ( & self . logger ) ;
5771
5754
if chan. is_shutdown ( ) {
5772
- if let Some ( short_id) = chan. get_short_channel_id ( ) {
5773
- short_to_id. remove ( & short_id) ;
5774
- }
5755
+ update_maps_on_chan_removal ! ( short_to_id, chan) ;
5775
5756
self . issue_channel_close_events ( chan, ClosureReason :: DisconnectedPeer ) ;
5776
5757
return false ;
5777
5758
} else {
0 commit comments