Bitcoin Core  0.20.99
P2P Digital Currency
txrequest.h
Go to the documentation of this file.
1 // Copyright (c) 2020 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> // For NodeId
10 #include <uint256.h>
11 
12 #include <chrono>
13 #include <vector>
14 
15 #include <stdint.h>
16 
97  // Avoid littering this header file with implementation details.
98  class Impl;
99  const std::unique_ptr<Impl> m_impl;
100 
101 public:
103  explicit TxRequestTracker(bool deterministic = false);
105 
106  // Conceptually, the data structure consists of a collection of "announcements", one for each peer/txhash
107  // combination:
108  //
109  // - CANDIDATE announcements represent transactions that were announced by a peer, and that become available for
110  // download after their reqtime has passed.
111  //
112  // - REQUESTED announcements represent transactions that have been requested, and which we're awaiting a
113  // response for from that peer. Their expiry value determines when the request times out.
114  //
115  // - COMPLETED announcements represent transactions that have been requested from a peer, and a NOTFOUND or a
116  // transaction was received in response (valid or not), or they timed out. They're only kept around to
117  // prevent requesting them again. If only COMPLETED announcements for a given txhash remain (so no CANDIDATE
118  // or REQUESTED ones), all of them are deleted (this is an invariant, and maintained by all operations below).
119  //
120  // The operations below manipulate the data structure.
121 
131  void ReceivedInv(NodeId peer, const GenTxid& gtxid, bool preferred,
132  std::chrono::microseconds reqtime);
133 
138  void DisconnectedPeer(NodeId peer);
139 
145  void ForgetTxHash(const uint256& txhash);
146 
163  std::vector<GenTxid> GetRequestable(NodeId peer, std::chrono::microseconds now,
164  std::vector<std::pair<NodeId, GenTxid>>* expired = nullptr);
165 
174  void RequestedTx(NodeId peer, const uint256& txhash, std::chrono::microseconds expiry);
175 
182  void ReceivedResponse(NodeId peer, const uint256& txhash);
183 
184  // The operations below inspect the data structure.
185 
187  size_t CountInFlight(NodeId peer) const;
188 
190  size_t CountCandidates(NodeId peer) const;
191 
193  size_t Count(NodeId peer) const;
194 
196  size_t Size() const;
197 
199  uint64_t ComputePriority(const uint256& txhash, NodeId peer, bool preferred) const;
200 
202  void SanityCheck() const;
203 
208  void PostGetRequestableSanityCheck(std::chrono::microseconds now) const;
209 };
210 
211 #endif // BITCOIN_TXREQUEST_H
void ReceivedResponse(NodeId peer, const uint256 &txhash)
Converts a CANDIDATE or REQUESTED announcement to a COMPLETED one.
Definition: txrequest.cpp:742
const std::chrono::seconds now
size_t Count(NodeId peer) const
Count how many announcements a peer has (REQUESTED, CANDIDATE, and COMPLETED combined).
Definition: txrequest.cpp:722
Data structure to keep track of, and schedule, transaction downloads from peers.
Definition: txrequest.h:96
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:747
void ReceivedInv(NodeId peer, const GenTxid &gtxid, bool preferred, std::chrono::microseconds reqtime)
Adds a new CANDIDATE announcement.
Definition: txrequest.cpp:731
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:724
TxRequestTracker(bool deterministic=false)
Construct a TxRequestTracker.
Definition: txrequest.cpp:713
int64_t NodeId
Definition: net.h:92
void RequestedTx(NodeId peer, const uint256 &txhash, std::chrono::microseconds expiry)
Marks a transaction as requested, with a specified expiry.
Definition: txrequest.cpp:737
256-bit opaque blob.
Definition: uint256.h:124
void PostGetRequestableSanityCheck(std::chrono::microseconds now) const
Run a time-dependent internal consistency check (testing only).
Definition: txrequest.cpp:726
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:753
const std::unique_ptr< Impl > m_impl
Definition: txrequest.h:98
A generic txid reference (txid or wtxid).
Definition: transaction.h:400
Actual implementation for TxRequestTracker&#39;s data structure.
Definition: txrequest.cpp:310
void ForgetTxHash(const uint256 &txhash)
Deletes all announcements for a given txhash (both txid and wtxid ones).
Definition: txrequest.cpp:718