Bitcoin Core  31.0.0
P2P Digital Currency
txrequest.h
Go to the documentation of this file.
1 // Copyright (c) 2020-present The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_TXREQUEST_H
6 #define BITCOIN_TXREQUEST_H
7 
9 #include <net.h>
10 #include <uint256.h>
11 
12 #include <chrono>
13 #include <cstdint>
14 #include <memory>
15 #include <vector>
16 
101  // Avoid littering this header file with implementation details.
102  class Impl;
103  const std::unique_ptr<Impl> m_impl;
104 
105 public:
107  explicit TxRequestTracker(bool deterministic = false);
109 
110  // Conceptually, the data structure consists of a collection of "announcements", one for each peer/txhash
111  // combination:
112  //
113  // - CANDIDATE announcements represent transactions that were announced by a peer, and that become available for
114  // download after their reqtime has passed.
115  //
116  // - REQUESTED announcements represent transactions that have been requested, and which we're awaiting a
117  // response for from that peer. Their expiry value determines when the request times out.
118  //
119  // - COMPLETED announcements represent transactions that have been requested from a peer, and a NOTFOUND or a
120  // transaction was received in response (valid or not), or they timed out. They're only kept around to
121  // prevent requesting them again. If only COMPLETED announcements for a given txhash remain (so no CANDIDATE
122  // or REQUESTED ones), all of them are deleted (this is an invariant, and maintained by all operations below).
123  //
124  // The operations below manipulate the data structure.
125 
135  void ReceivedInv(NodeId peer, const GenTxid& gtxid, bool preferred,
136  std::chrono::microseconds reqtime);
137 
142  void DisconnectedPeer(NodeId peer);
143 
149  void ForgetTxHash(const uint256& txhash);
150 
166  std::vector<GenTxid> GetRequestable(NodeId peer, std::chrono::microseconds now,
167  std::vector<std::pair<NodeId, GenTxid>>* expired = nullptr);
168 
177  void RequestedTx(NodeId peer, const uint256& txhash, std::chrono::microseconds expiry);
178 
185  void ReceivedResponse(NodeId peer, const uint256& txhash);
186 
187  // The operations below inspect the data structure.
188 
190  size_t CountInFlight(NodeId peer) const;
191 
193  size_t CountCandidates(NodeId peer) const;
194 
196  size_t Count(NodeId peer) const;
197 
199  size_t Size() const;
200 
203  void GetCandidatePeers(const uint256& txhash, std::vector<NodeId>& result_peers) const;
204 
206  uint64_t ComputePriority(const uint256& txhash, NodeId peer, bool preferred) const;
207 
209  void SanityCheck() const;
210 
215  void PostGetRequestableSanityCheck(std::chrono::microseconds now) const;
216 };
217 
218 #endif // BITCOIN_TXREQUEST_H
void ReceivedResponse(NodeId peer, const uint256 &txhash)
Converts a CANDIDATE or REQUESTED announcement to a COMPLETED one.
Definition: txrequest.cpp:743
size_t Count(NodeId peer) const
Count how many announcements a peer has (REQUESTED, CANDIDATE, and COMPLETED combined).
Definition: txrequest.cpp:722
void GetCandidatePeers(const uint256 &txhash, std::vector< NodeId > &result_peers) const
For some txhash (txid or wtxid), finds all peers with non-COMPLETED announcements and appends them to...
Definition: txrequest.cpp:724
Data structure to keep track of, and schedule, transaction downloads from peers.
Definition: txrequest.h:100
std::vector< GenTxid > GetRequestable(NodeId peer, std::chrono::microseconds now, std::vector< std::pair< NodeId, GenTxid >> *expired=nullptr)
Find the txids to request now from peer.
Definition: txrequest.cpp:748
void ReceivedInv(NodeId peer, const GenTxid &gtxid, bool preferred, std::chrono::microseconds reqtime)
Adds a new CANDIDATE announcement.
Definition: txrequest.cpp:732
size_t CountInFlight(NodeId peer) const
Count how many REQUESTED announcements a peer has.
Definition: txrequest.cpp:720
size_t Size() const
Count how many announcements are being tracked in total across all peers and transaction hashes...
Definition: txrequest.cpp:723
void DisconnectedPeer(NodeId peer)
Deletes all announcements for a given peer.
Definition: txrequest.cpp:719
void SanityCheck() const
Run internal consistency check (testing only).
Definition: txrequest.cpp:725
TxRequestTracker(bool deterministic=false)
Construct a TxRequestTracker.
Definition: txrequest.cpp:713
int64_t NodeId
Definition: net.h:103
void RequestedTx(NodeId peer, const uint256 &txhash, std::chrono::microseconds expiry)
Marks a transaction as requested, with a specified expiry.
Definition: txrequest.cpp:738
256-bit opaque blob.
Definition: uint256.h:195
void PostGetRequestableSanityCheck(std::chrono::microseconds now) const
Run a time-dependent internal consistency check (testing only).
Definition: txrequest.cpp:727
size_t CountCandidates(NodeId peer) const
Count how many CANDIDATE announcements a peer has.
Definition: txrequest.cpp:721
uint64_t ComputePriority(const uint256 &txhash, NodeId peer, bool preferred) const
Access to the internal priority computation (testing only)
Definition: txrequest.cpp:754
const std::unique_ptr< Impl > m_impl
Definition: txrequest.h:102
Actual implementation for TxRequestTracker&#39;s data structure.
Definition: txrequest.cpp:301
void ForgetTxHash(const uint256 &txhash)
Deletes all announcements for a given txhash (both txid and wtxid ones).
Definition: txrequest.cpp:718