Skip to content

Commit 694c7c8

Browse files
committed
Merge pull request #3087
caca6aa Make some globals in main non-public. (Pieter Wuille) 85eb2ce Do not use the redundant BestInvalidWork record in the block database. (Pieter Wuille)
2 parents 8373698 + caca6aa commit 694c7c8

File tree

4 files changed

+33
-46
lines changed

4 files changed

+33
-46
lines changed

src/main.cpp

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ unsigned int nTransactionsUpdated = 0;
3333

3434
map<uint256, CBlockIndex*> mapBlockIndex;
3535
CChain chainActive;
36-
uint256 nBestInvalidWork = 0;
37-
set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexValid; // may contain all CBlockIndex*'s that have validness >=BLOCK_VALID_TRANSACTIONS, and must contain those who aren't failed
3836
int64 nTimeBestReceived = 0;
3937
int nScriptCheckThreads = 0;
4038
bool fImporting = false;
@@ -65,7 +63,28 @@ const string strMessageMagic = "Bitcoin Signed Message:\n";
6563
// Settings
6664
int64 nTransactionFee = 0;
6765

66+
// Internal stuff
67+
namespace {
68+
struct CBlockIndexWorkComparator
69+
{
70+
bool operator()(CBlockIndex *pa, CBlockIndex *pb) {
71+
if (pa->nChainWork > pb->nChainWork) return false;
72+
if (pa->nChainWork < pb->nChainWork) return true;
6873

74+
if (pa->GetBlockHash() < pb->GetBlockHash()) return false;
75+
if (pa->GetBlockHash() > pb->GetBlockHash()) return true;
76+
77+
return false; // identical blocks
78+
}
79+
};
80+
81+
CBlockIndex *pindexBestInvalid;
82+
set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexValid; // may contain all CBlockIndex*'s that have validness >=BLOCK_VALID_TRANSACTIONS, and must contain those who aren't failed
83+
84+
CCriticalSection cs_LastBlockFile;
85+
CBlockFileInfo infoLastBlockFile;
86+
int nLastBlockFile = 0;
87+
}
6988

7089
//////////////////////////////////////////////////////////////////////////////
7190
//
@@ -1349,7 +1368,7 @@ void CheckForkWarningConditions()
13491368
if (pindexBestForkTip && chainActive.Height() - pindexBestForkTip->nHeight >= 72)
13501369
pindexBestForkTip = NULL;
13511370

1352-
if (pindexBestForkTip || nBestInvalidWork > chainActive.Tip()->nChainWork + (chainActive.Tip()->GetBlockWork() * 6).getuint256())
1371+
if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (chainActive.Tip()->GetBlockWork() * 6).getuint256()))
13531372
{
13541373
if (!fLargeWorkForkFound)
13551374
{
@@ -1416,10 +1435,13 @@ void CheckForkWarningConditionsOnNewFork(CBlockIndex* pindexNewForkTip)
14161435

14171436
void static InvalidChainFound(CBlockIndex* pindexNew)
14181437
{
1419-
if (pindexNew->nChainWork > nBestInvalidWork)
1438+
if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork)
14201439
{
1421-
nBestInvalidWork = pindexNew->nChainWork;
1422-
pblocktree->WriteBestInvalidWork(CBigNum(nBestInvalidWork));
1440+
pindexBestInvalid = pindexNew;
1441+
// The current code doesn't actually read the BestInvalidWork entry in
1442+
// the block database anymore, as it is derived from the flags in block
1443+
// index entry. We only write it for backward compatibility.
1444+
pblocktree->WriteBestInvalidWork(CBigNum(pindexBestInvalid->nChainWork));
14231445
uiInterface.NotifyBlocksChanged();
14241446
}
14251447
LogPrintf("InvalidChainFound: invalid block=%s height=%d log2_work=%.8g date=%s\n",
@@ -2691,10 +2713,6 @@ bool CheckDiskSpace(uint64 nAdditionalBytes)
26912713
return true;
26922714
}
26932715

2694-
CCriticalSection cs_LastBlockFile;
2695-
CBlockFileInfo infoLastBlockFile;
2696-
int nLastBlockFile = 0;
2697-
26982716
FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
26992717
{
27002718
if (pos.IsNull())
@@ -2769,6 +2787,8 @@ bool static LoadBlockIndexDB()
27692787
pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx;
27702788
if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS && !(pindex->nStatus & BLOCK_FAILED_MASK))
27712789
setBlockIndexValid.insert(pindex);
2790+
if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
2791+
pindexBestInvalid = pindex;
27722792
}
27732793

27742794
// Load block file info
@@ -2777,11 +2797,6 @@ bool static LoadBlockIndexDB()
27772797
if (pblocktree->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile))
27782798
LogPrintf("LoadBlockIndexDB(): last block file info: %s\n", infoLastBlockFile.ToString().c_str());
27792799

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

2794-
// Load hashBestChain pointer to end of best chain
2809+
// Load pointer to end of best chain
27952810
chainActive.SetTip(pcoinsTip->GetBestBlock());
27962811
if (chainActive.Tip() == NULL)
27972812
return true;
2798-
2799-
// register best chain
28002813
LogPrintf("LoadBlockIndexDB(): hashBestChain=%s height=%d date=%s\n",
28012814
chainActive.Tip()->GetBlockHash().ToString().c_str(), chainActive.Height(),
28022815
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()).c_str());
@@ -2882,7 +2895,7 @@ void UnloadBlockIndex()
28822895
mapBlockIndex.clear();
28832896
setBlockIndexValid.clear();
28842897
chainActive.SetTip(NULL);
2885-
nBestInvalidWork = 0;
2898+
pindexBestInvalid = NULL;
28862899
}
28872900

28882901
bool LoadBlockIndex()

src/main.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ class CAddress;
2626
class CInv;
2727
class CNode;
2828

29-
struct CBlockIndexWorkComparator;
30-
3129
/** The maximum allowed size for a serialized block, in bytes (network rule) */
3230
static const unsigned int MAX_BLOCK_SIZE = 1000000;
3331
/** The maximum size for mined blocks */
@@ -73,8 +71,6 @@ extern CScript COINBASE_FLAGS;
7371

7472
extern CCriticalSection cs_main;
7573
extern std::map<uint256, CBlockIndex*> mapBlockIndex;
76-
extern std::set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexValid;
77-
extern uint256 nBestInvalidWork;
7874
extern unsigned int nTransactionsUpdated;
7975
extern uint64 nLastBlockTx;
8076
extern uint64 nLastBlockSize;
@@ -647,10 +643,6 @@ class CBlockFileInfo
647643
}
648644
};
649645

650-
extern CCriticalSection cs_LastBlockFile;
651-
extern CBlockFileInfo infoLastBlockFile;
652-
extern int nLastBlockFile;
653-
654646
enum BlockStatus {
655647
BLOCK_VALID_UNKNOWN = 0,
656648
BLOCK_VALID_HEADER = 1, // parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future
@@ -850,19 +842,6 @@ class CBlockIndex
850842
}
851843
};
852844

853-
struct CBlockIndexWorkComparator
854-
{
855-
bool operator()(CBlockIndex *pa, CBlockIndex *pb) {
856-
if (pa->nChainWork > pb->nChainWork) return false;
857-
if (pa->nChainWork < pb->nChainWork) return true;
858-
859-
if (pa->GetBlockHash() < pb->GetBlockHash()) return false;
860-
if (pa->GetBlockHash() > pb->GetBlockHash()) return true;
861-
862-
return false; // identical blocks
863-
}
864-
};
865-
866845

867846

868847
/** Used to marshal pointers into hashes for db storage. */

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)