diff --git a/lightning-liquidity/src/lsps2/service.rs b/lightning-liquidity/src/lsps2/service.rs index e36c6b65f0b..052ccce776e 100644 --- a/lightning-liquidity/src/lsps2/service.rs +++ b/lightning-liquidity/src/lsps2/service.rs @@ -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}; @@ -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) + }, + } + }); } } diff --git a/lightning-liquidity/src/lsps2/utils.rs b/lightning-liquidity/src/lsps2/utils.rs index a48456ae769..3de2d2eb568 100644 --- a/lightning-liquidity/src/lsps2/utils.rs +++ b/lightning-liquidity/src/lsps2/utils.rs @@ -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::::new(promise_secret); hmac.input(&fee_params.min_fee_msat.to_be_bytes()); hmac.input(&fee_params.proportional.to_be_bytes()); @@ -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.