I can reply for Bitcoin Core, however know there exists software program that works very in a different way.
I can think about that within the state of affairs the brand new chain is clearly longer, the node must revert its UTXO set to the purpose at which the chains diverged, add again mempool transactions that have been used for the reason that chains diverged, after which re validate all blocks within the new chain.
Precisely.
How is the UTXO set reverted? Do nodes preserve a historical past of UTXO set diffs?
Sure. For each block that’s a part of the energetic fundamental chain, “undo” information is maintained, which incorporates all of the UTXOs spent by the block. When you see the UTXO set as a database, and blocks as “patches” of modifications utilized to it (along with signatures to authorize the adjustments), then the undo information consists of the reverse patches (with out signatures). The undo information is saved within the blocks/rev?????.dat
recordsdata within the information listing.
Within the case the place the size of the chains usually are not considerably completely different, I can think about {that a} node may need to preserve each chains concurrently till it turns into clear one will probably be longer. Are nodes programmed to do that?
No, there’s all the time precisely one energetic chain, and Bitcoin Core solely maintains a UTXO set for the tip block of that chain. The rule is:
- Contemplate all blocks which aren’t invalid and don’t have any invalid ancestors.
- Contemplate solely these amongst that set whose collected chain work is maximal.
- If there are a number of, decide the one for which the tip was acquired first.
What’s the longest reorg a node can deal with with out revalidating the whole chain from the genesis block?
Any depth reorg needs to be dealt with accurately, all the way down to the final checkpoint that has been handed. However these could also be eliminated quickly.
If a reorg is sufficiently deep (I consider 10 blocks or extra), re-adding transactions again to the mempool isn’t carried out (simply maintaining observe of these transactions throughout the reorg can use unbounded a lot reminiscence in any other case).