<p>This appears to be a function to calculate the average number of hashes needed to generate a particular block. (This is different from difficulty.)</p>

<p>I think this function is correct. To explain why, imagine that Bitcoin used 4 bit hashes instead of 256 bit hashes. This would make the range of possible values 0 to 15.</p>

<p>If the target were 15, that would mean that any hash would be valid. Therefore, it should take 1 hash on average to find a valid proof of work:</p>

<pre><code>= (maximum possible hash + 1) / (target + 1)
= (15 + 1) / (15 + 1)
= 1
</code></pre>

<p>If the target were 7, that would mean that half of the hashes would be valid. Therefore, it should take 2 hashes on average to find a valid proof of work:</p>

<pre><code>= (maximum possible hash + 1) / (target + 1)
= (15 + 1) / (7 + 1)
= 2
</code></pre>

<p>Remember, a proof of work is valid if it is less than or equal to the target. See also CheckProofOfWork().</p>

<p>I can't tell if this is used anywhere. This function appears to have been removed in the latest version.</p>

CC BY-SA 3.0
2014-12-19T00:09:13.537Z
So basically, the +1 in the denominator is because there are `target+1` values that are less than or equal to the target, which makes sense. And the numerator is `1+maxtarget', because there are 1+maxtarget values that are less than or equal to the max target. This clears it up, thanks!
morsecoder
CC BY-SA 3.0
2018-02-24T10:19:25.363Z
Hi, so eventually, based on recent litecoin source, how to code for actually mine the genesis block?  For example, this is based on version 0.8 litecoin source. https://pastebin.com/du2XdxCC   From this, how should be changed? Thanks.
creator
CC BY-SA 3.0
2014-12-18T23:56:44.067Z
2014-12-19T02:37:01.483Z
2014-12-19T02:37:01.483Z
Nick ODell
<p>The implementation of <code>GetBlockWork()</code> in the bitcoin v0.9.3 source (<a href="https://github.com/bitcoin/bitcoin/blob/v0.9.3/src/main.h#L815-L822" rel="nofollow">https://github.com/bitcoin/bitcoin/blob/v0.9.3/src/main.h#L815-L822</a>) seems strange to me. The code is given below for convenience.</p>

<pre><code>CBigNum GetBlockWork() const
{
    CBigNum bnTarget;
    bnTarget.SetCompact(nBits);
    if (bnTarget &lt;= 0)
        return 0;
    return (CBigNum(1)&lt;&lt;256) / (bnTarget+1);
}
</code></pre>

<ol>
<li>Why the +1 in the denominator?</li>
<li>And shouldn't the numerator be the maximum possible 256 bit integer, instead of 2^256?</li>
</ol>

<p>I would expect the implementation to be:</p>

<pre><code>CBigNum GetBlockWork() const
{
    CBigNum bnTarget;
    bnTarget.SetCompact(nBits);
    if (bnTarget &lt;= 0)
        return 0;
    CBigNum bnMax;
    bnMax.SetHex("0xFFFFFFFFFFFFFFFF"); // Max 256 bit int
    return (bnMax / bnTarget);
}
</code></pre>

<difficulty><bitcoin-core-development>
Bitcoin GetBlockWork() function
morsecoder
# Bitcoin GetBlockWork() function

(Asked by: username_0 on 2014-12-18)

The implementation of `GetBlockWork()` in the bitcoin v0.9.3 source (<https://github.com/bitcoin/bitcoin/blob/v0.9.3/src/main.h#L815-L822>) seems strange to me. The code is given below for convenience.

```
CBigNum GetBlockWork() const
{
    CBigNum bnTarget;
    bnTarget.SetCompact(nBits);
    if (bnTarget <= 0)
        return 0;
    return (CBigNum(1)<<256) / (bnTarget+1);
}

```

1. Why the +1 in the denominator?
2. And shouldn't the numerator be the maximum possible 256 bit integer, instead of 2^256?

I would expect the implementation to be:

```
CBigNum GetBlockWork() const
{
    CBigNum bnTarget;
    bnTarget.SetCompact(nBits);
    if (bnTarget <= 0)
        return 0;
    CBigNum bnMax;
    bnMax.SetHex("0xFFFFFFFFFFFFFFFF"); // Max 256 bit int
    return (bnMax / bnTarget);
}

```

**Tags:** difficulty, bitcoin-core-development


---

## 1 answers

### Answer by username_1 on 2014-12-18. Score: 3 | Accepted answer

This appears to be a function to calculate the average number of hashes needed to generate a particular block. (This is different from difficulty.)

I think this function is correct. To explain why, imagine that Bitcoin used 4 bit hashes instead of 256 bit hashes. This would make the range of possible values 0 to 15.

If the target were 15, that would mean that any hash would be valid. Therefore, it should take 1 hash on average to find a valid proof of work:

```
= (maximum possible hash + 1) / (target + 1)
= (15 + 1) / (15 + 1)
= 1

```

If the target were 7, that would mean that half of the hashes would be valid. Therefore, it should take 2 hashes on average to find a valid proof of work:

```
= (maximum possible hash + 1) / (target + 1)
= (15 + 1) / (7 + 1)
= 2

```

Remember, a proof of work is valid if it is less than or equal to the target. See also CheckProofOfWork().

I can't tell if this is used anywhere. This function appears to have been removed in the latest version.

> **Comments:**
> **username_0**: So basically, the +1 in the denominator is because there are `target+1` values that are less than or equal to the target, which makes sense. And the numerator is `1+maxtarget', because there are 1+maxtarget values that are less than or equal to the max target. This clears it up, thanks!  
> **username_2**: Hi, so eventually, based on recent litecoin source, how to code for actually mine the genesis block?  For example, this is based on version 0.8 litecoin source. https://pastebin.com/du2XdxCC   From this, how should be changed? Thanks.  

---

