Skip to content

Commit 7704139

Browse files
committed
[refactor] make GetCandidatePeers take uint256 and in-out vector
The txrequest fuzzer uses uint256s, not transactions, so it's best if GetCandidatePeers takes that as an input.
1 parent 6e4d392 commit 7704139

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

src/node/txdownloadman_impl.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -401,18 +401,19 @@ node::RejectedTxTodo TxDownloadManagerImpl::MempoolRejectedTx(const CTransaction
401401
// means it was already added to vExtraTxnForCompact.
402402
add_extra_compact_tx &= !m_orphanage.HaveTx(wtxid);
403403

404-
auto add_orphan_reso_candidate = [&](const CTransactionRef& orphan_tx, const std::vector<Txid>& unique_parents, NodeId nodeid, std::chrono::microseconds now) {
405-
if (MaybeAddOrphanResolutionCandidate(unique_parents, orphan_tx->GetWitnessHash(), nodeid, now)) {
406-
m_orphanage.AddTx(orphan_tx, nodeid);
407-
}
408-
};
409-
410404
// If there is no candidate for orphan resolution, AddTx will not be called. This means
411405
// that if a peer is overloading us with invs and orphans, they will eventually not be
412406
// able to add any more transactions to the orphanage.
413-
add_orphan_reso_candidate(ptx, unique_parents, nodeid, now);
414-
for (const auto& candidate : m_txrequest.GetCandidatePeers(ptx)) {
415-
add_orphan_reso_candidate(ptx, unique_parents, candidate, now);
407+
//
408+
// Search by txid and, if the tx has a witness, wtxid
409+
std::vector<NodeId> orphan_resolution_candidates{nodeid};
410+
m_txrequest.GetCandidatePeers(ptx->GetHash().ToUint256(), orphan_resolution_candidates);
411+
if (ptx->HasWitness()) m_txrequest.GetCandidatePeers(ptx->GetWitnessHash().ToUint256(), orphan_resolution_candidates);
412+
413+
for (const auto& nodeid : orphan_resolution_candidates) {
414+
if (MaybeAddOrphanResolutionCandidate(unique_parents, ptx->GetWitnessHash(), nodeid, now)) {
415+
m_orphanage.AddTx(ptx, nodeid);
416+
}
416417
}
417418

418419
// Once added to the orphan pool, a tx is considered AlreadyHave, and we shouldn't request it anymore.

src/txrequest.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -574,21 +574,13 @@ class TxRequestTracker::Impl {
574574
}
575575
}
576576

577-
std::vector<NodeId> GetCandidatePeers(const CTransactionRef& tx) const
577+
void GetCandidatePeers(const uint256& txhash, std::vector<NodeId>& result_peers) const
578578
{
579-
// Search by txid and, if the tx has a witness, wtxid
580-
std::vector<uint256> hashes{tx->GetHash().ToUint256()};
581-
if (tx->HasWitness()) hashes.emplace_back(tx->GetWitnessHash().ToUint256());
582-
583-
std::vector<NodeId> result_peers;
584-
for (const uint256& txhash : hashes) {
585-
auto it = m_index.get<ByTxHash>().lower_bound(ByTxHashView{txhash, State::CANDIDATE_DELAYED, 0});
586-
while (it != m_index.get<ByTxHash>().end() && it->m_txhash == txhash && it->GetState() != State::COMPLETED) {
587-
result_peers.push_back(it->m_peer);
588-
++it;
589-
}
579+
auto it = m_index.get<ByTxHash>().lower_bound(ByTxHashView{txhash, State::CANDIDATE_DELAYED, 0});
580+
while (it != m_index.get<ByTxHash>().end() && it->m_txhash == txhash && it->GetState() != State::COMPLETED) {
581+
result_peers.push_back(it->m_peer);
582+
++it;
590583
}
591-
return result_peers;
592584
}
593585

594586
void ReceivedInv(NodeId peer, const GenTxid& gtxid, bool preferred,
@@ -738,7 +730,7 @@ size_t TxRequestTracker::CountInFlight(NodeId peer) const { return m_impl->Count
738730
size_t TxRequestTracker::CountCandidates(NodeId peer) const { return m_impl->CountCandidates(peer); }
739731
size_t TxRequestTracker::Count(NodeId peer) const { return m_impl->Count(peer); }
740732
size_t TxRequestTracker::Size() const { return m_impl->Size(); }
741-
std::vector<NodeId> TxRequestTracker::GetCandidatePeers(const CTransactionRef& tx) const { return m_impl->GetCandidatePeers(tx); }
733+
void TxRequestTracker::GetCandidatePeers(const uint256& txhash, std::vector<NodeId>& result_peers) const { return m_impl->GetCandidatePeers(txhash, result_peers); }
742734
void TxRequestTracker::SanityCheck() const { m_impl->SanityCheck(); }
743735

744736
void TxRequestTracker::PostGetRequestableSanityCheck(std::chrono::microseconds now) const

src/txrequest.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,9 @@ class TxRequestTracker {
195195
/** Count how many announcements are being tracked in total across all peers and transaction hashes. */
196196
size_t Size() const;
197197

198-
/** For some tx return all peers with non-COMPLETED announcements for its txid or wtxid. The resulting vector may contain duplicate NodeIds. */
199-
std::vector<NodeId> GetCandidatePeers(const CTransactionRef& tx) const;
198+
/** For some txhash (txid or wtxid), finds all peers with non-COMPLETED announcements and appends them to
199+
* result_peers. Does not try to ensure that result_peers contains no duplicates. */
200+
void GetCandidatePeers(const uint256& txhash, std::vector<NodeId>& result_peers) const;
200201

201202
/** Access to the internal priority computation (testing only) */
202203
uint64_t ComputePriority(const uint256& txhash, NodeId peer, bool preferred) const;

0 commit comments

Comments
 (0)