Skip to content

Commit 4ba6da5

Browse files
committed
Merge #12743: Fix csBestBlock/cvBlockChange waiting in rpc/mining
4a6c0e3 Modernize best block mutex/cv/hash variable naming (Pieter Wuille) 45dd135 Fix csBestBlock/cvBlockChange waiting in rpc/mining (Pieter Wuille) Pull request description: This is an alternative to #11694. It reintroduces a uint256 variable with the best block hash, protected by csBestBlock, and only updated while holding it. Also rename the involved variable to modern guidelines, as there are very few uses. Tree-SHA512: 826a86c7d3cee7fe49f99f4398ae99e81cb0563197eaeba77306a3ca6072b67cdb932bc35720fc0f99c2a57b218efa029d0b8bdfb240591a629b2e90efa3199d
2 parents 8480d41 + 4a6c0e3 commit 4ba6da5

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ void OnRPCStopped()
323323
{
324324
uiInterface.NotifyBlockTip.disconnect(&RPCNotifyBlockChange);
325325
RPCNotifyBlockChange(false, nullptr);
326-
cvBlockChange.notify_all();
326+
g_best_block_cv.notify_all();
327327
LogPrint(BCLog::RPC, "RPC stopped.\n");
328328
}
329329

src/rpc/mining.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,10 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
471471
{
472472
checktxtime = std::chrono::steady_clock::now() + std::chrono::minutes(1);
473473

474-
WaitableLock lock(csBestBlock);
475-
while (chainActive.Tip()->GetBlockHash() == hashWatchedChain && IsRPCRunning())
474+
WaitableLock lock(g_best_block_mutex);
475+
while (g_best_block == hashWatchedChain && IsRPCRunning())
476476
{
477-
if (cvBlockChange.wait_until(lock, checktxtime) == std::cv_status::timeout)
477+
if (g_best_block_cv.wait_until(lock, checktxtime) == std::cv_status::timeout)
478478
{
479479
// Timeout: Check transactions for update
480480
if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLastLP)

src/validation.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,9 @@ CCriticalSection cs_main;
202202
BlockMap& mapBlockIndex = g_chainstate.mapBlockIndex;
203203
CChain& chainActive = g_chainstate.chainActive;
204204
CBlockIndex *pindexBestHeader = nullptr;
205-
CWaitableCriticalSection csBestBlock;
206-
CConditionVariable cvBlockChange;
205+
CWaitableCriticalSection g_best_block_mutex;
206+
CConditionVariable g_best_block_cv;
207+
uint256 g_best_block;
207208
int nScriptCheckThreads = 0;
208209
std::atomic_bool fImporting(false);
209210
std::atomic_bool fReindex(false);
@@ -2204,7 +2205,11 @@ void static UpdateTip(const CBlockIndex *pindexNew, const CChainParams& chainPar
22042205
// New best block
22052206
mempool.AddTransactionsUpdated(1);
22062207

2207-
cvBlockChange.notify_all();
2208+
{
2209+
WaitableLock lock(g_best_block_mutex);
2210+
g_best_block = pindexNew->GetBlockHash();
2211+
g_best_block_cv.notify_all();
2212+
}
22082213

22092214
std::vector<std::string> warningMessages;
22102215
if (!IsInitialBlockDownload())

src/validation.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,9 @@ extern BlockMap& mapBlockIndex;
165165
extern uint64_t nLastBlockTx;
166166
extern uint64_t nLastBlockWeight;
167167
extern const std::string strMessageMagic;
168-
extern CWaitableCriticalSection csBestBlock;
169-
extern CConditionVariable cvBlockChange;
168+
extern CWaitableCriticalSection g_best_block_mutex;
169+
extern CConditionVariable g_best_block_cv;
170+
extern uint256 g_best_block;
170171
extern std::atomic_bool fImporting;
171172
extern std::atomic_bool fReindex;
172173
extern int nScriptCheckThreads;

0 commit comments

Comments
 (0)