<blockquote>
<p><strong>I interpret that it's a representation of the difficulty for the block. ... Is my understanding correct?</strong></p>
</blockquote>
<p>Yes, it is a compact representation of the network target for that block. The target being a kind of inverse of the difficulty.</p>
<ul>
<li><a href="https://en.bitcoin.it/wiki/Difficulty#How_is_difficulty_stored_in_blocks.3F" rel="nofollow noreferrer">How is difficulty stored in blocks</a></li>
<li><a href="https://bitcoin.stackexchange.com/q/57567/13866">Is there a rationale for the compact representation of the target?</a></li>
</ul>

CC BY-SA 4.0
2023-04-24T20:36:23.693Z
2023-04-24T20:41:31.907Z
2023-04-24T20:41:31.907Z
RedGrittyBrick
<p>In the source code for the <a href="https://github.com/bitcoin/bitcoin/blob/306ccd4927a2efe325c8d84be1bdb79edeb29b04/src/primitives/block.h#L14-L65" rel="nofollow noreferrer"><code>CBlockHeader</code></a> class, there is a field called <code>nBits</code>.</p>
<pre><code>/** Nodes collect new transactions into a block, hash them into a hash tree,
 * and scan through nonce values to make the block's hash satisfy proof-of-work
 * requirements.  When they solve the proof-of-work, they broadcast the block
 * to everyone and the block is added to the block chain.  The first transaction
 * in the block is a special one that creates a new coin owned by the creator
 * of the block.
 */
class CBlockHeader
{
public:
    // header
    int32_t nVersion;
    uint256 hashPrevBlock;
    uint256 hashMerkleRoot;
    uint32_t nTime;
    uint32_t nBits;
    uint32_t nNonce;
</code></pre>
<p>As far as I searched in the code, I didn't found any comment for this field. But I saw the <a href="https://github.com/bitcoin/bitcoin/blob/306ccd4927a2efe325c8d84be1bdb79edeb29b04/src/validation.cpp#L3367" rel="nofollow noreferrer"><code>CheckBlockHeader</code></a> function using the <code>nBits</code> field to check proof of work of the block. So I interpret that it's a representation of the difficulty for the block.</p>
<pre><code>static bool CheckBlockHeader(const CBlockHeader&amp; block, BlockValidationState&amp; state, const Consensus::Params&amp; consensusParams, bool fCheckPOW = true)
{
    // Check proof of work matches claimed amount
    if (fCheckPOW &amp;&amp; !CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
        return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, &quot;high-hash&quot;, &quot;proof of work failed&quot;);

    return true;
}
</code></pre>
<p>Is my understanding correct?</p>

<bitcoin-core-development><difficulty><proof-of-work><block-header><c++>
What is the nBits field in the CBlockHeader class?
Amir reza Riahi
# What is the nBits field in the CBlockHeader class?

(Asked by: username_0 on 2023-04-24)

In the source code for the CBlockHeader class, there is a field called nBits.
/** Nodes collect new transactions into a block, hash them into a hash tree,
 * and scan through nonce values to make the block's hash satisfy proof-of-work
 * requirements.  When they solve the proof-of-work, they broadcast the block
 * to everyone and the block is added to the block chain.  The first transaction
 * in the block is a special one that creates a new coin owned by the creator
 * of the block.
 */
class CBlockHeader
{
public:
    // header
    int32_t nVersion;
    uint256 hashPrevBlock;
    uint256 hashMerkleRoot;
    uint32_t nTime;
    uint32_t nBits;
    uint32_t nNonce;

As far as I searched in the code, I didn't found any comment for this field. But I saw the CheckBlockHeader function using the nBits field to check proof of work of the block. So I interpret that it's a representation of the difficulty for the block.
static bool CheckBlockHeader(const CBlockHeader& block, BlockValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW = true)
{
    // Check proof of work matches claimed amount
    if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
        return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "high-hash", "proof of work failed");

    return true;
}

Is my understanding correct?

**Tags:** bitcoin-core-development, difficulty, proof-of-work, block-header, c++


---

## 1 answers

### Answer by username_1 on 2023-04-24. Score: 3 | Accepted answer

> **I interpret that it's a representation of the difficulty for the block. ... Is my understanding correct?**

Yes, it is a compact representation of the network target for that block. The target being a kind of inverse of the difficulty.

* [How is difficulty stored in blocks](https://en.bitcoin.it/wiki/Difficulty#How_is_difficulty_stored_in_blocks.3F)
* [Is there a rationale for the compact representation of the target?](https://bitcoin.stackexchange.com/q/57567/13866)


---

