blockchain – Wrong hashes for blocks when parsing from blk

[ad_1]

Hello I’m using a python script to actually retrieve information from a blk file and to read data.

import datetime

# Read block !
file = open("REDACTED\blocks\\blk00000.dat", 'rb')

numberOfBlocks = 0
i = 0
toAnalyse = 5000

while i < toAnalyse:  

    block_magic_bytes = file.read(4)
    
    if b''==block_magic_bytes :
        break
    
    block_size = file.read(4)
    block_size_int = int.from_bytes(block_size,'little')
    block_version = file.read(4)
    block_lastblock = file.read(32)
    block_lastblock_little_endian = int.from_bytes(block_lastblock,'little') 

    # merkle root
    file.seek(32,1)

    block_time = int.from_bytes(file.read(4),'little')

    #bits
    file.seek(4,1)

    block_nonce = int.from_bytes(file.read(4),'little')

    # Prints some information
    print("Block number   : " + str(numberOfBlocks))
    print(numberOfBlocks)

    print("Magic bytes    : " + block_magic_bytes.hex(" ").upper())
    print("Block size hex : " + block_size.hex(" ").upper())
    print("Block size int : " + str(block_size_int))
    print("Block version  : " + block_version.hex(" "))
    print( f'Block preBlock : {block_lastblock_little_endian:064x}' )
    print("Block time     : " + str(datetime.datetime.fromtimestamp(block_time)))
    print("Block nonce    : " + str(block_nonce))
    print()

    # Jumps to next magic_bytes
    file.seek(block_size_int-4-32-32-4-4-4,1)

    numberOfBlocks+=1
    i+=1
print("Last block read")

But when I read the block 486 in the blk file I get this :

Block number   : 486
486
Magic bytes    : F9 BE B4 D9
Block size hex : D8 00 00 00
Block size int : 216
Block version  : 01 00 00 00
Block preBlock : 00000000806df68baab17e49e567d4211177fef4849ffd8242d095c6a1169f45
Block time     : 2009-01-14 22:27:29
Block nonce    : 2063568441

As I understand the BTC blockchain the previous hash should be the hash of the block with height 485, but when I look using an explorer this is the hash of block 499 : https://www.blockchain.com/btc/block/00000000806df68baab17e49e567d4211177fef4849ffd8242d095c6a1169f45

Is it that blocks aren’t in order in blk files ? Are my blk files wrong ? I downloaded them using bitcoin-core. Are some blocks actually not included in the overall blockchain ?

I would appreciate any pointers,tips or help of course ^^

[ad_2]

Source link

Leave a Comment