Skip to content

Commit fa0c9db

Browse files
author
MarcoFalke
committed
txpool: Make nTransactionsUpdated atomic
1 parent d0f81a9 commit fa0c9db

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

src/rpc/mining.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
480480
if (g_best_block_cv.wait_until(lock, checktxtime) == std::cv_status::timeout)
481481
{
482482
// Timeout: Check transactions for update
483+
// without holding ::mempool.cs to avoid deadlocks
483484
if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLastLP)
484485
break;
485486
checktxtime += std::chrono::seconds(10);

src/txmempool.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ void CTxMemPoolEntry::UpdateAncestorState(int64_t modifySize, CAmount modifyFee,
322322
assert(int(nSigOpCostWithAncestors) >= 0);
323323
}
324324

325-
CTxMemPool::CTxMemPool(CBlockPolicyEstimator* estimator) :
326-
nTransactionsUpdated(0), minerPolicyEstimator(estimator)
325+
CTxMemPool::CTxMemPool(CBlockPolicyEstimator* estimator)
326+
: nTransactionsUpdated(0), minerPolicyEstimator(estimator)
327327
{
328328
_clear(); //lock free clear
329329

@@ -341,13 +341,11 @@ bool CTxMemPool::isSpent(const COutPoint& outpoint) const
341341

342342
unsigned int CTxMemPool::GetTransactionsUpdated() const
343343
{
344-
LOCK(cs);
345344
return nTransactionsUpdated;
346345
}
347346

348347
void CTxMemPool::AddTransactionsUpdated(unsigned int n)
349348
{
350-
LOCK(cs);
351349
nTransactionsUpdated += n;
352350
}
353351

src/txmempool.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
#ifndef BITCOIN_TXMEMPOOL_H
77
#define BITCOIN_TXMEMPOOL_H
88

9+
#include <atomic>
10+
#include <map>
911
#include <memory>
1012
#include <set>
11-
#include <map>
12-
#include <vector>
13-
#include <utility>
1413
#include <string>
14+
#include <utility>
15+
#include <vector>
1516

1617
#include <amount.h>
1718
#include <coins.h>
@@ -443,7 +444,7 @@ class CTxMemPool
443444
{
444445
private:
445446
uint32_t nCheckFrequency GUARDED_BY(cs); //!< Value n means that n times in 2^32 we check.
446-
unsigned int nTransactionsUpdated; //!< Used by getblocktemplate to trigger CreateNewBlock() invocation
447+
std::atomic<unsigned int> nTransactionsUpdated; //!< Used by getblocktemplate to trigger CreateNewBlock() invocation
447448
CBlockPolicyEstimator* minerPolicyEstimator;
448449

449450
uint64_t totalTxSize; //!< sum of all mempool tx's virtual sizes. Differs from serialized tx size since witness data is discounted. Defined in BIP 141.

0 commit comments

Comments
 (0)