Skip to content

Commit

Permalink
Use handle_chan_restoration!() in channel_reestablish handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBlueMatt committed Nov 23, 2020
1 parent b742761 commit ed1d8a7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 67 deletions.
96 changes: 29 additions & 67 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,16 +719,34 @@ macro_rules! handle_chan_restoration {
let channel_id = $channel_entry.get().channel_id();

let res = loop {
if !$pending_forwards.is_empty() {
let forwards: Vec<(PendingHTLCInfo, u64)> = $pending_forwards; // Force type-checking to resolve
if !forwards.is_empty() {
htlc_forwards = Some(($channel_entry.get().get_short_channel_id().expect("We can't have pending forwards before funding confirmation"),
$channel_entry.get().get_funding_txo().unwrap(), $pending_forwards));
$channel_entry.get().get_funding_txo().unwrap(), forwards));
}
if $chanmon_update.is_some() {
assert!($commitment_update.is_some());
assert!($funding_locked.is_none());
}

if let Some(msg) = $funding_locked {
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
node_id: counterparty_node_id,
msg,
});
if let Some(announcement_sigs) = $self.get_announcement_sigs($channel_entry.get()) {
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
node_id: counterparty_node_id,
msg: announcement_sigs,
});
}
$channel_state.short_to_id.insert($channel_entry.get().get_short_channel_id().unwrap(), channel_id);
}

macro_rules! handle_cs { () => {
if let Some(monitor_update) = $chanmon_update {
assert!($order == RAACommitmentOrder::RevokeAndACKFirst);
assert!(!$broadcast_safe);
assert!($funding_locked.is_none());
assert!($commitment_update.is_some());
if let Err(e) = $self.chain_monitor.update_channel($channel_entry.get().get_funding_txo().unwrap(), monitor_update) {
break handle_monitor_err!($self, e, $channel_state, $channel_entry, RAACommitmentOrder::CommitmentFirst, false, true);
Expand Down Expand Up @@ -765,19 +783,6 @@ macro_rules! handle_chan_restoration {
user_channel_id: $channel_entry.get().get_user_id(),
});
}
if let Some(msg) = $funding_locked {
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
node_id: counterparty_node_id,
msg,
});
if let Some(announcement_sigs) = $self.get_announcement_sigs($channel_entry.get()) {
$channel_state.pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
node_id: counterparty_node_id,
msg: announcement_sigs,
});
}
$channel_state.short_to_id.insert($channel_entry.get().get_short_channel_id().unwrap(), channel_id);
}
break Ok(());
};

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

$self.fail_holding_cell_htlcs($forwarding_failures, channel_id);
for failure in $pending_failures.drain(..) {
let forwarding_failures: Vec<(HTLCSource, PaymentHash)> = $forwarding_failures; // Force type-checking to resolve
$self.fail_holding_cell_htlcs(forwarding_failures, channel_id);

let mut pending_failures: Vec<(HTLCSource, PaymentHash, HTLCFailReason)> = $pending_failures; // Force type-checking to resolve
for failure in pending_failures.drain(..) {
$self.fail_htlc_backwards_internal($self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
}
if let Some(forwards) = htlc_forwards {
Expand Down Expand Up @@ -2314,7 +2322,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
return;
}

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);
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);
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);
}

Expand Down Expand Up @@ -2916,61 +2924,15 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
// disconnect, so Channel's reestablish will never hand us any holding cell
// freed HTLCs to fail backwards. If in the future we no longer drop pending
// add-HTLCs on disconnect, we may be handed HTLCs to fail backwards here.
let (funding_locked, revoke_and_ack, commitment_update, monitor_update_opt, mut order, shutdown) =
let (funding_locked, revoke_and_ack, commitment_update, monitor_update_opt, order, shutdown) =
try_chan_entry!(self, chan.get_mut().channel_reestablish(msg, &self.logger), channel_state, chan);
if let Some(monitor_update) = monitor_update_opt {
if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
// channel_reestablish doesn't guarantee the order it returns is sensical
// for the messages it returns, but if we're setting what messages to
// re-transmit on monitor update success, we need to make sure it is sane.
if revoke_and_ack.is_none() {
order = RAACommitmentOrder::CommitmentFirst;
}
if commitment_update.is_none() {
order = RAACommitmentOrder::RevokeAndACKFirst;
}
return_monitor_err!(self, e, channel_state, chan, order, revoke_and_ack.is_some(), commitment_update.is_some());
//TODO: Resend the funding_locked if needed once we get the monitor running again
}
}
if let Some(msg) = funding_locked {
channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
node_id: counterparty_node_id.clone(),
msg
});
}
macro_rules! send_raa { () => {
if let Some(msg) = revoke_and_ack {
channel_state.pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
node_id: counterparty_node_id.clone(),
msg
});
}
} }
macro_rules! send_cu { () => {
if let Some(updates) = commitment_update {
channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
node_id: counterparty_node_id.clone(),
updates
});
}
} }
match order {
RAACommitmentOrder::RevokeAndACKFirst => {
send_raa!();
send_cu!();
},
RAACommitmentOrder::CommitmentFirst => {
send_cu!();
send_raa!();
},
}
if let Some(msg) = shutdown {
channel_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
node_id: counterparty_node_id.clone(),
msg,
});
}
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);
Ok(())
},
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
Expand Down
5 changes: 5 additions & 0 deletions lightning/src/ln/functional_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,11 @@ macro_rules! handle_chan_reestablish_msgs {
None
};

if let Some(&MessageSendEvent::SendAnnouncementSignatures { ref node_id, msg: _ }) = msg_events.get(idx) {
idx += 1;
assert_eq!(*node_id, $dst_node.node.get_our_node_id());
}

let mut revoke_and_ack = None;
let mut commitment_update = None;
let order = if let Some(ev) = msg_events.get(idx) {
Expand Down

0 comments on commit ed1d8a7

Please sign in to comment.