0

The bitcoin whitepaper (page 3) states

The majority decision is represented by the longest chain, which has the greatest proof-of-work effort invested in it.

I could not find how exactly this difficulty is calculated.

Is it the sum of prefix zeros of the nonces of each block?


I found this answer but it is too short for me to understand.

1

Work is calculated as work = 2^256 / block_target, as stated in the linked answer, which is the minimum hash value that counts as a valid proof-of-work (note this only changes every 2016 blocks). The source code for this is src/chain.cpp L#121. The total chain work is the sum of work for all blocks in the chain, and is calculated here: src/validation.cpp L#3713. So to visualize, the lower the block target, the more work has been done.

Example

Block 0 target: 00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff
Block 1 target: 00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff

Block 0: work = (2^256) / <block 0 hash> = 4.295 × 10^9
Block 1: work = (2^256) / <block 1 hash> = 4.295 × 10^9

You can see that they have both done the same amount of work. In case of a tie, the following rules apply, see https://bitcoin.stackexchange.com/a/37275/60443

  • Which one was received first? (This can be different for different clients, which is why the previous rule is applied first.)
  • Which one has a larger pointer address? (This is largely random, and different for different clients.)
  • An explanation for "block_target" was missing. Now I understand that it is the literal block hash. – problemofficer Aug 28 '18 at 14:23
  • Could you by any chance provide a link to the respective method/line in the Bitcoin source code? – problemofficer Aug 28 '18 at 14:27
  • I've edited my answer, as I was mistaken about using the block hash for the block_target – JBaczuk Aug 28 '18 at 14:37
  • Also, added links to the source code. – JBaczuk Aug 28 '18 at 14:44

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.