diff --git a/src/main.cpp b/src/main.cpp index db0af063b..1b03f613f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4315,23 +4315,52 @@ static bool ActivateBestChainStep(bool fSkipdpow, CValidationState &state, CBloc assert(MAX_REORG_LENGTH > 0);//, "We must be able to reorg some distance"); if ( reorgLength > MAX_REORG_LENGTH) { + // A peer-delivered fork that would roll back more than MAX_REORG_LENGTH + // (-maxreorg, default 99) blocks. This used to StartShutdown() the whole + // process, which is a per-reachability remote DoS: the attacker's fork is + // re-pulled from addrman after every restart and re-kills the node. Instead + // refuse the fork, poison the attacker branch so it leaves + // setBlockIndexCandidates and is never re-selected or re-pulled, stay up on + // the current chain, and penalize the delivering peer (DoS(100); the default + // banscore 101 bans on the second such trip). This mirrors the + // past-notarized-height branch above, which already refuses a fork with + // InvalidateBlock + DoS(100) and keeps running. + // + // Invalidate the BASE of the attacker branch (the first block above the fork + // point), not just its tip: FindMostWorkChain's fFailedChain sweep then + // erases the whole branch from the candidate set in one pass, and + // AcceptBlockHeader rejects any future extension (pprev is BLOCK_FAILED_CHILD). + // The base is strictly on the fork side (height forkHeight+1), so it is never + // in chainActive and no honest block is disconnected or poisoned. + CBlockIndex *pindexBadBase = (pindexFork != 0) ? pindexMostWork->GetAncestor(pindexFork->GetHeight() + 1) : pindexMostWork; + if (pindexBadBase == 0) + pindexBadBase = pindexMostWork; + // Because the refusal is PERSISTED (BLOCK_FAILED on the base), raising + // -maxreorg alone does NOT make the node follow this fork after a trip -- + // recovery for a genuine deep reorg is an explicit `reconsiderblock ` + // (optionally with a raised -maxreorg so it isn't re-refused). Say so, and + // surface it in getinfo "errors" so a refused node is never silently stranded. auto msg = strprintf(_( - "A block chain reorganization 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." - ), reorgLength, MAX_REORG_LENGTH) + "\n\n" + + "A block chain reorganization that would roll back %d blocks (more than the maximum of %d) " + "was refused; staying on the current chain." + ), reorgLength, MAX_REORG_LENGTH) + "\n" + _("Reorganization details") + ":\n" + - "- " + strprintf(_("Current tip: %s, height %d, work %s\n"), - pindexOldTip->phashBlock->GetHex(), pindexOldTip->GetHeight(), pindexOldTip->chainPower.chainWork.GetHex()) + "\n" + - "- " + strprintf(_("New tip: %s, height %d, work %s\n"), + "- " + strprintf(_("Current tip: %s, height %d, work %s"), + pindexOldTip->phashBlock->GetHex(), pindexOldTip->GetHeight(), pindexOldTip->chainPower.chainWork.GetHex()) + "\n" + + "- " + strprintf(_("Rejected tip: %s, height %d, work %s"), pindexMostWork->phashBlock->GetHex(), pindexMostWork->GetHeight(), pindexMostWork->chainPower.chainWork.GetHex()) + "\n" + - "- " + strprintf(_("Fork point: %s %s, height %d"), - SMART_CHAIN_SYMBOL,pindexFork->phashBlock->GetHex(), pindexFork->GetHeight()) + "\n\n" + - _("Please help me, wise human!"); - LogPrintf("*** %s\nif you launch with -maxreorg=%d it might be able to resolve this automatically", msg,reorgLength+10); - fprintf(stderr,"*** %s\nif you launch with -maxreorg=%d it might be able to resolve this automatically", msg.c_str(),reorgLength+10); - uiInterface.ThreadSafeMessageBox(msg, "", CClientUIInterface::MSG_ERROR); - StartShutdown(); - return false; + "- " + strprintf(_("Fork point: %s %s, height %d"), + SMART_CHAIN_SYMBOL, pindexFork ? pindexFork->phashBlock->GetHex() : "(none)", pindexFork ? pindexFork->GetHeight() : -1) + "\n" + + strprintf(_("If this is a legitimate deep reorg, recover with: reconsiderblock %s (raise -maxreorg first if needed)"), + pindexBadBase->phashBlock->GetHex()); + LogPrintf("*** %s\n", msg); + fprintf(stderr,"*** %s\n", msg.c_str()); + strMiscWarning = msg; // surfaces in getinfo/getnetworkinfo (GetWarnings), not fatal + + CValidationState tmpstate; + InvalidateBlock(tmpstate, pindexBadBase); + return state.DoS(100, error("ActivateBestChainStep(): reorg of %d blocks exceeds maximum of %d; refusing fork and staying on current chain", + reorgLength, MAX_REORG_LENGTH), REJECT_INVALID, "oversized-reorg"); } // Disconnect active blocks which are no longer in the best chain.