From f4b96107fa9b6f2c001b0c4faccd7b19084dda9c Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Wed, 26 Aug 2026 08:32:35 +0000 Subject: [PATCH] fix: reduce pending payment polling rate --- tasks.py | 4 +++- tests/unit/test_tasks.py | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/tasks.py b/tasks.py index 8dffedb..5ea3ffc 100644 --- a/tasks.py +++ b/tasks.py @@ -34,6 +34,8 @@ from .paranoia import ( ) from .permission import nwc_permissions +PAYMENT_STATUS_POLL_INTERVAL_SECONDS = 1.0 + async def _check(nwc: NWCKey | None, method: str) -> dict | None: # check @@ -122,7 +124,7 @@ async def _process_invoice( }, "in_budget": in_budget, } - await asyncio.sleep(0.05) + await asyncio.sleep(PAYMENT_STATUS_POLL_INTERVAL_SECONDS) if not payment_status: raise Exception("Payment status not found") return { diff --git a/tests/unit/test_tasks.py b/tests/unit/test_tasks.py index cca51bb..9758954 100644 --- a/tests/unit/test_tasks.py +++ b/tests/unit/test_tasks.py @@ -29,3 +29,51 @@ async def test_process_invoice_returns_payment_failed_on_failed_status(monkeypat assert result["error"]["code"] == "PAYMENT_FAILED" assert result["error"]["message"] == "Payment failed." assert result["in_budget"] is True + + +@pytest.mark.asyncio +async def test_process_invoice_polls_pending_payment_at_sustainable_interval( + monkeypatch, +): + async def fake_tracked_spend_nwc(*args, **kwargs): + return True, "a" * 64 + + statuses = iter( + [ + SimpleNamespace(success=False, failed=False), + SimpleNamespace( + success=True, + failed=False, + preimage="b" * 64, + fee_msat=10, + paid=True, + ), + ] + ) + + async def fake_check_transaction_status(wallet_id: str, payment_hash: str): + return next(statuses) + + sleep_calls = [] + + async def fake_sleep(delay: float): + sleep_calls.append(delay) + + monkeypatch.setattr(tasks, "tracked_spend_nwc", fake_tracked_spend_nwc) + monkeypatch.setattr( + tasks, "check_transaction_status", fake_check_transaction_status + ) + monkeypatch.setattr(tasks.asyncio, "sleep", fake_sleep) + + result = await tasks._process_invoice( + wallet_id="wallet123", + pubkey="a" * 64, + invoice="lnbc1example", + amount_msats=1000, + description="test", + ) + + assert sleep_calls == [tasks.PAYMENT_STATUS_POLL_INTERVAL_SECONDS] + assert result["preimage"] == "b" * 64 + assert result["fee_msats"] == 10 + assert result["paid"] is True