Skip to content

Commit 680f005

Browse files
fixup: clean up LSPS5 client time, URL and timestamp parsing
- Replace reqwest::Url with url::Url. - Use SystemTime instead of Instant for cleanup handling. - Remove unused update_config method. - Update timestamp parsing in verify_notification_signature to use LSPSDateTime::from_str.
1 parent 5ec2f16 commit 680f005

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

lightning-liquidity/src/lsps5/client.rs

+18-25
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//! Client implementation for LSPS5 webhook registration
1010
1111
use crate::events::EventQueue;
12-
use crate::lsps0::ser::{LSPSMessage, LSPSProtocolMessageHandler, LSPSRequestId};
12+
use crate::lsps0::ser::{LSPSDateTime, LSPSMessage, LSPSProtocolMessageHandler, LSPSRequestId};
1313
use crate::lsps5::event::LSPS5ClientEvent;
1414
use crate::lsps5::msgs::{
1515
LSPS5Message, LSPS5Request, LSPS5Response, ListWebhooksRequest, RemoveWebhookRequest, SetWebhookRequest, WebhookNotification,
@@ -23,19 +23,18 @@ use bitcoin::secp256k1::{PublicKey, Secp256k1};
2323
use lightning::ln::msgs::{ErrorAction, LightningError};
2424

2525
use core::ops::Deref;
26-
use std::time::{Duration, SystemTime};
27-
use reqwest::Url;
28-
use std::sync::Arc;
29-
30-
use std::collections::HashSet;
31-
use crate::sync::{Mutex, RwLock};
32-
use chrono::DateTime;
26+
use core::str::FromStr;
27+
#[cfg(feature = "std")]
28+
use std::time::{SystemTime, UNIX_EPOCH};
29+
use url::Url;
30+
use crate::sync::{Arc, Mutex, RwLock};
3331

3432
use crate::prelude::{new_hash_map, HashMap, String};
3533

3634
use super::{MAX_APP_NAME_LENGTH, MAX_WEBHOOK_URL_LENGTH};
3735
use lightning::sign::EntropySource;
3836
use lightning::util::logger::Level;
37+
use core::time::Duration;
3938

4039
/// Default maximum age in seconds for cached responses (1 hour)
4140
pub const DEFAULT_RESPONSE_MAX_AGE_SECS: u64 = 3600;
@@ -60,25 +59,25 @@ struct PeerState {
6059
pending_list_webhooks_requests: HashSet<LSPSRequestId>,
6160
pending_remove_webhook_requests: HashMap<LSPSRequestId, String>, // RequestId -> app_name
6261
// Last cleanup time for garbage collection
63-
last_cleanup: std::time::Instant,
62+
last_cleanup: SystemTime,
6463
}
6564

6665
impl PeerState {
6766
fn new() -> Self {
6867
Self {
6968
pending_set_webhook_requests: new_hash_map(),
70-
pending_list_webhooks_requests: HashSet::new(),
69+
pending_list_webhooks_requests: new_hash_set(),
7170
pending_remove_webhook_requests: new_hash_map(),
72-
last_cleanup: std::time::Instant::now(),
71+
last_cleanup: SystemTime::now(),
7372
}
7473
}
7574

7675
/// Clean up expired responses based on max_age
7776
fn cleanup_expired_responses(&mut self) {
78-
let now = std::time::Instant::now();
77+
let now = SystemTime::now();
7978

8079
// Only run cleanup once per minute to avoid excessive processing
81-
if now.duration_since(self.last_cleanup) < Duration::from_secs(60) {
80+
if now.duration_since(self.last_cleanup).unwrap() < Duration::from_secs(60) {
8281
return;
8382
}
8483

@@ -124,11 +123,6 @@ where
124123
}
125124
}
126125

127-
/// Update the client configuration
128-
pub fn update_config(&mut self, config: LSPS5ClientConfig) {
129-
self.config = config;
130-
}
131-
132126
/// Register a webhook with the LSP
133127
///
134128
/// Implements the `lsps5.set_webhook` method from BLIP-55.
@@ -463,11 +457,11 @@ where
463457
&self, counterparty_node_id: PublicKey, timestamp: &str, signature: &str, notification: &WebhookNotification,
464458
) -> Result<bool, LightningError> {
465459
// Check timestamp format
466-
match DateTime::parse_from_rfc3339(timestamp) {
460+
match LSPSDateTime::from_str(timestamp) {
467461
Ok(timestamp_dt) => {
468462
// Check timestamp is within 10 minutes of current time
469463
let now = SystemTime::now()
470-
.duration_since(std::time::UNIX_EPOCH)
464+
.duration_since(UNIX_EPOCH)
471465
.unwrap()
472466
.as_secs() as i64;
473467
let diff = (timestamp_dt.timestamp() - now).abs();
@@ -478,9 +472,9 @@ where
478472
});
479473
}
480474
},
481-
Err(e) => {
475+
Err(_e) => {
482476
return Err(LightningError {
483-
err: format!("Invalid timestamp format: {} - {}", timestamp, e),
477+
err: format!("Invalid timestamp format: {}", timestamp),
484478
action: ErrorAction::IgnoreAndLog(Level::Error)
485479
});
486480
}
@@ -651,7 +645,6 @@ mod tests {
651645
use super::*;
652646
use bitcoin::secp256k1::SecretKey;
653647
use crate::{lsps0::ser::LSPSRequestId, lsps5::msgs::SetWebhookResponse, tests::utils::TestEntropy};
654-
use std::time::Duration;
655648

656649
fn setup_test_client() -> (LSPS5ClientHandler<Arc<TestEntropy>>, Arc<MessageQueue>, Arc<EventQueue>, PublicKey, PublicKey) {
657650
let test_entropy_source =Arc::new(TestEntropy {});
@@ -768,7 +761,7 @@ mod tests {
768761
fn test_cleanup_expired_responses() {
769762
// Create a mock PeerState with a very old cleanup time
770763
let mut peer_state = PeerState::new();
771-
peer_state.last_cleanup = std::time::Instant::now() - Duration::from_secs(120); // 2 minutes old
764+
peer_state.last_cleanup = SystemTime::now() - Duration::from_secs(120); // 2 minutes old
772765

773766
// Add some pending requests
774767
let req_id = LSPSRequestId("test:request:id".to_string());
@@ -781,7 +774,7 @@ mod tests {
781774
peer_state.cleanup_expired_responses();
782775

783776
// Verify last_cleanup was updated
784-
assert!(peer_state.last_cleanup > std::time::Instant::now() - Duration::from_secs(10));
777+
assert!(peer_state.last_cleanup > SystemTime::now() - Duration::from_secs(10));
785778
}
786779

787780
#[test]

0 commit comments

Comments
 (0)