fix: keep unrecorded Spark payments pending (#4369)
What changed, and why it matters
This change fixes how LNbits handles Spark wallet payment lookups when the payment provider has no record of a payment yet. Previously, an empty response was treated as 'failed', which could mark a payment as failed before it had a chance to complete. Now it stays 'pending' until the provider returns a definitive terminal status. This is a correctness fix that prevents premature payment failure states, which can have financial or trust implications in a Lightning payment system.
Review and merge. Consider auditing other wallet backends for similar premature-failure behavior when provider records are absent or temporarily unavailable. No immediate incident response required unless premature failure statuses have already caused user-facing payment disputes.
Security signals we found
State machine fix: prevents premature terminal failure status on missing payment record
Financial correctness: avoids marking in-flight Lightning payments as failed before confirmation
Defensive coding: aligns empty provider response with pending rather than failed
Test coverage added for exact terminal status mapping
Evidence from the diff
In lnbits/wallets/spark.py, get_payment_status() previously returned PaymentFailedStatus() when the Spark ‘listpays’ RPC returned an empty ‘pays’ array. The patch changes this to return PaymentPendingStatus(), keeping the payment in a pending state until the provider can confirm success or failure. A unit test is added to verify exact terminal status behavior for provider responses of None/pending/complete/failed. The change is narrow and defensive, reducing the risk of incorrectly finalizing an in-flight payment as failed.
Changed components
lnbits/wallets/spark.pySparkWallet.get_payment_status()tests/unit/test_wallet_payment_ambiguity.pyInspect captured patch +33 / −1
### lnbits/wallets/spark.py
@@ -244,7 +244,7 @@ async def get_payment_status(self, checking_id: str) -> PaymentStatus:
return PaymentPendingStatus()
if not r["pays"]:
- return PaymentFailedStatus()
+ return PaymentPendingStatus()
if r["pays"][0]["payment_hash"] == checking_id:
status = r["pays"][0]["status"]
if status == "complete":
### tests/unit/test_wallet_payment_ambiguity.py
@@ -568,6 +568,38 @@ async def test_spark_invoice_uses_exact_terminal_status(
assert status.paid is expected
+@pytest.mark.anyio
+@pytest.mark.parametrize(
+ ("provider_status", "expected"),
+ [(None, None), ("pending", None), ("failed", False), ("complete", True)],
+)
+async def test_spark_payment_uses_exact_terminal_status(
+ mocker: MockerFixture,
+ provider_status: str | None,
+ expected: bool | None,
+):
+ wallet = object.__new__(SparkWallet)
+ payment_hash = "01" * 32
+ pays = (
+ [
+ {
+ "payment_hash": payment_hash,
+ "status": provider_status,
+ "amount_msat": "1000msat",
+ "amount_sent_msat": "1001msat",
+ "preimage": "02" * 32,
+ }
+ ]
+ if provider_status is not None
+ else []
+ )
+ mocker.patch.object(wallet, "listpays", return_value={"pays": pays})
+
+ status = await wallet.get_payment_status(payment_hash)
+
+ assert status.paid is expected
+
+
@pytest.mark.anyio
@pytest.mark.parametrize(
("state", "expected"),Why this scored 59/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.