@@ -28,7 +28,7 @@ use core::ops::Deref;
2828use crate :: prelude:: { new_hash_map, HashMap , String } ;
2929
3030use super :: msgs:: { Lsps5AppName , Lsps5WebhookUrl } ;
31- use super :: service:: { DefaultTimeProvider , TimeProvider } ;
31+ use super :: service:: { from_rfc3339 , DefaultTimeProvider , TimeProvider } ;
3232use super :: url_utils:: Url ;
3333use core:: time:: Duration ;
3434use lightning:: sign:: EntropySource ;
@@ -88,14 +88,15 @@ impl PeerState {
8888 let now = time_provider. now ( ) ;
8989
9090 // Only run cleanup once per minute to avoid excessive processing
91- if now. abs_diff ( self . last_cleanup ) < Duration :: from_secs ( 60 ) {
91+ if now. checked_sub ( self . last_cleanup ) . unwrap ( ) < Duration :: from_secs ( 60 ) {
9292 return ;
9393 }
9494
9595 self . last_cleanup = now. clone ( ) ;
9696
9797 // Calculate the cutoff time for expired requests
98- let cutoff = now. abs_diff ( Duration :: from_secs ( max_age_secs. try_into ( ) . unwrap ( ) ) ) ;
98+ let cutoff =
99+ now. checked_sub ( Duration :: from_secs ( max_age_secs. try_into ( ) . unwrap ( ) ) ) . unwrap ( ) ;
99100
100101 // Remove expired set_webhook requests
101102 self . pending_set_webhook_requests . retain ( |_, ( _, _, timestamp) | * timestamp > cutoff) ;
@@ -517,14 +518,19 @@ where
517518 /// * On error: LightningError with error description
518519 pub fn verify_notification_signature (
519520 counterparty_node_id : PublicKey , timestamp : & str , signature : & str ,
520- notification : & WebhookNotification , time_provider : Arc < dyn TimeProvider > ,
521+ notification : & WebhookNotification , time_provider : & Arc < dyn TimeProvider > ,
521522 ) -> Result < bool , LightningError > {
522523 // Check timestamp format
523- match time_provider . from_rfc3339 ( timestamp) {
524+ match from_rfc3339 ( timestamp) {
524525 Ok ( timestamp_dt) => {
525526 // Check timestamp is within 10 minutes of current time
526527 let now = time_provider. now ( ) ;
527- let diff = now. abs_diff ( timestamp_dt) ;
528+ let diff;
529+ if now > timestamp_dt {
530+ diff = now. checked_sub ( timestamp_dt) . unwrap ( ) ;
531+ } else {
532+ diff = timestamp_dt. checked_sub ( now) . unwrap ( ) ;
533+ }
528534 if diff > Duration :: from_secs ( 600 ) {
529535 // 10 minutes
530536 return Err ( LightningError {
@@ -587,7 +593,7 @@ where
587593 /// * On error: LightningError with error description
588594 pub fn parse_webhook_notification (
589595 counterparty_node_id : PublicKey , timestamp : & str , signature : & str , notification_json : & str ,
590- time_provider : Arc < dyn TimeProvider > ,
596+ time_provider : & Arc < dyn TimeProvider > ,
591597 ) -> Result < WebhookNotification , LightningError > {
592598 // Parse the notification JSON
593599 let notification: WebhookNotification = match serde_json:: from_str ( notification_json) {
@@ -605,7 +611,7 @@ where
605611 timestamp,
606612 signature,
607613 & notification,
608- time_provider,
614+ & time_provider,
609615 ) {
610616 Ok ( _) => Ok ( notification) ,
611617 Err ( e) => Err ( e) ,
@@ -770,7 +776,7 @@ mod tests {
770776 fn test_cleanup_expired_responses ( ) {
771777 // use DefaultTimeProvider
772778 let ( client, _, _, _, _) = setup_test_client ( ) ;
773- let time_provider = client. time_provider ;
779+ let time_provider = & client. time_provider ;
774780 const OLD_APP_NAME : & str = "test-app-old" ;
775781 const NEW_APP_NAME : & str = "test-app-new" ;
776782 const WEBHOOK_URL : & str = "https://example.com/hook" ;
@@ -781,7 +787,7 @@ mod tests {
781787 let now = time_provider. now ( ) ;
782788 // Create a mock PeerState with a very old cleanup time
783789 let mut peer_state = PeerState :: new ( ) ;
784- peer_state. last_cleanup = now. abs_diff ( Duration :: from_secs ( 120 ) ) ;
790+ peer_state. last_cleanup = now. checked_sub ( Duration :: from_secs ( 120 ) ) . unwrap ( ) ;
785791
786792 // Add some test requests with different timestamps
787793 let old_request_id = LSPSRequestId ( "test:request:old" . to_string ( ) ) ;
@@ -793,14 +799,18 @@ mod tests {
793799 (
794800 lsps5_old_app_name,
795801 lsps5_webhook_url. clone ( ) ,
796- now. abs_diff ( Duration :: from_secs ( 7200 ) ) ,
802+ now. checked_sub ( Duration :: from_secs ( 7200 ) ) . unwrap ( ) ,
797803 ) , // 2 hours old
798804 ) ;
799805
800806 // Add a recent request (should be kept)
801807 peer_state. pending_set_webhook_requests . insert (
802808 new_request_id. clone ( ) ,
803- ( lsps5_new_app_name, lsps5_webhook_url, now. abs_diff ( Duration :: from_secs ( 600 ) ) ) , // 10 minutes old
809+ (
810+ lsps5_new_app_name,
811+ lsps5_webhook_url,
812+ now. checked_sub ( Duration :: from_secs ( 600 ) ) . unwrap ( ) ,
813+ ) , // 10 minutes old
804814 ) ;
805815
806816 // Run cleanup with 30 minutes (1800 seconds) max age
@@ -811,7 +821,7 @@ mod tests {
811821 assert ! ( peer_state. pending_set_webhook_requests. contains_key( & new_request_id) ) ;
812822
813823 // Verify last_cleanup was updated within the last 10 seconds
814- let cleanup_age = time_provider. clone ( ) . now ( ) . abs_diff ( peer_state. last_cleanup ) ;
824+ let cleanup_age = time_provider. now ( ) . checked_sub ( peer_state. last_cleanup ) . unwrap ( ) ;
815825 assert ! ( cleanup_age < Duration :: from_secs( 10 ) ) ;
816826 }
817827
0 commit comments