<p>The version that you are deserializing is not the block version but rather the client version that wrote that entry to the database. In your case, a client version of 210000 means Bitcoin Core 0.21 wrote that entry.</p>
<p>The block header (which includes the block version) is found after the status conditional fields (i.e. after undo pos). It is serialized as the header would be when received over the network.</p>

CC BY-SA 4.0
2022-01-03T13:42:23.660Z
Ok... This means I have to get the version from the actual block instead.
Vizeet Srivastava
CC BY-SA 4.0
2022-01-03T18:40:54.463Z
@VizeetSrivastava You shouldn't need to go to the actual block data. The block header should be included in the record you are already looking at. It should just be after all of the stuff you have deserialized.
Ava Chow
CC BY-SA 4.0
2022-01-11T16:57:17.677Z
Thanks, Andrew I'll look into this.
Vizeet Srivastava
CC BY-SA 4.0
2022-01-13T11:59:38.193Z
I am able to get it. The block index also contains block header. I modified the program to get it.
Vizeet Srivastava
CC BY-SA 4.0
2022-01-03T06:10:24.153Z
2022-01-03T06:10:24.153Z
Ava Chow
<p>I am reading block index using plyvel python package.
Following is the location of block indexes:</p>
<pre><code>export BLOCK_INDEX_DB=$BITCOIN_HOME/blocks/index
</code></pre>
<p>Following is the method to decode b128 varint:</p>
<pre><code>def b128_varint_decode(b: bytes, pos = 0):
    n = 0
    while True:
        data = b[pos]
        pos += 1
        n = (n &lt;&lt; 7) | (data &amp; 0x7f)
        if data &amp; 0x80 == 0:
            return (n, pos)
        n += 1
</code></pre>
<p>LevelDB database for block indexes:</p>
<pre><code>block_db_g = plyvel.DB(os.getenv('BLOCK_INDEX_DB'), compression=None)
</code></pre>
<p>Following method gets values in block index:</p>
<pre><code>def getBlockIndex(block_hash_b: bytes, block_db):
    key = b'b' + block_hash_b
    value = block_db.get(key)
    jsonobj = {}
    jsonobj['version'], pos = b128_varint_decode(value)
    jsonobj['height'], pos = b128_varint_decode(value, pos)
    jsonobj['status'], pos = b128_varint_decode(value, pos)
    jsonobj['tx_count'], pos = b128_varint_decode(value, pos)
    if jsonobj['status'] &amp; (BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO):
            jsonobj['n_file'], pos = b128_varint_decode(value, pos)
    if jsonobj['status'] &amp; BLOCK_HAVE_DATA:
            jsonobj['data_pos'], pos = b128_varint_decode(value, pos)
    if jsonobj['status'] &amp; BLOCK_HAVE_UNDO:
            jsonobj['undo_pos'], pos = b128_varint_decode(value, pos)
    return jsonobj
</code></pre>
<p>Following is the caller main:</p>
<pre><code>if __name__ == '__main__':
    prev_blkhash_s = '0000000000000000000c5fc4ba692dc2cf016b8f45ec75eea2e12274879945a8'
    prev_blkhash_b = bytes.fromhex(prev_blkhash_s)[::-1]
    jsonobj = getBlockIndex(prev_blkhash_b, block_db_g)
    print(json.dumps(jsonobj, indent=4))
    print('-----------------------')
    print('0x%x' % jsonobj['version'])
</code></pre>
<p>I get the following results:</p>
<pre><code>$ python3 SoftForkSupport.py
{
    &quot;version&quot;: 210000,
    &quot;height&quot;: 681458,
    &quot;status&quot;: 157,
    &quot;tx_count&quot;: 2313,
    &quot;n_file&quot;: 2560,
    &quot;data_pos&quot;: 46435432,
    &quot;undo_pos&quot;: 5527623
}
-----------------------
0x33450
</code></pre>
<p>This is wrong as I can see the actual version of the block is 0x2f900004.</p>
<p>How can I get the actual version of the block from leveldb indexes?</p>

<bitcoin-core><block><python><leveldb>
Getting wrong version number of block from block index of Bitcoin Core
Vizeet Srivastava
# Getting wrong version number of block from block index of Bitcoin Core

(Asked by: username_0 on 2022-01-03)

I am reading block index using plyvel python package.
Following is the location of block indexes:

```
export BLOCK_INDEX_DB=$BITCOIN_HOME/blocks/index

```

Following is the method to decode b128 varint:

```
def b128_varint_decode(b: bytes, pos = 0):
    n = 0
    while True:
        data = b[pos]
        pos += 1
        n = (n << 7) | (data & 0x7f)
        if data & 0x80 == 0:
            return (n, pos)
        n += 1

```

LevelDB database for block indexes:

```
block_db_g = plyvel.DB(os.getenv('BLOCK_INDEX_DB'), compression=None)

```

Following method gets values in block index:

```
def getBlockIndex(block_hash_b: bytes, block_db):
    key = b'b' + block_hash_b
    value = block_db.get(key)
    jsonobj = {}
    jsonobj['version'], pos = b128_varint_decode(value)
    jsonobj['height'], pos = b128_varint_decode(value, pos)
    jsonobj['status'], pos = b128_varint_decode(value, pos)
    jsonobj['tx_count'], pos = b128_varint_decode(value, pos)
    if jsonobj['status'] & (BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO):
            jsonobj['n_file'], pos = b128_varint_decode(value, pos)
    if jsonobj['status'] & BLOCK_HAVE_DATA:
            jsonobj['data_pos'], pos = b128_varint_decode(value, pos)
    if jsonobj['status'] & BLOCK_HAVE_UNDO:
            jsonobj['undo_pos'], pos = b128_varint_decode(value, pos)
    return jsonobj

```

Following is the caller main:

```
if __name__ == '__main__':
    prev_blkhash_s = '0000000000000000000c5fc4ba692dc2cf016b8f45ec75eea2e12274879945a8'
    prev_blkhash_b = bytes.fromhex(prev_blkhash_s)[::-1]
    jsonobj = getBlockIndex(prev_blkhash_b, block_db_g)
    print(json.dumps(jsonobj, indent=4))
    print('-----------------------')
    print('0x%x' % jsonobj['version'])

```

I get the following results:

```
$ python3 SoftForkSupport.py
{
    "version": 210000,
    "height": 681458,
    "status": 157,
    "tx_count": 2313,
    "n_file": 2560,
    "data_pos": 46435432,
    "undo_pos": 5527623
}
-----------------------
0x33450

```

This is wrong as I can see the actual version of the block is 0x2f900004.

How can I get the actual version of the block from leveldb indexes?

**Tags:** bitcoin-core, block, python, leveldb


---

## 1 answers

### Answer by username_1 on 2022-01-03. Score: 2

The version that you are deserializing is not the block version but rather the client version that wrote that entry to the database. In your case, a client version of 210000 means Bitcoin Core 0.21 wrote that entry.

The block header (which includes the block version) is found after the status conditional fields (i.e. after undo pos). It is serialized as the header would be when received over the network.

> **Comments:**
> **username_0**: Ok... This means I have to get the version from the actual block instead.  
> **username_1**: @username_0 You shouldn't need to go to the actual block data. The block header should be included in the record you are already looking at. It should just be after all of the stuff you have deserialized.  
> **username_0**: Thanks, Andrew I'll look into this.  
> **username_0**: I am able to get it. The block index also contains block header. I modified the program to get it.  

---

