Skip to content

Commit 3cf496d

Browse files
committed
Merge #8968: Don't hold cs_main when calling ProcessNewBlock from a cmpctblock
72ca7d9 Don't hold cs_main when calling ProcessNewBlock from a cmpctblock (Matt Corallo)
2 parents 5af9a71 + 72ca7d9 commit 3cf496d

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/main.cpp

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5828,29 +5828,34 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
58285828
BlockTransactions resp;
58295829
vRecv >> resp;
58305830

5831-
LOCK(cs_main);
5831+
CBlock block;
5832+
bool fBlockRead = false;
5833+
{
5834+
LOCK(cs_main);
58325835

5833-
map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator it = mapBlocksInFlight.find(resp.blockhash);
5834-
if (it == mapBlocksInFlight.end() || !it->second.second->partialBlock ||
5835-
it->second.first != pfrom->GetId()) {
5836-
LogPrint("net", "Peer %d sent us block transactions for block we weren't expecting\n", pfrom->id);
5837-
return true;
5838-
}
5836+
map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator it = mapBlocksInFlight.find(resp.blockhash);
5837+
if (it == mapBlocksInFlight.end() || !it->second.second->partialBlock ||
5838+
it->second.first != pfrom->GetId()) {
5839+
LogPrint("net", "Peer %d sent us block transactions for block we weren't expecting\n", pfrom->id);
5840+
return true;
5841+
}
58395842

5840-
PartiallyDownloadedBlock& partialBlock = *it->second.second->partialBlock;
5841-
CBlock block;
5842-
ReadStatus status = partialBlock.FillBlock(block, resp.txn);
5843-
if (status == READ_STATUS_INVALID) {
5844-
MarkBlockAsReceived(resp.blockhash); // Reset in-flight state in case of whitelist
5845-
Misbehaving(pfrom->GetId(), 100);
5846-
LogPrintf("Peer %d sent us invalid compact block/non-matching block transactions\n", pfrom->id);
5847-
return true;
5848-
} else if (status == READ_STATUS_FAILED) {
5849-
// Might have collided, fall back to getdata now :(
5850-
std::vector<CInv> invs;
5851-
invs.push_back(CInv(MSG_BLOCK | GetFetchFlags(pfrom, chainActive.Tip(), chainparams.GetConsensus()), resp.blockhash));
5852-
pfrom->PushMessage(NetMsgType::GETDATA, invs);
5853-
} else {
5843+
PartiallyDownloadedBlock& partialBlock = *it->second.second->partialBlock;
5844+
ReadStatus status = partialBlock.FillBlock(block, resp.txn);
5845+
if (status == READ_STATUS_INVALID) {
5846+
MarkBlockAsReceived(resp.blockhash); // Reset in-flight state in case of whitelist
5847+
Misbehaving(pfrom->GetId(), 100);
5848+
LogPrintf("Peer %d sent us invalid compact block/non-matching block transactions\n", pfrom->id);
5849+
return true;
5850+
} else if (status == READ_STATUS_FAILED) {
5851+
// Might have collided, fall back to getdata now :(
5852+
std::vector<CInv> invs;
5853+
invs.push_back(CInv(MSG_BLOCK | GetFetchFlags(pfrom, chainActive.Tip(), chainparams.GetConsensus()), resp.blockhash));
5854+
pfrom->PushMessage(NetMsgType::GETDATA, invs);
5855+
} else
5856+
fBlockRead = true;
5857+
} // Don't hold cs_main when we call into ProcessNewBlock
5858+
if (fBlockRead) {
58545859
CValidationState state;
58555860
ProcessNewBlock(state, chainparams, pfrom, &block, false, NULL);
58565861
int nDoS;

0 commit comments

Comments
 (0)