Bitcoin Core  31.0.0
P2P Digital Currency
txdownloadman.h
Go to the documentation of this file.
1 // Copyright (c) 2024-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_NODE_TXDOWNLOADMAN_H
6 #define BITCOIN_NODE_TXDOWNLOADMAN_H
7 
8 #include <net.h>
9 #include <node/txorphanage.h>
10 #include <policy/packages.h>
11 
12 #include <cstdint>
13 #include <memory>
14 
15 class CBlock;
17 class CTxMemPool;
18 class GenTxid;
19 class TxRequestTracker;
20 namespace node {
21 class TxDownloadManagerImpl;
22 
25 static constexpr int32_t MAX_PEER_TX_REQUEST_IN_FLIGHT = 100;
30 static constexpr int32_t MAX_PEER_TX_ANNOUNCEMENTS = 5000;
32 static constexpr auto TXID_RELAY_DELAY{2s};
34 static constexpr auto NONPREF_PEER_TX_DELAY{2s};
36 static constexpr auto OVERLOADED_PEER_TX_DELAY{2s};
38 static constexpr auto GETDATA_TX_INTERVAL{60s};
46 };
49  const bool m_preferred;
51  const bool m_relay_permissions;
53  const bool m_wtxid_relay;
54 };
57  std::vector<NodeId> m_senders;
59  explicit PackageToValidate(const CTransactionRef& parent,
60  const CTransactionRef& child,
61  NodeId parent_sender,
62  NodeId child_sender) :
63  m_txns{parent, child},
64  m_senders{parent_sender, child_sender}
65  {}
66 
67  // Move ctor
68  PackageToValidate(PackageToValidate&& other) : m_txns{std::move(other.m_txns)}, m_senders{std::move(other.m_senders)} {}
69  // Copy ctor
70  PackageToValidate(const PackageToValidate& other) = default;
71 
72  // Move assignment
74  this->m_txns = std::move(other.m_txns);
75  this->m_senders = std::move(other.m_senders);
76  return *this;
77  }
78 
79  std::string ToString() const {
80  Assume(m_txns.size() == 2);
81  return strprintf("parent %s (wtxid=%s, sender=%d) + child %s (wtxid=%s, sender=%d)",
82  m_txns.front()->GetHash().ToString(),
83  m_txns.front()->GetWitnessHash().ToString(),
84  m_senders.front(),
85  m_txns.back()->GetHash().ToString(),
86  m_txns.back()->GetWitnessHash().ToString(),
87  m_senders.back());
88  }
89 };
91 {
93  std::vector<Txid> m_unique_parents;
94  std::optional<PackageToValidate> m_package_to_validate;
95 };
96 
97 
118  const std::unique_ptr<TxDownloadManagerImpl> m_impl;
119 
120 public:
121  explicit TxDownloadManager(const TxDownloadOptions& options);
123 
124  // Responses to chain events. TxDownloadManager is not an actual client of ValidationInterface, these are called through PeerManager.
125  void ActiveTipChange();
126  void BlockConnected(const std::shared_ptr<const CBlock>& pblock);
127  void BlockDisconnected();
128 
130  void ConnectedPeer(NodeId nodeid, const TxDownloadConnectionInfo& info);
131 
133  void DisconnectedPeer(NodeId nodeid);
134 
138  bool AddTxAnnouncement(NodeId peer, const GenTxid& gtxid, std::chrono::microseconds now);
139 
141  std::vector<GenTxid> GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time);
142 
144  void ReceivedNotFound(NodeId nodeid, const std::vector<GenTxid>& gtxids);
145 
147  void MempoolAcceptedTx(const CTransactionRef& tx);
148 
150  RejectedTxTodo MempoolRejectedTx(const CTransactionRef& ptx, const TxValidationState& state, NodeId nodeid, bool first_time_failure);
151 
153  void MempoolRejectedPackage(const Package& package);
154 
158  std::pair<bool, std::optional<PackageToValidate>> ReceivedTx(NodeId nodeid, const CTransactionRef& ptx);
159 
161  bool HaveMoreWork(NodeId nodeid) const;
162 
165 
167  void CheckIsEmpty() const;
168 
170  void CheckIsEmpty(NodeId nodeid) const;
171 
173  std::vector<TxOrphanage::OrphanInfo> GetOrphanTransactions() const;
174 };
175 } // namespace node
176 #endif // BITCOIN_NODE_TXDOWNLOADMAN_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
static constexpr auto OVERLOADED_PEER_TX_DELAY
How long to delay requesting transactions from overloaded peers (see MAX_PEER_TX_REQUEST_IN_FLIGHT).
Definition: txdownloadman.h:36
static constexpr int32_t MAX_PEER_TX_REQUEST_IN_FLIGHT
Maximum number of in-flight transaction requests from a peer.
Definition: txdownloadman.h:25
std::vector< Txid > m_unique_parents
Definition: txdownloadman.h:93
bool AddTxAnnouncement(NodeId peer, const GenTxid &gtxid, std::chrono::microseconds now)
Consider adding this tx hash to txrequest.
static constexpr auto TXID_RELAY_DELAY
How long to delay requesting transactions via txids, if we have wtxid-relaying peers.
Definition: txdownloadman.h:32
void ConnectedPeer(NodeId nodeid, const TxDownloadConnectionInfo &info)
Creates a new PeerInfo.
std::optional< PackageToValidate > m_package_to_validate
Definition: txdownloadman.h:94
Definition: block.h:73
Data structure to keep track of, and schedule, transaction downloads from peers.
Definition: txrequest.h:100
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
CTransactionRef GetTxToReconsider(NodeId nodeid)
Returns next orphan tx to consider, or nullptr if none exist.
const bool m_relay_permissions
Whether this peer has Relay permissions.
Definition: txdownloadman.h:51
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
Definition: packages.h:45
void MempoolAcceptedTx(const CTransactionRef &tx)
Respond to successful transaction submission to mempool.
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set. ...
Definition: bloom.h:108
const CTxMemPool & m_mempool
Read-only reference to mempool.
Definition: txdownloadman.h:41
bool HaveMoreWork(NodeId nodeid) const
Whether there are any orphans to reconsider for this peer.
std::pair< bool, std::optional< PackageToValidate > > ReceivedTx(NodeId nodeid, const CTransactionRef &ptx)
Marks a tx as ReceivedResponse in txrequest and checks whether AlreadyHaveTx.
RejectedTxTodo MempoolRejectedTx(const CTransactionRef &ptx, const TxValidationState &state, NodeId nodeid, bool first_time_failure)
Respond to transaction rejected from mempool.
FastRandomContext & m_rng
RNG provided by caller.
Definition: txdownloadman.h:43
const bool m_wtxid_relay
Whether this peer supports wtxid relay.
Definition: txdownloadman.h:53
const std::unique_ptr< TxDownloadManagerImpl > m_impl
static constexpr int32_t MAX_PEER_TX_ANNOUNCEMENTS
Maximum number of transactions to consider for requesting, per peer.
Definition: txdownloadman.h:30
Class responsible for deciding what transactions to request and, once downloaded, whether and how to ...
const bool m_preferred
Whether this peer is preferred for transaction download.
Definition: txdownloadman.h:49
static constexpr auto GETDATA_TX_INTERVAL
How long to wait before downloading a transaction from an additional peer.
Definition: txdownloadman.h:38
TxDownloadManager(const TxDownloadOptions &options)
Fast randomness source.
Definition: random.h:385
int64_t NodeId
Definition: net.h:103
bool m_deterministic_txrequest
Instantiate TxRequestTracker as deterministic (used for tests).
Definition: txdownloadman.h:45
bool m_should_add_extra_compact_tx
Definition: txdownloadman.h:92
#define Assume(val)
Assume is the identity function.
Definition: check.h:125
Definition: messages.h:21
std::vector< GenTxid > GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time)
Get getdata requests to send.
void DisconnectedPeer(NodeId nodeid)
Deletes all txrequest announcements and orphans for a given peer.
static constexpr auto NONPREF_PEER_TX_DELAY
How long to delay requesting transactions from non-preferred peers.
Definition: txdownloadman.h:34
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:186
PackageToValidate(PackageToValidate &&other)
Definition: txdownloadman.h:68
std::vector< NodeId > m_senders
Definition: txdownloadman.h:57
PackageToValidate & operator=(PackageToValidate &&other)
Definition: txdownloadman.h:73
void ReceivedNotFound(NodeId nodeid, const std::vector< GenTxid > &gtxids)
Should be called when a notfound for a tx has been received.
void CheckIsEmpty() const
Check that all data structures are empty.
void BlockConnected(const std::shared_ptr< const CBlock > &pblock)
void MempoolRejectedPackage(const Package &package)
Respond to package rejected from mempool.
std::string ToString() const
Definition: txdownloadman.h:79
std::vector< TxOrphanage::OrphanInfo > GetOrphanTransactions() const
Wrapper for TxOrphanage::GetOrphanTransactions.
PackageToValidate(const CTransactionRef &parent, const CTransactionRef &child, NodeId parent_sender, NodeId child_sender)
Construct a 1-parent-1-child package.
Definition: txdownloadman.h:59