Skip to content

Commit 308b767

Browse files
committed
Fix bug around transaction requests
If a transaction is already in-flight when a peer announces a new tx to us, we schedule a time in the future to reconsider whether to download. At that future time, there was a bug that would prevent transactions from being rescheduled for potential download again (ie if the transaction was still in-flight at the time of reconsideration, such as from some other peer). Fix this.
1 parent f635a3b commit 308b767

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

src/net_processing.cpp

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,24 @@ void UpdateTxRequestTime(const uint256& txid, int64_t request_time) EXCLUSIVE_LO
700700
}
701701
}
702702

703+
int64_t CalculateTxGetDataTime(const uint256& txid, int64_t current_time, bool use_inbound_delay) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
704+
{
705+
int64_t process_time;
706+
int64_t last_request_time = GetTxRequestTime(txid);
707+
// First time requesting this tx
708+
if (last_request_time == 0) {
709+
process_time = current_time;
710+
} else {
711+
// Randomize the delay to avoid biasing some peers over others (such as due to
712+
// fixed ordering of peer processing in ThreadMessageHandler)
713+
process_time = last_request_time + GETDATA_TX_INTERVAL + GetRand(MAX_GETDATA_RANDOM_DELAY);
714+
}
715+
716+
// We delay processing announcements from inbound peers
717+
if (use_inbound_delay) process_time += INBOUND_PEER_TX_DELAY;
718+
719+
return process_time;
720+
}
703721

704722
void RequestTx(CNodeState* state, const uint256& txid, int64_t nNow) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
705723
{
@@ -713,19 +731,9 @@ void RequestTx(CNodeState* state, const uint256& txid, int64_t nNow) EXCLUSIVE_L
713731
}
714732
peer_download_state.m_tx_announced.insert(txid);
715733

716-
int64_t process_time;
717-
int64_t last_request_time = GetTxRequestTime(txid);
718-
// First time requesting this tx
719-
if (last_request_time == 0) {
720-
process_time = nNow;
721-
} else {
722-
// Randomize the delay to avoid biasing some peers over others (such as due to
723-
// fixed ordering of peer processing in ThreadMessageHandler)
724-
process_time = last_request_time + GETDATA_TX_INTERVAL + GetRand(MAX_GETDATA_RANDOM_DELAY);
725-
}
726-
727-
// We delay processing announcements from non-preferred (eg inbound) peers
728-
if (!state->fPreferredDownload) process_time += INBOUND_PEER_TX_DELAY;
734+
// Calculate the time to try requesting this transaction. Use
735+
// fPreferredDownload as a proxy for outbound peers.
736+
int64_t process_time = CalculateTxGetDataTime(txid, nNow, !state->fPreferredDownload);
729737

730738
peer_download_state.m_tx_process_time.emplace(process_time, txid);
731739
}
@@ -3967,7 +3975,10 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
39673975

39683976
auto& tx_process_time = state.m_tx_download.m_tx_process_time;
39693977
while (!tx_process_time.empty() && tx_process_time.begin()->first <= nNow && state.m_tx_download.m_tx_in_flight.size() < MAX_PEER_TX_IN_FLIGHT) {
3970-
const uint256& txid = tx_process_time.begin()->second;
3978+
const uint256 txid = tx_process_time.begin()->second;
3979+
// Erase this entry from tx_process_time (it may be added back for
3980+
// processing at a later time, see below)
3981+
tx_process_time.erase(tx_process_time.begin());
39713982
CInv inv(MSG_TX | GetFetchFlags(pto), txid);
39723983
if (!AlreadyHave(inv)) {
39733984
// If this transaction was last requested more than 1 minute ago,
@@ -3987,14 +3998,14 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
39873998
// up processing to happen after the download times out
39883999
// (with a slight delay for inbound peers, to prefer
39894000
// requests to outbound peers).
3990-
RequestTx(&state, txid, nNow);
4001+
int64_t next_process_time = CalculateTxGetDataTime(txid, nNow, !state.fPreferredDownload);
4002+
tx_process_time.emplace(next_process_time, txid);
39914003
}
39924004
} else {
39934005
// We have already seen this transaction, no need to download.
39944006
state.m_tx_download.m_tx_announced.erase(inv.hash);
39954007
state.m_tx_download.m_tx_in_flight.erase(inv.hash);
39964008
}
3997-
tx_process_time.erase(tx_process_time.begin());
39984009
}
39994010

40004011

0 commit comments

Comments
 (0)