From 5371c3c47ac8176172d218408572f3c685cbadcb Mon Sep 17 00:00:00 2001 From: Jesse de Wit Date: Fri, 12 Jul 2024 09:40:37 +0200 Subject: [PATCH] use cached lsp pubkey and features To determine whether a payment should use trampoline, use the cached lsp pubkey and peer features to save network requests for this check. It saves a network request for lsp_info and a network request to attempt a trampoline payment if trampoline is not supported by the LSP. --- libs/sdk-core/src/breez_services.rs | 40 +++++++++++++++++---------- libs/sdk-core/src/persist/settings.rs | 1 - 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/libs/sdk-core/src/breez_services.rs b/libs/sdk-core/src/breez_services.rs index 6325ac497..3b58acce1 100644 --- a/libs/sdk-core/src/breez_services.rs +++ b/libs/sdk-core/src/breez_services.rs @@ -282,23 +282,33 @@ impl BreezServices { return Err(SendPaymentError::AlreadyPaid); } - // If there is an lsp and the invoice route hint does not contain the - // lsp in the hint, attempt a trampoline payment. - let maybe_trampoline_id = match self.lsp_info().await { - Ok(lsp_info) => { - let lsp_pubkey = hex::encode(&lsp_info.lsp_pubkey); - match parsed_invoice.routing_hints.iter().any(|hint| { - hint.hops - .last() - .map(|hop| hop.src_node_id == lsp_pubkey) - .unwrap_or(false) - }) { - true => None, - false => Some(lsp_info.lsp_pubkey), + // If there is an lsp, the invoice route hint does not contain the + // lsp in the hint, and the lsp supports trampoline payments, attempt a + // trampoline payment. + let mut maybe_trampoline_id = None; + if let Some(lsp_pubkey) = self.persister.get_lsp_pubkey()? { + if !parsed_invoice.routing_hints.iter().any(|hint| { + hint.hops + .last() + .map(|hop| hop.src_node_id == lsp_pubkey) + .unwrap_or(false) + }) { + let node_state = self.node_info()?; + if let Some(peer) = node_state + .connected_peers + .iter() + .find(|peer| peer.id == lsp_pubkey) + { + if peer.features.trampoline { + maybe_trampoline_id = Some(hex::decode(lsp_pubkey).map_err(|_| { + SendPaymentError::Generic { + err: "failed to decode lsp pubkey".to_string(), + } + })?); + } } } - Err(_) => None, - }; + } self.persist_pending_payment(&parsed_invoice, amount_msat, req.label.clone())?; diff --git a/libs/sdk-core/src/persist/settings.rs b/libs/sdk-core/src/persist/settings.rs index 2272a7ddd..9a82ba690 100644 --- a/libs/sdk-core/src/persist/settings.rs +++ b/libs/sdk-core/src/persist/settings.rs @@ -63,7 +63,6 @@ impl SqliteStorage { self.update_setting("lsp-pubkey".to_string(), pubkey) } - #[allow(dead_code)] pub fn get_lsp_pubkey(&self) -> PersistResult> { self.get_setting("lsp-pubkey".to_string()) }