9
9
//! Client implementation for LSPS5 webhook registration
10
10
11
11
use crate :: events:: EventQueue ;
12
- use crate :: lsps0:: ser:: { LSPSMessage , LSPSProtocolMessageHandler , LSPSRequestId } ;
12
+ use crate :: lsps0:: ser:: { LSPSDateTime , LSPSMessage , LSPSProtocolMessageHandler , LSPSRequestId } ;
13
13
use crate :: lsps5:: event:: LSPS5ClientEvent ;
14
14
use crate :: lsps5:: msgs:: {
15
15
LSPS5Message , LSPS5Request , LSPS5Response , ListWebhooksRequest , RemoveWebhookRequest , SetWebhookRequest , WebhookNotification ,
@@ -23,19 +23,18 @@ use bitcoin::secp256k1::{PublicKey, Secp256k1};
23
23
use lightning:: ln:: msgs:: { ErrorAction , LightningError } ;
24
24
25
25
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 } ;
33
31
34
32
use crate :: prelude:: { new_hash_map, HashMap , String } ;
35
33
36
34
use super :: { MAX_APP_NAME_LENGTH , MAX_WEBHOOK_URL_LENGTH } ;
37
35
use lightning:: sign:: EntropySource ;
38
36
use lightning:: util:: logger:: Level ;
37
+ use core:: time:: Duration ;
39
38
40
39
/// Default maximum age in seconds for cached responses (1 hour)
41
40
pub const DEFAULT_RESPONSE_MAX_AGE_SECS : u64 = 3600 ;
@@ -60,25 +59,25 @@ struct PeerState {
60
59
pending_list_webhooks_requests : HashSet < LSPSRequestId > ,
61
60
pending_remove_webhook_requests : HashMap < LSPSRequestId , String > , // RequestId -> app_name
62
61
// Last cleanup time for garbage collection
63
- last_cleanup : std :: time :: Instant ,
62
+ last_cleanup : SystemTime ,
64
63
}
65
64
66
65
impl PeerState {
67
66
fn new ( ) -> Self {
68
67
Self {
69
68
pending_set_webhook_requests : new_hash_map ( ) ,
70
- pending_list_webhooks_requests : HashSet :: new ( ) ,
69
+ pending_list_webhooks_requests : new_hash_set ( ) ,
71
70
pending_remove_webhook_requests : new_hash_map ( ) ,
72
- last_cleanup : std :: time :: Instant :: now ( ) ,
71
+ last_cleanup : SystemTime :: now ( ) ,
73
72
}
74
73
}
75
74
76
75
/// Clean up expired responses based on max_age
77
76
fn cleanup_expired_responses ( & mut self ) {
78
- let now = std :: time :: Instant :: now ( ) ;
77
+ let now = SystemTime :: now ( ) ;
79
78
80
79
// 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 ) {
82
81
return ;
83
82
}
84
83
@@ -124,11 +123,6 @@ where
124
123
}
125
124
}
126
125
127
- /// Update the client configuration
128
- pub fn update_config ( & mut self , config : LSPS5ClientConfig ) {
129
- self . config = config;
130
- }
131
-
132
126
/// Register a webhook with the LSP
133
127
///
134
128
/// Implements the `lsps5.set_webhook` method from BLIP-55.
@@ -463,11 +457,11 @@ where
463
457
& self , counterparty_node_id : PublicKey , timestamp : & str , signature : & str , notification : & WebhookNotification ,
464
458
) -> Result < bool , LightningError > {
465
459
// Check timestamp format
466
- match DateTime :: parse_from_rfc3339 ( timestamp) {
460
+ match LSPSDateTime :: from_str ( timestamp) {
467
461
Ok ( timestamp_dt) => {
468
462
// Check timestamp is within 10 minutes of current time
469
463
let now = SystemTime :: now ( )
470
- . duration_since ( std :: time :: UNIX_EPOCH )
464
+ . duration_since ( UNIX_EPOCH )
471
465
. unwrap ( )
472
466
. as_secs ( ) as i64 ;
473
467
let diff = ( timestamp_dt. timestamp ( ) - now) . abs ( ) ;
@@ -478,9 +472,9 @@ where
478
472
} ) ;
479
473
}
480
474
} ,
481
- Err ( e ) => {
475
+ Err ( _e ) => {
482
476
return Err ( LightningError {
483
- err : format ! ( "Invalid timestamp format: {} - {} " , timestamp, e ) ,
477
+ err : format ! ( "Invalid timestamp format: {}" , timestamp) ,
484
478
action : ErrorAction :: IgnoreAndLog ( Level :: Error )
485
479
} ) ;
486
480
}
@@ -651,7 +645,6 @@ mod tests {
651
645
use super :: * ;
652
646
use bitcoin:: secp256k1:: SecretKey ;
653
647
use crate :: { lsps0:: ser:: LSPSRequestId , lsps5:: msgs:: SetWebhookResponse , tests:: utils:: TestEntropy } ;
654
- use std:: time:: Duration ;
655
648
656
649
fn setup_test_client ( ) -> ( LSPS5ClientHandler < Arc < TestEntropy > > , Arc < MessageQueue > , Arc < EventQueue > , PublicKey , PublicKey ) {
657
650
let test_entropy_source =Arc :: new ( TestEntropy { } ) ;
@@ -768,7 +761,7 @@ mod tests {
768
761
fn test_cleanup_expired_responses ( ) {
769
762
// Create a mock PeerState with a very old cleanup time
770
763
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
772
765
773
766
// Add some pending requests
774
767
let req_id = LSPSRequestId ( "test:request:id" . to_string ( ) ) ;
@@ -781,7 +774,7 @@ mod tests {
781
774
peer_state. cleanup_expired_responses ( ) ;
782
775
783
776
// 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 ) ) ;
785
778
}
786
779
787
780
#[ test]
0 commit comments