Skip to content

Commit a6ddb97

Browse files
committed
Return struct, not long tuple, from Channel::channel_reestablish
This improves readability and makes it easier to add additional return fields.
1 parent d62edd5 commit a6ddb97

File tree

2 files changed

+79
-22
lines changed

2 files changed

+79
-22
lines changed

lightning/src/ln/channel.rs

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,17 @@ pub(super) struct MonitorRestoreUpdates {
401401
pub funding_locked: Option<msgs::FundingLocked>,
402402
}
403403

404+
/// The return value of `channel_reestablish`
405+
pub(super) struct ReestablishResponses {
406+
pub funding_locked: Option<msgs::FundingLocked>,
407+
pub raa: Option<msgs::RevokeAndACK>,
408+
pub commitment_update: Option<msgs::CommitmentUpdate>,
409+
pub order: RAACommitmentOrder,
410+
pub mon_update: Option<ChannelMonitorUpdate>,
411+
pub holding_cell_failed_htlcs: Vec<(HTLCSource, PaymentHash)>,
412+
pub shutdown_msg: Option<msgs::Shutdown>,
413+
}
414+
404415
/// If the majority of the channels funds are to the fundee and the initiator holds only just
405416
/// enough funds to cover their reserve value, channels are at risk of getting "stuck". Because the
406417
/// initiator controls the feerate, if they then go to increase the channel fee, they may have no
@@ -3501,7 +3512,7 @@ impl<Signer: Sign> Channel<Signer> {
35013512

35023513
/// May panic if some calls other than message-handling calls (which will all Err immediately)
35033514
/// have been called between remove_uncommitted_htlcs_and_mark_paused and this call.
3504-
pub fn channel_reestablish<L: Deref>(&mut self, msg: &msgs::ChannelReestablish, logger: &L) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, Option<ChannelMonitorUpdate>, RAACommitmentOrder, Vec<(HTLCSource, PaymentHash)>, Option<msgs::Shutdown>), ChannelError> where L::Target: Logger {
3515+
pub fn channel_reestablish<L: Deref>(&mut self, msg: &msgs::ChannelReestablish, logger: &L) -> Result<ReestablishResponses, ChannelError> where L::Target: Logger {
35053516
if self.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
35063517
// While BOLT 2 doesn't indicate explicitly we should error this channel here, it
35073518
// almost certainly indicates we are going to end up out-of-sync in some way, so we
@@ -3553,15 +3564,27 @@ impl<Signer: Sign> Channel<Signer> {
35533564
return Err(ChannelError::Close("Peer claimed they saw a revoke_and_ack but we haven't sent funding_locked yet".to_owned()));
35543565
}
35553566
// Short circuit the whole handler as there is nothing we can resend them
3556-
return Ok((None, None, None, None, RAACommitmentOrder::CommitmentFirst, Vec::new(), shutdown_msg));
3567+
return Ok(ReestablishResponses {
3568+
funding_locked: None,
3569+
raa: None, commitment_update: None, mon_update: None,
3570+
order: RAACommitmentOrder::CommitmentFirst,
3571+
holding_cell_failed_htlcs: Vec::new(),
3572+
shutdown_msg
3573+
});
35573574
}
35583575

35593576
// We have OurFundingLocked set!
35603577
let next_per_commitment_point = self.holder_signer.get_per_commitment_point(self.cur_holder_commitment_transaction_number, &self.secp_ctx);
3561-
return Ok((Some(msgs::FundingLocked {
3562-
channel_id: self.channel_id(),
3563-
next_per_commitment_point,
3564-
}), None, None, None, RAACommitmentOrder::CommitmentFirst, Vec::new(), shutdown_msg));
3578+
return Ok(ReestablishResponses {
3579+
funding_locked: Some(msgs::FundingLocked {
3580+
channel_id: self.channel_id(),
3581+
next_per_commitment_point,
3582+
}),
3583+
raa: None, commitment_update: None, mon_update: None,
3584+
order: RAACommitmentOrder::CommitmentFirst,
3585+
holding_cell_failed_htlcs: Vec::new(),
3586+
shutdown_msg
3587+
});
35653588
}
35663589

35673590
let required_revoke = if msg.next_remote_commitment_number + 1 == INITIAL_COMMITMENT_NUMBER - self.cur_holder_commitment_transaction_number {
@@ -3585,7 +3608,7 @@ impl<Signer: Sign> Channel<Signer> {
35853608
// the corresponding revoke_and_ack back yet.
35863609
let next_counterparty_commitment_number = INITIAL_COMMITMENT_NUMBER - self.cur_counterparty_commitment_transaction_number + if (self.channel_state & ChannelState::AwaitingRemoteRevoke as u32) != 0 { 1 } else { 0 };
35873610

3588-
let resend_funding_locked = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.cur_holder_commitment_transaction_number == 1 {
3611+
let funding_locked = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.cur_holder_commitment_transaction_number == 1 {
35893612
// We should never have to worry about MonitorUpdateFailed resending FundingLocked
35903613
let next_per_commitment_point = self.holder_signer.get_per_commitment_point(self.cur_holder_commitment_transaction_number, &self.secp_ctx);
35913614
Some(msgs::FundingLocked {
@@ -3607,18 +3630,39 @@ impl<Signer: Sign> Channel<Signer> {
36073630
// have received some updates while we were disconnected. Free the holding cell
36083631
// now!
36093632
match self.free_holding_cell_htlcs(logger) {
3610-
Err(ChannelError::Close(msg)) => return Err(ChannelError::Close(msg)),
3633+
Err(ChannelError::Close(msg)) => Err(ChannelError::Close(msg)),
36113634
Err(ChannelError::Warn(_)) | Err(ChannelError::Ignore(_)) | Err(ChannelError::CloseDelayBroadcast(_)) =>
36123635
panic!("Got non-channel-failing result from free_holding_cell_htlcs"),
3613-
Ok((Some((commitment_update, monitor_update)), htlcs_to_fail)) => {
3614-
return Ok((resend_funding_locked, required_revoke, Some(commitment_update), Some(monitor_update), self.resend_order.clone(), htlcs_to_fail, shutdown_msg));
3636+
Ok((Some((commitment_update, monitor_update)), holding_cell_failed_htlcs)) => {
3637+
Ok(ReestablishResponses {
3638+
funding_locked, shutdown_msg,
3639+
raa: required_revoke,
3640+
commitment_update: Some(commitment_update),
3641+
order: self.resend_order.clone(),
3642+
mon_update: Some(monitor_update),
3643+
holding_cell_failed_htlcs,
3644+
})
36153645
},
3616-
Ok((None, htlcs_to_fail)) => {
3617-
return Ok((resend_funding_locked, required_revoke, None, None, self.resend_order.clone(), htlcs_to_fail, shutdown_msg));
3646+
Ok((None, holding_cell_failed_htlcs)) => {
3647+
Ok(ReestablishResponses {
3648+
funding_locked, shutdown_msg,
3649+
raa: required_revoke,
3650+
commitment_update: None,
3651+
order: self.resend_order.clone(),
3652+
mon_update: None,
3653+
holding_cell_failed_htlcs,
3654+
})
36183655
},
36193656
}
36203657
} else {
3621-
return Ok((resend_funding_locked, required_revoke, None, None, self.resend_order.clone(), Vec::new(), shutdown_msg));
3658+
Ok(ReestablishResponses {
3659+
funding_locked, shutdown_msg,
3660+
raa: required_revoke,
3661+
commitment_update: None,
3662+
order: self.resend_order.clone(),
3663+
mon_update: None,
3664+
holding_cell_failed_htlcs: Vec::new(),
3665+
})
36223666
}
36233667
} else if msg.next_local_commitment_number == next_counterparty_commitment_number - 1 {
36243668
if required_revoke.is_some() {
@@ -3629,12 +3673,24 @@ impl<Signer: Sign> Channel<Signer> {
36293673

36303674
if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) != 0 {
36313675
self.monitor_pending_commitment_signed = true;
3632-
return Ok((resend_funding_locked, None, None, None, self.resend_order.clone(), Vec::new(), shutdown_msg));
3676+
Ok(ReestablishResponses {
3677+
funding_locked, shutdown_msg,
3678+
commitment_update: None, raa: None, mon_update: None,
3679+
order: self.resend_order.clone(),
3680+
holding_cell_failed_htlcs: Vec::new(),
3681+
})
3682+
} else {
3683+
Ok(ReestablishResponses {
3684+
funding_locked, shutdown_msg,
3685+
raa: required_revoke,
3686+
commitment_update: Some(self.get_last_commitment_update(logger)),
3687+
order: self.resend_order.clone(),
3688+
mon_update: None,
3689+
holding_cell_failed_htlcs: Vec::new(),
3690+
})
36333691
}
3634-
3635-
return Ok((resend_funding_locked, required_revoke, Some(self.get_last_commitment_update(logger)), None, self.resend_order.clone(), Vec::new(), shutdown_msg));
36363692
} else {
3637-
return Err(ChannelError::Close("Peer attempted to reestablish channel with a very old remote commitment transaction".to_owned()));
3693+
Err(ChannelError::Close("Peer attempted to reestablish channel with a very old remote commitment transaction".to_owned()))
36383694
}
36393695
}
36403696

lightning/src/ln/channelmanager.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4722,10 +4722,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
47224722
// disconnect, so Channel's reestablish will never hand us any holding cell
47234723
// freed HTLCs to fail backwards. If in the future we no longer drop pending
47244724
// add-HTLCs on disconnect, we may be handed HTLCs to fail backwards here.
4725-
let (funding_locked, revoke_and_ack, commitment_update, monitor_update_opt, order, htlcs_failed_forward, shutdown) =
4726-
try_chan_entry!(self, chan.get_mut().channel_reestablish(msg, &self.logger), channel_state, chan);
4725+
let responses = try_chan_entry!(self, chan.get_mut().channel_reestablish(msg, &self.logger), channel_state, chan);
47274726
let mut channel_update = None;
4728-
if let Some(msg) = shutdown {
4727+
if let Some(msg) = responses.shutdown_msg {
47294728
channel_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
47304729
node_id: counterparty_node_id.clone(),
47314730
msg,
@@ -4740,11 +4739,13 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
47404739
});
47414740
}
47424741
let need_lnd_workaround = chan.get_mut().workaround_lnd_bug_4006.take();
4743-
chan_restoration_res = handle_chan_restoration_locked!(self, channel_state_lock, channel_state, chan, revoke_and_ack, commitment_update, order, monitor_update_opt, Vec::new(), None, funding_locked);
4742+
chan_restoration_res = handle_chan_restoration_locked!(
4743+
self, channel_state_lock, channel_state, chan, responses.raa, responses.commitment_update, responses.order,
4744+
responses.mon_update, Vec::new(), None, responses.funding_locked);
47444745
if let Some(upd) = channel_update {
47454746
channel_state.pending_msg_events.push(upd);
47464747
}
4747-
(htlcs_failed_forward, need_lnd_workaround)
4748+
(responses.holding_cell_failed_htlcs, need_lnd_workaround)
47484749
},
47494750
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
47504751
}

0 commit comments

Comments
 (0)