Skip to content

3.x.x: mining.authorize is bounded by default: 32 attempts per session, one node validation per payout address and at most four at once per frontend (#823) - #828

Merged
djh58 merged 3 commits into
3.x.xfrom
djh58/authorize-budget
Nov 2, 2026
Merged

djh58 merged 3 commits into
3.x.xfrom
djh58/authorize-budget

Conversation

@djh58

@djh58 djh58 commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

mining.authorize is now bounded by default: 32 attempts per session per interval, one node validateaddress per payout address, and at most four validations at once per frontend.

The problem (#823)

  • PRISM_STRATUM_MAX_AUTHORIZE_ATTEMPTS_PER_INTERVAL defaulted to 0 in stratum.rs, compose.yaml and .env.example, and SessionBudget::charge treats 0 as unlimited.
  • The identity cache was keyed by the whole username, so every new .suffix cost a validateaddress call. Usernames are not authenticated.
  • The RPC client has no concurrency bound, so these calls share the node's RPC work queue with getblocktemplate and submitblock.
  • Job builds after an authorization were already bounded by the initial-job and rebuild-lane semaphores. The node RPC calls were not.

The change

  • Budget on by default.
    • DEFAULT_MAX_AUTHORIZE_ATTEMPTS_PER_INTERVAL = 32 is used by StratumConfig::default(), and from_env starts from it.
    • compose.yaml uses :-32 and .env.example sets =32. 0 still disables the budget.
    • 32 is the value docs/prism-b4-stratum-admission.md already recommended.
  • Identity cache keyed by payout address.
    • The key is the text before the first ., exactly as typed, so ledger miner_id labels are unchanged.
    • The TTL (1 h) and capacity (4,096) are unchanged, a failed validation is still never cached, and the fallback-address handling is unchanged.
    • The code is in the new coordinator/payout_identity.rs.
  • Frontend-wide bound, not keyed by IP.
    • Sessions naming the same new address share one validation: a per-address single flight, with a cache re-check after the lock.
    • At most MAX_CONCURRENT_VALIDATIONS = 4 run at once.
    • A session waits at most VALIDATION_WAIT (5 s) in all, first behind another session's validation of its address and then for a slot. After that it is refused with the retryable backend-rpc-unavailable, and its connection stays open.
    • Only authorize takes this path. Share submission, getblocktemplate and submitblock never wait on it. Permits and locks are released by RAII on error and on cancellation.
  • Cache eviction. A full cache drops the entries of addresses the node refused, which resolved to PRISM_USERNAME_FALLBACK_ADDRESS, before the others. A run of such addresses cannot push out addresses that pay as typed.
  • Per-IP connection cap. PRISM_STRATUM_MAX_CONNECTIONS_PER_IP stays 0 by default. With no PROXY-protocol support, every miner behind a balancer that sends no PROXY header shares the balancer's addresses. docs/prism-configuration.md now says so.
  • Docs: docs/prism-configuration.md and docs/prism-b4-stratum-admission.md are updated.

Tests

New tests in coordinator::miner_tests::authorize, against the real Coordinator::authorize with the mock node, which now answers validateaddress:

  • worker_suffixes_on_one_address_share_one_validation. On the base this made 101 calls instead of 1.
  • concurrent_sessions_naming_a_new_address_share_its_validation. On the base this made 16 calls instead of 1.
  • validations_wait_for_a_slot_and_a_full_wait_is_a_retryable_refusal
  • a_session_behind_another_validation_waits_within_the_same_bound
  • a_full_cache_drops_fallback_entries_before_addresses

New test in tests/stratum_protocol.rs: the_default_authorize_budget_bounds_username_cycling. On the base it fails, because the budget is 0 instead of 32.

Updated tests: reauthorization_retains_original_username_capacity_until_timer_expiry and reauthorization_disconnect_reclaims_current_and_retained_username_capacity poll mining.authorize every 20 ms on one connection. They now set the budget to 0, because the budget is not what they measure.

Mutation checks: each of these mutants fails its test.

  • An unbounded flight wait.
  • No eviction of fallback entries first.
  • A slot wait that restarts its own 5 s.

Run at this head (d2541106) with CARGO_BUILD_JOBS=6 and --test-threads=4:

Command Result
cargo test -p qbit-prism-server --lib 889 passed, 3 ignored
cargo test -p qbit-prism-server --test stratum_protocol 50 passed, 1 ignored
--test stratum_admission / stratum_admission_gate / stratum_codec / stratum_properties / stratum_rebuild_lane 6 / 6 / 7 / 8 / 7 passed
--test observability / landing_metrics / vardiff_convergence / work_refresh_metric 17 / 41 / 7 / 1 passed
PRISM_TEST_REQUIRE_INTEGRATION=1 PRISM_TEST_DATABASE_URL=<disposable PostgreSQL 16> cargo test -p qbit-prism-server --test authorization_rpc 1 passed. The fallback contract is unchanged.
cargo clippy --locked -p qbit-prism-server --all-targets -- -D warnings clean, apart from the 3 existing vendored sqlx-core warnings
cargo fmt -p qbit-prism-server -- --check clean
python3 scripts/check_prism_settings.py, check_deleted_test_map.py, check_gate_env_reads.py, and python3 -m unittest tests.test_check_prism_settings tests.test_mainnet_compose_contract tests.test_mining_compose_profiles ok

This branch and #822's touch disjoint files. With both applied, cargo test -p qbit-prism-server --lib passed 890, with 3 ignored.

The branch is based on 987027fe and merges cleanly with the current 3.x.x (d0c649b4). CI tests the merge result.

Rollout

Release-note text:

mining.authorize is bounded by default (#823). PRISM_STRATUM_MAX_AUTHORIZE_ATTEMPTS_PER_INTERVAL now defaults to 32 per PRISM_STRATUM_SESSION_BUDGET_INTERVAL_SECONDS (60 s) in the binary, compose.yaml and .env.example. A session that exceeds it is disconnected and counted as authorize_budget in qbit_prism_stratum_connection_refusals_total; 0 still disables it.

An .env copied from an earlier .env.example still holds =0 and keeps the budget off; remove the line or set 32. Payout identities are cached per address rather than per username. Each frontend validates at most four new addresses at once, and a session that has waited five seconds for a validation is refused with backend-rpc-unavailable and may retry. PRISM_STRATUM_MAX_CONNECTIONS_PER_IP stays 0 by default; with no PROXY-protocol support, a cap behind a load balancer throttles the whole pool.

Before deploying:

  • Deployments that set no value pick up 32 from compose.yaml. The qbit-tools group_vars for mainnet and testnet4 set no PRISM_STRATUM_MAX_AUTHORIZE_ATTEMPTS_PER_INTERVAL, so both get 32.
  • A stratum proxy that sends mining.authorize for more than 32 downstream workers over one upstream connection within an interval needs a higher value, or 0.

Follow-ups in qbit-tools, not in this PR:

  • testnet4 group_vars should pin PRISM_STRATUM_MAX_CONNECTIONS_PER_IP: "0" explicitly, as mainnet's prism_pair.yml does, rather than inherit it.
  • The PRISM 3.x.x rollout doc still says the per-session budgets "also default off". That is no longer true for the authorize budget.

Review notes

  • The fallback validation is not cached. It is still validated afresh for each refused address. tests/authorization_rpc.rs asserts that contract, and changing it is a separate decision.
  • The default may need a load check. The value of 4 validation slots assumes validateaddress takes about 1 ms. A cold-cache herd of many new addresses arriving while the node is slow would get retryable refusals after 5 s. This was not load-tested against a real qbitd.

Refs #823

🤖 Generated with Claude Code


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

…n, one node validation per payout address and at most four at once per frontend (#823)

PRISM_STRATUM_MAX_AUTHORIZE_ATTEMPTS_PER_INTERVAL defaulted to 0, so a
session could authorize without limit, and the identity cache was keyed
by the whole username, so every new worker suffix cost a validateaddress
call. Usernames are not authenticated.

- The budget now defaults to 32 per interval in the code, compose.yaml and
  .env.example, the value docs/prism-b4-stratum-admission.md recommends;
  0 still disables it. StratumConfig::default() holds the code default and
  from_env starts from it.
- Resolved identities are cached by the address before the first '.', so
  a new worker suffix on a known address costs no node call. A failed
  validation is still never cached.
- Sessions naming the same new address share one validation, and a
  frontend runs at most four validations at once. A session waits at most
  five seconds in all, behind another session's validation of its address
  and then for a slot, before it is refused with backend-rpc-unavailable
  and may retry. Only authorization waits on this bound; share submission,
  getblocktemplate and submitblock never do.
- A full cache drops the entries of addresses the node refused, which
  resolved to the configured fallback, before the others, so a run of
  them cannot push out the addresses that pay as typed.
- PRISM_STRATUM_MAX_CONNECTIONS_PER_IP stays 0 by default: with no
  PROXY-protocol support, every miner behind a balancer that sends no
  PROXY header shares the balancer's addresses. The configuration docs now
  say so.

Two username-capacity tests poll mining.authorize every 20 ms on one
connection; they now disable the budget, which they do not measure.

Refs #823

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

blacksmith-sh Bot commented Oct 29, 2026

Copy link
Copy Markdown

Found 1 test failure on Blacksmith runners:

Failure

Test View Logs
test_check_prism_pool_acquires.PoolAcquireGuardTests.test_existing_classifications_are_
current/test_existing_classifications_are_current
View Logs

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

@djh58

djh58 commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

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-30T15:29:40.460673Z 0a3724f Manual request
ℹ️ 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: d254110645

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@qbit-reviewer qbit-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 findings posted inline.

Findings without an inline location

1 nit

2. [low · nit] Fallback validation reuses the spent deadline, so a definitive refusal can end as busy and is not cached

crates/qbit-prism-server/src/coordinator.rs:4240

EP-ERRORS. deadline is set once at coordinator.rs:4186. resolve() takes validation_slot(deadline) at 4196 and frees the slot at 4198. The fallback resolve at 4240 then needs a new slot under the same deadline. Suppose all 4 slots are busy: the session waits about 5 s, and the node answers isvalid:false. The fallback slot wait then times out right away with busy(). The node's definitive answer is thrown away, remember() at 4255 is never reached, and each retry repeats both waits and RPCs. This only happens when the frontend is saturated, and a retry that gets a slot early succeeds, so the cost is worse odds and wasted node calls, not a wrong payout.

Comment thread docs/prism-configuration.md Resolved
…orize doc (#823)

check_prism_pool_acquires.py requires every acquire-shaped call in the
server to carry a counted classification. The validation slot added for
#823 acquires a tokio semaphore, not a database connection, so it is
listed as non-database, as the stratum job-limit semaphores are.

docs/prism-configuration.md said share submission never waits on the
validation bound. A session handles its requests in order, so shares it
sends while its own mining.authorize waits are answered after it. Other
sessions' shares never wait on it.

Refs #823

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

@qbit-reviewer qbit-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 finding posted inline.

Still open from earlier reviews

  • [low · nit] Full-cache eviction drops fresh fallback entries but keeps entries already past the 1 h TTL: crates/qbit-prism-server/src/coordinator/payout_identity.rs:67, first reported at d254110

@djh58

djh58 commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: d135464cf2

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…it was validated in, and a full identity cache drops expired entries first (#823)

authorize took a validation slot for the address, released it after the
node call, then waited for a second slot under the same five-second
deadline to resolve the configured fallback. With every slot in use and
other validations queued, a session that got its first slot late lost
the node's definitive refusal to that second wait: it was refused as
busy, nothing was cached, and each retry repeated both waits and both
calls. One slot now covers both node calls of an authorization. The wait
bound is unchanged: a session waits at most five seconds in all, behind
another session's validation of its address and then for one slot.

When the identity cache was full, remember() first dropped every entry
that resolved to the fallback, however fresh, and kept address entries
past the hour that cached() already reads as misses. It now drops expired
entries first, then fallback entries, then, as before, entries older than
five minutes and finally the rest. docs/prism-configuration.md says so.

The payout_identity module comment now says, as the configuration doc
does, that other sessions' share submission never waits on these.

Refs #823

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

djh58 commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

qbit-reviewer #2 (the fallback validation reused the spent deadline) is fixed in 0a3724f. authorize now takes one validation slot before the first validateaddress call and holds it for the fallback call too, so a definitive refusal is resolved and cached instead of ending as busy behind validations queued meanwhile. The wait bound is unchanged: a session still waits at most VALIDATION_WAIT (5 s) in all, for the flight lock and then for one slot, and every early return drops the slot. a_refused_address_resolves_to_the_fallback_in_the_same_slot queues the session and a second validation behind four held slots. Before the fix the session was refused as busy after 5 s. Now it resolves to the fallback, its slot passes to the queued validation, and a second authorization of the address costs no node call.

Checks on this head: cargo test -p qbit-prism-server --lib 891 passed, 3 ignored; --test stratum_protocol 50 passed, 1 ignored; stratum_admission/stratum_admission_gate/stratum_codec/stratum_properties/stratum_rebuild_lane 6/6/7/8/7; authorization_rpc against a disposable PostgreSQL 16 passes; clippy -D warnings and fmt are clean; scripts/check_prism_pool_acquires.py exits 0.

@djh58

djh58 commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

@codex review

@qbit-reviewer qbit-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 0a3724fda3

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

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