Skip to content

Commit 4a838d7

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 00ddc3d commit 4a838d7

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -712,30 +712,29 @@ macro_rules! maybe_break_monitor_err {
712712
macro_rules! handle_chan_restoration_locked {
713713
($self: expr, $channel_lock: expr, $channel_state: expr, $channel_entry: expr,
714714
$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();
715+
$pending_forwards: expr, $broadcast_safe: expr, $funding_locked: expr) => { {
716+
let mut htlc_forwards = None;
717+
let mut funding_broadcast_safe = None;
718+
let counterparty_node_id = $channel_entry.get().get_counterparty_node_id();
719719

720720
{
721721
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"),
722+
htlc_forwards = Some(($channel_entry.get().get_short_channel_id().expect("We can't have pending forwards before funding confirmation"),
723723
$channel_entry.get().get_funding_txo().unwrap(), $pending_forwards));
724724
}
725-
htlc_failures.append(&mut $pending_failures);
726725

727726
macro_rules! handle_cs { () => {
728727
if let Some(update) = $commitment_update {
729728
$channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
730-
node_id: $channel_entry.get().get_counterparty_node_id(),
729+
node_id: counterparty_node_id,
731730
updates: update,
732731
});
733732
}
734733
} }
735734
macro_rules! handle_raa { () => {
736735
if let Some(revoke_and_ack) = $raa {
737736
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
738-
node_id: $channel_entry.get().get_counterparty_node_id(),
737+
node_id: counterparty_node_id,
739738
msg: revoke_and_ack,
740739
});
741740
}
@@ -751,38 +750,43 @@ macro_rules! handle_chan_restoration_locked {
751750
},
752751
}
753752
if $broadcast_safe {
754-
pending_events.push(events::Event::FundingBroadcastSafe {
753+
funding_broadcast_safe = Some(events::Event::FundingBroadcastSafe {
755754
funding_txo: $channel_entry.get().get_funding_txo().unwrap(),
756755
user_channel_id: $channel_entry.get().get_user_id(),
757756
});
758757
}
759758
if let Some(msg) = $funding_locked {
760759
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
761-
node_id: $channel_entry.get().get_counterparty_node_id(),
760+
node_id: counterparty_node_id,
762761
msg,
763762
});
764763
if let Some(announcement_sigs) = $self.get_announcement_sigs($channel_entry.get()) {
765764
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
766-
node_id: $channel_entry.get().get_counterparty_node_id(),
765+
node_id: counterparty_node_id,
767766
msg: announcement_sigs,
768767
});
769768
}
770769
$channel_state.short_to_id.insert($channel_entry.get().get_short_channel_id().unwrap(), $channel_entry.get().channel_id());
771770
}
772771
}
773-
(htlc_forwards, htlc_failures, pending_events)
772+
(htlc_forwards, funding_broadcast_safe)
774773
} }
775774
}
776775

777776
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);
777+
($self: expr, $locked_res: expr, $pending_failures: expr) => { {
778+
let (htlc_forwards, funding_broadcast_safe) = $locked_res;
781779

782-
for failure in htlc_failures.drain(..) {
780+
if let Some(ev) = funding_broadcast_safe {
781+
$self.pending_events.lock().unwrap().push(ev);
782+
}
783+
784+
for failure in $pending_failures.drain(..) {
783785
$self.fail_htlc_backwards_internal($self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
784786
}
785-
$self.forward_htlcs(&mut htlc_forwards[..]);
787+
if let Some(forwards) = htlc_forwards {
788+
$self.forward_htlcs(&mut [forwards][..]);
789+
}
786790
} }
787791
}
788792

@@ -2287,7 +2291,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
22872291
pub fn channel_monitor_updated(&self, funding_txo: &OutPoint, highest_applied_update_id: u64) {
22882292
let _consistency_lock = self.total_consistency_lock.read().unwrap();
22892293

2290-
let chan_restoration_res = {
2294+
let (mut pending_failures, chan_restoration_res) = {
22912295
let mut channel_lock = self.channel_state.lock().unwrap();
22922296
let channel_state = &mut *channel_lock;
22932297
let mut channel = match channel_state.by_id.entry(funding_txo.to_channel_id()) {
@@ -2298,10 +2302,10 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
22982302
return;
22992303
}
23002304

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)
2305+
let (raa, commitment_update, order, pending_forwards, pending_failures, needs_broadcast_safe, funding_locked) = channel.get_mut().monitor_updating_restored(&self.logger);
2306+
(pending_failures, handle_chan_restoration_locked!(self, channel_lock, channel_state, channel, raa, commitment_update, order, pending_forwards, needs_broadcast_safe, funding_locked))
23032307
};
2304-
post_handle_chan_restoration!(self, chan_restoration_res);
2308+
post_handle_chan_restoration!(self, chan_restoration_res, pending_failures);
23052309
}
23062310

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

0 commit comments

Comments
 (0)