@@ -75,6 +75,8 @@ static const unsigned int MAX_INV_SZ = 50000;
75
75
static constexpr int32_t MAX_PEER_TX_IN_FLIGHT = 100 ;
76
76
/* * Maximum number of announced transactions from a peer */
77
77
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 }};
78
80
/* * How many microseconds to delay requesting transactions from inbound peers */
79
81
static constexpr std::chrono::microseconds INBOUND_PEER_TX_DELAY{std::chrono::seconds{2 }};
80
82
/* * How long to wait (in microseconds) before downloading a transaction from an additional peer */
@@ -219,6 +221,9 @@ namespace {
219
221
/* * Number of peers from which we're downloading blocks. */
220
222
int nPeersWithValidatedDownloads GUARDED_BY (cs_main) = 0;
221
223
224
+ /* * Number of peers with wtxid relay. */
225
+ int g_wtxid_relay_peers GUARDED_BY (cs_main) = 0;
226
+
222
227
/* * Number of outbound peers with m_chain_sync.m_protect. */
223
228
int g_outbound_peers_with_protect_from_disconnect GUARDED_BY (cs_main) = 0;
224
229
@@ -764,7 +769,7 @@ void UpdateTxRequestTime(const uint256& txid, std::chrono::microseconds request_
764
769
}
765
770
}
766
771
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)
768
773
{
769
774
std::chrono::microseconds process_time;
770
775
const auto last_request_time = GetTxRequestTime (txid);
@@ -780,6 +785,9 @@ std::chrono::microseconds CalculateTxGetDataTime(const uint256& txid, std::chron
780
785
// We delay processing announcements from inbound peers
781
786
if (use_inbound_delay) process_time += INBOUND_PEER_TX_DELAY;
782
787
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
+
783
791
return process_time;
784
792
}
785
793
@@ -797,7 +805,7 @@ void RequestTx(CNodeState* state, const uint256& txid, std::chrono::microseconds
797
805
798
806
// Calculate the time to try requesting this transaction. Use
799
807
// 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 );
801
809
802
810
peer_download_state.m_tx_process_time .emplace (process_time, txid);
803
811
}
@@ -874,6 +882,8 @@ void PeerLogicValidation::FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTim
874
882
assert (nPeersWithValidatedDownloads >= 0 );
875
883
g_outbound_peers_with_protect_from_disconnect -= state->m_chain_sync .m_protect ;
876
884
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 );
877
887
878
888
mapNodeState.erase (nodeid);
879
889
@@ -883,6 +893,7 @@ void PeerLogicValidation::FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTim
883
893
assert (nPreferredDownload == 0 );
884
894
assert (nPeersWithValidatedDownloads == 0 );
885
895
assert (g_outbound_peers_with_protect_from_disconnect == 0 );
896
+ assert (g_wtxid_relay_peers == 0 );
886
897
}
887
898
LogPrint (BCLog::NET, " Cleared nodestate for peer=%d\n " , nodeid);
888
899
}
@@ -2489,6 +2500,7 @@ void ProcessMessage(
2489
2500
LOCK (cs_main);
2490
2501
if (!State (pfrom.GetId ())->m_wtxid_relay ) {
2491
2502
State (pfrom.GetId ())->m_wtxid_relay = true ;
2503
+ g_wtxid_relay_peers++;
2492
2504
}
2493
2505
}
2494
2506
return ;
@@ -4490,7 +4502,15 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
4490
4502
// up processing to happen after the download times out
4491
4503
// (with a slight delay for inbound peers, to prefer
4492
4504
// 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 );
4494
4514
tx_process_time.emplace (next_process_time, txid);
4495
4515
}
4496
4516
} else {
0 commit comments