diff --git a/src/bench/mempool_eviction.cpp b/src/bench/mempool_eviction.cpp --- a/src/bench/mempool_eviction.cpp +++ b/src/bench/mempool_eviction.cpp @@ -10,7 +10,7 @@ #include static void AddTx(const CTransactionRef &tx, const Amount &nFee, - CTxMemPool &pool) { + CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(pool.cs) { int64_t nTime = 0; double dPriority = 10.0; unsigned int nHeight = 1; @@ -98,7 +98,7 @@ tx7.vout[1].nValue = 10 * COIN; CTxMemPool pool; - + LOCK(pool.cs); // Create transaction references outside the "hot loop" const CTransactionRef tx1_r{MakeTransactionRef(tx1)}; const CTransactionRef tx2_r{MakeTransactionRef(tx2)}; diff --git a/src/test/blockencodings_tests.cpp b/src/test/blockencodings_tests.cpp --- a/src/test/blockencodings_tests.cpp +++ b/src/test/blockencodings_tests.cpp @@ -72,8 +72,8 @@ TestMemPoolEntryHelper entry; CBlock block(BuildBlockTestCase()); - pool.addUnchecked(block.vtx[2]->GetId(), entry.FromTx(block.vtx[2])); LOCK(pool.cs); + pool.addUnchecked(block.vtx[2]->GetId(), entry.FromTx(block.vtx[2])); BOOST_CHECK_EQUAL( pool.mapTx.find(block.vtx[2]->GetId())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 0); @@ -183,8 +183,8 @@ TestMemPoolEntryHelper entry; CBlock block(BuildBlockTestCase()); - pool.addUnchecked(block.vtx[2]->GetId(), entry.FromTx(block.vtx[2])); LOCK(pool.cs); + pool.addUnchecked(block.vtx[2]->GetId(), entry.FromTx(block.vtx[2])); BOOST_CHECK_EQUAL( pool.mapTx.find(block.vtx[2]->GetId())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 0); @@ -278,8 +278,8 @@ TestMemPoolEntryHelper entry; CBlock block(BuildBlockTestCase()); - pool.addUnchecked(block.vtx[1]->GetId(), entry.FromTx(block.vtx[1])); LOCK(pool.cs); + pool.addUnchecked(block.vtx[1]->GetId(), entry.FromTx(block.vtx[1])); BOOST_CHECK_EQUAL( pool.mapTx.find(block.vtx[1]->GetId())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 0); diff --git a/src/test/mempool_tests.cpp b/src/test/mempool_tests.cpp --- a/src/test/mempool_tests.cpp +++ b/src/test/mempool_tests.cpp @@ -163,6 +163,7 @@ } CTxMemPool testPool; + LOCK(testPool.cs); // Nothing in pool, remove should do nothing: unsigned int poolSize = testPool.size(); @@ -270,6 +271,7 @@ BOOST_AUTO_TEST_CASE(MempoolIndexingTest) { CTxMemPool pool; + LOCK(pool.cs); TestMemPoolEntryHelper entry; /* 3rd highest fee */ @@ -321,7 +323,6 @@ sortedOrder[2] = tx1.GetId().ToString(); // 10000 sortedOrder[3] = tx4.GetId().ToString(); // 15000 sortedOrder[4] = tx2.GetId().ToString(); // 20000 - LOCK(pool.cs); CheckSort(pool, sortedOrder, "MempoolIndexingTest1"); /* low fee but with high fee child */ @@ -461,6 +462,7 @@ BOOST_AUTO_TEST_CASE(MempoolAncestorIndexingTest) { CTxMemPool pool; + LOCK(pool.cs); TestMemPoolEntryHelper entry; /* 3rd highest fee */ @@ -520,7 +522,6 @@ } sortedOrder[4] = tx3.GetId().ToString(); // 0 - LOCK(pool.cs); CheckSort(pool, sortedOrder, "MempoolAncestorIndexingTest1"); @@ -601,6 +602,7 @@ BOOST_AUTO_TEST_CASE(MempoolSizeLimitTest) { CTxMemPool pool; + LOCK(pool.cs); TestMemPoolEntryHelper entry; entry.dPriority = 10.0; Amount feeIncrement = MEMPOOL_FULL_FEE_INCREMENT.GetFeePerK(); @@ -894,6 +896,7 @@ size_t ancestors, descendants; CTxMemPool pool; + LOCK(pool.cs); TestMemPoolEntryHelper entry; /* Base transaction */ diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp --- a/src/test/miner_tests.cpp +++ b/src/test/miner_tests.cpp @@ -104,8 +104,9 @@ // allow reusing the blockchain created in CreateNewBlock_validity. // Note that this test assumes blockprioritypercentage is 0. static void TestPackageSelection(const CChainParams &chainparams, - CScript scriptPubKey, - std::vector &txFirst) { + const CScript &scriptPubKey, + const std::vector &txFirst) + EXCLUSIVE_LOCKS_REQUIRED(::g_mempool.cs) { // Test the ancestor feerate transaction selection. TestMemPoolEntryHelper entry; @@ -323,6 +324,7 @@ } LOCK(cs_main); + LOCK(::g_mempool.cs); // Just to make sure we can still make simple blocks. BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams, g_mempool) diff --git a/src/test/policyestimator_tests.cpp b/src/test/policyestimator_tests.cpp --- a/src/test/policyestimator_tests.cpp +++ b/src/test/policyestimator_tests.cpp @@ -17,6 +17,7 @@ BOOST_AUTO_TEST_CASE(MempoolMinimumFeeEstimate) { CTxMemPool mpool; + LOCK(mpool.cs); TestMemPoolEntryHelper entry; // Create a transaction template diff --git a/src/txmempool.h b/src/txmempool.h --- a/src/txmempool.h +++ b/src/txmempool.h @@ -589,9 +589,10 @@ // Note that addUnchecked is ONLY called from ATMP outside of tests // and any other callers may break wallet's in-mempool tracking (due to // lack of CValidationInterface::TransactionAddedToMempool callbacks). - void addUnchecked(const uint256 &hash, const CTxMemPoolEntry &entry); + void addUnchecked(const uint256 &hash, const CTxMemPoolEntry &entry) + EXCLUSIVE_LOCKS_REQUIRED(cs); void addUnchecked(const uint256 &hash, const CTxMemPoolEntry &entry, - setEntries &setAncestors); + setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs); void removeRecursive( const CTransaction &tx, @@ -667,7 +668,8 @@ const CTxMemPoolEntry &entry, setEntries &setAncestors, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, - std::string &errString, bool fSearchForParents = true) const; + std::string &errString, bool fSearchForParents = true) const + EXCLUSIVE_LOCKS_REQUIRED(cs); /** * Populate setDescendants with all in-mempool descendants of hash. diff --git a/src/txmempool.cpp b/src/txmempool.cpp --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -183,7 +183,6 @@ uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, std::string &errString, bool fSearchForParents /* = true */) const { - LOCK(cs); setEntries parentHashes; const CTransaction &tx = entry.GetTx(); @@ -414,7 +413,6 @@ NotifyEntryAdded(entry.GetSharedTx()); // Add to memory pool without checking anything. // Used by AcceptToMemoryPool(), which DOES do all the appropriate checks. - LOCK(cs); indexed_transaction_set::iterator newit = mapTx.insert(entry).first; mapLinks.insert(make_pair(newit, TxLinks())); @@ -1084,7 +1082,6 @@ void CTxMemPool::addUnchecked(const uint256 &hash, const CTxMemPoolEntry &entry) { - LOCK(cs); setEntries setAncestors; uint64_t nNoLimit = std::numeric_limits::max(); std::string dummy; diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3302,6 +3302,7 @@ DEFAULT_DESCENDANT_SIZE_LIMIT) * 1000; std::string errString; + LOCK(::g_mempool.cs); if (!g_mempool.CalculateMemPoolAncestors( entry, setAncestors, nLimitAncestors, nLimitAncestorSize, nLimitDescendants, nLimitDescendantSize, errString)) {