<blockquote>
<ol start="2">
<li>and they're 1::1 with block files (i.e., for a given NNNNN the files <code>blkNNNNN.dat</code> and <code>revNNNNN.dat</code> hold information for the same
blocks),</li>
<li>and they're written and chunked in the order in which blocks are received by the node (from the network)</li>
</ol>
</blockquote>
<p>This assumption is incorrect. The <code>rev*.dat</code> files are actually written in height order. This is because they are written at a different time. The undo data for a block is only written after it has been connected to the chain tip, so the undo data ends up being in the order that blocks are connected. So for blocks which are downloaded but never connected, undo data does not exist for them (although this case is unlikely as blocks aren't downloaded unless they are supposed to be connected).</p>
<blockquote>
<p><strong>Then:</strong> <em>Are very old rev*.dat files ever used?</em>  Say, those that belong to blocks buried more than 100 blocks deep from the top of the chain?  If they <em>are</em> ever used <em>what is the use case?</em></p>
</blockquote>
<p>When it comes to the operation of the node, no, old undo data is not actually being used. However it does have some uses outside of node operation, particularly in examining the blockchain via RPC, and the creation of the block filter and the coin stats indices.</p>
<p>The RPC uses undo data in some places because it contains the UTXOs that were spend by that block. This allows <code>getblock</code> to calculate the transaction fees paid for each transaction in the block. However this is not a hard requirement, and if the undo data is not found, then it simply doesn't calculate the fees.</p>
<p>Another RPC that uses the undo data is <code>getblockstats</code> which uses the undo data to also calculate the fees for the block as well as the change in size of the UTXO set that the block causes. If the undo does not exist, then this RPC would fail.</p>
<p>For both the block filter and coin stats indices, the undo data is used to build them because it provides a snapshot of the UTXO set changes made. This allows the index to be populated without having to track the UTXO set while it is being built, so it reduces the memory usage and increases performance.</p>
<p>For node operation, undo data is only needed if the block were to be disconnected from the tip. This only occurs when reorgs happen. However Bitcoin is built with the possibility that there could be an extremely large work reorg that could reorg out things that are considered ancient history.</p>
<p>However, old undo data is indeed not used in operation of the node, and there could be a mode added which deletes them. On my node, this totals to 48 GB of data, so the space savings are not nearly as significant as normal pruning.</p>
<blockquote>
<p>(and, w.r.t. the answer <a href="https://bitcoin.stackexchange.com/a/71297/36307">here</a> of the first question in the following list, what is the database corruption that occurs - presumably to the UTXO state - if the rev files are all deleted and why is it necessary to regenerate them?)</p>
</blockquote>
<p>They need to be regenerated because of how the block index works. Since it also contains the location of the undo data, if that data were to be missing, the block index will find that to be an error and thus require a reindex. If an option were added to allow the deletion of rev*.dat files, then that would obviously have to be changed.</p>
<blockquote>
<p>(bonus question: given a txindex: is there anything in the rev*.dat file that can't be regenerated simply by looking at the blocks in the corresponding blk*.dat file and using the txindex to find previous transactions?</p>
</blockquote>
<p>Yes, but that would be pretty slow.</p>

CC BY-SA 4.0
2021-10-11T03:23:35.863Z
Thank you - I was totally unaware of the uses to satisfy the needs of RPC methods, also haven't gotten into the block filter/coin stats indexes yet so I'll look at those.  W.r.t. your first point though - I get what you're saying - I was going off of [this answer](https://bitcoin.stackexchange.com/a/92141/36307) which asserts that "the only guarantee is that block data and undo data is stored in matching files" but I see that doesn't imply they're 1::1 - extinct blocks will show up in the block file but not in the corresponding rev file.
davidbak
CC BY-SA 4.0
2021-10-11T12:15:23.497Z
Perhaps worth adding: the undo data is small relative to block data size, so if (undo+blocks) is a problem for you, then the blocks alone are most likely a problem too... and you should be running in pruning mode, which deletes both.
Pieter Wuille
CC BY-SA 4.0
2021-10-12T01:23:48.073Z
@PieterWuille - thank you size wasn't the issue, really it was _why_ which was the issue - I didn't understand, given the stated reason ("undo transactions from a block") why they were needed so badly they were regenerated for blocks buried way too deep to be affected by a reorg.
davidbak
CC BY-SA 4.0
2021-10-11T02:58:19.630Z
2021-10-11T02:58:19.630Z
Ava Chow
<p>There have been multiple questions on this but none, IMO, really get to the issue:</p>
<p><strong>Given:</strong></p>
<ol>
<li>The rev*.dat files hold &quot;undo&quot; data for rolling back the UTXO state, block by block,</li>
<li>and they're 1::1 with block files (i.e., for a given NNNNN the files <code>blkNNNNN.dat</code> and <code>revNNNNN.dat</code> hold information for the same blocks),</li>
<li>and they're written and chunked in the order in which blocks are received by the node (from the network)</li>
<li>and the UTXO set is computed (can <em>only</em> be computed) by considering blocks in block-chain order,</li>
<li>and in any block the coinbase transaction is unspendable for 101 confirmations (by consensus) which is so that reorgs of up to 100 blocks don't cause serious problems with coins suddenly disappearing, and 100 blocks piled on top is apparently <em>long enough</em> to outwait any reorganization ...</li>
</ol>
<p><strong>Then:</strong> <em>Are very old rev*.dat files ever used?</em>  Say, those that belong to blocks buried more than 100 blocks deep from the top of the chain?  If they <em>are</em> ever used <em>what is the use case?</em></p>
<p>(and, w.r.t. the answer <a href="https://bitcoin.stackexchange.com/a/71297/36307">here</a> of the first question in the following list, what is the database corruption that occurs - presumably to the UTXO state - if the rev files are all deleted and why is it necessary to regenerate them?)</p>
<p>(bonus question: given a txindex: is there anything in the rev*.dat file that can't be regenerated simply by looking at the blocks in the corresponding blk*.dat file and using the txindex to find previous transactions?</p>
<p>Other questions here approaching this question but <em>not</em> dupes:</p>
<ul>
<li><a href="https://bitcoin.stackexchange.com/questions/71272/are-old-rev-dat-file-safely-deletable">&quot;Are old rev*.dat file safely deletable?&quot;</a> - closest to being on point, the accepted answer explains (without really explaining) that if you delete them it &quot;effectively corrupts the databases&quot; and also that they'll be recreated if they're all deleted.  But doesn't say <em>why they need to be kept indefinitely</em>, which is what I'm asking.</li>
<li><a href="https://bitcoin.stackexchange.com/questions/32716/what-exactly-are-the-rev00000-dat-files">&quot;What exactly are the rev00000.dat files?&quot;</a> - directly answers what they're for but does <em>not</em> answer why they need to be kept forever</li>
<li><a href="https://bitcoin.stackexchange.com/questions/57978/file-format-rev-dat">&quot;File format -- rev*.dat&quot;</a> - specifically about the <em>format</em> of the contents of the rev files</li>
<li><a href="https://bitcoin.stackexchange.com/questions/70982/whats-raw-undo-data-for">&quot;What's raw undo data for?&quot;</a> - sadly, the only answer doesn't even answer the question asked but is in fact a cut/paste of the answer to the <em>file format</em> question #57978 directly above in this list - yet, it got the OP's checkmark, go figure...</li>
<li><a href="https://bitcoin.stackexchange.com/questions/95624/undo-files-format-change-and-reorg-process-question-for-v0-19">&quot;Undo files format change and reorg process question for v0.19&quot;</a> - <em>extremely</em> detailed answer about the <em>format</em> of the contents of the rev files</li>
<li><a href="https://bitcoin.stackexchange.com/questions/tagged/undofiles">The tag &quot;undofiles&quot;</a> - 3 questions (before this one) - 2 are listed above and the other is someone's one-off operational problem (blank blk and rev files).</li>
<li>This <a href="https://bitcoin.stackexchange.com/a/50071/36307">answer</a> is very good as to <em>when</em> rev*.dat files are used (blockchain reorgs) but doesn't address why they're kept forever.</li>
</ul>

<bitcoin-core-development><chain-reorganization><undofiles>
What are the use cases where very old rev*.dat files are needed?
davidbak
# What are the use cases where very old rev*.dat files are needed?

(Asked by: username_0 on 2021-10-11)

There have been multiple questions on this but none, IMO, really get to the issue:

**Given:**

1. The rev\*.dat files hold "undo" data for rolling back the UTXO state, block by block,
2. and they're 1::1 with block files (i.e., for a given NNNNN the files `blkNNNNN.dat` and `revNNNNN.dat` hold information for the same blocks),
3. and they're written and chunked in the order in which blocks are received by the node (from the network)
4. and the UTXO set is computed (can *only* be computed) by considering blocks in block-chain order,
5. and in any block the coinbase transaction is unspendable for 101 confirmations (by consensus) which is so that reorgs of up to 100 blocks don't cause serious problems with coins suddenly disappearing, and 100 blocks piled on top is apparently *long enough* to outwait any reorganization ...

**Then:** *Are very old rev\*.dat files ever used?* Say, those that belong to blocks buried more than 100 blocks deep from the top of the chain? If they *are* ever used *what is the use case?*

(and, w.r.t. the answer [here](https://bitcoin.stackexchange.com/a/71297/36307) of the first question in the following list, what is the database corruption that occurs - presumably to the UTXO state - if the rev files are all deleted and why is it necessary to regenerate them?)

(bonus question: given a txindex: is there anything in the rev\*.dat file that can't be regenerated simply by looking at the blocks in the corresponding blk\*.dat file and using the txindex to find previous transactions?

Other questions here approaching this question but *not* dupes:

* ["Are old rev\*.dat file safely deletable?"](https://bitcoin.stackexchange.com/questions/71272/are-old-rev-dat-file-safely-deletable) - closest to being on point, the accepted answer explains (without really explaining) that if you delete them it "effectively corrupts the databases" and also that they'll be recreated if they're all deleted. But doesn't say *why they need to be kept indefinitely*, which is what I'm asking.
* ["What exactly are the rev00000.dat files?"](https://bitcoin.stackexchange.com/questions/32716/what-exactly-are-the-rev00000-dat-files) - directly answers what they're for but does *not* answer why they need to be kept forever
* ["File format -- rev\*.dat"](https://bitcoin.stackexchange.com/questions/57978/file-format-rev-dat) - specifically about the *format* of the contents of the rev files
* ["What's raw undo data for?"](https://bitcoin.stackexchange.com/questions/70982/whats-raw-undo-data-for) - sadly, the only answer doesn't even answer the question asked but is in fact a cut/paste of the answer to the *file format* question #57978 directly above in this list - yet, it got the OP's checkmark, go figure...
* ["Undo files format change and reorg process question for v0.19"](https://bitcoin.stackexchange.com/questions/95624/undo-files-format-change-and-reorg-process-question-for-v0-19) - *extremely* detailed answer about the *format* of the contents of the rev files
* [The tag "undofiles"](https://bitcoin.stackexchange.com/questions/tagged/undofiles) - 3 questions (before this one) - 2 are listed above and the other is someone's one-off operational problem (blank blk and rev files).
* This [answer](https://bitcoin.stackexchange.com/a/50071/36307) is very good as to *when* rev\*.dat files are used (blockchain reorgs) but doesn't address why they're kept forever.

**Tags:** bitcoin-core-development, chain-reorganization, undofiles


---

## 1 answers

### Answer by username_1 on 2021-10-11. Score: 3 | Accepted answer

> 2. and they're 1::1 with block files (i.e., for a given NNNNN the files `blkNNNNN.dat` and `revNNNNN.dat` hold information for the same
>    blocks),
> 3. and they're written and chunked in the order in which blocks are received by the node (from the network)

This assumption is incorrect. The `rev*.dat` files are actually written in height order. This is because they are written at a different time. The undo data for a block is only written after it has been connected to the chain tip, so the undo data ends up being in the order that blocks are connected. So for blocks which are downloaded but never connected, undo data does not exist for them (although this case is unlikely as blocks aren't downloaded unless they are supposed to be connected).

> **Then:** *Are very old rev\*.dat files ever used?* Say, those that belong to blocks buried more than 100 blocks deep from the top of the chain? If they *are* ever used *what is the use case?*

When it comes to the operation of the node, no, old undo data is not actually being used. However it does have some uses outside of node operation, particularly in examining the blockchain via RPC, and the creation of the block filter and the coin stats indices.

The RPC uses undo data in some places because it contains the UTXOs that were spend by that block. This allows `getblock` to calculate the transaction fees paid for each transaction in the block. However this is not a hard requirement, and if the undo data is not found, then it simply doesn't calculate the fees.

Another RPC that uses the undo data is `getblockstats` which uses the undo data to also calculate the fees for the block as well as the change in size of the UTXO set that the block causes. If the undo does not exist, then this RPC would fail.

For both the block filter and coin stats indices, the undo data is used to build them because it provides a snapshot of the UTXO set changes made. This allows the index to be populated without having to track the UTXO set while it is being built, so it reduces the memory usage and increases performance.

For node operation, undo data is only needed if the block were to be disconnected from the tip. This only occurs when reorgs happen. However Bitcoin is built with the possibility that there could be an extremely large work reorg that could reorg out things that are considered ancient history.

However, old undo data is indeed not used in operation of the node, and there could be a mode added which deletes them. On my node, this totals to 48 GB of data, so the space savings are not nearly as significant as normal pruning.

> (and, w.r.t. the answer [here](https://bitcoin.stackexchange.com/a/71297/36307) of the first question in the following list, what is the database corruption that occurs - presumably to the UTXO state - if the rev files are all deleted and why is it necessary to regenerate them?)

They need to be regenerated because of how the block index works. Since it also contains the location of the undo data, if that data were to be missing, the block index will find that to be an error and thus require a reindex. If an option were added to allow the deletion of rev\*.dat files, then that would obviously have to be changed.

> (bonus question: given a txindex: is there anything in the rev\*.dat file that can't be regenerated simply by looking at the blocks in the corresponding blk\*.dat file and using the txindex to find previous transactions?

Yes, but that would be pretty slow.

> **Comments:**
> **username_0**: Thank you - I was totally unaware of the uses to satisfy the needs of RPC methods, also haven't gotten into the block filter/coin stats indexes yet so I'll look at those.  W.r.t. your first point though - I get what you're saying - I was going off of [this answer](https://bitcoin.stackexchange.com/a/92141/36307) which asserts that "the only guarantee is that block data and undo data is stored in matching files" but I see that doesn't imply they're 1::1 - extinct blocks will show up in the block file but not in the corresponding rev file.  
> **username_2**: Perhaps worth adding: the undo data is small relative to block data size, so if (undo+blocks) is a problem for you, then the blocks alone are most likely a problem too... and you should be running in pruning mode, which deletes both.  
> **username_0**: @username_2 - thank you size wasn't the issue, really it was _why_ which was the issue - I didn't understand, given the stated reason ("undo transactions from a block") why they were needed so badly they were regenerated for blocks buried way too deep to be affected by a reorg.  

---

