Skip to content

Commit 01a730d

Browse files
jkczyzvalentinewallace
authored andcommitted
Simplify create_pending_htlc_status closure
Remove need for checking the result of the closure by: - Passing pending_forward_info by value with move semantics - Removing Option from the return type - Returning either a new PendingHTLCStatus or the passed pending_forward_info
1 parent da6f99d commit 01a730d

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

lightning/src/ln/channel.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,16 +1734,14 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
17341734
}
17351735

17361736
pub fn update_add_htlc<F>(&mut self, msg: &msgs::UpdateAddHTLC, mut pending_forward_state: PendingHTLCStatus, create_pending_htlc_status: F) -> Result<(), ChannelError>
1737-
where F: for<'a, 'b> Fn(&'a Self, &'b PendingHTLCStatus, u16) -> Option<PendingHTLCStatus> {
1737+
where F: for<'a> Fn(&'a Self, PendingHTLCStatus, u16) -> PendingHTLCStatus {
17381738
if !self.is_usable() {
17391739
// TODO: Note that |20 is defined as "channel FROM the processing
17401740
// node has been disabled" (emphasis mine), which seems to imply
17411741
// that we can't return |20 for an inbound channel being disabled.
17421742
// This probably needs a spec update but should definitely be
17431743
// allowed.
1744-
if let Some(new_state) = create_pending_htlc_status(self, &pending_forward_state, 0x1000|20) {
1745-
pending_forward_state = new_state;
1746-
}
1744+
pending_forward_state = create_pending_htlc_status(self, pending_forward_state, 0x1000|20);
17471745
}
17481746
if (self.channel_state & (ChannelState::ChannelFunded as u32 | ChannelState::RemoteShutdownSent as u32)) != (ChannelState::ChannelFunded as u32) {
17491747
return Err(ChannelError::Close("Got add HTLC message when channel was not in an operational state"));
@@ -1817,9 +1815,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
18171815
// but should help protect us from stuck channels).
18181816
let remote_fee_cost_incl_stuck_buffer_msat = 2 * self.next_remote_commit_tx_fee_msat(1 + 1);
18191817
if pending_remote_value_msat - msg.amount_msat - chan_reserve_msat < remote_fee_cost_incl_stuck_buffer_msat {
1820-
if let Some(new_state) = create_pending_htlc_status(self, &pending_forward_state, 0x1000|7) {
1821-
pending_forward_state = new_state;
1822-
}
1818+
pending_forward_state = create_pending_htlc_status(self, pending_forward_state, 0x1000|7);
18231819
}
18241820
} else {
18251821
// Check that they won't violate our local required channel reserve by adding this HTLC.
@@ -4696,14 +4692,14 @@ mod tests {
46964692
amt_to_forward: max_can_recv + 1,
46974693
outgoing_cltv_value: htlc_cltv
46984694
});
4699-
let create_pending_htlc_status = |chan: &Channel<EnforcingChannelKeys>, _pending_forward_info: &PendingHTLCStatus, _error_code: u16| {
4700-
Some(PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(
4695+
let create_pending_htlc_status = |chan: &Channel<EnforcingChannelKeys>, _pending_forward_info: PendingHTLCStatus, _error_code: u16| {
4696+
PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(
47014697
UpdateFailHTLC{
47024698
channel_id: chan.channel_id,
47034699
htlc_id: 0,
47044700
reason: OnionErrorPacket { data: vec![] },
47054701
}
4706-
)))
4702+
))
47074703
};
47084704

47094705
// Assert that the HTLC was successfully added.
@@ -4718,14 +4714,14 @@ mod tests {
47184714
}
47194715

47204716
// Check that adding an HTLC worth 1 msat less will succeed.
4721-
let create_pending_htlc_status = |chan: &Channel<EnforcingChannelKeys>, _pending_forward_info: &PendingHTLCStatus, _error_code: u16| {
4722-
Some(PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(
4717+
let create_pending_htlc_status = |chan: &Channel<EnforcingChannelKeys>, _pending_forward_info: PendingHTLCStatus, _error_code: u16| {
4718+
PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(
47234719
UpdateFailHTLC{
47244720
channel_id: chan.channel_id,
47254721
htlc_id: 0,
47264722
reason: OnionErrorPacket { data: vec![] },
47274723
}
4728-
)))
4724+
))
47294725
};
47304726
chans[1].pending_inbound_htlcs = vec![];
47314727
let pending_forward_state = PendingHTLCStatus::Forward(PendingHTLCInfo{
@@ -4801,8 +4797,8 @@ mod tests {
48014797
outgoing_cltv_value: htlc_cltv
48024798
});
48034799

4804-
let create_pending_htlc_status = |_chan: &Channel<EnforcingChannelKeys>, _pending_forward_info: &PendingHTLCStatus, _error_code: u16| {
4805-
None
4800+
let create_pending_htlc_status = |_chan: &Channel<EnforcingChannelKeys>, pending_forward_info: PendingHTLCStatus, _error_code: u16| {
4801+
pending_forward_info
48064802
};
48074803

48084804
match chans[0].update_add_htlc(&msg, pending_forward_state, &create_pending_htlc_status) {

lightning/src/ln/channelmanager.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,12 +2504,12 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
25042504
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
25052505
}
25062506

2507-
let create_pending_htlc_status = |chan: &Channel<ChanSigner>, pending_forward_info: &PendingHTLCStatus, error_code: u16| {
2507+
let create_pending_htlc_status = |chan: &Channel<ChanSigner>, pending_forward_info: PendingHTLCStatus, error_code: u16| {
25082508
// If the update_add is completely bogus, the call will Err and we will close,
25092509
// but if we've sent a shutdown and they haven't acknowledged it yet, we just
25102510
// want to reject the new HTLC and fail it backwards instead of forwarding.
25112511
match pending_forward_info {
2512-
&PendingHTLCStatus::Forward(PendingHTLCInfo { ref incoming_shared_secret, .. }) => {
2512+
PendingHTLCStatus::Forward(PendingHTLCInfo { ref incoming_shared_secret, .. }) => {
25132513
let mut reason = onion_utils::build_first_hop_failure_packet(incoming_shared_secret, 0x4000|10, &[]);
25142514
// The only case where we'd be unable to successfully get a channel
25152515
// update here is if the channel isn't in the fully-funded
@@ -2530,9 +2530,9 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
25302530
htlc_id: msg.htlc_id,
25312531
reason
25322532
};
2533-
Some(PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msg)))
2533+
PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msg))
25342534
},
2535-
_ => None
2535+
_ => pending_forward_info
25362536
}
25372537
};
25382538
try_chan_entry!(self, chan.get_mut().update_add_htlc(&msg, pending_forward_info, create_pending_htlc_status), channel_state, chan);

0 commit comments

Comments
 (0)