aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfanquake <fanquake@gmail.com>2022-09-15 10:45:07 +0100
committerfanquake <fanquake@gmail.com>2023-01-05 09:05:14 +0000
commit282019cd3ddb060db350654e6f855f7ea37e0d34 (patch)
tree0d4cf47437572b3d80a6275392d291701ec14f3f
parent296e88225096125b08665b97715c5b8ebb1d28ec (diff)
downloadbitcoin-282019cd3ddb060db350654e6f855f7ea37e0d34.tar.xz
refactor: add kernel/cs_main.*
Co-authored-by: Anthony Towns <aj@erisian.com.au>
-rw-r--r--src/Makefile.am3
-rw-r--r--src/bench/rpc_mempool.cpp1
-rw-r--r--src/chain.h3
-rw-r--r--src/kernel/cs_main.cpp7
-rw-r--r--src/kernel/cs_main.h22
-rw-r--r--src/node/blockstorage.h3
-rw-r--r--src/rpc/blockchain.h2
-rw-r--r--src/rpc/node.cpp1
-rw-r--r--src/test/mempool_tests.cpp2
-rw-r--r--src/test/txvalidationcache_tests.cpp2
-rw-r--r--src/test/validation_chainstatemanager_tests.cpp2
-rw-r--r--src/txdb.h4
-rw-r--r--src/txmempool.h2
-rw-r--r--src/validation.cpp12
-rw-r--r--src/validation.h2
-rw-r--r--src/validationinterface.h2
-rw-r--r--src/zmq/zmqpublishnotifier.cpp1
17 files changed, 44 insertions, 27 deletions
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 <bench/bench.h>
#include <chainparamsbase.h>
+#include <kernel/cs_main.h>
#include <kernel/mempool_entry.h>
#include <rpc/mempool.h>
#include <test/util/setup_common.h>
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 <arith_uint256.h>
#include <consensus/params.h>
#include <flatfile.h>
+#include <kernel/cs_main.h>
#include <primitives/block.h>
#include <sync.h>
#include <uint256.h>
@@ -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 <sync.h>
+
+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 <sync.h>
+
+/**
+ * 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 <attributes.h>
#include <chain.h>
#include <fs.h>
+#include <kernel/cs_main.h>
#include <protocol.h>
#include <sync.h>
#include <txdb.h>
@@ -17,8 +18,6 @@
#include <unordered_map>
#include <vector>
-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 <stdint.h>
#include <vector>
-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 <interfaces/echo.h>
#include <interfaces/init.h>
#include <interfaces/ipc.h>
+#include <kernel/cs_main.h>
#include <node/context.h>
#include <rpc/server.h>
#include <rpc/server_util.h>
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 <coins.h>
#include <dbwrapper.h>
+#include <kernel/cs_main.h>
#include <sync.h>
#include <fs.h>
@@ -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 <coins.h>
#include <consensus/amount.h>
#include <indirectmap.h>
+#include <kernel/cs_main.h>
#include <kernel/mempool_entry.h>
#include <policy/feerate.h>
#include <policy/packages.h>
@@ -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<std::string> 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 <consensus/amount.h>
#include <deploymentstatus.h>
#include <fs.h>
+#include <kernel/cs_main.h> // IWYU pragma: export
#include <node/blockstorage.h>
#include <policy/feerate.h>
#include <policy/packages.h>
@@ -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 <kernel/cs_main.h>
#include <primitives/transaction.h> // CTransaction(Ref)
#include <sync.h>
#include <functional>
#include <memory>
-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 <chain.h>
#include <chainparams.h>
#include <crypto/common.h>
+#include <kernel/cs_main.h>
#include <logging.h>
#include <netaddress.h>
#include <netbase.h>