[#TxRequestTracker]
= TxRequestTracker
:mrdocs:

Data structure to keep track of, and schedule, transaction downloads from peers&period;

== Synopsis

Declared in `&lt;txrequest&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
class TxRequestTracker;
----

== Description

&equals;&equals;&equals; Specification &equals;&equals;&equals;

We keep track of which peers have announced which transactions, and use that to determine which requests should go to which peer, when, and in what order&period;

The following information is tracked per peer&sol;tx combination (&quot;announcement&quot;)&colon;

* Which peer announced it (through their NodeId)
* The txid or wtxid of the transaction (collectively called &quot;txhash&quot; in what follows)
* Whether it was a tx or wtx announcement (see BIP339)&period;
* What the earliest permitted time is that the transaction can be requested from that peer (called &quot;reqtime&quot;)&period;
* Whether it&apos;s from a &quot;preferred&quot; peer or not&period; Which announcements get this flag is determined by the caller, but this is designed for outbound peers, or other peers that we have a higher level of trust in&period; Even when the peers&apos; preferredness changes, the preferred flag of existing announcements from that peer won&apos;t change&period;
* Whether or not the transaction was requested already, and if so, when it times out (called &quot;expiry&quot;)&period;
* Whether or not the transaction request failed already (timed out, or invalid transaction or NOTFOUND was received)&period;

Transaction requests are then assigned to peers, following these rules&colon;

* No transaction is requested as long as another request for the same txhash is outstanding (it needs to fail first by passing expiry, or a NOTFOUND or invalid transaction has to be received for it)&period;

Rationale&colon; to avoid wasting bandwidth on multiple copies of the same transaction&period; Note that this only works per txhash, so if the same transaction is announced both through txid and wtxid, we have no means to prevent fetching both (the caller can however mitigate this by delaying one, see further)&period;

* The same transaction is never requested twice from the same peer, unless the announcement was forgotten in between, and re&hyphen;announced&period; Announcements are forgotten only&colon;
* If a peer goes offline, all its announcements are forgotten&period;
* If a transaction has been successfully received, or is otherwise no longer needed, the caller can call ForgetTxHash, which removes all announcements across all peers with the specified txhash&period;
* If for a given txhash only already&hyphen;failed announcements remain, they are all forgotten&period;

Rationale&colon; giving a peer multiple chances to announce a transaction would allow them to bias requests in their favor, worsening transaction censoring attacks&period; The flip side is that as long as an attacker manages to prevent us from receiving a transaction, failed announcements (including those from honest peers) will linger longer, increasing memory usage somewhat&period; The impact of this is limited by imposing a cap on the number of tracked announcements per peer&period; As failed requests in response to announcements from honest peers should be rare, this almost solely hinders attackers&period; Transaction censoring attacks can be done by announcing transactions quickly while not answering requests for them&period; See https&colon;&sol;&sol;www&period;cs&period;umd&period;edu&sol;projects&sol;coinscope&sol;coinscope&period;pdf for more information&period;

* Transactions are not requested from a peer until its reqtime has passed&period;

Rationale&colon; enable the calling code to define a delay for less&hyphen;than&hyphen;ideal peers, so that (presumed) better peers have a chance to give their announcement first&period;

* If multiple viable candidate peers exist according to the above rules, pick a peer as follows&colon;

* If any preferred peers are available, non&hyphen;preferred peers are not considered for what follows&period;

Rationale&colon; preferred peers are more trusted by us, so are less likely to be under attacker control&period;

* Pick a uniformly random peer among the candidates&period;

Rationale&colon; random assignments are hard to influence for attackers&period;

Together these rules strike a balance between being fast in non&hyphen;adverserial conditions and minimizing susceptibility to censorship attacks&period; An attacker that races the network&colon;

* Will be unsuccessful if all preferred connections are honest (and there is at least one preferred connection)&period;
* If there are P preferred connections of which Ph&gt;&equals;1 are honest, the attacker can delay us from learning about a transaction by k expiration periods, where k &#126; 1 &plus; NHG(N&equals;P&hyphen;1,K&equals;P&hyphen;Ph&hyphen;1,r&equals;1), which has mean P&sol;(Ph&plus;1) (where NHG stands for Negative Hypergeometric distribution)&period; The &quot;1 &plus;&quot; is due to the fact that the attacker can be the first to announce through a preferred connection in this scenario, which very likely means they get the first request&period;
* If all P preferred connections are to the attacker, and there are NP non&hyphen;preferred connections of which NPh&gt;&equals;1 are honest, where we assume that the attacker can disconnect and reconnect those connections, the distribution becomes k &#126; P &plus; NB(p&equals;1&hyphen;NPh&sol;NP,r&equals;1) (where NB stands for Negative Binomial distribution), which has mean P&hyphen;1&plus;NP&sol;NPh&period;

Complexity&colon;

* Memory usage is proportional to the total number of tracked announcements (Size()) plus the number of peers with a nonzero number of tracked announcements&period;
* CPU usage is generally logarithmic in the total number of tracked announcements, plus the number of announcements affected by an operation (amortized O(1) per announcement)&period;

Context&colon;

* In an earlier version of the transaction request logic it was possible for a peer to prevent us from seeing a specific transaction&period; See https&colon;&sol;&sol;bitcoincore&period;org&sol;en&sol;2024&sol;07&sol;03&sol;disclose&lowbar;already&lowbar;asked&lowbar;for&period;

== Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:TxRequestTracker/2constructor.adoc[`TxRequestTracker`]         [.small]#[constructor]#
| Construct a TxRequestTracker&period;
| xref:TxRequestTracker/2destructor.adoc[`&#126;TxRequestTracker`] [.small]#[destructor]#
| Destroy the TxRequestTracker&period;
| xref:TxRequestTracker/ComputePriority.adoc[`ComputePriority`] 
| Access to the internal priority computation (testing only)
| xref:TxRequestTracker/Count.adoc[`Count`] 
| Count how many announcements a peer has (REQUESTED, CANDIDATE, and COMPLETED combined)&period;
| xref:TxRequestTracker/CountCandidates.adoc[`CountCandidates`] 
| Count how many CANDIDATE announcements a peer has&period;
| xref:TxRequestTracker/CountInFlight.adoc[`CountInFlight`] 
| Count how many REQUESTED announcements a peer has&period;
| xref:TxRequestTracker/DisconnectedPeer.adoc[`DisconnectedPeer`] 
| Deletes all announcements for a given peer&period;
| xref:TxRequestTracker/ForgetTxHash.adoc[`ForgetTxHash`] 
| Deletes all announcements for a given txhash (both txid and wtxid ones)&period;
| xref:TxRequestTracker/GetCandidatePeers.adoc[`GetCandidatePeers`] 
| For some txhash (txid or wtxid), finds all peers with non&hyphen;COMPLETED announcements and appends them to result&lowbar;peers&period; Does not try to ensure that result&lowbar;peers contains no duplicates&period;
| xref:TxRequestTracker/GetRequestable.adoc[`GetRequestable`] 
| Find the txids to request now from peer&period;
| xref:TxRequestTracker/PostGetRequestableSanityCheck.adoc[`PostGetRequestableSanityCheck`] 
| Run a time&hyphen;dependent internal consistency check (testing only)&period;
| xref:TxRequestTracker/ReceivedInv.adoc[`ReceivedInv`] 
| Adds a new CANDIDATE announcement&period;
| xref:TxRequestTracker/ReceivedResponse.adoc[`ReceivedResponse`] 
| Converts a CANDIDATE or REQUESTED announcement to a COMPLETED one&period; If no such announcement exists for the provided peer and txhash, nothing happens&period;
| xref:TxRequestTracker/RequestedTx.adoc[`RequestedTx`] 
| Marks a transaction as requested, with a specified expiry&period;
| xref:TxRequestTracker/SanityCheck.adoc[`SanityCheck`] 
| Run internal consistency check (testing only)&period;
| xref:TxRequestTracker/Size.adoc[`Size`] 
| Count how many announcements are being tracked in total across all peers and transaction hashes&period;
|===



[.small]#Created with https://www.mrdocs.com[MrDocs]#
