Skip to content

Commit 6170828

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 f5451ec commit 6170828

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
@@ -747,30 +747,29 @@ macro_rules! maybe_break_monitor_err {
747747
macro_rules! handle_chan_restoration_locked {
748748
($self: expr, $channel_lock: expr, $channel_state: expr, $channel_entry: expr,
749749
$raa: expr, $commitment_update: expr, $order: expr,
750-
$pending_forwards: expr, $pending_failures: expr, $broadcast_safe: expr, $funding_locked: expr) => { {
751-
let mut htlc_forwards = Vec::new();
752-
let mut htlc_failures = Vec::new();
753-
let mut pending_events = Vec::new();
750+
$pending_forwards: expr, $broadcast_safe: expr, $funding_locked: expr) => { {
751+
let mut htlc_forwards = None;
752+
let mut funding_broadcast_safe = None;
753+
let counterparty_node_id = $channel_entry.get().get_counterparty_node_id();
754754

755755
{
756756
if !$pending_forwards.is_empty() {
757-
htlc_forwards.push(($channel_entry.get().get_short_channel_id().expect("We can't have pending forwards before funding confirmation"),
757+
htlc_forwards = Some(($channel_entry.get().get_short_channel_id().expect("We can't have pending forwards before funding confirmation"),
758758
$channel_entry.get().get_funding_txo().unwrap(), $pending_forwards));
759759
}
760-
htlc_failures.append(&mut $pending_failures);
761760

762761
macro_rules! handle_cs { () => {
763762
if let Some(update) = $commitment_update {
764763
$channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
765-
node_id: $channel_entry.get().get_counterparty_node_id(),
764+
node_id: counterparty_node_id,
766765
updates: update,
767766
});
768767
}
769768
} }
770769
macro_rules! handle_raa { () => {
771770
if let Some(revoke_and_ack) = $raa {
772771
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
773-
node_id: $channel_entry.get().get_counterparty_node_id(),
772+
node_id: counterparty_node_id,
774773
msg: revoke_and_ack,
775774
});
776775
}
@@ -786,38 +785,43 @@ macro_rules! handle_chan_restoration_locked {
786785
},
787786
}
788787
if $broadcast_safe {
789-
pending_events.push(events::Event::FundingBroadcastSafe {
788+
funding_broadcast_safe = Some(events::Event::FundingBroadcastSafe {
790789
funding_txo: $channel_entry.get().get_funding_txo().unwrap(),
791790
user_channel_id: $channel_entry.get().get_user_id(),
792791
});
793792
}
794793
if let Some(msg) = $funding_locked {
795794
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
796-
node_id: $channel_entry.get().get_counterparty_node_id(),
795+
node_id: counterparty_node_id,
797796
msg,
798797
});
799798
if let Some(announcement_sigs) = $self.get_announcement_sigs($channel_entry.get()) {
800799
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
801-
node_id: $channel_entry.get().get_counterparty_node_id(),
800+
node_id: counterparty_node_id,
802801
msg: announcement_sigs,
803802
});
804803
}
805804
$channel_state.short_to_id.insert($channel_entry.get().get_short_channel_id().unwrap(), $channel_entry.get().channel_id());
806805
}
807806
}
808-
(htlc_forwards, htlc_failures, pending_events)
807+
(htlc_forwards, funding_broadcast_safe)
809808
} }
810809
}
811810

812811
macro_rules! post_handle_chan_restoration {
813-
($self: expr, $locked_res: expr) => { {
814-
let (mut htlc_forwards, mut htlc_failures, mut pending_events) = $locked_res;
815-
$self.pending_events.lock().unwrap().append(&mut pending_events);
812+
($self: expr, $locked_res: expr, $pending_failures: expr) => { {
813+
let (htlc_forwards, funding_broadcast_safe) = $locked_res;
816814

817-
for failure in htlc_failures.drain(..) {
815+
if let Some(ev) = funding_broadcast_safe {
816+
$self.pending_events.lock().unwrap().push(ev);
817+
}
818+
819+
for failure in $pending_failures.drain(..) {
818820
$self.fail_htlc_backwards_internal($self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
819821
}
820-
$self.forward_htlcs(&mut htlc_forwards[..]);
822+
if let Some(forwards) = htlc_forwards {
823+
$self.forward_htlcs(&mut [forwards][..]);
824+
}
821825
} }
822826
}
823827

@@ -2339,7 +2343,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
23392343
pub fn channel_monitor_updated(&self, funding_txo: &OutPoint, highest_applied_update_id: u64) {
23402344
let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
23412345

2342-
let chan_restoration_res = {
2346+
let (mut pending_failures, chan_restoration_res) = {
23432347
let mut channel_lock = self.channel_state.lock().unwrap();
23442348
let channel_state = &mut *channel_lock;
23452349
let mut channel = match channel_state.by_id.entry(funding_txo.to_channel_id()) {
@@ -2350,10 +2354,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
23502354
return;
23512355
}
23522356

2353-
let (raa, commitment_update, order, pending_forwards, mut pending_failures, needs_broadcast_safe, funding_locked) = channel.get_mut().monitor_updating_restored(&self.logger);
2354-
handle_chan_restoration_locked!(self, channel_lock, channel_state, channel, raa, commitment_update, order, pending_forwards, pending_failures, needs_broadcast_safe, funding_locked)
2357+
let (raa, commitment_update, order, pending_forwards, pending_failures, needs_broadcast_safe, funding_locked) = channel.get_mut().monitor_updating_restored(&self.logger);
2358+
(pending_failures, handle_chan_restoration_locked!(self, channel_lock, channel_state, channel, raa, commitment_update, order, pending_forwards, needs_broadcast_safe, funding_locked))
23552359
};
2356-
post_handle_chan_restoration!(self, chan_restoration_res);
2360+
post_handle_chan_restoration!(self, chan_restoration_res, pending_failures);
23572361
}
23582362

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

0 commit comments

Comments
 (0)