Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mining] IncrementExtraNonce without cs_main #921

Merged
merged 1 commit into from
Apr 18, 2021

Commits on Apr 4, 2021

  1. [Mining] IncrementExtraNonce without cs_main

    Removes the `cs_main` lock usage from the nonce setup
    in the miners.
    
    The chain tip is only used for getting the Height of the tip,
    so instead we can just retrieve the height directly. This requires
    a change to the signature of `IncrementExtraNonce`.
    
    The other item cs_main was protecting is the static `prevHeaderHash`;
    this is only used to reset `nExtraNonce`. However, `nExtraNonce` is
    thread-local, so the thread nonces will increment indefinitely, with the
    occasional 0 thrown in. This is therefore unnecessary--and rarely may
    result in a race:
    - Thread 1: creates block template on block N-1
    - Thread 2: accepts new block N
    - Thread 3: creates block template on block N, resets local nonce
    - Thread 1: resets local nonce
    - Thread 4: creates block template on block N, resets local nonce
    Thread 1 is doing useless work on the old block, this happens.
    Threads 3 and 4 are doing the same work since they have the same nonce.
    
    (If I understand right, and the nonces are used here mainly to give
    the threads distinct works sets, I believe this means actually
    incrementing `nExtraNonce` is unnecessary, since we take `cs_nonce` to
    increment `nNonce_base` in order to give each thread a unique nonce.
    We would need to rename `IncrementExtraNonce` to describe
    what it's actually doing then.)
    
    Removes one extraneous copy of the nonce into a thread local variable
    (there were previously two, and I suspect the extra was already being
    optimized away).
    Zannick committed Apr 4, 2021
    Configuration menu
    Copy the full SHA
    a3168e7 View commit details
    Browse the repository at this point in the history