Skip to content

Commit 00ddc3d

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 681f89a commit 00ddc3d

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
@@ -709,6 +709,83 @@ macro_rules! maybe_break_monitor_err {
709709
}
710710
}
711711

712+
macro_rules! handle_chan_restoration_locked {
713+
($self: expr, $channel_lock: expr, $channel_state: expr, $channel_entry: expr,
714+
$raa: expr, $commitment_update: expr, $order: expr,
715+
$pending_forwards: expr, $pending_failures: expr, $broadcast_safe: expr, $funding_locked: expr) => { {
716+
let mut htlc_forwards = Vec::new();
717+
let mut htlc_failures = Vec::new();
718+
let mut pending_events = Vec::new();
719+
720+
{
721+
if !$pending_forwards.is_empty() {
722+
htlc_forwards.push(($channel_entry.get().get_short_channel_id().expect("We can't have pending forwards before funding confirmation"),
723+
$channel_entry.get().get_funding_txo().unwrap(), $pending_forwards));
724+
}
725+
htlc_failures.append(&mut $pending_failures);
726+
727+
macro_rules! handle_cs { () => {
728+
if let Some(update) = $commitment_update {
729+
$channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
730+
node_id: $channel_entry.get().get_counterparty_node_id(),
731+
updates: update,
732+
});
733+
}
734+
} }
735+
macro_rules! handle_raa { () => {
736+
if let Some(revoke_and_ack) = $raa {
737+
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
738+
node_id: $channel_entry.get().get_counterparty_node_id(),
739+
msg: revoke_and_ack,
740+
});
741+
}
742+
} }
743+
match $order {
744+
RAACommitmentOrder::CommitmentFirst => {
745+
handle_cs!();
746+
handle_raa!();
747+
},
748+
RAACommitmentOrder::RevokeAndACKFirst => {
749+
handle_raa!();
750+
handle_cs!();
751+
},
752+
}
753+
if $broadcast_safe {
754+
pending_events.push(events::Event::FundingBroadcastSafe {
755+
funding_txo: $channel_entry.get().get_funding_txo().unwrap(),
756+
user_channel_id: $channel_entry.get().get_user_id(),
757+
});
758+
}
759+
if let Some(msg) = $funding_locked {
760+
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
761+
node_id: $channel_entry.get().get_counterparty_node_id(),
762+
msg,
763+
});
764+
if let Some(announcement_sigs) = $self.get_announcement_sigs($channel_entry.get()) {
765+
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
766+
node_id: $channel_entry.get().get_counterparty_node_id(),
767+
msg: announcement_sigs,
768+
});
769+
}
770+
$channel_state.short_to_id.insert($channel_entry.get().get_short_channel_id().unwrap(), $channel_entry.get().channel_id());
771+
}
772+
}
773+
(htlc_forwards, htlc_failures, pending_events)
774+
} }
775+
}
776+
777+
macro_rules! post_handle_chan_restoration {
778+
($self: expr, $locked_res: expr) => { {
779+
let (mut htlc_forwards, mut htlc_failures, mut pending_events) = $locked_res;
780+
$self.pending_events.lock().unwrap().append(&mut pending_events);
781+
782+
for failure in htlc_failures.drain(..) {
783+
$self.fail_htlc_backwards_internal($self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
784+
}
785+
$self.forward_htlcs(&mut htlc_forwards[..]);
786+
} }
787+
}
788+
712789
impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<ChanSigner, M, T, K, F, L>
713790
where M::Target: chain::Watch<Keys=ChanSigner>,
714791
T::Target: BroadcasterInterface,
@@ -2210,82 +2287,21 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
22102287
pub fn channel_monitor_updated(&self, funding_txo: &OutPoint, highest_applied_update_id: u64) {
22112288
let _consistency_lock = self.total_consistency_lock.read().unwrap();
22122289

2213-
let mut htlc_forwards = Vec::new();
2214-
let mut htlc_failures = Vec::new();
2215-
let mut pending_events = Vec::new();
2216-
2217-
{
2290+
let chan_restoration_res = {
22182291
let mut channel_lock = self.channel_state.lock().unwrap();
22192292
let channel_state = &mut *channel_lock;
2220-
let short_to_id = &mut channel_state.short_to_id;
2221-
let pending_msg_events = &mut channel_state.pending_msg_events;
2222-
let channel = match channel_state.by_id.get_mut(&funding_txo.to_channel_id()) {
2223-
Some(chan) => chan,
2224-
None => return,
2293+
let mut channel = match channel_state.by_id.entry(funding_txo.to_channel_id()) {
2294+
hash_map::Entry::Occupied(chan) => chan,
2295+
hash_map::Entry::Vacant(_) => return,
22252296
};
2226-
if !channel.is_awaiting_monitor_update() || channel.get_latest_monitor_update_id() != highest_applied_update_id {
2297+
if !channel.get().is_awaiting_monitor_update() || channel.get().get_latest_monitor_update_id() != highest_applied_update_id {
22272298
return;
22282299
}
22292300

2230-
let (raa, commitment_update, order, pending_forwards, mut pending_failures, needs_broadcast_safe, funding_locked) = channel.monitor_updating_restored(&self.logger);
2231-
if !pending_forwards.is_empty() {
2232-
htlc_forwards.push((channel.get_short_channel_id().expect("We can't have pending forwards before funding confirmation"), funding_txo.clone(), pending_forwards));
2233-
}
2234-
htlc_failures.append(&mut pending_failures);
2235-
2236-
macro_rules! handle_cs { () => {
2237-
if let Some(update) = commitment_update {
2238-
pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
2239-
node_id: channel.get_counterparty_node_id(),
2240-
updates: update,
2241-
});
2242-
}
2243-
} }
2244-
macro_rules! handle_raa { () => {
2245-
if let Some(revoke_and_ack) = raa {
2246-
pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
2247-
node_id: channel.get_counterparty_node_id(),
2248-
msg: revoke_and_ack,
2249-
});
2250-
}
2251-
} }
2252-
match order {
2253-
RAACommitmentOrder::CommitmentFirst => {
2254-
handle_cs!();
2255-
handle_raa!();
2256-
},
2257-
RAACommitmentOrder::RevokeAndACKFirst => {
2258-
handle_raa!();
2259-
handle_cs!();
2260-
},
2261-
}
2262-
if needs_broadcast_safe {
2263-
pending_events.push(events::Event::FundingBroadcastSafe {
2264-
funding_txo: channel.get_funding_txo().unwrap(),
2265-
user_channel_id: channel.get_user_id(),
2266-
});
2267-
}
2268-
if let Some(msg) = funding_locked {
2269-
pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
2270-
node_id: channel.get_counterparty_node_id(),
2271-
msg,
2272-
});
2273-
if let Some(announcement_sigs) = self.get_announcement_sigs(channel) {
2274-
pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
2275-
node_id: channel.get_counterparty_node_id(),
2276-
msg: announcement_sigs,
2277-
});
2278-
}
2279-
short_to_id.insert(channel.get_short_channel_id().unwrap(), channel.channel_id());
2280-
}
2281-
}
2282-
2283-
self.pending_events.lock().unwrap().append(&mut pending_events);
2284-
2285-
for failure in htlc_failures.drain(..) {
2286-
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
2287-
}
2288-
self.forward_htlcs(&mut htlc_forwards[..]);
2301+
let (raa, commitment_update, order, pending_forwards, mut pending_failures, needs_broadcast_safe, funding_locked) = channel.get_mut().monitor_updating_restored(&self.logger);
2302+
handle_chan_restoration_locked!(self, channel_lock, channel_state, channel, raa, commitment_update, order, pending_forwards, pending_failures, needs_broadcast_safe, funding_locked)
2303+
};
2304+
post_handle_chan_restoration!(self, chan_restoration_res);
22892305
}
22902306

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

0 commit comments

Comments
 (0)