Fix stale target_length when nbits changes on same base_hash#706
Merged
NamecoinGithub merged 2 commits intoMay 27, 2026
Conversation
Copilot
AI
changed the title
[WIP] Fix target length rebind logic for network difficulty changes
Prime engine: rebind target_length on nBits-only template updates
May 27, 2026
…length helper Extend PrimeMiningEngine::run_pool_thread rebind gate to also fire when session->nbits changes without a base_hash change (rare singleflight waiter race at difficulty retarget). Without this, the sieve continues with the previous target_length and silently drops candidates. Add a reason= field to the rebind diagnostic log so operators can grep for the nbits-only case. Centralise the ceil(nbits/1e7) derivation into mining::derive_target_length() so CPU and GPU cannot drift apart. Co-authored-by: NamecoinGithub <130555019+NamecoinGithub@users.noreply.github.com>
Copilot
AI
changed the title
Prime engine: rebind target_length on nBits-only template updates
Fix stale target_length when nbits changes on same base_hash
May 27, 2026
NamecoinGithub
approved these changes
May 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The sieve rebind gate in
PrimeMiningEngine::run_pool_threadonly fires onbase_hashchange. Whennbitschanges across two same-base templates (possible at difficulty retarget via LLL-TAO's singleflight waiter path), the sieve continues with the staletarget_length. Symptom: Best 7.x climbing while bucket 7 stays at 0;rejected_below_diffdominatesvalidate_attempts.Changes
|| my_nbits != bound_session->nbitsso the sieve re-prepares when difficulty changes without a Merkle rotationreason=to diagnostic log — emits"base","nbits", or"base+nbits"so the rare nbits-only rebind is greppable in operator logsderive_target_length(nbits)inmining/prime_thresholds.hpp— single formula (clamp(ceil(nbits / 1e7))) replaces inline derivations in both CPU and GPU engines, preventing future driftOriginal prompt
Context
Deep research session https://github.com/NamecoinGithub/NexusMiner/tasks/a44e71ef-3448-4cb2-80a9-f38bed797e7c identified the root cause of the observed
Best 7.14 / slot 7 = 0symptom under low network difficulty (~6.48):The miner's own documentation references this as an older regression at
docs/diagrams/mining-loops/prime-mining-flow.md:111-126, with the implementation site atsrc/cpu/src/cpu/prime/prime_mining_engine.cpp:639-645.This is observable to operators as:
Chain Count: 5:594 6:25 7:0 8:0 9:0 10:0 11:0 Best 7.14 Current Difficulty 6.48rejected_below_diff=614out ofvalidate_attempts=615over 19 hours of miningBlocks accepted: 1 rejected: 0(so the rare share that DOES tick through is valid — the gate isn't broken, just over-rejecting)Hard constraints
src/cpu/src/cpu/prime/prime_mining_engine.cpp:639-645FIRST. The exact symbol names below are inferred from the research report and may need adjustment.base_hashalone is used elsewhere in the codebase (e.g., for cache lookups, telemetry), the change must not break those.copilot/prime-engine-nbits-invalidation-key.Task
Step 1 — Read and confirm the invalidation site
Open
src/cpu/src/cpu/prime/prime_mining_engine.cpparound lines 630-680. Locate:m_base_hash,m_current_base, orm_last_base_hash).target_length(likelym_target_lengthor similar).target_length— probably triggered inside the main mining loop when a new work unit arrives. Look for a comparison likeif (new_base_hash != m_base_hash) { rebind_target_length(); m_base_hash = new_base_hash; }.Also locate where the engine receives
nBitsfrom the upstream template path. Search for:nBitsfield assignmentstarget_lengthderivation fromnBits(typicallytarget_length = bits_to_chain_length(nBits)or similar)If the actual code structure differs significantly from this description, stop and report rather than guessing. The research report may have referenced an older line range.
Step 2 — Extend the invalidation key
Change the rebind condition from:
to:
Add a new member
m_nbits(or equivalent name matching local conventions) initialized to0(or0xFFFFFFFF, whichever is the "unset" sentinel used elsewhere for nBits in this file). Initialize it in the constructor and in anyReset()/Reinit()method.Step 3 — Add a diagnostic log line
When the rebind fires due to nBits change only (base_hash unchanged), log it at info level:
When the rebind fires due to base_hash change (the existing path), preserve existing logging behavior — don't add a new line if one already exists. If no log exists, add a symmetric one:
Match the project's existing debug/log style — if the codebase uses a different logger (spdlog, custom, etc.), use that instead.
Step 4 — Add a regression test if a test harness exists
Check
tests/ortest/directory for existing PrimeMiningEngine tests. If a unit-test harness exists (Catch2, gtest, or similar):Add a test case that:
(base_hash=H1, nBits=N1)— asserttarget_lengthis bound to derivation-of-N1.(base_hash=H1, nBits=N2)where N2 != N1 —...This pull request was created from Copilot chat.