Skip to content

Commit 7011624

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 233272a commit 7011624

File tree

2 files changed

+80
-23
lines changed

2 files changed

+80
-23
lines changed

lightning/src/ln/channel.rs

Lines changed: 74 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,17 @@ pub(super) struct MonitorRestoreUpdates {
363363
pub funding_locked: Option<msgs::FundingLocked>,
364364
}
365365

366+
/// The return value of `channel_reestablish`
367+
pub(super) struct ReestablishResponses {
368+
pub funding_locked: Option<msgs::FundingLocked>,
369+
pub raa: Option<msgs::RevokeAndACK>,
370+
pub commitment_update: Option<msgs::CommitmentUpdate>,
371+
pub order: RAACommitmentOrder,
372+
pub mon_update: Option<ChannelMonitorUpdate>,
373+
pub holding_cell_failed_htlcs: Vec<(HTLCSource, PaymentHash)>,
374+
pub shutdown: Option<msgs::Shutdown>,
375+
}
376+
366377
/// If the majority of the channels funds are to the fundee and the initiator holds only just
367378
/// enough funds to cover their reserve value, channels are at risk of getting "stuck". Because the
368379
/// initiator controls the feerate, if they then go to increase the channel fee, they may have no
@@ -3337,7 +3348,7 @@ impl<Signer: Sign> Channel<Signer> {
33373348

33383349
/// May panic if some calls other than message-handling calls (which will all Err immediately)
33393350
/// have been called between remove_uncommitted_htlcs_and_mark_paused and this call.
3340-
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 {
3351+
pub fn channel_reestablish<L: Deref>(&mut self, msg: &msgs::ChannelReestablish, logger: &L) -> Result<ReestablishResponses, ChannelError> where L::Target: Logger {
33413352
if self.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
33423353
// While BOLT 2 doesn't indicate explicitly we should error this channel here, it
33433354
// almost certainly indicates we are going to end up out-of-sync in some way, so we
@@ -3373,7 +3384,7 @@ impl<Signer: Sign> Channel<Signer> {
33733384
// remaining cases either succeed or ErrorMessage-fail).
33743385
self.channel_state &= !(ChannelState::PeerDisconnected as u32);
33753386

3376-
let shutdown_msg = if self.channel_state & (ChannelState::LocalShutdownSent as u32) != 0 {
3387+
let shutdown = if self.channel_state & (ChannelState::LocalShutdownSent as u32) != 0 {
33773388
assert!(self.shutdown_scriptpubkey.is_some());
33783389
Some(msgs::Shutdown {
33793390
channel_id: self.channel_id,
@@ -3389,15 +3400,27 @@ impl<Signer: Sign> Channel<Signer> {
33893400
return Err(ChannelError::Close("Peer claimed they saw a revoke_and_ack but we haven't sent funding_locked yet".to_owned()));
33903401
}
33913402
// Short circuit the whole handler as there is nothing we can resend them
3392-
return Ok((None, None, None, None, RAACommitmentOrder::CommitmentFirst, Vec::new(), shutdown_msg));
3403+
return Ok(ReestablishResponses {
3404+
funding_locked: None,
3405+
raa: None, commitment_update: None, mon_update: None,
3406+
order: RAACommitmentOrder::CommitmentFirst,
3407+
holding_cell_failed_htlcs: Vec::new(),
3408+
shutdown
3409+
});
33933410
}
33943411

33953412
// We have OurFundingLocked set!
33963413
let next_per_commitment_point = self.holder_signer.get_per_commitment_point(self.cur_holder_commitment_transaction_number, &self.secp_ctx);
3397-
return Ok((Some(msgs::FundingLocked {
3398-
channel_id: self.channel_id(),
3399-
next_per_commitment_point,
3400-
}), None, None, None, RAACommitmentOrder::CommitmentFirst, Vec::new(), shutdown_msg));
3414+
return Ok(ReestablishResponses {
3415+
funding_locked: Some(msgs::FundingLocked {
3416+
channel_id: self.channel_id(),
3417+
next_per_commitment_point,
3418+
}),
3419+
raa: None, commitment_update: None, mon_update: None,
3420+
order: RAACommitmentOrder::CommitmentFirst,
3421+
holding_cell_failed_htlcs: Vec::new(),
3422+
shutdown
3423+
});
34013424
}
34023425

34033426
let required_revoke = if msg.next_remote_commitment_number + 1 == INITIAL_COMMITMENT_NUMBER - self.cur_holder_commitment_transaction_number {
@@ -3421,7 +3444,7 @@ impl<Signer: Sign> Channel<Signer> {
34213444
// the corresponding revoke_and_ack back yet.
34223445
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 };
34233446

3424-
let resend_funding_locked = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.cur_holder_commitment_transaction_number == 1 {
3447+
let funding_locked = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.cur_holder_commitment_transaction_number == 1 {
34253448
// We should never have to worry about MonitorUpdateFailed resending FundingLocked
34263449
let next_per_commitment_point = self.holder_signer.get_per_commitment_point(self.cur_holder_commitment_transaction_number, &self.secp_ctx);
34273450
Some(msgs::FundingLocked {
@@ -3443,18 +3466,39 @@ impl<Signer: Sign> Channel<Signer> {
34433466
// have received some updates while we were disconnected. Free the holding cell
34443467
// now!
34453468
match self.free_holding_cell_htlcs(logger) {
3446-
Err(ChannelError::Close(msg)) => return Err(ChannelError::Close(msg)),
3469+
Err(ChannelError::Close(msg)) => Err(ChannelError::Close(msg)),
34473470
Err(ChannelError::Warn(_)) | Err(ChannelError::Ignore(_)) | Err(ChannelError::CloseDelayBroadcast(_)) =>
34483471
panic!("Got non-channel-failing result from free_holding_cell_htlcs"),
3449-
Ok((Some((commitment_update, monitor_update)), htlcs_to_fail)) => {
3450-
return Ok((resend_funding_locked, required_revoke, Some(commitment_update), Some(monitor_update), self.resend_order.clone(), htlcs_to_fail, shutdown_msg));
3472+
Ok((Some((commitment_update, monitor_update)), holding_cell_failed_htlcs)) => {
3473+
Ok(ReestablishResponses {
3474+
funding_locked, shutdown,
3475+
raa: required_revoke,
3476+
commitment_update: Some(commitment_update),
3477+
order: self.resend_order.clone(),
3478+
mon_update: Some(monitor_update),
3479+
holding_cell_failed_htlcs,
3480+
})
34513481
},
3452-
Ok((None, htlcs_to_fail)) => {
3453-
return Ok((resend_funding_locked, required_revoke, None, None, self.resend_order.clone(), htlcs_to_fail, shutdown_msg));
3482+
Ok((None, holding_cell_failed_htlcs)) => {
3483+
Ok(ReestablishResponses {
3484+
funding_locked, shutdown,
3485+
raa: required_revoke,
3486+
commitment_update: None,
3487+
order: self.resend_order.clone(),
3488+
mon_update: None,
3489+
holding_cell_failed_htlcs,
3490+
})
34543491
},
34553492
}
34563493
} else {
3457-
return Ok((resend_funding_locked, required_revoke, None, None, self.resend_order.clone(), Vec::new(), shutdown_msg));
3494+
Ok(ReestablishResponses {
3495+
funding_locked, shutdown,
3496+
raa: required_revoke,
3497+
commitment_update: None,
3498+
order: self.resend_order.clone(),
3499+
mon_update: None,
3500+
holding_cell_failed_htlcs: Vec::new(),
3501+
})
34583502
}
34593503
} else if msg.next_local_commitment_number == next_counterparty_commitment_number - 1 {
34603504
if required_revoke.is_some() {
@@ -3465,12 +3509,24 @@ impl<Signer: Sign> Channel<Signer> {
34653509

34663510
if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) != 0 {
34673511
self.monitor_pending_commitment_signed = true;
3468-
return Ok((resend_funding_locked, None, None, None, self.resend_order.clone(), Vec::new(), shutdown_msg));
3512+
Ok(ReestablishResponses {
3513+
funding_locked, shutdown,
3514+
commitment_update: None, raa: None, mon_update: None,
3515+
order: self.resend_order.clone(),
3516+
holding_cell_failed_htlcs: Vec::new(),
3517+
})
3518+
} else {
3519+
Ok(ReestablishResponses {
3520+
funding_locked, shutdown,
3521+
raa: required_revoke,
3522+
commitment_update: Some(self.get_last_commitment_update(logger)),
3523+
order: self.resend_order.clone(),
3524+
mon_update: None,
3525+
holding_cell_failed_htlcs: Vec::new(),
3526+
})
34693527
}
3470-
3471-
return Ok((resend_funding_locked, required_revoke, Some(self.get_last_commitment_update(logger)), None, self.resend_order.clone(), Vec::new(), shutdown_msg));
34723528
} else {
3473-
return Err(ChannelError::Close("Peer attempted to reestablish channel with a very old remote commitment transaction".to_owned()));
3529+
Err(ChannelError::Close("Peer attempted to reestablish channel with a very old remote commitment transaction".to_owned()))
34743530
}
34753531
}
34763532

lightning/src/ln/channelmanager.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4262,10 +4262,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
42624262
// disconnect, so Channel's reestablish will never hand us any holding cell
42634263
// freed HTLCs to fail backwards. If in the future we no longer drop pending
42644264
// add-HTLCs on disconnect, we may be handed HTLCs to fail backwards here.
4265-
let (funding_locked, revoke_and_ack, commitment_update, monitor_update_opt, order, htlcs_failed_forward, shutdown) =
4266-
try_chan_entry!(self, chan.get_mut().channel_reestablish(msg, &self.logger), channel_state, chan);
4265+
let responses = try_chan_entry!(self, chan.get_mut().channel_reestablish(msg, &self.logger), channel_state, chan);
42674266
let mut channel_update = None;
4268-
if let Some(msg) = shutdown {
4267+
if let Some(msg) = responses.shutdown {
42694268
channel_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
42704269
node_id: counterparty_node_id.clone(),
42714270
msg,
@@ -4280,11 +4279,13 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
42804279
});
42814280
}
42824281
let need_lnd_workaround = chan.get_mut().workaround_lnd_bug_4006.take();
4283-
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);
4282+
chan_restoration_res = handle_chan_restoration_locked!(
4283+
self, channel_state_lock, channel_state, chan, responses.raa, responses.commitment_update, responses.order,
4284+
responses.mon_update, Vec::new(), None, responses.funding_locked);
42844285
if let Some(upd) = channel_update {
42854286
channel_state.pending_msg_events.push(upd);
42864287
}
4287-
(htlcs_failed_forward, need_lnd_workaround)
4288+
(responses.holding_cell_failed_htlcs, need_lnd_workaround)
42884289
},
42894290
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
42904291
}

0 commit comments

Comments
 (0)