Skip to content

Commit 7b31712

Browse files
Struct-ify decoded onion failures
To avoid several long hard-to-read tuple return values.
1 parent fb9ad56 commit 7b31712

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

lightning/src/ln/onion_utils.rs

+37-10
Original file line numberDiff line numberDiff line change
@@ -396,15 +396,22 @@ pub(super) fn build_first_hop_failure_packet(shared_secret: &[u8], failure_type:
396396
encrypt_failure_packet(shared_secret, &failure_packet.encode()[..])
397397
}
398398

399+
pub(crate) struct DecodedOnionFailure {
400+
pub(crate) network_update: Option<NetworkUpdate>,
401+
pub(crate) short_channel_id: Option<u64>,
402+
pub(crate) payment_retryable: bool,
403+
#[cfg(test)]
404+
pub(crate) onion_error_code: Option<u16>,
405+
#[cfg(test)]
406+
pub(crate) onion_error_data: Option<Vec<u8>>,
407+
}
408+
399409
/// Process failure we got back from upstream on a payment we sent (implying htlc_source is an
400410
/// OutboundRoute).
401-
/// Returns update, a boolean indicating that the payment itself failed, the short channel id of
402-
/// the responsible channel, and the error code.
403411
#[inline]
404412
pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(
405413
secp_ctx: &Secp256k1<T>, logger: &L, htlc_source: &HTLCSource, mut packet_decrypted: Vec<u8>
406-
) -> (Option<NetworkUpdate>, Option<u64>, bool, Option<u16>, Option<Vec<u8>>)
407-
where L::Target: Logger {
414+
) -> DecodedOnionFailure where L::Target: Logger {
408415
let (path, session_priv, first_hop_htlc_msat) = if let &HTLCSource::OutboundRoute {
409416
ref path, ref session_priv, ref first_hop_htlc_msat, ..
410417
} = htlc_source {
@@ -634,12 +641,24 @@ where L::Target: Logger {
634641
log_info!(logger, "Onion Error[from {}: {}({:#x})] {}", route_hop.pubkey, title, error_code, description);
635642
}
636643
}).expect("Route that we sent via spontaneously grew invalid keys in the middle of it?");
637-
if let Some((channel_update, short_channel_id, payment_retryable)) = res {
638-
(channel_update, short_channel_id, payment_retryable, error_code_ret, error_packet_ret)
644+
if let Some((network_update, short_channel_id, payment_retryable)) = res {
645+
DecodedOnionFailure {
646+
network_update, short_channel_id, payment_retryable,
647+
#[cfg(test)]
648+
onion_error_code: error_code_ret,
649+
#[cfg(test)]
650+
onion_error_data: error_packet_ret
651+
}
639652
} else {
640653
// only not set either packet unparseable or hmac does not match with any
641654
// payment not retryable only when garbage is from the final node
642-
(None, None, !is_from_final_node, None, None)
655+
DecodedOnionFailure {
656+
network_update: None, short_channel_id: None, payment_retryable: !is_from_final_node,
657+
#[cfg(test)]
658+
onion_error_code: None,
659+
#[cfg(test)]
660+
onion_error_data: None
661+
}
643662
}
644663
}
645664

@@ -763,20 +782,28 @@ impl HTLCFailReason {
763782

764783
pub(super) fn decode_onion_failure<T: secp256k1::Signing, L: Deref>(
765784
&self, secp_ctx: &Secp256k1<T>, logger: &L, htlc_source: &HTLCSource
766-
) -> (Option<NetworkUpdate>, Option<u64>, bool, Option<u16>, Option<Vec<u8>>)
767-
where L::Target: Logger {
785+
) -> DecodedOnionFailure where L::Target: Logger {
768786
match self.0 {
769787
HTLCFailReasonRepr::LightningError { ref err } => {
770788
process_onion_failure(secp_ctx, logger, &htlc_source, err.data.clone())
771789
},
790+
#[allow(unused)]
772791
HTLCFailReasonRepr::Reason { ref failure_code, ref data, .. } => {
773792
// we get a fail_malformed_htlc from the first hop
774793
// TODO: We'd like to generate a NetworkUpdate for temporary
775794
// failures here, but that would be insufficient as find_route
776795
// generally ignores its view of our own channels as we provide them via
777796
// ChannelDetails.
778797
if let &HTLCSource::OutboundRoute { ref path, .. } = htlc_source {
779-
(None, Some(path.hops[0].short_channel_id), true, Some(*failure_code), Some(data.clone()))
798+
DecodedOnionFailure {
799+
network_update: None,
800+
payment_retryable: true,
801+
short_channel_id: Some(path.hops[0].short_channel_id),
802+
#[cfg(test)]
803+
onion_error_code: Some(*failure_code),
804+
#[cfg(test)]
805+
onion_error_data: Some(data.clone()),
806+
}
780807
} else { unreachable!(); }
781808
}
782809
}

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;
@@ -1293,9 +1293,12 @@ impl OutboundPayments {
12931293
pending_events: &Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>, logger: &L,
12941294
) -> bool where L::Target: Logger {
12951295
#[cfg(test)]
1296-
let (network_update, short_channel_id, payment_retryable, onion_error_code, onion_error_data) = onion_error.decode_onion_failure(secp_ctx, logger, &source);
1296+
let DecodedOnionFailure {
1297+
network_update, short_channel_id, payment_retryable, onion_error_code, onion_error_data
1298+
} = onion_error.decode_onion_failure(secp_ctx, logger, &source);
12971299
#[cfg(not(test))]
1298-
let (network_update, short_channel_id, payment_retryable, _, _) = onion_error.decode_onion_failure(secp_ctx, logger, &source);
1300+
let DecodedOnionFailure { network_update, short_channel_id, payment_retryable } =
1301+
onion_error.decode_onion_failure(secp_ctx, logger, &source);
12991302

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

0 commit comments

Comments
 (0)