lock annotations for MemPoolAccept functions

We should already have the mempool lock when entering
CheckInputsFromMempoolAndCache
This commit is contained in:
gzhao408 2020-12-30 09:57:00 -08:00
parent 86a8b35f32
commit 85cc6bed64

View file

@ -407,21 +407,17 @@ static void UpdateMempoolForReorg(CTxMemPool& mempool, DisconnectedBlockTransact
// Used to avoid mempool polluting consensus critical paths if CCoinsViewMempool // Used to avoid mempool polluting consensus critical paths if CCoinsViewMempool
// were somehow broken and returning the wrong scriptPubKeys // were somehow broken and returning the wrong scriptPubKeys
static bool CheckInputsFromMempoolAndCache(const CTransaction& tx, TxValidationState& state, const CCoinsViewCache& view, const CTxMemPool& pool, static bool CheckInputsFromMempoolAndCache(const CTransaction& tx, TxValidationState& state, const CCoinsViewCache& view, const CTxMemPool& pool,
unsigned int flags, PrecomputedTransactionData& txdata) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { unsigned int flags, PrecomputedTransactionData& txdata) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs) {
AssertLockHeld(cs_main); AssertLockHeld(cs_main);
AssertLockHeld(pool.cs);
// pool.cs should be locked already, but go ahead and re-take the lock here
// to enforce that mempool doesn't change between when we check the view
// and when we actually call through to CheckInputScripts
LOCK(pool.cs);
assert(!tx.IsCoinBase()); assert(!tx.IsCoinBase());
for (const CTxIn& txin : tx.vin) { for (const CTxIn& txin : tx.vin) {
const Coin& coin = view.AccessCoin(txin.prevout); const Coin& coin = view.AccessCoin(txin.prevout);
// AcceptToMemoryPoolWorker has already checked that the coins are // This coin was checked in PreChecks and MemPoolAccept
// available, so this shouldn't fail. If the inputs are not available // has been holding cs_main since then.
// here then return false. Assume(!coin.IsSpent());
if (coin.IsSpent()) return false; if (coin.IsSpent()) return false;
// Check equivalence for available inputs. // Check equivalence for available inputs.
@ -502,13 +498,13 @@ private:
// Run the script checks using our policy flags. As this can be slow, we should // Run the script checks using our policy flags. As this can be slow, we should
// only invoke this on transactions that have otherwise passed policy checks. // only invoke this on transactions that have otherwise passed policy checks.
bool PolicyScriptChecks(ATMPArgs& args, const Workspace& ws, PrecomputedTransactionData& txdata) EXCLUSIVE_LOCKS_REQUIRED(cs_main); bool PolicyScriptChecks(ATMPArgs& args, const Workspace& ws, PrecomputedTransactionData& txdata) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
// Re-run the script checks, using consensus flags, and try to cache the // Re-run the script checks, using consensus flags, and try to cache the
// result in the scriptcache. This should be done after // result in the scriptcache. This should be done after
// PolicyScriptChecks(). This requires that all inputs either be in our // PolicyScriptChecks(). This requires that all inputs either be in our
// utxo set or in the mempool. // utxo set or in the mempool.
bool ConsensusScriptChecks(ATMPArgs& args, const Workspace& ws, PrecomputedTransactionData &txdata) EXCLUSIVE_LOCKS_REQUIRED(cs_main); bool ConsensusScriptChecks(ATMPArgs& args, const Workspace& ws, PrecomputedTransactionData &txdata) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
// Try to add the transaction to the mempool, removing any conflicts first. // Try to add the transaction to the mempool, removing any conflicts first.
// Returns true if the transaction is in the mempool after any size // Returns true if the transaction is in the mempool after any size
@ -516,7 +512,7 @@ private:
bool Finalize(ATMPArgs& args, Workspace& ws) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs); bool Finalize(ATMPArgs& args, Workspace& ws) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
// Compare a package's feerate against minimum allowed. // Compare a package's feerate against minimum allowed.
bool CheckFeeRate(size_t package_size, CAmount package_fee, TxValidationState& state) bool CheckFeeRate(size_t package_size, CAmount package_fee, TxValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs)
{ {
CAmount mempoolRejectFee = m_pool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFee(package_size); CAmount mempoolRejectFee = m_pool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFee(package_size);
if (mempoolRejectFee > 0 && package_fee < mempoolRejectFee) { if (mempoolRejectFee > 0 && package_fee < mempoolRejectFee) {