Web Interstitial Ad Example

chain reorganization – Transaction incorrect when reading a block from `blk*.dat` files


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!

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()

        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

it’s a segwit transaction, f07b4eb879ead12e58742e322878310d4b5bcd48b25c1e4ab781672a9f44a312 is the txid and 6ef0f0d3fcf20aedefa37425ea93e69c7da4d04fd1ce983c5cba613630e775ca it’s the hash!


Source link