Skip to content

Commit

Permalink
Add skip cache option to get latest LSP information
Browse files Browse the repository at this point in the history
  • Loading branch information
dangeross committed Apr 8, 2024
1 parent 14c8b31 commit cbaebd2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 35 deletions.
2 changes: 1 addition & 1 deletion libs/sdk-bindings/src/uniffi_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl BlockingBreezServices {
}

pub fn lsp_info(&self) -> SdkResult<LspInformation> {
rt().block_on(self.breez_services.lsp_info())
rt().block_on(self.breez_services.lsp_info(false))
}

pub fn open_channel_fee(
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk-core/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ pub fn fetch_lsp_info(id: String) -> Result<Option<LspInformation>> {

/// See [BreezServices::lsp_info]
pub fn lsp_info() -> Result<LspInformation> {
block_on(async { get_breez_services().await?.lsp_info().await })
block_on(async { get_breez_services().await?.lsp_info(false).await })
.map_err(anyhow::Error::new::<SdkError>)
}

Expand Down
72 changes: 40 additions & 32 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ impl BreezServices {
&self,
req: OpenChannelFeeRequest,
) -> SdkResult<OpenChannelFeeResponse> {
let lsp_info = self.lsp_info().await?;
let lsp_info = self.lsp_info(false).await?;
let fee_params = lsp_info
.cheapest_open_channel_fee(req.expiry.unwrap_or(INVOICE_PAYMENT_FEE_EXPIRY_SECONDS))?
.clone();
Expand All @@ -719,7 +719,7 @@ impl BreezServices {
/// Should be called when the user wants to close all the channels.
pub async fn close_lsp_channels(&self) -> SdkResult<Vec<String>> {
self.start_node().await?;
let lsp = self.lsp_info().await?;
let lsp = self.lsp_info(false).await?;
let tx_ids = self.node_api.close_peer_channels(lsp.pubkey).await?;
self.sync().await?;
Ok(tx_ids)
Expand Down Expand Up @@ -747,7 +747,7 @@ impl BreezServices {
)});
}
let channel_opening_fees = req.opening_fee_params.unwrap_or(
self.lsp_info()
self.lsp_info(true)
.await?
.cheapest_open_channel_fee(SWAP_PAYMENT_FEE_EXPIRY_SECONDS)?
.clone(),
Expand Down Expand Up @@ -1271,27 +1271,8 @@ impl BreezServices {
}

/// Convenience method to look up LSP info based on current LSP ID
pub async fn lsp_info(&self) -> SdkResult<LspInformation> {
let persisted_lsp_info = self.persister.get_lsp_information()?.and_then(|lsp_info| {
match lsp_info.opening_fee_params_list.is_valid() {
true => Some(lsp_info),
false => {
debug!("Ignoring invalid LSP information: {:?}", lsp_info);
None
}
}
});
match persisted_lsp_info {
Some(lsp_info) => {
debug!("Using persisted LSP information");
Ok(lsp_info)
}
None => {
let lsp_info = get_lsp(self.persister.clone(), self.lsp_api.clone()).await?;
self.persister.set_lsp_information(&lsp_info)?;
Ok(lsp_info)
}
}
pub async fn lsp_info(&self, skip_cache: bool) -> SdkResult<LspInformation> {
Ok(get_lsp(self.persister.clone(), self.lsp_api.clone(), skip_cache).await?)
}

pub(crate) async fn start_node(&self) -> Result<()> {
Expand Down Expand Up @@ -1871,7 +1852,7 @@ impl BreezServices {
let message = webhook_url.clone();
let sign_request = SignMessageRequest { message };
let sign_response = self.sign_message(sign_request).await?;
let lsp_info = self.lsp_info().await?;
let lsp_info = self.lsp_info(false).await?;
self.lsp_api
.register_payment_notifications(
lsp_info.id,
Expand Down Expand Up @@ -2385,7 +2366,6 @@ impl Receiver for PaymentReceiver {
req: ReceivePaymentRequest,
) -> Result<ReceivePaymentResponse, ReceivePaymentError> {
self.node_api.start().await?;
let lsp_info = get_lsp(self.persister.clone(), self.lsp.clone()).await?;
let node_state = self
.persister
.get_node_state()?
Expand All @@ -2405,6 +2385,12 @@ impl Receiver for PaymentReceiver {

// check if we need to open channel
let open_channel_needed = node_state.inbound_liquidity_msats < req.amount_msat;
let lsp_info = get_lsp(
self.persister.clone(),
self.lsp.clone(),
open_channel_needed,
)
.await?;
if open_channel_needed {
info!("We need to open a channel");

Expand Down Expand Up @@ -2485,7 +2471,7 @@ impl Receiver for PaymentReceiver {
) -> Result<String, ReceivePaymentError> {
let lsp_info = match lsp_info {
Some(lsp_info) => lsp_info,
None => get_lsp(self.persister.clone(), self.lsp.clone()).await?,
None => get_lsp(self.persister.clone(), self.lsp.clone(), true).await?,
};

match params {
Expand Down Expand Up @@ -2627,12 +2613,34 @@ impl PaymentReceiver {
async fn get_lsp(
persister: Arc<SqliteStorage>,
lsp_api: Arc<dyn LspAPI>,
skip_cache: bool,
) -> Result<LspInformation> {
let lsp_id = persister.get_lsp_id()?.ok_or(anyhow!("No LSP ID found"))?;

get_lsp_by_id(persister, lsp_api, lsp_id.as_str())
.await?
.ok_or_else(|| anyhow!("No LSP found for id {lsp_id}"))
let persisted_lsp_info =
persister.get_lsp_information()?.and_then(|lsp_info| {
match !skip_cache && lsp_info.opening_fee_params_list.is_valid() {
true => Some(lsp_info),
false => {
debug!("Ignoring LSP information: {:?} {:?}", skip_cache, lsp_info);
None
}
}
});
match persisted_lsp_info {
Some(lsp_info) => {
debug!("Using persisted LSP information");
Ok(lsp_info)
}
None => {
let lsp_id = persister.get_lsp_id()?.ok_or(anyhow!("No LSP ID found"))?;
match get_lsp_by_id(persister.clone(), lsp_api, lsp_id.as_str()).await? {
Some(lsp_info) => {
persister.set_lsp_information(&lsp_info)?;
Ok(lsp_info)
}
None => Err(anyhow!("No LSP found for id {lsp_id}")),
}
}
}
}

async fn get_lsp_by_id(
Expand Down
2 changes: 1 addition & 1 deletion tools/sdk-cli/src/command_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ pub(crate) async fn handle_command(
serde_json::to_string_pretty(&lsps).map_err(|e| e.into())
}
Commands::LspInfo {} => {
let lsp_info = sdk()?.lsp_info().await?;
let lsp_info = sdk()?.lsp_info(false).await?;
serde_json::to_string_pretty(&lsp_info).map_err(|e| e.into())
}
Commands::ConnectLSP { lsp_id } => {
Expand Down

0 comments on commit cbaebd2

Please sign in to comment.