6
3
I'm keeping a simple log of blocks, and storing them in an array. When a new block arrives, I add it to the array.
Let's say this is my block chain array:
E <- tip
D
C
B
A
Now, say a new block arrives, extending a branch:
Z <- new tip
E Y
D X <- new branch start
C
B
A
Now the blocks C and D have been replaced in the main chain by X and Y.
Questions:
- Is there a way to detect that the latest block has extended a branch?
- In other words, how can I detect whether blocks lower down in the array need to be replaced?
- How frequently does this happen?
EDIT: Initial solution is to check if the previousblockhash of the new block is equal to the hash of the tip in the array. If not, update the old tip, and keep working down through the array updating each block until their is a match for the previousblockhash.
1Excellent. Do you also know what headers-only means? – inersha – 2016-08-02T16:13:20.820
1valid-fork means we received and validated the blocks in this fork. valid-headers means we received the blocks but never bothered to verify them, as they were no longer the best chain when we got them. headers-only means we never even received the blocks. – Pieter Wuille – 2016-08-02T17:33:29.053
1Why would a node receive a block header (headers-only) but not download the rest of the block? Or to put it another way, why would a node download a full block (valid-headers) if the node knows that we already have a valid block at the same height? – inersha – 2016-08-02T18:37:23.717
1@inersha Because it may have learned of a better block that extends another chain after requesting the header, but before receiving the header. In that case we don't bother downloading the rest of the block data. – Pieter Wuille – 2016-08-02T19:37:32.270