Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: BusinessChain on December 05, 2019, 10:14:20 AM



Title: How is an old block that forks the chain being validated in Bitcoin Core
Post by: BusinessChain on December 05, 2019, 10:14:20 AM
In the Bitcoin Core source code https://github.com/bitcoin/bitcoin/blob/21ee676dd6a7d9704367b6412bf8e1e443ec2b5b/src/chainparams.cpp#L139 (https://github.com/bitcoin/bitcoin/blob/21ee676dd6a7d9704367b6412bf8e1e443ec2b5b/src/chainparams.cpp#L139) I see that the latest checkpoint is at height 295000.
If I understand correctly, this means that today a miner could produce a block with height 295001. Although this block would not go into the main chain of course it still has to be validated by all nodes, and if valid, it would go into the secondary chain pool.
My question is, how is this block validated, since it is not known anymore what the state of the UTXO set was at height 295000. My guess is that in order to validate, the node has to temporarily reorganize the UTXO set to height 295000, check if block 295001 is valid, if yes store it, otherwise reject it, and then restore the current state of the UTXO.
 
Looks like a lot of work. Is that the way it works?


Title: Re: How is an old block that forks the chain being validated in Bitcoin Core
Post by: darosior on December 05, 2019, 10:20:38 AM
The proof-of-work is checked first, and in order to make a node validate a block that old, a miner would have to produce a chain of blocks with more total work than the one from 295000 to current height. If you are interested here is (https://github.com/bitcoin/bitcoin/blob/5aee0e2163cc02cdf1cd6bec69bfbb513bb302d5/src/validation.cpp#L62) the work comparison function.


Title: Re: How is an old block that forks the chain being validated in Bitcoin Core
Post by: gmaxwell on December 05, 2019, 10:24:28 AM
What darosior said, plus it doesn't even fetch or store the block until it has headers that indicate it would be on the most-work chain assuming it at all its ancestors are valid.


Title: Re: How is an old block that forks the chain being validated in Bitcoin Core
Post by: BusinessChain on January 26, 2020, 12:48:58 AM
ok so let's assume there is a long valid fork that is stronger than mainchain. This might happen if a split network rejoins. How would the reorganization of the UTXO take place? Please describe the process.


Title: Re: How is an old block that forks the chain being validated in Bitcoin Core
Post by: gmaxwell on January 26, 2020, 11:10:44 AM
ok so let's assume there is a long valid fork that is stronger than mainchain. This might happen if a split network rejoins. How would the reorganization of the UTXO take place? Please describe the process.

If another branch gets more work it will fetch the blocks on it, undo the blocks on its current chain back to the point of the fork, then it applies the new blocks. If it encounters invalidity in the new blocks it marks the invalid block and any descendants as invalid which would make that branch no longer the longest, then it would undo back to the common ancestor and apply forward back up to the tip.

So your original thinking is close, except it doesn't even start fetching the data or storing it unless it would be the best chain by POW if it was also valid.