B7: confirmations with merkle proof, adoption of the confirmed version, RBF bumper and reconciler (TX-30..33, DAT-85)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
feff993584
commit
fa57d2e5ab
20 files changed
+1165
-20
No files matched your search
@@ -0,0 +1,181 @@
|
||||
"""B7 — replace-by-fee: TX-32, TX-10..TX-13, TX-24, DAT-85."""
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
import pytest
|
||||
from django.utils import timezone
|
||||
from embit.transaction import Transaction
|
||||
from plm_engine.psbt_builder import DUST_LIMIT_SATS, MAX_FEE_RATE_SAT_VB, estimate_vsize
|
||||
|
||||
from apps.lottery import bets, rbf
|
||||
from apps.lottery.electrum_chain import Outcome, Verdict
|
||||
from apps.lottery.models import AuditEntry, ChainTransaction, LotteryConfig, Participation, TransactionVersion
|
||||
from tests.django.test_chain_bets import BET, PLM, ScriptedChain, bet, fund, make_round, player, world # noqa: F401
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
||||
|
||||
def pending(world, funds=3 * BET):
|
||||
user, round_ = player(funds=funds), make_round()
|
||||
bet(user, round_)
|
||||
bets.sign_pending()
|
||||
bets.broadcast_pending(ScriptedChain())
|
||||
return user, round_, ChainTransaction.objects.get()
|
||||
|
||||
|
||||
def age(tx, seconds):
|
||||
ChainTransaction.objects.filter(pk=tx.pk).update(last_broadcast_at=timezone.now() - timedelta(seconds=seconds))
|
||||
|
||||
|
||||
# --- when ------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_a_fresh_transaction_is_not_due(world):
|
||||
user, round_, tx = pending(world)
|
||||
assert rbf.due() == []
|
||||
age(tx, 899)
|
||||
assert rbf.due() == []
|
||||
age(tx, 901)
|
||||
assert [t.pk for t in rbf.due()] == [tx.pk]
|
||||
|
||||
|
||||
def test_the_timeout_is_the_configured_one(world):
|
||||
user, round_, tx = pending(world)
|
||||
config = LotteryConfig.get()
|
||||
config.rbf_timeout_seconds = 60
|
||||
config.save()
|
||||
age(tx, 61)
|
||||
assert [t.pk for t in rbf.due()] == [tx.pk]
|
||||
|
||||
|
||||
def test_only_pending_bets_are_due(world):
|
||||
user, round_ = player(), make_round()
|
||||
bet(user, round_)
|
||||
bets.sign_pending() # still building
|
||||
ChainTransaction.objects.update(last_broadcast_at=timezone.now() - timedelta(days=1))
|
||||
assert rbf.due() == []
|
||||
|
||||
|
||||
# --- the replacement ---------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_a_slow_transaction_is_replaced_with_a_higher_fee(world):
|
||||
user, round_, tx = pending(world)
|
||||
old = Transaction.parse(bytes.fromhex(tx.raw_hex))
|
||||
age(tx, 1000)
|
||||
sent = ScriptedChain()
|
||||
new_txid = rbf.bump(tx.pk, sent)
|
||||
tx.refresh_from_db()
|
||||
new = Transaction.parse(bytes.fromhex(tx.raw_hex))
|
||||
assert new_txid == tx.current_txid != old.txid().hex() and sent.sent == [new_txid]
|
||||
assert [(v.txid, v.vout) for v in new.vin] == [(v.txid, v.vout) for v in old.vin] # same coins
|
||||
assert new.vout[0].value == old.vout[0].value and new.vout[0].script_pubkey.data == old.vout[0].script_pubkey.data
|
||||
fee_old = 3 * BET - sum(o.value for o in old.vout) - 0 # inputs - outputs
|
||||
fee_new = 3 * BET - sum(o.value for o in new.vout)
|
||||
assert fee_new - fee_old >= estimate_vsize(1, 2) # BIP125: at least 1 sat/vB more
|
||||
assert (tx.fee_rate_sat_vb, tx.attempt_count) == (2, 2) and tx.last_broadcast_at > timezone.now() - timedelta(seconds=30)
|
||||
assert tx.versions.count() == 2 and Participation.objects.get().bet_txid == new_txid
|
||||
assert AuditEntry.objects.filter(action="rbf_bumped").count() == 1
|
||||
|
||||
|
||||
def test_the_replacement_is_recorded_before_it_is_sent(world):
|
||||
user, round_, tx = pending(world)
|
||||
old_txid = tx.current_txid
|
||||
age(tx, 1000)
|
||||
|
||||
class Crashing:
|
||||
async def broadcast(self, raw, txid):
|
||||
raise RuntimeError("the process died here")
|
||||
|
||||
with pytest.raises(RuntimeError):
|
||||
rbf.bump(tx.pk, Crashing())
|
||||
tx.refresh_from_db()
|
||||
assert tx.current_txid == old_txid and tx.versions.count() == 2 # the candidate is known, the row not moved
|
||||
|
||||
|
||||
def test_an_uncertain_send_still_moves_the_row(world):
|
||||
"""TX-12: the replacement may well be on the network, so the row follows it."""
|
||||
user, round_, tx = pending(world)
|
||||
age(tx, 1000)
|
||||
assert rbf.bump(tx.pk, ScriptedChain(Verdict(Outcome.UNCERTAIN, "timeout"))) is not None
|
||||
tx.refresh_from_db()
|
||||
assert tx.attempt_count == 2 and tx.versions.count() == 2
|
||||
|
||||
|
||||
def test_a_refused_replacement_leaves_the_row_and_is_recorded_once(world):
|
||||
user, round_, tx = pending(world)
|
||||
old_txid = tx.current_txid
|
||||
age(tx, 1000)
|
||||
refusal = Verdict(Outcome.REFUSED, "insufficient fee")
|
||||
assert rbf.bump(tx.pk, ScriptedChain(refusal)) is None
|
||||
assert rbf.bump(tx.pk, ScriptedChain(refusal)) is None
|
||||
tx.refresh_from_db()
|
||||
assert tx.current_txid == old_txid and tx.status == "pending"
|
||||
assert AuditEntry.objects.filter(action="rbf_bump_failed").count() == 1 # TX-24: once per (txid, reason)
|
||||
|
||||
|
||||
def test_a_row_that_changed_meanwhile_is_a_conflict_not_an_overwrite(world, monkeypatch):
|
||||
"""TX-13: confirmed while the replacement was being sent. The row is not overwritten, the candidate stays."""
|
||||
import asyncio
|
||||
|
||||
user, round_, tx = pending(world)
|
||||
age(tx, 1000)
|
||||
real_run = asyncio.run
|
||||
|
||||
def run_then_confirm(coroutine):
|
||||
result = real_run(coroutine) # the send happens...
|
||||
ChainTransaction.objects.filter(pk=tx.pk).update(status="confirmed", confirmed_at=timezone.now()) # ...then this
|
||||
return result
|
||||
|
||||
monkeypatch.setattr(rbf.asyncio, "run", run_then_confirm)
|
||||
assert rbf.bump(tx.pk, ScriptedChain()) is None
|
||||
tx.refresh_from_db()
|
||||
assert tx.status == "confirmed" and tx.attempt_count == 1 and tx.versions.count() == 2
|
||||
|
||||
|
||||
def test_a_transaction_that_is_no_longer_pending_is_skipped(world):
|
||||
user, round_, tx = pending(world)
|
||||
ChainTransaction.objects.filter(pk=tx.pk).update(status="confirmed", confirmed_at=timezone.now())
|
||||
assert rbf.bump(tx.pk, ScriptedChain()) is None and tx.versions.count() == 1
|
||||
|
||||
|
||||
# --- limits ------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_the_maximum_fee_rate_is_never_exceeded(world):
|
||||
user, round_, tx = pending(world)
|
||||
ChainTransaction.objects.filter(pk=tx.pk).update(fee_rate_sat_vb=MAX_FEE_RATE_SAT_VB)
|
||||
assert rbf.bump(tx.pk, ScriptedChain()) is None
|
||||
assert AuditEntry.objects.get(action="rbf_bump_failed").payload["reason"] == "max_fee_rate"
|
||||
|
||||
|
||||
def test_a_change_that_would_fall_below_dust_is_dropped_and_that_is_the_last_bump(world):
|
||||
user, round_, tx = pending(world, funds=BET + 300) # a change of 300 sat
|
||||
age(tx, 1000)
|
||||
assert rbf.bump(tx.pk, ScriptedChain()) is not None
|
||||
tx.refresh_from_db()
|
||||
assert len(Transaction.parse(bytes.fromhex(tx.raw_hex)).vout) == 1 # only the pool output is left
|
||||
age(tx, 1000)
|
||||
assert rbf.bump(tx.pk, ScriptedChain()) is None # nothing left to take the increase from
|
||||
assert AuditEntry.objects.get(action="rbf_bump_failed").payload["reason"] == "no_change_output"
|
||||
|
||||
|
||||
def test_an_error_on_one_row_does_not_stop_the_others(world):
|
||||
user, round_, first = pending(world)
|
||||
other, other_round = player("luigi"), round_
|
||||
bet(other, other_round)
|
||||
bets.sign_pending()
|
||||
bets.broadcast_pending(ScriptedChain())
|
||||
for t in ChainTransaction.objects.all():
|
||||
age(t, 1000)
|
||||
ChainTransaction.objects.filter(pk=first.pk).update(fee_rate_sat_vb=MAX_FEE_RATE_SAT_VB) # this one cannot
|
||||
assert rbf.bump_due(ScriptedChain()) == 1
|
||||
|
||||
|
||||
def test_without_the_signing_key_nothing_is_bumped(world):
|
||||
user, round_, tx = pending(world)
|
||||
age(tx, 1000)
|
||||
world.PROCESS_ROLE = "web"
|
||||
assert rbf.bump(tx.pk, ScriptedChain()) is None
|
||||
tx.refresh_from_db()
|
||||
assert tx.versions.count() == 1 and tx.attempt_count == 1
|
||||
Reference in new issue
Block a user