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

1 Answer 1

0

Is it that blocks aren't in order in blk files ?

This is true. For example, during the initial-block-loading phase, your node may be concurrently collecting different ranges of blocks from multiple peers. So they can arrive out of order. You also have dead forks.

I can't tell whether this feature accounts for the whole of your issue.


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

Any? ...

Remove i everywhere and, in your while loop, replace it with numberOfBlocks :-)

3
  • Thanks for the explanation, so theoritically I have to parse and hash every block. And then looking into another block if the hash matches starting from block with height 0 ? But dead forks aren't on the main net are they ? And thanks for the python tip :D ^^ Sep 2 at 11:03
  • Yes, you need to do something like build an index of blocks with the block hash as key. Since occasionally two miners produce blocks at the same height, your BLK files will contain both even after one is selected as constituting part of the main chain and the other marked stale. Sep 2 at 11:59
  • It's in there, but I feel it could be perhaps stated a bit more clearly that the .blk files store block data in the order that it is received by the node, rather than in the order it was requested or the order of their heights.
    – Murch
    Sep 7 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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