Skip to content

Commit 828a6ea

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 638d48b commit 828a6ea

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
@@ -1385,6 +1385,14 @@ macro_rules! handle_error {
13851385
}
13861386
}
13871387

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+
13881396
/// Returns (boolean indicating if we should remove the Channel object from memory, a mapped error)
13891397
macro_rules! convert_chan_err {
13901398
($self: ident, $err: expr, $short_to_id: expr, $channel: expr, $channel_id: expr) => {
@@ -1397,18 +1405,14 @@ macro_rules! convert_chan_err {
13971405
},
13981406
ChannelError::Close(msg) => {
13991407
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);
14031409
let shutdown_res = $channel.force_shutdown(true);
14041410
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, $channel.get_user_id(),
14051411
shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
14061412
},
14071413
ChannelError::CloseDelayBroadcast(msg) => {
14081414
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);
14121416
let shutdown_res = $channel.force_shutdown(false);
14131417
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, $channel.get_user_id(),
14141418
shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
@@ -1451,9 +1455,7 @@ macro_rules! remove_channel {
14511455
($channel_state: expr, $entry: expr) => {
14521456
{
14531457
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);
14571459
channel
14581460
}
14591461
}
@@ -2029,17 +2031,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
20292031
return Err(APIError::ChannelUnavailable{err: "No such channel".to_owned()});
20302032
}
20312033
}
2032-
if let Some(short_id) = chan.get().get_short_channel_id() {
2033-
channel_state.short_to_id.remove(&short_id);
2034-
}
20352034
if peer_node_id.is_some() {
20362035
if let Some(peer_msg) = peer_msg {
20372036
self.issue_channel_close_events(chan.get(),ClosureReason::CounterpartyForceClosed { peer_msg: peer_msg.to_string() });
20382037
}
20392038
} else {
20402039
self.issue_channel_close_events(chan.get(),ClosureReason::HolderForceClosed);
20412040
}
2042-
chan.remove_entry().1
2041+
remove_channel!(channel_state, chan)
20432042
} else {
20442043
return Err(APIError::ChannelUnavailable{err: "No such channel".to_owned()});
20452044
}
@@ -3180,12 +3179,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31803179
}
31813180
ChannelError::Close(msg) => {
31823181
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);
31873183
// 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()))
31893185
},
31903186
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"); }
31913187
};
@@ -4446,10 +4442,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
44464442
// also implies there are no pending HTLCs left on the channel, so we can
44474443
// fully delete it from tracking (the channel monitor is still around to
44484444
// 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)))
44534446
} else { (tx, None) }
44544447
},
44554448
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
48854878
let mut channel_lock = self.channel_state.lock().unwrap();
48864879
let channel_state = &mut *channel_lock;
48874880
let by_id = &mut channel_state.by_id;
4888-
let short_to_id = &mut channel_state.short_to_id;
48894881
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);
48944884
failed_channels.push(chan.force_shutdown(false));
48954885
if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
48964886
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
50195009
if let Some(tx) = tx_opt {
50205010
// We're done with this channel. We got a closing_signed and sent back
50215011
// 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-
50265012
if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
50275013
pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
50285014
msg: update
@@ -5033,6 +5019,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
50335019

50345020
log_info!(self.logger, "Broadcasting {}", log_tx!(tx));
50355021
self.tx_broadcaster.broadcast_transaction(&tx);
5022+
update_maps_on_chan_removal!(short_to_id, chan);
50365023
false
50375024
} else { true }
50385025
},
@@ -5550,9 +5537,7 @@ where
55505537
}
55515538
}
55525539
} 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);
55565541
// It looks like our counterparty went on-chain or funding transaction was
55575542
// reorged out of the main chain. Close the channel.
55585543
failed_channels.push(channel.force_shutdown(true));
@@ -5748,9 +5733,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
57485733
log_debug!(self.logger, "Failing all channels with {} due to no_connection_possible", log_pubkey!(counterparty_node_id));
57495734
channel_state.by_id.retain(|_, chan| {
57505735
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);
57545737
failed_channels.push(chan.force_shutdown(true));
57555738
if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
57565739
pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
@@ -5769,9 +5752,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
57695752
if chan.get_counterparty_node_id() == *counterparty_node_id {
57705753
chan.remove_uncommitted_htlcs_and_mark_paused(&self.logger);
57715754
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);
57755756
self.issue_channel_close_events(chan, ClosureReason::DisconnectedPeer);
57765757
return false;
57775758
} else {

0 commit comments

Comments
 (0)