Bitcoin Core uses GetBlockProof() function to determine the contribution of a block to the total difficulty of the current chain, aka, nChainWork
in CBlockIndex. I'm having trouble understanding the logic in that implementation.
Let's ignore the math gymnastics done there to avoid overflowing off uint256
. The only part we need is the beginning of the comment:
// We need to compute 2**256 / (bnTarget+1)
And the question is simply, why?!
If I want to calculate the difficulty contribution of a block to the chain, my (possibly naive) logic would be: The lower the block hash value, the higher its contribution. While the equation mentioned in the comment achieves that, it doesn't do so linearly, but in a f(x)=1/x
fashion. I don't get why. Again, my (possibly naive) implementation would simply be:
~arith_uint256(0) - UintToArith256(block.GetBlockHash())
which basically means: The lower the hash of a block, the higher its contribution to the difficulty. And if using the block hash is bad for some reason (I'd appreciate explaining why if it's the case), a similar proportionality can be made with just the target:
~arith_uint256(0) - bnTarget
Can someone please explain the reason for that division choice?