diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -642,6 +642,7 @@ init.cpp init/common.cpp invrequest.cpp + kernel/cs_main.cpp kernel/disconnected_transactions.cpp kernel/mempool_persist.cpp mapport.cpp @@ -838,6 +839,7 @@ # like index/*.cpp will be removed. add_library(bitcoinkernel kernel/bitcoinkernel.cpp + kernel/cs_main.cpp kernel/disconnected_transactions.cpp kernel/mempool_persist.cpp arith_uint256.cpp diff --git a/src/bench/rpc_mempool.cpp b/src/bench/rpc_mempool.cpp --- a/src/bench/rpc_mempool.cpp +++ b/src/bench/rpc_mempool.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include diff --git a/src/blockindex.h b/src/blockindex.h --- a/src/blockindex.h +++ b/src/blockindex.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -16,8 +17,6 @@ struct BlockHash; -extern RecursiveMutex cs_main; - /** * The block chain is a tree shaped structure starting with the genesis block at * the root, with each block potentially having multiple candidates to be the diff --git a/src/chain.h b/src/chain.h --- a/src/chain.h +++ b/src/chain.h @@ -13,6 +13,7 @@ #include #include // for ReadLE64 #include +#include #include #include #include @@ -43,8 +44,6 @@ */ static constexpr int64_t MAX_BLOCK_TIME_GAP = 90 * 60; -extern RecursiveMutex cs_main; - arith_uint256 GetBlockProof(const CBlockIndex &block); /** diff --git a/src/kernel/cs_main.h b/src/kernel/cs_main.h new file mode 100644 --- /dev/null +++ b/src/kernel/cs_main.h @@ -0,0 +1,22 @@ +// Copyright (c) 2023 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_KERNEL_CS_MAIN_H +#define BITCOIN_KERNEL_CS_MAIN_H + +#include + +/** + * Mutex to guard access to validation specific variables, such as reading + * or changing the chainstate. + * + * This may also need to be locked when updating the transaction pool, e.g. on + * AcceptToMemoryPool. See CTxMemPool::cs comment for details. + * + * The transaction pool has a separate lock to allow reading from it and the + * chainstate at the same time. + */ +extern RecursiveMutex cs_main; + +#endif // BITCOIN_KERNEL_CS_MAIN_H diff --git a/src/kernel/cs_main.cpp b/src/kernel/cs_main.cpp new file mode 100644 --- /dev/null +++ b/src/kernel/cs_main.cpp @@ -0,0 +1,7 @@ +// Copyright (c) 2023 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +RecursiveMutex cs_main; diff --git a/src/net.h b/src/net.h --- a/src/net.h +++ b/src/net.h @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -31,7 +32,6 @@ #include #include #include -#include // For cs_main #include #include diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h --- a/src/node/blockstorage.h +++ b/src/node/blockstorage.h @@ -10,12 +10,11 @@ #include #include +#include #include // For CMessageHeader::MessageStartChars #include #include -extern RecursiveMutex cs_main; - class ArgsManager; class BlockValidationState; class CBlock; diff --git a/src/node/utxo_snapshot.h b/src/node/utxo_snapshot.h --- a/src/node/utxo_snapshot.h +++ b/src/node/utxo_snapshot.h @@ -15,8 +15,6 @@ struct BlockHash; -extern RecursiveMutex cs_main; - namespace node { //! Metadata describing a serialized version of a UTXO set from which an //! assumeutxo Chainstate can be constructed. diff --git a/src/rpc/blockchain.h b/src/rpc/blockchain.h --- a/src/rpc/blockchain.h +++ b/src/rpc/blockchain.h @@ -22,8 +22,6 @@ struct NodeContext; } // namespace node -extern RecursiveMutex cs_main; - RPCHelpMan getblockchaininfo(); /** diff --git a/src/script/scriptcache.h b/src/script/scriptcache.h --- a/src/script/scriptcache.h +++ b/src/script/scriptcache.h @@ -8,12 +8,9 @@ #include #include +#include #include -// Actually declared in validation.cpp; can't include because of circular -// dependency. -extern RecursiveMutex cs_main; - class CTransaction; /** diff --git a/src/test/mempool_tests.cpp b/src/test/mempool_tests.cpp --- a/src/test/mempool_tests.cpp +++ b/src/test/mempool_tests.cpp @@ -55,7 +55,7 @@ } CTxMemPool &testPool = *Assert(m_node.mempool); - LOCK2(cs_main, testPool.cs); + LOCK2(::cs_main, testPool.cs); // Nothing in pool, remove should do nothing: unsigned int poolSize = testPool.size(); diff --git a/src/test/txvalidationcache_tests.cpp b/src/test/txvalidationcache_tests.cpp --- a/src/test/txvalidationcache_tests.cpp +++ b/src/test/txvalidationcache_tests.cpp @@ -135,7 +135,7 @@ static void ValidateCheckInputsForAllFlags( const CTransaction &tx, uint32_t failing_flags, uint32_t required_flags, bool add_to_cache, CCoinsViewCache &active_coins_tip, - int expected_sigchecks) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { + int expected_sigchecks) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) { PrecomputedTransactionData txdata(tx); MMIXLinearCongruentialGenerator lcg; diff --git a/src/test/validation_chainstatemanager_tests.cpp b/src/test/validation_chainstatemanager_tests.cpp --- a/src/test/validation_chainstatemanager_tests.cpp +++ b/src/test/validation_chainstatemanager_tests.cpp @@ -128,7 +128,7 @@ // Create a legacy (IBD) chainstate. // Chainstate &c1 = - WITH_LOCK(cs_main, return manager.InitializeChainstate(&mempool)); + WITH_LOCK(::cs_main, return manager.InitializeChainstate(&mempool)); chainstates.push_back(&c1); c1.InitCoinsDB( /* cache_size_bytes */ 1 << 23, /* in_memory */ true, diff --git a/src/txdb.h b/src/txdb.h --- a/src/txdb.h +++ b/src/txdb.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -51,10 +52,6 @@ //! Max memory allocated to coin DB specific cache (MiB) static constexpr int64_t MAX_COINS_DB_CACHE_MB = 8; -// Actually declared in validation.cpp; can't include because of circular -// dependency. -extern RecursiveMutex cs_main; - /** CCoinsView backed by the coin database (chainstate/) */ class CCoinsViewDB final : public CCoinsView { protected: diff --git a/src/txmempool.h b/src/txmempool.h --- a/src/txmempool.h +++ b/src/txmempool.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -37,8 +38,6 @@ class Chainstate; class Config; -extern RecursiveMutex cs_main; - /** * Fake height value used in Coins to signify they are only in the memory * pool(since 0.8) diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include