Skip to content

Commit a209df9

Browse files
committed
Move channel restoration after monitor update to a two-part macro
The channel restoration code in channel monitor updating and peer reconnection both do incredibly similar things, and there is little reason to have them be separate. Sadly because they require holding a lock with a reference to elements in the lock, its not practical to make them utility functions, so instead we introduce a two-step macro here which will eventually be used for both. Because we still support pre-NLL Rust, the macro has to be in two parts - one which runs with the channel_state lock, and one which does not.
1 parent c8cb994 commit a209df9

File tree

1 file changed

+86
-70
lines changed

1 file changed

+86
-70
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 86 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,83 @@ macro_rules! maybe_break_monitor_err {
785785
}
786786
}
787787

788+
macro_rules! handle_chan_restoration_locked {
789+
($self: expr, $channel_lock: expr, $channel_state: expr, $channel_entry: expr,
790+
$raa: expr, $commitment_update: expr, $order: expr,
791+
$pending_forwards: expr, $pending_failures: expr, $broadcast_safe: expr, $funding_locked: expr) => { {
792+
let mut htlc_forwards = Vec::new();
793+
let mut htlc_failures = Vec::new();
794+
let mut pending_events = Vec::new();
795+
796+
{
797+
if !$pending_forwards.is_empty() {
798+
htlc_forwards.push(($channel_entry.get().get_short_channel_id().expect("We can't have pending forwards before funding confirmation"),
799+
$channel_entry.get().get_funding_txo().unwrap(), $pending_forwards));
800+
}
801+
htlc_failures.append(&mut $pending_failures);
802+
803+
macro_rules! handle_cs { () => {
804+
if let Some(update) = $commitment_update {
805+
$channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
806+
node_id: $channel_entry.get().get_counterparty_node_id(),
807+
updates: update,
808+
});
809+
}
810+
} }
811+
macro_rules! handle_raa { () => {
812+
if let Some(revoke_and_ack) = $raa {
813+
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
814+
node_id: $channel_entry.get().get_counterparty_node_id(),
815+
msg: revoke_and_ack,
816+
});
817+
}
818+
} }
819+
match $order {
820+
RAACommitmentOrder::CommitmentFirst => {
821+
handle_cs!();
822+
handle_raa!();
823+
},
824+
RAACommitmentOrder::RevokeAndACKFirst => {
825+
handle_raa!();
826+
handle_cs!();
827+
},
828+
}
829+
if $broadcast_safe {
830+
pending_events.push(events::Event::FundingBroadcastSafe {
831+
funding_txo: $channel_entry.get().get_funding_txo().unwrap(),
832+
user_channel_id: $channel_entry.get().get_user_id(),
833+
});
834+
}
835+
if let Some(msg) = $funding_locked {
836+
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
837+
node_id: $channel_entry.get().get_counterparty_node_id(),
838+
msg,
839+
});
840+
if let Some(announcement_sigs) = $self.get_announcement_sigs($channel_entry.get()) {
841+
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
842+
node_id: $channel_entry.get().get_counterparty_node_id(),
843+
msg: announcement_sigs,
844+
});
845+
}
846+
$channel_state.short_to_id.insert($channel_entry.get().get_short_channel_id().unwrap(), $channel_entry.get().channel_id());
847+
}
848+
}
849+
(htlc_forwards, htlc_failures, pending_events)
850+
} }
851+
}
852+
853+
macro_rules! post_handle_chan_restoration {
854+
($self: expr, $locked_res: expr) => { {
855+
let (mut htlc_forwards, mut htlc_failures, mut pending_events) = $locked_res;
856+
$self.pending_events.lock().unwrap().append(&mut pending_events);
857+
858+
for failure in htlc_failures.drain(..) {
859+
$self.fail_htlc_backwards_internal($self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
860+
}
861+
$self.forward_htlcs(&mut htlc_forwards[..]);
862+
} }
863+
}
864+
788865
impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<Signer, M, T, K, F, L>
789866
where M::Target: chain::Watch<Signer>,
790867
T::Target: BroadcasterInterface,
@@ -2336,82 +2413,21 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
23362413
pub fn channel_monitor_updated(&self, funding_txo: &OutPoint, highest_applied_update_id: u64) {
23372414
let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
23382415

2339-
let mut htlc_forwards = Vec::new();
2340-
let mut htlc_failures = Vec::new();
2341-
let mut pending_events = Vec::new();
2342-
2343-
{
2416+
let chan_restoration_res = {
23442417
let mut channel_lock = self.channel_state.lock().unwrap();
23452418
let channel_state = &mut *channel_lock;
2346-
let short_to_id = &mut channel_state.short_to_id;
2347-
let pending_msg_events = &mut channel_state.pending_msg_events;
2348-
let channel = match channel_state.by_id.get_mut(&funding_txo.to_channel_id()) {
2349-
Some(chan) => chan,
2350-
None => return,
2419+
let mut channel = match channel_state.by_id.entry(funding_txo.to_channel_id()) {
2420+
hash_map::Entry::Occupied(chan) => chan,
2421+
hash_map::Entry::Vacant(_) => return,
23512422
};
2352-
if !channel.is_awaiting_monitor_update() || channel.get_latest_monitor_update_id() != highest_applied_update_id {
2423+
if !channel.get().is_awaiting_monitor_update() || channel.get().get_latest_monitor_update_id() != highest_applied_update_id {
23532424
return;
23542425
}
23552426

2356-
let (raa, commitment_update, order, pending_forwards, mut pending_failures, needs_broadcast_safe, funding_locked) = channel.monitor_updating_restored(&self.logger);
2357-
if !pending_forwards.is_empty() {
2358-
htlc_forwards.push((channel.get_short_channel_id().expect("We can't have pending forwards before funding confirmation"), funding_txo.clone(), pending_forwards));
2359-
}
2360-
htlc_failures.append(&mut pending_failures);
2361-
2362-
macro_rules! handle_cs { () => {
2363-
if let Some(update) = commitment_update {
2364-
pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
2365-
node_id: channel.get_counterparty_node_id(),
2366-
updates: update,
2367-
});
2368-
}
2369-
} }
2370-
macro_rules! handle_raa { () => {
2371-
if let Some(revoke_and_ack) = raa {
2372-
pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
2373-
node_id: channel.get_counterparty_node_id(),
2374-
msg: revoke_and_ack,
2375-
});
2376-
}
2377-
} }
2378-
match order {
2379-
RAACommitmentOrder::CommitmentFirst => {
2380-
handle_cs!();
2381-
handle_raa!();
2382-
},
2383-
RAACommitmentOrder::RevokeAndACKFirst => {
2384-
handle_raa!();
2385-
handle_cs!();
2386-
},
2387-
}
2388-
if needs_broadcast_safe {
2389-
pending_events.push(events::Event::FundingBroadcastSafe {
2390-
funding_txo: channel.get_funding_txo().unwrap(),
2391-
user_channel_id: channel.get_user_id(),
2392-
});
2393-
}
2394-
if let Some(msg) = funding_locked {
2395-
pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
2396-
node_id: channel.get_counterparty_node_id(),
2397-
msg,
2398-
});
2399-
if let Some(announcement_sigs) = self.get_announcement_sigs(channel) {
2400-
pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
2401-
node_id: channel.get_counterparty_node_id(),
2402-
msg: announcement_sigs,
2403-
});
2404-
}
2405-
short_to_id.insert(channel.get_short_channel_id().unwrap(), channel.channel_id());
2406-
}
2407-
}
2408-
2409-
self.pending_events.lock().unwrap().append(&mut pending_events);
2410-
2411-
for failure in htlc_failures.drain(..) {
2412-
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
2413-
}
2414-
self.forward_htlcs(&mut htlc_forwards[..]);
2427+
let (raa, commitment_update, order, pending_forwards, mut pending_failures, needs_broadcast_safe, funding_locked) = channel.get_mut().monitor_updating_restored(&self.logger);
2428+
handle_chan_restoration_locked!(self, channel_lock, channel_state, channel, raa, commitment_update, order, pending_forwards, pending_failures, needs_broadcast_safe, funding_locked)
2429+
};
2430+
post_handle_chan_restoration!(self, chan_restoration_res);
24152431
}
24162432

24172433
fn internal_open_channel(&self, counterparty_node_id: &PublicKey, their_features: InitFeatures, msg: &msgs::OpenChannel) -> Result<(), MsgHandleErrInternal> {

0 commit comments

Comments
 (0)