Corroborate deposit credits, not just external spends (B-59)

A candidate external spend has needed a quorum since B-29, but `value` and
`height` for a *credit* came from the single active connection and went straight
into utxo_events. One hostile or broken server could therefore inflate a user's
displayed balance with outpoints that don't exist. It never spends anyone else's
coins — a bet or withdrawal built on a phantom UTXO is refused at broadcast and
rolled back — but it wedges the balance display and burns build attempts, and on
a custodial platform a balance that isn't real is a support incident either way.
Balances move in both directions; both directions now need the same quorum.

corroborate_utxo_credit asks the other configured servers whether they report
the same outpoint, for the same amount, confirmed. The height itself isn't
compared: a server still catching up reports height 0 and simply doesn't agree,
which is the same answer, while two honest servers can't disagree on the height
of a genuinely confirmed outpoint.

refresh_user gains the phase that shape already implied: find_new_credit_
candidates (new, confirmed, not already held) inside the first session,
corroboration outside any session, then credit_confirmed_utxos over what
survived. Only new outpoints are corroborated — re-checking what we already hold
would open a connection to every other server on every refresh for an answer
that can no longer change anything.

A failed corroboration delays a credit, it never loses one: the next scripthash
notification or DepositReconciler sweep (300s) re-offers the same outpoint, and
the withholding is logged.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
davideandClaude Opus 5 committed 2026-08-03 22:40:15 +02:00
1 parent 0aac73e557
commit 6246b13247
6 files changed
+223 -24

No files matched your search

+23
View File
@@ -184,3 +184,26 @@ async def test_reinstate_reappeared_utxos_ignores_unmarked_rows(session_factory,
async with session_factory() as session:
reinstated = await reinstate_reappeared_utxos(session, user_id, entries)
assert reinstated == 0
async def test_find_new_credit_candidates_skips_unconfirmed_and_already_known(session_factory, user_id): # B-59
"""What the caller has to corroborate before crediting: only entries that would
actually write something. Re-corroborating what we already hold would open a
connection to every other server on every refresh, for an answer that can no
longer change anything."""
from app.deposits.service import find_new_credit_candidates
async with session_factory() as session:
await credit_confirmed_utxos(
session, user_id, [{"tx_hash": "aa" * 32, "tx_pos": 0, "height": 100, "value": 1_000}]
)
entries = [
{"tx_hash": "aa" * 32, "tx_pos": 0, "height": 100, "value": 1_000}, # already credited
{"tx_hash": "bb" * 32, "tx_pos": 0, "height": 0, "value": 2_000}, # still in the mempool
{"tx_hash": "cc" * 32, "tx_pos": 1, "height": 101, "value": 3_000}, # genuinely new
]
async with session_factory() as session:
candidates = await find_new_credit_candidates(session, user_id, entries)
assert [(c["tx_hash"], c["tx_pos"]) for c in candidates] == [("cc" * 32, 1)]
+100 -2
View File
@@ -446,8 +446,14 @@ _UNRELATED_ENTRY = [{"tx_hash": "99" * 32, "tx_pos": 0, "height": 100, "value":
async def test_refresh_user_marks_a_utxo_spent_once_others_corroborate_it(session_factory):
user_id = await _seed_funded_user(session_factory, username="bob", address="plm1q0xcqpzrky6eff2g52qdye53xkk9jxkvraxkkwd")
# The others agree the tracked UTXO is gone, and agree about the unrelated
# entry — which B-59 now requires before that one may be credited.
others_factory = await _listunspent_client_factory(
{"first.example": [], "second.example": [], "third.example": []}
{
"first.example": _UNRELATED_ENTRY,
"second.example": _UNRELATED_ENTRY,
"third.example": _UNRELATED_ENTRY,
}
)
listener = ElectrumListener(others_factory, session_factory, _ENDPOINTS)
listener.client = _ActiveClient(_UNRELATED_ENTRY) # our own connection no longer sees the UTXO either
@@ -471,7 +477,7 @@ async def test_refresh_user_does_not_mark_when_corroboration_fails(session_facto
bad reply."""
user_id = await _seed_funded_user(session_factory, username="carol", address="plm1qtest2")
still_there = [{"tx_hash": "dd" * 32, "tx_pos": 0}]
still_there = [{"tx_hash": "dd" * 32, "tx_pos": 0}] + _UNRELATED_ENTRY
others_factory = await _listunspent_client_factory(
{"first.example": [], "second.example": still_there, "third.example": still_there}
)
@@ -630,3 +636,95 @@ async def test_run_once_keeps_consuming_headers_while_resubscribing(session_fact
finally:
await client.close()
await run_once_task
# --- B-59: a credit must clear the same quorum a debit already had to (B-29) -----
_PHANTOM = [{"tx_hash": "77" * 32, "tx_pos": 0, "height": 200, "value": 5_000_000}]
async def test_corroborate_utxo_credit_true_when_others_report_the_same_outpoint(session_factory):
factory = await _listunspent_client_factory(
{"first.example": _PHANTOM, "second.example": _PHANTOM, "third.example": _PHANTOM}
)
listener = ElectrumListener(factory, session_factory, _ENDPOINTS)
assert await listener.corroborate_utxo_credit("scripthash", "77" * 32, 0, 5_000_000) is True
async def test_corroborate_utxo_credit_false_when_the_amount_differs(session_factory):
"""Agreement is on the amount too, not just on the outpoint existing — the
inflated number is the whole point of the attack."""
smaller = [{"tx_hash": "77" * 32, "tx_pos": 0, "height": 200, "value": 1_000}]
factory = await _listunspent_client_factory(
{"first.example": smaller, "second.example": smaller, "third.example": _PHANTOM}
)
listener = ElectrumListener(factory, session_factory, _ENDPOINTS)
assert await listener.corroborate_utxo_credit("scripthash", "77" * 32, 0, 5_000_000) is False
async def test_corroborate_utxo_credit_false_when_others_call_it_unconfirmed(session_factory):
"""height <= 0 is Electrum's "still in the mempool" — a server that hasn't seen
the block yet doesn't corroborate a 1-conf credit."""
mempool = [{"tx_hash": "77" * 32, "tx_pos": 0, "height": 0, "value": 5_000_000}]
factory = await _listunspent_client_factory(
{"first.example": mempool, "second.example": mempool, "third.example": _PHANTOM}
)
listener = ElectrumListener(factory, session_factory, _ENDPOINTS)
assert await listener.corroborate_utxo_credit("scripthash", "77" * 32, 0, 5_000_000) is False
async def test_corroborate_utxo_credit_false_when_nobody_responds(session_factory):
factory = await _listunspent_client_factory(
{"first.example": None, "second.example": None, "third.example": ConnectionRefusedError("down")}
)
listener = ElectrumListener(factory, session_factory, _ENDPOINTS)
assert await listener.corroborate_utxo_credit("scripthash", "77" * 32, 0, 5_000_000) is False
async def test_refresh_user_does_not_credit_a_utxo_only_our_own_server_reports(session_factory):
"""The mirror of B-29's headline case: before this, one hostile or broken
server could inflate a user's displayed balance with an outpoint that doesn't
exist. The credit is withheld, not lost — the next refresh retries it."""
user_id = await _seed_funded_user(session_factory, username="dave", address="plm1qtest3")
others_factory = await _listunspent_client_factory(
{"first.example": [], "second.example": [], "third.example": []}
)
listener = ElectrumListener(others_factory, session_factory, _ENDPOINTS)
# Our own connection reports the tracked UTXO plus a phantom one nobody else has.
listener.client = _ActiveClient(
[{"tx_hash": "dd" * 32, "tx_pos": 0, "height": 100, "value": 20_000_000}] + _PHANTOM
)
await listener.refresh_user(user_id, "scripthash")
async with session_factory() as session:
assert (
await session.scalars(select(UtxoEvent).where(UtxoEvent.txid == "77" * 32))
).all() == []
user = await session.get(User, user_id)
assert user.cached_balance_sats == 20_000_000 # unchanged, not inflated
async def test_refresh_user_credits_once_the_others_corroborate(session_factory):
user_id = await _seed_funded_user(session_factory, username="erin", address="plm1qtest4")
# first.example is the active endpoint, which _corroborate_majority never asks —
# the quorum here is second + third.
others_factory = await _listunspent_client_factory(
{"first.example": [], "second.example": _PHANTOM, "third.example": _PHANTOM}
)
listener = ElectrumListener(others_factory, session_factory, _ENDPOINTS)
listener.client = _ActiveClient(
[{"tx_hash": "dd" * 32, "tx_pos": 0, "height": 100, "value": 20_000_000}] + _PHANTOM
)
await listener.refresh_user(user_id, "scripthash")
async with session_factory() as session:
user = await session.get(User, user_id)
assert user.cached_balance_sats == 25_000_000