<p>Work is calculated as <code>work = 2^256 / block_target</code>, 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 <a href="https://github.com/bitcoin/bitcoin/blob/v0.16.2/src/chain.cpp#L121" rel="nofollow noreferrer">src/chain.cpp L#121</a>. The total chain work is the sum of work for all blocks in the chain, and is calculated here: <a href="https://github.com/bitcoin/bitcoin/blob/v0.16.2/src/validation.cpp#L3713" rel="nofollow noreferrer">src/validation.cpp L#3713</a>. So to visualize, the lower the block target, the more work has been done.</p>

<h2>Example</h2>

<p>Block 0 target: <code>00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff</code><br>
Block 1 target:
<code>00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff</code></p>

<p><strong><a href="https://www.rapidtables.com/convert/number/hex-to-decimal.html?x=00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048" rel="nofollow noreferrer">Block 0</a>:</strong> <a href="http://www.wolframalpha.com/input/?i=(2%5E256)+%2F+26959946667150639794667015087019630673637144422540572481103610249215" rel="nofollow noreferrer"><code>work = (2^256) / &lt;block 0 hash&gt; = 4.295 × 10^9</code></a><br>
<strong><a href="https://www.rapidtables.com/convert/number/hex-to-decimal.html?x=00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048" rel="nofollow noreferrer">Block 1</a>:</strong> <a href="http://www.wolframalpha.com/input/?i=(2%5E256)+%2F+26959946667150639794667015087019630673637144422540572481103610249215" rel="nofollow noreferrer"><code>work = (2^256) / &lt;block 1 hash&gt; = 4.295 × 10^9</code></a></p>

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

<ul>
<li>Which one was received first? (This can be different for different clients, which is why the previous rule is applied first.)</li>
<li>Which one has a larger pointer address? (This is largely random, and different for different clients.)</li>
</ul>

CC BY-SA 4.0
2018-08-28T14:23:29.800Z
An explanation for "block_target" was missing. Now I understand that it is the literal block hash.
problemofficer - n.f. Monica
CC BY-SA 4.0
2018-08-28T14:27:22.367Z
Could you by any chance provide a link to the respective method/line in the Bitcoin source code?
problemofficer - n.f. Monica
CC BY-SA 4.0
2018-08-28T14:37:13.900Z
I've edited my answer, as I was mistaken about using the block hash for the `block_target`
JBaczuk
CC BY-SA 4.0
2018-08-28T14:44:23.053Z
Also, added links to the source code.
JBaczuk
CC BY-SA 4.0
2018-08-28T13:41:55.343Z
2018-08-28T15:22:21.403Z
2018-08-28T15:22:21.403Z
JBaczuk
<p>The <a href="https://bitcoin.org/bitcoin.pdf" rel="nofollow noreferrer">bitcoin whitepaper</a> (page 3) states</p>

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

<p>I could not find how exactly this difficulty is calculated.</p>

<p>Is it the sum of prefix zeros of the nonces of each block?</p>

<p><hr>
<em>I found <a href="https://bitcoin.stackexchange.com/a/939/86900">this answer</a> but it is too short for me to understand.</em></p>

<proof-of-work><consensus>
How is the amount of proof-of-work invested in a chain calculated?
problemofficer - n.f. Monica
# How is the amount of proof-of-work invested in a chain calculated?

(Asked by: username_0 on 2018-08-28)

The [bitcoin whitepaper](https://bitcoin.org/bitcoin.pdf) (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](https://bitcoin.stackexchange.com/a/939/86900) but it is too short for me to understand.*

**Tags:** proof-of-work, consensus


---

## 1 answers

### Answer by username_1 on 2018-08-28. Score: 1 | Accepted answer

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](https://github.com/bitcoin/bitcoin/blob/v0.16.2/src/chain.cpp#L121). The total chain work is the sum of work for all blocks in the chain, and is calculated here: [src/validation.cpp L#3713](https://github.com/bitcoin/bitcoin/blob/v0.16.2/src/validation.cpp#L3713). 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](https://www.rapidtables.com/convert/number/hex-to-decimal.html?x=00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048):** [`work = (2^256) / <block 0 hash> = 4.295 × 10^9`](http://www.wolframalpha.com/input/?i=(2%5E256)+%2F+26959946667150639794667015087019630673637144422540572481103610249215)  
**[Block 1](https://www.rapidtables.com/convert/number/hex-to-decimal.html?x=00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048):** [`work = (2^256) / <block 1 hash> = 4.295 × 10^9`](http://www.wolframalpha.com/input/?i=(2%5E256)+%2F+26959946667150639794667015087019630673637144422540572481103610249215)

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.)

> **Comments:**
> **username_0**: An explanation for "block_target" was missing. Now I understand that it is the literal block hash.  
> **username_0**: Could you by any chance provide a link to the respective method/line in the Bitcoin source code?  
> **username_1**: I've edited my answer, as I was mistaken about using the block hash for the `block_target`  
> **username_1**: Also, added links to the source code.  

---

