Friday, November 22, 2024

blockchain – Improper hashes for blocks when parsing from blk

Hiya I am utilizing a python script to truly retrieve data from a blk file and to learn knowledge.

import datetime

# Learn block !
file = open("REDACTEDblocksblk00000.dat", 'rb')

numberOfBlocks = 0
i = 0
toAnalyse = 5000

whereas i < toAnalyse:  

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

    # merkle root
    file.search(32,1)

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

    #bits
    file.search(4,1)

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

    # Prints some data
    print("Block quantity   : " + str(numberOfBlocks))
    print(numberOfBlocks)

    print("Magic bytes    : " + block_magic_bytes.hex(" ").higher())
    print("Block measurement hex : " + block_size.hex(" ").higher())
    print("Block measurement int : " + str(block_size_int))
    print("Block model  : " + 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 subsequent magic_bytes
    file.search(block_size_int-4-32-32-4-4-4,1)

    numberOfBlocks+=1
    i+=1
print("Final block learn")

However after I learn the block 486 within the blk file I get this :

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

As I perceive the BTC blockchain the earlier hash needs to be the hash of the block with top 485, however after I look utilizing an explorer that is the hash of block 499 : https://www.blockchain.com/btc/block/00000000806df68baab17e49e567d4211177fef4849ffd8242d095c6a1169f45

Is it that blocks aren’t so as in blk information ? Are my blk information flawed ? I downloaded them utilizing bitcoin-core. Are some blocks truly not included within the general blockchain ?

I’d admire any pointers,suggestions or assist in fact ^^

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles