Skip to content

Commit

Permalink
use cached lsp pubkey and features
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JssDWt committed Jul 12, 2024
1 parent 10d88bf commit 5371c3c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
40 changes: 25 additions & 15 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())?;

Expand Down
1 change: 0 additions & 1 deletion libs/sdk-core/src/persist/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ impl SqliteStorage {
self.update_setting("lsp-pubkey".to_string(), pubkey)
}

#[allow(dead_code)]
pub fn get_lsp_pubkey(&self) -> PersistResult<Option<String>> {
self.get_setting("lsp-pubkey".to_string())
}
Expand Down

0 comments on commit 5371c3c

Please sign in to comment.