@@ -474,6 +474,7 @@ unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
474
474
475
475
bool IsStandardTx (const CTransaction& tx, string& reason)
476
476
{
477
+ AssertLockHeld (cs_main);
477
478
if (tx.nVersion > CTransaction::CURRENT_VERSION || tx.nVersion < 1 ) {
478
479
reason = " version" ;
479
480
return false ;
@@ -556,6 +557,7 @@ bool IsStandardTx(const CTransaction& tx, string& reason)
556
557
557
558
bool IsFinalTx (const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
558
559
{
560
+ AssertLockHeld (cs_main);
559
561
// Time based nLockTime implemented in 0.1.6
560
562
if (tx.nLockTime == 0 )
561
563
return true ;
@@ -667,6 +669,7 @@ unsigned int GetP2SHSigOpCount(const CTransaction& tx, CCoinsViewCache& inputs)
667
669
668
670
int CMerkleTx::SetMerkleBranch (const CBlock* pblock)
669
671
{
672
+ AssertLockHeld (cs_main);
670
673
CBlock blockTmp;
671
674
672
675
if (pblock == NULL ) {
@@ -813,6 +816,7 @@ int64_t GetMinFee(const CTransaction& tx, unsigned int nBytes, bool fAllowFree,
813
816
bool AcceptToMemoryPool (CTxMemPool& pool, CValidationState &state, const CTransaction &tx, bool fLimitFree ,
814
817
bool * pfMissingInputs, bool fRejectInsaneFee )
815
818
{
819
+ AssertLockHeld (cs_main);
816
820
if (pfMissingInputs)
817
821
*pfMissingInputs = false ;
818
822
@@ -958,6 +962,7 @@ int CMerkleTx::GetDepthInMainChainINTERNAL(CBlockIndex* &pindexRet) const
958
962
{
959
963
if (hashBlock == 0 || nIndex == -1 )
960
964
return 0 ;
965
+ AssertLockHeld (cs_main);
961
966
962
967
// Find the block it claims to be in
963
968
map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find (hashBlock);
@@ -981,6 +986,7 @@ int CMerkleTx::GetDepthInMainChainINTERNAL(CBlockIndex* &pindexRet) const
981
986
982
987
int CMerkleTx::GetDepthInMainChain (CBlockIndex* &pindexRet) const
983
988
{
989
+ AssertLockHeld (cs_main);
984
990
int nResult = GetDepthInMainChainINTERNAL (pindexRet);
985
991
if (nResult == 0 && !mempool.exists (GetHash ()))
986
992
return -1 ; // Not in chain, not in mempool
@@ -1304,6 +1310,7 @@ int GetNumBlocksOfPeers()
1304
1310
1305
1311
bool IsInitialBlockDownload ()
1306
1312
{
1313
+ LOCK (cs_main);
1307
1314
if (fImporting || fReindex || chainActive.Height () < Checkpoints::GetTotalBlocksEstimate ())
1308
1315
return true ;
1309
1316
static int64_t nLastUpdate;
@@ -1323,6 +1330,7 @@ CBlockIndex *pindexBestForkTip = NULL, *pindexBestForkBase = NULL;
1323
1330
1324
1331
void CheckForkWarningConditions ()
1325
1332
{
1333
+ AssertLockHeld (cs_main);
1326
1334
// Before we get past initial download, we cannot reliably alert about forks
1327
1335
// (we assume we don't get stuck on a fork before the last checkpoint)
1328
1336
if (IsInitialBlockDownload ())
@@ -1368,6 +1376,7 @@ void CheckForkWarningConditions()
1368
1376
1369
1377
void CheckForkWarningConditionsOnNewFork (CBlockIndex* pindexNewForkTip)
1370
1378
{
1379
+ AssertLockHeld (cs_main);
1371
1380
// If we are on a fork that is sufficiently large, set a warning flag
1372
1381
CBlockIndex* pfork = pindexNewForkTip;
1373
1382
CBlockIndex* plonger = chainActive.Tip ();
@@ -2078,6 +2087,7 @@ void static FindMostWorkChain() {
2078
2087
2079
2088
// Try to activate to the most-work chain (thereby connecting it).
2080
2089
bool ActivateBestChain (CValidationState &state) {
2090
+ LOCK (cs_main);
2081
2091
CBlockIndex *pindexOldTip = chainActive.Tip ();
2082
2092
bool fComplete = false ;
2083
2093
while (!fComplete ) {
@@ -2162,6 +2172,7 @@ bool AddToBlockIndex(CBlock& block, CValidationState& state, const CDiskBlockPos
2162
2172
if (!ActivateBestChain (state))
2163
2173
return false ;
2164
2174
2175
+ LOCK (cs_main);
2165
2176
if (pindexNew == chainActive.Tip ())
2166
2177
{
2167
2178
// Clear fork warning if its no longer applicable
@@ -2344,6 +2355,7 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo
2344
2355
2345
2356
bool AcceptBlock (CBlock& block, CValidationState& state, CDiskBlockPos* dbp)
2346
2357
{
2358
+ AssertLockHeld (cs_main);
2347
2359
// Check for duplicate
2348
2360
uint256 hash = block.GetHash ();
2349
2361
if (mapBlockIndex.count (hash))
@@ -2455,6 +2467,7 @@ bool CBlockIndex::IsSuperMajority(int minVersion, const CBlockIndex* pstart, uns
2455
2467
2456
2468
int64_t CBlockIndex::GetMedianTime () const
2457
2469
{
2470
+ AssertLockHeld (cs_main);
2458
2471
const CBlockIndex* pindex = this ;
2459
2472
for (int i = 0 ; i < nMedianTimeSpan/2 ; i++)
2460
2473
{
@@ -2467,6 +2480,7 @@ int64_t CBlockIndex::GetMedianTime() const
2467
2480
2468
2481
void PushGetBlocks (CNode* pnode, CBlockIndex* pindexBegin, uint256 hashEnd)
2469
2482
{
2483
+ AssertLockHeld (cs_main);
2470
2484
// Filter out duplicate requests
2471
2485
if (pindexBegin == pnode->pindexLastGetBlocksBegin && hashEnd == pnode->hashLastGetBlocksEnd )
2472
2486
return ;
@@ -2948,6 +2962,7 @@ bool LoadBlockIndex()
2948
2962
2949
2963
2950
2964
bool InitBlockIndex () {
2965
+ LOCK (cs_main);
2951
2966
// Check whether we're already initialized
2952
2967
if (chainActive.Genesis () != NULL )
2953
2968
return true ;
@@ -2983,6 +2998,7 @@ bool InitBlockIndex() {
2983
2998
2984
2999
void PrintBlockTree ()
2985
3000
{
3001
+ AssertLockHeld (cs_main);
2986
3002
// pre-compute tree structure
2987
3003
map<CBlockIndex*, vector<CBlockIndex*> > mapNext;
2988
3004
for (map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.begin (); mi != mapBlockIndex.end (); ++mi)
@@ -4186,6 +4202,10 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
4186
4202
}
4187
4203
}
4188
4204
4205
+ TRY_LOCK (cs_main, lockMain); // Acquire cs_main for IsInitialBlockDownload() and CNodeState()
4206
+ if (!lockMain)
4207
+ return true ;
4208
+
4189
4209
// Address refresh broadcast
4190
4210
static int64_t nLastRebroadcast;
4191
4211
if (!IsInitialBlockDownload () && (GetTime () - nLastRebroadcast > 24 * 60 * 60 ))
@@ -4236,10 +4256,6 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
4236
4256
pto->PushMessage (" addr" , vAddr);
4237
4257
}
4238
4258
4239
- TRY_LOCK (cs_main, lockMain);
4240
- if (!lockMain)
4241
- return true ;
4242
-
4243
4259
CNodeState &state = *State (pto->GetId ());
4244
4260
if (state.fShouldBan ) {
4245
4261
if (pto->addr .IsLocal ())
0 commit comments