Skip to content

Commit 8b9c6e0

Browse files
committed
LSPS2: Prune expired buy requests on disconnection
.. we clean up any pending buy requests that hit their `valid_until` time when the counterparty disconnects.
1 parent f003239 commit 8b9c6e0

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

lightning-liquidity/src/lsps2/service.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ use crate::lsps0::ser::{
1515
};
1616
use crate::lsps2::event::LSPS2ServiceEvent;
1717
use crate::lsps2::payment_queue::{InterceptedHTLC, PaymentQueue};
18-
use crate::lsps2::utils::{compute_opening_fee, is_valid_opening_fee_params};
18+
use crate::lsps2::utils::{
19+
compute_opening_fee, is_expired_opening_fee_params, is_valid_opening_fee_params,
20+
};
1921
use crate::message_queue::MessageQueue;
2022
use crate::prelude::{new_hash_map, HashMap, String, ToString, Vec};
2123
use crate::sync::{Arc, Mutex, RwLock};
@@ -477,8 +479,15 @@ impl PeerState {
477479
}
478480

479481
fn peer_disconnected(&mut self) {
480-
// Clean any pending `get_info` requests.
481-
self.pending_requests.retain(|_, entry| !matches!(entry, LSPS2Request::GetInfo(_)));
482+
self.pending_requests.retain(|_, entry| {
483+
match entry {
484+
LSPS2Request::GetInfo(_) => false,
485+
LSPS2Request::Buy(request) => {
486+
// Prune any expired buy requests.
487+
!is_expired_opening_fee_params(&request.opening_fee_params)
488+
},
489+
}
490+
});
482491
}
483492
}
484493

lightning-liquidity/src/lsps2/utils.rs

+24-16
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,9 @@ use std::time::{SystemTime, UNIX_EPOCH};
1414
pub fn is_valid_opening_fee_params(
1515
fee_params: &OpeningFeeParams, promise_secret: &[u8; 32],
1616
) -> bool {
17-
#[cfg(feature = "std")]
18-
{
19-
// TODO: We need to find a way to check expiry times in no-std builds.
20-
let seconds_since_epoch = SystemTime::now()
21-
.duration_since(UNIX_EPOCH)
22-
.expect("system clock to be ahead of the unix epoch")
23-
.as_secs();
24-
let valid_until_seconds_since_epoch = fee_params
25-
.valid_until
26-
.timestamp()
27-
.try_into()
28-
.expect("expiration to be ahead of unix epoch");
29-
if seconds_since_epoch > valid_until_seconds_since_epoch {
30-
return false;
31-
}
17+
if is_expired_opening_fee_params(fee_params) {
18+
return false;
3219
}
33-
3420
let mut hmac = HmacEngine::<Sha256>::new(promise_secret);
3521
hmac.input(&fee_params.min_fee_msat.to_be_bytes());
3622
hmac.input(&fee_params.proportional.to_be_bytes());
@@ -44,6 +30,28 @@ pub fn is_valid_opening_fee_params(
4430
promise == fee_params.promise
4531
}
4632

33+
/// Determines if the given parameters are expired, or still valid.
34+
pub fn is_expired_opening_fee_params(fee_params: &OpeningFeeParams) -> bool {
35+
#[cfg(feature = "std")]
36+
{
37+
let seconds_since_epoch = SystemTime::now()
38+
.duration_since(UNIX_EPOCH)
39+
.expect("system clock to be ahead of the unix epoch")
40+
.as_secs();
41+
let valid_until_seconds_since_epoch = fee_params
42+
.valid_until
43+
.timestamp()
44+
.try_into()
45+
.expect("expiration to be ahead of unix epoch");
46+
seconds_since_epoch > valid_until_seconds_since_epoch
47+
}
48+
#[cfg(not(feature = "std"))]
49+
{
50+
// TODO: We need to find a way to check expiry times in no-std builds.
51+
false
52+
}
53+
}
54+
4755
/// Computes the opening fee given a payment size and the fee parameters.
4856
///
4957
/// Returns [`Option::None`] when the computation overflows.

0 commit comments

Comments
 (0)