Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4565,6 +4565,16 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,

LOCK2(cs_main, m_tx_download_mutex);

// Do not process unrequested transactions to mitigate potential DoS risks.
// We check both identifiers as txid mode may happen with wtxidrelay peers
// due to parent-orphan fetching.
bool is_expected = tx.HasWitness() ? m_txrequest.ExpectedTx(pfrom.GetId(), wtxid) ||
m_txrequest.ExpectedTx(pfrom.GetId(), txid) : m_txrequest.ExpectedTx(pfrom.GetId(), txid);
if (!is_expected) {
LogDebug(BCLog::NET, "unrequested transaction %s (wtxid=%s) from peer=%d (%s)\n",
tx.GetHash().ToString(), tx.GetWitnessHash().ToString(), pfrom.GetId(), pfrom.m_addr_name);
}

m_txrequest.ReceivedResponse(pfrom.GetId(), txid);
if (tx.HasWitness()) m_txrequest.ReceivedResponse(pfrom.GetId(), wtxid);

Expand Down
18 changes: 18 additions & 0 deletions src/txrequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,19 @@ class TxRequestTracker::Impl {
if (it != m_index.get<ByPeer>().end()) MakeCompleted(m_index.project<ByTxHash>(it));
}

bool ExpectedTx(NodeId peer, const uint256& txhash)
{
// We need to traverse all the REQUEST / COMPLETED announcements to verify we effectively
// requested the txhash from this peer.
auto it = m_index.get<ByPeer>().find(ByPeerView{peer, false, txhash});
if (it == m_index.get<ByPeer>().end()) {
it = m_index.get<ByPeer>().find(ByPeerView{peer, true, txhash});
}
if (it != m_index.get<ByPeer>().end()) { return false; }
return true;
}


size_t CountInFlight(NodeId peer) const
{
auto it = m_peerinfo.find(peer);
Expand Down Expand Up @@ -744,6 +757,11 @@ void TxRequestTracker::ReceivedResponse(NodeId peer, const uint256& txhash)
m_impl->ReceivedResponse(peer, txhash);
}

bool TxRequestTracker::ExpectedTx(NodeId peer, const uint256& txhash)
{
return m_impl->ExpectedTx(peer, txhash);
}

std::vector<GenTxid> TxRequestTracker::GetRequestable(NodeId peer, std::chrono::microseconds now,
std::vector<std::pair<NodeId, GenTxid>>* expired)
{
Expand Down
2 changes: 2 additions & 0 deletions src/txrequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ class TxRequestTracker {
*/
void ReceivedResponse(NodeId peer, const uint256& txhash);

bool ExpectedTx(NodeId peer, const uint256& txhash);

// The operations below inspect the data structure.

/** Count how many REQUESTED announcements a peer has. */
Expand Down