From b961bab134ebddb785ea71982ac3fbc30f5c6ba9 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Wed, 20 Mar 2019 15:26:21 -0700 Subject: [PATCH] Interrupt orphan processing after every transaction This makes orphan processing work like handling getdata messages: After every actual transaction validation attempt, interrupt processing to deal with messages arriving from other peers. Cherry-picked from: 9f2ab9ed Github Pull Request: #3575 --- src/net.h | 1 + src/net_processing.cpp | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/net.h b/src/net.h index 6cfbd9550..466ae5384 100644 --- a/src/net.h +++ b/src/net.h @@ -696,6 +696,7 @@ public: // Counts getheaders requests sent to this peer std::atomic nPendingHeaderRequests; + std::set orphan_work_set; CNode(NodeId id, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress &addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const std::string &addrNameIn = "", bool fInboundIn = false); ~CNode(); diff --git a/src/net_processing.cpp b/src/net_processing.cpp index b6f88e3a1..c8d94101c 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1346,7 +1346,8 @@ void static ProcessOrphanTx(CConnman* connman, std::set& orphan_work_se { AssertLockHeld(cs_main); std::set setMisbehaving; - while (!orphan_work_set.empty()) { + bool done = false; + while (!done && !orphan_work_set.empty()) { const uint256 orphanHash = *orphan_work_set.begin(); orphan_work_set.erase(orphan_work_set.begin()); @@ -1381,6 +1382,7 @@ void static ProcessOrphanTx(CConnman* connman, std::set& orphan_work_se } EraseOrphanTx(orphanHash); + done = true; } else if (!fMissingInputs2) { @@ -1403,6 +1405,7 @@ void static ProcessOrphanTx(CConnman* connman, std::set& orphan_work_se } EraseOrphanTx(orphanHash); + done = true; } @@ -2073,7 +2076,6 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr return true; } - std::set orphan_work_set; CTransactionRef ptx; vRecv >> ptx; const CTransaction& tx = *ptx; @@ -2100,7 +2102,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr auto it_by_prev = mapOrphanTransactionsByPrev.find(COutPoint(inv.hash, i)); if (it_by_prev != mapOrphanTransactionsByPrev.end()) { for (const auto& elem : it_by_prev->second) { - orphan_work_set.insert(elem->first); + pfrom->orphan_work_set.insert(elem->first); } } } @@ -2113,7 +2115,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr mempool.size(), mempool.DynamicMemoryUsage() / 1000); // Recursively process any orphan transactions that depended on this one - ProcessOrphanTx(&connman, orphan_work_set, lRemovedTxn); + ProcessOrphanTx(&connman, pfrom->orphan_work_set, lRemovedTxn); } else if (fMissingInputs) { @@ -2926,11 +2928,21 @@ bool ProcessMessages(CNode* pfrom, CConnman& connman, const std::atomic& i if (!pfrom->vRecvGetData.empty()) ProcessGetData(pfrom, chainparams.GetConsensus(chainActive.Height()), connman, interruptMsgProc); + if (!pfrom->orphan_work_set.empty()) { + std::list removed_txn; + LOCK(cs_main); + ProcessOrphanTx(&connman, pfrom->orphan_work_set, removed_txn); + for (const CTransactionRef& removedTx : removed_txn) { + AddToCompactExtraTransactions(removedTx); + } + } + if (pfrom->fDisconnect) return false; // this maintains the order of responses if (!pfrom->vRecvGetData.empty()) return true; + if (!pfrom->orphan_work_set.empty()) return true; // Don't bother if send buffer is too full to respond anyway if (pfrom->fPauseSend)