From 6055b9101bc91647fb80c7ed86fcff90e3e6dedf Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sat, 20 Jul 2013 14:34:16 +0200 Subject: Allow SendMessages to run partially without cs_main SendMessages() tries to acquire a cs_main lock now, but this isn't nessecary for much of its functionality. Move those parts out of the locked section, so they can always be performed, and we hold cs_main for a shorter time. --- src/main.cpp | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 477bc74fae..0c31655fe9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4134,8 +4134,7 @@ bool ProcessMessages(CNode* pfrom) bool SendMessages(CNode* pto, bool fSendTrickle) { - TRY_LOCK(cs_main, lockMain); - if (lockMain) { + { // Don't send anything until we get their version message if (pto->nVersion == 0) return true; @@ -4170,20 +4169,6 @@ bool SendMessages(CNode* pto, bool fSendTrickle) } } - // Start block sync - if (pto->fStartSync && !fImporting && !fReindex) { - pto->fStartSync = false; - PushGetBlocks(pto, chainActive.Tip(), uint256(0)); - } - - // Resend wallet transactions that haven't gotten in a block yet - // Except during reindex, importing and IBD, when old wallet - // transactions become unconfirmed and spams other nodes. - if (!fReindex && !fImporting && !IsInitialBlockDownload()) - { - ResendWalletTransactions(); - } - // Address refresh broadcast static int64 nLastRebroadcast; if (!IsInitialBlockDownload() && (GetTime() - nLastRebroadcast > 24 * 60 * 60)) @@ -4234,6 +4219,23 @@ bool SendMessages(CNode* pto, bool fSendTrickle) pto->PushMessage("addr", vAddr); } + TRY_LOCK(cs_main, lockMain); + if (!lockMain) + return true; + + // Start block sync + if (pto->fStartSync && !fImporting && !fReindex) { + pto->fStartSync = false; + PushGetBlocks(pto, chainActive.Tip(), uint256(0)); + } + + // Resend wallet transactions that haven't gotten in a block yet + // Except during reindex, importing and IBD, when old wallet + // transactions become unconfirmed and spams other nodes. + if (!fReindex && !fImporting && !IsInitialBlockDownload()) + { + ResendWalletTransactions(); + } // // Message: inventory -- cgit v1.2.3 From 7d38af3c493f9ea24c722ec2e6d3c51f4e851364 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Thu, 25 Jul 2013 02:34:42 +0200 Subject: Push down cs_main locking in ProcessMessage --- src/main.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 0c31655fe9..f2e6b67f8a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3278,6 +3278,8 @@ void static ProcessGetData(CNode* pfrom) vector vNotFound; + LOCK(cs_main); + while (it != pfrom->vRecvGetData.end()) { // Don't bother if send buffer is too full to respond anyway if (pfrom->nSendSize >= SendBufferSize()) @@ -3450,7 +3452,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) pfrom->fClient = !(pfrom->nServices & NODE_NETWORK); - AddTimeData(pfrom->addr, nTime); // Change version pfrom->PushMessage("verack"); @@ -3492,6 +3493,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) LogPrintf("receive version message: version %d, blocks=%d, us=%s, them=%s, peer=%s\n", pfrom->nVersion, pfrom->nStartingHeight, addrMe.ToString().c_str(), addrFrom.ToString().c_str(), pfrom->addr.ToString().c_str()); + LOCK(cs_main); + AddTimeData(pfrom->addr, nTime); cPeerBlockCounts.input(pfrom->nStartingHeight); } @@ -3595,6 +3598,9 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) break; } } + + LOCK(cs_main); + for (unsigned int nInv = 0; nInv < vInv.size(); nInv++) { const CInv &inv = vInv[nInv]; @@ -3652,6 +3658,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(); @@ -3686,6 +3694,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) uint256 hashStop; vRecv >> locator >> hashStop; + LOCK(cs_main); + CBlockIndex* pindex = NULL; if (locator.IsNull()) { @@ -3728,6 +3738,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; CValidationState state; if (mempool.accept(state, tx, true, &fMissingInputs)) @@ -3802,6 +3814,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) CInv inv(MSG_BLOCK, block.GetHash()); pfrom->AddInventoryKnown(inv); + LOCK(cs_main); + CValidationState state; if (ProcessBlock(state, pfrom, &block)) mapAlreadyAskedFor.erase(inv); @@ -3823,6 +3837,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) else if (strCommand == "mempool") { + LOCK(cs_main); + std::vector vtxid; LOCK2(mempool.cs, pfrom->cs_filter); mempool.queryHashes(vtxid); @@ -4088,10 +4104,7 @@ bool ProcessMessages(CNode* pfrom) bool fRet = false; try { - { - LOCK(cs_main); - fRet = ProcessMessage(pfrom, strCommand, vRecv); - } + fRet = ProcessMessage(pfrom, strCommand, vRecv); boost::this_thread::interruption_point(); } catch (std::ios_base::failure& e) -- cgit v1.2.3