2

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!

4
  • What is the block hash you looked up? As in, the one for which you read the b<block_hash> key? Commented Jan 31 at 12:46
  • @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. Commented Jan 31 at 15:37
  • Thank you @PieterWuille, just found the solution! edited the question! Commented Jan 31 at 16:07
  • Feel free to post an answer to your own question. Commented Jan 31 at 16:08

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.