Skip to content

Commit a64ff1c

Browse files
author
MarcoFalke
committed
Merge #19905: Remove dead CheckForkWarningConditionsOnNewFork
fa7eed5 doc: Clarify that vpindexToConnect is in reverse order (MarcoFalke) fa62304 Remove dead CheckForkWarningConditionsOnNewFork (MarcoFalke) Pull request description: The function has several code and logic bugs, which prevent it from working at all: * `vpindexToConnect.back()` is passed to `CheckForkWarningConditionsOnNewFork`, which is the earliest connected block (least work block), *not* the new fork tip * `ActivateBestChainStep` will never try to connect a block that descends from an invalid block, so the invalid fork will only ever be of height 1, never hitting the 7 block minimum condition Instead of dragging the dead and wrong code around through every change in validation, remove it. In the future it could make sense to add a fork detection somewhere outside of the `ActivateBestChainStep` logic (maybe net_processing). ACKs for top commit: jnewbery: utACK fa7eed5 fjahr: Code review ACK fa7eed5 glozow: utACK bitcoin/bitcoin@fa7eed5 I see that it's dead code Tree-SHA512: 815bdbac7c1eb5b7594b0866a2dbd3c7619797afaadb03a5269fb96739ffb83b05b8e4f7c1e68d48d7886132dd0b12c14c3fb4ee0e72de1074726050ed203e1a
2 parents 54532f4 + fa7eed5 commit a64ff1c

File tree

3 files changed

+13
-94
lines changed

3 files changed

+13
-94
lines changed

src/validation.cpp

Lines changed: 12 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,8 +1312,6 @@ bool CChainState::IsInitialBlockDownload() const
13121312
return false;
13131313
}
13141314

1315-
static CBlockIndex *pindexBestForkTip = nullptr, *pindexBestForkBase = nullptr;
1316-
13171315
static void AlertNotify(const std::string& strMessage)
13181316
{
13191317
uiInterface.NotifyAlertChanged();
@@ -1339,73 +1337,16 @@ static void CheckForkWarningConditions() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
13391337
AssertLockHeld(cs_main);
13401338
// Before we get past initial download, we cannot reliably alert about forks
13411339
// (we assume we don't get stuck on a fork before finishing our initial sync)
1342-
if (::ChainstateActive().IsInitialBlockDownload())
1340+
if (::ChainstateActive().IsInitialBlockDownload()) {
13431341
return;
1344-
1345-
// If our best fork is no longer within 72 blocks (+/- 12 hours if no one mines it)
1346-
// of our head, drop it
1347-
if (pindexBestForkTip && ::ChainActive().Height() - pindexBestForkTip->nHeight >= 72)
1348-
pindexBestForkTip = nullptr;
1349-
1350-
if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > ::ChainActive().Tip()->nChainWork + (GetBlockProof(*::ChainActive().Tip()) * 6)))
1351-
{
1352-
if (!GetfLargeWorkForkFound() && pindexBestForkBase)
1353-
{
1354-
std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
1355-
pindexBestForkBase->phashBlock->ToString() + std::string("'");
1356-
AlertNotify(warning);
1357-
}
1358-
if (pindexBestForkTip && pindexBestForkBase)
1359-
{
1360-
LogPrintf("%s: Warning: Large valid fork found\n forking the chain at height %d (%s)\n lasting to height %d (%s).\nChain state database corruption likely.\n", __func__,
1361-
pindexBestForkBase->nHeight, pindexBestForkBase->phashBlock->ToString(),
1362-
pindexBestForkTip->nHeight, pindexBestForkTip->phashBlock->ToString());
1363-
SetfLargeWorkForkFound(true);
1364-
}
1365-
else
1366-
{
1367-
LogPrintf("%s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n", __func__);
1368-
SetfLargeWorkInvalidChainFound(true);
1369-
}
13701342
}
1371-
else
1372-
{
1373-
SetfLargeWorkForkFound(false);
1374-
SetfLargeWorkInvalidChainFound(false);
1375-
}
1376-
}
13771343

1378-
static void CheckForkWarningConditionsOnNewFork(CBlockIndex* pindexNewForkTip) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1379-
{
1380-
AssertLockHeld(cs_main);
1381-
// If we are on a fork that is sufficiently large, set a warning flag
1382-
CBlockIndex* pfork = pindexNewForkTip;
1383-
CBlockIndex* plonger = ::ChainActive().Tip();
1384-
while (pfork && pfork != plonger)
1385-
{
1386-
while (plonger && plonger->nHeight > pfork->nHeight)
1387-
plonger = plonger->pprev;
1388-
if (pfork == plonger)
1389-
break;
1390-
pfork = pfork->pprev;
1391-
}
1392-
1393-
// We define a condition where we should warn the user about as a fork of at least 7 blocks
1394-
// with a tip within 72 blocks (+/- 12 hours if no one mines it) of ours
1395-
// We use 7 blocks rather arbitrarily as it represents just under 10% of sustained network
1396-
// hash rate operating on the fork.
1397-
// or a chain that is entirely longer than ours and invalid (note that this should be detected by both)
1398-
// We define it this way because it allows us to only store the highest fork tip (+ base) which meets
1399-
// the 7-block condition and from this always have the most-likely-to-cause-warning fork
1400-
if (pfork && (!pindexBestForkTip || pindexNewForkTip->nHeight > pindexBestForkTip->nHeight) &&
1401-
pindexNewForkTip->nChainWork - pfork->nChainWork > (GetBlockProof(*pfork) * 7) &&
1402-
::ChainActive().Height() - pindexNewForkTip->nHeight < 72)
1403-
{
1404-
pindexBestForkTip = pindexNewForkTip;
1405-
pindexBestForkBase = pfork;
1344+
if (pindexBestInvalid && pindexBestInvalid->nChainWork > ::ChainActive().Tip()->nChainWork + (GetBlockProof(*::ChainActive().Tip()) * 6)) {
1345+
LogPrintf("%s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n", __func__);
1346+
SetfLargeWorkInvalidChainFound(true);
1347+
} else {
1348+
SetfLargeWorkInvalidChainFound(false);
14061349
}
1407-
1408-
CheckForkWarningConditions();
14091350
}
14101351

14111352
// Called both upon regular invalid block discovery *and* InvalidateBlock
@@ -2746,8 +2687,8 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
27462687
AssertLockHeld(cs_main);
27472688
AssertLockHeld(m_mempool.cs);
27482689

2749-
const CBlockIndex *pindexOldTip = m_chain.Tip();
2750-
const CBlockIndex *pindexFork = m_chain.FindFork(pindexMostWork);
2690+
const CBlockIndex* pindexOldTip = m_chain.Tip();
2691+
const CBlockIndex* pindexFork = m_chain.FindFork(pindexMostWork);
27512692

27522693
// Disconnect active blocks which are no longer in the best chain.
27532694
bool fBlocksDisconnected = false;
@@ -2767,7 +2708,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
27672708
fBlocksDisconnected = true;
27682709
}
27692710

2770-
// Build list of new blocks to connect.
2711+
// Build list of new blocks to connect (in descending height order).
27712712
std::vector<CBlockIndex*> vpindexToConnect;
27722713
bool fContinue = true;
27732714
int nHeight = pindexFork ? pindexFork->nHeight : -1;
@@ -2777,15 +2718,15 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
27772718
int nTargetHeight = std::min(nHeight + 32, pindexMostWork->nHeight);
27782719
vpindexToConnect.clear();
27792720
vpindexToConnect.reserve(nTargetHeight - nHeight);
2780-
CBlockIndex *pindexIter = pindexMostWork->GetAncestor(nTargetHeight);
2721+
CBlockIndex* pindexIter = pindexMostWork->GetAncestor(nTargetHeight);
27812722
while (pindexIter && pindexIter->nHeight != nHeight) {
27822723
vpindexToConnect.push_back(pindexIter);
27832724
pindexIter = pindexIter->pprev;
27842725
}
27852726
nHeight = nTargetHeight;
27862727

27872728
// Connect new blocks.
2788-
for (CBlockIndex *pindexConnect : reverse_iterate(vpindexToConnect)) {
2729+
for (CBlockIndex* pindexConnect : reverse_iterate(vpindexToConnect)) {
27892730
if (!ConnectTip(state, chainparams, pindexConnect, pindexConnect == pindexMostWork ? pblock : std::shared_ptr<const CBlock>(), connectTrace, disconnectpool)) {
27902731
if (state.IsInvalid()) {
27912732
// The block violates a consensus rule.
@@ -2821,11 +2762,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
28212762
}
28222763
m_mempool.check(&CoinsTip());
28232764

2824-
// Callbacks/notifications for a new best chain.
2825-
if (fInvalidFound)
2826-
CheckForkWarningConditionsOnNewFork(vpindexToConnect.back());
2827-
else
2828-
CheckForkWarningConditions();
2765+
CheckForkWarningConditions();
28292766

28302767
return true;
28312768
}

src/warnings.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
static Mutex g_warnings_mutex;
1616
static bilingual_str g_misc_warnings GUARDED_BY(g_warnings_mutex);
17-
static bool fLargeWorkForkFound GUARDED_BY(g_warnings_mutex) = false;
1817
static bool fLargeWorkInvalidChainFound GUARDED_BY(g_warnings_mutex) = false;
1918

2019
void SetMiscWarning(const bilingual_str& warning)
@@ -23,18 +22,6 @@ void SetMiscWarning(const bilingual_str& warning)
2322
g_misc_warnings = warning;
2423
}
2524

26-
void SetfLargeWorkForkFound(bool flag)
27-
{
28-
LOCK(g_warnings_mutex);
29-
fLargeWorkForkFound = flag;
30-
}
31-
32-
bool GetfLargeWorkForkFound()
33-
{
34-
LOCK(g_warnings_mutex);
35-
return fLargeWorkForkFound;
36-
}
37-
3825
void SetfLargeWorkInvalidChainFound(bool flag)
3926
{
4027
LOCK(g_warnings_mutex);
@@ -60,10 +47,7 @@ bilingual_str GetWarnings(bool verbose)
6047
warnings_verbose.emplace_back(warnings_concise);
6148
}
6249

63-
if (fLargeWorkForkFound) {
64-
warnings_concise = _("Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues.");
65-
warnings_verbose.emplace_back(warnings_concise);
66-
} else if (fLargeWorkInvalidChainFound) {
50+
if (fLargeWorkInvalidChainFound) {
6751
warnings_concise = _("Warning: We do not appear to fully agree with our peers! You may need to upgrade, or other nodes may need to upgrade.");
6852
warnings_verbose.emplace_back(warnings_concise);
6953
}

src/warnings.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
struct bilingual_str;
1212

1313
void SetMiscWarning(const bilingual_str& warning);
14-
void SetfLargeWorkForkFound(bool flag);
15-
bool GetfLargeWorkForkFound();
1614
void SetfLargeWorkInvalidChainFound(bool flag);
1715
/** Format a string that describes several potential problems detected by the core.
1816
* @param[in] verbose bool

0 commit comments

Comments
 (0)