@@ -76,7 +76,7 @@ std::map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(g_cs_orphans);
76
76
void EraseOrphansFor (NodeId peer);
77
77
78
78
/* * Increase a node's misbehavior score. */
79
- void Misbehaving (NodeId nodeid, int howmuch, const std::string& message=" " );
79
+ void Misbehaving (NodeId nodeid, int howmuch, const std::string& message=" " ) EXCLUSIVE_LOCKS_REQUIRED(cs_main) ;
80
80
81
81
/* * Average delay between local address broadcasts in seconds. */
82
82
static constexpr unsigned int AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL = 24 * 60 * 60 ;
@@ -96,22 +96,20 @@ static constexpr unsigned int MAX_FEEFILTER_CHANGE_DELAY = 5 * 60;
96
96
// Internal stuff
97
97
namespace {
98
98
/* * Number of nodes with fSyncStarted. */
99
- int nSyncStarted = 0 ;
99
+ int nSyncStarted GUARDED_BY (cs_main) = 0;
100
100
101
101
/* *
102
102
* Sources of received blocks, saved to be able to send them reject
103
- * messages or ban them when processing happens afterwards. Protected by
104
- * cs_main.
103
+ * messages or ban them when processing happens afterwards.
105
104
* Set mapBlockSource[hash].second to false if the node should not be
106
105
* punished if the block is invalid.
107
106
*/
108
- std::map<uint256, std::pair<NodeId, bool >> mapBlockSource;
107
+ std::map<uint256, std::pair<NodeId, bool >> mapBlockSource GUARDED_BY (cs_main) ;
109
108
110
109
/* *
111
110
* Filter for transactions that were recently rejected by
112
111
* AcceptToMemoryPool. These are not rerequested until the chain tip
113
- * changes, at which point the entire filter is reset. Protected by
114
- * cs_main.
112
+ * changes, at which point the entire filter is reset.
115
113
*
116
114
* Without this filter we'd be re-requesting txs from each of our peers,
117
115
* increasing bandwidth consumption considerably. For instance, with 100
@@ -127,38 +125,38 @@ namespace {
127
125
*
128
126
* Memory used: 1.3 MB
129
127
*/
130
- std::unique_ptr<CRollingBloomFilter> recentRejects;
131
- uint256 hashRecentRejectsChainTip;
128
+ std::unique_ptr<CRollingBloomFilter> recentRejects GUARDED_BY (cs_main) ;
129
+ uint256 hashRecentRejectsChainTip GUARDED_BY (cs_main) ;
132
130
133
- /* * Blocks that are in flight, and that are in the queue to be downloaded. Protected by cs_main. */
131
+ /* * Blocks that are in flight, and that are in the queue to be downloaded. */
134
132
struct QueuedBlock {
135
133
uint256 hash;
136
134
const CBlockIndex* pindex; // !< Optional.
137
135
bool fValidatedHeaders ; // !< Whether this block has validated headers at the time of request.
138
136
std::unique_ptr<PartiallyDownloadedBlock> partialBlock; // !< Optional, used for CMPCTBLOCK downloads
139
137
};
140
- std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> > mapBlocksInFlight;
138
+ std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> > mapBlocksInFlight GUARDED_BY (cs_main) ;
141
139
142
140
/* * Stack of nodes which we have set to announce using compact blocks */
143
- std::list<NodeId> lNodesAnnouncingHeaderAndIDs;
141
+ std::list<NodeId> lNodesAnnouncingHeaderAndIDs GUARDED_BY (cs_main) ;
144
142
145
143
/* * Number of preferable block download peers. */
146
- int nPreferredDownload = 0 ;
144
+ int nPreferredDownload GUARDED_BY (cs_main) = 0;
147
145
148
146
/* * Number of peers from which we're downloading blocks. */
149
- int nPeersWithValidatedDownloads = 0 ;
147
+ int nPeersWithValidatedDownloads GUARDED_BY (cs_main) = 0;
150
148
151
149
/* * Number of outbound peers with m_chain_sync.m_protect. */
152
- int g_outbound_peers_with_protect_from_disconnect = 0 ;
150
+ int g_outbound_peers_with_protect_from_disconnect GUARDED_BY (cs_main) = 0;
153
151
154
152
/* * When our tip was last updated. */
155
153
std::atomic<int64_t > g_last_tip_update (0 );
156
154
157
- /* * Relay map, protected by cs_main. */
155
+ /* * Relay map */
158
156
typedef std::map<uint256, CTransactionRef> MapRelay;
159
- MapRelay mapRelay;
160
- /* * Expiration-time ordered list of (expire time, relay map entry) pairs, protected by cs_main) . */
161
- std::deque<std::pair<int64_t , MapRelay::iterator>> vRelayExpiration;
157
+ MapRelay mapRelay GUARDED_BY (cs_main) ;
158
+ /* * Expiration-time ordered list of (expire time, relay map entry) pairs. */
159
+ std::deque<std::pair<int64_t , MapRelay::iterator>> vRelayExpiration GUARDED_BY (cs_main) ;
162
160
163
161
std::atomic<int64_t > nTimeBestReceived (0 ); // Used only to inform the wallet of when we last received a block
164
162
@@ -302,18 +300,17 @@ struct CNodeState {
302
300
}
303
301
};
304
302
305
- /* * Map maintaining per-node state. Requires cs_main. */
306
- static std::map<NodeId, CNodeState> mapNodeState;
303
+ /* * Map maintaining per-node state. */
304
+ static std::map<NodeId, CNodeState> mapNodeState GUARDED_BY (cs_main) ;
307
305
308
- // Requires cs_main.
309
- static CNodeState *State (NodeId pnode) {
306
+ static CNodeState *State (NodeId pnode) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
310
307
std::map<NodeId, CNodeState>::iterator it = mapNodeState.find (pnode);
311
308
if (it == mapNodeState.end ())
312
309
return nullptr ;
313
310
return &it->second ;
314
311
}
315
312
316
- static void UpdatePreferredDownload (CNode* node, CNodeState* state)
313
+ static void UpdatePreferredDownload (CNode* node, CNodeState* state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
317
314
{
318
315
nPreferredDownload -= state->fPreferredDownload ;
319
316
@@ -344,10 +341,9 @@ static void PushNodeVersion(CNode *pnode, CConnman* connman, int64_t nTime)
344
341
}
345
342
}
346
343
347
- // Requires cs_main.
348
344
// Returns a bool indicating whether we requested this block.
349
345
// Also used if a block was /not/ received and timed out or started with another peer
350
- static bool MarkBlockAsReceived (const uint256& hash) {
346
+ static bool MarkBlockAsReceived (const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
351
347
std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find (hash);
352
348
if (itInFlight != mapBlocksInFlight.end ()) {
353
349
CNodeState *state = State (itInFlight->second .first );
@@ -370,10 +366,9 @@ static bool MarkBlockAsReceived(const uint256& hash) {
370
366
return false ;
371
367
}
372
368
373
- // Requires cs_main.
374
369
// returns false, still setting pit, if the block was already in flight from the same peer
375
370
// pit will only be valid as long as the same cs_main lock is being held
376
- static bool MarkBlockAsInFlight (NodeId nodeid, const uint256& hash, const CBlockIndex* pindex = nullptr , std::list<QueuedBlock>::iterator** pit = nullptr ) {
371
+ static bool MarkBlockAsInFlight (NodeId nodeid, const uint256& hash, const CBlockIndex* pindex = nullptr , std::list<QueuedBlock>::iterator** pit = nullptr ) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
377
372
CNodeState *state = State (nodeid);
378
373
assert (state != nullptr );
379
374
@@ -407,7 +402,7 @@ static bool MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const CBlock
407
402
}
408
403
409
404
/* * Check whether the last unknown block a peer advertised is not yet known. */
410
- static void ProcessBlockAvailability (NodeId nodeid) {
405
+ static void ProcessBlockAvailability (NodeId nodeid) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
411
406
CNodeState *state = State (nodeid);
412
407
assert (state != nullptr );
413
408
@@ -423,7 +418,7 @@ static void ProcessBlockAvailability(NodeId nodeid) {
423
418
}
424
419
425
420
/* * Update tracking information about which blocks a peer is assumed to have. */
426
- static void UpdateBlockAvailability (NodeId nodeid, const uint256 &hash) {
421
+ static void UpdateBlockAvailability (NodeId nodeid, const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
427
422
CNodeState *state = State (nodeid);
428
423
assert (state != nullptr );
429
424
@@ -447,7 +442,8 @@ static void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
447
442
* lNodesAnnouncingHeaderAndIDs, and keeping that list under a certain size by
448
443
* removing the first element if necessary.
449
444
*/
450
- static void MaybeSetPeerAsAnnouncingHeaderAndIDs (NodeId nodeid, CConnman* connman) {
445
+ static void MaybeSetPeerAsAnnouncingHeaderAndIDs (NodeId nodeid, CConnman* connman)
446
+ {
451
447
AssertLockHeld (cs_main);
452
448
CNodeState* nodestate = State (nodeid);
453
449
if (!nodestate || !nodestate->fSupportsDesiredCmpctVersion ) {
@@ -463,11 +459,13 @@ static void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid, CConnman* connma
463
459
}
464
460
}
465
461
connman->ForNode (nodeid, [connman](CNode* pfrom){
462
+ AssertLockHeld (cs_main);
466
463
uint64_t nCMPCTBLOCKVersion = (pfrom->GetLocalServices () & NODE_WITNESS) ? 2 : 1 ;
467
464
if (lNodesAnnouncingHeaderAndIDs.size () >= 3 ) {
468
465
// As per BIP152, we only get 3 of our peers to announce
469
466
// blocks using compact encodings.
470
467
connman->ForNode (lNodesAnnouncingHeaderAndIDs.front (), [connman, nCMPCTBLOCKVersion](CNode* pnodeStop){
468
+ AssertLockHeld (cs_main);
471
469
connman->PushMessage (pnodeStop, CNetMsgMaker (pnodeStop->GetSendVersion ()).Make (NetMsgType::SENDCMPCT, /* fAnnounceUsingCMPCTBLOCK=*/ false , nCMPCTBLOCKVersion));
472
470
return true ;
473
471
});
@@ -480,7 +478,7 @@ static void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid, CConnman* connma
480
478
}
481
479
}
482
480
483
- static bool TipMayBeStale (const Consensus::Params &consensusParams)
481
+ static bool TipMayBeStale (const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
484
482
{
485
483
AssertLockHeld (cs_main);
486
484
if (g_last_tip_update == 0 ) {
@@ -489,14 +487,12 @@ static bool TipMayBeStale(const Consensus::Params &consensusParams)
489
487
return g_last_tip_update < GetTime () - consensusParams.nPowTargetSpacing * 3 && mapBlocksInFlight.empty ();
490
488
}
491
489
492
- // Requires cs_main
493
- static bool CanDirectFetch (const Consensus::Params &consensusParams)
490
+ static bool CanDirectFetch (const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
494
491
{
495
492
return chainActive.Tip ()->GetBlockTime () > GetAdjustedTime () - consensusParams.nPowTargetSpacing * 20 ;
496
493
}
497
494
498
- // Requires cs_main
499
- static bool PeerHasHeader (CNodeState *state, const CBlockIndex *pindex)
495
+ static bool PeerHasHeader (CNodeState *state, const CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
500
496
{
501
497
if (state->pindexBestKnownBlock && pindex == state->pindexBestKnownBlock ->GetAncestor (pindex->nHeight ))
502
498
return true ;
@@ -507,7 +503,8 @@ static bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex)
507
503
508
504
/* * Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
509
505
* at most count entries. */
510
- static void FindNextBlocksToDownload (NodeId nodeid, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) {
506
+ static void FindNextBlocksToDownload (NodeId nodeid, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
507
+ {
511
508
if (count == 0 )
512
509
return ;
513
510
@@ -797,10 +794,8 @@ unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
797
794
798
795
/* *
799
796
* Mark a misbehaving peer to be banned depending upon the value of `-banscore`.
800
- *
801
- * Requires cs_main.
802
797
*/
803
- void Misbehaving (NodeId pnode, int howmuch, const std::string& message)
798
+ void Misbehaving (NodeId pnode, int howmuch, const std::string& message) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
804
799
{
805
800
if (howmuch == 0 )
806
801
return ;
@@ -898,10 +893,10 @@ void PeerLogicValidation::BlockConnected(const std::shared_ptr<const CBlock>& pb
898
893
899
894
// All of the following cache a recent block, and are protected by cs_most_recent_block
900
895
static CCriticalSection cs_most_recent_block;
901
- static std::shared_ptr<const CBlock> most_recent_block;
902
- static std::shared_ptr<const CBlockHeaderAndShortTxIDs> most_recent_compact_block;
903
- static uint256 most_recent_block_hash;
904
- static bool fWitnessesPresentInMostRecentCompactBlock ;
896
+ static std::shared_ptr<const CBlock> most_recent_block GUARDED_BY (cs_most_recent_block) ;
897
+ static std::shared_ptr<const CBlockHeaderAndShortTxIDs> most_recent_compact_block GUARDED_BY (cs_most_recent_block) ;
898
+ static uint256 most_recent_block_hash GUARDED_BY (cs_most_recent_block) ;
899
+ static bool fWitnessesPresentInMostRecentCompactBlock GUARDED_BY (cs_most_recent_block) ;
905
900
906
901
/* *
907
902
* Maintain state about the best-seen block and fast-announce a compact block
@@ -930,6 +925,8 @@ void PeerLogicValidation::NewPoWValidBlock(const CBlockIndex *pindex, const std:
930
925
}
931
926
932
927
connman->ForEachNode ([this , &pcmpctblock, pindex, &msgMaker, fWitnessEnabled , &hashBlock](CNode* pnode) {
928
+ AssertLockHeld (cs_main);
929
+
933
930
// TODO: Avoid the repeated-serialization here
934
931
if (pnode->nVersion < INVALID_CB_NO_BAN_VERSION || pnode->fDisconnect )
935
932
return ;
@@ -1327,7 +1324,7 @@ void static ProcessGetData(CNode* pfrom, const CChainParams& chainparams, CConnm
1327
1324
}
1328
1325
}
1329
1326
1330
- static uint32_t GetFetchFlags (CNode* pfrom) {
1327
+ static uint32_t GetFetchFlags (CNode* pfrom) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
1331
1328
uint32_t nFetchFlags = 0 ;
1332
1329
if ((pfrom->GetLocalServices () & NODE_WITNESS) && State (pfrom->GetId ())->fHaveWitness ) {
1333
1330
nFetchFlags |= MSG_WITNESS_FLAG;
@@ -3160,6 +3157,8 @@ void PeerLogicValidation::EvictExtraOutboundPeers(int64_t time_in_seconds)
3160
3157
LOCK (cs_main);
3161
3158
3162
3159
connman->ForEachNode ([&](CNode* pnode) {
3160
+ AssertLockHeld (cs_main);
3161
+
3163
3162
// Ignore non-outbound peers, or nodes marked for disconnect already
3164
3163
if (!IsOutboundDisconnectionCandidate (pnode) || pnode->fDisconnect ) return ;
3165
3164
CNodeState *state = State (pnode->GetId ());
@@ -3173,6 +3172,8 @@ void PeerLogicValidation::EvictExtraOutboundPeers(int64_t time_in_seconds)
3173
3172
});
3174
3173
if (worst_peer != -1 ) {
3175
3174
bool disconnected = connman->ForNode (worst_peer, [&](CNode *pnode) {
3175
+ AssertLockHeld (cs_main);
3176
+
3176
3177
// Only disconnect a peer that has been connected to us for
3177
3178
// some reasonable fraction of our check-frequency, to give
3178
3179
// it time for new information to have arrived.
0 commit comments