# How is the amount of proof-of-work invested in a chain calculated?

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.

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`

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. Commented Aug 28, 2018 at 14:23
• Could you by any chance provide a link to the respective method/line in the Bitcoin source code? Commented Aug 28, 2018 at 14:27
• I've edited my answer, as I was mistaken about using the block hash for the `block_target` Commented Aug 28, 2018 at 14:37
• Also, added links to the source code. Commented Aug 28, 2018 at 14:44