Skip to content

3.x.x: check legacy carry-forward chains per payout program, as 2.x keeps them, not per case-sensitive label (migration 025) (#708) - #710

Merged
RobertClarke merged 1 commit into
3.x.xfrom
RobertClarke/carry-integrity-by-program
Oct 6, 2026
Merged

RobertClarke merged 1 commit into
3.x.xfrom
RobertClarke/carry-integrity-by-program

Conversation

@RobertClarke

@RobertClarke RobertClarke commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Fixes #708 (close by hand: base is not the default branch)

Problem

qbit_carry_forward_integrity_report() reports 5,506 mismatches on union's restored ledger (current_drift_count 0, 2,753,849 active carry rows). All of them are in one payout program, which carries two spellings of one bech32 address: uppercase on 5,527 rows (heights 11341 to 118151) and lowercase on 116 rows (11470 to 12772).

The money is right. 2.x keeps a carry balance per payout program: qbit_current_carry_forward_balances() groups by p2mr_program, and the payout policy reads the prior balance by program. 3.x's native writer reads the same function. The legacy sequential rule of qbit_carry_forward_integrity_mismatches() (001, kept by 011) instead sums each (miner_id, payout_order_key, p2mr_program) apart, so each label looks like a chain missing the other label's balance. Run read-only over the restored copy, the same rule partitioned by p2mr_program alone finds 0 mismatches over all 2,753,849 active rows.

Refused while mismatch_count > 0, and unblocked by this change:

  • self-check (tools.rs:1866-1880), so every pre-admission and cutover gate;
  • fatal-state clear (ledger/fatal_state.rs:253-261);
  • scripts/prism-recovery-evidence.py, so the C1/C3 evidence cmp.

/audit/carry-forward-integrity (api.rs:981) only reports; it now reports the corrected count.

Change

Migration 025 (025_carry_forward_integrity_by_program.sql) replaces the validator with 011's body. Against 011's definition, the only change is the legacy rule's window:

+    -- A program's chain runs across every label it was paid under (#708),
+    -- as qbit_current_carry_forward_balances() sums it.
     sequential AS (
         SELECT
             active.*,
             COALESCE(
                 SUM(active.gross_amount_sats::numeric - active.onchain_amount_sats::numeric)
                 OVER (
-                    PARTITION BY active.miner_id, active.payout_order_key, active.p2mr_program
+                    PARTITION BY active.p2mr_program
                     ORDER BY active.block_height ASC, active.carry_forward_seq ASC
                     ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
                 ),

The marked (as-issued) rule, the report, the per-label summary and qbit_carry_forward_current_drift() are unchanged. Labels stay as written, since the signed as-issued manifests and the summary name them.

Upgrade. 025 changes no stored row, so it is a plain in-transaction migration: no capability and no shutdown proof. An earlier native binary, rc.1 included, accepts the unknown migration with a warning (require_schema_version, #321) and keeps starting; it reads the corrected report. Because 011 defines the validator that 025 replaces, migrate runs 025 again whenever it runs 011. So a record missing 11 but holding 25 cannot bring the label rule back.

Recovery summarizer. scripts/prism-recovery-evidence.py re-checks every unmarked active carry row against its program's running chain, from the exported rows.

  • A legacy finding in the database's report is cleared only when 2.x's per-label rule makes it, on a row whose program chain holds.
  • Any other finding fails, as do a real chain break, an as-issued finding, current drift, an incomplete or malformed mismatch list, a malformed amount, and out-of-order records.
  • The summary keeps the database's report minus the cleared findings. A clean ledger therefore summarizes byte for byte as before, and a 2.x source summarizes the same as its migrated copy.
  • A note: on stderr names the findings it cleared.

Fixture. recovery::seed_legacy gave three miners one program, each chain starting from zero, which 2.x cannot write. Each miner now has its own program, and the pinned carry head was recomputed independently, from the rows written out by hand.

Plumbing:

  • REQUIRED_SCHEMA_VERSIONS, NATIVE_MIGRATIONS and the migrate_schema block;
  • the version pins;
  • the evidence script's required_versions;
  • ledger_2x::undo_006;
  • the gated-test list;
  • prism-ledger-ops.md and prism-rust-migration.md.

Tests

The new PostgreSQL tests use union's shape (tests/support/case_labels.rs): one program under two case labels, interleaved over seven blocks, paid on chain under both.

  • offer_lifecycle::integrity_validator_follows_a_legacy_chain_per_payout_program_across_case_labels:
    • the shape is clean under 025;
    • a ledger at 24 running 011's rule reports 5 of the 7 rows, and migrate clears them with every balance and per-label summary row unchanged;
    • a break under either label is a finding on that row alone;
    • a changed amount under one label moves the chain under both.
  • offer_lifecycle::migration_025_runs_again_after_011_runs: a v2.0.2 fix: bound candidate replay and audit finalization #258 record without 11 but with 25 keeps the per-program rule after migrate runs 011 again.
  • migration_rollback::a_2x_program_paid_under_case_labels_migrates_clean_and_summarizes_as_its_source, on a frozen 2.x source:
    • 2.x's rule reports 5 and the migrated validator 0;
    • recovery::evidence of the source and of the migrated copy are equal;
    • qbit_current_carry_forward_balances() is unchanged;
    • a break under either label fails both the validator and the summary.
  • ledger_postgres::fatal_state::self_check_and_clear_accept_a_programs_legacy_chain_across_case_labels: self-check passes its integrity gate and fatal-state clear succeeds; a break under either label stops both.
  • Each test was checked against the bug. With 025 reverted to the label rule, the three 3.x.x: the legacy carry-forward integrity rule partitions by case-sensitive label, so self-check refuses union's ledger (5,506 false mismatches; money is per payout program) #708 tests fail; self-check reports mismatch_count 5, which is the false alarm. With the 011-rerun guard removed, the v2.0.2 fix: bound candidate replay and audit finalization #258 test fails.
  • tests/test_prism_recovery_evidence.py (16 tests): the per-program chain, real breaks under any label, unexplained and as-issued findings, marked rows, the clean report kept as written, and malformed exports.

Local run, on a private PostgreSQL 16:

  • Full gated suite (--lib and every tests/*.rs but live_regtest, --test-threads=6): 1,930 passed and 3 failed. The 3 failures are environmental, and each passes on rerun:
    • Two compact_runtime_observer tests hit canceling statement due to lock timeout under the suite's load. The binary passes 25/25 alone, with 1 thread and with 6.
    • public_database_config::direct_public_binary_authenticates_only_with_effective_reader_credentials failed because my cluster had trust auth, so a login with no password succeeded. It passes 5/5 once pg_hba requires scram, as CI's PostgreSQL does.
  • cargo clippy --locked --workspace --all-targets -- -D warnings: clean.
  • cargo fmt --all --check: clean.
  • python3 -m unittest tests.test_prism_recovery_evidence: 16/16 passed.

Read-only check for the restored copy

This is 025's legacy rule as a standalone query. On a 2.x.x copy every block is unmarked, so every active row is legacy. Expect legacy_mismatch_count 0 and checked_active_rows 2753849:

BEGIN READ ONLY;
WITH active AS (
    SELECT ledger.*
    FROM qbit_payout_carry_forward ledger
    JOIN qbit_pool_blocks block ON block.block_hash = ledger.block_hash
    WHERE ledger.maturity_state <> 'reversed'
      AND block.chain_state = 'confirmed'
      AND block.maturity_state <> 'reversed'
),
sequential AS (
    SELECT active.*,
           COALESCE(
               SUM(active.gross_amount_sats::numeric - active.onchain_amount_sats::numeric)
               OVER (
                   PARTITION BY active.p2mr_program
                   ORDER BY active.block_height ASC, active.carry_forward_seq ASC
                   ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
               ),
               0::numeric
           ) AS running_prior_balance_sats
    FROM active
)
SELECT count(*) AS legacy_mismatch_count,
       (SELECT count(*) FROM active) AS checked_active_rows
FROM sequential
WHERE prior_balance_sats <> running_prior_balance_sats
   OR candidate_balance_sats <> running_prior_balance_sats + gross_amount_sats::numeric
   OR carry_forward_balance_sats
      <> running_prior_balance_sats + gross_amount_sats::numeric - onchain_amount_sats::numeric;
ROLLBACK;

Do not merge: 3.x.x waits for the v3.0.0-rc.1 tag and the coordinator's go after reading the SQL diff.

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

…eeps them, not per case-sensitive label (migration 025) (#708)

2.x keeps a miner's carry balance per payout program:
qbit_current_carry_forward_balances() sums a program's per-label rows,
and the payout policy reads the prior balance by program. Stratum keeps
an address as typed and bech32 is case-insensitive, so one program can
be paid under an uppercase and a lowercase label. On union one program
carries both: 5,527 uppercase rows (heights 11341 to 118151) and 116
lowercase rows (11470 to 12772), one running balance between them.

The legacy sequential rule of qbit_carry_forward_integrity_mismatches(),
from 001 and kept by 011, sums each (miner_id, payout_order_key,
p2mr_program) apart. On the restored union copy it reports 5,506
mismatches, all in that program. Summed per p2mr_program it reports 0
over all 2,753,849 active rows, with no current drift. self-check and
fatal-state clear refuse any mismatch, and the recovery summarizer
refused the export, so the false alarm blocks the cutover.

Migration 025 replaces the validator with 011's body and changes only
the legacy rule's window, to PARTITION BY p2mr_program. The marked
(as-issued) rule, the report, the per-label summary and its drift check
are unchanged, and labels stay as written. 025 changes no stored row and
needs no capability or shutdown proof: it is applied in the migration
transaction, and an earlier native binary accepts the unknown migration
with a warning and reads the corrected report. Because 011 defines the
validator 025 replaces, migrate runs 025 again whenever it runs 011, so
a record missing 11 but holding 25 cannot bring the label rule back.

scripts/prism-recovery-evidence.py re-checks every unmarked active carry
row against its program's running chain, from the exported rows. A
legacy finding in the database's report is cleared only when 2.x's
per-label rule makes it on a row whose program chain holds; any other
finding, a real chain break, an as-issued finding, current drift, an
incomplete or malformed mismatch list, a malformed amount or
out-of-order records fail. The summary keeps the database's report less
the cleared findings, so a clean ledger summarizes byte for byte as
before, and a 2.x source and its migrated copy summarize alike. A note
on stderr names the findings it cleared.

Tests:
- offer_lifecycle: union's shape (one program, two case labels,
  interleaved, paid on chain under both) is clean under 025. A ledger
  at 24 with 011's rule reports 5 of its 7 rows, and migrate clears
  them with every balance and summary row unchanged. A break under
  either label is a finding on that row alone, and a changed amount
  moves the chain under both labels. A #258 record without 11 but with
  25 keeps the per-program rule after migrate runs 011 again.
- migration_rollback: the same shape on a frozen 2.x source. 2.x's
  rule reports 5 and the migrated validator 0, the source's and the
  migrated copy's recovery evidence are equal, balances are unchanged,
  and a break under either label fails the validator and the summary.
- ledger_postgres::fatal_state: self-check passes its integrity gate
  and fatal-state clear succeeds on the shape; a break under either
  label stops both.
- With 025 reverted to the label rule, the three #708 tests fail; with
  the 011 rerun guard removed, the #258 test fails.
- tests/test_prism_recovery_evidence.py: the per-program chain, real
  breaks under any label, unexplained and as-issued findings, marked
  rows, the clean report kept as written, and malformed exports.

recovery::seed_legacy gave three miners one program, each starting from
zero, which 2.x cannot write. Each now has its own program, and the
pinned carry head is recomputed independently. Migration pins, the
evidence script's required versions, the undo fixtures, the gated-test
list and the ledger-ops and migration docs include 025.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-06T07:21:32.253559Z 7937cce PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@RobertClarke
RobertClarke merged commit bac4432 into 3.x.x Oct 6, 2026
40 checks passed
@RobertClarke
RobertClarke deleted the RobertClarke/carry-integrity-by-program branch October 6, 2026 07:42
RobertClarke added a commit that referenced this pull request Oct 6, 2026
…rade's sidecar and FETCH_COUNT steps, and migrate-before-start

From the review of the rc.2 notes:
- Known issues now list what opened since rc.1 and isn't fixed here:
  #716 (a valid share can lose its credit on "commit gate closed" at a
  tip change; seen on the pair at rc.1), #711 (the order lock across
  about nine round trips), #712 (85-minute evidence export), #713
  (verify-ca checks the host name) and #704 (a drill flake).
- Upgrading from 2.0.x adds running the sidecar tool before
  import-audits and exporting the evidence with FETCH_COUNT. Upgrading
  from rc.1 says to run migrate first, because an rc.2 frontend refuses a
  database without 025.
- #708's bullet says #710 also changes the evidence summarizer, which must
  be this candidate's on both sides of a cutover, and puts #710 next to
  the fix, not the refusal.
- #600's fix (#627) was already in rc.1. The carried-over soak passed
  every gate, and its job failed only on the tripwire #714 removed.
  faults-long-real-node is not re-run here.
- docs/prism-e2e-load-ci.md's example tag reads v3.0.0-rc.1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RobertClarke added a commit that referenced this pull request Oct 6, 2026
Set VERSION and [workspace.package].version to 3.0.0-rc.2: rc.1 plus
migration 025 (#708, #710) and the legacy-audit-body sidecar tool (#715).
Cargo.lock and the fuzz crate's lock move only their workspace-member
entries.

doc/release-notes-3.0.0-rc.2.md covers:
- the changes since rc.1: 025's per-program carry-forward rule, the
  canonical sidecars for escaped legacy range digests, FETCH_COUNT for
  the recovery evidence (#706), and the harness and CI fixes;
- upgrading from 2.0.x and from rc.1;
- the lean verification, since the only runtime change is 025;
- the known issues.

The development-line notes name 3.0.0-rc.2 as the tree's identity, and
they and the 3.0.0 notes link both candidates' notes. U3 wrote the
change lines.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RobertClarke added a commit that referenced this pull request Oct 6, 2026
…rade's sidecar and FETCH_COUNT steps, and migrate-before-start

From the review of the rc.2 notes:
- Known issues now list what opened since rc.1 and isn't fixed here:
  #716 (a valid share can lose its credit on "commit gate closed" at a
  tip change; seen on the pair at rc.1), #711 (the order lock across
  about nine round trips), #712 (85-minute evidence export), #713
  (verify-ca checks the host name) and #704 (a drill flake).
- Upgrading from 2.0.x adds running the sidecar tool before
  import-audits and exporting the evidence with FETCH_COUNT. Upgrading
  from rc.1 says to run migrate first, because an rc.2 frontend refuses a
  database without 025.
- #708's bullet says #710 also changes the evidence summarizer, which must
  be this candidate's on both sides of a cutover, and puts #710 next to
  the fix, not the refusal.
- #600's fix (#627) was already in rc.1. The carried-over soak passed
  every gate, and its job failed only on the tripwire #714 removed.
  faults-long-real-node is not re-run here.
- docs/prism-e2e-load-ci.md's example tag reads v3.0.0-rc.1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RobertClarke added a commit that referenced this pull request Oct 6, 2026
Set VERSION and [workspace.package].version to 3.0.0-rc.2: rc.1 plus
migration 025 (#708, #710), #718's commit-gate answer for #716, and the
legacy-audit-body sidecar tool (#715). Both Cargo.lock files move only
their workspace-member entries.

doc/release-notes-3.0.0-rc.2.md covers:
- the changes since rc.1 (U3's lines, and #716 fix A in the coordinator's
  words);
- upgrading from 2.0.x (the sidecar tool and FETCH_COUNT) and from rc.1
  (migrate before any rc.2 frontend starts);
- the lean verification, since the runtime changes are 025 and #718;
- the known issues, including #711, #712, #713 and #704.

The development-line notes name 3.0.0-rc.2 as the tree's identity, they
and the 3.0.0 notes link both candidates' notes, and a doc example's tag
reads v3.0.0-rc.1.

L3 run 37444397320 (l3-full) measured this tree. 32 of 33 runs passed.
throughput-400k-window-2fe-async-3-blocks-r2 refused one valid share at
the commit gate (#716's open part, answered backend-rpc-unavailable), and
the coordinator carried that verdict as a named exception.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant