Bitcoin Core 28.99.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
15class CBlock;
17class CTxMemPool;
18class GenTxid;
20namespace node {
21class TxDownloadManagerImpl;
22
25static constexpr int32_t MAX_PEER_TX_REQUEST_IN_FLIGHT = 100;
30static constexpr int32_t MAX_PEER_TX_ANNOUNCEMENTS = 5000;
32static constexpr auto TXID_RELAY_DELAY{2s};
34static constexpr auto NONPREF_PEER_TX_DELAY{2s};
36static constexpr auto OVERLOADED_PEER_TX_DELAY{2s};
38static constexpr auto GETDATA_TX_INTERVAL{60s};
45 const uint32_t m_max_orphan_txs;
48};
51 const bool m_preferred;
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<uint256> m_unique_parents;
96 std::optional<PackageToValidate> m_package_to_validate;
97};
98
99
120 const std::unique_ptr<TxDownloadManagerImpl> m_impl;
121
122public:
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
141 bool AddTxAnnouncement(NodeId peer, const GenTxid& gtxid, std::chrono::microseconds now, bool p2p_inv);
142
144 std::vector<GenTxid> GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time);
145
147 void ReceivedNotFound(NodeId nodeid, const std::vector<uint256>& txhashes);
148
150 void MempoolAcceptedTx(const CTransactionRef& tx);
151
153 RejectedTxTodo MempoolRejectedTx(const CTransactionRef& ptx, const TxValidationState& state, NodeId nodeid, bool first_time_failure);
154
156 void MempoolRejectedPackage(const Package& package);
157
161 std::pair<bool, std::optional<PackageToValidate>> ReceivedTx(NodeId nodeid, const CTransactionRef& ptx);
162
164 bool HaveMoreWork(NodeId nodeid) const;
165
168
170 void CheckIsEmpty() const;
171
173 void CheckIsEmpty(NodeId nodeid) const;
174
176 std::vector<TxOrphanage::OrphanTxBase> GetOrphanTransactions() const;
177};
178} // namespace node
179#endif // BITCOIN_NODE_TXDOWNLOADMAN_H
#define Assume(val)
Assume is the identity function.
Definition: check.h:97
Definition: block.h:69
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set.
Definition: bloom.h:109
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:304
Fast randomness source.
Definition: random.h:377
A generic txid reference (txid or wtxid).
Definition: transaction.h:428
Data structure to keep track of, and schedule, transaction downloads from peers.
Definition: txrequest.h:96
Class responsible for deciding what transactions to request and, once downloaded, whether and how to ...
std::vector< TxOrphanage::OrphanTxBase > GetOrphanTransactions() const
Wrapper for TxOrphanage::GetOrphanTransactions.
const std::unique_ptr< TxDownloadManagerImpl > m_impl
std::vector< GenTxid > GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time)
Get getdata requests to send.
std::pair< bool, std::optional< PackageToValidate > > ReceivedTx(NodeId nodeid, const CTransactionRef &ptx)
Marks a tx as ReceivedResponse in txrequest and checks whether AlreadyHaveTx.
bool AddTxAnnouncement(NodeId peer, const GenTxid &gtxid, std::chrono::microseconds now, bool p2p_inv)
Consider adding this tx hash to txrequest.
void BlockConnected(const std::shared_ptr< const CBlock > &pblock)
CTransactionRef GetTxToReconsider(NodeId nodeid)
Returns next orphan tx to consider, or nullptr if none exist.
void CheckIsEmpty() const
Check that all data structures are empty.
void MempoolAcceptedTx(const CTransactionRef &tx)
Respond to successful transaction submission to mempool.
void ConnectedPeer(NodeId nodeid, const TxDownloadConnectionInfo &info)
Creates a new PeerInfo.
void MempoolRejectedPackage(const Package &package)
Respond to package rejected from mempool.
void DisconnectedPeer(NodeId nodeid)
Deletes all txrequest announcements and orphans for a given peer.
RejectedTxTodo MempoolRejectedTx(const CTransactionRef &ptx, const TxValidationState &state, NodeId nodeid, bool first_time_failure)
Respond to transaction rejected from mempool.
TxDownloadManager(const TxDownloadOptions &options)
void ReceivedNotFound(NodeId nodeid, const std::vector< uint256 > &txhashes)
Should be called when a notfound for a tx has been received.
bool HaveMoreWork(NodeId nodeid) const
Whether there are any orphans to reconsider for this peer.
Definition: messages.h:20
static constexpr auto GETDATA_TX_INTERVAL
How long to wait before downloading a transaction from an additional peer.
Definition: txdownloadman.h:38
static constexpr int32_t MAX_PEER_TX_ANNOUNCEMENTS
Maximum number of transactions to consider for requesting, per peer.
Definition: txdownloadman.h:30
static constexpr int32_t MAX_PEER_TX_REQUEST_IN_FLIGHT
Maximum number of in-flight transaction requests from a peer.
Definition: txdownloadman.h:25
static constexpr auto TXID_RELAY_DELAY
How long to delay requesting transactions via txids, if we have wtxid-relaying peers.
Definition: txdownloadman.h:32
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 auto NONPREF_PEER_TX_DELAY
How long to delay requesting transactions from non-preferred peers.
Definition: txdownloadman.h:34
int64_t NodeId
Definition: net.h:97
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
Definition: packages.h:50
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
std::vector< NodeId > m_senders
Definition: txdownloadman.h:59
PackageToValidate(const CTransactionRef &parent, const CTransactionRef &child, NodeId parent_sender, NodeId child_sender)
Construct a 1-parent-1-child package.
Definition: txdownloadman.h:61
std::string ToString() const
Definition: txdownloadman.h:81
PackageToValidate(PackageToValidate &&other)
Definition: txdownloadman.h:70
PackageToValidate & operator=(PackageToValidate &&other)
Definition: txdownloadman.h:75
PackageToValidate(const PackageToValidate &other)=default
std::vector< uint256 > m_unique_parents
Definition: txdownloadman.h:95
bool m_should_add_extra_compact_tx
Definition: txdownloadman.h:94
std::optional< PackageToValidate > m_package_to_validate
Definition: txdownloadman.h:96
const bool m_preferred
Whether this peer is preferred for transaction download.
Definition: txdownloadman.h:51
const bool m_wtxid_relay
Whether this peer supports wtxid relay.
Definition: txdownloadman.h:55
const bool m_relay_permissions
Whether this peer has Relay permissions.
Definition: txdownloadman.h:53
bool m_deterministic_txrequest
Instantiate TxRequestTracker as deterministic (used for tests).
Definition: txdownloadman.h:47
FastRandomContext & m_rng
RNG provided by caller.
Definition: txdownloadman.h:43
const uint32_t m_max_orphan_txs
Maximum number of transactions allowed in orphanage.
Definition: txdownloadman.h:45
const CTxMemPool & m_mempool
Read-only reference to mempool.
Definition: txdownloadman.h:41
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1165