Skip to content

Commit 6b7ccb9

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#26251: refactor: add kernel/cs_main.h
282019c refactor: add kernel/cs_main.* (fanquake) Pull request description: One place to find / include `cs_main`. No more: > // Actually declared in validation.cpp; can't include because of circular dependency. > extern RecursiveMutex cs_main; Ultimately, no more need to include `validation.h` (which also includes (heavy/boost filled) `txmempool.h`) everywhere for `cs_main`. See #26087 for another example of why that is useful. ACKs for top commit: ajtowns: ACK 282019c Tree-SHA512: 142835b794873e7a09c3246d6101843ae81ec0c6295e6873130c98a2abfa5f7282748d0f1a37237a779cc71c3bc0a75d03b20313ef5398c83d4814215cbc8287
2 parents 2182149 + 282019c commit 6b7ccb9

17 files changed

+44
-27
lines changed

src/Makefile.am

+3
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ BITCOIN_CORE_H = \
177177
kernel/checks.h \
178178
kernel/coinstats.h \
179179
kernel/context.h \
180+
kernel/cs_main.h \
180181
kernel/mempool_entry.h \
181182
kernel/mempool_limits.h \
182183
kernel/mempool_options.h \
@@ -375,6 +376,7 @@ libbitcoin_node_a_SOURCES = \
375376
kernel/checks.cpp \
376377
kernel/coinstats.cpp \
377378
kernel/context.cpp \
379+
kernel/cs_main.cpp \
378380
kernel/mempool_persist.cpp \
379381
mapport.cpp \
380382
net.cpp \
@@ -906,6 +908,7 @@ libbitcoinkernel_la_SOURCES = \
906908
kernel/checks.cpp \
907909
kernel/coinstats.cpp \
908910
kernel/context.cpp \
911+
kernel/cs_main.cpp \
909912
kernel/mempool_persist.cpp \
910913
key.cpp \
911914
logging.cpp \

src/bench/rpc_mempool.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <bench/bench.h>
66
#include <chainparamsbase.h>
7+
#include <kernel/cs_main.h>
78
#include <kernel/mempool_entry.h>
89
#include <rpc/mempool.h>
910
#include <test/util/setup_common.h>

src/chain.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <arith_uint256.h>
1010
#include <consensus/params.h>
1111
#include <flatfile.h>
12+
#include <kernel/cs_main.h>
1213
#include <primitives/block.h>
1314
#include <sync.h>
1415
#include <uint256.h>
@@ -38,8 +39,6 @@ static constexpr int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME;
3839
*/
3940
static constexpr int64_t MAX_BLOCK_TIME_GAP = 90 * 60;
4041

41-
extern RecursiveMutex cs_main;
42-
4342
class CBlockFileInfo
4443
{
4544
public:

src/kernel/cs_main.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) 2023 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <sync.h>
6+
7+
RecursiveMutex cs_main;

src/kernel/cs_main.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2023 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_KERNEL_CS_MAIN_H
6+
#define BITCOIN_KERNEL_CS_MAIN_H
7+
8+
#include <sync.h>
9+
10+
/**
11+
* Mutex to guard access to validation specific variables, such as reading
12+
* or changing the chainstate.
13+
*
14+
* This may also need to be locked when updating the transaction pool, e.g. on
15+
* AcceptToMemoryPool. See CTxMemPool::cs comment for details.
16+
*
17+
* The transaction pool has a separate lock to allow reading from it and the
18+
* chainstate at the same time.
19+
*/
20+
extern RecursiveMutex cs_main;
21+
22+
#endif // BITCOIN_KERNEL_CS_MAIN_H

src/node/blockstorage.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <attributes.h>
99
#include <chain.h>
1010
#include <fs.h>
11+
#include <kernel/cs_main.h>
1112
#include <protocol.h>
1213
#include <sync.h>
1314
#include <txdb.h>
@@ -17,8 +18,6 @@
1718
#include <unordered_map>
1819
#include <vector>
1920

20-
extern RecursiveMutex cs_main;
21-
2221
class ArgsManager;
2322
class BlockValidationState;
2423
class CBlock;

src/rpc/blockchain.h

-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
#include <stdint.h>
1717
#include <vector>
1818

19-
extern RecursiveMutex cs_main;
20-
2119
class CBlock;
2220
class CBlockIndex;
2321
class Chainstate;

src/rpc/node.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <interfaces/echo.h>
1313
#include <interfaces/init.h>
1414
#include <interfaces/ipc.h>
15+
#include <kernel/cs_main.h>
1516
#include <node/context.h>
1617
#include <rpc/server.h>
1718
#include <rpc/server_util.h>

src/test/mempool_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ BOOST_AUTO_TEST_CASE(MempoolRemoveTest)
6464

6565

6666
CTxMemPool& testPool = *Assert(m_node.mempool);
67-
LOCK2(cs_main, testPool.cs);
67+
LOCK2(::cs_main, testPool.cs);
6868

6969
// Nothing in pool, remove should do nothing:
7070
unsigned int poolSize = testPool.size();

src/test/txvalidationcache_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend, Dersig100Setup)
117117
// should fail.
118118
// Capture this interaction with the upgraded_nop argument: set it when evaluating
119119
// any script flag that is implemented as an upgraded NOP code.
120-
static void ValidateCheckInputsForAllFlags(const CTransaction &tx, uint32_t failing_flags, bool add_to_cache, CCoinsViewCache& active_coins_tip) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
120+
static void ValidateCheckInputsForAllFlags(const CTransaction &tx, uint32_t failing_flags, bool add_to_cache, CCoinsViewCache& active_coins_tip) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
121121
{
122122
PrecomputedTransactionData txdata;
123123

src/test/validation_chainstatemanager_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches)
116116

117117
// Create a legacy (IBD) chainstate.
118118
//
119-
Chainstate& c1 = WITH_LOCK(cs_main, return manager.InitializeChainstate(&mempool));
119+
Chainstate& c1 = WITH_LOCK(::cs_main, return manager.InitializeChainstate(&mempool));
120120
chainstates.push_back(&c1);
121121
c1.InitCoinsDB(
122122
/*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);

src/txdb.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <coins.h>
1010
#include <dbwrapper.h>
11+
#include <kernel/cs_main.h>
1112
#include <sync.h>
1213
#include <fs.h>
1314

@@ -44,9 +45,6 @@ static const int64_t max_filter_index_cache = 1024;
4445
//! Max memory allocated to coin DB specific cache (MiB)
4546
static const int64_t nMaxCoinsDBCache = 8;
4647

47-
// Actually declared in validation.cpp; can't include because of circular dependency.
48-
extern RecursiveMutex cs_main;
49-
5048
/** CCoinsView backed by the coin database (chainstate/) */
5149
class CCoinsViewDB final : public CCoinsView
5250
{

src/txmempool.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <coins.h>
2222
#include <consensus/amount.h>
2323
#include <indirectmap.h>
24+
#include <kernel/cs_main.h>
2425
#include <kernel/mempool_entry.h>
2526
#include <policy/feerate.h>
2627
#include <policy/packages.h>
@@ -39,7 +40,6 @@
3940
class CBlockIndex;
4041
class CChain;
4142
class Chainstate;
42-
extern RecursiveMutex cs_main;
4343

4444
/** Fake height value used in Coin to signify they are only in the memory pool (since 0.8) */
4545
static const uint32_t MEMPOOL_HEIGHT = 0x7FFFFFFF;

src/validation.cpp

-12
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,6 @@ const std::vector<std::string> CHECKLEVEL_DOC {
107107
* */
108108
static constexpr int PRUNE_LOCK_BUFFER{10};
109109

110-
/**
111-
* Mutex to guard access to validation specific variables, such as reading
112-
* or changing the chainstate.
113-
*
114-
* This may also need to be locked when updating the transaction pool, e.g. on
115-
* AcceptToMemoryPool. See CTxMemPool::cs comment for details.
116-
*
117-
* The transaction pool has a separate lock to allow reading from it and the
118-
* chainstate at the same time.
119-
*/
120-
RecursiveMutex cs_main;
121-
122110
GlobalMutex g_best_block_mutex;
123111
std::condition_variable g_best_block_cv;
124112
uint256 g_best_block;

src/validation.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <consensus/amount.h>
1919
#include <deploymentstatus.h>
2020
#include <fs.h>
21+
#include <kernel/cs_main.h> // IWYU pragma: export
2122
#include <node/blockstorage.h>
2223
#include <policy/feerate.h>
2324
#include <policy/packages.h>
@@ -86,7 +87,6 @@ enum class SynchronizationState {
8687
POST_INIT
8788
};
8889

89-
extern RecursiveMutex cs_main;
9090
extern GlobalMutex g_best_block_mutex;
9191
extern std::condition_variable g_best_block_cv;
9292
/** Used to notify getblocktemplate RPC of new tips. */

src/validationinterface.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
#ifndef BITCOIN_VALIDATIONINTERFACE_H
77
#define BITCOIN_VALIDATIONINTERFACE_H
88

9+
#include <kernel/cs_main.h>
910
#include <primitives/transaction.h> // CTransaction(Ref)
1011
#include <sync.h>
1112

1213
#include <functional>
1314
#include <memory>
1415

15-
extern RecursiveMutex cs_main;
1616
class BlockValidationState;
1717
class CBlock;
1818
class CBlockIndex;

src/zmq/zmqpublishnotifier.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <chain.h>
88
#include <chainparams.h>
99
#include <crypto/common.h>
10+
#include <kernel/cs_main.h>
1011
#include <logging.h>
1112
#include <netaddress.h>
1213
#include <netbase.h>

0 commit comments

Comments
 (0)