From e32c7c8e807883552d5e4a70e329bf2322f3b936 Mon Sep 17 00:00:00 2001 From: James Stewart Date: Sun, 5 Jan 2020 07:07:18 -0600 Subject: [PATCH] Merge #1203: [Backport][Performance] Cache + guard best block hash. 4976c0a7ecaacd62e4b97086a32c9a3f1f0627a1 [Backport][Performance] Cache best block hash for miner thread usage + refactor. (furszy) Pull request description: This comes from upstream PR 12743. * Best block hash cached for miner thread usage (preventing one of the many many cs_main locks..). * csBestBlock, cvBlockChange names updated to latest upstream naming convention. ACKs for top commit: random-zebra: ACK 4976c0a7ecaacd62e4b97086a32c9a3f1f0627a1 Fuzzbawls: utACK 4976c0a7ecaacd62e4b97086a32c9a3f1f0627a1 Tree-SHA512: 9d3281bd4b01ef250af9136136582af99f04b1d81b7c6b6c1b8e41aa208f8ebff7a25c0bca823bc5b74d4802d29d10e58b8ebf43764fb9208bdf4a4f6140d822 --- src/init.cpp | 2 +- src/main.cpp | 30 ++++++++++++++++-------------- src/main.h | 8 ++++++-- src/miner.cpp | 6 +++--- src/rpc/mining.cpp | 6 +++--- 5 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 1173e6f2da..4f2e37d813 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -354,7 +354,7 @@ void OnRPCStopped() { uiInterface.NotifyBlockTip.disconnect(RPCNotifyBlockChange); //RPCNotifyBlockChange(0); - cvBlockChange.notify_all(); + g_best_block_cv.notify_all(); LogPrint("rpc", "RPC stopped.\n"); } diff --git a/src/main.cpp b/src/main.cpp index a9d7af7c96..b44744bb60 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -70,8 +70,12 @@ map mapHashedBlocks; CChain chainActive; CBlockIndex* pindexBestHeader = nullptr; int64_t nTimeBestReceived = 0; -CWaitableCriticalSection csBestBlock; -CConditionVariable cvBlockChange; + +// Best block section +CWaitableCriticalSection g_best_block_mutex; +std::condition_variable g_best_block_cv; +uint256 g_best_block; + int nScriptCheckThreads = 0; bool fImporting = false; bool fReindex = false; @@ -3475,23 +3479,21 @@ void FlushStateToDisk() void static UpdateTip(CBlockIndex* pindexNew) { chainActive.SetTip(pindexNew); -/* -#ifdef ENABLE_WALLET - // If turned on AutoZeromint will automatically convert DOGEC to zdogec - if (pwalletMain && pwalletMain->isZeromintEnabled()) - pwalletMain->AutoZeromint(); -#endif // ENABLE_WALLET -*/ + // New best block nTimeBestReceived = GetTime(); mempool.AddTransactionsUpdated(1); - LogPrintf("UpdateTip: new best=%s height=%d version=%d log2_work=%.8g tx=%lu date=%s progress=%f cache=%u\n", - chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), chainActive.Tip()->nVersion, log(chainActive.Tip()->nChainWork.getdouble()) / log(2.0), (unsigned long)chainActive.Tip()->nChainTx, - DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()), - Checkpoints::GuessVerificationProgress(chainActive.Tip()), (unsigned int)pcoinsTip->GetCacheSize()); + { + WaitableLock lock(g_best_block_mutex); + g_best_block = pindexNew->GetBlockHash(); + g_best_block_cv.notify_all(); + } - cvBlockChange.notify_all(); + LogPrintf("UpdateTip: new best=%s height=%d version=%d log2_work=%.8g tx=%lu date=%s progress=%f cache=%u\n", + chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), chainActive.Tip()->nVersion, log(chainActive.Tip()->nChainWork.getdouble()) / log(2.0), (unsigned long)chainActive.Tip()->nChainTx, + DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()), + Checkpoints::GuessVerificationProgress(chainActive.Tip()), (unsigned int)pcoinsTip->GetCacheSize()); // Check the version of the last 100 blocks to see if we need to upgrade: static bool fWarned = false; diff --git a/src/main.h b/src/main.h index 892251e10a..dbbd4cab4c 100644 --- a/src/main.h +++ b/src/main.h @@ -149,8 +149,12 @@ extern uint64_t nLastBlockTx; extern uint64_t nLastBlockSize; extern const std::string strMessageMagic; extern int64_t nTimeBestReceived; -extern CWaitableCriticalSection csBestBlock; -extern CConditionVariable cvBlockChange; + +// Best block section +extern CWaitableCriticalSection g_best_block_mutex; +extern std::condition_variable g_best_block_cv; +extern uint256 g_best_block; + extern bool fImporting; extern bool fReindex; extern int nScriptCheckThreads; diff --git a/src/miner.cpp b/src/miner.cpp index d564bf3be8..ccea2df6d1 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -601,9 +601,9 @@ bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey) // Found a solution { - LOCK(cs_main); - if (pblock->hashPrevBlock != chainActive.Tip()->GetBlockHash()) - return error("DogeCashMiner : generated block is stale"); + WaitableLock lock(g_best_block_mutex); + if (pblock->hashPrevBlock != g_best_block) + return error("PIVXMiner : generated block is stale"); } // Remove key from key pool diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 8772b5a2e4..a0ffb3ae41 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -494,9 +494,9 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp) { checktxtime = std::chrono::steady_clock::now() + std::chrono::minutes(1); - WaitableLock lock(csBestBlock); - while (chainActive.Tip()->GetBlockHash() == hashWatchedChain && IsRPCRunning()) { - if (cvBlockChange.wait_until(lock, checktxtime) == std::cv_status::timeout) + WaitableLock lock(g_best_block_mutex); + while (g_best_block == hashWatchedChain && IsRPCRunning()) { + if (g_best_block_cv.wait_until(lock, checktxtime) == std::cv_status::timeout) { // Timeout: Check transactions for update if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLastLP)