From 282019cd3ddb060db350654e6f855f7ea37e0d34 Mon Sep 17 00:00:00 2001 From: fanquake Date: Thu, 15 Sep 2022 10:45:07 +0100 Subject: refactor: add kernel/cs_main.* Co-authored-by: Anthony Towns --- src/Makefile.am | 3 +++ src/bench/rpc_mempool.cpp | 1 + src/chain.h | 3 +-- src/kernel/cs_main.cpp | 7 +++++++ src/kernel/cs_main.h | 22 ++++++++++++++++++++++ src/node/blockstorage.h | 3 +-- src/rpc/blockchain.h | 2 -- src/rpc/node.cpp | 1 + src/test/mempool_tests.cpp | 2 +- src/test/txvalidationcache_tests.cpp | 2 +- src/test/validation_chainstatemanager_tests.cpp | 2 +- src/txdb.h | 4 +--- src/txmempool.h | 2 +- src/validation.cpp | 12 ------------ src/validation.h | 2 +- src/validationinterface.h | 2 +- src/zmq/zmqpublishnotifier.cpp | 1 + 17 files changed, 44 insertions(+), 27 deletions(-) create mode 100644 src/kernel/cs_main.cpp create mode 100644 src/kernel/cs_main.h diff --git a/src/Makefile.am b/src/Makefile.am index e73245daeb..62c94f59fb 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -177,6 +177,7 @@ BITCOIN_CORE_H = \ kernel/checks.h \ kernel/coinstats.h \ kernel/context.h \ + kernel/cs_main.h \ kernel/mempool_entry.h \ kernel/mempool_limits.h \ kernel/mempool_options.h \ @@ -375,6 +376,7 @@ libbitcoin_node_a_SOURCES = \ kernel/checks.cpp \ kernel/coinstats.cpp \ kernel/context.cpp \ + kernel/cs_main.cpp \ kernel/mempool_persist.cpp \ mapport.cpp \ net.cpp \ @@ -906,6 +908,7 @@ libbitcoinkernel_la_SOURCES = \ kernel/checks.cpp \ kernel/coinstats.cpp \ kernel/context.cpp \ + kernel/cs_main.cpp \ kernel/mempool_persist.cpp \ key.cpp \ logging.cpp \ diff --git a/src/bench/rpc_mempool.cpp b/src/bench/rpc_mempool.cpp index 3d82e495fa..e3e1a07c83 100644 --- a/src/bench/rpc_mempool.cpp +++ b/src/bench/rpc_mempool.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include diff --git a/src/chain.h b/src/chain.h index fbbb715986..f5dd0fd315 100644 --- a/src/chain.h +++ b/src/chain.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -38,8 +39,6 @@ static constexpr int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME; */ static constexpr int64_t MAX_BLOCK_TIME_GAP = 90 * 60; -extern RecursiveMutex cs_main; - class CBlockFileInfo { public: diff --git a/src/kernel/cs_main.cpp b/src/kernel/cs_main.cpp new file mode 100644 index 0000000000..c3a08c9695 --- /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/kernel/cs_main.h b/src/kernel/cs_main.h new file mode 100644 index 0000000000..8d03903b8e --- /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/node/blockstorage.h b/src/node/blockstorage.h index d4411a7776..cdf667c754 100644 --- a/src/node/blockstorage.h +++ b/src/node/blockstorage.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -17,8 +18,6 @@ #include #include -extern RecursiveMutex cs_main; - class ArgsManager; class BlockValidationState; class CBlock; diff --git a/src/rpc/blockchain.h b/src/rpc/blockchain.h index 2a802f7fee..9ccb87b78a 100644 --- a/src/rpc/blockchain.h +++ b/src/rpc/blockchain.h @@ -16,8 +16,6 @@ #include #include -extern RecursiveMutex cs_main; - class CBlock; class CBlockIndex; class Chainstate; diff --git a/src/rpc/node.cpp b/src/rpc/node.cpp index d8f2576b6d..ab1bc6615f 100644 --- a/src/rpc/node.cpp +++ b/src/rpc/node.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include diff --git a/src/test/mempool_tests.cpp b/src/test/mempool_tests.cpp index 7e9079743e..94e553a304 100644 --- a/src/test/mempool_tests.cpp +++ b/src/test/mempool_tests.cpp @@ -64,7 +64,7 @@ BOOST_AUTO_TEST_CASE(MempoolRemoveTest) 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 index 52c0b1d938..8cdea3890e 100644 --- a/src/test/txvalidationcache_tests.cpp +++ b/src/test/txvalidationcache_tests.cpp @@ -117,7 +117,7 @@ BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend, Dersig100Setup) // should fail. // Capture this interaction with the upgraded_nop argument: set it when evaluating // any script flag that is implemented as an upgraded NOP code. -static void ValidateCheckInputsForAllFlags(const CTransaction &tx, uint32_t failing_flags, bool add_to_cache, CCoinsViewCache& active_coins_tip) EXCLUSIVE_LOCKS_REQUIRED(cs_main) +static void ValidateCheckInputsForAllFlags(const CTransaction &tx, uint32_t failing_flags, bool add_to_cache, CCoinsViewCache& active_coins_tip) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) { PrecomputedTransactionData txdata; diff --git a/src/test/validation_chainstatemanager_tests.cpp b/src/test/validation_chainstatemanager_tests.cpp index 620c41ff0c..56867a584b 100644 --- a/src/test/validation_chainstatemanager_tests.cpp +++ b/src/test/validation_chainstatemanager_tests.cpp @@ -116,7 +116,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches) // Create a legacy (IBD) chainstate. // - Chainstate& c1 = WITH_LOCK(cs_main, return manager.InitializeChainstate(&mempool)); + Chainstate& c1 = WITH_LOCK(::cs_main, return manager.InitializeChainstate(&mempool)); chainstates.push_back(&c1); c1.InitCoinsDB( /*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false); diff --git a/src/txdb.h b/src/txdb.h index 0570355016..5a409d7dcc 100644 --- a/src/txdb.h +++ b/src/txdb.h @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -44,9 +45,6 @@ static const int64_t max_filter_index_cache = 1024; //! Max memory allocated to coin DB specific cache (MiB) static const int64_t nMaxCoinsDBCache = 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 { diff --git a/src/txmempool.h b/src/txmempool.h index c3bc86cc04..51b8af3286 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -39,7 +40,6 @@ class CBlockIndex; class CChain; class Chainstate; -extern RecursiveMutex cs_main; /** Fake height value used in Coin to signify they are only in the memory pool (since 0.8) */ static const uint32_t MEMPOOL_HEIGHT = 0x7FFFFFFF; diff --git a/src/validation.cpp b/src/validation.cpp index e24d39170e..7b410b9897 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -107,18 +107,6 @@ const std::vector CHECKLEVEL_DOC { * */ static constexpr int PRUNE_LOCK_BUFFER{10}; -/** - * 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. - */ -RecursiveMutex cs_main; - GlobalMutex g_best_block_mutex; std::condition_variable g_best_block_cv; uint256 g_best_block; diff --git a/src/validation.h b/src/validation.h index 63ff9247be..2f33dc58dd 100644 --- a/src/validation.h +++ b/src/validation.h @@ -18,6 +18,7 @@ #include #include #include +#include // IWYU pragma: export #include #include #include @@ -86,7 +87,6 @@ enum class SynchronizationState { POST_INIT }; -extern RecursiveMutex cs_main; extern GlobalMutex g_best_block_mutex; extern std::condition_variable g_best_block_cv; /** Used to notify getblocktemplate RPC of new tips. */ diff --git a/src/validationinterface.h b/src/validationinterface.h index acd775ada4..8c20cc8ffb 100644 --- a/src/validationinterface.h +++ b/src/validationinterface.h @@ -6,13 +6,13 @@ #ifndef BITCOIN_VALIDATIONINTERFACE_H #define BITCOIN_VALIDATIONINTERFACE_H +#include #include // CTransaction(Ref) #include #include #include -extern RecursiveMutex cs_main; class BlockValidationState; class CBlock; class CBlockIndex; diff --git a/src/zmq/zmqpublishnotifier.cpp b/src/zmq/zmqpublishnotifier.cpp index 5ba13ecc9c..6418455d19 100644 --- a/src/zmq/zmqpublishnotifier.cpp +++ b/src/zmq/zmqpublishnotifier.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include -- cgit v1.2.3