<p>It looks like this function from <code>validation.h</code> does the job:</p>

<pre><code>CBlockIndex* LookupBlockIndex(const uint256&amp; hash)
</code></pre>

CC BY-SA 4.0
2019-11-01T02:40:09.017Z
The answer I posted is the answer to your next question. "How do I load the index so this function can work?"
Jose Fonseca
CC BY-SA 4.0
2019-10-31T18:48:26.300Z
2019-10-31T18:48:26.300Z
warchantua
<p>You'd need to get a full node started up by calling <code>AppInitMain</code> in <code>init.cpp</code> in order to get the index subsystem running. </p>

<p>Otherwise it can't query the index database to get the block height. </p>

<p>The node subsystem must be warmed up and running in order to run queries linked from C++ directly. You'd also need to synchronize the calls and respect locks - not easy.</p>

<p>Running AppInitMain is basically the same as running <code>bitcoind</code>, so you're probably better off calling into the RPC interface from C++ and passing the <code>getblock &lt;hash&gt; 1</code> command to it. The process the resulting JSON.</p>

<p><code>getblock &lt;hash&gt; 1</code> returns something like this</p>

<pre><code>{
  "hash": "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048",
  "confirmations": 600340,
  "strippedsize": 215,
  "size": 215,
  "weight": 860,
  "height": 1,
  "version": 1,
  "versionHex": "00000001",
  "merkleroot": "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098",
  "tx": [
    "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
  ],
  "time": 1231469665,
  "mediantime": 1231469665,
  "nonce": 2573394689,
  "bits": "1d00ffff",
  "difficulty": 1,
  "chainwork": "0000000000000000000000000000000000000000000000000000000200020002",
  "nTx": 1,
  "previousblockhash": "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
  "nextblockhash": "000000006a625f06636b8bb6ac7b960a8d03705d1ace08b1a19da3fdcc99ddbd"
}
</code></pre>

<p>Which includes the height and other data.</p>

CC BY-SA 4.0
2019-10-31T18:47:54.593Z
I wanted to get a name of C++ function to retrieve it, but anyway, thanks.
warchantua
CC BY-SA 4.0
2019-10-31T03:19:00.647Z
2019-10-31T03:19:00.647Z
Jose Fonseca
<p>Imagine I executed <a href="https://github.com/bitcoin/bitcoin/blob/3c40bc6726b6dc639c4ca2c00c720bccd4cd4dc7/src/validation.cpp#L1135" rel="nofollow noreferrer">ReadBlockFromDisk</a> and now I have valid instance of CBlock. </p>

<p>How do I read height of this block?</p>

<pre><code>CBlock block = ...;
CBlockIndex index(block);
assert (index.nHeight != 0); // fails, nHeight is default initialized
</code></pre>

CC BY-SA 4.0
2019-10-31T18:55:36.270Z
I think for have the height of block you can analysis of the bitcoin blockchain
vincenzopalazzo
<c++><height>
How to get block height, if I have only block header?
warchantua
# How to get block height, if I have only block header?

(Asked by: username_0 on 2019-10-30)

Imagine I executed [ReadBlockFromDisk](https://github.com/bitcoin/bitcoin/blob/3c40bc6726b6dc639c4ca2c00c720bccd4cd4dc7/src/validation.cpp#L1135) and now I have valid instance of CBlock.

How do I read height of this block?

```
CBlock block = ...;
CBlockIndex index(block);
assert (index.nHeight != 0); // fails, nHeight is default initialized

```

**Tags:** c++, height

> **Comments:**
> **username_1**: I think for have the height of block you can analysis of the bitcoin blockchain  

---

## 2 answers

### Answer by username_0 on 2019-10-31. Score: 1 | Accepted answer

It looks like this function from `validation.h` does the job:

```
CBlockIndex* LookupBlockIndex(const uint256& hash)

```

> **Comments:**
> **username_2**: The answer I posted is the answer to your next question. "How do I load the index so this function can work?"  

---

### Answer by username_2 on 2019-10-31. Score: 0

You'd need to get a full node started up by calling `AppInitMain` in `init.cpp` in order to get the index subsystem running.

Otherwise it can't query the index database to get the block height.

The node subsystem must be warmed up and running in order to run queries linked from C++ directly. You'd also need to synchronize the calls and respect locks - not easy.

Running AppInitMain is basically the same as running `bitcoind`, so you're probably better off calling into the RPC interface from C++ and passing the `getblock <hash> 1` command to it. The process the resulting JSON.

`getblock <hash> 1` returns something like this

```
{
  "hash": "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048",
  "confirmations": 600340,
  "strippedsize": 215,
  "size": 215,
  "weight": 860,
  "height": 1,
  "version": 1,
  "versionHex": "00000001",
  "merkleroot": "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098",
  "tx": [
    "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
  ],
  "time": 1231469665,
  "mediantime": 1231469665,
  "nonce": 2573394689,
  "bits": "1d00ffff",
  "difficulty": 1,
  "chainwork": "0000000000000000000000000000000000000000000000000000000200020002",
  "nTx": 1,
  "previousblockhash": "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
  "nextblockhash": "000000006a625f06636b8bb6ac7b960a8d03705d1ace08b1a19da3fdcc99ddbd"
}

```

Which includes the height and other data.

> **Comments:**
> **username_0**: I wanted to get a name of C++ function to retrieve it, but anyway, thanks.  

---

