Skip to content

Commit ec9fefe

Browse files
committed
Handle short_to_id state updates on channel closure via macros
This avoids needing to update channel closure code in many places as we add multiple SCIDs for each channel and have to track them.
1 parent 0b94399 commit ec9fefe

File tree

1 file changed

+21
-40
lines changed

1 file changed

+21
-40
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,14 @@ macro_rules! handle_error {
13871387
}
13881388
}
13891389

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+
13901398
/// Returns (boolean indicating if we should remove the Channel object from memory, a mapped error)
13911399
macro_rules! convert_chan_err {
13921400
($self: ident, $err: expr, $short_to_id: expr, $channel: expr, $channel_id: expr) => {
@@ -1399,18 +1407,14 @@ macro_rules! convert_chan_err {
13991407
},
14001408
ChannelError::Close(msg) => {
14011409
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);
14051411
let shutdown_res = $channel.force_shutdown(true);
14061412
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, $channel.get_user_id(),
14071413
shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
14081414
},
14091415
ChannelError::CloseDelayBroadcast(msg) => {
14101416
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);
14141418
let shutdown_res = $channel.force_shutdown(false);
14151419
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, $channel.get_user_id(),
14161420
shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
@@ -1453,9 +1457,7 @@ macro_rules! remove_channel {
14531457
($channel_state: expr, $entry: expr) => {
14541458
{
14551459
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);
14591461
channel
14601462
}
14611463
}
@@ -2031,17 +2033,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
20312033
return Err(APIError::ChannelUnavailable{err: "No such channel".to_owned()});
20322034
}
20332035
}
2034-
if let Some(short_id) = chan.get().get_short_channel_id() {
2035-
channel_state.short_to_id.remove(&short_id);
2036-
}
20372036
if peer_node_id.is_some() {
20382037
if let Some(peer_msg) = peer_msg {
20392038
self.issue_channel_close_events(chan.get(),ClosureReason::CounterpartyForceClosed { peer_msg: peer_msg.to_string() });
20402039
}
20412040
} else {
20422041
self.issue_channel_close_events(chan.get(),ClosureReason::HolderForceClosed);
20432042
}
2044-
chan.remove_entry().1
2043+
remove_channel!(channel_state, chan)
20452044
} else {
20462045
return Err(APIError::ChannelUnavailable{err: "No such channel".to_owned()});
20472046
}
@@ -3182,12 +3181,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31823181
}
31833182
ChannelError::Close(msg) => {
31843183
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);
31893185
// 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()))
31913187
},
31923188
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"); }
31933189
};
@@ -4448,10 +4444,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
44484444
// also implies there are no pending HTLCs left on the channel, so we can
44494445
// fully delete it from tracking (the channel monitor is still around to
44504446
// 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)))
44554448
} else { (tx, None) }
44564449
},
44574450
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
48874880
let mut channel_lock = self.channel_state.lock().unwrap();
48884881
let channel_state = &mut *channel_lock;
48894882
let by_id = &mut channel_state.by_id;
4890-
let short_to_id = &mut channel_state.short_to_id;
48914883
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);
48964886
failed_channels.push(chan.force_shutdown(false));
48974887
if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
48984888
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
50215011
if let Some(tx) = tx_opt {
50225012
// We're done with this channel. We got a closing_signed and sent back
50235013
// 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-
50285014
if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
50295015
pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
50305016
msg: update
@@ -5035,6 +5021,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
50355021

50365022
log_info!(self.logger, "Broadcasting {}", log_tx!(tx));
50375023
self.tx_broadcaster.broadcast_transaction(&tx);
5024+
update_maps_on_chan_removal!(short_to_id, chan);
50385025
false
50395026
} else { true }
50405027
},
@@ -5552,9 +5539,7 @@ where
55525539
}
55535540
}
55545541
} 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);
55585543
// It looks like our counterparty went on-chain or funding transaction was
55595544
// reorged out of the main chain. Close the channel.
55605545
failed_channels.push(channel.force_shutdown(true));
@@ -5750,9 +5735,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
57505735
log_debug!(self.logger, "Failing all channels with {} due to no_connection_possible", log_pubkey!(counterparty_node_id));
57515736
channel_state.by_id.retain(|_, chan| {
57525737
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);
57565739
failed_channels.push(chan.force_shutdown(true));
57575740
if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
57585741
pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
@@ -5771,9 +5754,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
57715754
if chan.get_counterparty_node_id() == *counterparty_node_id {
57725755
chan.remove_uncommitted_htlcs_and_mark_paused(&self.logger);
57735756
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);
57775758
self.issue_channel_close_events(chan, ClosureReason::DisconnectedPeer);
57785759
return false;
57795760
} else {

0 commit comments

Comments
 (0)