Skip to content

Commit

Permalink
Merge pull request #1295 from getlipa/feature/cache-both-lsp-fee-params
Browse files Browse the repository at this point in the history
Cache both LSP fee params
  • Loading branch information
gcomte authored Dec 4, 2024
2 parents 8f82085 + 6e0b83e commit 2540872
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ impl LightningNode {
/// Get information about the fee charged by the LSP for opening new channels
///
/// Requires network: **no**
#[deprecated = "lightning().get_lsp_fee() should be used instead"]
#[deprecated = "lightning().get_lsp_fee() or swap().get_lsp_fee() should be used instead"]
pub fn query_lsp_fee(&self) -> Result<LspFee> {
self.lightning.get_lsp_fee()
}
Expand Down
6 changes: 5 additions & 1 deletion src/lightning/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ impl Lightning {
/// Requires network: **no**
pub fn get_lsp_fee(&self) -> Result<LspFee> {
let exchange_rate = self.support.get_exchange_rate();
let lsp_fee = self.support.task_manager.lock_unwrap().get_lsp_fee()?;
let lsp_fee = self
.support
.task_manager
.lock_unwrap()
.get_cheaper_lsp_fee()?;
Ok(LspFee {
channel_minimum_fee: lsp_fee.min_msat.as_msats().to_amount_up(&exchange_rate),
channel_fee_permyriad: lsp_fee.proportional as u64 / 100,
Expand Down
3 changes: 3 additions & 0 deletions src/lipalightninglib.udl
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ interface Swap {

[Throws=LnError]
string swap(FailedSwapInfo failed_swap_info, u32 sats_per_vbyte, OpeningFeeParams? lsp_fee_param);

[Throws=LnError]
LspFee get_lsp_fee();
};

interface ReverseSwap {
Expand Down
22 changes: 20 additions & 2 deletions src/onchain/swap.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::amount::{AsSats, Sats, ToAmount};
use crate::errors::Result;
use crate::locker::Locker;
use crate::onchain::{get_onchain_resolving_fees, query_onchain_fee_rate};
use crate::support::Support;
use crate::util::unix_timestamp_to_system_time;
use crate::{
Amount, CalculateLspFeeResponseV2, FailedSwapInfo, OnchainResolvingFees, ResolveFailedSwapInfo,
RuntimeErrorCode, SwapAddressInfo,
Amount, CalculateLspFeeResponseV2, FailedSwapInfo, LspFee, OnchainResolvingFees,
ResolveFailedSwapInfo, RuntimeErrorCode, SwapAddressInfo,
};
use breez_sdk_core::error::ReceiveOnchainError;
use breez_sdk_core::{
Expand Down Expand Up @@ -320,6 +321,23 @@ impl Swap {
self.support
.calculate_lsp_fee_for_amount(amount_sat, Some(TWO_WEEKS))
}

/// When receiving swaps, a new channel MAY be required. A fee will be charged to the user.
/// Get information about the fee charged by the LSP for opening new channels
///
/// Requires network: **no**
pub fn get_lsp_fee(&self) -> Result<LspFee> {
let exchange_rate = self.support.get_exchange_rate();
let lsp_fee = self
.support
.task_manager
.lock_unwrap()
.get_longer_valid_lsp_fee()?;
Ok(LspFee {
channel_minimum_fee: lsp_fee.min_msat.as_msats().to_amount_up(&exchange_rate),
channel_fee_permyriad: lsp_fee.proportional as u64 / 100,
})
}
}

/// Information the resolution of a failed swap.
Expand Down
47 changes: 37 additions & 10 deletions src/task_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ pub(crate) struct TaskManager {
exchange_rates: Arc<Mutex<Vec<ExchangeRate>>>,
data_store: Arc<Mutex<DataStore>>,
sdk: Arc<BreezServices>,
lsp_fee: Arc<Mutex<Option<OpeningFeeParams>>>,
lsp_fee_cheaper: Arc<Mutex<Option<OpeningFeeParams>>>,
lsp_fee_longer_valid: Arc<Mutex<Option<OpeningFeeParams>>>,
backup_manager: Arc<BackupManager>,
events_callback: Arc<Box<dyn EventsCallback>>,
breez_health_status: Arc<Mutex<Option<BreezHealthCheckStatus>>>,
Expand Down Expand Up @@ -69,7 +70,8 @@ impl TaskManager {
exchange_rates: Arc::new(Mutex::new(exchange_rates)),
data_store,
sdk,
lsp_fee: Arc::new(Mutex::new(None)),
lsp_fee_cheaper: Arc::new(Mutex::new(None)),
lsp_fee_longer_valid: Arc::new(Mutex::new(None)),
backup_manager: Arc::new(backup_manager),
events_callback,
breez_health_status: Arc::new(Mutex::new(None)),
Expand All @@ -82,11 +84,23 @@ impl TaskManager {
self.exchange_rates.lock_unwrap().clone()
}

pub fn get_lsp_fee(&self) -> Result<OpeningFeeParams> {
self.lsp_fee.lock_unwrap().clone().ok_or_runtime_error(
RuntimeErrorCode::LspServiceUnavailable,
"Cached LSP fee isn't available",
)
pub fn get_cheaper_lsp_fee(&self) -> Result<OpeningFeeParams> {
self.lsp_fee_cheaper
.lock_unwrap()
.clone()
.ok_or_runtime_error(
RuntimeErrorCode::LspServiceUnavailable,
"Cached cheaper LSP fee isn't available",
)
}
pub fn get_longer_valid_lsp_fee(&self) -> Result<OpeningFeeParams> {
self.lsp_fee_longer_valid
.lock_unwrap()
.clone()
.ok_or_runtime_error(
RuntimeErrorCode::LspServiceUnavailable,
"Cached longer valid LSP fee isn't available",
)
}

pub fn foreground(&mut self) {
Expand Down Expand Up @@ -180,10 +194,12 @@ impl TaskManager {

fn start_lsp_fee_update(&self, period: Duration) -> RepeatingTaskHandle {
let sdk = Arc::clone(&self.sdk);
let lsp_fee = Arc::clone(&self.lsp_fee);
let lsp_fee_longer_valid = Arc::clone(&self.lsp_fee_longer_valid);
let lsp_fee_cheaper = Arc::clone(&self.lsp_fee_cheaper);
self.runtime_handle.spawn_repeating_task(period, move || {
let sdk = Arc::clone(&sdk);
let lsp_fee = Arc::clone(&lsp_fee);
let lsp_fee_longer_valid = Arc::clone(&lsp_fee_longer_valid);
let lsp_fee_cheaper = Arc::clone(&lsp_fee_cheaper);

async move {
debug!("Starting lsp fee update task");
Expand All @@ -194,12 +210,23 @@ impl TaskManager {
.get_cheapest_opening_fee_params()
{
Ok(opening_fee_params) => {
*lsp_fee.lock_unwrap() = Some(opening_fee_params);
*lsp_fee_cheaper.lock_unwrap() = Some(opening_fee_params);
}
Err(e) => {
error!("Failed to retrieve cheapest opening fee params: {e}");
}
};
match lsp_information
.opening_fee_params_list
.get_48h_opening_fee_params()
{
Ok(opening_fee_params) => {
*lsp_fee_longer_valid.lock_unwrap() = Some(opening_fee_params);
}
Err(e) => {
error!("Failed to retrieve 48 hours opening fee params: {e}");
}
};
}
Err(e) => {
error!("Failed to update lsp fee: {e}");
Expand Down
1 change: 1 addition & 0 deletions tests/monitoring_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ fn node_can_start() {
fn lsp_fee_can_be_fetched() {
let sender = TransactingNode::new(NodeType::Sender).unwrap();
wait_for!(sender.node.lightning().get_lsp_fee().is_ok());
wait_for!(sender.node.onchain().swap().get_lsp_fee().is_ok());
}

#[test]
Expand Down

0 comments on commit 2540872

Please sign in to comment.