diff options
author | Suhas Daftuar <sdaftuar@gmail.com> | 2019-03-08 15:30:36 -0500 |
---|---|---|
committer | Suhas Daftuar <sdaftuar@gmail.com> | 2019-08-28 13:41:58 -0400 |
commit | c4aa2ba82211ea5988ed7fe21e1b08bc3367e6d4 (patch) | |
tree | f0666a205ae658352be324c48aac5507110c7ab6 /src | |
parent | 4de0dbac9b286c42a9b10132b7c2d76712f1a319 (diff) |
[refactor] Change tx_relay structure to be unique_ptr
Diffstat (limited to 'src')
-rw-r--r-- | src/net.cpp | 13 | ||||
-rw-r--r-- | src/net.h | 12 | ||||
-rw-r--r-- | src/net_processing.cpp | 106 |
3 files changed, 66 insertions, 65 deletions
diff --git a/src/net.cpp b/src/net.cpp index 527c001308..78b33954d6 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -500,8 +500,8 @@ void CNode::copyStats(CNodeStats &stats) X(addr); X(addrBind); { - LOCK(m_tx_relay.cs_filter); - stats.fRelayTxes = m_tx_relay.fRelayTxes; + LOCK(m_tx_relay->cs_filter); + stats.fRelayTxes = m_tx_relay->fRelayTxes; } X(nLastSend); X(nLastRecv); @@ -529,8 +529,8 @@ void CNode::copyStats(CNodeStats &stats) X(m_legacyWhitelisted); X(m_permissionFlags); { - LOCK(m_tx_relay.cs_feeFilter); - stats.minFeeFilter = m_tx_relay.minFeeFilter; + LOCK(m_tx_relay->cs_feeFilter); + stats.minFeeFilter = m_tx_relay->minFeeFilter; } // It is common for nodes with good ping times to suddenly become lagged, @@ -818,11 +818,11 @@ bool CConnman::AttemptToEvictConnection() continue; if (node->fDisconnect) continue; - LOCK(node->m_tx_relay.cs_filter); + LOCK(node->m_tx_relay->cs_filter); NodeEvictionCandidate candidate = {node->GetId(), node->nTimeConnected, node->nMinPingUsecTime, node->nLastBlockTime, node->nLastTXTime, HasAllDesirableServiceFlags(node->nServices), - node->m_tx_relay.fRelayTxes, node->m_tx_relay.pfilter != nullptr, node->addr, node->nKeyedNetGroup, + node->m_tx_relay->fRelayTxes, node->m_tx_relay->pfilter != nullptr, node->addr, node->nKeyedNetGroup, node->m_prefer_evict}; vEvictionCandidates.push_back(candidate); } @@ -2633,6 +2633,7 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn hSocket = hSocketIn; addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn; hashContinue = uint256(); + m_tx_relay = MakeUnique<TxRelay>(); for (const std::string &msg : getAllNetMessageTypes()) mapRecvBytesPerMsgCmd[msg] = 0; @@ -733,7 +733,7 @@ public: int64_t nextSendTimeFeeFilter{0}; }; - TxRelay m_tx_relay; + std::unique_ptr<TxRelay> m_tx_relay; // Used for headers announcements - unfiltered blocks to relay std::vector<uint256> vBlockHashesToAnnounce GUARDED_BY(cs_inventory); @@ -849,17 +849,17 @@ public: void AddInventoryKnown(const CInv& inv) { { - LOCK(m_tx_relay.cs_tx_inventory); - m_tx_relay.filterInventoryKnown.insert(inv.hash); + LOCK(m_tx_relay->cs_tx_inventory); + m_tx_relay->filterInventoryKnown.insert(inv.hash); } } void PushInventory(const CInv& inv) { if (inv.type == MSG_TX) { - LOCK(m_tx_relay.cs_tx_inventory); - if (!m_tx_relay.filterInventoryKnown.contains(inv.hash)) { - m_tx_relay.setInventoryTxToSend.insert(inv.hash); + LOCK(m_tx_relay->cs_tx_inventory); + if (!m_tx_relay->filterInventoryKnown.contains(inv.hash)) { + m_tx_relay->setInventoryTxToSend.insert(inv.hash); } } else if (inv.type == MSG_BLOCK) { LOCK(cs_inventory); diff --git a/src/net_processing.cpp b/src/net_processing.cpp index c88d17b6ee..303a060a99 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1449,10 +1449,10 @@ void static ProcessGetBlockData(CNode* pfrom, const CChainParams& chainparams, c bool sendMerkleBlock = false; CMerkleBlock merkleBlock; { - LOCK(pfrom->m_tx_relay.cs_filter); - if (pfrom->m_tx_relay.pfilter) { + LOCK(pfrom->m_tx_relay->cs_filter); + if (pfrom->m_tx_relay->pfilter) { sendMerkleBlock = true; - merkleBlock = CMerkleBlock(*pblock, *pfrom->m_tx_relay.pfilter); + merkleBlock = CMerkleBlock(*pblock, *pfrom->m_tx_relay->pfilter); } } if (sendMerkleBlock) { @@ -1532,11 +1532,11 @@ void static ProcessGetData(CNode* pfrom, const CChainParams& chainparams, CConnm if (mi != mapRelay.end()) { connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *mi->second)); push = true; - } else if (pfrom->m_tx_relay.timeLastMempoolReq) { + } else if (pfrom->m_tx_relay->timeLastMempoolReq) { auto txinfo = mempool.info(inv.hash); // To protect privacy, do not answer getdata using the mempool when // that TX couldn't have been INVed in reply to a MEMPOOL request. - if (txinfo.tx && txinfo.nTime <= pfrom->m_tx_relay.timeLastMempoolReq) { + if (txinfo.tx && txinfo.nTime <= pfrom->m_tx_relay->timeLastMempoolReq) { connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *txinfo.tx)); push = true; } @@ -1996,8 +1996,8 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr pfrom->m_limited_node = (!(nServices & NODE_NETWORK) && (nServices & NODE_NETWORK_LIMITED)); { - LOCK(pfrom->m_tx_relay.cs_filter); - pfrom->m_tx_relay.fRelayTxes = fRelay; // set to true after we get the first filter* message + LOCK(pfrom->m_tx_relay->cs_filter); + pfrom->m_tx_relay->fRelayTxes = fRelay; // set to true after we get the first filter* message } // Change version @@ -3030,8 +3030,8 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr return true; } - LOCK(pfrom->m_tx_relay.cs_tx_inventory); - pfrom->m_tx_relay.fSendMempool = true; + LOCK(pfrom->m_tx_relay->cs_tx_inventory); + pfrom->m_tx_relay->fSendMempool = true; return true; } @@ -3124,10 +3124,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr } else { - LOCK(pfrom->m_tx_relay.cs_filter); - pfrom->m_tx_relay.pfilter.reset(new CBloomFilter(filter)); - pfrom->m_tx_relay.pfilter->UpdateEmptyFull(); - pfrom->m_tx_relay.fRelayTxes = true; + LOCK(pfrom->m_tx_relay->cs_filter); + pfrom->m_tx_relay->pfilter.reset(new CBloomFilter(filter)); + pfrom->m_tx_relay->pfilter->UpdateEmptyFull(); + pfrom->m_tx_relay->fRelayTxes = true; } return true; } @@ -3142,9 +3142,9 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE) { bad = true; } else { - LOCK(pfrom->m_tx_relay.cs_filter); - if (pfrom->m_tx_relay.pfilter) { - pfrom->m_tx_relay.pfilter->insert(vData); + LOCK(pfrom->m_tx_relay->cs_filter); + if (pfrom->m_tx_relay->pfilter) { + pfrom->m_tx_relay->pfilter->insert(vData); } else { bad = true; } @@ -3157,11 +3157,11 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr } if (strCommand == NetMsgType::FILTERCLEAR) { - LOCK(pfrom->m_tx_relay.cs_filter); + LOCK(pfrom->m_tx_relay->cs_filter); if (pfrom->GetLocalServices() & NODE_BLOOM) { - pfrom->m_tx_relay.pfilter.reset(new CBloomFilter()); + pfrom->m_tx_relay->pfilter.reset(new CBloomFilter()); } - pfrom->m_tx_relay.fRelayTxes = true; + pfrom->m_tx_relay->fRelayTxes = true; return true; } @@ -3170,8 +3170,8 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr vRecv >> newFeeFilter; if (MoneyRange(newFeeFilter)) { { - LOCK(pfrom->m_tx_relay.cs_feeFilter); - pfrom->m_tx_relay.minFeeFilter = newFeeFilter; + LOCK(pfrom->m_tx_relay->cs_feeFilter); + pfrom->m_tx_relay->minFeeFilter = newFeeFilter; } LogPrint(BCLog::NET, "received: feefilter of %s from peer=%d\n", CFeeRate(newFeeFilter).ToString(), pfrom->GetId()); } @@ -3791,70 +3791,70 @@ bool PeerLogicValidation::SendMessages(CNode* pto) } pto->vInventoryBlockToSend.clear(); - LOCK(pto->m_tx_relay.cs_tx_inventory); + LOCK(pto->m_tx_relay->cs_tx_inventory); // Check whether periodic sends should happen bool fSendTrickle = pto->HasPermission(PF_NOBAN); - if (pto->m_tx_relay.nNextInvSend < nNow) { + if (pto->m_tx_relay->nNextInvSend < nNow) { fSendTrickle = true; if (pto->fInbound) { - pto->m_tx_relay.nNextInvSend = connman->PoissonNextSendInbound(nNow, INVENTORY_BROADCAST_INTERVAL); + pto->m_tx_relay->nNextInvSend = connman->PoissonNextSendInbound(nNow, INVENTORY_BROADCAST_INTERVAL); } else { // Use half the delay for outbound peers, as there is less privacy concern for them. - pto->m_tx_relay.nNextInvSend = PoissonNextSend(nNow, INVENTORY_BROADCAST_INTERVAL >> 1); + pto->m_tx_relay->nNextInvSend = PoissonNextSend(nNow, INVENTORY_BROADCAST_INTERVAL >> 1); } } // Time to send but the peer has requested we not relay transactions. if (fSendTrickle) { - LOCK(pto->m_tx_relay.cs_filter); - if (!pto->m_tx_relay.fRelayTxes) pto->m_tx_relay.setInventoryTxToSend.clear(); + LOCK(pto->m_tx_relay->cs_filter); + if (!pto->m_tx_relay->fRelayTxes) pto->m_tx_relay->setInventoryTxToSend.clear(); } // Respond to BIP35 mempool requests - if (fSendTrickle && pto->m_tx_relay.fSendMempool) { + if (fSendTrickle && pto->m_tx_relay->fSendMempool) { auto vtxinfo = mempool.infoAll(); - pto->m_tx_relay.fSendMempool = false; + pto->m_tx_relay->fSendMempool = false; CAmount filterrate = 0; { - LOCK(pto->m_tx_relay.cs_feeFilter); - filterrate = pto->m_tx_relay.minFeeFilter; + LOCK(pto->m_tx_relay->cs_feeFilter); + filterrate = pto->m_tx_relay->minFeeFilter; } - LOCK(pto->m_tx_relay.cs_filter); + LOCK(pto->m_tx_relay->cs_filter); for (const auto& txinfo : vtxinfo) { const uint256& hash = txinfo.tx->GetHash(); CInv inv(MSG_TX, hash); - pto->m_tx_relay.setInventoryTxToSend.erase(hash); + pto->m_tx_relay->setInventoryTxToSend.erase(hash); if (filterrate) { if (txinfo.feeRate.GetFeePerK() < filterrate) continue; } - if (pto->m_tx_relay.pfilter) { - if (!pto->m_tx_relay.pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue; + if (pto->m_tx_relay->pfilter) { + if (!pto->m_tx_relay->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue; } - pto->m_tx_relay.filterInventoryKnown.insert(hash); + pto->m_tx_relay->filterInventoryKnown.insert(hash); vInv.push_back(inv); if (vInv.size() == MAX_INV_SZ) { connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv)); vInv.clear(); } } - pto->m_tx_relay.timeLastMempoolReq = GetTime(); + pto->m_tx_relay->timeLastMempoolReq = GetTime(); } // Determine transactions to relay if (fSendTrickle) { // Produce a vector with all candidates for sending std::vector<std::set<uint256>::iterator> vInvTx; - vInvTx.reserve(pto->m_tx_relay.setInventoryTxToSend.size()); - for (std::set<uint256>::iterator it = pto->m_tx_relay.setInventoryTxToSend.begin(); it != pto->m_tx_relay.setInventoryTxToSend.end(); it++) { + vInvTx.reserve(pto->m_tx_relay->setInventoryTxToSend.size()); + for (std::set<uint256>::iterator it = pto->m_tx_relay->setInventoryTxToSend.begin(); it != pto->m_tx_relay->setInventoryTxToSend.end(); it++) { vInvTx.push_back(it); } CAmount filterrate = 0; { - LOCK(pto->m_tx_relay.cs_feeFilter); - filterrate = pto->m_tx_relay.minFeeFilter; + LOCK(pto->m_tx_relay->cs_feeFilter); + filterrate = pto->m_tx_relay->minFeeFilter; } // Topologically and fee-rate sort the inventory we send for privacy and priority reasons. // A heap is used so that not all items need sorting if only a few are being sent. @@ -3863,7 +3863,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto) // No reason to drain out at many times the network's capacity, // especially since we have many peers and some will draw much shorter delays. unsigned int nRelayedTransactions = 0; - LOCK(pto->m_tx_relay.cs_filter); + LOCK(pto->m_tx_relay->cs_filter); while (!vInvTx.empty() && nRelayedTransactions < INVENTORY_BROADCAST_MAX) { // Fetch the top element from the heap std::pop_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder); @@ -3871,9 +3871,9 @@ bool PeerLogicValidation::SendMessages(CNode* pto) vInvTx.pop_back(); uint256 hash = *it; // Remove it from the to-be-sent set - pto->m_tx_relay.setInventoryTxToSend.erase(it); + pto->m_tx_relay->setInventoryTxToSend.erase(it); // Check if not in the filter already - if (pto->m_tx_relay.filterInventoryKnown.contains(hash)) { + if (pto->m_tx_relay->filterInventoryKnown.contains(hash)) { continue; } // Not in the mempool anymore? don't bother sending it. @@ -3884,7 +3884,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto) if (filterrate && txinfo.feeRate.GetFeePerK() < filterrate) { continue; } - if (pto->m_tx_relay.pfilter && !pto->m_tx_relay.pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue; + if (pto->m_tx_relay->pfilter && !pto->m_tx_relay->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue; // Send vInv.push_back(CInv(MSG_TX, hash)); nRelayedTransactions++; @@ -3905,7 +3905,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto) connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv)); vInv.clear(); } - pto->m_tx_relay.filterInventoryKnown.insert(hash); + pto->m_tx_relay->filterInventoryKnown.insert(hash); } } } @@ -4070,23 +4070,23 @@ bool PeerLogicValidation::SendMessages(CNode* pto) !pto->HasPermission(PF_FORCERELAY)) { CAmount currentFilter = mempool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFeePerK(); int64_t timeNow = GetTimeMicros(); - if (timeNow > pto->m_tx_relay.nextSendTimeFeeFilter) { + if (timeNow > pto->m_tx_relay->nextSendTimeFeeFilter) { static CFeeRate default_feerate(DEFAULT_MIN_RELAY_TX_FEE); static FeeFilterRounder filterRounder(default_feerate); CAmount filterToSend = filterRounder.round(currentFilter); // We always have a fee filter of at least minRelayTxFee filterToSend = std::max(filterToSend, ::minRelayTxFee.GetFeePerK()); - if (filterToSend != pto->m_tx_relay.lastSentFeeFilter) { + if (filterToSend != pto->m_tx_relay->lastSentFeeFilter) { connman->PushMessage(pto, msgMaker.Make(NetMsgType::FEEFILTER, filterToSend)); - pto->m_tx_relay.lastSentFeeFilter = filterToSend; + pto->m_tx_relay->lastSentFeeFilter = filterToSend; } - pto->m_tx_relay.nextSendTimeFeeFilter = PoissonNextSend(timeNow, AVG_FEEFILTER_BROADCAST_INTERVAL); + pto->m_tx_relay->nextSendTimeFeeFilter = PoissonNextSend(timeNow, AVG_FEEFILTER_BROADCAST_INTERVAL); } // If the fee filter has changed substantially and it's still more than MAX_FEEFILTER_CHANGE_DELAY // until scheduled broadcast, then move the broadcast to within MAX_FEEFILTER_CHANGE_DELAY. - else if (timeNow + MAX_FEEFILTER_CHANGE_DELAY * 1000000 < pto->m_tx_relay.nextSendTimeFeeFilter && - (currentFilter < 3 * pto->m_tx_relay.lastSentFeeFilter / 4 || currentFilter > 4 * pto->m_tx_relay.lastSentFeeFilter / 3)) { - pto->m_tx_relay.nextSendTimeFeeFilter = timeNow + GetRandInt(MAX_FEEFILTER_CHANGE_DELAY) * 1000000; + else if (timeNow + MAX_FEEFILTER_CHANGE_DELAY * 1000000 < pto->m_tx_relay->nextSendTimeFeeFilter && + (currentFilter < 3 * pto->m_tx_relay->lastSentFeeFilter / 4 || currentFilter > 4 * pto->m_tx_relay->lastSentFeeFilter / 3)) { + pto->m_tx_relay->nextSendTimeFeeFilter = timeNow + GetRandInt(MAX_FEEFILTER_CHANGE_DELAY) * 1000000; } } } |