]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Decode onion fail outside of outbound_payments lock
authorValentine Wallace <vwallace@protonmail.com>
Tue, 24 Jan 2023 19:26:57 +0000 (14:26 -0500)
committerValentine Wallace <vwallace@protonmail.com>
Wed, 25 Jan 2023 19:44:07 +0000 (14:44 -0500)
It's not ideal to do all this computation while the lock is held. We also want
to decode the failure *before* taking the lock, so we can store the failed scid
in the relevant outbound for retry in the next commit(s).

lightning/src/ln/outbound_payment.rs

index a4211ea34d6912d7283a09105258c14a143f32b1..0a5a964b33b54a0da359fb1a698e886476602dc6 100644 (file)
@@ -783,6 +783,11 @@ impl OutboundPayments {
                payment_params: &Option<PaymentParameters>, probing_cookie_secret: [u8; 32],
                secp_ctx: &Secp256k1<secp256k1::All>, pending_events: &Mutex<Vec<events::Event>>, logger: &L
        ) where L::Target: Logger {
+               #[cfg(test)]
+               let (network_update, short_channel_id, payment_retryable, onion_error_code, onion_error_data) = onion_error.decode_onion_failure(secp_ctx, logger, &source);
+               #[cfg(not(test))]
+               let (network_update, short_channel_id, payment_retryable, _, _) = onion_error.decode_onion_failure(secp_ctx, logger, &source);
+
                let mut session_priv_bytes = [0; 32];
                session_priv_bytes.copy_from_slice(&session_priv[..]);
                let mut outbounds = self.pending_outbound_payments.lock().unwrap();
@@ -811,6 +816,7 @@ impl OutboundPayments {
                        log_trace!(logger, "Received duplicative fail for HTLC with payment_hash {}", log_bytes!(payment_hash.0));
                        return
                }
+               core::mem::drop(outbounds);
                let mut retry = if let Some(payment_params_data) = payment_params {
                        let path_last_hop = path.last().expect("Outbound payments must have had a valid path");
                        Some(RouteParameters {
@@ -822,11 +828,6 @@ impl OutboundPayments {
                log_trace!(logger, "Failing outbound payment HTLC with payment_hash {}", log_bytes!(payment_hash.0));
 
                let path_failure = {
-                       #[cfg(test)]
-                       let (network_update, short_channel_id, payment_retryable, onion_error_code, onion_error_data) = onion_error.decode_onion_failure(secp_ctx, logger, &source);
-                       #[cfg(not(test))]
-                       let (network_update, short_channel_id, payment_retryable, _, _) = onion_error.decode_onion_failure(secp_ctx, logger, &source);
-
                        if payment_is_probe(payment_hash, &payment_id, probing_cookie_secret) {
                                if !payment_retryable {
                                        events::Event::ProbeSuccessful {