Skip to content

Commit ed1d8a7

Browse files
committed
Use handle_chan_restoration!() in channel_reestablish handling.
1 parent b742761 commit ed1d8a7

File tree

2 files changed

+34
-67
lines changed

2 files changed

+34
-67
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 29 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -719,16 +719,34 @@ macro_rules! handle_chan_restoration {
719719
let channel_id = $channel_entry.get().channel_id();
720720

721721
let res = loop {
722-
if !$pending_forwards.is_empty() {
722+
let forwards: Vec<(PendingHTLCInfo, u64)> = $pending_forwards; // Force type-checking to resolve
723+
if !forwards.is_empty() {
723724
htlc_forwards = Some(($channel_entry.get().get_short_channel_id().expect("We can't have pending forwards before funding confirmation"),
724-
$channel_entry.get().get_funding_txo().unwrap(), $pending_forwards));
725+
$channel_entry.get().get_funding_txo().unwrap(), forwards));
726+
}
727+
if $chanmon_update.is_some() {
728+
assert!($commitment_update.is_some());
729+
assert!($funding_locked.is_none());
730+
}
731+
732+
if let Some(msg) = $funding_locked {
733+
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
734+
node_id: counterparty_node_id,
735+
msg,
736+
});
737+
if let Some(announcement_sigs) = $self.get_announcement_sigs($channel_entry.get()) {
738+
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
739+
node_id: counterparty_node_id,
740+
msg: announcement_sigs,
741+
});
742+
}
743+
$channel_state.short_to_id.insert($channel_entry.get().get_short_channel_id().unwrap(), channel_id);
725744
}
726745

727746
macro_rules! handle_cs { () => {
728747
if let Some(monitor_update) = $chanmon_update {
729748
assert!($order == RAACommitmentOrder::RevokeAndACKFirst);
730749
assert!(!$broadcast_safe);
731-
assert!($funding_locked.is_none());
732750
assert!($commitment_update.is_some());
733751
if let Err(e) = $self.chain_monitor.update_channel($channel_entry.get().get_funding_txo().unwrap(), monitor_update) {
734752
break handle_monitor_err!($self, e, $channel_state, $channel_entry, RAACommitmentOrder::CommitmentFirst, false, true);
@@ -765,19 +783,6 @@ macro_rules! handle_chan_restoration {
765783
user_channel_id: $channel_entry.get().get_user_id(),
766784
});
767785
}
768-
if let Some(msg) = $funding_locked {
769-
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
770-
node_id: counterparty_node_id,
771-
msg,
772-
});
773-
if let Some(announcement_sigs) = $self.get_announcement_sigs($channel_entry.get()) {
774-
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
775-
node_id: counterparty_node_id,
776-
msg: announcement_sigs,
777-
});
778-
}
779-
$channel_state.short_to_id.insert($channel_entry.get().get_short_channel_id().unwrap(), channel_id);
780-
}
781786
break Ok(());
782787
};
783788

@@ -789,8 +794,11 @@ macro_rules! handle_chan_restoration {
789794
$self.pending_events.lock().unwrap().push(ev);
790795
}
791796

792-
$self.fail_holding_cell_htlcs($forwarding_failures, channel_id);
793-
for failure in $pending_failures.drain(..) {
797+
let forwarding_failures: Vec<(HTLCSource, PaymentHash)> = $forwarding_failures; // Force type-checking to resolve
798+
$self.fail_holding_cell_htlcs(forwarding_failures, channel_id);
799+
800+
let mut pending_failures: Vec<(HTLCSource, PaymentHash, HTLCFailReason)> = $pending_failures; // Force type-checking to resolve
801+
for failure in pending_failures.drain(..) {
794802
$self.fail_htlc_backwards_internal($self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
795803
}
796804
if let Some(forwards) = htlc_forwards {
@@ -2314,7 +2322,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
23142322
return;
23152323
}
23162324

2317-
let (raa, commitment_update, order, chanmon_update, pending_forwards, mut pending_failures, forwarding_failds, needs_broadcast_safe, funding_locked) = channel.get_mut().monitor_updating_restored(&self.logger);
2325+
let (raa, commitment_update, order, chanmon_update, pending_forwards, pending_failures, forwarding_failds, needs_broadcast_safe, funding_locked) = channel.get_mut().monitor_updating_restored(&self.logger);
23182326
handle_chan_restoration!(self, channel_lock, channel_state, channel, raa, commitment_update, order, chanmon_update, pending_forwards, pending_failures, forwarding_failds, needs_broadcast_safe, funding_locked);
23192327
}
23202328

@@ -2916,61 +2924,15 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
29162924
// disconnect, so Channel's reestablish will never hand us any holding cell
29172925
// freed HTLCs to fail backwards. If in the future we no longer drop pending
29182926
// add-HTLCs on disconnect, we may be handed HTLCs to fail backwards here.
2919-
let (funding_locked, revoke_and_ack, commitment_update, monitor_update_opt, mut order, shutdown) =
2927+
let (funding_locked, revoke_and_ack, commitment_update, monitor_update_opt, order, shutdown) =
29202928
try_chan_entry!(self, chan.get_mut().channel_reestablish(msg, &self.logger), channel_state, chan);
2921-
if let Some(monitor_update) = monitor_update_opt {
2922-
if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
2923-
// channel_reestablish doesn't guarantee the order it returns is sensical
2924-
// for the messages it returns, but if we're setting what messages to
2925-
// re-transmit on monitor update success, we need to make sure it is sane.
2926-
if revoke_and_ack.is_none() {
2927-
order = RAACommitmentOrder::CommitmentFirst;
2928-
}
2929-
if commitment_update.is_none() {
2930-
order = RAACommitmentOrder::RevokeAndACKFirst;
2931-
}
2932-
return_monitor_err!(self, e, channel_state, chan, order, revoke_and_ack.is_some(), commitment_update.is_some());
2933-
//TODO: Resend the funding_locked if needed once we get the monitor running again
2934-
}
2935-
}
2936-
if let Some(msg) = funding_locked {
2937-
channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
2938-
node_id: counterparty_node_id.clone(),
2939-
msg
2940-
});
2941-
}
2942-
macro_rules! send_raa { () => {
2943-
if let Some(msg) = revoke_and_ack {
2944-
channel_state.pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
2945-
node_id: counterparty_node_id.clone(),
2946-
msg
2947-
});
2948-
}
2949-
} }
2950-
macro_rules! send_cu { () => {
2951-
if let Some(updates) = commitment_update {
2952-
channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
2953-
node_id: counterparty_node_id.clone(),
2954-
updates
2955-
});
2956-
}
2957-
} }
2958-
match order {
2959-
RAACommitmentOrder::RevokeAndACKFirst => {
2960-
send_raa!();
2961-
send_cu!();
2962-
},
2963-
RAACommitmentOrder::CommitmentFirst => {
2964-
send_cu!();
2965-
send_raa!();
2966-
},
2967-
}
29682929
if let Some(msg) = shutdown {
29692930
channel_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
29702931
node_id: counterparty_node_id.clone(),
29712932
msg,
29722933
});
29732934
}
2935+
handle_chan_restoration!(self, channel_state_lock, channel_state, chan, revoke_and_ack, commitment_update, order, monitor_update_opt, Vec::new(), Vec::new(), Vec::new(), false, funding_locked);
29742936
Ok(())
29752937
},
29762938
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))

lightning/src/ln/functional_test_utils.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,11 @@ macro_rules! handle_chan_reestablish_msgs {
13501350
None
13511351
};
13521352

1353+
if let Some(&MessageSendEvent::SendAnnouncementSignatures { ref node_id, msg: _ }) = msg_events.get(idx) {
1354+
idx += 1;
1355+
assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1356+
}
1357+
13531358
let mut revoke_and_ack = None;
13541359
let mut commitment_update = None;
13551360
let order = if let Some(ev) = msg_events.get(idx) {

0 commit comments

Comments
 (0)