In NBitcoin, I have coded a class that allows me to enumerate blocks in the Block Directory folder of bitcoind.
It works perfectly.
Then I am creating an open source indexer like BlockChain.info with stealth and CC support, and here how it works :
-I run bitcoind that maintain the Block Directory.
-Every minutes, the Indexer run, and traverse the Block Directory from its last position, to the end, and save the new position, indexing everything on the way.
It works fine, under the assumption that bitcoind will never append a block to blk5.dat, if its last block file is blk10.dat.
But one of my user seems to tell me that my assumption is wrong, and got a bug because of it.
So I looked at bitcoind code source.
I noticed that LoadBlockIndexDB() method, that is called at startup, retrieve the last position where it wrote a block into the nLastBlockFile and nLastBlockFile global variables.
pblocktree->ReadLastBlockFile(nLastBlockFile);
LogPrintf("LoadBlockIndexDB(): last block file = %i\n", nLastBlockFile);
if (pblocktree->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile))
LogPrintf("LoadBlockIndexDB(): last block file info: %s\n", infoLastBlockFile.ToString());
Then I have seen that when you save a new block, you find the next free position in such file with the FindBlockPos.
The search for free space start from the nLastBlockFile position.
So with this information, I conclude that bitcoind is writing sequentially to the BlockDirectory, and can never write back.
Can a dev would confirm my conclusion or am I missing something ?