@@ -92,8 +92,6 @@ impl Default for LSPS5ServiceConfig {
92
92
/// - `lsps5.remove_webhook` -> delete a named webhook or return [`app_name_not_found`] error.
93
93
/// - Prune stale webhooks after a client has no open channels and no activity for at least
94
94
/// [`MIN_WEBHOOK_RETENTION_DAYS`].
95
- /// - Rate-limit repeat notifications of the same method to a client by
96
- /// [`notification_cooldown_hours`].
97
95
/// - Sign and enqueue outgoing webhook notifications:
98
96
/// - Construct JSON-RPC 2.0 Notification objects [`WebhookNotification`],
99
97
/// - Timestamp and LN-style zbase32-sign each payload,
@@ -108,7 +106,6 @@ impl Default for LSPS5ServiceConfig {
108
106
/// [`bLIP-55 / LSPS5`]: https://github.com/lightning/blips/pull/55/files
109
107
/// [`max_webhooks_per_client`]: super::service::LSPS5ServiceConfig::max_webhooks_per_client
110
108
/// [`app_name_not_found`]: super::msgs::LSPS5ProtocolError::AppNameNotFound
111
- /// [`notification_cooldown_hours`]: super::service::LSPS5ServiceConfig::notification_cooldown_hours
112
109
/// [`WebhookNotification`]: super::msgs::WebhookNotification
113
110
/// [`LSPS5ServiceEvent::SendWebhookNotification`]: super::event::LSPS5ServiceEvent::SendWebhookNotification
114
111
/// [`app_name`]: super::msgs::LSPS5AppName
@@ -224,23 +221,27 @@ where
224
221
}
225
222
226
223
if !no_change {
227
- self . send_webhook_registered_notification (
224
+ let result = self . send_webhook_registered_notification (
228
225
counterparty_node_id,
229
226
params. app_name ,
230
227
params. webhook ,
231
- )
232
- . map_err ( |e| {
228
+ ) ;
229
+
230
+ // If the send_notification failed because of a SLOW_DOWN_ERROR, it means we sent this
231
+ // notification recently, and the user has not seen it yet. It's safe to continue, but we still need to handle other error types.
232
+ if result. is_err ( ) && !matches ! ( result, Err ( LSPS5ProtocolError :: SlowDownError ) ) {
233
+ let e = result. unwrap_err ( ) ;
233
234
let msg = LSPS5Message :: Response (
234
235
request_id. clone ( ) ,
235
236
LSPS5Response :: SetWebhookError ( e. clone ( ) . into ( ) ) ,
236
237
)
237
238
. into ( ) ;
238
239
self . pending_messages . enqueue ( & counterparty_node_id, msg) ;
239
- LightningError {
240
+ return Err ( LightningError {
240
241
err : e. message ( ) . into ( ) ,
241
242
action : ErrorAction :: IgnoreAndLog ( Level :: Info ) ,
242
- }
243
- } ) ? ;
243
+ } ) ;
244
+ }
244
245
}
245
246
246
247
let msg = LSPS5Message :: Response (
@@ -326,10 +327,14 @@ where
326
327
/// This builds a [`WebhookNotificationMethod::LSPS5PaymentIncoming`] webhook notification, signs it with your
327
328
/// node key, and enqueues HTTP POSTs to all registered webhook URLs for that client.
328
329
///
330
+ /// This may fail if a similar notification was sent too recently,
331
+ /// violating the notification cooldown period defined in [`DEFAULT_NOTIFICATION_COOLDOWN_HOURS`].
332
+ ///
329
333
/// # Parameters
330
334
/// - `client_id`: the client's node-ID whose webhooks should be invoked.
331
335
///
332
336
/// [`WebhookNotificationMethod::LSPS5PaymentIncoming`]: super::msgs::WebhookNotificationMethod::LSPS5PaymentIncoming
337
+ /// [`DEFAULT_NOTIFICATION_COOLDOWN_HOURS`]: super::service::LSPS5ServiceConfig::DEFAULT_NOTIFICATION_COOLDOWN_HOURS
333
338
pub fn notify_payment_incoming ( & self , client_id : PublicKey ) -> Result < ( ) , LSPS5ProtocolError > {
334
339
let notification = WebhookNotification :: payment_incoming ( ) ;
335
340
self . send_notifications_to_client_webhooks ( client_id, notification)
@@ -343,11 +348,15 @@ where
343
348
/// the `timeout` block height, signs it, and enqueues HTTP POSTs to the client's
344
349
/// registered webhooks.
345
350
///
351
+ /// This may fail if a similar notification was sent too recently,
352
+ /// violating the notification cooldown period defined in [`DEFAULT_NOTIFICATION_COOLDOWN_HOURS`].
353
+ ///
346
354
/// # Parameters
347
355
/// - `client_id`: the client's node-ID whose webhooks should be invoked.
348
356
/// - `timeout`: the block height at which the channel contract will expire.
349
357
///
350
358
/// [`WebhookNotificationMethod::LSPS5ExpirySoon`]: super::msgs::WebhookNotificationMethod::LSPS5ExpirySoon
359
+ /// [`DEFAULT_NOTIFICATION_COOLDOWN_HOURS`]: super::service::LSPS5ServiceConfig::DEFAULT_NOTIFICATION_COOLDOWN_HOURS
351
360
pub fn notify_expiry_soon (
352
361
& self , client_id : PublicKey , timeout : u32 ,
353
362
) -> Result < ( ) , LSPS5ProtocolError > {
@@ -361,10 +370,14 @@ where
361
370
/// liquidity for `client_id`. Builds a [`WebhookNotificationMethod::LSPS5LiquidityManagementRequest`] notification,
362
371
/// signs it, and sends it to all of the client's registered webhook URLs.
363
372
///
373
+ /// This may fail if a similar notification was sent too recently,
374
+ /// violating the notification cooldown period defined in [`DEFAULT_NOTIFICATION_COOLDOWN_HOURS`].
375
+ ///
364
376
/// # Parameters
365
377
/// - `client_id`: the client's node-ID whose webhooks should be invoked.
366
378
///
367
379
/// [`WebhookNotificationMethod::LSPS5LiquidityManagementRequest`]: super::msgs::WebhookNotificationMethod::LSPS5LiquidityManagementRequest
380
+ /// [`DEFAULT_NOTIFICATION_COOLDOWN_HOURS`]: super::service::LSPS5ServiceConfig::DEFAULT_NOTIFICATION_COOLDOWN_HOURS
368
381
pub fn notify_liquidity_management_request (
369
382
& self , client_id : PublicKey ,
370
383
) -> Result < ( ) , LSPS5ProtocolError > {
@@ -378,10 +391,14 @@ where
378
391
/// for `client_id` while the client is offline. Builds a [`WebhookNotificationMethod::LSPS5OnionMessageIncoming`]
379
392
/// notification, signs it, and enqueues HTTP POSTs to each registered webhook.
380
393
///
394
+ /// This may fail if a similar notification was sent too recently,
395
+ /// violating the notification cooldown period defined in [`DEFAULT_NOTIFICATION_COOLDOWN_HOURS`].
396
+ ///
381
397
/// # Parameters
382
398
/// - `client_id`: the client's node-ID whose webhooks should be invoked.
383
399
///
384
400
/// [`WebhookNotificationMethod::LSPS5OnionMessageIncoming`]: super::msgs::WebhookNotificationMethod::LSPS5OnionMessageIncoming
401
+ /// [`DEFAULT_NOTIFICATION_COOLDOWN_HOURS`]: super::service::LSPS5ServiceConfig::DEFAULT_NOTIFICATION_COOLDOWN_HOURS
385
402
pub fn notify_onion_message_incoming (
386
403
& self , client_id : PublicKey ,
387
404
) -> Result < ( ) , LSPS5ProtocolError > {
@@ -402,22 +419,27 @@ where
402
419
let now =
403
420
LSPSDateTime :: new_from_duration_since_epoch ( self . time_provider . duration_since_epoch ( ) ) ;
404
421
405
- for ( app_name , webhook ) in client_webhooks. iter_mut ( ) {
406
- if webhook
422
+ let rate_limit_applies = client_webhooks. iter ( ) . any ( | ( _ , webhook ) | {
423
+ webhook
407
424
. last_notification_sent
408
425
. get ( & notification. method )
409
- . map ( |last_sent| now. clone ( ) . abs_diff ( & last_sent) )
410
- . map_or ( true , |duration| duration >= DEFAULT_NOTIFICATION_COOLDOWN_HOURS . as_secs ( ) )
411
- {
412
- webhook. last_notification_sent . insert ( notification. method . clone ( ) , now. clone ( ) ) ;
413
- webhook. last_used = now. clone ( ) ;
414
- self . send_notification (
415
- client_id,
416
- app_name. clone ( ) ,
417
- webhook. url . clone ( ) ,
418
- notification. clone ( ) ,
419
- ) ?;
420
- }
426
+ . map ( |last_sent| now. abs_diff ( & last_sent) )
427
+ . map_or ( false , |duration| duration < DEFAULT_NOTIFICATION_COOLDOWN_HOURS . as_secs ( ) )
428
+ } ) ;
429
+
430
+ if rate_limit_applies {
431
+ return Err ( LSPS5ProtocolError :: SlowDownError ) ;
432
+ }
433
+
434
+ for ( app_name, webhook) in client_webhooks. iter_mut ( ) {
435
+ webhook. last_notification_sent . insert ( notification. method . clone ( ) , now. clone ( ) ) ;
436
+ webhook. last_used = now. clone ( ) ;
437
+ self . send_notification (
438
+ client_id,
439
+ app_name. clone ( ) ,
440
+ webhook. url . clone ( ) ,
441
+ notification. clone ( ) ,
442
+ ) ?;
421
443
}
422
444
Ok ( ( ) )
423
445
}
0 commit comments