@@ -286,8 +286,10 @@ bool CheckSequenceLocks(CBlockIndex* tip,
286
286
static unsigned int GetBlockScriptFlags (const CBlockIndex* pindex, const Consensus::Params& chainparams);
287
287
288
288
static void LimitMempoolSize (CTxMemPool& pool, CCoinsViewCache& coins_cache, size_t limit, std::chrono::seconds age)
289
- EXCLUSIVE_LOCKS_REQUIRED(pool.cs, ::cs_main)
289
+ EXCLUSIVE_LOCKS_REQUIRED(::cs_main, pool.cs )
290
290
{
291
+ AssertLockHeld (::cs_main);
292
+ AssertLockHeld (pool.cs );
291
293
int expired = pool.Expire (GetTime<std::chrono::seconds>() - age);
292
294
if (expired != 0 ) {
293
295
LogPrint (BCLog::MEMPOOL, " Expired %i transactions from the memory pool\n " , expired);
@@ -628,8 +630,10 @@ class MemPoolAccept
628
630
EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
629
631
630
632
// Compare a package's feerate against minimum allowed.
631
- bool CheckFeeRate (size_t package_size, CAmount package_fee, TxValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs)
633
+ bool CheckFeeRate (size_t package_size, CAmount package_fee, TxValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(:: cs_main, m_pool.cs)
632
634
{
635
+ AssertLockHeld (::cs_main);
636
+ AssertLockHeld (m_pool.cs );
633
637
CAmount mempoolRejectFee = m_pool.GetMinFee (gArgs .GetIntArg (" -maxmempool" , DEFAULT_MAX_MEMPOOL_SIZE) * 1000000 ).GetFee (package_size);
634
638
if (mempoolRejectFee > 0 && package_fee < mempoolRejectFee) {
635
639
return state.Invalid (TxValidationResult::TX_MEMPOOL_POLICY, " mempool min fee not met" , strprintf (" %d < %d" , package_fee, mempoolRejectFee));
@@ -663,6 +667,8 @@ class MemPoolAccept
663
667
664
668
bool MemPoolAccept::PreChecks (ATMPArgs& args, Workspace& ws)
665
669
{
670
+ AssertLockHeld (cs_main);
671
+ AssertLockHeld (m_pool.cs );
666
672
const CTransactionRef& ptx = ws.m_ptx ;
667
673
const CTransaction& tx = *ws.m_ptx ;
668
674
const uint256& hash = ws.m_hash ;
@@ -963,6 +969,8 @@ bool MemPoolAccept::PackageMempoolChecks(const std::vector<CTransactionRef>& txn
963
969
964
970
bool MemPoolAccept::PolicyScriptChecks (const ATMPArgs& args, Workspace& ws)
965
971
{
972
+ AssertLockHeld (cs_main);
973
+ AssertLockHeld (m_pool.cs );
966
974
const CTransaction& tx = *ws.m_ptx ;
967
975
TxValidationState& state = ws.m_state ;
968
976
@@ -989,6 +997,8 @@ bool MemPoolAccept::PolicyScriptChecks(const ATMPArgs& args, Workspace& ws)
989
997
990
998
bool MemPoolAccept::ConsensusScriptChecks (const ATMPArgs& args, Workspace& ws)
991
999
{
1000
+ AssertLockHeld (cs_main);
1001
+ AssertLockHeld (m_pool.cs );
992
1002
const CTransaction& tx = *ws.m_ptx ;
993
1003
const uint256& hash = ws.m_hash ;
994
1004
TxValidationState& state = ws.m_state ;
@@ -1021,6 +1031,8 @@ bool MemPoolAccept::ConsensusScriptChecks(const ATMPArgs& args, Workspace& ws)
1021
1031
1022
1032
bool MemPoolAccept::Finalize (const ATMPArgs& args, Workspace& ws)
1023
1033
{
1034
+ AssertLockHeld (cs_main);
1035
+ AssertLockHeld (m_pool.cs );
1024
1036
const CTransaction& tx = *ws.m_ptx ;
1025
1037
const uint256& hash = ws.m_hash ;
1026
1038
TxValidationState& state = ws.m_state ;
@@ -1342,8 +1354,9 @@ PackageMempoolAcceptResult MemPoolAccept::AcceptPackage(const Package& package,
1342
1354
1343
1355
MempoolAcceptResult AcceptToMemoryPool (CChainState& active_chainstate, const CTransactionRef& tx,
1344
1356
int64_t accept_time, bool bypass_limits, bool test_accept)
1345
- EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1357
+ EXCLUSIVE_LOCKS_REQUIRED(:: cs_main)
1346
1358
{
1359
+ AssertLockHeld (::cs_main);
1347
1360
const CChainParams& chainparams{active_chainstate.m_params };
1348
1361
assert (active_chainstate.GetMempool () != nullptr );
1349
1362
CTxMemPool& pool{*active_chainstate.GetMempool ()};
@@ -1421,6 +1434,7 @@ CoinsViews::CoinsViews(
1421
1434
1422
1435
void CoinsViews::InitCache ()
1423
1436
{
1437
+ AssertLockHeld (::cs_main);
1424
1438
m_cacheview = std::make_unique<CCoinsViewCache>(&m_catcherview);
1425
1439
}
1426
1440
@@ -1451,6 +1465,7 @@ void CChainState::InitCoinsDB(
1451
1465
1452
1466
void CChainState::InitCoinsCache (size_t cache_size_bytes)
1453
1467
{
1468
+ AssertLockHeld (::cs_main);
1454
1469
assert (m_coins_views != nullptr );
1455
1470
m_coinstip_cache_size_bytes = cache_size_bytes;
1456
1471
m_coins_views->InitCache ();
@@ -1524,6 +1539,7 @@ void CChainState::CheckForkWarningConditions()
1524
1539
// Called both upon regular invalid block discovery *and* InvalidateBlock
1525
1540
void CChainState::InvalidChainFound (CBlockIndex* pindexNew)
1526
1541
{
1542
+ AssertLockHeld (cs_main);
1527
1543
if (!m_chainman.m_best_invalid || pindexNew->nChainWork > m_chainman.m_best_invalid ->nChainWork ) {
1528
1544
m_chainman.m_best_invalid = pindexNew;
1529
1545
}
@@ -1546,6 +1562,7 @@ void CChainState::InvalidChainFound(CBlockIndex* pindexNew)
1546
1562
// which does its own setBlockIndexCandidates management.
1547
1563
void CChainState::InvalidBlockFound (CBlockIndex* pindex, const BlockValidationState& state)
1548
1564
{
1565
+ AssertLockHeld (cs_main);
1549
1566
if (state.GetResult () != BlockValidationResult::BLOCK_MUTATED) {
1550
1567
pindex->nStatus |= BLOCK_FAILED_VALID;
1551
1568
m_chainman.m_failed_blocks .insert (pindex);
@@ -2209,6 +2226,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
2209
2226
2210
2227
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState ()
2211
2228
{
2229
+ AssertLockHeld (::cs_main);
2212
2230
return this ->GetCoinsCacheSizeState (
2213
2231
m_coinstip_cache_size_bytes,
2214
2232
gArgs .GetIntArg (" -maxmempool" , DEFAULT_MAX_MEMPOOL_SIZE) * 1000000 );
@@ -2218,6 +2236,7 @@ CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(
2218
2236
size_t max_coins_cache_size_bytes,
2219
2237
size_t max_mempool_size_bytes)
2220
2238
{
2239
+ AssertLockHeld (::cs_main);
2221
2240
const int64_t nMempoolUsage = m_mempool ? m_mempool->DynamicMemoryUsage () : 0 ;
2222
2241
int64_t cacheSize = CoinsTip ().DynamicMemoryUsage ();
2223
2242
int64_t nTotalSpace =
@@ -2426,6 +2445,7 @@ static void UpdateTipLog(
2426
2445
2427
2446
void CChainState::UpdateTip (const CBlockIndex* pindexNew)
2428
2447
{
2448
+ AssertLockHeld (::cs_main);
2429
2449
const auto & coins_tip = this ->CoinsTip ();
2430
2450
2431
2451
// The remainder of the function isn't relevant if we are not acting on
@@ -2649,7 +2669,9 @@ bool CChainState::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew
2649
2669
* Return the tip of the chain with the most work in it, that isn't
2650
2670
* known to be invalid (it's however far from certain to be valid).
2651
2671
*/
2652
- CBlockIndex* CChainState::FindMostWorkChain () {
2672
+ CBlockIndex* CChainState::FindMostWorkChain ()
2673
+ {
2674
+ AssertLockHeld (::cs_main);
2653
2675
do {
2654
2676
CBlockIndex *pindexNew = nullptr ;
2655
2677
@@ -2853,7 +2875,7 @@ bool CChainState::ActivateBestChain(BlockValidationState& state, std::shared_ptr
2853
2875
// far from a guarantee. Things in the P2P/RPC will often end up calling
2854
2876
// us in the middle of ProcessNewBlock - do not assume pblock is set
2855
2877
// sanely for performance or correctness!
2856
- AssertLockNotHeld (cs_main);
2878
+ AssertLockNotHeld (:: cs_main);
2857
2879
2858
2880
// ABC maintains a fair degree of expensive-to-calculate internal state
2859
2881
// because this function periodically releases cs_main so that it does not lock up other threads for too long
@@ -2949,6 +2971,8 @@ bool CChainState::ActivateBestChain(BlockValidationState& state, std::shared_ptr
2949
2971
2950
2972
bool CChainState::PreciousBlock (BlockValidationState& state, CBlockIndex* pindex)
2951
2973
{
2974
+ AssertLockNotHeld (m_chainstate_mutex);
2975
+ AssertLockNotHeld (::cs_main);
2952
2976
{
2953
2977
LOCK (cs_main);
2954
2978
if (pindex->nChainWork < m_chain.Tip ()->nChainWork ) {
@@ -2979,6 +3003,7 @@ bool CChainState::PreciousBlock(BlockValidationState& state, CBlockIndex* pindex
2979
3003
bool CChainState::InvalidateBlock (BlockValidationState& state, CBlockIndex* pindex)
2980
3004
{
2981
3005
AssertLockNotHeld (m_chainstate_mutex);
3006
+ AssertLockNotHeld (::cs_main);
2982
3007
2983
3008
// Genesis block can't be invalidated
2984
3009
assert (pindex);
@@ -3157,6 +3182,7 @@ void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) {
3157
3182
/* * Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS). */
3158
3183
void CChainState::ReceivedBlockTransactions (const CBlock& block, CBlockIndex* pindexNew, const FlatFilePos& pos)
3159
3184
{
3185
+ AssertLockHeld (cs_main);
3160
3186
pindexNew->nTx = block.vtx .size ();
3161
3187
pindexNew->nChainTx = 0 ;
3162
3188
pindexNew->nFile = pos.nFile ;
@@ -3329,8 +3355,9 @@ std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBloc
3329
3355
* in ConnectBlock().
3330
3356
* Note that -reindex-chainstate skips the validation that happens here!
3331
3357
*/
3332
- static bool ContextualCheckBlockHeader (const CBlockHeader& block, BlockValidationState& state, BlockManager& blockman, const CChainParams& params, const CBlockIndex* pindexPrev, int64_t nAdjustedTime) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
3358
+ static bool ContextualCheckBlockHeader (const CBlockHeader& block, BlockValidationState& state, BlockManager& blockman, const CChainParams& params, const CBlockIndex* pindexPrev, int64_t nAdjustedTime) EXCLUSIVE_LOCKS_REQUIRED(:: cs_main)
3333
3359
{
3360
+ AssertLockHeld (::cs_main);
3334
3361
assert (pindexPrev != nullptr );
3335
3362
const int nHeight = pindexPrev->nHeight + 1 ;
3336
3363
@@ -3701,6 +3728,7 @@ bool ChainstateManager::ProcessNewBlock(const CChainParams& chainparams, const s
3701
3728
3702
3729
MempoolAcceptResult ChainstateManager::ProcessTransaction (const CTransactionRef& tx, bool test_accept)
3703
3730
{
3731
+ AssertLockHeld (cs_main);
3704
3732
CChainState& active_chainstate = ActiveChainstate ();
3705
3733
if (!active_chainstate.GetMempool ()) {
3706
3734
TxValidationState state;
@@ -3914,6 +3942,7 @@ bool CVerifyDB::VerifyDB(
3914
3942
/* * Apply the effects of a block on the utxo cache, ignoring that it may already have been applied. */
3915
3943
bool CChainState::RollforwardBlock (const CBlockIndex* pindex, CCoinsViewCache& inputs)
3916
3944
{
3945
+ AssertLockHeld (cs_main);
3917
3946
// TODO: merge with ConnectBlock
3918
3947
CBlock block;
3919
3948
if (!ReadBlockFromDisk (block, pindex, m_params.GetConsensus ())) {
@@ -4017,7 +4046,9 @@ bool CChainState::NeedsRedownload() const
4017
4046
return false ;
4018
4047
}
4019
4048
4020
- void CChainState::UnloadBlockIndex () {
4049
+ void CChainState::UnloadBlockIndex ()
4050
+ {
4051
+ AssertLockHeld (::cs_main);
4021
4052
nBlockSequenceId = 1 ;
4022
4053
setBlockIndexCandidates.clear ();
4023
4054
}
@@ -4089,6 +4120,7 @@ bool CChainState::LoadGenesisBlock()
4089
4120
4090
4121
void CChainState::LoadExternalBlockFile (FILE* fileIn, FlatFilePos* dbp)
4091
4122
{
4123
+ AssertLockNotHeld (m_chainstate_mutex);
4092
4124
// Map of disk positions for blocks with unknown parent (only used for reindex)
4093
4125
static std::multimap<uint256, FlatFilePos> mapBlocksUnknownParent;
4094
4126
int64_t nStart = GetTimeMillis ();
@@ -4429,6 +4461,7 @@ void CChainState::CheckBlockIndex()
4429
4461
4430
4462
std::string CChainState::ToString ()
4431
4463
{
4464
+ AssertLockHeld (::cs_main);
4432
4465
CBlockIndex* tip = m_chain.Tip ();
4433
4466
return strprintf (" Chainstate [%s] @ height %d (%s)" ,
4434
4467
m_from_snapshot_blockhash ? " snapshot" : " ibd" ,
@@ -4437,6 +4470,7 @@ std::string CChainState::ToString()
4437
4470
4438
4471
bool CChainState::ResizeCoinsCaches (size_t coinstip_size, size_t coinsdb_size)
4439
4472
{
4473
+ AssertLockHeld (::cs_main);
4440
4474
if (coinstip_size == m_coinstip_cache_size_bytes &&
4441
4475
coinsdb_size == m_coinsdb_cache_size_bytes) {
4442
4476
// Cache sizes are unchanged, no need to continue.
@@ -4661,6 +4695,7 @@ std::vector<CChainState*> ChainstateManager::GetAll()
4661
4695
CChainState& ChainstateManager::InitializeChainstate (
4662
4696
CTxMemPool* mempool, const std::optional<uint256>& snapshot_blockhash)
4663
4697
{
4698
+ AssertLockHeld (::cs_main);
4664
4699
bool is_snapshot = snapshot_blockhash.has_value ();
4665
4700
std::unique_ptr<CChainState>& to_modify =
4666
4701
is_snapshot ? m_snapshot_chainstate : m_ibd_chainstate;
@@ -4998,6 +5033,7 @@ bool ChainstateManager::IsSnapshotActive() const
4998
5033
4999
5034
void ChainstateManager::Unload ()
5000
5035
{
5036
+ AssertLockHeld (::cs_main);
5001
5037
for (CChainState* chainstate : this ->GetAll ()) {
5002
5038
chainstate->m_chain .SetTip (nullptr );
5003
5039
chainstate->UnloadBlockIndex ();
@@ -5019,6 +5055,7 @@ void ChainstateManager::Reset()
5019
5055
5020
5056
void ChainstateManager::MaybeRebalanceCaches ()
5021
5057
{
5058
+ AssertLockHeld (::cs_main);
5022
5059
if (m_ibd_chainstate && !m_snapshot_chainstate) {
5023
5060
LogPrintf (" [snapshot] allocating all cache to the IBD chainstate\n " );
5024
5061
// Allocate everything to the IBD chainstate.
0 commit comments