<p>I finally found the answer here:</p>
<p>It's a segwit transaction, <code>f07b4eb879ead12e58742e322878310d4b5bcd48b25c1e4ab781672a9f44a312</code> is the txid and <code>6ef0f0d3fcf20aedefa37425ea93e69c7da4d04fd1ce983c5cba613630e775ca</code> it's the hash!</p>
<p>for the record here the snippet to fix my problem:</p>
<pre><code>hash_data = data
if transaction['segwit']:
    hash_data = data[:4] + data[6:offset_before_tx_witnesses] + data[-4:]
transaction['tx_hash'] = ib2h(double_hash(hash_data))
</code></pre>

CC BY-SA 4.0
2024-01-31T17:56:22.237Z
2024-01-31T17:56:22.237Z
Ouziel Slama
<p>The problem occurs with block 1,669,774 on testnet (<a href="https://blockstream.info/testnet/block/0000000010001cebd3e4f2fe5f2e2cbbe35ba6457554d9e0e6eb100b6f91eaee" rel="nofollow noreferrer">https://blockstream.info/testnet/block/0000000010001cebd3e4f2fe5f2e2cbbe35ba6457554d9e0e6eb100b6f91eaee</a>).</p>
<p>To obtain a block I retrieve the file and the position by reading the key <code>b&lt;block_hash&gt;</code> from the leveldb database and I then parse the block in the usual way. This works well for all blocks except this one which returns me the list of transactions for this block BEFORE the reorganization.</p>
<p>For example the list of transactions contains the hash <code>6ef0f0d3fcf20aedefa37425ea93e69c7da4d04fd1ce983c5cba613630e775ca</code> instead of the hash <code>f07b4eb879ead12e58742e322878310d4b5bcd48b25c1e4ab781 672a9f44a312</code>.
<code>bitcoin-cli getblock &lt;hash&gt;</code> returns me the correct list of transactions. (of course I use the correct hash in both cases <code>0000000010001cebd3e4f2fe5f2e2cbbe35ba6457554d9e0e6eb100b6f91eaee</code>).</p>
<p>How to obtain the correct version of the block?
Thanks in advance!</p>
<p>Edit:
Here the function I use to get the file and the position:</p>
<pre><code>def read_raw_block(self, block_hash, only_header=False):
        block_key = bytes('b', 'utf-8') + binascii.unhexlify(inverse_hash(block_hash))
        block_data = self.blocks_leveldb.get(block_key)
        ds = BCDataStream()
        ds.write(block_data)

        version = ds.read_var_int()
        height = ds.read_var_int()
        status = ds.read_var_int()
        tx_count = ds.read_var_int()
        file_num = ds.read_var_int()
        block_pos_in_file = ds.read_var_int() - 8
        block_undo_pos_in_file = ds.read_var_int()
        block_header = ds.read_bytes(80)
        self.prepare_data_stream(file_num, block_pos_in_file)
        block = self.read_block(self.data_stream, only_header=only_header)
        block['block_index'] = height
        return block
</code></pre>
<p>SOLVED:
it's a segwit transaction, <code>f07b4eb879ead12e58742e322878310d4b5bcd48b25c1e4ab781672a9f44a312</code> is the txid and <code>6ef0f0d3fcf20aedefa37425ea93e69c7da4d04fd1ce983c5cba613630e775ca</code> it's the hash!</p>

CC BY-SA 4.0
2024-01-31T12:46:30.537Z
What is the block hash you looked up? As in, the one for which you read the `b<block_hash>` key?
Pieter Wuille
CC BY-SA 4.0
2024-01-31T15:37:22.747Z
@PieterWuille Yes I look for the transactions in the block '0000000010001cebd3e4f2fe5f2e2cbbe35ba6457554d9e0e6eb100b6f91eaee' and I use the key b'b\xee\xea\x91o\x0b\x10\xeb\xe6\xe0\xd9TuE\xa6[\xe3\ xbb,._\xfe \xf2\xe4\xd3\xeb\x1c\x00\x10\x00\x00\x00\x00', I get the file number and the position of the block and then I parse like everyone else. It's just the one that's the problem. Thanks in advance! I edited my question with the little function I'm using.
Ouziel Slama
CC BY-SA 4.0
2024-01-31T16:07:27.290Z
Thank you @PieterWuille, just found the solution! edited the question!
Ouziel Slama
CC BY-SA 4.0
2024-01-31T16:08:37.320Z
Feel free to post an answer to your own question.
Pieter Wuille
<chain-reorganization><leveldb>
Transaction incorrect when reading a block from `blk*.dat` files
Ouziel Slama
# Transaction incorrect when reading a block from `blk*.dat` files

(Asked by: username_0 on 2024-01-31)

The problem occurs with block 1,669,774 on testnet (<https://blockstream.info/testnet/block/0000000010001cebd3e4f2fe5f2e2cbbe35ba6457554d9e0e6eb100b6f91eaee>).

To obtain a block I retrieve the file and the position by reading the key `b<block_hash>` from the leveldb database and I then parse the block in the usual way. This works well for all blocks except this one which returns me the list of transactions for this block BEFORE the reorganization.

For example the list of transactions contains the hash `6ef0f0d3fcf20aedefa37425ea93e69c7da4d04fd1ce983c5cba613630e775ca` instead of the hash `f07b4eb879ead12e58742e322878310d4b5bcd48b25c1e4ab781 672a9f44a312`.
`bitcoin-cli getblock <hash>` returns me the correct list of transactions. (of course I use the correct hash in both cases `0000000010001cebd3e4f2fe5f2e2cbbe35ba6457554d9e0e6eb100b6f91eaee`).

How to obtain the correct version of the block?
Thanks in advance!

Edit:
Here the function I use to get the file and the position:

```
def read_raw_block(self, block_hash, only_header=False):
        block_key = bytes('b', 'utf-8') + binascii.unhexlify(inverse_hash(block_hash))
        block_data = self.blocks_leveldb.get(block_key)
        ds = BCDataStream()
        ds.write(block_data)

        version = ds.read_var_int()
        height = ds.read_var_int()
        status = ds.read_var_int()
        tx_count = ds.read_var_int()
        file_num = ds.read_var_int()
        block_pos_in_file = ds.read_var_int() - 8
        block_undo_pos_in_file = ds.read_var_int()
        block_header = ds.read_bytes(80)
        self.prepare_data_stream(file_num, block_pos_in_file)
        block = self.read_block(self.data_stream, only_header=only_header)
        block['block_index'] = height
        return block

```

SOLVED:
it's a segwit transaction, `f07b4eb879ead12e58742e322878310d4b5bcd48b25c1e4ab781672a9f44a312` is the txid and `6ef0f0d3fcf20aedefa37425ea93e69c7da4d04fd1ce983c5cba613630e775ca` it's the hash!

**Tags:** chain-reorganization, leveldb

> **Comments:**
> **username_1**: What is the block hash you looked up? As in, the one for which you read the `b<block_hash>` key?  
> **username_0**: @username_1 Yes I look for the transactions in the block '0000000010001cebd3e4f2fe5f2e2cbbe35ba6457554d9e0e6eb100b6f91eaee' and I use the key b'b\xee\xea\x91o\x0b\x10\xeb\xe6\xe0\xd9TuE\xa6[\xe3\ xbb,._\xfe \xf2\xe4\xd3\xeb\x1c\x00\x10\x00\x00\x00\x00', I get the file number and the position of the block and then I parse like everyone else. It's just the one that's the problem. Thanks in advance! I edited my question with the little function I'm using.  
> **username_0**: Thank you @username_1, just found the solution! edited the question!  
> **username_1**: Feel free to post an answer to your own question.  

---

## 1 answers

### Answer by username_0 on 2024-01-31. Score: 1 | Accepted answer

I finally found the answer here:

It's a segwit transaction, `f07b4eb879ead12e58742e322878310d4b5bcd48b25c1e4ab781672a9f44a312` is the txid and `6ef0f0d3fcf20aedefa37425ea93e69c7da4d04fd1ce983c5cba613630e775ca` it's the hash!

for the record here the snippet to fix my problem:

```
hash_data = data
if transaction['segwit']:
    hash_data = data[:4] + data[6:offset_before_tx_witnesses] + data[-4:]
transaction['tx_hash'] = ib2h(double_hash(hash_data))

```


---

