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 – 2019-02-21T15:22:19.413
1The 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 – 2019-02-21T15:52:46.287Ahh ... 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 – 2019-02-22T06:54:53.720
See https://bitcoin.stackexchange.com/a/51170/60443
– JBaczuk – 2019-02-22T12:58:11.080