Skip to content

Commit c6131bf

Browse files
committed
Abstract logic to determine whether to answer tx GETDATA
1 parent 8da1e43 commit c6131bf

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

src/net_processing.cpp

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,26 @@ void static ProcessGetBlockData(CNode* pfrom, const CChainParams& chainparams, c
16081608
}
16091609
}
16101610

1611+
//! Determine whether or not a peer can request a transaction, and return it (or nullptr if not found or not allowed).
1612+
CTransactionRef static FindTxForGetData(const uint256& txid, const std::chrono::seconds mempool_req, const std::chrono::seconds longlived_mempool_time) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1613+
{
1614+
// Look up transaction in relay pool
1615+
auto mi = mapRelay.find(txid);
1616+
if (mi != mapRelay.end()) return mi->second;
1617+
1618+
auto txinfo = mempool.info(txid);
1619+
if (txinfo.tx) {
1620+
// To protect privacy, do not answer getdata using the mempool when
1621+
// that TX couldn't have been INVed in reply to a MEMPOOL request,
1622+
// or when it's too recent to have expired from mapRelay.
1623+
if ((mempool_req.count() && txinfo.m_time <= mempool_req) || txinfo.m_time <= longlived_mempool_time) {
1624+
return txinfo.tx;
1625+
}
1626+
}
1627+
1628+
return {};
1629+
}
1630+
16111631
void static ProcessGetData(CNode* pfrom, const CChainParams& chainparams, CConnman* connman, CTxMemPool& mempool, const std::atomic<bool>& interruptMsgProc) LOCKS_EXCLUDED(cs_main)
16121632
{
16131633
AssertLockNotHeld(cs_main);
@@ -1643,31 +1663,10 @@ void static ProcessGetData(CNode* pfrom, const CChainParams& chainparams, CConnm
16431663
continue;
16441664
}
16451665

1646-
// Send stream from relay memory
1647-
bool push = false;
1648-
auto mi = mapRelay.find(inv.hash);
1649-
int nSendFlags = (inv.type == MSG_TX ? SERIALIZE_TRANSACTION_NO_WITNESS : 0);
1650-
if (mi != mapRelay.end()) {
1651-
connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *mi->second));
1652-
push = true;
1653-
} else {
1654-
auto txinfo = mempool.info(inv.hash);
1655-
// To protect privacy, do not answer getdata using the mempool when
1656-
// that TX couldn't have been INVed in reply to a MEMPOOL request,
1657-
// or when it's too recent to have expired from mapRelay.
1658-
if (txinfo.tx && (
1659-
(mempool_req.count() && txinfo.m_time <= mempool_req)
1660-
|| (txinfo.m_time <= longlived_mempool_time)))
1661-
{
1662-
connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *txinfo.tx));
1663-
push = true;
1664-
}
1665-
}
1666-
1667-
if (push) {
1668-
// We interpret fulfilling a GETDATA for a transaction as a
1669-
// successful initial broadcast and remove it from our
1670-
// unbroadcast set.
1666+
CTransactionRef tx = FindTxForGetData(inv.hash, mempool_req, longlived_mempool_time);
1667+
if (tx) {
1668+
int nSendFlags = (inv.type == MSG_TX ? SERIALIZE_TRANSACTION_NO_WITNESS : 0);
1669+
connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *tx));
16711670
mempool.RemoveUnbroadcastTx(inv.hash);
16721671
} else {
16731672
vNotFound.push_back(inv);

0 commit comments

Comments
 (0)