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
|
||||
|
||||
Reference in New Issue
Block a user