From 45dd13503918e75a45ce33eb5c934b998790fdc8 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Tue, 20 Mar 2018 21:04:27 -0700 Subject: [PATCH 1/2] Fix csBestBlock/cvBlockChange waiting in rpc/mining --- src/rpc/mining.cpp | 2 +- src/validation.cpp | 7 ++++++- src/validation.h | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 06882c0dfdd..5a5ff06fe34 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -471,7 +471,7 @@ UniValue getblocktemplate(const JSONRPCRequest& request) checktxtime = std::chrono::steady_clock::now() + std::chrono::minutes(1); WaitableLock lock(csBestBlock); - while (chainActive.Tip()->GetBlockHash() == hashWatchedChain && IsRPCRunning()) + while (hashBestBlock == hashWatchedChain && IsRPCRunning()) { if (cvBlockChange.wait_until(lock, checktxtime) == std::cv_status::timeout) { diff --git a/src/validation.cpp b/src/validation.cpp index df8729e3823..c3e411f1fc7 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -204,6 +204,7 @@ CChain& chainActive = g_chainstate.chainActive; CBlockIndex *pindexBestHeader = nullptr; CWaitableCriticalSection csBestBlock; CConditionVariable cvBlockChange; +uint256 hashBestBlock; int nScriptCheckThreads = 0; std::atomic_bool fImporting(false); std::atomic_bool fReindex(false); @@ -2195,7 +2196,11 @@ void static UpdateTip(const CBlockIndex *pindexNew, const CChainParams& chainPar // New best block mempool.AddTransactionsUpdated(1); - cvBlockChange.notify_all(); + { + WaitableLock lock(csBestBlock); + hashBestBlock = pindexNew->GetBlockHash(); + cvBlockChange.notify_all(); + } std::vector warningMessages; if (!IsInitialBlockDownload()) diff --git a/src/validation.h b/src/validation.h index 95c31bf0fc6..b51ad9fc769 100644 --- a/src/validation.h +++ b/src/validation.h @@ -166,6 +166,7 @@ extern uint64_t nLastBlockWeight; extern const std::string strMessageMagic; extern CWaitableCriticalSection csBestBlock; extern CConditionVariable cvBlockChange; +extern uint256 hashBestBlock; extern std::atomic_bool fImporting; extern std::atomic_bool fReindex; extern int nScriptCheckThreads; From 4a6c0e3dcfdca98270cb96b73db4c3d4446dba50 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Tue, 3 Apr 2018 21:53:07 -0700 Subject: [PATCH 2/2] Modernize best block mutex/cv/hash variable naming --- src/init.cpp | 2 +- src/rpc/mining.cpp | 6 +++--- src/validation.cpp | 12 ++++++------ src/validation.h | 6 +++--- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index f6f522da66b..747d91745a2 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -307,7 +307,7 @@ void OnRPCStopped() { uiInterface.NotifyBlockTip.disconnect(&RPCNotifyBlockChange); RPCNotifyBlockChange(false, nullptr); - cvBlockChange.notify_all(); + g_best_block_cv.notify_all(); LogPrint(BCLog::RPC, "RPC stopped.\n"); } diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 5a5ff06fe34..fbdcc3b4dda 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -470,10 +470,10 @@ UniValue getblocktemplate(const JSONRPCRequest& request) { checktxtime = std::chrono::steady_clock::now() + std::chrono::minutes(1); - WaitableLock lock(csBestBlock); - while (hashBestBlock == hashWatchedChain && IsRPCRunning()) + WaitableLock lock(g_best_block_mutex); + while (g_best_block == hashWatchedChain && IsRPCRunning()) { - if (cvBlockChange.wait_until(lock, checktxtime) == std::cv_status::timeout) + if (g_best_block_cv.wait_until(lock, checktxtime) == std::cv_status::timeout) { // Timeout: Check transactions for update if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLastLP) diff --git a/src/validation.cpp b/src/validation.cpp index c3e411f1fc7..9dda65923b9 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -202,9 +202,9 @@ CCriticalSection cs_main; BlockMap& mapBlockIndex = g_chainstate.mapBlockIndex; CChain& chainActive = g_chainstate.chainActive; CBlockIndex *pindexBestHeader = nullptr; -CWaitableCriticalSection csBestBlock; -CConditionVariable cvBlockChange; -uint256 hashBestBlock; +CWaitableCriticalSection g_best_block_mutex; +CConditionVariable g_best_block_cv; +uint256 g_best_block; int nScriptCheckThreads = 0; std::atomic_bool fImporting(false); std::atomic_bool fReindex(false); @@ -2197,9 +2197,9 @@ void static UpdateTip(const CBlockIndex *pindexNew, const CChainParams& chainPar mempool.AddTransactionsUpdated(1); { - WaitableLock lock(csBestBlock); - hashBestBlock = pindexNew->GetBlockHash(); - cvBlockChange.notify_all(); + WaitableLock lock(g_best_block_mutex); + g_best_block = pindexNew->GetBlockHash(); + g_best_block_cv.notify_all(); } std::vector warningMessages; diff --git a/src/validation.h b/src/validation.h index b51ad9fc769..12f11d80d4f 100644 --- a/src/validation.h +++ b/src/validation.h @@ -164,9 +164,9 @@ extern BlockMap& mapBlockIndex; extern uint64_t nLastBlockTx; extern uint64_t nLastBlockWeight; extern const std::string strMessageMagic; -extern CWaitableCriticalSection csBestBlock; -extern CConditionVariable cvBlockChange; -extern uint256 hashBestBlock; +extern CWaitableCriticalSection g_best_block_mutex; +extern CConditionVariable g_best_block_cv; +extern uint256 g_best_block; extern std::atomic_bool fImporting; extern std::atomic_bool fReindex; extern int nScriptCheckThreads;