Skip to content

Commit d57d10e

Browse files
committed
Merge #12368: Hold mempool.cs for the duration of ATMP.
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 #12273 Tree-SHA512: 29464b9ca3890010ae13b7dc1c53487cc2bc9c3cf3d32a14cb09c8aa33848f57959d8991ea096beebcfb72f062e4e1962f104aefe4252c7db87633bbfe4ab317
2 parents 6db4fa7 + 02fc886 commit d57d10e

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

src/validation.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,10 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool
547547
const CTransaction& tx = *ptx;
548548
const uint256 hash = tx.GetHash();
549549
AssertLockHeld(cs_main);
550-
if (pfMissingInputs)
550+
LOCK(pool.cs); // mempool "read lock" (held through GetMainSignals().TransactionAddedToMempool())
551+
if (pfMissingInputs) {
551552
*pfMissingInputs = false;
553+
}
552554

553555
if (!CheckTransaction(tx, state))
554556
return false; // state filled in by CheckTransaction
@@ -581,8 +583,6 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool
581583

582584
// Check for conflicts with in-memory transactions
583585
std::set<uint256> setConflicts;
584-
{
585-
LOCK(pool.cs); // protect pool.mapNextTx
586586
for (const CTxIn &txin : tx.vin)
587587
{
588588
auto itConflicting = pool.mapNextTx.find(txin.prevout);
@@ -623,15 +623,12 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool
623623
}
624624
}
625625
}
626-
}
627626

628627
{
629628
CCoinsView dummy;
630629
CCoinsViewCache view(&dummy);
631630

632631
LockPoints lp;
633-
{
634-
LOCK(pool.cs);
635632
CCoinsViewMemPool viewMemPool(pcoinsTip.get(), pool);
636633
view.SetBackend(viewMemPool);
637634

@@ -670,8 +667,6 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool
670667
if (!CheckSequenceLocks(tx, STANDARD_LOCKTIME_VERIFY_FLAGS, &lp))
671668
return state.DoS(0, false, REJECT_NONSTANDARD, "non-BIP68-final");
672669

673-
} // end LOCK(pool.cs)
674-
675670
CAmount nFees = 0;
676671
if (!Consensus::CheckTxInputs(tx, state, view, GetSpendHeight(view), nFees)) {
677672
return error("%s: Consensus::CheckTxInputs: %s, %s", __func__, tx.GetHash().ToString(), FormatStateMessage(state));
@@ -768,7 +763,6 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool
768763
// If we don't hold the lock allConflicting might be incomplete; the
769764
// subsequent RemoveStaged() and addUnchecked() calls don't guarantee
770765
// mempool consistency for us.
771-
LOCK(pool.cs);
772766
const bool fReplacementTransaction = setConflicts.size();
773767
if (fReplacementTransaction)
774768
{

0 commit comments

Comments
 (0)