Skip to content

Commit b694acd

Browse files
Struct-ify decoded onion failures
To avoid several long hard-to-read tuple return values
1 parent 924aa2b commit b694acd

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

lightning/src/ln/onion_utils.rs

+36-9
Original file line numberDiff line numberDiff line change
@@ -391,15 +391,22 @@ pub(super) fn build_first_hop_failure_packet(shared_secret: &[u8], failure_type:
391391
encrypt_failure_packet(shared_secret, &failure_packet.encode()[..])
392392
}
393393

394+
pub(crate) struct DecodedOnionFailure {
395+
pub(crate) network_update: Option<NetworkUpdate>,
396+
pub(crate) short_channel_id: Option<u64>,
397+
pub(crate) payment_retryable: bool,
398+
#[cfg(test)]
399+
pub(crate) onion_error_code: Option<u16>,
400+
#[cfg(test)]
401+
pub(crate) onion_error_data: Option<Vec<u8>>,
402+
}
403+
394404
/// Process failure we got back from upstream on a payment we sent (implying htlc_source is an
395405
/// OutboundRoute).
396-
/// Returns update, a boolean indicating that the payment itself failed, the short channel id of
397-
/// the responsible channel, and the error code.
398406
#[inline]
399407
pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(
400408
secp_ctx: &Secp256k1<T>, logger: &L, htlc_source: &HTLCSource, mut packet_decrypted: Vec<u8>
401-
) -> (Option<NetworkUpdate>, Option<u64>, bool, Option<u16>, Option<Vec<u8>>)
402-
where L::Target: Logger {
409+
) -> DecodedOnionFailure where L::Target: Logger {
403410
let (path, session_priv, first_hop_htlc_msat) =
404411
if let &HTLCSource::OutboundRoute { ref path, ref session_priv, ref first_hop_htlc_msat, .. } = htlc_source {
405412
(path, session_priv, first_hop_htlc_msat)
@@ -629,11 +636,23 @@ where L::Target: Logger {
629636
}
630637
}).expect("Route that we sent via spontaneously grew invalid keys in the middle of it?");
631638
if let Some((channel_update, short_channel_id, payment_retryable)) = res {
632-
(channel_update, short_channel_id, payment_retryable, error_code_ret, error_packet_ret)
639+
DecodedOnionFailure {
640+
network_update: channel_update, short_channel_id, payment_retryable,
641+
#[cfg(test)]
642+
onion_error_code: error_code_ret,
643+
#[cfg(test)]
644+
onion_error_data: error_packet_ret
645+
}
633646
} else {
634647
// only not set either packet unparseable or hmac does not match with any
635648
// payment not retryable only when garbage is from the final node
636-
(None, None, !is_from_final_node, None, None)
649+
DecodedOnionFailure {
650+
network_update: None, short_channel_id: None, payment_retryable: !is_from_final_node,
651+
#[cfg(test)]
652+
onion_error_code: None,
653+
#[cfg(test)]
654+
onion_error_data: None
655+
}
637656
}
638657
}
639658

@@ -757,20 +776,28 @@ impl HTLCFailReason {
757776

758777
pub(super) fn decode_onion_failure<T: secp256k1::Signing, L: Deref>(
759778
&self, secp_ctx: &Secp256k1<T>, logger: &L, htlc_source: &HTLCSource
760-
) -> (Option<NetworkUpdate>, Option<u64>, bool, Option<u16>, Option<Vec<u8>>)
761-
where L::Target: Logger {
779+
) -> DecodedOnionFailure where L::Target: Logger {
762780
match self.0 {
763781
HTLCFailReasonRepr::LightningError { ref err } => {
764782
process_onion_failure(secp_ctx, logger, &htlc_source, err.data.clone())
765783
},
784+
#[allow(unused)]
766785
HTLCFailReasonRepr::Reason { ref failure_code, ref data, .. } => {
767786
// we get a fail_malformed_htlc from the first hop
768787
// TODO: We'd like to generate a NetworkUpdate for temporary
769788
// failures here, but that would be insufficient as find_route
770789
// generally ignores its view of our own channels as we provide them via
771790
// ChannelDetails.
772791
if let &HTLCSource::OutboundRoute { ref path, .. } = htlc_source {
773-
(None, Some(path.hops[0].short_channel_id), true, Some(*failure_code), Some(data.clone()))
792+
DecodedOnionFailure {
793+
network_update: None,
794+
payment_retryable: true,
795+
short_channel_id: Some(path.hops[0].short_channel_id),
796+
#[cfg(test)]
797+
onion_error_code: Some(*failure_code),
798+
#[cfg(test)]
799+
onion_error_data: Some(data.clone()),
800+
}
774801
} else { unreachable!(); }
775802
}
776803
}

lightning/src/ln/outbound_payment.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::sign::{EntropySource, NodeSigner, Recipient};
1717
use crate::events::{self, PaymentFailureReason};
1818
use crate::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
1919
use crate::ln::channelmanager::{ChannelDetails, EventCompletionAction, HTLCSource, IDEMPOTENCY_TIMEOUT_TICKS, PaymentId};
20-
use crate::ln::onion_utils::HTLCFailReason;
20+
use crate::ln::onion_utils::{DecodedOnionFailure, HTLCFailReason};
2121
use crate::routing::router::{InFlightHtlcs, Path, PaymentParameters, Route, RouteParameters, Router};
2222
use crate::util::errors::APIError;
2323
use crate::util::logger::Logger;
@@ -1234,9 +1234,12 @@ impl OutboundPayments {
12341234
pending_events: &Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>, logger: &L,
12351235
) -> bool where L::Target: Logger {
12361236
#[cfg(test)]
1237-
let (network_update, short_channel_id, payment_retryable, onion_error_code, onion_error_data) = onion_error.decode_onion_failure(secp_ctx, logger, &source);
1237+
let DecodedOnionFailure {
1238+
network_update, short_channel_id, payment_retryable, onion_error_code, onion_error_data
1239+
} = onion_error.decode_onion_failure(secp_ctx, logger, &source);
12381240
#[cfg(not(test))]
1239-
let (network_update, short_channel_id, payment_retryable, _, _) = onion_error.decode_onion_failure(secp_ctx, logger, &source);
1241+
let DecodedOnionFailure { network_update, short_channel_id, payment_retryable, } =
1242+
onion_error.decode_onion_failure(secp_ctx, logger, &source);
12401243

12411244
let payment_is_probe = payment_is_probe(payment_hash, &payment_id, probing_cookie_secret);
12421245
let mut session_priv_bytes = [0; 32];

0 commit comments

Comments
 (0)