fix: reconciler hardening, decimal formatting, webhook retries

- Advisory lock: wrap per-invoice processing in try/finally so lock is
  ALWAYS released, even on exception. Previously, any error between lock
  acquisition and release would leak the lock, causing the invoice to be
  silently skipped forever on subsequent cycles.

- Error logging: wallet RPC errors now logged at ERROR level (was DEBUG),
  making silent reconciliation failures visible in production logs.

- Transfer protection: don't delete stored transfers when wallet-rpc
  returns an empty response (transient RPC glitch). Only prune when we
  actually got transfer data back.

- Wallet restore height: new wallets generated from keys now look back
  ~2 days (1440 blocks) instead of starting at current height. Prevents
  missing payments that arrived before the wallet was first opened on a
  given wallet-rpc instance.

- Decimal formatting: _format_xmr_fixed() now strips trailing zeros
  instead of always showing 12 decimal places.

- Invoice amount rounding: fiat-converted amounts now use ROUND_UP with
  3 decimal places (was ROUND_DOWN with 12), preventing undercharging.

- Webhook retries: BTCPay webhook delivery now retries up to 3 times
  with 1s/2s backoff on 5xx or network errors. Timeout increased 5s→10s.
  Failures logged at ERROR level (was WARNING/DEBUG).
This commit is contained in:
jwinterm
2026-03-28 11:51:04 -04:00
parent 2b8cb798b9
commit 11974276c4
5 changed files with 160 additions and 124 deletions
+6 -2
View File
@@ -1,7 +1,7 @@
import uuid
from typing import Any
from datetime import datetime, timedelta, timezone
from decimal import Decimal, ROUND_DOWN, InvalidOperation
from decimal import Decimal, ROUND_DOWN, ROUND_UP, InvalidOperation
import csv
import io
import json
@@ -342,8 +342,12 @@ def _resolve_invoice_amount(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Fiat quote service unavailable",
) from exc
# Round UP to 3 decimal places so we never undercharge.
# 3 decimals = 0.001 WOW — more than enough precision for Wownero.
# See also: btcpay_routes.py for the same rounding.
# To change precision: adjust the Decimal quantize string here and in btcpay_routes.py.
amount_xmr = (Decimal(requested_amount_fiat) / quote.rate).quantize(
Decimal("0.000000000001"), rounding=ROUND_DOWN
Decimal("0.001"), rounding=ROUND_UP
)
warnings = ["Fiat conversion is an estimate and does not lock a rate."]
quote_payload = {