Rather than splitting this logic up (hoisting the CInv declaration above the code block, fetching the type inside the cs_main scope, and then putting the rest of the logic outside the cs_main scope), I recommend you keep all the logic together:
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -2872,8 +2872,6 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
return;
}
- CInv inv;
-
{
LOCK(cs_main);
@@ -2892,17 +2890,18 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
return;
}
- // If an older block is requested (should never happen in practice,
- // but can happen in tests) send a block response instead of a
- // blocktxn response. Sending a full block response instead of a
- // small blocktxn response is preferable in the case where a peer
- // might maliciously send lots of getblocktxn requests to trigger
- // expensive disk reads, because it will require the peer to
- // actually receive all the data read from disk over the network.
- inv.type = State(pfrom.GetId())->fWantsCmpctWitness ? MSG_WITNESS_BLOCK : MSG_BLOCK;
}
+ // If an older block is requested (should never happen in practice,
+ // but can happen in tests) send a block response instead of a
+ // blocktxn response. Sending a full block response instead of a
+ // small blocktxn response is preferable in the case where a peer
+ // might maliciously send lots of getblocktxn requests to trigger
+ // expensive disk reads, because it will require the peer to
+ // actually receive all the data read from disk over the network.
LogPrint(BCLog::NET, "Peer %d sent us a getblocktxn for a block > %i deep\n", pfrom.GetId(), MAX_BLOCKTXN_DEPTH);
+ CInv inv;
+ inv.type = WITH_LOCK(cs_main, return State(pfrom.GetId())->fWantsCmpctWitness ? MSG_WITNESS_BLOCK : MSG_BLOCK);
inv.hash = req.blockhash;
It's slightly wasteful that cs_main is released and then taken again, but I think it's ok because:
- As the comment says, we don't expect this to happen except in tests
fWantsCmpctWitness should move to the Peer struct eventually, removing the need to take cs_main