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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 22 additions & 0 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)