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)
This commit is contained in:
Pieter Wuille 2013-11-02 15:10:32 +01:00
commit 694c7c8161
No known key found for this signature in database
GPG Key ID: 8F653255C87992E0
4 changed files with 33 additions and 46 deletions

View File

@ -33,8 +33,6 @@ unsigned int nTransactionsUpdated = 0;
map<uint256, CBlockIndex*> mapBlockIndex; map<uint256, CBlockIndex*> mapBlockIndex;
CChain chainActive; CChain chainActive;
uint256 nBestInvalidWork = 0;
set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexValid; // may contain all CBlockIndex*'s that have validness >=BLOCK_VALID_TRANSACTIONS, and must contain those who aren't failed
int64 nTimeBestReceived = 0; int64 nTimeBestReceived = 0;
int nScriptCheckThreads = 0; int nScriptCheckThreads = 0;
bool fImporting = false; bool fImporting = false;
@ -65,7 +63,28 @@ const string strMessageMagic = "Bitcoin Signed Message:\n";
// Settings // Settings
int64 nTransactionFee = 0; int64 nTransactionFee = 0;
// Internal stuff
namespace {
struct CBlockIndexWorkComparator
{
bool operator()(CBlockIndex *pa, CBlockIndex *pb) {
if (pa->nChainWork > pb->nChainWork) return false;
if (pa->nChainWork < pb->nChainWork) return true;
if (pa->GetBlockHash() < pb->GetBlockHash()) return false;
if (pa->GetBlockHash() > pb->GetBlockHash()) return true;
return false; // identical blocks
}
};
CBlockIndex *pindexBestInvalid;
set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexValid; // may contain all CBlockIndex*'s that have validness >=BLOCK_VALID_TRANSACTIONS, and must contain those who aren't failed
CCriticalSection cs_LastBlockFile;
CBlockFileInfo infoLastBlockFile;
int nLastBlockFile = 0;
}
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// //
@ -1349,7 +1368,7 @@ void CheckForkWarningConditions()
if (pindexBestForkTip && chainActive.Height() - pindexBestForkTip->nHeight >= 72) if (pindexBestForkTip && chainActive.Height() - pindexBestForkTip->nHeight >= 72)
pindexBestForkTip = NULL; pindexBestForkTip = NULL;
if (pindexBestForkTip || nBestInvalidWork > chainActive.Tip()->nChainWork + (chainActive.Tip()->GetBlockWork() * 6).getuint256()) if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (chainActive.Tip()->GetBlockWork() * 6).getuint256()))
{ {
if (!fLargeWorkForkFound) if (!fLargeWorkForkFound)
{ {
@ -1416,10 +1435,13 @@ void CheckForkWarningConditionsOnNewFork(CBlockIndex* pindexNewForkTip)
void static InvalidChainFound(CBlockIndex* pindexNew) void static InvalidChainFound(CBlockIndex* pindexNew)
{ {
if (pindexNew->nChainWork > nBestInvalidWork) if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork)
{ {
nBestInvalidWork = pindexNew->nChainWork; pindexBestInvalid = pindexNew;
pblocktree->WriteBestInvalidWork(CBigNum(nBestInvalidWork)); // The current code doesn't actually read the BestInvalidWork entry in
// the block database anymore, as it is derived from the flags in block
// index entry. We only write it for backward compatibility.
pblocktree->WriteBestInvalidWork(CBigNum(pindexBestInvalid->nChainWork));
uiInterface.NotifyBlocksChanged(); uiInterface.NotifyBlocksChanged();
} }
LogPrintf("InvalidChainFound: invalid block=%s height=%d log2_work=%.8g date=%s\n", LogPrintf("InvalidChainFound: invalid block=%s height=%d log2_work=%.8g date=%s\n",
@ -2691,10 +2713,6 @@ bool CheckDiskSpace(uint64 nAdditionalBytes)
return true; return true;
} }
CCriticalSection cs_LastBlockFile;
CBlockFileInfo infoLastBlockFile;
int nLastBlockFile = 0;
FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly) FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
{ {
if (pos.IsNull()) if (pos.IsNull())
@ -2769,6 +2787,8 @@ bool static LoadBlockIndexDB()
pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx; pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx;
if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS && !(pindex->nStatus & BLOCK_FAILED_MASK)) if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS && !(pindex->nStatus & BLOCK_FAILED_MASK))
setBlockIndexValid.insert(pindex); setBlockIndexValid.insert(pindex);
if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
pindexBestInvalid = pindex;
} }
// Load block file info // Load block file info
@ -2777,11 +2797,6 @@ bool static LoadBlockIndexDB()
if (pblocktree->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile)) if (pblocktree->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile))
LogPrintf("LoadBlockIndexDB(): last block file info: %s\n", infoLastBlockFile.ToString().c_str()); LogPrintf("LoadBlockIndexDB(): last block file info: %s\n", infoLastBlockFile.ToString().c_str());
// Load nBestInvalidWork, OK if it doesn't exist
CBigNum bnBestInvalidWork;
pblocktree->ReadBestInvalidWork(bnBestInvalidWork);
nBestInvalidWork = bnBestInvalidWork.getuint256();
// Check whether we need to continue reindexing // Check whether we need to continue reindexing
bool fReindexing = false; bool fReindexing = false;
pblocktree->ReadReindexing(fReindexing); pblocktree->ReadReindexing(fReindexing);
@ -2791,12 +2806,10 @@ bool static LoadBlockIndexDB()
pblocktree->ReadFlag("txindex", fTxIndex); pblocktree->ReadFlag("txindex", fTxIndex);
LogPrintf("LoadBlockIndexDB(): transaction index %s\n", fTxIndex ? "enabled" : "disabled"); LogPrintf("LoadBlockIndexDB(): transaction index %s\n", fTxIndex ? "enabled" : "disabled");
// Load hashBestChain pointer to end of best chain // Load pointer to end of best chain
chainActive.SetTip(pcoinsTip->GetBestBlock()); chainActive.SetTip(pcoinsTip->GetBestBlock());
if (chainActive.Tip() == NULL) if (chainActive.Tip() == NULL)
return true; return true;
// register best chain
LogPrintf("LoadBlockIndexDB(): hashBestChain=%s height=%d date=%s\n", LogPrintf("LoadBlockIndexDB(): hashBestChain=%s height=%d date=%s\n",
chainActive.Tip()->GetBlockHash().ToString().c_str(), chainActive.Height(), chainActive.Tip()->GetBlockHash().ToString().c_str(), chainActive.Height(),
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()).c_str()); DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()).c_str());
@ -2882,7 +2895,7 @@ void UnloadBlockIndex()
mapBlockIndex.clear(); mapBlockIndex.clear();
setBlockIndexValid.clear(); setBlockIndexValid.clear();
chainActive.SetTip(NULL); chainActive.SetTip(NULL);
nBestInvalidWork = 0; pindexBestInvalid = NULL;
} }
bool LoadBlockIndex() bool LoadBlockIndex()

View File

@ -26,8 +26,6 @@ class CAddress;
class CInv; class CInv;
class CNode; class CNode;
struct CBlockIndexWorkComparator;
/** The maximum allowed size for a serialized block, in bytes (network rule) */ /** The maximum allowed size for a serialized block, in bytes (network rule) */
static const unsigned int MAX_BLOCK_SIZE = 1000000; static const unsigned int MAX_BLOCK_SIZE = 1000000;
/** The maximum size for mined blocks */ /** The maximum size for mined blocks */
@ -73,8 +71,6 @@ extern CScript COINBASE_FLAGS;
extern CCriticalSection cs_main; extern CCriticalSection cs_main;
extern std::map<uint256, CBlockIndex*> mapBlockIndex; extern std::map<uint256, CBlockIndex*> mapBlockIndex;
extern std::set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexValid;
extern uint256 nBestInvalidWork;
extern unsigned int nTransactionsUpdated; extern unsigned int nTransactionsUpdated;
extern uint64 nLastBlockTx; extern uint64 nLastBlockTx;
extern uint64 nLastBlockSize; extern uint64 nLastBlockSize;
@ -647,10 +643,6 @@ public:
} }
}; };
extern CCriticalSection cs_LastBlockFile;
extern CBlockFileInfo infoLastBlockFile;
extern int nLastBlockFile;
enum BlockStatus { enum BlockStatus {
BLOCK_VALID_UNKNOWN = 0, BLOCK_VALID_UNKNOWN = 0,
BLOCK_VALID_HEADER = 1, // parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future BLOCK_VALID_HEADER = 1, // parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future
@ -850,19 +842,6 @@ public:
} }
}; };
struct CBlockIndexWorkComparator
{
bool operator()(CBlockIndex *pa, CBlockIndex *pb) {
if (pa->nChainWork > pb->nChainWork) return false;
if (pa->nChainWork < pb->nChainWork) return true;
if (pa->GetBlockHash() < pb->GetBlockHash()) return false;
if (pa->GetBlockHash() > pb->GetBlockHash()) return true;
return false; // identical blocks
}
};
/** Used to marshal pointers into hashes for db storage. */ /** Used to marshal pointers into hashes for db storage. */

View File

@ -74,13 +74,9 @@ bool CBlockTreeDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
return Write(make_pair('b', blockindex.GetBlockHash()), blockindex); return Write(make_pair('b', blockindex.GetBlockHash()), blockindex);
} }
bool CBlockTreeDB::ReadBestInvalidWork(CBigNum& bnBestInvalidWork)
{
return Read('I', bnBestInvalidWork);
}
bool CBlockTreeDB::WriteBestInvalidWork(const CBigNum& bnBestInvalidWork) bool CBlockTreeDB::WriteBestInvalidWork(const CBigNum& bnBestInvalidWork)
{ {
// Obsolete; only written for backward compatibility.
return Write('I', bnBestInvalidWork); return Write('I', bnBestInvalidWork);
} }

View File

@ -35,7 +35,6 @@ private:
void operator=(const CBlockTreeDB&); void operator=(const CBlockTreeDB&);
public: public:
bool WriteBlockIndex(const CDiskBlockIndex& blockindex); bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
bool ReadBestInvalidWork(CBigNum& bnBestInvalidWork);
bool WriteBestInvalidWork(const CBigNum& bnBestInvalidWork); bool WriteBestInvalidWork(const CBigNum& bnBestInvalidWork);
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo); bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo);
bool WriteBlockFileInfo(int nFile, const CBlockFileInfo &fileinfo); bool WriteBlockFileInfo(int nFile, const CBlockFileInfo &fileinfo);