diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -5189,6 +5189,15 @@ // std::vector vInv; { + auto addInvAndMaybeFlush = [&](uint32_t type, const uint256 &hash) { + vInv.emplace_back(type, hash); + if (vInv.size() == MAX_INV_SZ) { + m_connman.PushMessage(pto, + msgMaker.Make(NetMsgType::INV, vInv)); + vInv.clear(); + } + }; + LOCK(pto->cs_inventory); vInv.reserve(std::max(pto->vInventoryBlockToSend.size(), INVENTORY_BROADCAST_MAX_PER_MB * @@ -5197,12 +5206,7 @@ // Add blocks for (const BlockHash &hash : pto->vInventoryBlockToSend) { - vInv.push_back(CInv(MSG_BLOCK, hash)); - if (vInv.size() == MAX_INV_SZ) { - m_connman.PushMessage(pto, - msgMaker.Make(NetMsgType::INV, vInv)); - vInv.clear(); - } + addInvAndMaybeFlush(MSG_BLOCK, hash); } pto->vInventoryBlockToSend.clear(); @@ -5247,7 +5251,6 @@ for (const auto &txinfo : vtxinfo) { const TxId &txid = txinfo.tx->GetId(); - CInv inv(MSG_TX, txid); pto->m_tx_relay->setInventoryTxToSend.erase(txid); // Don't send transactions that peers will not put into // their mempool @@ -5262,12 +5265,7 @@ pto->m_tx_relay->filterInventoryKnown.insert(txid); // Responses to MEMPOOL requests bypass the // m_recently_announced_invs filter. - vInv.push_back(inv); - if (vInv.size() == MAX_INV_SZ) { - m_connman.PushMessage( - pto, msgMaker.Make(NetMsgType::INV, vInv)); - vInv.clear(); - } + addInvAndMaybeFlush(MSG_TX, txid); } pto->m_tx_relay->m_last_mempool_req = GetTime(); @@ -5336,7 +5334,6 @@ // Send State(pto->GetId()) ->m_recently_announced_invs.insert(txid); - vInv.push_back(CInv(MSG_TX, txid)); nRelayedTransactions++; { // Expire old relay messages @@ -5357,11 +5354,7 @@ ret.first)); } } - if (vInv.size() == MAX_INV_SZ) { - m_connman.PushMessage( - pto, msgMaker.Make(NetMsgType::INV, vInv)); - vInv.clear(); - } + addInvAndMaybeFlush(MSG_TX, txid); pto->m_tx_relay->filterInventoryKnown.insert(txid); } }