Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![deny(clippy::all)]

use std::{
collections::HashMap,
collections::{HashMap, HashSet},
convert::TryFrom,
fmt::{self, Write},
str::FromStr,
Expand Down Expand Up @@ -38,6 +38,7 @@ use ldk_node::{
util::scid_utils,
},
lightning_invoice::{Bolt11Invoice, Bolt11InvoiceDescription, Description},
lightning_types::payment::PaymentHash,
Builder, Event, Node,
};
use tokio::runtime::Runtime;
Expand Down Expand Up @@ -439,7 +440,11 @@ impl MdkNode {
min_threshold_ms: i64,
quiet_threshold_ms: i64,
) -> Vec<ReceivedPayment> {
// Hard timeout to prevent infinite wait if claims get stuck (60 seconds)
const HARD_TIMEOUT_MS: i64 = 60_000;

let mut received_payments = vec![];
let mut pending_claims: HashSet<PaymentHash> = HashSet::new();

if let Err(err) = self.node.start() {
eprintln!("[lightning-js] Failed to start node in receive_payment: {err}");
Expand All @@ -460,7 +465,25 @@ impl MdkNode {
let total_time_elapsed = now.duration_since(start_sync_at).as_millis() as i64;
let quiet_time_elapsed = now.duration_since(last_event_time).as_millis() as i64;

if total_time_elapsed >= min_threshold_ms && quiet_time_elapsed >= quiet_threshold_ms {
// Don't exit if we have pending claims - wait for them to resolve
let can_exit = pending_claims.is_empty()
&& total_time_elapsed >= min_threshold_ms
&& quiet_time_elapsed >= quiet_threshold_ms;

if can_exit {
break;
}

// Hard timeout to prevent infinite wait
if total_time_elapsed >= HARD_TIMEOUT_MS {
if !pending_claims.is_empty() {
eprintln!(
"[lightning-js] WARNING: Exiting receive_payment with {} pending claims after hard timeout ({}ms): {:?}",
Comment on lines +477 to +481

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor caller thresholds before hard timeout

This hard-timeout check is unconditional, so receive_payment will break after 60s even when min_threshold_ms/quiet_threshold_ms are larger and there are no pending claims. That changes the API’s timing semantics: callers that pass a larger minimum (e.g., to wait a few minutes for incoming payments) now return early at 60s and can miss later events. Consider applying the hard timeout only when pending_claims is non-empty or making it max(min_threshold_ms, HARD_TIMEOUT_MS).

Useful? React with 👍 / 👎.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not ideal but it's unreasonable to pass in a larger threshold right now as it sets a floor on payment confirmation time. The hard timeout already only applies when pending claims is non empty

pending_claims.len(),
HARD_TIMEOUT_MS,
pending_claims.iter().map(|h| bytes_to_hex(&h.0)).collect::<Vec<_>>()
);
}
break;
}

Expand Down Expand Up @@ -489,6 +512,11 @@ impl MdkNode {
eprintln!(
"[lightning-js] PaymentFailed payment_id={payment_id_hex} payment_hash={payment_hash_hex} reason={reason_str}",
);

// Remove from pending claims if present (claim failed, no longer pending)
if let Some(hash) = payment_hash {
pending_claims.remove(hash);
}
}
Event::PaymentClaimable {
payment_hash,
Expand All @@ -505,6 +533,9 @@ impl MdkNode {
eprintln!(
"[lightning-js] PaymentClaimable payment_hash={payment_hash_hex} claimable_amount_msat={claimable_amount_msat} claim_deadline={claim_deadline_str}",
);

// Track this payment as pending - we must wait for it to complete
pending_claims.insert(*payment_hash);
}
Event::PaymentReceived {
payment_hash,
Expand All @@ -515,6 +546,9 @@ impl MdkNode {
eprintln!(
"[lightning-js] PaymentReceived payment_hash={payment_hash_hex} amount_msat={amount_msat}",
);

// Claim completed - remove from pending
pending_claims.remove(payment_hash);
}
_ => {}
}
Expand Down
Loading