@@ -1405,6 +1405,14 @@ macro_rules! handle_error {
1405
1405
}
1406
1406
}
1407
1407
1408
+ macro_rules! update_maps_on_chan_removal {
1409
+ ( $short_to_id: expr, $channel: expr) => {
1410
+ if let Some ( short_id) = $channel. get_short_channel_id( ) {
1411
+ $short_to_id. remove( & short_id) ;
1412
+ }
1413
+ }
1414
+ }
1415
+
1408
1416
/// Returns (boolean indicating if we should remove the Channel object from memory, a mapped error)
1409
1417
macro_rules! convert_chan_err {
1410
1418
( $self: ident, $err: expr, $short_to_id: expr, $channel: expr, $channel_id: expr) => {
@@ -1417,18 +1425,14 @@ macro_rules! convert_chan_err {
1417
1425
} ,
1418
1426
ChannelError :: Close ( msg) => {
1419
1427
log_error!( $self. logger, "Closing channel {} due to close-required error: {}" , log_bytes!( $channel_id[ ..] ) , msg) ;
1420
- if let Some ( short_id) = $channel. get_short_channel_id( ) {
1421
- $short_to_id. remove( & short_id) ;
1422
- }
1428
+ update_maps_on_chan_removal!( $short_to_id, $channel) ;
1423
1429
let shutdown_res = $channel. force_shutdown( true ) ;
1424
1430
( true , MsgHandleErrInternal :: from_finish_shutdown( msg, * $channel_id, $channel. get_user_id( ) ,
1425
1431
shutdown_res, $self. get_channel_update_for_broadcast( & $channel) . ok( ) ) )
1426
1432
} ,
1427
1433
ChannelError :: CloseDelayBroadcast ( msg) => {
1428
1434
log_error!( $self. logger, "Channel {} need to be shutdown but closing transactions not broadcast due to {}" , log_bytes!( $channel_id[ ..] ) , msg) ;
1429
- if let Some ( short_id) = $channel. get_short_channel_id( ) {
1430
- $short_to_id. remove( & short_id) ;
1431
- }
1435
+ update_maps_on_chan_removal!( $short_to_id, $channel) ;
1432
1436
let shutdown_res = $channel. force_shutdown( false ) ;
1433
1437
( true , MsgHandleErrInternal :: from_finish_shutdown( msg, * $channel_id, $channel. get_user_id( ) ,
1434
1438
shutdown_res, $self. get_channel_update_for_broadcast( & $channel) . ok( ) ) )
@@ -1471,9 +1475,7 @@ macro_rules! remove_channel {
1471
1475
( $channel_state: expr, $entry: expr) => {
1472
1476
{
1473
1477
let channel = $entry. remove_entry( ) . 1 ;
1474
- if let Some ( short_id) = channel. get_short_channel_id( ) {
1475
- $channel_state. short_to_id. remove( & short_id) ;
1476
- }
1478
+ update_maps_on_chan_removal!( $channel_state. short_to_id, channel) ;
1477
1479
channel
1478
1480
}
1479
1481
}
@@ -1484,9 +1486,7 @@ macro_rules! handle_monitor_err {
1484
1486
match $err {
1485
1487
ChannelMonitorUpdateErr :: PermanentFailure => {
1486
1488
log_error!( $self. logger, "Closing channel {} due to monitor update ChannelMonitorUpdateErr::PermanentFailure" , log_bytes!( $chan_id[ ..] ) ) ;
1487
- if let Some ( short_id) = $chan. get_short_channel_id( ) {
1488
- $short_to_id. remove( & short_id) ;
1489
- }
1489
+ update_maps_on_chan_removal!( $short_to_id, $chan) ;
1490
1490
// TODO: $failed_fails is dropped here, which will cause other channels to hit the
1491
1491
// chain in a confused state! We need to move them into the ChannelMonitor which
1492
1492
// will be responsible for failing backwards once things confirm on-chain.
@@ -2049,17 +2049,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2049
2049
return Err ( APIError :: ChannelUnavailable { err : "No such channel" . to_owned ( ) } ) ;
2050
2050
}
2051
2051
}
2052
- if let Some ( short_id) = chan. get ( ) . get_short_channel_id ( ) {
2053
- channel_state. short_to_id . remove ( & short_id) ;
2054
- }
2055
2052
if peer_node_id. is_some ( ) {
2056
2053
if let Some ( peer_msg) = peer_msg {
2057
2054
self . issue_channel_close_events ( chan. get ( ) , ClosureReason :: CounterpartyForceClosed { peer_msg : peer_msg. to_string ( ) } ) ;
2058
2055
}
2059
2056
} else {
2060
2057
self . issue_channel_close_events ( chan. get ( ) , ClosureReason :: HolderForceClosed ) ;
2061
2058
}
2062
- chan . remove_entry ( ) . 1
2059
+ remove_channel ! ( channel_state , chan )
2063
2060
} else {
2064
2061
return Err ( APIError :: ChannelUnavailable { err : "No such channel" . to_owned ( ) } ) ;
2065
2062
}
@@ -3206,12 +3203,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3206
3203
}
3207
3204
ChannelError :: Close ( msg) => {
3208
3205
log_trace ! ( self . logger, "Closing channel {} due to Close-required error: {}" , log_bytes!( chan. key( ) [ ..] ) , msg) ;
3209
- let ( channel_id, mut channel) = chan. remove_entry ( ) ;
3210
- if let Some ( short_id) = channel. get_short_channel_id ( ) {
3211
- channel_state. short_to_id . remove ( & short_id) ;
3212
- }
3206
+ let mut channel = remove_channel ! ( channel_state, chan) ;
3213
3207
// ChannelClosed event is generated by handle_error for us.
3214
- Err ( MsgHandleErrInternal :: from_finish_shutdown ( msg, channel_id, channel. get_user_id ( ) , channel. force_shutdown ( true ) , self . get_channel_update_for_broadcast ( & channel) . ok ( ) ) )
3208
+ 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 ( ) ) )
3215
3209
} ,
3216
3210
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" ) ; }
3217
3211
} ;
@@ -4479,10 +4473,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
4479
4473
// also implies there are no pending HTLCs left on the channel, so we can
4480
4474
// fully delete it from tracking (the channel monitor is still around to
4481
4475
// watch for old state broadcasts)!
4482
- if let Some ( short_id) = chan_entry. get ( ) . get_short_channel_id ( ) {
4483
- channel_state. short_to_id . remove ( & short_id) ;
4484
- }
4485
- ( tx, Some ( chan_entry. remove_entry ( ) . 1 ) )
4476
+ ( tx, Some ( remove_channel ! ( channel_state, chan_entry) ) )
4486
4477
} else { ( tx, None ) }
4487
4478
} ,
4488
4479
hash_map:: Entry :: Vacant ( _) => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" . to_owned ( ) , msg. channel_id ) )
@@ -4920,12 +4911,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
4920
4911
let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
4921
4912
let channel_state = & mut * channel_lock;
4922
4913
let by_id = & mut channel_state. by_id ;
4923
- let short_to_id = & mut channel_state. short_to_id ;
4924
4914
let pending_msg_events = & mut channel_state. pending_msg_events ;
4925
- if let Some ( mut chan) = by_id. remove ( & funding_outpoint. to_channel_id ( ) ) {
4926
- if let Some ( short_id) = chan. get_short_channel_id ( ) {
4927
- short_to_id. remove ( & short_id) ;
4928
- }
4915
+ if let hash_map:: Entry :: Occupied ( chan_entry) = by_id. entry ( funding_outpoint. to_channel_id ( ) ) {
4916
+ let mut chan = remove_channel ! ( channel_state, chan_entry) ;
4929
4917
failed_channels. push ( chan. force_shutdown ( false ) ) ;
4930
4918
if let Ok ( update) = self . get_channel_update_for_broadcast ( & chan) {
4931
4919
pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
@@ -5054,10 +5042,6 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
5054
5042
if let Some ( tx) = tx_opt {
5055
5043
// We're done with this channel. We got a closing_signed and sent back
5056
5044
// a closing_signed with a closing transaction to broadcast.
5057
- if let Some ( short_id) = chan. get_short_channel_id ( ) {
5058
- short_to_id. remove ( & short_id) ;
5059
- }
5060
-
5061
5045
if let Ok ( update) = self . get_channel_update_for_broadcast ( & chan) {
5062
5046
pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
5063
5047
msg : update
@@ -5068,6 +5052,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
5068
5052
5069
5053
log_info ! ( self . logger, "Broadcasting {}" , log_tx!( tx) ) ;
5070
5054
self . tx_broadcaster . broadcast_transaction ( & tx) ;
5055
+ update_maps_on_chan_removal ! ( short_to_id, chan) ;
5071
5056
false
5072
5057
} else { true }
5073
5058
} ,
@@ -5587,9 +5572,7 @@ where
5587
5572
}
5588
5573
}
5589
5574
} else if let Err ( reason) = res {
5590
- if let Some ( short_id) = channel. get_short_channel_id ( ) {
5591
- short_to_id. remove ( & short_id) ;
5592
- }
5575
+ update_maps_on_chan_removal ! ( short_to_id, channel) ;
5593
5576
// It looks like our counterparty went on-chain or funding transaction was
5594
5577
// reorged out of the main chain. Close the channel.
5595
5578
failed_channels. push ( channel. force_shutdown ( true ) ) ;
@@ -5785,9 +5768,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
5785
5768
log_debug ! ( self . logger, "Failing all channels with {} due to no_connection_possible" , log_pubkey!( counterparty_node_id) ) ;
5786
5769
channel_state. by_id . retain ( |_, chan| {
5787
5770
if chan. get_counterparty_node_id ( ) == * counterparty_node_id {
5788
- if let Some ( short_id) = chan. get_short_channel_id ( ) {
5789
- short_to_id. remove ( & short_id) ;
5790
- }
5771
+ update_maps_on_chan_removal ! ( short_to_id, chan) ;
5791
5772
failed_channels. push ( chan. force_shutdown ( true ) ) ;
5792
5773
if let Ok ( update) = self . get_channel_update_for_broadcast ( & chan) {
5793
5774
pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
@@ -5806,9 +5787,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
5806
5787
if chan. get_counterparty_node_id ( ) == * counterparty_node_id {
5807
5788
chan. remove_uncommitted_htlcs_and_mark_paused ( & self . logger ) ;
5808
5789
if chan. is_shutdown ( ) {
5809
- if let Some ( short_id) = chan. get_short_channel_id ( ) {
5810
- short_to_id. remove ( & short_id) ;
5811
- }
5790
+ update_maps_on_chan_removal ! ( short_to_id, chan) ;
5812
5791
self . issue_channel_close_events ( chan, ClosureReason :: DisconnectedPeer ) ;
5813
5792
return false ;
5814
5793
} else {
0 commit comments