Skip to content

Commit

Permalink
LSPS2: Prune expired buy requests on disconnection
Browse files Browse the repository at this point in the history
.. we clean up any pending buy requests that hit their `valid_until`
time when the counterparty disconnects.
  • Loading branch information
tnull committed Dec 10, 2024
1 parent a349935 commit a302a74
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
15 changes: 12 additions & 3 deletions lightning-liquidity/src/lsps2/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ use crate::lsps0::ser::{
};
use crate::lsps2::event::LSPS2ServiceEvent;
use crate::lsps2::payment_queue::{InterceptedHTLC, PaymentQueue};
use crate::lsps2::utils::{compute_opening_fee, is_valid_opening_fee_params};
use crate::lsps2::utils::{
compute_opening_fee, is_expired_opening_fee_params, is_valid_opening_fee_params,
};
use crate::message_queue::MessageQueue;
use crate::prelude::{new_hash_map, HashMap, String, ToString, Vec};
use crate::sync::{Arc, Mutex, RwLock};
Expand Down Expand Up @@ -477,8 +479,15 @@ impl PeerState {
}

fn peer_disconnected(&mut self) {
// Clean any pending `get_info` requests.
self.pending_requests.retain(|_, entry| !matches!(entry, LSPS2Request::GetInfo(_)));
self.pending_requests.retain(|_, entry| {
match entry {
LSPS2Request::GetInfo(_) => false,
LSPS2Request::Buy(request) => {
// Prune any expired buy requests.
!is_expired_opening_fee_params(&request.opening_fee_params)
},
}
});
}
}

Expand Down
40 changes: 24 additions & 16 deletions lightning-liquidity/src/lsps2/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,9 @@ use std::time::{SystemTime, UNIX_EPOCH};
pub fn is_valid_opening_fee_params(
fee_params: &OpeningFeeParams, promise_secret: &[u8; 32],
) -> bool {
#[cfg(feature = "std")]
{
// TODO: We need to find a way to check expiry times in no-std builds.
let seconds_since_epoch = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("system clock to be ahead of the unix epoch")
.as_secs();
let valid_until_seconds_since_epoch = fee_params
.valid_until
.timestamp()
.try_into()
.expect("expiration to be ahead of unix epoch");
if seconds_since_epoch > valid_until_seconds_since_epoch {
return false;
}
if is_expired_opening_fee_params(fee_params) {
return false;
}

let mut hmac = HmacEngine::<Sha256>::new(promise_secret);
hmac.input(&fee_params.min_fee_msat.to_be_bytes());
hmac.input(&fee_params.proportional.to_be_bytes());
Expand All @@ -44,6 +30,28 @@ pub fn is_valid_opening_fee_params(
promise == fee_params.promise
}

/// Determines if the given parameters are expired, or still valid.
pub fn is_expired_opening_fee_params(fee_params: &OpeningFeeParams) -> bool {
#[cfg(feature = "std")]
{
let seconds_since_epoch = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("system clock to be ahead of the unix epoch")
.as_secs();
let valid_until_seconds_since_epoch = fee_params
.valid_until
.timestamp()
.try_into()
.expect("expiration to be ahead of unix epoch");
seconds_since_epoch > valid_until_seconds_since_epoch
}
#[cfg(not(feature = "std"))]
{
// TODO: We need to find a way to check expiry times in no-std builds.
false
}
}

/// Computes the opening fee given a payment size and the fee parameters.
///
/// Returns [`Option::None`] when the computation overflows.
Expand Down

0 comments on commit a302a74

Please sign in to comment.