Files
plm-lottery/migrations/versions/a3f5c2d9b7e1_add_pending_transaction_txids.py
T
davideandClaude Sonnet 5.5 f936d6660c Track every version of a bumped transaction and write the RBF replacement before broadcasting it (B-77, TX-12, TX-13, TX-19, TX-21, DAT-13, DAT-14, DAT-34, DAT-44, DAT-45, DAT-70, DAT-71)
The poller and the reconciler matched current_txid only. If an earlier version of
a bumped transaction confirmed it was never seen, and after 6h the row was abandoned:
a confirmed bet's participant deleted, a sent withdrawal marked failed, a paid payout
rebuilt. bump_fee also broadcast in phase 2 and persisted in phase 3 unconditionally,
so a crash or a `database is locked` in between left a transaction on the network that
the DB did not know, and an abandon or a confirmation in the meantime was overwritten.

- New table pending_transaction_txids (migration a3f5c2d9b7e1; txid unique, index on
  the owning row; existing rows backfilled with current_txid + raw and the txid their
  last bump replaced, raw_hex empty). Written with the row by place_bet,
  request_withdrawal and _trigger_payout; removed with a bet row that a verified
  refusal deletes (DAT-38).
- bump_fee in four phases: read, build and sign, commit the replacement as a candidate
  version, broadcast (verdict of B-76, fanned out when accepted; an explicit refusal
  raises RbfError and leaves the row), then switch the row over with
  `UPDATE ... WHERE status='pending' AND current_txid=<replaced>`. Zero rows matched is
  a logged conflict: nothing is overwritten and the candidate stays recorded. An
  uncertain broadcast adopts the replacement. A retry rebuilds the same candidate
  without tripping the unique txid.
- poll_once and reconcile_once look for every txid of a row. When the version found is
  not the current one, broadcast.adopt_version makes it current (raw tx included) and
  retargets spent_txid, bet_txid, withdrawals.txid and payout_txid as a bump does,
  before the confirmation handler runs. _release_inputs frees inputs claimed by any
  version of the row.
- RbfBumper takes the listener as `network`.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 14:46:43 +02:00

69 lines
2.5 KiB
Python

"""add pending_transaction_txids
Revision ID: a3f5c2d9b7e1
Revises: 477bddae1a84
Create Date: 2026-10-14 15:02:41.118204
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'a3f5c2d9b7e1'
down_revision: Union[str, Sequence[str], None] = '477bddae1a84'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema.
B-77 (DAT-70): every version a pending transaction has ever had. A new table, so
nothing here rebuilds an existing one and the expression indexes (B-97) are not at
risk. Rows that already exist are backfilled with what they still remember: their
`current_txid` with its raw transaction and, when it differs, the `replaced_by_txid`
of their last bump with an empty `raw_hex` (that raw transaction was overwritten).
"""
op.create_table(
"pending_transaction_txids",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("pending_transaction_id", sa.Integer(), nullable=False),
sa.Column("txid", sa.String(length=64), nullable=False),
sa.Column("raw_hex", sa.Text(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(["pending_transaction_id"], ["pending_transactions.id"]),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("txid"),
)
op.create_index(
op.f("ix_pending_transaction_txids_pending_transaction_id"),
"pending_transaction_txids",
["pending_transaction_id"],
unique=False,
)
op.execute(
"""
INSERT INTO pending_transaction_txids (pending_transaction_id, txid, raw_hex, created_at)
SELECT id, current_txid, raw_tx_hex, broadcast_at FROM pending_transactions
"""
)
op.execute(
"""
INSERT INTO pending_transaction_txids (pending_transaction_id, txid, raw_hex, created_at)
SELECT id, replaced_by_txid, '', broadcast_at FROM pending_transactions
WHERE replaced_by_txid IS NOT NULL
AND replaced_by_txid != current_txid
AND replaced_by_txid NOT IN (SELECT txid FROM pending_transaction_txids)
"""
)
def downgrade() -> None:
"""Downgrade schema."""
op.drop_index(op.f("ix_pending_transaction_txids_pending_transaction_id"), table_name="pending_transaction_txids")
op.drop_table("pending_transaction_txids")