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