Skip to content

3.x.x: follow weighted multi-address shares to spent outputs on a live regtest node (#521 scenario 7) - #524

Merged
djh58 merged 1 commit into
3.x.xfrom
djh58/prism-b521-live-weighted-recipients
Sep 28, 2026
Merged

djh58 merged 1 commit into
3.x.xfrom
djh58/prism-b521-live-weighted-recipients

Conversation

@djh58

@djh58 djh58 commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Refs #521 (scenario 7). Refs #474 (its many-recipient share-to-spend item only; the 1,001-recipient chunk boundary, direct-only mode, async failover and lost-reply slices stay with #474).

What this adds

A gated live_regtest case, weighted_recipients_tests::real_weighted_recipients_pay_exact_pplns_outputs_through_fanout_spend, in a new support module tests/support/live_weighted_recipients.rs. It follows every sat from the accepted share to the spent output, with a real qbitd, both PRISM servers and PostgreSQL:

  • 40 payout addresses with seeded, skewed weights: a whale at about 80%, a second at 7%, a Zipf tail, one payee held just under the payout floor, and four near-zero payees that submit a single minimum-difficulty share.
  • 59 Stratum sessions. Every share is a real mining.submit, and each worker asks for its difficulty in its password. Worker difficulty spans about 950x (about three orders of magnitude, as on mainnet). 16 addresses mine on both servers, and 11 mine at more than one difficulty.
  • Four found blocks. The test chooses share-only proofs for ordinary shares and one block-bearing proof per round, so it decides when each block is found and which worker finds it.

It asserts the following. Every expected value is recomputed in the test from the ledger's accepted shares and the chain, never from the server's bundles or summaries.

  • Attribution. The ledger holds exactly the shares the servers acknowledged. Each share is credited to the submitting worker's address, order key and P2MR program, at that worker's exact weight, and written by the server the worker submitted to.
  • PPLNS window and payout policy, recomputed per block.
    • The window runs newest first from the block share's snapshot anchor, covers eight blocks of weight, and counts the oldest share only in part. Three of the four windows end inside a share.
    • The gross split uses largest-remainder rounding.
    • The payout floor, carry-forward, pool-fee dust sweep and direct/fanout partition (amount priority into four direct slots) follow the policy, as does the fanout fee split.
  • Chain and ledger match the model.
    • Each coinbase pays the direct recipients their exact amounts, with one covenant output carrying the chunk's gross.
    • Every qbit_payout_carry_forward row matches (gross, prior, candidate, on-chain, fee, carry, action), and the final carried balances match.
  • Share-to-spend.
    • After maturity, each block's fanout spends its covenant outpoint and pays each fanout recipient its exact net amount.
    • Every payee address received exactly what the model paid it, the payees' wallet holds all of it as spendable, and it spends one direct and one fanout output of every block in a transaction that confirms.
    • qbit_carry_forward_integrity_report() is clean.
  • Non-degeneracy. The seeded scenario must keep a wide difficulty spread, cut windows, dust carried in every block, and a carried balance that is later paid, so a change that flattens it fails.

nightly_mainnet_floor_weighted_recipients_share_to_spend is the same case at the #521 mainnet floor: 130 recipients, one whale near 85%, 202 sessions and six blocks. It is #[ignore] and opt-in, and is not in the gate list until a nightly job selects it. It passed locally in 99 s and 142 s, the second run on a busier host.

Why the chain is made harder first

On regtest's proof-of-work limit, a share can be at most twice as easy as a block, so a spread of worker difficulty cannot exist there. The case therefore restarts the fixture's node with -legacyretarget and mines about 8,500 blocks quickly under a mock clock pinned to the tip. Each epoch's 4x clamp raises the target 1,024x in about ten seconds. The mock clock only runs ahead of the wall clock, and the server accepts a future template time.

Server settings the case uses

  • Fixed per-worker difficulty: PRISM_STRATUM_VARDIFF=0.
  • Payout floor: 1,000,000 bits, set with PRISM_PAYOUT_MIN_OUTPUT_BITS so dust exists at regtest scale.
  • Direct outputs: the default direct floor and PRISM_MAX_DIRECT_COINBASE_OUTPUTS=4, so direct-eligible overflow reaches the fanout by amount priority.
  • Pool fee: 200 bps. Mainnet runs with a pool fee. Without one, any sub-floor balance leaves the selected balances short of the coinbase and no work can be built (see repro_prism_stall).

Shared fixture change

The shared fixture change is additive. Fixture gains server_env, which is applied after every other server setting and is empty unless a case sets it; this case uses it for the settings above. live_regtest.rs also gains the mod line for the new module. No other test is affected.

Mutants the case catches

Each mutant was applied to the server, rebuilt, and run against the new case:

Mutant Where Failure
The authorize identity cache is keyed by worker name, so addrB.w1 reuses addrA.w1's payout identity coordinator.rs authorize share <addrB>.w1:… credited to <addrA>/…, not <addrB>
The credited share weight is rounded down to a multiple of 1,000 miner_submit.rs share … weighs 58294000, its worker's difficulty … weighs 58294013
Leftover sats go to the smallest remainders instead of the largest (1-sat moves the server's own audit cannot see) qbit-prism allocate_weighted_amounts block … direct outputs differ from the recomputed PPLNS split (e.g. 58402734 vs 58402733)

Observation, not changed here

Each round shows one stale-job refusal per active session, 135 in the per-PR run and deterministic. After every landing, the first job each session receives on the new parent is retired by the same-parent payout-revision bump that follows. The client retries on newer work, as a miner would, and the count is reported. Share accounting stays exact.

CI runtime

The per-PR case adds about 50-54 s locally (ramp, four rounds, maturity, fanout, spend and cleanup) to the live_regtest binary's serial run on the native job's shard. On the 2-vCPU CI runner it measured 55.7 s.

Validation

  • cargo fmt --all -- --check, and cargo clippy --locked -p qbit-prism-server --test live_regtest -- -D warnings
  • The per-PR case passed three times in a row with PRISM_TEST_REQUIRE_INTEGRATION=1 against PostgreSQL 16 (fsync on) and qbitd 1.0.0, in 53.7 s, 50.2 s and 50.2 s wall time. The nightly case passed twice.
  • The full live_regtest binary passed with the fixture change: 44 passed and 1 ignored (the nightly case)
  • scripts/check_gate_env_reads.py is clean, and the gate list stays sorted

🤖 Generated with Claude Code

…e regtest node (#521 scenario 7)

Add a gated live_regtest case in which 40 payout addresses, with a
seeded whale, Zipf tail, near-floor and near-zero weights, mine through
four found blocks. They submit real Stratum shares from 59 sessions on
both servers, at worker difficulties spanning about three orders of
magnitude. The node is restarted with -legacyretarget and ramped 1024x
under a mock clock, because on regtest's limit a share can be at most
twice as easy as a block.

The case recomputes each block's payout from the ledger's accepted
shares and the chain alone:

- the PPLNS window, including the partly counted oldest share;
- the gross split, floor, carry-forward and pool-fee dust sweep;
- the direct/fanout partition and the fanout fee.

It checks that recomputation against the coinbase outputs, every
carry-forward row and the final balances. After maturity, it checks
each fanout's net outputs, every payee address's receipts in the
payees' wallet, a confirmed wallet spend of direct and fanout outputs,
and the carry-forward integrity report. A 130-recipient nightly
variant is #[ignore] and opt-in.

The fixture gains an additive server_env hook, applied after every
other server setting.

Refs #521
Refs #474

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@djh58

djh58 commented Sep 28, 2026

Copy link
Copy Markdown
Member Author

Review ledger: head ebc464e9

This PR changes tests only, so a proportionate check replaces the full deep-review, as agreed: one medium code-review pass over the test code and three consecutive runs of the new test.

Code review (medium). No findings. The review checked the following against the server code:

  • the override order of server_env against the fixture's CTV defaults;
  • that all eight settings and the d=/md= password format exist;
  • the fanout fee formula and the direct-slot count rule;
  • the PPLNS cutoff (job_issued_at and accepted_at at or before the anchor, newest first);
  • the Stratum header, merkle and prevhash handling;
  • maturity and the fanout scan range.

It noted two low risks and counted neither as a finding:

  • Millisecond rounding of accepted_at can't differ from the ledger, because it stores whole milliseconds.
  • The scenario-size check reads from the plan; the paid-payee floor covers a collapse.

Flakiness: three consecutive local runs of the per-PR case

Run Result Wall time
1 pass 53.7 s
2 pass 50.2 s
3 pass 50.2 s

Every run had the same seeded shape: 1,425 accepted shares, 135 stale retries, 954x weight spread, 3 of 4 windows cut.

CI. All checks pass on ebc464e9, and check-gate-manifest reports all 745 expected gated tests executed. The new case took 55.7 s on the 2-vCPU native runner; that is the per-PR CI time it adds. live_regtest on that shard passed 48 with 2 ignored, the two opt-in nightly variants. One native shard first failed in landing_metrics::lost_commit_reply_timeouts_are_not_revision_work_failures ("Stratum closed before a response"). That binary is outside this diff, the test passed 5 of 5 locally, and it passed on rerun.

The full live_regtest binary on 3.x.x plus this branch (with #523 scenario 4) passed 48 with 2 ignored. All 15 expected live_regtest ids executed, with none missing or unexpected. #523's RPC-proxy fixture works with server_env, which is empty by default and applied after the proxy's port swap.

Nightly 130-recipient variant (opt-in, not listed): passed twice, in 99 s and 142 s. It covered 3,053 shares from 202 sessions over six blocks, paid 102 payees, and carried 71–92 sub-floor accounts per block.

Mutants. Each was applied to the server, rebuilt, and caught by the new case:

Mutant Caught by
A: the authorize identity cache is keyed by worker name, so addrB.w1 reuses addrA.w1's payout identity attribution: share <addrB>.w1:… credited to <addrA>/…, not <addrB>
B: the credited share weight is rounded down to a multiple of 1,000 (miner_submit.rs) attribution: weighs 58294000, its worker's difficulty … weighs 58294013
C: allocate_weighted_amounts gives leftover sats to the smallest remainders independent PPLNS recompute: direct outputs differ (58402734 vs 58402733, and so on), a 1-sat split that the server's own audit cannot see

Follow-up filed separately. With the pool fee disabled, any positive sub-floor carry makes apply_payout_policy fail with PayoutExceedsCandidateBalance on every refresh: "template refresh deferred", and no work on the new tip. This was reproduced live by removing the pool-fee settings from this case. The case therefore runs with a 200-bps pool fee, as mainnet does.

🤖 Generated with Claude Code

@djh58
djh58 merged commit d83f613 into 3.x.x Sep 28, 2026
38 of 41 checks passed
djh58 added a commit that referenced this pull request Sep 29, 2026
… fake node's difficulty and reconciled against the chain (#547)

`--node qbitd` runs the harness's existing phases, share reconciliation and
exit codes against a managed pair of regtest qbitd nodes instead of the
in-process fake. The frontends reach pool node A through a recording
JSON-RPC relay. Peer B, which A connects to as #530's PeerNode does, mints
every external tip with generatetoaddress.

The chain is ramped first, as #524 does. -legacyretarget epochs on qbit
regtest are 1,440 blocks, each clamped to 4x, so eight retargets reach bits
1e7fffc0 at height 12,960. That is the fake node's 1e7fffff to 0.0008%, so
the window, share difficulty and hashes per share are the fake run's. Two
node rules shape the clock: qbit refuses a block about ten minutes ahead of
its clock, and it serves a template more than 150 s after the tip at the
regtest limit. So the ramp's shared mock clock ends at wall time, the clocks
are then cleared, and B mints a keepalive once the tip's own timestamp is
120 s old. The timestamp matters because a pool block carries its job's
template time. The 1,439 blocks before the next retarget are headroom,
checked at entry.

A share is a block with probability 8 / window whatever the ramp, so a phase
at R shares/s implies an own block every W / (8R) seconds. Real-node mode
refuses a phase outside 9-600 s and reports the band, each phase's interval
and the stepped-over block solutions under node.cadence_band. --plan short
at its defaults implies one every 50 s.

A watcher on A stamps every tip change: a waitfornewblock long-poll given
the last tip, then a height walk. The relay records each submitblock verdict
verbatim in the fake node's submission shape. After the load, every block
above the ramp must be exactly one peer mint or one accepted submission, on
A's active chain, at the ramped bits, with every template at those bits.
Any disagreement is a contradicted premise (exit 8); a node that exits is an
abort (6). Provenance checks are unchanged; artifact_kind is always example
(#487 decision 1).

Fake-node mode is unchanged. Its code path is not edited, real-node report
keys appear only in real-node runs, and presets gain "--node": "fake".
fake_node_mode_is_unchanged checks the fake node's answers to a fixed RPC
corpus and a pr-smoke frontend's environment against goldens taken before
this change; load_smoke checks the side report's key sets against a third.

The tips plan now schedules --scheduled-blocks in its warm-up, its only
phase; before, it silently scheduled none. The gated
real_node_smoke_reconciles_against_postgres_and_the_chain runs the new
real-node-smoke preset on every PR: 50 sessions, 3 peer tips and 1 own
block. short-plan-real-node and short-plan-fake-node, the bridging pair for
#552, run nightly: the load harness's preset jobs now install the pinned
qbit, as the live-nightly job does.

Review fixes. The peer link is kept against
qbit's peer timeouts with a very large -peertimeout (the ramp's message
stamps are taken under a mock clock that starts about 36 minutes in the
past), and a lost link is named: peer-to-peer logging goes on after the
ramp, a disagreement adds both nodes' peers and chain tips to
node.chain_reconciliation, and the smoke test prints that and the nodes'
post-ramp logs when it fails. A mint first waits (10 s at most) for B to hold
A's tip, so it never forks a pool block still on its way. The relay forwards
and records in its own task, so a frontend that gives up on a request cannot
lose the record of a block A took. A rejected submitblock verdict (anything
but acceptance, duplicate or inconclusive), an unreadable submit and a tip
asked of B but never minted are problems. A node that exits or can no longer
be reached aborts the run (exit 6) instead of becoming a contradiction (8).
Tips are stamped when B minted them or A answered the submit, if earlier
than the watcher's wake-up. A failed start and an early drop remove the
data only after both nodes are dead. QBITD_BIN is read through the test
gate's name for it, and the runner-probe preset pins "--node": "fake".

Closes #547

Co-Authored-By: Claude Opus 5.5 <[email protected]>
djh58 added a commit that referenced this pull request Sep 29, 2026
… fake node's difficulty and reconciled against the chain (#547) (#590)

`--node qbitd` runs the harness's existing phases, share reconciliation and
exit codes against a managed pair of regtest qbitd nodes instead of the
in-process fake. The frontends reach pool node A through a recording
JSON-RPC relay. Peer B, which A connects to as #530's PeerNode does, mints
every external tip with generatetoaddress.

The chain is ramped first, as #524 does. -legacyretarget epochs on qbit
regtest are 1,440 blocks, each clamped to 4x, so eight retargets reach bits
1e7fffc0 at height 12,960. That is the fake node's 1e7fffff to 0.0008%, so
the window, share difficulty and hashes per share are the fake run's. Two
node rules shape the clock: qbit refuses a block about ten minutes ahead of
its clock, and it serves a template more than 150 s after the tip at the
regtest limit. So the ramp's shared mock clock ends at wall time, the clocks
are then cleared, and B mints a keepalive once the tip's own timestamp is
120 s old. The timestamp matters because a pool block carries its job's
template time. The 1,439 blocks before the next retarget are headroom,
checked at entry.

A share is a block with probability 8 / window whatever the ramp, so a phase
at R shares/s implies an own block every W / (8R) seconds. Real-node mode
refuses a phase outside 9-600 s and reports the band, each phase's interval
and the stepped-over block solutions under node.cadence_band. --plan short
at its defaults implies one every 50 s.

A watcher on A stamps every tip change: a waitfornewblock long-poll given
the last tip, then a height walk. The relay records each submitblock verdict
verbatim in the fake node's submission shape. After the load, every block
above the ramp must be exactly one peer mint or one accepted submission, on
A's active chain, at the ramped bits, with every template at those bits.
Any disagreement is a contradicted premise (exit 8); a node that exits is an
abort (6). Provenance checks are unchanged; artifact_kind is always example
(#487 decision 1).

Fake-node mode is unchanged. Its code path is not edited, real-node report
keys appear only in real-node runs, and presets gain "--node": "fake".
fake_node_mode_is_unchanged checks the fake node's answers to a fixed RPC
corpus and a pr-smoke frontend's environment against goldens taken before
this change; load_smoke checks the side report's key sets against a third.

The tips plan now schedules --scheduled-blocks in its warm-up, its only
phase; before, it silently scheduled none. The gated
real_node_smoke_reconciles_against_postgres_and_the_chain runs the new
real-node-smoke preset on every PR: 50 sessions, 3 peer tips and 1 own
block. short-plan-real-node and short-plan-fake-node, the bridging pair for
#552, run nightly: the load harness's preset jobs now install the pinned
qbit, as the live-nightly job does.

Review fixes. The peer link is kept against
qbit's peer timeouts with a very large -peertimeout (the ramp's message
stamps are taken under a mock clock that starts about 36 minutes in the
past), and a lost link is named: peer-to-peer logging goes on after the
ramp, a disagreement adds both nodes' peers and chain tips to
node.chain_reconciliation, and the smoke test prints that and the nodes'
post-ramp logs when it fails. A mint first waits (10 s at most) for B to hold
A's tip, so it never forks a pool block still on its way. The relay forwards
and records in its own task, so a frontend that gives up on a request cannot
lose the record of a block A took. A rejected submitblock verdict (anything
but acceptance, duplicate or inconclusive), an unreadable submit and a tip
asked of B but never minted are problems. A node that exits or can no longer
be reached aborts the run (exit 6) instead of becoming a contradiction (8).
Tips are stamped when B minted them or A answered the submit, if earlier
than the watcher's wake-up. A failed start and an early drop remove the
data only after both nodes are dead. QBITD_BIN is read through the test
gate's name for it, and the runner-probe preset pins "--node": "fake".

Closes #547

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