"""Replace-by-fee for the platform's transactions (roadmap B7, TX-32, TX-10..TX-13, TX-24). Same coins, same outputs to the counterpart; the increase comes out of one output only, the player's change. The replacement is signed and **recorded as a version before it is sent**; after the send (accepted or uncertain) the row moves to it with an update that matches only if the row is still `pending` and still on the txid that was replaced. """ import asyncio import logging from datetime import datetime, timedelta from django.db import transaction from django.utils import timezone from embit import script from embit.finalizer import finalize_psbt from embit.psbt import PSBT from embit.transaction import Transaction, TransactionInput, TransactionOutput from plm_engine import hd from plm_engine.psbt_builder import DUST_LIMIT_SATS, MAX_FEE_RATE_SAT_VB, RBF_SEQUENCE, estimate_vsize from . import audit, keys from .chain import register_version from .electrum_chain import Outcome from .models import AuditEntry, ChainTransaction, DepositAddress, LotteryConfig, Participation logger = logging.getLogger(__name__) WORKER = "worker" FEE_RATE_INCREMENT = 1 # sat/vB more than the recorded rate, per bump (TX-10) INCREMENTAL_RELAY_FEE_RATE = 1 # BIP125 rule 4, sat/vB REASON_MAX_RATE = "max_fee_rate" REASON_NO_CHANGE = "no_change_output" REASON_REFUSED = "replacement_refused" class RbfError(Exception): def __init__(self, message: str, reason: str): super().__init__(message) self.reason = reason def due(now: datetime | None = None) -> list[ChainTransaction]: """TX-32: pending bets whose last send is older than the timeout.""" now = now or timezone.now() limit = now - timedelta(seconds=LotteryConfig.get().rbf_timeout_seconds) rows = ChainTransaction.objects.filter(kind=ChainTransaction.Kind.BET, status=ChainTransaction.Status.PENDING) return [t for t in rows.order_by("id") if (t.last_broadcast_at or t.broadcast_at or t.created_at) <= limit] def _build_replacement(tx: ChainTransaction, account) -> tuple[str, str, int, int]: """Phase 2, no network and no open transaction: the signed replacement. Returns `(txid, raw hex, new rate, vsize)`.""" if (tx.fee_rate_sat_vb or 1) >= MAX_FEE_RATE_SAT_VB: raise RbfError(f"already at the maximum fee rate ({MAX_FEE_RATE_SAT_VB} sat/vB)", REASON_MAX_RATE) owner = DepositAddress.objects.get(user=tx.user) key = hd.user_key(account, owner.derivation_index) own_script = script.p2wpkh(key.to_public()) old = Transaction.parse(bytes.fromhex(tx.raw_hex)) amounts = {(u.txid, u.vout): u.amount_sats for u in tx.reserved_utxos.all()} input_amounts = [amounts[(v.txid.hex(), v.vout)] for v in old.vin] old_fee = sum(input_amounts) - sum(o.value for o in old.vout) vsize = estimate_vsize(len(old.vin), len(old.vout)) rate = tx.fee_rate_sat_vb or 1 target_rate = min(rate + FEE_RATE_INCREMENT, MAX_FEE_RATE_SAT_VB) min_delta = vsize * INCREMENTAL_RELAY_FEE_RATE # BIP125: never below this, whatever the target says delta = max(vsize * target_rate - old_fee, min_delta) change = next((i for i, o in enumerate(old.vout) if o.script_pubkey.data == own_script.data), None) if change is None: raise RbfError("there is no change output to take the increase from", REASON_NO_CHANGE) available = old.vout[change].value if available < min_delta: raise RbfError(f"its change ({available} sat) cannot cover the minimum increase ({min_delta} sat)", REASON_NO_CHANGE) outputs = list(old.vout) if available - delta >= DUST_LIMIT_SATS: outputs[change] = TransactionOutput(available - delta, outputs[change].script_pubkey) else: # TX-11: nothing relayable would be left, so the output goes to the fee and this is the last replacement del outputs[change] delta = available new = Transaction(vin=[TransactionInput(v.txid, v.vout, sequence=RBF_SEQUENCE) for v in old.vin], vout=outputs) psbt = PSBT(new) for i, amount in enumerate(input_amounts): psbt.inputs[i].witness_utxo = TransactionOutput(amount, own_script) if psbt.sign_with(key) != len(old.vin): raise RuntimeError("the replacement was not signed on every input") final = finalize_psbt(psbt) if final is None: raise RuntimeError("failed to finalize the replacement") new_rate = (old_fee + delta) // estimate_vsize(len(old.vin), len(outputs)) return final.txid().hex(), final.serialize().hex(), new_rate, vsize def _record_failure(tx: ChainTransaction, reason: str, message: str) -> None: """TX-24: once per (txid, reason); the log carries every repeat.""" seen = AuditEntry.objects.filter(action="rbf_bump_failed", subject_id=str(tx.pk)).values_list("payload", flat=True) if any(p.get("txid") == tx.current_txid and p.get("reason") == reason for p in seen): logger.info("bump of transaction %s failed again (%s): %s", tx.pk, reason, message) return audit.record("rbf_bump_failed", actor_label=WORKER, subject_type="chain_transaction", subject_id=tx.pk, outcome=AuditEntry.Outcome.ERROR, reason=message, payload={"txid": tx.current_txid, "reason": reason}) def bump(tx_id: int, chain, now: datetime | None = None) -> str | None: """Accelerate one transaction. Returns the new txid, or None if there was nothing to do (not pending any more, a conflict, or an impossible/refused bump, which is recorded).""" now = now or timezone.now() tx = ChainTransaction.objects.select_related("user").filter(pk=tx_id).first() if tx is None or tx.status != ChainTransaction.Status.PENDING: return None try: account = keys.signing_account_key() new_txid, raw_hex, new_rate, _ = _build_replacement(tx, account) except RbfError as error: _record_failure(tx, error.reason, str(error)) return None except keys.SigningKeyUnavailable as error: logger.error("cannot bump transaction %s: %s", tx_id, error) return None old_txid = tx.current_txid with transaction.atomic(): # the candidate is recorded before it is sent (TX-12) fresh = ChainTransaction.objects.select_for_update().get(pk=tx_id) if fresh.status != ChainTransaction.Status.PENDING or fresh.current_txid != old_txid: return None from .models import TransactionVersion if not TransactionVersion.objects.filter(txid=new_txid).exists(): TransactionVersion.objects.create(transaction=fresh, txid=new_txid, raw_hex=raw_hex) verdict = asyncio.run(chain.broadcast(raw_hex, new_txid)) if verdict.outcome is Outcome.REFUSED: # the candidate stays recorded; nobody will confirm it (TX-24) _record_failure(tx, REASON_REFUSED, f"the network refused the replacement: {verdict.reason}") return None with transaction.atomic(): # TX-12: move the row, only if it is still exactly what was replaced moved = ChainTransaction.objects.filter( pk=tx_id, status=ChainTransaction.Status.PENDING, current_txid=old_txid ).update(current_txid=new_txid, raw_hex=raw_hex, fee_rate_sat_vb=new_rate, last_broadcast_at=now, attempt_count=tx.attempt_count + 1) if not moved: # TX-13: it changed under us; the candidate stays and confirmation looks for it anyway logger.warning("transaction %s changed while its replacement was being sent: conflict", tx_id) return None Participation.objects.filter(movements__chain_transaction_id=tx_id, bet_txid=old_txid).update(bet_txid=new_txid) audit.record("rbf_bumped", actor_label=WORKER, subject_type="chain_transaction", subject_id=tx_id, payload={"from": old_txid, "to": new_txid, "fee_rate_sat_vb": new_rate, "network": verdict.outcome.value}) return new_txid def bump_due(chain, now: datetime | None = None) -> int: """TX-32: accelerate every transaction that waited too long. An error on one row stops no other.""" count = 0 for tx in due(now): try: if bump(tx.pk, chain, now): count += 1 except Exception: logger.exception("bumping transaction %s failed", tx.pk) return count