Skip to content

Commit e0d9d3b

Browse files
committed
Clean up channel updating macro somewhat
This mostly swaps some Vecs that can only ever contain one element for Options.
1 parent b11b359 commit e0d9d3b

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -817,30 +817,28 @@ macro_rules! maybe_break_monitor_err {
817817
macro_rules! handle_chan_restoration_locked {
818818
($self: expr, $channel_lock: expr, $channel_state: expr, $channel_entry: expr,
819819
$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();
820+
$pending_forwards: expr, $funding_broadcastable: expr, $funding_locked: expr) => { {
821+
let mut htlc_forwards = None;
822+
let counterparty_node_id = $channel_entry.get().get_counterparty_node_id();
824823

825824
{
826825
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"),
826+
htlc_forwards = Some(($channel_entry.get().get_short_channel_id().expect("We can't have pending forwards before funding confirmation"),
828827
$channel_entry.get().get_funding_txo().unwrap(), $pending_forwards));
829828
}
830-
htlc_failures.append(&mut $pending_failures);
831829

832830
macro_rules! handle_cs { () => {
833831
if let Some(update) = $commitment_update {
834832
$channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
835-
node_id: $channel_entry.get().get_counterparty_node_id(),
833+
node_id: counterparty_node_id,
836834
updates: update,
837835
});
838836
}
839837
} }
840838
macro_rules! handle_raa { () => {
841839
if let Some(revoke_and_ack) = $raa {
842840
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
843-
node_id: $channel_entry.get().get_counterparty_node_id(),
841+
node_id: counterparty_node_id,
844842
msg: revoke_and_ack,
845843
});
846844
}
@@ -860,31 +858,29 @@ macro_rules! handle_chan_restoration_locked {
860858
}
861859
if let Some(msg) = $funding_locked {
862860
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
863-
node_id: $channel_entry.get().get_counterparty_node_id(),
861+
node_id: counterparty_node_id,
864862
msg,
865863
});
866864
if let Some(announcement_sigs) = $self.get_announcement_sigs($channel_entry.get()) {
867865
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
868-
node_id: $channel_entry.get().get_counterparty_node_id(),
866+
node_id: counterparty_node_id,
869867
msg: announcement_sigs,
870868
});
871869
}
872870
$channel_state.short_to_id.insert($channel_entry.get().get_short_channel_id().unwrap(), $channel_entry.get().channel_id());
873871
}
874872
}
875-
(htlc_forwards, htlc_failures, pending_events)
873+
htlc_forwards
876874
} }
877875
}
878876

879877
macro_rules! post_handle_chan_restoration {
880878
($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);
879+
let htlc_forwards = $locked_res;
883880

884-
for failure in htlc_failures.drain(..) {
885-
$self.fail_htlc_backwards_internal($self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
881+
if let Some(forwards) = htlc_forwards {
882+
$self.forward_htlcs(&mut [forwards][..]);
886883
}
887-
$self.forward_htlcs(&mut htlc_forwards[..]);
888884
} }
889885
}
890886

@@ -2510,7 +2506,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
25102506
pub fn channel_monitor_updated(&self, funding_txo: &OutPoint, highest_applied_update_id: u64) {
25112507
let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
25122508

2513-
let chan_restoration_res = {
2509+
let (mut pending_failures, chan_restoration_res) = {
25142510
let mut channel_lock = self.channel_state.lock().unwrap();
25152511
let channel_state = &mut *channel_lock;
25162512
let mut channel = match channel_state.by_id.entry(funding_txo.to_channel_id()) {
@@ -2521,10 +2517,13 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
25212517
return;
25222518
}
25232519

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)
2520+
let (raa, commitment_update, order, pending_forwards, pending_failures, funding_broadcastable, funding_locked) = channel.get_mut().monitor_updating_restored(&self.logger);
2521+
(pending_failures, handle_chan_restoration_locked!(self, channel_lock, channel_state, channel, raa, commitment_update, order, pending_forwards, funding_broadcastable, funding_locked))
25262522
};
25272523
post_handle_chan_restoration!(self, chan_restoration_res);
2524+
for failure in pending_failures.drain(..) {
2525+
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
2526+
}
25282527
}
25292528

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

0 commit comments

Comments
 (0)