Skip to content

Commit 18e7216

Browse files
committed
Push cs_mains down in ProcessBlock
1 parent 202e019 commit 18e7216

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

src/main.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,8 +2243,7 @@ bool ReceivedBlockTransactions(const CBlock &block, CValidationState& state, CBl
22432243
if (!pblocktree->WriteBlockIndex(CDiskBlockIndex(pindexNew)))
22442244
return state.Abort(_("Failed to write block index"));
22452245

2246-
// New best?
2247-
return ActivateBestChain(state);
2246+
return true;
22482247
}
22492248

22502249

@@ -2520,7 +2519,6 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex,
25202519
}
25212520

25222521
int nHeight = pindex->nHeight;
2523-
uint256 hash = pindex->GetBlockHash();
25242522

25252523
// Check that all transactions are finalized
25262524
BOOST_FOREACH(const CTransaction& tx, block.vtx)
@@ -2593,10 +2591,11 @@ void PushGetBlocks(CNode* pnode, CBlockIndex* pindexBegin, uint256 hashEnd)
25932591

25942592
bool ProcessBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBlockPos *dbp)
25952593
{
2596-
AssertLockHeld(cs_main);
2597-
25982594
// Check for duplicate
25992595
uint256 hash = pblock->GetHash();
2596+
2597+
{
2598+
LOCK(cs_main);
26002599
if (mapBlockIndex.count(hash))
26012600
return state.Invalid(error("ProcessBlock() : already have block %d %s", mapBlockIndex[hash]->nHeight, hash.ToString()), 0, "duplicate");
26022601
if (mapOrphanBlocks.count(hash))
@@ -2665,7 +2664,11 @@ bool ProcessBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBl
26652664
mapOrphanBlocksByPrev.erase(hashPrev);
26662665
}
26672666

2668-
LogPrintf("ProcessBlock: ACCEPTED\n");
2667+
}
2668+
2669+
if (!ActivateBestChain(state))
2670+
return error("ProcessBlock() : ActivateBestChain failed");
2671+
26692672
return true;
26702673
}
26712674

@@ -3101,6 +3104,8 @@ bool InitBlockIndex() {
31013104
CBlockIndex *pindex = AddToBlockIndex(block);
31023105
if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
31033106
return error("LoadBlockIndex() : genesis block not accepted");
3107+
if (!ActivateBestChain(state))
3108+
return error("LoadBlockIndex() : genesis block cannot be activated");
31043109
} catch(std::runtime_error &e) {
31053110
return error("LoadBlockIndex() : failed to initialize block database: %s", e.what());
31063111
}
@@ -3230,7 +3235,6 @@ bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
32303235

32313236
// process block
32323237
if (nBlockPos >= nStartByte) {
3233-
LOCK(cs_main);
32343238
if (dbp)
32353239
dbp->nPos = nBlockPos;
32363240
CValidationState state;
@@ -3919,10 +3923,12 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
39193923
CInv inv(MSG_BLOCK, block.GetHash());
39203924
pfrom->AddInventoryKnown(inv);
39213925

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+
}
39263932

39273933
CValidationState state;
39283934
ProcessBlock(state, pfrom, &block);

src/miner.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -484,22 +484,22 @@ bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey)
484484
LOCK(cs_main);
485485
if (pblock->hashPrevBlock != chainActive.Tip()->GetBlockHash())
486486
return error("BitcoinMiner : generated block is stale");
487+
}
487488

488-
// Remove key from key pool
489-
reservekey.KeepKey();
490-
491-
// Track how many getdata requests this block gets
492-
{
493-
LOCK(wallet.cs_wallet);
494-
wallet.mapRequestCount[pblock->GetHash()] = 0;
495-
}
489+
// Remove key from key pool
490+
reservekey.KeepKey();
496491

497-
// Process this block the same as if we had received it from another node
498-
CValidationState state;
499-
if (!ProcessBlock(state, NULL, pblock))
500-
return error("BitcoinMiner : ProcessBlock, block not accepted");
492+
// Track how many getdata requests this block gets
493+
{
494+
LOCK(wallet.cs_wallet);
495+
wallet.mapRequestCount[pblock->GetHash()] = 0;
501496
}
502497

498+
// Process this block the same as if we had received it from another node
499+
CValidationState state;
500+
if (!ProcessBlock(state, NULL, pblock))
501+
return error("BitcoinMiner : ProcessBlock, block not accepted");
502+
503503
return true;
504504
}
505505

src/rpcserver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ static const CRPCCommand vRPCCommands[] =
254254
{ "getblocktemplate", &getblocktemplate, true, false, false },
255255
{ "getmininginfo", &getmininginfo, true, false, false },
256256
{ "getnetworkhashps", &getnetworkhashps, true, false, false },
257-
{ "submitblock", &submitblock, false, false, false },
257+
{ "submitblock", &submitblock, false, true, false },
258258

259259
/* Raw transactions */
260260
{ "createrawtransaction", &createrawtransaction, false, false, false },

0 commit comments

Comments
 (0)