Skip to content

Commit

Permalink
Merge pull request #687 from andrei-21/feature/goodies
Browse files Browse the repository at this point in the history
Make code more idiomatic and simple
  • Loading branch information
ok300 authored Dec 19, 2023
2 parents 9f35333 + 48fbf0e commit 946e4e1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 57 deletions.
100 changes: 46 additions & 54 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::cmp::max;
use std::fs::OpenOptions;
use std::io::Write;
use std::str::FromStr;
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use anyhow::{anyhow, Result};
use anyhow::{anyhow, ensure, Result};
use bip39::*;
use bitcoin::hashes::hex::ToHex;
use bitcoin::hashes::{sha256, Hash};
Expand Down Expand Up @@ -198,7 +197,7 @@ impl BreezServices {
.await?;
services.start().await?;
let connect_duration = start.elapsed();
info!("SDK connected in: {:?}", connect_duration);
info!("SDK connected in: {connect_duration:?}");
Ok(services)
}

Expand All @@ -211,25 +210,25 @@ impl BreezServices {
/// background and back.
async fn start(self: &Arc<BreezServices>) -> Result<()> {
let mut started = self.started.lock().await;
if *started {
return Err(anyhow!("BreezServices already started"));
}
ensure!(!*started, "BreezServices already started");

let start = Instant::now();
self.start_background_tasks().await?;
let start_duration = start.elapsed();
info!("SDK initialized in: {:?}", start_duration);
info!("SDK initialized in: {start_duration:?}");
*started = true;
Ok(())
}

/// Trigger the stopping of BreezServices background threads for this instance.
pub async fn disconnect(&self) -> SdkResult<()> {
let mut started = self.started.lock().await;
if !*started {
return Err(SdkError::Generic {
ensure_sdk!(
*started,
SdkError::Generic {
err: "BreezServices is not running".into(),
});
}
}
);
self.shutdown_sender
.send(())
.map_err(|e| SdkError::Generic {
Expand All @@ -255,21 +254,20 @@ impl BreezServices {
// Valid the invoice network against the config network
validate_network(parsed_invoice.clone(), self.config.network)?;

// Ensure amount is provided for zero invoice
if provided_amount_msat == 0 && invoice_amount_msat == 0 {
return Err(SendPaymentError::InvalidAmount {
err: "Amount must be provided when paying a zero invoice".into(),
});
}

// Ensure amount is not provided for invoice that contains amount
if provided_amount_msat > 0 && invoice_amount_msat > 0 {
return Err(SendPaymentError::InvalidAmount {
err: "Amount should not be provided when paying a non zero invoice".into(),
});
}

let amount_msat = max(provided_amount_msat, invoice_amount_msat);
let amount_msat = match (provided_amount_msat, invoice_amount_msat) {
(0, 0) => {
return Err(SendPaymentError::InvalidAmount {
err: "Amount must be provided when paying a zero invoice".into(),
})
}
(0, amount_msat) => amount_msat,
(amount_msat, 0) => amount_msat,
(_amount_1, _amount_2) => {
return Err(SendPaymentError::InvalidAmount {
err: "Amount should not be provided when paying a non zero invoice".into(),
})
}
};

match self
.persister
Expand Down Expand Up @@ -341,18 +339,18 @@ impl BreezServices {

let payment = match self.send_payment(pay_req).await {
Ok(p) => Ok(p),
Err(e) => match e {
SendPaymentError::InvalidInvoice { .. } => Err(e),
SendPaymentError::ServiceConnectivity { .. } => Err(e),
_ => {
return Ok(LnUrlPayResult::PayError {
data: LnUrlPayErrorData {
payment_hash: invoice.payment_hash,
reason: e.to_string(),
},
});
}
},
e @ Err(
SendPaymentError::InvalidInvoice { .. }
| SendPaymentError::ServiceConnectivity { .. },
) => e,
Err(e) => {
return Ok(LnUrlPayResult::PayError {
data: LnUrlPayErrorData {
payment_hash: invoice.payment_hash,
reason: e.to_string(),
},
})
}
}?
.payment;
let details = match &payment.details {
Expand Down Expand Up @@ -957,7 +955,7 @@ impl BreezServices {
err: format!("(LSP: {node_id}) Failed to connect: {e}"),
})?;
}
debug!("connected to lsp {}@{}", node_id.clone(), address.clone());
debug!("connected to lsp {node_id}@{address}");
}
}
Ok(())
Expand Down Expand Up @@ -1235,7 +1233,7 @@ impl BreezServices {
tokio::spawn(async move {
let mut shutdown_receiver = cloned.shutdown_receiver.clone();
loop {
if shutdown_receiver.has_changed().map_or(true, |c| c) {
if shutdown_receiver.has_changed().unwrap_or(true) {
return;
}
let invoice_stream_res = cloned.node_api.stream_incoming_payments().await;
Expand Down Expand Up @@ -1294,7 +1292,7 @@ impl BreezServices {
tokio::spawn(async move {
let mut shutdown_receiver = cloned.shutdown_receiver.clone();
loop {
if shutdown_receiver.has_changed().map_or(true, |c| c) {
if shutdown_receiver.has_changed().unwrap_or(true) {
return;
}
let log_stream_res = cloned.node_api.stream_log_messages().await;
Expand Down Expand Up @@ -1850,21 +1848,15 @@ impl BreezServer {
.connect()
.await
.map_err(|e| SdkError::ServiceConnectivity {
err: format!(
"(Breez: {}) Failed to connect: {e}",
self.server_url.clone()
),
err: format!("(Breez: {}) Failed to connect: {e}", self.server_url),
})
}

fn api_key_metadata(&self) -> SdkResult<Option<MetadataValue<Ascii>>> {
match &self.api_key {
Some(key) => Ok(Some(format!("Bearer {key}").parse().map_err(
|e: InvalidMetadataValue| SdkError::ServiceConnectivity {
err: format!(
"(Breez: {}) Failed parse API key: {e}",
self.server_url.clone()
),
err: format!("(Breez: {}) Failed parse API key: {e}", self.server_url),
},
)?)),
_ => Ok(None),
Expand All @@ -1886,28 +1878,28 @@ impl BreezServer {
&self,
) -> SdkResult<PaymentNotifierClient<Channel>> {
let url = Uri::from_str(&self.server_url).map_err(|e| SdkError::ServiceConnectivity {
err: format!("(Breez: {}) {e}", self.server_url.clone()),
err: format!("(Breez: {}) {e}", self.server_url),
})?;
Ok(PaymentNotifierClient::connect(url).await?)
}

pub(crate) async fn get_information_client(&self) -> SdkResult<InformationClient<Channel>> {
let url = Uri::from_str(&self.server_url).map_err(|e| SdkError::ServiceConnectivity {
err: format!("(Breez: {}) {e}", self.server_url.clone()),
err: format!("(Breez: {}) {e}", self.server_url),
})?;
Ok(InformationClient::connect(url).await?)
}

pub(crate) async fn get_fund_manager_client(&self) -> SdkResult<FundManagerClient<Channel>> {
let url = Uri::from_str(&self.server_url).map_err(|e| SdkError::ServiceConnectivity {
err: format!("(Breez: {}) {e}", self.server_url.clone()),
err: format!("(Breez: {}) {e}", self.server_url),
})?;
Ok(FundManagerClient::connect(url).await?)
}

pub(crate) async fn get_signer_client(&self) -> SdkResult<SignerClient<Channel>> {
let url = Uri::from_str(&self.server_url).map_err(|e| SdkError::ServiceConnectivity {
err: format!("(Breez: {}) {e}", self.server_url.clone()),
err: format!("(Breez: {}) {e}", self.server_url),
})?;
Ok(SignerClient::new(Endpoint::new(url)?.connect().await?))
}
Expand All @@ -1925,7 +1917,7 @@ impl BreezServer {

pub(crate) async fn get_swapper_client(&self) -> SdkResult<SwapperClient<Channel>> {
let url = Uri::from_str(&self.server_url).map_err(|e| SdkError::ServiceConnectivity {
err: format!("(Breez: {}) {e}", self.server_url.clone()),
err: format!("(Breez: {}) {e}", self.server_url),
})?;
Ok(SwapperClient::new(Endpoint::new(url)?.connect().await?))
}
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk-core/src/lsps0/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Transport {
tokio::spawn(async move {
loop {
let mut cancel = cancel.clone();
if cancel.has_changed().map_or(true, |c| c) {
if cancel.has_changed().unwrap_or(true) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion libs/sdk-core/src/persist/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ impl SqliteStorage {
let status: i32 = row
.get::<&str, Option<i32>>("status")?
.unwrap_or(SwapStatus::Initial as i32);
let status: SwapStatus = status.try_into().map_or(SwapStatus::Initial, |v| v);
let status: SwapStatus = status.try_into().unwrap_or(SwapStatus::Initial);
let refund_txs_raw: String = row
.get::<&str, Option<String>>("refund_tx_ids")?
.unwrap_or("[]".to_string());
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk-core/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ pub fn create_invoice(
hints: Vec<RouteHint>,
invoice_preimage: Option<Vec<u8>>,
) -> LNInvoice {
let preimage = invoice_preimage.map_or(rand::thread_rng().gen::<[u8; 32]>().to_vec(), |p| p);
let preimage = invoice_preimage.unwrap_or(rand::thread_rng().gen::<[u8; 32]>().to_vec());
let hashed = Message::from_hashed_data::<sha256::Hash>(&preimage[..]);
let hash = hashed.as_ref();

Expand Down

0 comments on commit 946e4e1

Please sign in to comment.