@@ -2243,8 +2243,7 @@ bool ReceivedBlockTransactions(const CBlock &block, CValidationState& state, CBl
2243
2243
if (!pblocktree->WriteBlockIndex (CDiskBlockIndex (pindexNew)))
2244
2244
return state.Abort (_ (" Failed to write block index" ));
2245
2245
2246
- // New best?
2247
- return ActivateBestChain (state);
2246
+ return true ;
2248
2247
}
2249
2248
2250
2249
@@ -2520,7 +2519,6 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex,
2520
2519
}
2521
2520
2522
2521
int nHeight = pindex->nHeight ;
2523
- uint256 hash = pindex->GetBlockHash ();
2524
2522
2525
2523
// Check that all transactions are finalized
2526
2524
BOOST_FOREACH (const CTransaction& tx, block.vtx )
@@ -2593,10 +2591,11 @@ void PushGetBlocks(CNode* pnode, CBlockIndex* pindexBegin, uint256 hashEnd)
2593
2591
2594
2592
bool ProcessBlock (CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBlockPos *dbp)
2595
2593
{
2596
- AssertLockHeld (cs_main);
2597
-
2598
2594
// Check for duplicate
2599
2595
uint256 hash = pblock->GetHash ();
2596
+
2597
+ {
2598
+ LOCK (cs_main);
2600
2599
if (mapBlockIndex.count (hash))
2601
2600
return state.Invalid (error (" ProcessBlock() : already have block %d %s" , mapBlockIndex[hash]->nHeight , hash.ToString ()), 0 , " duplicate" );
2602
2601
if (mapOrphanBlocks.count (hash))
@@ -2665,7 +2664,11 @@ bool ProcessBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBl
2665
2664
mapOrphanBlocksByPrev.erase (hashPrev);
2666
2665
}
2667
2666
2668
- LogPrintf (" ProcessBlock: ACCEPTED\n " );
2667
+ }
2668
+
2669
+ if (!ActivateBestChain (state))
2670
+ return error (" ProcessBlock() : ActivateBestChain failed" );
2671
+
2669
2672
return true ;
2670
2673
}
2671
2674
@@ -3101,6 +3104,8 @@ bool InitBlockIndex() {
3101
3104
CBlockIndex *pindex = AddToBlockIndex (block);
3102
3105
if (!ReceivedBlockTransactions (block, state, pindex, blockPos))
3103
3106
return error (" LoadBlockIndex() : genesis block not accepted" );
3107
+ if (!ActivateBestChain (state))
3108
+ return error (" LoadBlockIndex() : genesis block cannot be activated" );
3104
3109
} catch (std::runtime_error &e) {
3105
3110
return error (" LoadBlockIndex() : failed to initialize block database: %s" , e.what ());
3106
3111
}
@@ -3230,7 +3235,6 @@ bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
3230
3235
3231
3236
// process block
3232
3237
if (nBlockPos >= nStartByte) {
3233
- LOCK (cs_main);
3234
3238
if (dbp)
3235
3239
dbp->nPos = nBlockPos;
3236
3240
CValidationState state;
@@ -3919,10 +3923,12 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
3919
3923
CInv inv (MSG_BLOCK, block.GetHash ());
3920
3924
pfrom->AddInventoryKnown (inv);
3921
3925
3922
- LOCK (cs_main);
3923
- // Remember who we got this block from.
3924
- mapBlockSource[inv.hash ] = pfrom->GetId ();
3925
- MarkBlockAsReceived (inv.hash , pfrom->GetId ());
3926
+ {
3927
+ LOCK (cs_main);
3928
+ // Remember who we got this block from.
3929
+ mapBlockSource[inv.hash ] = pfrom->GetId ();
3930
+ MarkBlockAsReceived (inv.hash , pfrom->GetId ());
3931
+ }
3926
3932
3927
3933
CValidationState state;
3928
3934
ProcessBlock (state, pfrom, &block);
0 commit comments