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

Block 0: work = (2^256) / &lt;block 0 hash&gt; = 4.295 × 10^9
Block 1: work = (2^256) / &lt;block 1 hash&gt; = 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.)