piotrnar / gocoin

Full bitcoin solution written in Go (golang)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong sequence of blocks when using FetchNextBlock

Sekagra opened this issue · comments

commented

I'm experiencing strange results when using the blockdb and FetchNextBlock to iterate over blocks in my local blocks directory.
My goal is to use the parser of gocoin for a related project and I noticed that the sequence in which blocks are returned by FetchNextBlock is not the necessarily the order given by the blockchain.
The deviation starts at around block #3857 and can be reproduced with this minimal example:

package main

import (
	"log"
	"os"

	"github.com/piotrnar/gocoin/lib/btc"
	"github.com/piotrnar/gocoin/lib/others/blockdb"
)

func main() {
	magic := [4]byte{0xF9, 0xBE, 0xB4, 0xD9}
	db := blockdb.NewBlockDB(os.Args[1], magic)

	for i := 0; i < 4000; i++ {
		dat, _ := db.FetchNextBlock()
		block, _ := btc.NewBlock(dat[:])
		log.Printf("Block #%d hash: %v\n", i, block.Hash)
	}
}

I'm aware that there might be orphaned blocks, however the last two block hashes output by this code are:

Block #3998 hash: 00000000e86a5b1406aec394d058496f0ca3c09ef999e39c2adbd247f3be401f
Block #3999 hash: 000000005b1c6306ad9bf38cdb60642bc24793db171daec4986fc41eb414a7c2

These blocks are #4017 and #4010 in the main chain which means the later block #4017 is processed before #4010!
Any ideas what's going one here? I doubt that this is an actual bug and guess I might just be using it incorrectly. However, I cannot figure out how.
Thanks in advance.

It's just returning the blocks in the same order as they are stored on disk.
Sorry, there is nothing I can do here.