Skip to content

NWC funding source: Reduce polling#4008

Merged
blackcoffeexbt merged 13 commits into
lnbits:devfrom
blackcoffeexbt:bugfix/nwc-improve-load
Jul 2, 2026
Merged

NWC funding source: Reduce polling#4008
blackcoffeexbt merged 13 commits into
lnbits:devfrom
blackcoffeexbt:bugfix/nwc-improve-load

Conversation

@blackcoffeexbt

Copy link
Copy Markdown
Collaborator

Improve NWC funding source by relying more on list_transactions, caching and NWC notifications, and less on lookup_invoice polling.

This changes the NWC wallet backend so pending incoming invoices are cached and reconciled through list_transactions, and marked as paid from notifications. If notifications are missing or delayed, it falls back to scheduled lookups with a backoff schedule instead of polling aggressively.

@blackcoffeexbt blackcoffeexbt force-pushed the bugfix/nwc-improve-load branch from ea4ae6e to ab831e9 Compare June 15, 2026 11:32
@codecov

codecov Bot commented Jun 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.09276% with 269 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.26%. Comparing base (8c7e5c2) to head (0f0a007).
⚠️ Report is 160 commits behind head on dev.

Files with missing lines Patch % Lines
lnbits/wallets/nwc.py 50.09% 269 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #4008      +/-   ##
==========================================
+ Coverage   56.50%   64.26%   +7.76%     
==========================================
  Files         123      128       +5     
  Lines       16914    18921    +2007     
==========================================
+ Hits         9557    12160    +2603     
+ Misses       7357     6761     -596     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@riccardobl riccardobl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

left some comments

Comment thread lnbits/wallets/nwc.py
"#p": [self.account_public_key_hex],
"#e": [event["id"]],
"since": event["created_at"],
"authors": [self.service_pubkey_hex],

@riccardobl riccardobl Jun 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this should be added back to filter out spam and replayed events

Comment thread lnbits/wallets/nwc.py
sub_id = cast(str, msg[1])
event = cast(dict, msg[2])
# Ensure the event is valid (do not trust relays)
if not verify_event(event) or event.get("pubkey") != self.service_pubkey_hex:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this should be added back to protect against malicious relays

Comment thread lnbits/wallets/nwc.py Outdated
was_pending = self._remove_pending_invoice(checking_id)
if was_pending:
logger.debug("Pending invoice " + checking_id + " settled via " + source)
self.paid_invoices_queue.put_nowait(checking_id)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i think this should go inside the if, to avoid firing multiple paid events on lnbits if notifications are duplicated

Comment thread lnbits/wallets/nwc.py
"#p": [self.account_public_key_hex],
"since": int(time.time()),
}
self.notification_subscription_ids.add(sub_id)

@riccardobl riccardobl Jun 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

these need to be added to self.subscriptions so that they are properly cleared by _close_subscription_by_subid

Comment thread lnbits/wallets/nwc.py
except Exception as e:
logger.error("Error handling pending invoice: " + str(e))
invoice["lookup_attempts"] = int(invoice.get("lookup_attempts", 0)) + 1
self._schedule_next_lookup(invoice, now)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think there should be a sleep here.
The original issue imo was caused by instances with many pending invoices, while the lookup round is rate-limited, all the pending invoices are checked at once.

See this commit riccardobl@dd5bbd1

@motorina0 motorina0 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@blackcoffeexbt blackcoffeexbt force-pushed the bugfix/nwc-improve-load branch from d797f00 to 568b37c Compare June 22, 2026 12:10
@blackcoffeexbt blackcoffeexbt force-pushed the bugfix/nwc-improve-load branch from 973e1b0 to 3cf26be Compare June 24, 2026 12:04
@DoktorShift DoktorShift self-requested a review June 24, 2026 13:52
@dadofsambonzuki

Copy link
Copy Markdown
Contributor

Linking to lnbits/nostrclient#70 for prosperity.

@blackcoffeexbt blackcoffeexbt merged commit 7db5c98 into lnbits:dev Jul 2, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants