Skip to content

Commit 9ddf1e6

Browse files
committed
Fix unsafe unwraps while getting fail data
Also refactored this section in `get_htlc_failure_reason_from_failure_code` to use nested if let instead of checking .is_none and unwrapping because I thought it made the control flow clearer.
1 parent cdf4fd2 commit 9ddf1e6

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

lightning/src/ln/channelmanager.rs

+20-16
Original file line numberDiff line numberDiff line change
@@ -3525,25 +3525,29 @@ where
35253525
FailureCode::TemporaryNodeFailure => HTLCFailReason::from_failure_code(*failure_code as u16),
35263526
FailureCode::RequiredNodeFeatureMissing => HTLCFailReason::from_failure_code(*failure_code as u16),
35273527
FailureCode::ExpiryTooSoon => {
3528+
let mut expiry_fail_reason = None;
35283529
let short_channel_id = htlc.prev_hop.short_channel_id;
3529-
let (counterparty_node_id, chan_id) = self.short_to_chan_info.read().unwrap().get(&short_channel_id).cloned().unwrap();
3530-
let per_peer_state = self.per_peer_state.read().unwrap();
3531-
let peer_state = per_peer_state.get(&counterparty_node_id);
3532-
if peer_state.is_none() {
3533-
return self.get_htlc_fail_reason_from_failure_code(&FailureCode::IncorrectOrUnknownPaymentDetails, htlc)
3530+
let cp_node_chan_id = self.short_to_chan_info.read().unwrap().get(&short_channel_id).cloned();
3531+
if let Some((counterparty_node_id, chan_id)) = cp_node_chan_id {
3532+
let per_peer_state = self.per_peer_state.read().unwrap();
3533+
if let Some(peer_state) = per_peer_state.get(&counterparty_node_id) {
3534+
let peer_state = peer_state.lock().unwrap();
3535+
if let Some(chan) = peer_state.channel_by_id.get(&chan_id) {
3536+
if let Ok(channel_update) = self.get_channel_update_for_onion(short_channel_id, chan) {
3537+
let mut channel_update_data = Vec::new();
3538+
(channel_update.serialized_length() as u16 + 2).write(&mut channel_update_data).expect("Writes cannot fail");
3539+
msgs::ChannelUpdate::TYPE.write(&mut channel_update_data).expect("Writes cannot fail");
3540+
channel_update.write(&mut channel_update_data).expect("Writes cannot fail");
3541+
expiry_fail_reason = Some(HTLCFailReason::reason(*failure_code as u16, channel_update_data));
3542+
}
3543+
}
3544+
}
35343545
}
3535-
let peer_state = peer_state.unwrap().lock().unwrap();
3536-
let chan = peer_state.channel_by_id.get(&chan_id);
3537-
if chan.is_none() {
3538-
return self.get_htlc_fail_reason_from_failure_code(&FailureCode::IncorrectOrUnknownPaymentDetails, htlc)
3546+
if let Some(expiry_fail_reason) = expiry_fail_reason {
3547+
expiry_fail_reason
3548+
} else {
3549+
self.get_htlc_fail_reason_from_failure_code(&FailureCode::IncorrectOrUnknownPaymentDetails, htlc)
35393550
}
3540-
let chan = chan.unwrap();
3541-
let channel_update = self.get_channel_update_for_onion(short_channel_id, chan).unwrap();
3542-
let mut channel_update_data = Vec::new();
3543-
(channel_update.serialized_length() as u16 + 2).write(&mut channel_update_data).expect("Writes cannot fail");
3544-
msgs::ChannelUpdate::TYPE.write(&mut channel_update_data).expect("Writes cannot fail");
3545-
channel_update.write(&mut channel_update_data).expect("Writes cannot fail");
3546-
HTLCFailReason::reason(*failure_code as u16, channel_update_data)
35473551
},
35483552
FailureCode::IncorrectOrUnknownPaymentDetails => {
35493553
let mut htlc_msat_height_data = htlc.value.to_be_bytes().to_vec();

0 commit comments

Comments
 (0)