From 42243e88eadc26d009e671220719d17fa2bd2021 Mon Sep 17 00:00:00 2001 From: Martin Saposnic Date: Mon, 4 Aug 2025 12:20:29 -0300 Subject: [PATCH 1/2] Change abs_diff to duration_since, so it returns a Duration --- lightning-liquidity/src/lsps0/ser.rs | 7 ++++--- lightning-liquidity/src/lsps5/service.rs | 10 ++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lightning-liquidity/src/lsps0/ser.rs b/lightning-liquidity/src/lsps0/ser.rs index aeb29422678..0c44b6f796c 100644 --- a/lightning-liquidity/src/lsps0/ser.rs +++ b/lightning-liquidity/src/lsps0/ser.rs @@ -240,9 +240,10 @@ impl LSPSDateTime { now_seconds_since_epoch > datetime_seconds_since_epoch } - /// Returns the time in seconds since the unix epoch. - pub fn abs_diff(&self, other: &Self) -> u64 { - self.0.timestamp().abs_diff(other.0.timestamp()) + /// Returns the absolute difference between two datetimes as a `Duration`. + pub fn duration_since(&self, other: &Self) -> Duration { + let diff_secs = self.0.timestamp().abs_diff(other.0.timestamp()); + Duration::from_secs(diff_secs) } /// Returns the time in seconds since the unix epoch. diff --git a/lightning-liquidity/src/lsps5/service.rs b/lightning-liquidity/src/lsps5/service.rs index 52d8310f971..4b2e59659b7 100644 --- a/lightning-liquidity/src/lsps5/service.rs +++ b/lightning-liquidity/src/lsps5/service.rs @@ -156,7 +156,7 @@ where let should_prune = { let last_pruning = self.last_pruning.lock().unwrap(); last_pruning.as_ref().map_or(true, |last_time| { - now.abs_diff(&last_time) > PRUNE_STALE_WEBHOOKS_INTERVAL_DAYS.as_secs() + now.duration_since(&last_time) > PRUNE_STALE_WEBHOOKS_INTERVAL_DAYS }) }; @@ -428,10 +428,8 @@ where webhook .last_notification_sent .get(¬ification.method) - .map(|last_sent| now.abs_diff(&last_sent)) - .map_or(false, |duration| { - duration < DEFAULT_NOTIFICATION_COOLDOWN_HOURS.as_secs() - }) + .map(|last_sent| now.duration_since(&last_sent)) + .map_or(false, |duration| duration < DEFAULT_NOTIFICATION_COOLDOWN_HOURS) }); if rate_limit_applies { @@ -505,7 +503,7 @@ where webhooks.retain(|client_id, client_webhooks| { if !self.client_has_open_channel(client_id) { client_webhooks.retain(|_, webhook| { - now.abs_diff(&webhook.last_used) < MIN_WEBHOOK_RETENTION_DAYS.as_secs() + now.duration_since(&webhook.last_used) < MIN_WEBHOOK_RETENTION_DAYS }); !client_webhooks.is_empty() } else { From 552d8e4e1d0d08899ac51f2099ed024a88dba12a Mon Sep 17 00:00:00 2001 From: Martin Saposnic Date: Mon, 4 Aug 2025 12:22:03 -0300 Subject: [PATCH 2/2] Make LSPSDateTime Copy rather than explicitely _clone_ing --- lightning-liquidity/src/lsps0/ser.rs | 2 +- lightning-liquidity/src/lsps1/service.rs | 2 +- lightning-liquidity/src/lsps2/msgs.rs | 4 ++-- lightning-liquidity/src/lsps5/service.rs | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lightning-liquidity/src/lsps0/ser.rs b/lightning-liquidity/src/lsps0/ser.rs index 0c44b6f796c..213e2760119 100644 --- a/lightning-liquidity/src/lsps0/ser.rs +++ b/lightning-liquidity/src/lsps0/ser.rs @@ -218,7 +218,7 @@ impl wire::Type for RawLSPSMessage { pub struct LSPSRequestId(pub String); /// An object representing datetimes as described in bLIP-50 / LSPS0. -#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)] +#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash, Deserialize, Serialize)] #[serde(transparent)] pub struct LSPSDateTime(pub chrono::DateTime); diff --git a/lightning-liquidity/src/lsps1/service.rs b/lightning-liquidity/src/lsps1/service.rs index 4dadf2e03dc..aa10e735565 100644 --- a/lightning-liquidity/src/lsps1/service.rs +++ b/lightning-liquidity/src/lsps1/service.rs @@ -266,7 +266,7 @@ where let order_id = self.generate_order_id(); let channel = OutboundCRChannel::new( params.order.clone(), - created_at.clone(), + created_at, order_id.clone(), payment.clone(), ); diff --git a/lightning-liquidity/src/lsps2/msgs.rs b/lightning-liquidity/src/lsps2/msgs.rs index 84875d4ab7c..8fb9536b6d4 100644 --- a/lightning-liquidity/src/lsps2/msgs.rs +++ b/lightning-liquidity/src/lsps2/msgs.rs @@ -72,7 +72,7 @@ impl LSPS2RawOpeningFeeParams { LSPS2OpeningFeeParams { min_fee_msat: self.min_fee_msat, proportional: self.proportional, - valid_until: self.valid_until.clone(), + valid_until: self.valid_until, min_lifetime: self.min_lifetime, max_client_to_self_delay: self.max_client_to_self_delay, min_payment_size_msat: self.min_payment_size_msat, @@ -235,7 +235,7 @@ mod tests { let raw = LSPS2RawOpeningFeeParams { min_fee_msat, proportional, - valid_until: valid_until.clone().into(), + valid_until: valid_until.into(), min_lifetime, max_client_to_self_delay, min_payment_size_msat, diff --git a/lightning-liquidity/src/lsps5/service.rs b/lightning-liquidity/src/lsps5/service.rs index 4b2e59659b7..e0fb3ab442a 100644 --- a/lightning-liquidity/src/lsps5/service.rs +++ b/lightning-liquidity/src/lsps5/service.rs @@ -185,7 +185,7 @@ where Entry::Occupied(mut entry) => { no_change = entry.get().url == params.webhook; let (last_used, last_notification_sent) = if no_change { - (entry.get().last_used.clone(), entry.get().last_notification_sent.clone()) + (entry.get().last_used, entry.get().last_notification_sent.clone()) } else { (now, new_hash_map()) }; @@ -438,8 +438,8 @@ where } for (app_name, webhook) in client_webhooks.iter_mut() { - webhook.last_notification_sent.insert(notification.method.clone(), now.clone()); - webhook.last_used = now.clone(); + webhook.last_notification_sent.insert(notification.method.clone(), now); + webhook.last_used = now; self.send_notification( client_id, app_name.clone(),