Skip to content

Commit a734896

Browse files
committed
Avoid cs_main in net_processing ActivateBestChain calls
1 parent 66aa1d5 commit a734896

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/net_processing.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,8 +1040,6 @@ static void RelayAddress(const CAddress& addr, bool fReachable, CConnman* connma
10401040

10411041
void static ProcessGetBlockData(CNode* pfrom, const Consensus::Params& consensusParams, const CInv& inv, CConnman* connman, const std::atomic<bool>& interruptMsgProc)
10421042
{
1043-
LOCK(cs_main);
1044-
10451043
bool send = false;
10461044
std::shared_ptr<const CBlock> a_recent_block;
10471045
std::shared_ptr<const CBlockHeaderAndShortTxIDs> a_recent_compact_block;
@@ -1053,7 +1051,9 @@ void static ProcessGetBlockData(CNode* pfrom, const Consensus::Params& consensus
10531051
fWitnessesPresentInARecentCompactBlock = fWitnessesPresentInMostRecentCompactBlock;
10541052
}
10551053

1054+
bool need_activate_chain = false;
10561055
{
1056+
LOCK(cs_main);
10571057
BlockMap::iterator mi = mapBlockIndex.find(inv.hash);
10581058
if (mi != mapBlockIndex.end())
10591059
{
@@ -1064,11 +1064,16 @@ void static ProcessGetBlockData(CNode* pfrom, const Consensus::Params& consensus
10641064
// before ActivateBestChain but after AcceptBlock).
10651065
// In this case, we need to run ActivateBestChain prior to checking the relay
10661066
// conditions below.
1067-
CValidationState dummy;
1068-
ActivateBestChain(dummy, Params(), a_recent_block);
1067+
need_activate_chain = true;
10691068
}
10701069
}
1070+
} // release cs_main before calling ActivateBestChain
1071+
if (need_activate_chain) {
1072+
CValidationState dummy;
1073+
ActivateBestChain(dummy, Params(), a_recent_block);
10711074
}
1075+
1076+
LOCK(cs_main);
10721077
BlockMap::iterator mi = mapBlockIndex.find(inv.hash);
10731078
if (mi != mapBlockIndex.end()) {
10741079
send = BlockRequestAllowed(mi->second, consensusParams);
@@ -1177,6 +1182,8 @@ void static ProcessGetBlockData(CNode* pfrom, const Consensus::Params& consensus
11771182

11781183
void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParams, CConnman* connman, const std::atomic<bool>& interruptMsgProc)
11791184
{
1185+
AssertLockNotHeld(cs_main);
1186+
11801187
std::deque<CInv>::iterator it = pfrom->vRecvGetData.begin();
11811188
std::vector<CInv> vNotFound;
11821189
const CNetMsgMaker msgMaker(pfrom->GetSendVersion());
@@ -1216,15 +1223,15 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
12161223
// Track requests for our stuff.
12171224
GetMainSignals().Inventory(inv.hash);
12181225
}
1226+
} // release cs_main
12191227

1220-
if (it != pfrom->vRecvGetData.end()) {
1221-
const CInv &inv = *it;
1222-
it++;
1223-
if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK || inv.type == MSG_CMPCT_BLOCK || inv.type == MSG_WITNESS_BLOCK) {
1224-
ProcessGetBlockData(pfrom, consensusParams, inv, connman, interruptMsgProc);
1225-
}
1228+
if (it != pfrom->vRecvGetData.end()) {
1229+
const CInv &inv = *it;
1230+
it++;
1231+
if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK || inv.type == MSG_CMPCT_BLOCK || inv.type == MSG_WITNESS_BLOCK) {
1232+
ProcessGetBlockData(pfrom, consensusParams, inv, connman, interruptMsgProc);
12261233
}
1227-
} // release cs_main
1234+
}
12281235

12291236
pfrom->vRecvGetData.erase(pfrom->vRecvGetData.begin(), it);
12301237

@@ -2027,7 +2034,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
20272034
inv.type = State(pfrom->GetId())->fWantsCmpctWitness ? MSG_WITNESS_BLOCK : MSG_BLOCK;
20282035
inv.hash = req.blockhash;
20292036
pfrom->vRecvGetData.push_back(inv);
2030-
ProcessGetData(pfrom, chainparams.GetConsensus(), connman, interruptMsgProc);
2037+
// The message processing loop will go around again (without pausing) and we'll respond then (without cs_main)
20312038
return true;
20322039
}
20332040

0 commit comments

Comments
 (0)