From cb580c72413c42ee26398f98fd553a8b4cd7f64e Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sun, 4 Feb 2018 00:35:41 +0000 Subject: [PATCH] Add rollback limit to block index rewinding This will prevent users from starting their nodes if they switch between software versions that implement different network upgrades. It will also prevent users from using the testnet if they have more than MAX_REORG_LIMIT post-upgrade blocks, and the upgrade point is shifted in a newer software version. --- src/main.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 755b58b8a..43aac0d18 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3826,6 +3826,26 @@ bool RewindBlockIndex(const CChainParams& params) } // nHeight is now the height of the first insufficiently-validated block, or tipheight + 1 + auto rewindLength = chainActive.Height() - nHeight; + if (rewindLength > 0 && rewindLength > MAX_REORG_LENGTH) { + auto pindexOldTip = chainActive.Tip(); + auto pindexRewind = chainActive[nHeight - 1]; + auto msg = strprintf(_( + "A block chain rewind has been detected that would roll back %d blocks! " + "This is larger than the maximum of %d blocks, and so the node is shutting down for your safety." + ), rewindLength, MAX_REORG_LENGTH) + "\n\n" + + _("Rewind details") + ":\n" + + "- " + strprintf(_("Current tip: %s, height %d"), + pindexOldTip->phashBlock->GetHex(), pindexOldTip->nHeight) + "\n" + + "- " + strprintf(_("Rewinding to: %s, height %d"), + pindexRewind->phashBlock->GetHex(), pindexRewind->nHeight) + "\n\n" + + _("Please help, human!"); + LogPrintf("*** %s\n", msg); + uiInterface.ThreadSafeMessageBox(msg, "", CClientUIInterface::MSG_ERROR); + StartShutdown(); + return false; + } + CValidationState state; CBlockIndex* pindex = chainActive.Tip(); while (chainActive.Height() >= nHeight) {