fix: reduce pending payment polling rate
This commit is contained in:
parent
d432d74c88
commit
f4b96107fa
2 changed files with 51 additions and 1 deletions
4
tasks.py
4
tasks.py
|
|
@ -34,6 +34,8 @@ from .paranoia import (
|
||||||
)
|
)
|
||||||
from .permission import nwc_permissions
|
from .permission import nwc_permissions
|
||||||
|
|
||||||
|
PAYMENT_STATUS_POLL_INTERVAL_SECONDS = 1.0
|
||||||
|
|
||||||
|
|
||||||
async def _check(nwc: NWCKey | None, method: str) -> dict | None:
|
async def _check(nwc: NWCKey | None, method: str) -> dict | None:
|
||||||
# check
|
# check
|
||||||
|
|
@ -122,7 +124,7 @@ async def _process_invoice(
|
||||||
},
|
},
|
||||||
"in_budget": in_budget,
|
"in_budget": in_budget,
|
||||||
}
|
}
|
||||||
await asyncio.sleep(0.05)
|
await asyncio.sleep(PAYMENT_STATUS_POLL_INTERVAL_SECONDS)
|
||||||
if not payment_status:
|
if not payment_status:
|
||||||
raise Exception("Payment status not found")
|
raise Exception("Payment status not found")
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
|
|
@ -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"]["code"] == "PAYMENT_FAILED"
|
||||||
assert result["error"]["message"] == "Payment failed."
|
assert result["error"]["message"] == "Payment failed."
|
||||||
assert result["in_budget"] is True
|
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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue