Skip to content

Commit 638c915

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 a209df9 commit 638c915

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -788,30 +788,29 @@ macro_rules! maybe_break_monitor_err {
788788
macro_rules! handle_chan_restoration_locked {
789789
($self: expr, $channel_lock: expr, $channel_state: expr, $channel_entry: expr,
790790
$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();
791+
$pending_forwards: expr, $broadcast_safe: expr, $funding_locked: expr) => { {
792+
let mut htlc_forwards = None;
793+
let mut funding_broadcast_safe = None;
794+
let counterparty_node_id = $channel_entry.get().get_counterparty_node_id();
795795

796796
{
797797
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"),
798+
htlc_forwards = Some(($channel_entry.get().get_short_channel_id().expect("We can't have pending forwards before funding confirmation"),
799799
$channel_entry.get().get_funding_txo().unwrap(), $pending_forwards));
800800
}
801-
htlc_failures.append(&mut $pending_failures);
802801

803802
macro_rules! handle_cs { () => {
804803
if let Some(update) = $commitment_update {
805804
$channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
806-
node_id: $channel_entry.get().get_counterparty_node_id(),
805+
node_id: counterparty_node_id,
807806
updates: update,
808807
});
809808
}
810809
} }
811810
macro_rules! handle_raa { () => {
812811
if let Some(revoke_and_ack) = $raa {
813812
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
814-
node_id: $channel_entry.get().get_counterparty_node_id(),
813+
node_id: counterparty_node_id,
815814
msg: revoke_and_ack,
816815
});
817816
}
@@ -827,38 +826,40 @@ macro_rules! handle_chan_restoration_locked {
827826
},
828827
}
829828
if $broadcast_safe {
830-
pending_events.push(events::Event::FundingBroadcastSafe {
829+
funding_broadcast_safe = Some(events::Event::FundingBroadcastSafe {
831830
funding_txo: $channel_entry.get().get_funding_txo().unwrap(),
832831
user_channel_id: $channel_entry.get().get_user_id(),
833832
});
834833
}
835834
if let Some(msg) = $funding_locked {
836835
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
837-
node_id: $channel_entry.get().get_counterparty_node_id(),
836+
node_id: counterparty_node_id,
838837
msg,
839838
});
840839
if let Some(announcement_sigs) = $self.get_announcement_sigs($channel_entry.get()) {
841840
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
842-
node_id: $channel_entry.get().get_counterparty_node_id(),
841+
node_id: counterparty_node_id,
843842
msg: announcement_sigs,
844843
});
845844
}
846845
$channel_state.short_to_id.insert($channel_entry.get().get_short_channel_id().unwrap(), $channel_entry.get().channel_id());
847846
}
848847
}
849-
(htlc_forwards, htlc_failures, pending_events)
848+
(htlc_forwards, funding_broadcast_safe)
850849
} }
851850
}
852851

853852
macro_rules! post_handle_chan_restoration {
854853
($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);
854+
let (htlc_forwards, funding_broadcast_safe) = $locked_res;
857855

858-
for failure in htlc_failures.drain(..) {
859-
$self.fail_htlc_backwards_internal($self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
856+
if let Some(ev) = funding_broadcast_safe {
857+
$self.pending_events.lock().unwrap().push(ev);
858+
}
859+
860+
if let Some(forwards) = htlc_forwards {
861+
$self.forward_htlcs(&mut [forwards][..]);
860862
}
861-
$self.forward_htlcs(&mut htlc_forwards[..]);
862863
} }
863864
}
864865

@@ -2413,7 +2414,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
24132414
pub fn channel_monitor_updated(&self, funding_txo: &OutPoint, highest_applied_update_id: u64) {
24142415
let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
24152416

2416-
let chan_restoration_res = {
2417+
let (mut pending_failures, chan_restoration_res) = {
24172418
let mut channel_lock = self.channel_state.lock().unwrap();
24182419
let channel_state = &mut *channel_lock;
24192420
let mut channel = match channel_state.by_id.entry(funding_txo.to_channel_id()) {
@@ -2424,10 +2425,13 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
24242425
return;
24252426
}
24262427

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)
2428+
let (raa, commitment_update, order, pending_forwards, pending_failures, needs_broadcast_safe, funding_locked) = channel.get_mut().monitor_updating_restored(&self.logger);
2429+
(pending_failures, handle_chan_restoration_locked!(self, channel_lock, channel_state, channel, raa, commitment_update, order, pending_forwards, needs_broadcast_safe, funding_locked))
24292430
};
24302431
post_handle_chan_restoration!(self, chan_restoration_res);
2432+
for failure in pending_failures.drain(..) {
2433+
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
2434+
}
24312435
}
24322436

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

0 commit comments

Comments
 (0)