diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -39,6 +39,7 @@ #include "warnings.h" #include +#include #include #include @@ -1138,6 +1139,27 @@ // sync) if (IsInitialBlockDownload()) return; + if (pindexBestForkTip || pindexBestForkBase || pindexBestInvalid) + LogPrintf("DEBUG CALIN - " + "pindexBestInvalid->nHeight=%d / " + "pindexBestForkTip->nHeight=%d / " + "chainActive.Height()=%d / " + "pindexBestForkBase->nHeight=%d / " + "(pibi->hash=%s pbft->hash=%s pbfb->hash=%s)\n", + pindexBestInvalid ? pindexBestInvalid->nHeight : 0, + pindexBestForkTip ? pindexBestForkTip->nHeight : 0, + chainActive.Height(), + pindexBestForkBase ? pindexBestForkBase->nHeight : 0, + pindexBestInvalid ? pindexBestInvalid->phashBlock->ToString() + : std::string(""), + pindexBestForkTip ? pindexBestForkTip->phashBlock->ToString() + : std::string(""), + pindexBestForkBase + ? pindexBestForkBase->phashBlock->ToString() + : std::string("") + + ); + // If our best fork is no longer within 72 blocks (+/- 12 hours if no one // mines it) of our head, drop it if (pindexBestForkTip && @@ -1149,10 +1171,11 @@ pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (GetBlockProof(*chainActive.Tip()) * 6))) { + if (!GetfLargeWorkForkFound() && pindexBestForkBase) { std::string warning = - std::string("'Warning: Large-work fork detected, forking after " - "block ") + + std::string("'Warning: Large-work fork detected, " + "forking after block ") + pindexBestForkBase->phashBlock->ToString() + std::string("'"); AlertNotify(warning); } @@ -1166,11 +1189,32 @@ pindexBestForkTip->phashBlock->ToString()); SetfLargeWorkForkFound(true); } else { - LogPrintf("%s: Warning: Found invalid chain at least ~6 blocks " - "longer than our best chain.\nChain state database " - "corruption likely.\n", - __func__); - SetfLargeWorkInvalidChainFound(true); + const CBlockIndex *forkBase = + pindexBestInvalid ? chainActive.FindFork(pindexBestInvalid) + : nullptr; + const CBlockIndex *chkPt = + Checkpoints::GetLastCheckpoint(Params().Checkpoints()); + + if (forkBase && chkPt && forkBase->nHeight < chkPt->nHeight) { + static std::set warnedForks; + + SetfLargeWorkInvalidChainFound(false); + if (!warnedForks.count(forkBase->nHeight)) { + LogPrintf("%s: Found a long fork chain at least ~6 blocks " + "longer than our best chain at height %d, " + "however since it is behind a checkpoint at %d, " + "this is the legacy chain (ignoring).\n", + __func__, forkBase->nHeight, chkPt->nHeight); + warnedForks.insert(forkBase->nHeight); + } + } else { + + LogPrintf("%s: Warning: Found invalid chain at least ~6 " + "blocks longer than our best chain.\n" + "Chain state database corruption likely.\n", + __func__); + SetfLargeWorkInvalidChainFound(true); + } } } else { SetfLargeWorkForkFound(false);