From f2b5691a813296156d43f8a2665b462dc6d6a83c Mon Sep 17 00:00:00 2001 From: Marco Nilsson Date: Wed, 21 Feb 2018 10:12:28 +0100 Subject: [PATCH] Revert lock order from #708. This order is still used in blackcoin. Any order which violates this is a new regression introduced in Gridcoin and should be changed. The cs_main scope has also been reduced which fixes the RPC and UI slowdowns when syncing. --- src/main.cpp | 28 +++++++++++++++------------- src/net.cpp | 15 ++++++++++++--- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 927964415b..70f46225c4 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -6612,7 +6612,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, if (vInv.size() > MAX_INV_SZ) { pfrom->Misbehaving(50); - printf("\r\n **Hacker tried to send inventory > MAX_INV_SZ **\r\n"); return error("message inv size() = %" PRIszu "", vInv.size()); } @@ -6624,6 +6623,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, break; } } + + LOCK(cs_main); CTxDB txdb("r"); for (unsigned int nInv = 0; nInv < vInv.size(); nInv++) { @@ -6671,6 +6672,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, if (fDebug10) printf("received getdata (%" PRIszu " invsz)\n", vInv.size()); } + LOCK(cs_main); for (auto const& inv : vInv) { if (fShutdown) @@ -6739,6 +6741,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, uint256 hashStop; vRecv >> locator >> hashStop; + LOCK(cs_main); + // Find the last block the caller has in the main chain CBlockIndex* pindex = locator.GetBlockIndex(); @@ -6776,6 +6780,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, uint256 hashStop; vRecv >> locator >> hashStop; + LOCK(cs_main); + CBlockIndex* pindex = NULL; if (locator.IsNull()) { @@ -6814,6 +6820,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, CInv inv(MSG_TX, tx.GetHash()); pfrom->AddInventoryKnown(inv); + LOCK(cs_main); + bool fMissingInputs = false; if (AcceptToMemoryPool(mempool, tx, &fMissingInputs)) { @@ -6892,6 +6900,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, CInv inv(MSG_BLOCK, hashBlock); pfrom->AddInventoryKnown(inv); + LOCK(cs_main); + if (ProcessBlock(pfrom, &block, false)) { mapAlreadyAskedFor.erase(inv); @@ -6920,6 +6930,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, else if (strCommand == "mempool") { + LOCK(cs_main); + std::vector vtxid; mempool.queryHashes(vtxid); vector vInv; @@ -7212,13 +7224,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, // requires LOCK(cs_vRecvMsg) bool ProcessMessages(CNode* pfrom) -{ - LOCK(cs_main); - - TRY_LOCK(pfrom->cs_vRecvMsg, lockRecv); - if (!lockRecv) - return true; - +{ // // Message format // (4) message start @@ -7286,7 +7292,7 @@ bool ProcessMessages(CNode* pfrom) bool fRet = false; try { - fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime); + fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime); if (fShutdown) break; } @@ -7971,10 +7977,6 @@ bool SendMessages(CNode* pto, bool fSendTrickle) if(!lockMain) return true; - TRY_LOCK(pto->cs_vSend, lockSend); - if (!lockSend) - return true; - // Don't send anything until we get their version message if (pto->nVersion == 0) return true; diff --git a/src/net.cpp b/src/net.cpp index 5e59746550..df172027df 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -2053,14 +2053,23 @@ void ThreadMessageHandler2(void* parg) //11-25-2015 // Receive messages - if (!ProcessMessages(pnode)) - pnode->CloseSocketDisconnect(); + { + TRY_LOCK(pnode->cs_vRecvMsg, lockRecv); + if (lockRecv) + if (!ProcessMessages(pnode)) + pnode->CloseSocketDisconnect(); + } if (fShutdown) return; // Send messages - SendMessages(pnode, pnode == pnodeTrickle); + { + TRY_LOCK(pnode->cs_vSend, lockSend); + if (lockSend) + SendMessages(pnode, pnode == pnodeTrickle); + } + if (fShutdown) return; }