Bitcoin Core  0.20.99
P2P Digital Currency
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
9 #include <net.h> // For NodeId
10 #include <uint256.h>
12 #include <chrono>
13 #include <vector>
15 #include <stdint.h>
97  // Avoid littering this header file with implementation details.
98  class Impl;
99  const std::unique_ptr<Impl> m_impl;
101 public:
103  explicit TxRequestTracker(bool deterministic = false);
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.
131  void ReceivedInv(NodeId peer, const GenTxid& gtxid, bool preferred,
132  std::chrono::microseconds reqtime);
138  void DisconnectedPeer(NodeId peer);
145  void ForgetTxHash(const uint256& txhash);
163  std::vector<GenTxid> GetRequestable(NodeId peer, std::chrono::microseconds now,
164  std::vector<std::pair<NodeId, GenTxid>>* expired = nullptr);
174  void RequestedTx(NodeId peer, const uint256& txhash, std::chrono::microseconds expiry);
182  void ReceivedResponse(NodeId peer, const uint256& txhash);
184  // The operations below inspect the data structure.
187  size_t CountInFlight(NodeId peer) const;
190  size_t CountCandidates(NodeId peer) const;
193  size_t Count(NodeId peer) const;
196  size_t Size() const;
199  uint64_t ComputePriority(const uint256& txhash, NodeId peer, bool preferred) const;
202  void SanityCheck() const;
208  void PostGetRequestableSanityCheck(std::chrono::microseconds now) const;
209 };
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