Files
plm-lottery/migrations/versions/b7d2e41c9a05_add_failed_at_and_paused_reason.py
T
davideandClaude Sonnet 5.5 a4b160e240 Abandon a transaction only on proof, and keep watching failed rows (B-78, TX-15, TX-17, TX-23, TX-26, ELC-28, DAT-33, DAT-36, DAT-69, DAT-72, AUD-11, AUD-12, AUD-31, AUD-39, API-32)
reconcile_once abandoned a row when the *active* server's history lacked its txid:
after 120s for `building` (which a restart onto a different server makes likely),
after 6h for `pending`, and all at once after a forward clock jump. It re-checked the
status but not the txid, so it raced the bumper, and a `failed` row was never looked at
again.

- The reconciler asks every server (ElectrumListener.get_histories, active + others) for
  the history of the row's address and looks for every txid of the row. Present on any
  server -> promoted (adopting the version found). Absent is only "no server of the
  quorum knows it" and needs the active server plus the quorum minimum of the others to
  have answered; even then nothing is released without proof:
  (a) an input spent in a block by a transaction that is none of the row's versions,
      agreed by a strict majority of a quorum of the other servers (confirmed_spender), or
  (b) a rebroadcast of the latest raw tx to every server (rebroadcast_everywhere)
      refused by all of them, explicitly, for missing/spent inputs.
  Accepted or "already known" by any server means alive; a fee refusal is not proof
  (`building` -> `pending` for the bumper, `pending` untouched); too few answers or
  anything else leaves the row alone. failure_reason names the proof.
- The write re-reads status *and* current_txid under begin_immediate (TX-26).
- pending_transactions.failed_at (set by _abandon and _release_failed_withdrawal) starts
  a 14-day watch: a failed row that a majority of the other servers (at least the quorum
  minimum) list in a block becomes `confirmed`, writes pending_tx_resurrected once and
  pauses the lottery with paused_reason `resurrected_tx`; linked rows are not corrected.
  The confirmation is by quorum of histories for now; merkle proofs come with B-83.
- round_config.paused_reason (migration b7d2e41c9a05, with the failed_at one): `operator`
  from POST /admin/pause (which accepts an optional `note` and never overwrites an
  automatic reason), cleared by resume, which logs the reason it lifted; exposed on
  /admin/config and GET /rounds/current. rounds/pause.py:pause_automatically is the
  system's way in.
- PendingTransactionReconciler takes the listener as `network`; with none it can promote
  but never abandon.

Tests changed because the spec changed: the three reconciler tests that expect an
abandonment (and the one in test_tx_versions) now supply a network that proves the
transaction is gone.

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

48 lines
1.8 KiB
Python

"""add failed_at to pending_transactions and paused_reason to round_config
Revision ID: b7d2e41c9a05
Revises: a3f5c2d9b7e1
Create Date: 2026-10-14 15:48:12.003915
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'b7d2e41c9a05'
down_revision: Union[str, Sequence[str], None] = 'a3f5c2d9b7e1'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
_ACTIVE = "'open', 'closing', 'drawing', 'paying_out'"
def upgrade() -> None:
"""Upgrade schema.
B-78: `failed_at` (DAT-36, DAT-72) starts the 14-day watch over a failed row; rows
that are already failed get their last update as the best available estimate.
`paused_reason` (DAT-69) says why the lottery is paused; an instance that is already
paused was paused by its operator.
Both are plain add_column calls — never batch_alter_table, which on SQLite rebuilds
the table and silently drops the expression indexes (B-97, DAT-56).
"""
op.add_column("pending_transactions", sa.Column("failed_at", sa.DateTime(), nullable=True))
op.execute("UPDATE pending_transactions SET failed_at = updated_at WHERE status = 'failed'")
op.add_column("round_config", sa.Column("paused_reason", sa.String(length=16), nullable=True))
op.execute("UPDATE round_config SET paused_reason = 'operator' WHERE paused = 1")
def downgrade() -> None:
"""Downgrade schema."""
# drop_column on SQLite is a table rebuild: recreate the expression indexes (B-97).
op.drop_column("round_config", "paused_reason")
op.drop_column("pending_transactions", "failed_at")
op.execute("DROP INDEX IF EXISTS ix_rounds_single_active")
op.execute(f"CREATE UNIQUE INDEX ix_rounds_single_active ON rounds ((1)) WHERE status IN ({_ACTIVE})")