Skip to content

Commit 85eb2ce

Browse files
committed
Do not use the redundant BestInvalidWork record in the block database.
As block index entries have a flag for marking invalid blocks, the 'best invalid work' information can be derived from there. In addition, remove the global from main.h
1 parent ede3ee3 commit 85eb2ce

File tree

4 files changed

+13
-21
lines changed

4 files changed

+13
-21
lines changed

src/main.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ unsigned int nTransactionsUpdated = 0;
3333

3434
map<uint256, CBlockIndex*> mapBlockIndex;
3535
CChain chainActive;
36-
uint256 nBestInvalidWork = 0;
36+
CBlockIndex *pindexBestInvalid;
3737
set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexValid; // may contain all CBlockIndex*'s that have validness >=BLOCK_VALID_TRANSACTIONS, and must contain those who aren't failed
3838
int64 nTimeBestReceived = 0;
3939
int nScriptCheckThreads = 0;
@@ -1349,7 +1349,7 @@ void CheckForkWarningConditions()
13491349
if (pindexBestForkTip && chainActive.Height() - pindexBestForkTip->nHeight >= 72)
13501350
pindexBestForkTip = NULL;
13511351

1352-
if (pindexBestForkTip || nBestInvalidWork > chainActive.Tip()->nChainWork + (chainActive.Tip()->GetBlockWork() * 6).getuint256())
1352+
if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (chainActive.Tip()->GetBlockWork() * 6).getuint256()))
13531353
{
13541354
if (!fLargeWorkForkFound)
13551355
{
@@ -1416,10 +1416,13 @@ void CheckForkWarningConditionsOnNewFork(CBlockIndex* pindexNewForkTip)
14161416

14171417
void static InvalidChainFound(CBlockIndex* pindexNew)
14181418
{
1419-
if (pindexNew->nChainWork > nBestInvalidWork)
1419+
if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork)
14201420
{
1421-
nBestInvalidWork = pindexNew->nChainWork;
1422-
pblocktree->WriteBestInvalidWork(CBigNum(nBestInvalidWork));
1421+
pindexBestInvalid = pindexNew;
1422+
// The current code doesn't actually read the BestInvalidWork entry in
1423+
// the block database anymore, as it is derived from the flags in block
1424+
// index entry. We only write it for backward compatibility.
1425+
pblocktree->WriteBestInvalidWork(CBigNum(pindexBestInvalid->nChainWork));
14231426
uiInterface.NotifyBlocksChanged();
14241427
}
14251428
LogPrintf("InvalidChainFound: invalid block=%s height=%d log2_work=%.8g date=%s\n",
@@ -2769,6 +2772,8 @@ bool static LoadBlockIndexDB()
27692772
pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx;
27702773
if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS && !(pindex->nStatus & BLOCK_FAILED_MASK))
27712774
setBlockIndexValid.insert(pindex);
2775+
if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
2776+
pindexBestInvalid = pindex;
27722777
}
27732778

27742779
// Load block file info
@@ -2777,11 +2782,6 @@ bool static LoadBlockIndexDB()
27772782
if (pblocktree->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile))
27782783
LogPrintf("LoadBlockIndexDB(): last block file info: %s\n", infoLastBlockFile.ToString().c_str());
27792784

2780-
// Load nBestInvalidWork, OK if it doesn't exist
2781-
CBigNum bnBestInvalidWork;
2782-
pblocktree->ReadBestInvalidWork(bnBestInvalidWork);
2783-
nBestInvalidWork = bnBestInvalidWork.getuint256();
2784-
27852785
// Check whether we need to continue reindexing
27862786
bool fReindexing = false;
27872787
pblocktree->ReadReindexing(fReindexing);
@@ -2791,12 +2791,10 @@ bool static LoadBlockIndexDB()
27912791
pblocktree->ReadFlag("txindex", fTxIndex);
27922792
LogPrintf("LoadBlockIndexDB(): transaction index %s\n", fTxIndex ? "enabled" : "disabled");
27932793

2794-
// Load hashBestChain pointer to end of best chain
2794+
// Load pointer to end of best chain
27952795
chainActive.SetTip(pcoinsTip->GetBestBlock());
27962796
if (chainActive.Tip() == NULL)
27972797
return true;
2798-
2799-
// register best chain
28002798
LogPrintf("LoadBlockIndexDB(): hashBestChain=%s height=%d date=%s\n",
28012799
chainActive.Tip()->GetBlockHash().ToString().c_str(), chainActive.Height(),
28022800
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()).c_str());
@@ -2882,7 +2880,7 @@ void UnloadBlockIndex()
28822880
mapBlockIndex.clear();
28832881
setBlockIndexValid.clear();
28842882
chainActive.SetTip(NULL);
2885-
nBestInvalidWork = 0;
2883+
pindexBestInvalid = NULL;
28862884
}
28872885

28882886
bool LoadBlockIndex()

src/main.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ extern CScript COINBASE_FLAGS;
7474
extern CCriticalSection cs_main;
7575
extern std::map<uint256, CBlockIndex*> mapBlockIndex;
7676
extern std::set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexValid;
77-
extern uint256 nBestInvalidWork;
7877
extern unsigned int nTransactionsUpdated;
7978
extern uint64 nLastBlockTx;
8079
extern uint64 nLastBlockSize;

src/txdb.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,9 @@ bool CBlockTreeDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
7474
return Write(make_pair('b', blockindex.GetBlockHash()), blockindex);
7575
}
7676

77-
bool CBlockTreeDB::ReadBestInvalidWork(CBigNum& bnBestInvalidWork)
78-
{
79-
return Read('I', bnBestInvalidWork);
80-
}
81-
8277
bool CBlockTreeDB::WriteBestInvalidWork(const CBigNum& bnBestInvalidWork)
8378
{
79+
// Obsolete; only written for backward compatibility.
8480
return Write('I', bnBestInvalidWork);
8581
}
8682

src/txdb.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class CBlockTreeDB : public CLevelDB
3535
void operator=(const CBlockTreeDB&);
3636
public:
3737
bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
38-
bool ReadBestInvalidWork(CBigNum& bnBestInvalidWork);
3938
bool WriteBestInvalidWork(const CBigNum& bnBestInvalidWork);
4039
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo);
4140
bool WriteBlockFileInfo(int nFile, const CBlockFileInfo &fileinfo);

0 commit comments

Comments
 (0)