When a node receives a new block and passes the verification, it will try to add the new block to the blockchain, but there may be three different situations at this time, let's discuss them one by one.
- Add the block to the main chain
This is the most common situation. When a node receives a new block and passes verification, it will directly link the new block to the last block of the current blockchain, as shown in the following figure.
- Add the new block to the branch chain generated by the main chain
Due to the geographical distribution of Bitcoin nodes, network transmission and other reasons, there is a certain difference in the time each node receives new blocks generated by other nodes. Suppose there are two honest nodes X and Y that have completed the proof of work in a similar time. At this time, they have not received the new zone broadcasted by other nodes, so both X and Y broadcast the blocks they discovered to the network. At this time, some nodes will receive the block discovered by X first, and some nodes will receive the block discovered by Y first. Since the blocks discovered by X and Y are both valid, these nodes will add the blocks received first to their main chain.
As shown in the figure below (X blocks are represented by regular triangles, and Y blocks are represented by inverted triangles):
As the new blocks discovered by X and Y are further forwarded in the network, the node that receives the X discovery block first will also receive the Y block, and the node that receives the Y discovery block first will also receive the X zone. Piece. At this time, these nodes will add the valid blocks received late to the branch chain generated by the main chain, and the entire blockchain will have a short fork.
As shown in the figure below (X blocks are represented by regular triangles, Y blocks are represented by inverted triangles):
After the blocks discovered by X and Y are accepted by each node, the miners will only connect the valid blocks received late to the branch chain, and then continue mining based on their own main chain, which is the candidate block assembled by the miner Block Hash always corresponds to the last block on the main chain.
Then, node Z completed the next proof of work, assembled a new block and started broadcasting the block, and the last block of Z’s main chain was the block discovered by X (an equilateral triangle), so Z extended the previous X discovery The blockchain where the block is located.
As shown in the figure below (X blocks are represented by regular triangles, Y blocks are represented by inverted triangles, and Z blocks are represented by diamonds):
If the last block of the node's original main chain is a block of X, the block discovered by Z is normally added to the main chain. If the last block of the node's main chain is a block of Y, the block discovered by Z will be added to the branch chain first, and then the branch chain will be switched to the main chain, that is, the node will choose the largest amount of work (block height The highest chain is used as its main chain.
As the blocks discovered by Z continue to be propagated in the network, eventually the entire Bitcoin network has returned to a consistent state (no fork).
As shown in the figure below (X blocks are represented by regular triangles, Y blocks are represented by inverted triangles, and Z blocks are represented by diamonds):
In the situation we discussed above, only one block bifurcation occurred. In practice, the same situation (forking) may occur again after a block bifurcation. All nodes handle the same situation. However, the probability of each node continuously appearing in a short period of time to complete the proof of work at a similar time decreases exponentially. In the Bitcoin network, the fork of one block occurs every day, and the fork of two blocks only occurs once in a few weeks. The fork of more than six blocks has never happened, which is why we usually think that a transaction is confirmed by six blocks is the safest.