mirror of
https://github.com/Such-Software/xmrcheckout.git
synced 2026-12-01 12:14:11 +01:00
fix(payments): confirm an invoice only when confirmed payments cover it
The reconciler took the deepest single payment's confirmations as the invoice's. A tiny early payment could therefore make an unconfirmed top-up count as confirmed, and InvoiceSettled fired before the money was final. The invoice depth is now the depth at which payments, taken deepest first, cover the whole amount. It is 0 while the invoice is underpaid.
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Iterable, Protocol
|
||||
|
||||
|
||||
class _Payment(Protocol):
|
||||
amount_atomic: int
|
||||
confirmations: int
|
||||
|
||||
|
||||
def effective_confirmations(transfers: Iterable[_Payment], required_atomic: int) -> int:
|
||||
"""Return the confirmation depth at which the invoice amount is fully covered.
|
||||
|
||||
Payments are taken from the deepest down, and the depth of the last payment
|
||||
needed to reach required_atomic is the invoice's depth. An invoice is
|
||||
therefore confirmed to N only when payments with at least N confirmations
|
||||
cover the whole amount. A small early payment cannot lend its depth to an
|
||||
unconfirmed top-up. Returns 0 while the payments do not cover the amount.
|
||||
"""
|
||||
payments = sorted(
|
||||
(t for t in transfers if t.amount_atomic > 0),
|
||||
key=lambda t: t.confirmations,
|
||||
reverse=True,
|
||||
)
|
||||
covered = 0
|
||||
for payment in payments:
|
||||
covered += payment.amount_atomic
|
||||
if covered >= required_atomic:
|
||||
return max(0, int(payment.confirmations))
|
||||
return 0
|
||||
@@ -12,6 +12,7 @@ from sqlalchemy.orm import Session
|
||||
from .btcpay_webhooks import dispatch_btcpay_webhooks
|
||||
from .config import INVOICE_RECONCILE_INTERVAL_SECONDS, LATE_PAYMENT_LOOKBACK_HOURS
|
||||
from .db import SessionLocal
|
||||
from .payment_confirmations import effective_confirmations
|
||||
from .models import Invoice, InvoiceTransfer, SystemStatus, User
|
||||
from .monero_service import MoneroWalletService, TransferDetail
|
||||
from .webhooks import dispatch_webhooks
|
||||
@@ -132,13 +133,14 @@ def _reconcile_invoices(service: MoneroWalletService) -> None:
|
||||
)
|
||||
|
||||
total_atomic = 0
|
||||
max_confirmations = 0
|
||||
for transfer in transfers:
|
||||
if transfer.amount_atomic <= 0:
|
||||
continue
|
||||
total_atomic += transfer.amount_atomic
|
||||
if transfer.confirmations > max_confirmations:
|
||||
max_confirmations = transfer.confirmations
|
||||
required_atomic = _xmr_to_atomic(invoice.amount_xmr)
|
||||
# Depth of the payments that cover the amount, not of the
|
||||
# deepest single payment.
|
||||
max_confirmations = effective_confirmations(transfers, required_atomic)
|
||||
logger.debug(
|
||||
"Invoice totals",
|
||||
extra={
|
||||
@@ -163,7 +165,6 @@ def _reconcile_invoices(service: MoneroWalletService) -> None:
|
||||
invoice.total_paid_atomic = total_atomic
|
||||
db.add(invoice)
|
||||
db.commit()
|
||||
required_atomic = _xmr_to_atomic(invoice.amount_xmr)
|
||||
is_paid = total_atomic >= required_atomic
|
||||
|
||||
expires_at = invoice.expires_at
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import unittest
|
||||
from dataclasses import dataclass
|
||||
|
||||
from app.payment_confirmations import effective_confirmations
|
||||
|
||||
|
||||
@dataclass
|
||||
class _Payment:
|
||||
amount_atomic: int
|
||||
confirmations: int
|
||||
|
||||
|
||||
class EffectiveConfirmationTests(unittest.TestCase):
|
||||
def test_a_deep_small_payment_does_not_confirm_an_unconfirmed_top_up(self):
|
||||
payments = [_Payment(1, 30), _Payment(999, 0)]
|
||||
self.assertEqual(effective_confirmations(payments, 1000), 0)
|
||||
|
||||
def test_depth_is_that_of_the_shallowest_payment_needed(self):
|
||||
payments = [_Payment(400, 12), _Payment(600, 3)]
|
||||
self.assertEqual(effective_confirmations(payments, 1000), 3)
|
||||
|
||||
def test_an_extra_unconfirmed_payment_does_not_lower_a_covered_invoice(self):
|
||||
payments = [_Payment(1000, 10), _Payment(5, 0)]
|
||||
self.assertEqual(effective_confirmations(payments, 1000), 10)
|
||||
|
||||
def test_an_underpaid_invoice_has_no_depth(self):
|
||||
self.assertEqual(effective_confirmations([_Payment(500, 50)], 1000), 0)
|
||||
|
||||
def test_non_positive_amounts_are_ignored(self):
|
||||
payments = [_Payment(-5, 40), _Payment(0, 40), _Payment(1000, 2)]
|
||||
self.assertEqual(effective_confirmations(payments, 1000), 2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user