Skip to content

Commit

Permalink
Hold mempool.cs for the duration of ATMP.
Browse files Browse the repository at this point in the history
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 85aa839
zcash: bitcoin/bitcoin#12368
  • Loading branch information
TheBlueMatt authored and LarryRuane committed Apr 12, 2021
1 parent cbd1113 commit a4730c8
Showing 1 changed file with 1 addition and 10 deletions.
11 changes: 1 addition & 10 deletions src/main.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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()));

Expand Down

0 comments on commit a4730c8

Please sign in to comment.