Skip to content

Commit 2009c23

Browse files
committed
Make Channel::revoke_and_ack's return tuple a struct
This substantially improves readability at the callsite and in the function.
1 parent 0fcd61d commit 2009c23

File tree

2 files changed

+58
-20
lines changed

2 files changed

+58
-20
lines changed

lightning/src/ln/channel.rs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,16 @@ pub enum UpdateFulfillCommitFetch {
339339
DuplicateClaim {},
340340
}
341341

342+
/// The return value of `revoke_and_ack` on success, primarily updates to other channels or HTLC
343+
/// state.
344+
pub(super) struct RAAUpdates {
345+
pub commitment_update: Option<msgs::CommitmentUpdate>,
346+
pub to_forward_htlcs: Vec<(PendingHTLCInfo, u64)>,
347+
pub failed_htlcs: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
348+
pub monitor_update: ChannelMonitorUpdate,
349+
pub holding_cell_failed_htlcs: Vec<(HTLCSource, PaymentHash)>,
350+
}
351+
342352
/// If the majority of the channels funds are to the fundee and the initiator holds only just
343353
/// enough funds to cover their reserve value, channels are at risk of getting "stuck". Because the
344354
/// initiator controls the feerate, if they then go to increase the channel fee, they may have no
@@ -2702,7 +2712,7 @@ impl<Signer: Sign> Channel<Signer> {
27022712
/// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
27032713
/// generating an appropriate error *after* the channel state has been updated based on the
27042714
/// revoke_and_ack message.
2705-
pub fn revoke_and_ack<L: Deref>(&mut self, msg: &msgs::RevokeAndACK, logger: &L) -> Result<(Option<msgs::CommitmentUpdate>, Vec<(PendingHTLCInfo, u64)>, Vec<(HTLCSource, PaymentHash, HTLCFailReason)>, ChannelMonitorUpdate, Vec<(HTLCSource, PaymentHash)>), ChannelError>
2715+
pub fn revoke_and_ack<L: Deref>(&mut self, msg: &msgs::RevokeAndACK, logger: &L) -> Result<RAAUpdates, ChannelError>
27062716
where L::Target: Logger,
27072717
{
27082718
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
@@ -2891,7 +2901,12 @@ impl<Signer: Sign> Channel<Signer> {
28912901
self.monitor_pending_forwards.append(&mut to_forward_infos);
28922902
self.monitor_pending_failures.append(&mut revoked_htlcs);
28932903
log_debug!(logger, "Received a valid revoke_and_ack for channel {} but awaiting a monitor update resolution to reply.", log_bytes!(self.channel_id()));
2894-
return Ok((None, Vec::new(), Vec::new(), monitor_update, Vec::new()))
2904+
return Ok(RAAUpdates {
2905+
commitment_update: None,
2906+
to_forward_htlcs: Vec::new(), failed_htlcs: Vec::new(),
2907+
monitor_update,
2908+
holding_cell_failed_htlcs: Vec::new()
2909+
});
28952910
}
28962911

28972912
match self.free_holding_cell_htlcs(logger)? {
@@ -2910,7 +2925,13 @@ impl<Signer: Sign> Channel<Signer> {
29102925
self.latest_monitor_update_id = monitor_update.update_id;
29112926
monitor_update.updates.append(&mut additional_update.updates);
29122927

2913-
Ok((Some(commitment_update), to_forward_infos, revoked_htlcs, monitor_update, htlcs_to_fail))
2928+
Ok(RAAUpdates {
2929+
commitment_update: Some(commitment_update),
2930+
to_forward_htlcs: to_forward_infos,
2931+
failed_htlcs: revoked_htlcs,
2932+
monitor_update,
2933+
holding_cell_failed_htlcs: htlcs_to_fail
2934+
})
29142935
},
29152936
(None, htlcs_to_fail) => {
29162937
if require_commitment {
@@ -2923,17 +2944,25 @@ impl<Signer: Sign> Channel<Signer> {
29232944

29242945
log_debug!(logger, "Received a valid revoke_and_ack for channel {}. Responding with a commitment update with {} HTLCs failed.",
29252946
log_bytes!(self.channel_id()), update_fail_htlcs.len() + update_fail_malformed_htlcs.len());
2926-
Ok((Some(msgs::CommitmentUpdate {
2927-
update_add_htlcs: Vec::new(),
2928-
update_fulfill_htlcs: Vec::new(),
2929-
update_fail_htlcs,
2930-
update_fail_malformed_htlcs,
2931-
update_fee: None,
2932-
commitment_signed
2933-
}), to_forward_infos, revoked_htlcs, monitor_update, htlcs_to_fail))
2947+
Ok(RAAUpdates {
2948+
commitment_update: Some(msgs::CommitmentUpdate {
2949+
update_add_htlcs: Vec::new(),
2950+
update_fulfill_htlcs: Vec::new(),
2951+
update_fail_htlcs,
2952+
update_fail_malformed_htlcs,
2953+
update_fee: None,
2954+
commitment_signed
2955+
}),
2956+
to_forward_htlcs: to_forward_infos, failed_htlcs: revoked_htlcs,
2957+
monitor_update, holding_cell_failed_htlcs: htlcs_to_fail
2958+
})
29342959
} else {
29352960
log_debug!(logger, "Received a valid revoke_and_ack for channel {} with no reply necessary.", log_bytes!(self.channel_id()));
2936-
Ok((None, to_forward_infos, revoked_htlcs, monitor_update, htlcs_to_fail))
2961+
Ok(RAAUpdates {
2962+
commitment_update: None,
2963+
to_forward_htlcs: to_forward_infos, failed_htlcs: revoked_htlcs,
2964+
monitor_update, holding_cell_failed_htlcs: htlcs_to_fail
2965+
})
29372966
}
29382967
}
29392968
}

lightning/src/ln/channelmanager.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3917,26 +3917,35 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
39173917
break Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
39183918
}
39193919
let was_frozen_for_monitor = chan.get().is_awaiting_monitor_update();
3920-
let (commitment_update, pending_forwards, pending_failures, monitor_update, htlcs_to_fail_in) =
3921-
break_chan_entry!(self, chan.get_mut().revoke_and_ack(&msg, &self.logger), channel_state, chan);
3922-
htlcs_to_fail = htlcs_to_fail_in;
3923-
if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
3920+
//let (commitment_update, pending_forwards, pending_failures, monitor_update, htlcs_to_fail_in) =
3921+
let raa_updates = break_chan_entry!(self,
3922+
chan.get_mut().revoke_and_ack(&msg, &self.logger), channel_state, chan);
3923+
htlcs_to_fail = raa_updates.holding_cell_failed_htlcs;
3924+
if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), raa_updates.monitor_update) {
39243925
if was_frozen_for_monitor {
3925-
assert!(commitment_update.is_none() && pending_forwards.is_empty() && pending_failures.is_empty());
3926+
assert!(raa_updates.commitment_update.is_none());
3927+
assert!(raa_updates.to_forward_htlcs.is_empty());
3928+
assert!(raa_updates.failed_htlcs.is_empty());
39263929
break Err(MsgHandleErrInternal::ignore_no_close("Previous monitor update failure prevented responses to RAA".to_owned()));
39273930
} else {
3928-
if let Err(e) = handle_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, commitment_update.is_some(), pending_forwards, pending_failures) {
3931+
if let Err(e) = handle_monitor_err!(self, e, channel_state, chan,
3932+
RAACommitmentOrder::CommitmentFirst, false,
3933+
raa_updates.commitment_update.is_some(),
3934+
raa_updates.to_forward_htlcs, raa_updates.failed_htlcs) {
39293935
break Err(e);
39303936
} else { unreachable!(); }
39313937
}
39323938
}
3933-
if let Some(updates) = commitment_update {
3939+
if let Some(updates) = raa_updates.commitment_update {
39343940
channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
39353941
node_id: counterparty_node_id.clone(),
39363942
updates,
39373943
});
39383944
}
3939-
break Ok((pending_forwards, pending_failures, chan.get().get_short_channel_id().expect("RAA should only work on a short-id-available channel"), chan.get().get_funding_txo().unwrap()))
3945+
break Ok((raa_updates.to_forward_htlcs, raa_updates.failed_htlcs,
3946+
chan.get().get_short_channel_id()
3947+
.expect("RAA should only work on a short-id-available channel"),
3948+
chan.get().get_funding_txo().unwrap()))
39403949
},
39413950
hash_map::Entry::Vacant(_) => break Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
39423951
}

0 commit comments

Comments
 (0)