Skip to content

Commit e7a4908

Browse files
committed
Use return struct in monitor_updating_restored+channel_reestablish
As pointed out by Jeff, using a return struct instead of an incredibly-long tuple improves readability in several places.
1 parent 6e865ea commit e7a4908

File tree

2 files changed

+175
-65
lines changed

2 files changed

+175
-65
lines changed

lightning/src/ln/channel.rs

Lines changed: 141 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,21 @@ enum HTLCUpdateAwaitingACK {
187187
},
188188
}
189189

190+
/// A struct capturing the set of things we may need to do after a reconnect or after monitor
191+
/// updating has been restored.
192+
pub(super) struct ChannelRestoredUpdates {
193+
pub(super) revoke_and_ack: Option<msgs::RevokeAndACK>,
194+
pub(super) commitment_update: Option<msgs::CommitmentUpdate>,
195+
pub(super) raa_commitment_order: RAACommitmentOrder,
196+
pub(super) chanmon_update: Option<ChannelMonitorUpdate>,
197+
pub(super) pending_forwards: Vec<(PendingHTLCInfo, u64)>,
198+
pub(super) pending_failures: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
199+
pub(super) forwarding_failures: Vec<(HTLCSource, PaymentHash)>,
200+
pub(super) needs_broadcast_safe: bool,
201+
pub(super) funding_locked: Option<msgs::FundingLocked>,
202+
pub(super) shutdown: Option<msgs::Shutdown>,
203+
}
204+
190205
/// There are a few "states" and then a number of flags which can be applied:
191206
/// We first move through init with OurInitSent -> TheirInitSent -> FundingCreated -> FundingSent.
192207
/// TheirFundingLocked and OurFundingLocked then get set on FundingSent, and when both are set we
@@ -2739,10 +2754,7 @@ impl<Signer: Sign> Channel<Signer> {
27392754
/// Indicates that the latest ChannelMonitor update has been committed by the client
27402755
/// successfully and we should restore normal operation. Returns messages which should be sent
27412756
/// to the remote side.
2742-
pub fn monitor_updating_restored<L: Deref>(&mut self, logger: &L) -> (
2743-
Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, RAACommitmentOrder, Option<ChannelMonitorUpdate>,
2744-
Vec<(PendingHTLCInfo, u64)>, Vec<(HTLCSource, PaymentHash, HTLCFailReason)>, Vec<(HTLCSource, PaymentHash)>,
2745-
bool, Option<msgs::FundingLocked>) where L::Target: Logger {
2757+
pub fn monitor_updating_restored<L: Deref>(&mut self, logger: &L) -> ChannelRestoredUpdates where L::Target: Logger {
27462758
assert_eq!(self.channel_state & ChannelState::MonitorUpdateFailed as u32, ChannelState::MonitorUpdateFailed as u32);
27472759
self.channel_state &= !(ChannelState::MonitorUpdateFailed as u32);
27482760

@@ -2764,35 +2776,46 @@ impl<Signer: Sign> Channel<Signer> {
27642776
})
27652777
} else { None };
27662778

2767-
let mut forwards = Vec::new();
2768-
mem::swap(&mut forwards, &mut self.monitor_pending_forwards);
2769-
let mut failures = Vec::new();
2770-
mem::swap(&mut failures, &mut self.monitor_pending_failures);
2779+
let mut pending_forwards = Vec::new();
2780+
mem::swap(&mut pending_forwards, &mut self.monitor_pending_forwards);
2781+
let mut pending_failures = Vec::new();
2782+
mem::swap(&mut pending_failures, &mut self.monitor_pending_failures);
27712783

27722784
if self.channel_state & (ChannelState::PeerDisconnected as u32) != 0 {
27732785
self.monitor_pending_revoke_and_ack = false;
27742786
self.monitor_pending_commitment_signed = false;
2775-
return (None, None, RAACommitmentOrder::RevokeAndACKFirst, None, forwards, failures, Vec::new(), needs_broadcast_safe, funding_locked);
2787+
return ChannelRestoredUpdates {
2788+
revoke_and_ack: None,
2789+
commitment_update: None,
2790+
raa_commitment_order: RAACommitmentOrder::RevokeAndACKFirst,
2791+
chanmon_update: None,
2792+
pending_forwards,
2793+
pending_failures,
2794+
forwarding_failures: Vec::new(),
2795+
needs_broadcast_safe,
2796+
funding_locked,
2797+
shutdown: None,
2798+
}
27762799
}
27772800

2778-
let raa = if self.monitor_pending_revoke_and_ack {
2801+
let revoke_and_ack = if self.monitor_pending_revoke_and_ack {
27792802
Some(self.get_last_revoke_and_ack())
27802803
} else { None };
27812804
let mut commitment_update = if self.monitor_pending_commitment_signed {
27822805
Some(self.get_last_commitment_update(logger))
27832806
} else { None };
27842807

2785-
let mut order = self.resend_order.clone();
2808+
let mut raa_commitment_order = self.resend_order.clone();
27862809
self.monitor_pending_revoke_and_ack = false;
27872810
self.monitor_pending_commitment_signed = false;
27882811

2789-
let mut htlcs_failed_to_forward = Vec::new();
2812+
let mut forwarding_failures = Vec::new();
27902813
let mut chanmon_update = None;
27912814
if commitment_update.is_none() && self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32) == 0 {
2792-
order = RAACommitmentOrder::RevokeAndACKFirst;
2815+
raa_commitment_order = RAACommitmentOrder::RevokeAndACKFirst;
27932816

27942817
let (update_opt, mut failed_htlcs) = self.free_holding_cell_htlcs(logger).unwrap();
2795-
htlcs_failed_to_forward.append(&mut failed_htlcs);
2818+
forwarding_failures.append(&mut failed_htlcs);
27962819
if let Some((com_update, mon_update)) = update_opt {
27972820
commitment_update = Some(com_update);
27982821
chanmon_update = Some(mon_update);
@@ -2802,9 +2825,21 @@ impl<Signer: Sign> Channel<Signer> {
28022825
log_trace!(logger, "Restored monitor updating resulting in {}{} commitment update and {} RAA, with {} first",
28032826
if needs_broadcast_safe { "a funding broadcast safe, " } else { "" },
28042827
if commitment_update.is_some() { "a" } else { "no" },
2805-
if raa.is_some() { "an" } else { "no" },
2806-
match order { RAACommitmentOrder::CommitmentFirst => "commitment", RAACommitmentOrder::RevokeAndACKFirst => "RAA"});
2807-
(raa, commitment_update, order, chanmon_update, forwards, failures, htlcs_failed_to_forward, needs_broadcast_safe, funding_locked)
2828+
if revoke_and_ack.is_some() { "an" } else { "no" },
2829+
match raa_commitment_order { RAACommitmentOrder::CommitmentFirst => "commitment", RAACommitmentOrder::RevokeAndACKFirst => "RAA"});
2830+
2831+
ChannelRestoredUpdates {
2832+
revoke_and_ack,
2833+
commitment_update,
2834+
raa_commitment_order,
2835+
chanmon_update,
2836+
pending_forwards,
2837+
pending_failures,
2838+
forwarding_failures,
2839+
needs_broadcast_safe,
2840+
funding_locked,
2841+
shutdown: None,
2842+
}
28082843
}
28092844

28102845
pub fn update_fee<F: Deref>(&mut self, fee_estimator: &F, msg: &msgs::UpdateFee) -> Result<(), ChannelError>
@@ -2891,7 +2926,7 @@ impl<Signer: Sign> Channel<Signer> {
28912926

28922927
/// May panic if some calls other than message-handling calls (which will all Err immediately)
28932928
/// have been called between remove_uncommitted_htlcs_and_mark_paused and this call.
2894-
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 {
2929+
pub fn channel_reestablish<L: Deref>(&mut self, msg: &msgs::ChannelReestablish, logger: &L) -> Result<ChannelRestoredUpdates, ChannelError> where L::Target: Logger {
28952930
if self.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
28962931
// While BOLT 2 doesn't indicate explicitly we should error this channel here, it
28972932
// almost certainly indicates we are going to end up out-of-sync in some way, so we
@@ -2942,15 +2977,38 @@ impl<Signer: Sign> Channel<Signer> {
29422977
return Err(ChannelError::Close("Peer claimed they saw a revoke_and_ack but we haven't sent funding_locked yet".to_owned()));
29432978
}
29442979
// Short circuit the whole handler as there is nothing we can resend them
2945-
return Ok((None, None, None, None, RAACommitmentOrder::CommitmentFirst, Vec::new(), shutdown_msg));
2980+
return Ok(ChannelRestoredUpdates {
2981+
revoke_and_ack: None,
2982+
commitment_update: None,
2983+
raa_commitment_order: RAACommitmentOrder::CommitmentFirst,
2984+
chanmon_update: None,
2985+
pending_forwards: Vec::new(),
2986+
pending_failures: Vec::new(),
2987+
forwarding_failures: Vec::new(),
2988+
needs_broadcast_safe: false,
2989+
funding_locked: None,
2990+
shutdown: shutdown_msg,
2991+
});
29462992
}
29472993

29482994
// We have OurFundingLocked set!
29492995
let next_per_commitment_point = self.holder_signer.get_per_commitment_point(self.cur_holder_commitment_transaction_number, &self.secp_ctx);
2950-
return Ok((Some(msgs::FundingLocked {
2951-
channel_id: self.channel_id(),
2952-
next_per_commitment_point,
2953-
}), None, None, None, RAACommitmentOrder::CommitmentFirst, Vec::new(), shutdown_msg));
2996+
2997+
return Ok(ChannelRestoredUpdates {
2998+
revoke_and_ack: None,
2999+
commitment_update: None,
3000+
raa_commitment_order: RAACommitmentOrder::CommitmentFirst,
3001+
chanmon_update: None,
3002+
pending_forwards: Vec::new(),
3003+
pending_failures: Vec::new(),
3004+
forwarding_failures: Vec::new(),
3005+
needs_broadcast_safe: false,
3006+
funding_locked: Some(msgs::FundingLocked {
3007+
channel_id: self.channel_id(),
3008+
next_per_commitment_point,
3009+
}),
3010+
shutdown: shutdown_msg,
3011+
});
29543012
}
29553013

29563014
let required_revoke = if msg.next_remote_commitment_number + 1 == INITIAL_COMMITMENT_NUMBER - self.cur_holder_commitment_transaction_number {
@@ -2999,14 +3057,47 @@ impl<Signer: Sign> Channel<Signer> {
29993057
Err(ChannelError::Close(msg)) => return Err(ChannelError::Close(msg)),
30003058
Err(ChannelError::Ignore(_)) | Err(ChannelError::CloseDelayBroadcast(_)) => panic!("Got non-channel-failing result from free_holding_cell_htlcs"),
30013059
Ok((Some((commitment_update, monitor_update)), htlcs_to_fail)) => {
3002-
return Ok((resend_funding_locked, required_revoke, Some(commitment_update), Some(monitor_update), self.resend_order.clone(), htlcs_to_fail, shutdown_msg));
3060+
return Ok(ChannelRestoredUpdates {
3061+
revoke_and_ack: required_revoke,
3062+
commitment_update: Some(commitment_update),
3063+
raa_commitment_order: self.resend_order.clone(),
3064+
chanmon_update: Some(monitor_update),
3065+
pending_forwards: Vec::new(),
3066+
pending_failures: Vec::new(),
3067+
forwarding_failures: htlcs_to_fail,
3068+
needs_broadcast_safe: false,
3069+
funding_locked: resend_funding_locked,
3070+
shutdown: shutdown_msg,
3071+
});
30033072
},
30043073
Ok((None, htlcs_to_fail)) => {
3005-
return Ok((resend_funding_locked, required_revoke, None, None, self.resend_order.clone(), htlcs_to_fail, shutdown_msg));
3074+
return Ok(ChannelRestoredUpdates {
3075+
revoke_and_ack: required_revoke,
3076+
commitment_update: None,
3077+
raa_commitment_order: self.resend_order.clone(),
3078+
chanmon_update: None,
3079+
pending_forwards: Vec::new(),
3080+
pending_failures: Vec::new(),
3081+
forwarding_failures: htlcs_to_fail,
3082+
needs_broadcast_safe: false,
3083+
funding_locked: resend_funding_locked,
3084+
shutdown: shutdown_msg,
3085+
});
30063086
},
30073087
}
30083088
} else {
3009-
return Ok((resend_funding_locked, required_revoke, None, None, self.resend_order.clone(), Vec::new(), shutdown_msg));
3089+
return Ok(ChannelRestoredUpdates {
3090+
revoke_and_ack: required_revoke,
3091+
commitment_update: None,
3092+
raa_commitment_order: self.resend_order.clone(),
3093+
chanmon_update: None,
3094+
pending_forwards: Vec::new(),
3095+
pending_failures: Vec::new(),
3096+
forwarding_failures: Vec::new(),
3097+
needs_broadcast_safe: false,
3098+
funding_locked: resend_funding_locked,
3099+
shutdown: shutdown_msg,
3100+
});
30103101
}
30113102
} else if msg.next_local_commitment_number == next_counterparty_commitment_number - 1 {
30123103
if required_revoke.is_some() {
@@ -3017,10 +3108,32 @@ impl<Signer: Sign> Channel<Signer> {
30173108

30183109
if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) != 0 {
30193110
self.monitor_pending_commitment_signed = true;
3020-
return Ok((resend_funding_locked, None, None, None, self.resend_order.clone(), Vec::new(), shutdown_msg));
3111+
return Ok(ChannelRestoredUpdates {
3112+
revoke_and_ack: None,
3113+
commitment_update: None,
3114+
raa_commitment_order: self.resend_order.clone(),
3115+
chanmon_update: None,
3116+
pending_forwards: Vec::new(),
3117+
pending_failures: Vec::new(),
3118+
forwarding_failures: Vec::new(),
3119+
needs_broadcast_safe: false,
3120+
funding_locked: resend_funding_locked,
3121+
shutdown: shutdown_msg,
3122+
});
30213123
}
30223124

3023-
return Ok((resend_funding_locked, required_revoke, Some(self.get_last_commitment_update(logger)), None, self.resend_order.clone(), Vec::new(), shutdown_msg));
3125+
return Ok(ChannelRestoredUpdates {
3126+
revoke_and_ack: required_revoke,
3127+
commitment_update: Some(self.get_last_commitment_update(logger)),
3128+
raa_commitment_order: self.resend_order.clone(),
3129+
chanmon_update: None,
3130+
pending_forwards: Vec::new(),
3131+
pending_failures: Vec::new(),
3132+
forwarding_failures: Vec::new(),
3133+
needs_broadcast_safe: false,
3134+
funding_locked: resend_funding_locked,
3135+
shutdown: shutdown_msg,
3136+
});
30243137
} else {
30253138
return Err(ChannelError::Close("Peer attempted to reestablish channel with a very old remote commitment transaction".to_owned()));
30263139
}

0 commit comments

Comments
 (0)