Closes #17862
Context from original comment (minor edits):
RewindBlockIndex()
is a mechanism to allow nodes to be upgraded after segwit activation, while still keeping their chainstate/datadir in a consistent state. It works as follows:
- A pre-segwit (i.e. v0.13.0 or older) node is running.
- Segwit activates. The pre-segwit node remains sync’ed to the tip, but is not enforcing the new segwit rules.
- The user upgrades the node to a segwit-aware version (v0.13.1 or newer).
- On startup, in
AppInitMain()
,RewindBlockIndex()
is called. This walks the chain backwards from the tip, disconnecting and erasing blocks that from after segwit activation that weren’t validated with segwit rules. - those blocks are then redownloaded (with witness data) and validated with segwit rules.
This logic probably isn’t required any more since:
- Segwit activated at height 481824, when the block chain was 130GB and the total number of txs was 250 million. Today, we’re at height 667704, the blockchain is over 315GB and the total number of txs is over 600 million. Even if 20% of that added data is witness data (a high estimate), then around 150GB of transactions would need to be rewound to get back to segwit activation height. It’d probably be faster to simply validate from genesis, especially since we won’t be validating any scripts before the assumevalid block. It’s also unclear whether rewinding 150GB of transactions would even work. It’s certainly never been tested.
- Bitcoin Core v0.13 is hardly used any more. https://luke.dashjr.org/programs/bitcoin/files/charts/software.html shows less than 50 nodes running it. The software was EOL on Aug 1st 2018. It’s very unlikely that anyone is running 0.13 and will want to upgrade to 0.22.
This PR introduces NeedsRedownload()
which merely checks for insufficiently validated segwit blocks and requests that the user restarts the node with -reindex
. Reindexing the block files upon restart will make the node rebuild chain state and block index from the blk*.dat
files on disk. The node won’t be able to index the blocks with BLOCK_OPT_WITNESS
, so they will be missing from the chain and be re-downloaded, with witness data.
Removing this code allows the following (done in follow-up #21090):
- removal of tests using
segwitheight=-1
inp2p_segwit.py
. - in turn, that allows us to drop support for
-segwitheight=-1
, which is only supported for that test. - that allows us to always set
NODE_WITNESS
in our local services. The only reason we don’t do that is to support-segwitheight=-1
. - that in turn allows us to drop all of the
GetLocalServices() & NODE_WITNESS
checks insidenet_processing.cpp
, since our local services would always includeNODE_WITNESS