Skip to content

Commit

Permalink
Merge bitcoin#12368: Hold mempool.cs for the duration of ATMP.
Browse files Browse the repository at this point in the history
02fc886 Add braces to meet code style on line-after-the-one-changed. (Matt Corallo)
85aa839 Hold mempool.cs for the duration of ATMP. (Matt Corallo)

Pull request description:

  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 bitcoin#12273

Tree-SHA512: 29464b9ca3890010ae13b7dc1c53487cc2bc9c3cf3d32a14cb09c8aa33848f57959d8991ea096beebcfb72f062e4e1962f104aefe4252c7db87633bbfe4ab317
  • Loading branch information
laanwj authored and PastaPastaPasta committed Mar 14, 2020
1 parent 61bd9bd commit 9c9a1ab
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions src/validation.cpp
Expand Up @@ -543,8 +543,10 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool
const CTransaction& tx = *ptx;
const uint256 hash = tx.GetHash();
AssertLockHeld(cs_main);
if (pfMissingInputs)
LOCK(pool.cs); // mempool "read lock" (held through GetMainSignals().TransactionAddedToMempool())
if (pfMissingInputs) {
*pfMissingInputs = false;
}

if (!CheckTransaction(tx, state))
return false; // state filled in by CheckTransaction
Expand Down Expand Up @@ -590,8 +592,6 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool
}

// Check for conflicts with in-memory transactions
{
LOCK(pool.cs); // protect pool.mapNextTx
for (const CTxIn &txin : tx.vin)
{
auto itConflicting = pool.mapNextTx.find(txin.prevout);
Expand All @@ -603,15 +603,12 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool
return state.Invalid(false, REJECT_DUPLICATE, "txn-mempool-conflict");
}
}
}

{
CCoinsView dummy;
CCoinsViewCache view(&dummy);

LockPoints lp;
{
LOCK(pool.cs);
CCoinsViewMemPool viewMemPool(pcoinsTip.get(), pool);
view.SetBackend(viewMemPool);

Expand Down Expand Up @@ -650,8 +647,6 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool
if (!CheckSequenceLocks(tx, STANDARD_LOCKTIME_VERIFY_FLAGS, &lp))
return state.DoS(0, false, REJECT_NONSTANDARD, "non-BIP68-final");

} // end LOCK(pool.cs)

CAmount nFees = 0;
if (!Consensus::CheckTxInputs(tx, state, view, GetSpendHeight(view), nFees)) {
return error("%s: Consensus::CheckTxInputs: %s, %s", __func__, tx.GetHash().ToString(), FormatStateMessage(state));
Expand Down

0 comments on commit 9c9a1ab

Please sign in to comment.