Skip to content

Commit b11b359

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 9d1d4c3 commit b11b359

File tree

1 file changed

+83
-67
lines changed

1 file changed

+83
-67
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 83 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,80 @@ macro_rules! maybe_break_monitor_err {
814814
}
815815
}
816816

817+
macro_rules! handle_chan_restoration_locked {
818+
($self: expr, $channel_lock: expr, $channel_state: expr, $channel_entry: expr,
819+
$raa: expr, $commitment_update: expr, $order: expr,
820+
$pending_forwards: expr, $pending_failures: expr, $funding_broadcastable: expr, $funding_locked: expr) => { {
821+
let mut htlc_forwards = Vec::new();
822+
let mut htlc_failures = Vec::new();
823+
let mut pending_events = Vec::new();
824+
825+
{
826+
if !$pending_forwards.is_empty() {
827+
htlc_forwards.push(($channel_entry.get().get_short_channel_id().expect("We can't have pending forwards before funding confirmation"),
828+
$channel_entry.get().get_funding_txo().unwrap(), $pending_forwards));
829+
}
830+
htlc_failures.append(&mut $pending_failures);
831+
832+
macro_rules! handle_cs { () => {
833+
if let Some(update) = $commitment_update {
834+
$channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
835+
node_id: $channel_entry.get().get_counterparty_node_id(),
836+
updates: update,
837+
});
838+
}
839+
} }
840+
macro_rules! handle_raa { () => {
841+
if let Some(revoke_and_ack) = $raa {
842+
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
843+
node_id: $channel_entry.get().get_counterparty_node_id(),
844+
msg: revoke_and_ack,
845+
});
846+
}
847+
} }
848+
match $order {
849+
RAACommitmentOrder::CommitmentFirst => {
850+
handle_cs!();
851+
handle_raa!();
852+
},
853+
RAACommitmentOrder::RevokeAndACKFirst => {
854+
handle_raa!();
855+
handle_cs!();
856+
},
857+
}
858+
if let Some(tx) = $funding_broadcastable {
859+
$self.tx_broadcaster.broadcast_transaction(&tx);
860+
}
861+
if let Some(msg) = $funding_locked {
862+
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
863+
node_id: $channel_entry.get().get_counterparty_node_id(),
864+
msg,
865+
});
866+
if let Some(announcement_sigs) = $self.get_announcement_sigs($channel_entry.get()) {
867+
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
868+
node_id: $channel_entry.get().get_counterparty_node_id(),
869+
msg: announcement_sigs,
870+
});
871+
}
872+
$channel_state.short_to_id.insert($channel_entry.get().get_short_channel_id().unwrap(), $channel_entry.get().channel_id());
873+
}
874+
}
875+
(htlc_forwards, htlc_failures, pending_events)
876+
} }
877+
}
878+
879+
macro_rules! post_handle_chan_restoration {
880+
($self: expr, $locked_res: expr) => { {
881+
let (mut htlc_forwards, mut htlc_failures, mut pending_events) = $locked_res;
882+
$self.pending_events.lock().unwrap().append(&mut pending_events);
883+
884+
for failure in htlc_failures.drain(..) {
885+
$self.fail_htlc_backwards_internal($self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
886+
}
887+
$self.forward_htlcs(&mut htlc_forwards[..]);
888+
} }
889+
}
890+
817891
impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<Signer, M, T, K, F, L>
818892
where M::Target: chain::Watch<Signer>,
819893
T::Target: BroadcasterInterface,
@@ -2436,79 +2510,21 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
24362510
pub fn channel_monitor_updated(&self, funding_txo: &OutPoint, highest_applied_update_id: u64) {
24372511
let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
24382512

2439-
let mut htlc_forwards = Vec::new();
2440-
let mut htlc_failures = Vec::new();
2441-
let mut pending_events = Vec::new();
2442-
2443-
{
2513+
let chan_restoration_res = {
24442514
let mut channel_lock = self.channel_state.lock().unwrap();
24452515
let channel_state = &mut *channel_lock;
2446-
let short_to_id = &mut channel_state.short_to_id;
2447-
let pending_msg_events = &mut channel_state.pending_msg_events;
2448-
let channel = match channel_state.by_id.get_mut(&funding_txo.to_channel_id()) {
2449-
Some(chan) => chan,
2450-
None => return,
2516+
let mut channel = match channel_state.by_id.entry(funding_txo.to_channel_id()) {
2517+
hash_map::Entry::Occupied(chan) => chan,
2518+
hash_map::Entry::Vacant(_) => return,
24512519
};
2452-
if !channel.is_awaiting_monitor_update() || channel.get_latest_monitor_update_id() != highest_applied_update_id {
2520+
if !channel.get().is_awaiting_monitor_update() || channel.get().get_latest_monitor_update_id() != highest_applied_update_id {
24532521
return;
24542522
}
24552523

2456-
let (raa, commitment_update, order, pending_forwards, mut pending_failures, funding_broadcastable, funding_locked) = channel.monitor_updating_restored(&self.logger);
2457-
if !pending_forwards.is_empty() {
2458-
htlc_forwards.push((channel.get_short_channel_id().expect("We can't have pending forwards before funding confirmation"), funding_txo.clone(), pending_forwards));
2459-
}
2460-
htlc_failures.append(&mut pending_failures);
2461-
2462-
macro_rules! handle_cs { () => {
2463-
if let Some(update) = commitment_update {
2464-
pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
2465-
node_id: channel.get_counterparty_node_id(),
2466-
updates: update,
2467-
});
2468-
}
2469-
} }
2470-
macro_rules! handle_raa { () => {
2471-
if let Some(revoke_and_ack) = raa {
2472-
pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
2473-
node_id: channel.get_counterparty_node_id(),
2474-
msg: revoke_and_ack,
2475-
});
2476-
}
2477-
} }
2478-
match order {
2479-
RAACommitmentOrder::CommitmentFirst => {
2480-
handle_cs!();
2481-
handle_raa!();
2482-
},
2483-
RAACommitmentOrder::RevokeAndACKFirst => {
2484-
handle_raa!();
2485-
handle_cs!();
2486-
},
2487-
}
2488-
if let Some(tx) = funding_broadcastable {
2489-
self.tx_broadcaster.broadcast_transaction(&tx);
2490-
}
2491-
if let Some(msg) = funding_locked {
2492-
pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
2493-
node_id: channel.get_counterparty_node_id(),
2494-
msg,
2495-
});
2496-
if let Some(announcement_sigs) = self.get_announcement_sigs(channel) {
2497-
pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
2498-
node_id: channel.get_counterparty_node_id(),
2499-
msg: announcement_sigs,
2500-
});
2501-
}
2502-
short_to_id.insert(channel.get_short_channel_id().unwrap(), channel.channel_id());
2503-
}
2504-
}
2505-
2506-
self.pending_events.lock().unwrap().append(&mut pending_events);
2507-
2508-
for failure in htlc_failures.drain(..) {
2509-
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
2510-
}
2511-
self.forward_htlcs(&mut htlc_forwards[..]);
2524+
let (raa, commitment_update, order, pending_forwards, mut pending_failures, funding_broadcastable, funding_locked) = channel.get_mut().monitor_updating_restored(&self.logger);
2525+
handle_chan_restoration_locked!(self, channel_lock, channel_state, channel, raa, commitment_update, order, pending_forwards, pending_failures, funding_broadcastable, funding_locked)
2526+
};
2527+
post_handle_chan_restoration!(self, chan_restoration_res);
25122528
}
25132529

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

0 commit comments

Comments
 (0)