From a3c31f60f9e98a5bd360315078eebc01194eb31e Mon Sep 17 00:00:00 2001 From: Valentine Wallace Date: Wed, 23 Oct 2024 17:57:18 -0700 Subject: [PATCH] f avoid unncessarily remove'ing InvoiceReceived entries --- lightning/src/ln/outbound_payment.rs | 45 +++++++++++++--------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/lightning/src/ln/outbound_payment.rs b/lightning/src/ln/outbound_payment.rs index 88b112c89e6..b50d09a69af 100644 --- a/lightning/src/ln/outbound_payment.rs +++ b/lightning/src/ln/outbound_payment.rs @@ -881,7 +881,7 @@ impl OutboundPayments { route_params.max_total_routing_fee_msat = Some(max_fee_msat); } self.send_payment_for_bolt12_invoice_internal( - payment_id, payment_hash, None, route_params, retry_strategy, router, first_hops, + payment_id, payment_hash, None, None, route_params, retry_strategy, router, first_hops, inflight_htlcs, entropy_source, node_signer, node_id_lookup, secp_ctx, best_block_height, logger, pending_events, send_payment_along_path ) @@ -891,10 +891,10 @@ impl OutboundPayments { R: Deref, ES: Deref, NS: Deref, NL: Deref, IH, SP, L: Deref >( &self, payment_id: PaymentId, payment_hash: PaymentHash, - keysend_preimage: Option, mut route_params: RouteParameters, - retry_strategy: Retry, router: &R, first_hops: Vec, inflight_htlcs: IH, - entropy_source: &ES, node_signer: &NS, node_id_lookup: &NL, - secp_ctx: &Secp256k1, best_block_height: u32, logger: &L, + keysend_preimage: Option, invoice_request: Option<&InvoiceRequest>, + mut route_params: RouteParameters, retry_strategy: Retry, router: &R, + first_hops: Vec, inflight_htlcs: IH, entropy_source: &ES, node_signer: &NS, + node_id_lookup: &NL, secp_ctx: &Secp256k1, best_block_height: u32, logger: &L, pending_events: &Mutex)>>, send_payment_along_path: SP, ) -> Result<(), Bolt12PaymentError> @@ -952,27 +952,21 @@ impl OutboundPayments { payment_hash, recipient_onion.clone(), keysend_preimage, &route, Some(retry_strategy), payment_params, entropy_source, best_block_height ); - let mut invoice_request_opt = None; - let mut outbounds = self.pending_outbound_payments.lock().unwrap(); - match outbounds.entry(payment_id) { - hash_map::Entry::Occupied(entry) => match entry.remove() { - PendingOutboundPayment::InvoiceReceived { .. } => { - outbounds.insert(payment_id, retryable_payment); - }, - PendingOutboundPayment::StaticInvoiceReceived { invoice_request, .. } => { - invoice_request_opt = Some(invoice_request); - outbounds.insert(payment_id, retryable_payment); + match self.pending_outbound_payments.lock().unwrap().entry(payment_id) { + hash_map::Entry::Occupied(entry) => match entry.get() { + PendingOutboundPayment::InvoiceReceived { .. } + | PendingOutboundPayment::StaticInvoiceReceived { .. } => { + *entry.into_mut() = retryable_payment; }, _ => return Err(Bolt12PaymentError::DuplicateInvoice), }, hash_map::Entry::Vacant(_) => return Err(Bolt12PaymentError::UnexpectedInvoice), } - core::mem::drop(outbounds); let result = self.pay_route_internal( - &route, payment_hash, &recipient_onion, keysend_preimage, invoice_request_opt.as_ref(), - payment_id, Some(route_params.final_value_msat), onion_session_privs, node_signer, - best_block_height, &send_payment_along_path + &route, payment_hash, &recipient_onion, keysend_preimage, invoice_request, payment_id, + Some(route_params.final_value_msat), onion_session_privs, node_signer, best_block_height, + &send_payment_along_path ); log_info!( logger, "Sending payment with id {} and hash {} returned {:?}", payment_id, @@ -1088,13 +1082,14 @@ impl OutboundPayments { IH: Fn() -> InFlightHtlcs, SP: Fn(SendAlongPathArgs) -> Result<(), APIError>, { - let (payment_hash, keysend_preimage, route_params, retry_strategy) = + let (payment_hash, keysend_preimage, route_params, retry_strategy, invoice_request) = match self.pending_outbound_payments.lock().unwrap().entry(payment_id) { hash_map::Entry::Occupied(entry) => match entry.get() { PendingOutboundPayment::StaticInvoiceReceived { - payment_hash, route_params, retry_strategy, keysend_preimage, .. + payment_hash, route_params, retry_strategy, keysend_preimage, invoice_request, .. } => { - (*payment_hash, *keysend_preimage, route_params.clone(), *retry_strategy) + (*payment_hash, *keysend_preimage, route_params.clone(), *retry_strategy, + invoice_request.clone()) }, _ => return Err(Bolt12PaymentError::DuplicateInvoice), }, @@ -1102,9 +1097,9 @@ impl OutboundPayments { }; self.send_payment_for_bolt12_invoice_internal( - payment_id, payment_hash, Some(keysend_preimage), route_params, retry_strategy, router, - first_hops, inflight_htlcs, entropy_source, node_signer, node_id_lookup, secp_ctx, - best_block_height, logger, pending_events, send_payment_along_path + payment_id, payment_hash, Some(keysend_preimage), Some(&invoice_request), route_params, + retry_strategy, router, first_hops, inflight_htlcs, entropy_source, node_signer, + node_id_lookup, secp_ctx, best_block_height, logger, pending_events, send_payment_along_path ) }