1

Where in the source code do we check whether

 hash(blockheader*nonce) < Difficulty 

In fact, the line I found is as follows: https://github.com/bitcoin/bitcoin/blob/v0.17.0.1/src/pow.cpp#L87

My problem with this is 3-fold

a) this is not the code that miners use to perform the check; it probably is the validation on node-side ?

b) here the "hash" refers to what exactly ?

c) why is this value tested against nTarget ? rather than Difficulty ?

d) this check is usually advertised in media as "<"less rather than "<=" less than equal. But seeing how this line is coded , it means HASH<= nTarget is OK.

2

Where in the source code do we check whether hash(blockheader*nonce) < Difficulty

The function you are looking for is in pow.cpp:

bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
{
    bool fNegative;
    bool fOverflow;
    arith_uint256 bnTarget;

    bnTarget.SetCompact(nBits, &fNegative, &fOverflow);

    // Check range
    if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit))
        return false;

    // Check proof of work matches claimed amount
    if (UintToArith256(hash) > bnTarget)
        return false;

    return true;
}

a) this is not the code that miners use to perform the check; it probably is the validation on node-side ?

This is on the node side. Miners do not use Bitcoin Core software for mining, this check is implemented as part of the ASIC design, so not even software at all.

b) here the "hash" refers to what exactly ?

Here the hash is the function used to calculate the block hash. In Bitcoin, this is 2 rounds of sha256, sometimes called hash256. See Bitcoin Wiki - Bitcoin Hashing Algorithm

c) why is this value tested against nTarget ? rather than Difficulty ?

Difficulty is just an abstraction of the nTarget, which represents the actual 256 bit value that the header must be less than (or equal to). The header is a 256 bit value, so it must be compared to nTarget. The conversion between nTarget and difficulty is difficulty = difficulty_1_target / nTarget, where difficulty_1_target is the target of the genesis block. See Bitcoin Wiki - Difficulty

d) this check is usually advertised in media as "<"less rather than "<=" less than equal. But seeing how this line is coded , it means HASH<= nTarget is OK.

correct.

  • @JBaczuk.Thank you. This helps. But is the PoW definition of : Hash(blockheader + nonce) <= Difficulty incorrect ? .... or is this correct on the miner client end. Where is that line of code in the source code ? I'd be interested to see if that's "<" or "<=". Note that in your outlined answer, we are talking here about the node side. The process that "validates" the hash (which has been computed on the miner's client). This validation takes milliseconds. If on the miner's client "<" was set (rather than "<="), then it's irrelevant of to include the "=" on the node end. – user92452 Feb 21 at 15:22
  • 1
    The nonce is part of the block header. The miners have their own code, they don't use Bitcoin Core to mine, in fact they probably do the check in the ASIC, so no software at all. This is the line in Bitcoin Core if (UintToArith256(hash) > bnTarget) return false, so if it's >=, then it will be valid. Also, there is virtually no chance the hash will ever be =, so it doesn't matter. – JBaczuk Feb 21 at 15:52
  • Ahh ... thanks. but I am now a bit confused. I thought that if I run a full node, I perform mining too ? That should be in the bitcoin core code, no ? There must have been a pre-implementation for CPU mining ... where can we find that ? – user92452 Feb 22 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.