@@ -1312,8 +1312,6 @@ bool CChainState::IsInitialBlockDownload() const
1312
1312
return false ;
1313
1313
}
1314
1314
1315
- static CBlockIndex *pindexBestForkTip = nullptr , *pindexBestForkBase = nullptr ;
1316
-
1317
1315
static void AlertNotify (const std::string& strMessage)
1318
1316
{
1319
1317
uiInterface.NotifyAlertChanged ();
@@ -1339,73 +1337,16 @@ static void CheckForkWarningConditions() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1339
1337
AssertLockHeld (cs_main);
1340
1338
// Before we get past initial download, we cannot reliably alert about forks
1341
1339
// (we assume we don't get stuck on a fork before finishing our initial sync)
1342
- if (::ChainstateActive ().IsInitialBlockDownload ())
1340
+ if (::ChainstateActive ().IsInitialBlockDownload ()) {
1343
1341
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).\n Chain 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.\n Chain state database corruption likely.\n " , __func__);
1368
- SetfLargeWorkInvalidChainFound (true );
1369
- }
1370
1342
}
1371
- else
1372
- {
1373
- SetfLargeWorkForkFound (false );
1374
- SetfLargeWorkInvalidChainFound (false );
1375
- }
1376
- }
1377
1343
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.\n Chain state database corruption likely.\n " , __func__);
1346
+ SetfLargeWorkInvalidChainFound (true );
1347
+ } else {
1348
+ SetfLargeWorkInvalidChainFound (false );
1406
1349
}
1407
-
1408
- CheckForkWarningConditions ();
1409
1350
}
1410
1351
1411
1352
// Called both upon regular invalid block discovery *and* InvalidateBlock
@@ -2746,8 +2687,8 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
2746
2687
AssertLockHeld (cs_main);
2747
2688
AssertLockHeld (m_mempool.cs );
2748
2689
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);
2751
2692
2752
2693
// Disconnect active blocks which are no longer in the best chain.
2753
2694
bool fBlocksDisconnected = false ;
@@ -2767,7 +2708,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
2767
2708
fBlocksDisconnected = true ;
2768
2709
}
2769
2710
2770
- // Build list of new blocks to connect.
2711
+ // Build list of new blocks to connect (in descending height order) .
2771
2712
std::vector<CBlockIndex*> vpindexToConnect;
2772
2713
bool fContinue = true ;
2773
2714
int nHeight = pindexFork ? pindexFork->nHeight : -1 ;
@@ -2777,15 +2718,15 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
2777
2718
int nTargetHeight = std::min (nHeight + 32 , pindexMostWork->nHeight );
2778
2719
vpindexToConnect.clear ();
2779
2720
vpindexToConnect.reserve (nTargetHeight - nHeight);
2780
- CBlockIndex * pindexIter = pindexMostWork->GetAncestor (nTargetHeight);
2721
+ CBlockIndex* pindexIter = pindexMostWork->GetAncestor (nTargetHeight);
2781
2722
while (pindexIter && pindexIter->nHeight != nHeight) {
2782
2723
vpindexToConnect.push_back (pindexIter);
2783
2724
pindexIter = pindexIter->pprev ;
2784
2725
}
2785
2726
nHeight = nTargetHeight;
2786
2727
2787
2728
// Connect new blocks.
2788
- for (CBlockIndex * pindexConnect : reverse_iterate (vpindexToConnect)) {
2729
+ for (CBlockIndex* pindexConnect : reverse_iterate (vpindexToConnect)) {
2789
2730
if (!ConnectTip (state, chainparams, pindexConnect, pindexConnect == pindexMostWork ? pblock : std::shared_ptr<const CBlock>(), connectTrace, disconnectpool)) {
2790
2731
if (state.IsInvalid ()) {
2791
2732
// The block violates a consensus rule.
@@ -2821,11 +2762,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
2821
2762
}
2822
2763
m_mempool.check (&CoinsTip ());
2823
2764
2824
- // Callbacks/notifications for a new best chain.
2825
- if (fInvalidFound )
2826
- CheckForkWarningConditionsOnNewFork (vpindexToConnect.back ());
2827
- else
2828
- CheckForkWarningConditions ();
2765
+ CheckForkWarningConditions ();
2829
2766
2830
2767
return true ;
2831
2768
}
0 commit comments