Skip to content

Commit 98ad1d5

Browse files
TheBlueMatttnull
authored andcommitted
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 cf37b70 commit 98ad1d5

File tree

1 file changed

+22
-43
lines changed

1 file changed

+22
-43
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,14 @@ macro_rules! handle_error {
14051405
}
14061406
}
14071407

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+
14081416
/// Returns (boolean indicating if we should remove the Channel object from memory, a mapped error)
14091417
macro_rules! convert_chan_err {
14101418
($self: ident, $err: expr, $short_to_id: expr, $channel: expr, $channel_id: expr) => {
@@ -1417,18 +1425,14 @@ macro_rules! convert_chan_err {
14171425
},
14181426
ChannelError::Close(msg) => {
14191427
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);
14231429
let shutdown_res = $channel.force_shutdown(true);
14241430
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, $channel.get_user_id(),
14251431
shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
14261432
},
14271433
ChannelError::CloseDelayBroadcast(msg) => {
14281434
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);
14321436
let shutdown_res = $channel.force_shutdown(false);
14331437
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, $channel.get_user_id(),
14341438
shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
@@ -1471,9 +1475,7 @@ macro_rules! remove_channel {
14711475
($channel_state: expr, $entry: expr) => {
14721476
{
14731477
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);
14771479
channel
14781480
}
14791481
}
@@ -1484,9 +1486,7 @@ macro_rules! handle_monitor_err {
14841486
match $err {
14851487
ChannelMonitorUpdateErr::PermanentFailure => {
14861488
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);
14901490
// TODO: $failed_fails is dropped here, which will cause other channels to hit the
14911491
// chain in a confused state! We need to move them into the ChannelMonitor which
14921492
// 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
20492049
return Err(APIError::ChannelUnavailable{err: "No such channel".to_owned()});
20502050
}
20512051
}
2052-
if let Some(short_id) = chan.get().get_short_channel_id() {
2053-
channel_state.short_to_id.remove(&short_id);
2054-
}
20552052
if peer_node_id.is_some() {
20562053
if let Some(peer_msg) = peer_msg {
20572054
self.issue_channel_close_events(chan.get(),ClosureReason::CounterpartyForceClosed { peer_msg: peer_msg.to_string() });
20582055
}
20592056
} else {
20602057
self.issue_channel_close_events(chan.get(),ClosureReason::HolderForceClosed);
20612058
}
2062-
chan.remove_entry().1
2059+
remove_channel!(channel_state, chan)
20632060
} else {
20642061
return Err(APIError::ChannelUnavailable{err: "No such channel".to_owned()});
20652062
}
@@ -3206,12 +3203,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32063203
}
32073204
ChannelError::Close(msg) => {
32083205
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);
32133207
// 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()))
32153209
},
32163210
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"); }
32173211
};
@@ -4479,10 +4473,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
44794473
// also implies there are no pending HTLCs left on the channel, so we can
44804474
// fully delete it from tracking (the channel monitor is still around to
44814475
// 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)))
44864477
} else { (tx, None) }
44874478
},
44884479
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
49204911
let mut channel_lock = self.channel_state.lock().unwrap();
49214912
let channel_state = &mut *channel_lock;
49224913
let by_id = &mut channel_state.by_id;
4923-
let short_to_id = &mut channel_state.short_to_id;
49244914
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);
49294917
failed_channels.push(chan.force_shutdown(false));
49304918
if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
49314919
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
50545042
if let Some(tx) = tx_opt {
50555043
// We're done with this channel. We got a closing_signed and sent back
50565044
// 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-
50615045
if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
50625046
pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
50635047
msg: update
@@ -5068,6 +5052,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
50685052

50695053
log_info!(self.logger, "Broadcasting {}", log_tx!(tx));
50705054
self.tx_broadcaster.broadcast_transaction(&tx);
5055+
update_maps_on_chan_removal!(short_to_id, chan);
50715056
false
50725057
} else { true }
50735058
},
@@ -5587,9 +5572,7 @@ where
55875572
}
55885573
}
55895574
} 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);
55935576
// It looks like our counterparty went on-chain or funding transaction was
55945577
// reorged out of the main chain. Close the channel.
55955578
failed_channels.push(channel.force_shutdown(true));
@@ -5785,9 +5768,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
57855768
log_debug!(self.logger, "Failing all channels with {} due to no_connection_possible", log_pubkey!(counterparty_node_id));
57865769
channel_state.by_id.retain(|_, chan| {
57875770
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);
57915772
failed_channels.push(chan.force_shutdown(true));
57925773
if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
57935774
pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
@@ -5806,9 +5787,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
58065787
if chan.get_counterparty_node_id() == *counterparty_node_id {
58075788
chan.remove_uncommitted_htlcs_and_mark_paused(&self.logger);
58085789
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);
58125791
self.issue_channel_close_events(chan, ClosureReason::DisconnectedPeer);
58135792
return false;
58145793
} else {

0 commit comments

Comments
 (0)