How bitcoin nodes update UTXO set when their latests blocks are replaced?

3

1

Let's say bitcoin node has 100 blocks. Then he recieve another two blocks. Now it have 102 blocks. But now he recieve block 103. And this block is from another chain and our node have to remove blocks 101 and 102 and replace them with this chain with block 101, 102 and 103.

So now it must rewind UTXO set back to node 100 and update with transactions from newly recieved nodes 101..103.

How nodes making this rollback in utxo set?

Michal

Posted 2019-05-26T14:54:43.473

Reputation: 539

Answers

6

Bitcoin Core since v0.8 maintains "undo files" that contain the information necessary to undo the effect of a block on the UTXO set.

In a way you can see blocks as authenticated patches to be applied to the UTXO set; they list new outputs to be added, and which inputs to be spent. In order to support rolling back the UTXO set, undo blocks are created as a side effect of validation: structures that contain the UTXOs that were spent. When rolling back, the undo files are applied in reverse order.

Pieter Wuille

Posted 2019-05-26T14:54:43.473

Reputation: 54 032

and how was this before v0.8? and for how many blocks are undo files hold?Michal 2019-05-26T20:12:59.577

2Before 0.8 an entirely different design was used, where instead of a UTXO set, a database was kept with for every output ever created whether it was spent, and if so, where it was spent. Rolling back just required marking those database entries as unspent again.Pieter Wuille 2019-05-26T20:15:23.957

2Undo data is kept for every block that is kept (undo files are around 10x smaller than the corresponding block files). If you prune your node, both old blocks and old undo data are pruned.Pieter Wuille 2019-05-26T20:16:21.077