Always keep a change output, so every tx stays fee-bumpable (B-62)
The max-amount checkbox sends amount_sats == the whole confirmed balance, so change came out at 0, the change output was dropped, and the transaction had a single output. bump_fee has nothing to shrink there: it raised RbfError every 30s until the reconciler abandoned the row six hours later. The RBF single-change-output limitation was a documented gap, but the UI made it the *default* withdrawal path. The extra-input fallback would not have helped this case: a transaction moving the entire balance already spends every UTXO the sender has. So the fix is at build time — build_signed_transaction never produces a change output below DUST_LIMIT_SATS, and never folds it into the fee either: - withdrawals pass reduce_amount_to_keep_change=True and move a dust limit less. The fee already comes out of the withdrawn amount by design, so this is the same rule applied a little harder, and Withdrawal.amount_requested_sats vs amount_sent_sats already existed to record the difference. - bets don't: the bet is a fixed price that can't be quietly reduced. A balance exactly equal to the bet is refused with balance_leaves_no_change (translated into all 7 languages, carrying required_extra_sats), which turns "a user's balance must never exactly equal the bet" from a documented assumption into an enforced one — and stops an unbumpable bet from holding a round open until the reconciler gives up on it. bump_fee's no-change guard stays: a single-output tx broadcast before this change can still be pending across the deploy, and it must fail loudly rather than start shrinking a recipient's output. Its test now hand-builds that shape, precisely because the builder no longer will. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
37cc5eeeb5
commit
8dd913ec59
9 files changed
+197
-51
No files matched your search
@@ -219,3 +219,75 @@ async def test_failed_broadcast_marks_the_withdrawal_failed_and_frees_the_coins(
|
||||
assert (await session.scalars(select(UtxoEvent).where(UtxoEvent.user_id == user_id))).one().spent_txid is None
|
||||
user = await session.get(User, user_id)
|
||||
assert user.cached_balance_sats == 3_000_000_000
|
||||
|
||||
|
||||
# --- B-62: "withdraw everything" must not build an unbumpable transaction ---------
|
||||
|
||||
|
||||
async def test_full_balance_withdrawal_keeps_a_bumpable_change_output(session_factory):
|
||||
"""The UI's max-amount checkbox sends the whole confirmed balance, so change came
|
||||
out at 0, the change output was dropped, and the tx had a single output —
|
||||
bump_fee then had nothing to shrink and raised RbfError every 30s until the
|
||||
reconciler abandoned the row hours later. Adding inputs is no answer here: the tx
|
||||
already spends every UTXO the user has. So a dust limit stays behind instead."""
|
||||
from embit.transaction import Transaction
|
||||
|
||||
from app.wallet.psbt_builder import DUST_LIMIT_SATS
|
||||
|
||||
balance = 2_000_000_000
|
||||
user_id = await _make_funded_user(session_factory, 40, balance)
|
||||
client = FakeElectrumClient()
|
||||
|
||||
async with session_factory() as session:
|
||||
user = await session.get(User, user_id)
|
||||
withdrawal = await request_withdrawal(session, client, user, EXTERNAL_ADDRESS, balance)
|
||||
|
||||
tx = Transaction.parse(bytes.fromhex(client.broadcasted[0]))
|
||||
assert len(tx.vout) == 2 # recipient + change: bumpable
|
||||
assert all(o.value >= DUST_LIMIT_SATS for o in tx.vout)
|
||||
|
||||
# The user asked for everything and is told what actually went out — the row
|
||||
# already distinguishes the two, since the fee comes out of the amount anyway.
|
||||
assert withdrawal.amount_requested_sats == balance
|
||||
fee = balance - sum(o.value for o in tx.vout)
|
||||
change = min(o.value for o in tx.vout)
|
||||
assert change == DUST_LIMIT_SATS
|
||||
assert withdrawal.amount_sent_sats == balance - DUST_LIMIT_SATS - fee
|
||||
|
||||
|
||||
async def test_a_bet_from_a_balance_equal_to_the_bet_is_refused(session_factory):
|
||||
"""The same shape on the PLAY side, where reducing the amount isn't an option —
|
||||
the bet is a fixed price. "A user's balance must never exactly equal the bet" is
|
||||
a documented invariant of the PLAY phase; this is where it's enforced, with an
|
||||
error that says how much more is needed rather than a bare "insufficient"."""
|
||||
from app.bets.service import BetError
|
||||
from app.wallet.psbt_builder import DUST_LIMIT_SATS
|
||||
|
||||
user_id = await _make_funded_user(session_factory, 41, BET_AMOUNT_SATS) # exactly the bet
|
||||
client = FakeElectrumClient()
|
||||
|
||||
async with session_factory() as session:
|
||||
user = await session.get(User, user_id)
|
||||
with pytest.raises(BetError) as excinfo:
|
||||
await place_bet(session, client, user)
|
||||
|
||||
assert excinfo.value.code == "balance_leaves_no_change"
|
||||
assert excinfo.value.params == {"required_extra_sats": DUST_LIMIT_SATS}
|
||||
assert not client.broadcasted
|
||||
|
||||
async with session_factory() as session:
|
||||
utxo = (await session.scalars(select(UtxoEvent).where(UtxoEvent.user_id == user_id))).one()
|
||||
assert utxo.spent_txid is None # refused before anything moved
|
||||
|
||||
|
||||
async def test_a_bet_with_a_dust_limit_of_headroom_is_accepted(session_factory):
|
||||
from app.wallet.psbt_builder import DUST_LIMIT_SATS
|
||||
|
||||
user_id = await _make_funded_user(session_factory, 42, BET_AMOUNT_SATS + DUST_LIMIT_SATS)
|
||||
client = FakeElectrumClient()
|
||||
|
||||
async with session_factory() as session:
|
||||
user = await session.get(User, user_id)
|
||||
participant = await place_bet(session, client, user)
|
||||
|
||||
assert participant.status == "broadcast"
|
||||
Reference in new issue
Block a user