Is block height always sequential? Can it serve as a 100% reliable block counter in case if I want to check whether I missed a block or I need to account for any corner cases?
2 Answers
Block height is by definition sequential in the sense that adjacent blocks will always have heights that differ by 1. But there is a chicken-and-egg problem: in order to compute the height, you have to have access to all the blocks in between the genesis block and the current one. (The height is not recorded in the block itself.)
There's also the issue that multiple blocks can have the same height, if they are on different branches of the chain. So just because you have one block at every height, doesn't mean you have all the blocks on the main chain; some of them might be on orphan branches.
Block heights on the "valid blockchain" are sequential, but a block that is produced might become reorganized out of the blockchain (as a result of a soft fork in the network). This does happen occasionally and isn't anything to be concerned most of the time.
Those blocks that are reorganized out of the blockchain still have a height, but that is still in sequence from its predecessor, but the block on the main blockchain will also have that height. It is possible that your blkXXXXX.dat contain a few blocks that are not on the blockchain.
Genesis block(0) --> 1 --> 2 --> 3 Main Chain
\-- 2 (reorged out)