Skip to content

Commit 57221ad

Browse files
committed
[refactor] move parent inv-adding to OrphanResolutionCandidate
Deduplicate the logic of adding the parents as announcements to txrequest. The function can return a bool (indicating whether we're attempting orphan resolution) instead of the delay.
1 parent 89720b7 commit 57221ad

File tree

2 files changed

+20
-28
lines changed

2 files changed

+20
-28
lines changed

src/node/txdownloadman_impl.cpp

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,8 @@ bool TxDownloadManagerImpl::AddTxAnnouncement(NodeId peer, const GenTxid& gtxid,
187187

188188
if (unique_parents.empty()) return true;
189189

190-
if (auto delay{OrphanResolutionCandidate(peer, Wtxid::FromUint256(gtxid.GetHash()), unique_parents.size())}) {
191-
m_orphanage.AddAnnouncer(Wtxid::FromUint256(gtxid.GetHash()), peer);
192-
193-
const auto& info = m_peer_info.at(peer).m_connection_info;
194-
for (const auto& parent_txid : unique_parents) {
195-
m_txrequest.ReceivedInv(peer, GenTxid::Txid(parent_txid), info.m_preferred, now + *delay);
196-
}
197-
198-
LogDebug(BCLog::TXPACKAGES, "added peer=%d as a candidate for resolving orphan %s\n", peer, gtxid.GetHash().ToString());
190+
if (OrphanResolutionCandidate(unique_parents, orphan_tx->GetWitnessHash(), peer, now)) {
191+
m_orphanage.AddAnnouncer(orphan_tx->GetWitnessHash(), peer);
199192
}
200193

201194
// Return even if the peer isn't an orphan resolution candidate. This would be caught by AlreadyHaveTx.
@@ -231,21 +224,23 @@ bool TxDownloadManagerImpl::AddTxAnnouncement(NodeId peer, const GenTxid& gtxid,
231224
return false;
232225
}
233226

234-
std::optional<std::chrono::seconds> TxDownloadManagerImpl::OrphanResolutionCandidate(NodeId nodeid, const Wtxid& orphan_wtxid, size_t num_parents)
227+
bool TxDownloadManagerImpl::OrphanResolutionCandidate(const std::vector<Txid>& unique_parents, const Wtxid& wtxid, NodeId nodeid, std::chrono::microseconds now)
235228
{
236-
if (m_peer_info.count(nodeid) == 0) return std::nullopt;
237-
if (m_orphanage.HaveTxFromPeer(orphan_wtxid, nodeid)) return std::nullopt;
229+
auto it_peer = m_peer_info.find(nodeid);
230+
if (it_peer == m_peer_info.end()) return false;
231+
if (m_orphanage.HaveTxFromPeer(wtxid, nodeid)) return false;
238232

239233
const auto& peer_entry = m_peer_info.at(nodeid);
240234
const auto& info = peer_entry.m_connection_info;
235+
241236
// TODO: add delays and limits based on the amount of orphan resolution we are already doing
242237
// with this peer, how much they are using the orphanage, etc.
243238
if (!info.m_relay_permissions) {
244239
// This mirrors the delaying and dropping behavior in AddTxAnnouncement in order to preserve
245240
// existing behavior: drop if we are tracking too many invs for this peer already. Each
246241
// orphan resolution involves at least 1 transaction request which may or may not be
247242
// currently tracked in m_txrequest, so we include that in the count.
248-
if (m_txrequest.Count(nodeid) + num_parents > MAX_PEER_TX_ANNOUNCEMENTS) return std::nullopt;
243+
if (m_txrequest.Count(nodeid) + unique_parents.size() > MAX_PEER_TX_ANNOUNCEMENTS) return false;
249244
}
250245

251246
std::chrono::seconds delay{0s};
@@ -258,7 +253,13 @@ std::optional<std::chrono::seconds> TxDownloadManagerImpl::OrphanResolutionCandi
258253
const bool overloaded = !info.m_relay_permissions && m_txrequest.CountInFlight(nodeid) >= MAX_PEER_TX_REQUEST_IN_FLIGHT;
259254
if (overloaded) delay += OVERLOADED_PEER_TX_DELAY;
260255

261-
return delay;
256+
// Treat finding orphan resolution candidate as equivalent to the peer announcing all missing parents.
257+
// In the future, orphan resolution may include more explicit steps
258+
for (const auto& parent_txid : unique_parents) {
259+
m_txrequest.ReceivedInv(nodeid, GenTxid::Txid(parent_txid), info.m_preferred, now + delay);
260+
}
261+
LogDebug(BCLog::TXPACKAGES, "added peer=%d as a candidate for resolving orphan %s\n", nodeid, wtxid.ToString());
262+
return true;
262263
}
263264

264265
std::vector<GenTxid> TxDownloadManagerImpl::GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time)
@@ -401,17 +402,8 @@ node::RejectedTxTodo TxDownloadManagerImpl::MempoolRejectedTx(const CTransaction
401402
add_extra_compact_tx &= !m_orphanage.HaveTx(wtxid);
402403

403404
auto add_orphan_reso_candidate = [&](const CTransactionRef& orphan_tx, const std::vector<Txid>& unique_parents, NodeId nodeid, std::chrono::microseconds now) {
404-
const auto& wtxid = orphan_tx->GetWitnessHash();
405-
if (auto delay{OrphanResolutionCandidate(nodeid, wtxid, unique_parents.size())}) {
406-
const auto& info = m_peer_info.at(nodeid).m_connection_info;
405+
if (OrphanResolutionCandidate(unique_parents, orphan_tx->GetWitnessHash(), nodeid, now)) {
407406
m_orphanage.AddTx(orphan_tx, nodeid);
408-
409-
// Treat finding orphan resolution candidate as equivalent to the peer announcing all missing parents
410-
// In the future, orphan resolution may include more explicit steps
411-
for (const auto& parent_txid : unique_parents) {
412-
m_txrequest.ReceivedInv(nodeid, GenTxid::Txid(parent_txid), info.m_preferred, now + *delay);
413-
}
414-
LogDebug(BCLog::TXPACKAGES, "added peer=%d as a candidate for resolving orphan %s\n", nodeid, wtxid.ToString());
415407
}
416408
};
417409

src/node/txdownloadman_impl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ class TxDownloadManagerImpl {
194194
/** Helper for getting deduplicated vector of Txids in vin. */
195195
std::vector<Txid> GetUniqueParents(const CTransaction& tx);
196196

197-
/** Determine candidacy (and delay) for potential orphan resolution candidate.
198-
* @returns delay for orphan resolution if this peer is a good candidate for orphan resolution,
199-
* std::nullopt if this peer cannot be added because it has reached download/orphanage limits.
197+
/** If this peer is an orphan resolution candidate for this transaction, treat the unique_parents as announced by
198+
* this peer; add them as new invs to m_txrequest.
199+
* @returns whether this transaction was a valid orphan resolution candidate.
200200
* */
201-
std::optional<std::chrono::seconds> OrphanResolutionCandidate(NodeId nodeid, const Wtxid& orphan_wtxid, size_t num_parents);
201+
bool OrphanResolutionCandidate(const std::vector<Txid>& unique_parents, const Wtxid& wtxid, NodeId nodeid, std::chrono::microseconds now);
202202
};
203203
} // namespace node
204204
#endif // BITCOIN_NODE_TXDOWNLOADMAN_IMPL_H

0 commit comments

Comments
 (0)