From a4730c8a0ea34f57f4e28c16234108185b2aed4b Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 6 Feb 2018 13:51:44 -0500 Subject: [PATCH] Hold mempool.cs for the duration of ATMP. This resolves an issue where getrawmempool() can race mempool notification signals. Intuitively we use mempool.cs as a "read lock" on the mempool with cs_main being the write lock, so holding the read lock intermittently while doing write operations is somewhat strange. This also avoids the introduction of cs_main in getrawmempool() which reviewers objected to in the previous fix in #12273 zcash: cherry picked from commit 85aa8398f5d13c659299b81cdae377462b4f8316 zcash: https://github.com/bitcoin/bitcoin/pull/12368 --- src/main.cpp | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index d01ea286388..abdcbf5f606 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1483,6 +1483,7 @@ bool AcceptToMemoryPool( bool* pfMissingInputs, bool fRejectAbsurdFee) { AssertLockHeld(cs_main); + LOCK(pool.cs); // mempool "read lock" (held through pool.addUnchecked()) if (pfMissingInputs) *pfMissingInputs = false; @@ -1536,8 +1537,6 @@ bool AcceptToMemoryPool( return false; // Check for conflicts with in-memory transactions - { - LOCK(pool.cs); // protect pool.mapNextTx for (unsigned int i = 0; i < tx.vin.size(); i++) { COutPoint outpoint = tx.vin[i].prevout; @@ -1559,15 +1558,12 @@ bool AcceptToMemoryPool( return false; } } - } { CCoinsView dummy; CCoinsViewCache view(&dummy); CAmount nValueIn = 0; - { - LOCK(pool.cs); CCoinsViewMemPool viewMemPool(pcoinsTip, pool); view.SetBackend(viewMemPool); @@ -1609,9 +1605,7 @@ bool AcceptToMemoryPool( nValueIn = view.GetValueIn(tx); - // we have all inputs cached now, so switch back to dummy, so we don't need to keep lock on mempool view.SetBackend(dummy); - } // Check for non-standard pay-to-script-hash in inputs if (chainparams.RequireStandard() && !AreInputsStandard(tx, view, consensusBranchId)) @@ -1723,9 +1717,6 @@ bool AcceptToMemoryPool( } { - // We lock to prevent other threads from accessing the mempool between adding and evicting - LOCK(pool.cs); - // Store transaction in memory pool.addUnchecked(hash, entry, !IsInitialBlockDownload(chainparams.GetConsensus()));