Skip to content

Commit 1b52839

Browse files
committed
Convert txrequest to GenTxidVariant
Switch all instances of GenTxid to the new variant in `txrequest` and complete `txdownloadman_impl` by converting `GetRequestsToSend`.
1 parent bde4579 commit 1b52839

File tree

9 files changed

+103
-108
lines changed

9 files changed

+103
-108
lines changed

src/net_processing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5962,8 +5962,8 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
59625962
//
59635963
{
59645964
LOCK(m_tx_download_mutex);
5965-
for (const GenTxid& gtxid : m_txdownloadman.GetRequestsToSend(pto->GetId(), current_time)) {
5966-
vGetData.emplace_back(gtxid.IsWtxid() ? MSG_WTX : (MSG_TX | GetFetchFlags(*peer)), gtxid.GetHash());
5965+
for (const GenTxidVariant& gtxid : m_txdownloadman.GetRequestsToSend(pto->GetId(), current_time)) {
5966+
vGetData.emplace_back(gtxid.IsWtxid() ? MSG_WTX : (MSG_TX | GetFetchFlags(*peer)), gtxid.ToUint256());
59675967
if (vGetData.size() >= MAX_GETDATA_SZ) {
59685968
MakeAndPushMessage(*pto, NetMsgType::GETDATA, vGetData);
59695969
vGetData.clear();

src/node/txdownloadman.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class TxDownloadManager {
141141
bool AddTxAnnouncement(NodeId peer, const GenTxidVariant& gtxid, std::chrono::microseconds now);
142142

143143
/** Get getdata requests to send. */
144-
std::vector<GenTxid> GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time);
144+
std::vector<GenTxidVariant> GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time);
145145

146146
/** Should be called when a notfound for a tx has been received. */
147147
void ReceivedNotFound(NodeId nodeid, const std::vector<GenTxidVariant>& gtxids);

src/node/txdownloadman_impl.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ bool TxDownloadManager::AddTxAnnouncement(NodeId peer, const GenTxidVariant& gtx
4343
{
4444
return m_impl->AddTxAnnouncement(peer, gtxid, now);
4545
}
46-
std::vector<GenTxid> TxDownloadManager::GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time)
46+
std::vector<GenTxidVariant> TxDownloadManager::GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time)
4747
{
4848
return m_impl->GetRequestsToSend(nodeid, current_time);
4949
}
@@ -218,7 +218,7 @@ bool TxDownloadManagerImpl::AddTxAnnouncement(NodeId peer, const GenTxidVariant&
218218
const bool overloaded = !info.m_relay_permissions && m_txrequest.CountInFlight(peer) >= MAX_PEER_TX_REQUEST_IN_FLIGHT;
219219
if (overloaded) delay += OVERLOADED_PEER_TX_DELAY;
220220

221-
m_txrequest.ReceivedInv(peer, GenTxid::FromVariant(gtxid), info.m_preferred, now + delay);
221+
m_txrequest.ReceivedInv(peer, gtxid, info.m_preferred, now + delay);
222222

223223
return false;
224224
}
@@ -255,31 +255,31 @@ bool TxDownloadManagerImpl::MaybeAddOrphanResolutionCandidate(const std::vector<
255255
// Treat finding orphan resolution candidate as equivalent to the peer announcing all missing parents.
256256
// In the future, orphan resolution may include more explicit steps
257257
for (const auto& parent_txid : unique_parents) {
258-
m_txrequest.ReceivedInv(nodeid, GenTxid::Txid(parent_txid), info.m_preferred, now + delay);
258+
m_txrequest.ReceivedInv(nodeid, parent_txid, info.m_preferred, now + delay);
259259
}
260260
LogDebug(BCLog::TXPACKAGES, "added peer=%d as a candidate for resolving orphan %s\n", nodeid, wtxid.ToString());
261261
return true;
262262
}
263263

264-
std::vector<GenTxid> TxDownloadManagerImpl::GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time)
264+
std::vector<GenTxidVariant> TxDownloadManagerImpl::GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time)
265265
{
266-
std::vector<GenTxid> requests;
267-
std::vector<std::pair<NodeId, GenTxid>> expired;
266+
std::vector<GenTxidVariant> requests;
267+
std::vector<std::pair<NodeId, GenTxidVariant>> expired;
268268
auto requestable = m_txrequest.GetRequestable(nodeid, current_time, &expired);
269-
for (const auto& entry : expired) {
270-
LogDebug(BCLog::NET, "timeout of inflight %s %s from peer=%d\n", entry.second.IsWtxid() ? "wtx" : "tx",
271-
entry.second.GetHash().ToString(), entry.first);
269+
for (const auto& [expired_nodeid, gtxid] : expired) {
270+
LogDebug(BCLog::NET, "timeout of inflight %s %s from peer=%d\n", gtxid.IsWtxid() ? "wtx" : "tx",
271+
gtxid.ToUint256().ToString(), expired_nodeid);
272272
}
273-
for (const GenTxid& gtxid : requestable) {
274-
if (!AlreadyHaveTx(gtxid.ToVariant(), /*include_reconsiderable=*/false)) {
273+
for (const GenTxidVariant& gtxid : requestable) {
274+
if (!AlreadyHaveTx(gtxid, /*include_reconsiderable=*/false)) {
275275
LogDebug(BCLog::NET, "Requesting %s %s peer=%d\n", gtxid.IsWtxid() ? "wtx" : "tx",
276-
gtxid.GetHash().ToString(), nodeid);
276+
gtxid.ToUint256().ToString(), nodeid);
277277
requests.emplace_back(gtxid);
278-
m_txrequest.RequestedTx(nodeid, gtxid.GetHash(), current_time + GETDATA_TX_INTERVAL);
278+
m_txrequest.RequestedTx(nodeid, gtxid.ToUint256(), current_time + GETDATA_TX_INTERVAL);
279279
} else {
280280
// We have already seen this transaction, no need to download. This is just a belt-and-suspenders, as
281281
// this should already be called whenever a transaction becomes AlreadyHaveTx().
282-
m_txrequest.ForgetTxHash(gtxid.GetHash());
282+
m_txrequest.ForgetTxHash(gtxid.ToUint256());
283283
}
284284
}
285285
return requests;

src/node/txdownloadman_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class TxDownloadManagerImpl {
166166
bool AddTxAnnouncement(NodeId peer, const GenTxidVariant& gtxid, std::chrono::microseconds now);
167167

168168
/** Get getdata requests to send. */
169-
std::vector<GenTxid> GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time);
169+
std::vector<GenTxidVariant> GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time);
170170

171171
/** Marks a tx as ReceivedResponse in txrequest. */
172172
void ReceivedNotFound(NodeId nodeid, const std::vector<GenTxidVariant>& gtxids);

src/test/fuzz/txdownloadman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ FUZZ_TARGET(txdownloadman_impl, .init = initialize)
382382
// TxDownloadManager should not be telling us to request things we already have.
383383
// Exclude m_lazy_recent_rejects_reconsiderable because it may request low-feerate parent of orphan.
384384
for (const auto& gtxid : getdata_requests) {
385-
Assert(!txdownload_impl.AlreadyHaveTx(gtxid.ToVariant(), /*include_reconsiderable=*/false));
385+
Assert(!txdownload_impl.AlreadyHaveTx(gtxid, /*include_reconsiderable=*/false));
386386
}
387387
},
388388
[&] {

src/test/fuzz/txrequest.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace {
1919
constexpr int MAX_TXHASHES = 16;
2020
constexpr int MAX_PEERS = 16;
2121

22-
//! Randomly generated GenTxids used in this test (length is MAX_TXHASHES).
22+
//! Randomly generated txhashes used in this test (length is MAX_TXHASHES).
2323
uint256 TXHASHES[MAX_TXHASHES];
2424

2525
//! Precomputed random durations (positive and negative, each ~exponentially distributed).
@@ -204,7 +204,8 @@ class Tester
204204
}
205205

206206
// Call TxRequestTracker's implementation.
207-
m_tracker.ReceivedInv(peer, is_wtxid ? GenTxid::Wtxid(TXHASHES[txhash]) : GenTxid::Txid(TXHASHES[txhash]), preferred, reqtime);
207+
auto gtxid = is_wtxid ? GenTxidVariant{Wtxid::FromUint256(TXHASHES[txhash])} : GenTxidVariant{Txid::FromUint256(TXHASHES[txhash])};
208+
m_tracker.ReceivedInv(peer, gtxid, preferred, reqtime);
208209
}
209210

210211
void RequestedTx(int peer, int txhash, std::chrono::microseconds exptime)
@@ -246,13 +247,14 @@ class Tester
246247

247248
//! list of (sequence number, txhash, is_wtxid) tuples.
248249
std::vector<std::tuple<uint64_t, int, bool>> result;
249-
std::vector<std::pair<NodeId, GenTxid>> expected_expired;
250+
std::vector<std::pair<NodeId, GenTxidVariant>> expected_expired;
250251
for (int txhash = 0; txhash < MAX_TXHASHES; ++txhash) {
251252
// Mark any expired REQUESTED announcements as COMPLETED.
252253
for (int peer2 = 0; peer2 < MAX_PEERS; ++peer2) {
253254
Announcement& ann2 = m_announcements[txhash][peer2];
254255
if (ann2.m_state == State::REQUESTED && ann2.m_time <= m_now) {
255-
expected_expired.emplace_back(peer2, ann2.m_is_wtxid ? GenTxid::Wtxid(TXHASHES[txhash]) : GenTxid::Txid(TXHASHES[txhash]));
256+
auto gtxid = ann2.m_is_wtxid ? GenTxidVariant{Wtxid::FromUint256(TXHASHES[txhash])} : GenTxidVariant{Txid::FromUint256(TXHASHES[txhash])};
257+
expected_expired.emplace_back(peer2, gtxid);
256258
ann2.m_state = State::COMPLETED;
257259
break;
258260
}
@@ -270,15 +272,15 @@ class Tester
270272
std::sort(expected_expired.begin(), expected_expired.end());
271273

272274
// Compare with TxRequestTracker's implementation.
273-
std::vector<std::pair<NodeId, GenTxid>> expired;
275+
std::vector<std::pair<NodeId, GenTxidVariant>> expired;
274276
const auto actual = m_tracker.GetRequestable(peer, m_now, &expired);
275277
std::sort(expired.begin(), expired.end());
276278
assert(expired == expected_expired);
277279

278280
m_tracker.PostGetRequestableSanityCheck(m_now);
279281
assert(result.size() == actual.size());
280282
for (size_t pos = 0; pos < actual.size(); ++pos) {
281-
assert(TXHASHES[std::get<1>(result[pos])] == actual[pos].GetHash());
283+
assert(TXHASHES[std::get<1>(result[pos])] == actual[pos].ToUint256());
282284
assert(std::get<2>(result[pos]) == actual[pos].IsWtxid());
283285
}
284286
}

0 commit comments

Comments
 (0)