Select coins until the change output is guaranteed (B-98, BET-24)

select_utxos took the first coin covering the amount, so 10 + 1 PLM and a
10 PLM bet was refused with balance_leaves_no_change and a 10 PLM withdrawal
was reduced. User txs now ask for target + DUST_LIMIT_SATS; the old outcome is
kept only when no allowed combination leaves change.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
davideandClaude Sonnet 5.5 committed 2026-10-18 00:35:17 +02:00
1 parent 264441f5d6
commit 32df50cb75
7 files changed
+77 -34

No files matched your search

+19 -11
View File
@@ -127,7 +127,7 @@ def estimate_vsize(n_inputs: int, n_outputs: int) -> int:
def select_utxos(
utxos: list[Utxo], target_sats: int, max_inputs: int = MAX_TX_INPUTS
utxos: list[Utxo], target_sats: int, max_inputs: int = MAX_TX_INPUTS, min_change_sats: int = 0
) -> tuple[list[Utxo], int]:
"""Greedily select UTXOs (largest first, to minimize input count) covering
target_sats — the amount deducted from the sender's balance. The fee is paid
@@ -142,7 +142,13 @@ def select_utxos(
want different ones (B-52): MAX_TX_INPUTS protects a user from a fee that would
eat into their own bet/withdrawal, while the payout drains a pool holding one
UTXO per bet and needs MAX_PAYOUT_TX_INPUTS to be able to pay a full round at
all."""
all.
B-98 (BET-24): with `min_change_sats` selection goes on until the total covers
`target + min_change_sats`, so a second coin is taken when the first covers the
target exactly. Only when every allowed coin together cannot leave that change is
the shorter selection returned (any total >= target), and the caller then refuses
the bet or reduces the withdrawal as before."""
# B-75: a UTXO below the dust limit is never offered, even if one is already on
# record: it costs more to spend than it is worth.
ordered = sorted(
@@ -151,16 +157,18 @@ def select_utxos(
selected: list[Utxo] = []
total = 0
for utxo in ordered:
if len(selected) == max_inputs:
raise InsufficientFundsError(
f"balance too fragmented: more than {max_inputs} inputs would be needed",
code="too_many_inputs",
max_inputs=max_inputs,
)
if total >= target_sats + min_change_sats or len(selected) == max_inputs:
break
selected.append(utxo)
total += utxo.amount_sats
if total >= target_sats:
return selected, total
if total >= target_sats:
return selected, total
if len(selected) == max_inputs and len(ordered) > max_inputs:
raise InsufficientFundsError(
f"balance too fragmented: more than {max_inputs} inputs would be needed",
code="too_many_inputs",
max_inputs=max_inputs,
)
raise InsufficientFundsError("not enough confirmed balance to cover amount")
@@ -204,7 +212,7 @@ def build_signed_transaction(
never exactly equal the bet" is a documented invariant of the PLAY phase. The
player needs a little more than the bet amount, which is what the error says.
"""
selected, total_in = select_utxos(utxos, amount_sats)
selected, total_in = select_utxos(utxos, amount_sats, min_change_sats=DUST_LIMIT_SATS)
fee = estimate_vsize(len(selected), 2) * fee_rate_sat_vb
change = total_in - amount_sats