Skip to content

Commit 97141ca

Browse files
committed
Delay getdata requests from peers using txid-based relay
Using both txid and wtxid-based relay with peers means that we could sometimes download the same transaction twice, if announced via two different hashes from different peers. Use a heuristic of delaying txid-peer-getdata requests by 2 seconds, if we have at least one wtxid-based peer.
1 parent 46d78d4 commit 97141ca

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/net_processing.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ static const unsigned int MAX_INV_SZ = 50000;
7575
static constexpr int32_t MAX_PEER_TX_IN_FLIGHT = 100;
7676
/** Maximum number of announced transactions from a peer */
7777
static constexpr int32_t MAX_PEER_TX_ANNOUNCEMENTS = 2 * MAX_INV_SZ;
78+
/** How many microseconds to delay requesting transactions via txids, if we have wtxid-relaying peers */
79+
static constexpr std::chrono::microseconds TXID_RELAY_DELAY{std::chrono::seconds{2}};
7880
/** How many microseconds to delay requesting transactions from inbound peers */
7981
static constexpr std::chrono::microseconds INBOUND_PEER_TX_DELAY{std::chrono::seconds{2}};
8082
/** How long to wait (in microseconds) before downloading a transaction from an additional peer */
@@ -219,6 +221,9 @@ namespace {
219221
/** Number of peers from which we're downloading blocks. */
220222
int nPeersWithValidatedDownloads GUARDED_BY(cs_main) = 0;
221223

224+
/** Number of peers with wtxid relay. */
225+
int g_wtxid_relay_peers GUARDED_BY(cs_main) = 0;
226+
222227
/** Number of outbound peers with m_chain_sync.m_protect. */
223228
int g_outbound_peers_with_protect_from_disconnect GUARDED_BY(cs_main) = 0;
224229

@@ -764,7 +769,7 @@ void UpdateTxRequestTime(const uint256& txid, std::chrono::microseconds request_
764769
}
765770
}
766771

767-
std::chrono::microseconds CalculateTxGetDataTime(const uint256& txid, std::chrono::microseconds current_time, bool use_inbound_delay) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
772+
std::chrono::microseconds CalculateTxGetDataTime(const uint256& txid, std::chrono::microseconds current_time, bool use_inbound_delay, bool use_txid_delay) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
768773
{
769774
std::chrono::microseconds process_time;
770775
const auto last_request_time = GetTxRequestTime(txid);
@@ -780,6 +785,9 @@ std::chrono::microseconds CalculateTxGetDataTime(const uint256& txid, std::chron
780785
// We delay processing announcements from inbound peers
781786
if (use_inbound_delay) process_time += INBOUND_PEER_TX_DELAY;
782787

788+
// We delay processing announcements from peers that use txid-relay (instead of wtxid)
789+
if (use_txid_delay) process_time += TXID_RELAY_DELAY;
790+
783791
return process_time;
784792
}
785793

@@ -797,7 +805,7 @@ void RequestTx(CNodeState* state, const uint256& txid, std::chrono::microseconds
797805

798806
// Calculate the time to try requesting this transaction. Use
799807
// fPreferredDownload as a proxy for outbound peers.
800-
const auto process_time = CalculateTxGetDataTime(txid, current_time, !state->fPreferredDownload);
808+
const auto process_time = CalculateTxGetDataTime(txid, current_time, !state->fPreferredDownload, !state->m_wtxid_relay && g_wtxid_relay_peers > 0);
801809

802810
peer_download_state.m_tx_process_time.emplace(process_time, txid);
803811
}
@@ -874,6 +882,8 @@ void PeerLogicValidation::FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTim
874882
assert(nPeersWithValidatedDownloads >= 0);
875883
g_outbound_peers_with_protect_from_disconnect -= state->m_chain_sync.m_protect;
876884
assert(g_outbound_peers_with_protect_from_disconnect >= 0);
885+
g_wtxid_relay_peers -= state->m_wtxid_relay;
886+
assert(g_wtxid_relay_peers >= 0);
877887

878888
mapNodeState.erase(nodeid);
879889

@@ -883,6 +893,7 @@ void PeerLogicValidation::FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTim
883893
assert(nPreferredDownload == 0);
884894
assert(nPeersWithValidatedDownloads == 0);
885895
assert(g_outbound_peers_with_protect_from_disconnect == 0);
896+
assert(g_wtxid_relay_peers == 0);
886897
}
887898
LogPrint(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid);
888899
}
@@ -2489,6 +2500,7 @@ void ProcessMessage(
24892500
LOCK(cs_main);
24902501
if (!State(pfrom.GetId())->m_wtxid_relay) {
24912502
State(pfrom.GetId())->m_wtxid_relay = true;
2503+
g_wtxid_relay_peers++;
24922504
}
24932505
}
24942506
return;
@@ -4490,7 +4502,15 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
44904502
// up processing to happen after the download times out
44914503
// (with a slight delay for inbound peers, to prefer
44924504
// requests to outbound peers).
4493-
const auto next_process_time = CalculateTxGetDataTime(txid, current_time, !state.fPreferredDownload);
4505+
// Don't apply the txid-delay to re-requests of a
4506+
// transaction; the heuristic of delaying requests to
4507+
// txid-relay peers is to save bandwidth on initial
4508+
// announcement of a transaction, and doesn't make sense
4509+
// for a followup request if our first peer times out (and
4510+
// would open us up to an attacker using inbound
4511+
// wtxid-relay to prevent us from requesting transactions
4512+
// from outbound txid-relay peers).
4513+
const auto next_process_time = CalculateTxGetDataTime(txid, current_time, !state.fPreferredDownload, false);
44944514
tx_process_time.emplace(next_process_time, txid);
44954515
}
44964516
} else {

test/functional/p2p_tx_download.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ def on_getdata(self, message):
4444
GETDATA_TX_INTERVAL = 60 # seconds
4545
MAX_GETDATA_RANDOM_DELAY = 2 # seconds
4646
INBOUND_PEER_TX_DELAY = 2 # seconds
47+
TXID_RELAY_DELAY = 2 # seconds
4748
MAX_GETDATA_IN_FLIGHT = 100
4849
TX_EXPIRY_INTERVAL = GETDATA_TX_INTERVAL * 10
4950

5051
# Python test constants
5152
NUM_INBOUND = 10
52-
MAX_GETDATA_INBOUND_WAIT = GETDATA_TX_INTERVAL + MAX_GETDATA_RANDOM_DELAY + INBOUND_PEER_TX_DELAY
53+
MAX_GETDATA_INBOUND_WAIT = GETDATA_TX_INTERVAL + MAX_GETDATA_RANDOM_DELAY + INBOUND_PEER_TX_DELAY + TXID_RELAY_DELAY
5354

5455

5556
class TxDownloadTest(BitcoinTestFramework):

0 commit comments

Comments
 (0)