Skip to content

Commit 2a534c4

Browse files
committed
Deduplicate PaymentReceived events on restart
PaymentClaimed events can be replayed on every Node startup. Since nodes are pinged at minimum every 30 minutes, this causes duplicate PaymentReceived events to stack up in the queue when the event queue is not being actively processed. These stacked events can cause payouts to fail as they timeout before the queue is drained to the actual events related to a payout. Add a check before queuing PaymentReceived to skip if an event for that payment_id already exists in the queue, preventing duplicates.
1 parent 9fd9946 commit 2a534c4

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/event.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,14 @@ where
358358
Ok(())
359359
}
360360

361+
/// Check if a PaymentReceived event with the given payment_id already exists in the queue.
362+
pub(crate) fn contains_payment_received(&self, payment_id: &PaymentId) -> bool {
363+
let locked_queue = self.queue.lock().unwrap();
364+
locked_queue.iter().any(|event| {
365+
matches!(event, Event::PaymentReceived { payment_id: Some(id), .. } if id == payment_id)
366+
})
367+
}
368+
361369
fn persist_queue(&self, locked_queue: &VecDeque<Event>) -> Result<(), Error> {
362370
let data = EventQueueSerWrapper(locked_queue).encode();
363371
self.kv_store
@@ -938,6 +946,18 @@ where
938946
},
939947
}
940948

949+
// Check if a PaymentReceived event for this payment already exists in the queue.
950+
// This handles the case where PaymentClaimed is replayed on restart - we only
951+
// want to queue one PaymentReceived event per payment.
952+
if self.event_queue.contains_payment_received(&payment_id) {
953+
log_debug!(
954+
self.logger,
955+
"Skipping duplicate PaymentReceived for payment {}: event already queued",
956+
payment_id,
957+
);
958+
return Ok(());
959+
}
960+
941961
let event = Event::PaymentReceived {
942962
payment_id: Some(payment_id),
943963
payment_hash,

0 commit comments

Comments
 (0)