Bitcoin Core  29.1.0
P2P Digital Currency
txdownloadman.h
Go to the documentation of this file.
1 // Copyright (c) 2024 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 <policy/packages.h>
10 #include <txorphanage.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};
45  const uint32_t m_max_orphan_txs;
48 };
51  const bool m_preferred;
53  const bool m_relay_permissions;
55  const bool m_wtxid_relay;
56 };
59  std::vector<NodeId> m_senders;
61  explicit PackageToValidate(const CTransactionRef& parent,
62  const CTransactionRef& child,
63  NodeId parent_sender,
64  NodeId child_sender) :
65  m_txns{parent, child},
66  m_senders{parent_sender, child_sender}
67  {}
68 
69  // Move ctor
70  PackageToValidate(PackageToValidate&& other) : m_txns{std::move(other.m_txns)}, m_senders{std::move(other.m_senders)} {}
71  // Copy ctor
72  PackageToValidate(const PackageToValidate& other) = default;
73 
74  // Move assignment
76  this->m_txns = std::move(other.m_txns);
77  this->m_senders = std::move(other.m_senders);
78  return *this;
79  }
80 
81  std::string ToString() const {
82  Assume(m_txns.size() == 2);
83  return strprintf("parent %s (wtxid=%s, sender=%d) + child %s (wtxid=%s, sender=%d)",
84  m_txns.front()->GetHash().ToString(),
85  m_txns.front()->GetWitnessHash().ToString(),
86  m_senders.front(),
87  m_txns.back()->GetHash().ToString(),
88  m_txns.back()->GetWitnessHash().ToString(),
89  m_senders.back());
90  }
91 };
93 {
95  std::vector<Txid> m_unique_parents;
96  std::optional<PackageToValidate> m_package_to_validate;
97 };
98 
99 
120  const std::unique_ptr<TxDownloadManagerImpl> m_impl;
121 
122 public:
123  explicit TxDownloadManager(const TxDownloadOptions& options);
125 
126  // Responses to chain events. TxDownloadManager is not an actual client of ValidationInterface, these are called through PeerManager.
127  void ActiveTipChange();
128  void BlockConnected(const std::shared_ptr<const CBlock>& pblock);
129  void BlockDisconnected();
130 
132  void ConnectedPeer(NodeId nodeid, const TxDownloadConnectionInfo& info);
133 
135  void DisconnectedPeer(NodeId nodeid);
136 
140  bool AddTxAnnouncement(NodeId peer, const GenTxid& gtxid, std::chrono::microseconds now);
141 
143  std::vector<GenTxid> GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time);
144 
146  void ReceivedNotFound(NodeId nodeid, const std::vector<uint256>& txhashes);
147 
149  void MempoolAcceptedTx(const CTransactionRef& tx);
150 
152  RejectedTxTodo MempoolRejectedTx(const CTransactionRef& ptx, const TxValidationState& state, NodeId nodeid, bool first_time_failure);
153 
155  void MempoolRejectedPackage(const Package& package);
156 
160  std::pair<bool, std::optional<PackageToValidate>> ReceivedTx(NodeId nodeid, const CTransactionRef& ptx);
161 
163  bool HaveMoreWork(NodeId nodeid) const;
164 
167 
169  void CheckIsEmpty() const;
170 
172  void CheckIsEmpty(NodeId nodeid) const;
173 
175  std::vector<TxOrphanage::OrphanTxBase> GetOrphanTransactions() const;
176 };
177 } // namespace node
178 #endif // BITCOIN_NODE_TXDOWNLOADMAN_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
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:95
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:96
Definition: block.h:68
Data structure to keep track of, and schedule, transaction downloads from peers.
Definition: txrequest.h:96
#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:53
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
Definition: packages.h:50
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.
void ReceivedNotFound(NodeId nodeid, const std::vector< uint256 > &txhashes)
Should be called when a notfound for a tx has been received.
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:55
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:51
std::vector< TxOrphanage::OrphanTxBase > GetOrphanTransactions() const
Wrapper for TxOrphanage::GetOrphanTransactions.
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:376
int64_t NodeId
Definition: net.h:97
bool m_deterministic_txrequest
Instantiate TxRequestTracker as deterministic (used for tests).
Definition: txdownloadman.h:47
bool m_should_add_extra_compact_tx
Definition: txdownloadman.h:94
const uint32_t m_max_orphan_txs
Maximum number of transactions allowed in orphanage.
Definition: txdownloadman.h:45
#define Assume(val)
Assume is the identity function.
Definition: check.h:97
Definition: messages.h:20
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:303
PackageToValidate(PackageToValidate &&other)
Definition: txdownloadman.h:70
std::vector< NodeId > m_senders
Definition: txdownloadman.h:59
PackageToValidate & operator=(PackageToValidate &&other)
Definition: txdownloadman.h:75
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:81
A generic txid reference (txid or wtxid).
Definition: transaction.h:427
PackageToValidate(const CTransactionRef &parent, const CTransactionRef &child, NodeId parent_sender, NodeId child_sender)
Construct a 1-parent-1-child package.
Definition: txdownloadman.h:61