Branch data Line data Source code
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 : : #include <util/not_null.h>
12 : :
13 : : #include <cstdint>
14 : : #include <memory>
15 : :
16 : : class CBlock;
17 : : class CRollingBloomFilter;
18 : : class CTxMemPool;
19 : : class GenTxid;
20 : : class TxRequestTracker;
21 : : namespace node {
22 : : class TxDownloadManagerImpl;
23 : :
24 : : /** Maximum number of in-flight transaction requests from a peer. It is not a hard limit, but the threshold at which
25 : : * point the OVERLOADED_PEER_TX_DELAY kicks in. */
26 : : inline constexpr int32_t MAX_PEER_TX_REQUEST_IN_FLIGHT = 100;
27 : : /** Maximum number of transactions to consider for requesting, per peer. It provides a reasonable DoS limit to
28 : : * per-peer memory usage spent on announcements, while covering peers continuously sending INVs at the maximum
29 : : * rate (by our own policy, see DEFAULT_TX_SEND_RATE) for several minutes, while not receiving
30 : : * the actual transaction (from any peer) in response to requests for them. */
31 : : inline constexpr int32_t MAX_PEER_TX_ANNOUNCEMENTS = 5000;
32 : : /** How long to delay requesting transactions via txids, if we have wtxid-relaying peers */
33 : : inline constexpr auto TXID_RELAY_DELAY{2s};
34 : : /** How long to delay requesting transactions from non-preferred peers */
35 : : inline constexpr auto NONPREF_PEER_TX_DELAY{2s};
36 : : /** How long to delay requesting transactions from overloaded peers (see MAX_PEER_TX_REQUEST_IN_FLIGHT). */
37 : : inline constexpr auto OVERLOADED_PEER_TX_DELAY{2s};
38 : : /** How long to wait before downloading a transaction from an additional peer */
39 : : inline constexpr auto GETDATA_TX_INTERVAL{60s};
40 : : struct TxDownloadOptions {
41 : : /** Read-only reference to mempool. */
42 : : const CTxMemPool& m_mempool;
43 : : /** Instantiate TxRequestTracker as deterministic (used for tests). */
44 : : bool m_deterministic_txrequest{false};
45 : : };
46 : : struct TxDownloadConnectionInfo {
47 : : /** Whether this peer is preferred for transaction download. */
48 : : const bool m_preferred;
49 : : /** Whether this peer has Relay permissions. */
50 : : const bool m_relay_permissions;
51 : : /** Whether this peer supports wtxid relay. */
52 : : const bool m_wtxid_relay;
53 : : };
54 : 447 : struct PackageToValidate {
55 : : Package m_txns;
56 : : std::vector<NodeId> m_senders;
57 : : /** Construct a 1-parent-1-child package. */
58 : 134 : explicit PackageToValidate(const CTransactionRef& parent,
59 : : const CTransactionRef& child,
60 : : NodeId parent_sender,
61 : 134 : NodeId child_sender) :
62 [ + + + - : 402 : m_txns{parent, child},
- - - - ]
63 [ + - ]: 134 : m_senders{parent_sender, child_sender}
64 [ + - + - : 536 : {}
+ - ]
65 : :
66 : : // Move ctor
67 : 294 : PackageToValidate(PackageToValidate&& other) : m_txns{std::move(other.m_txns)}, m_senders{std::move(other.m_senders)} {}
68 : : // Copy ctor
69 [ + - ]: 19 : PackageToValidate(const PackageToValidate& other) = default;
70 : :
71 : : // Move assignment
72 : 0 : PackageToValidate& operator=(PackageToValidate&& other) {
73 : 0 : this->m_txns = std::move(other.m_txns);
74 : 0 : this->m_senders = std::move(other.m_senders);
75 : 0 : return *this;
76 : : }
77 : :
78 : 0 : std::string ToString() const {
79 [ # # ]: 0 : Assume(m_txns.size() == 2);
80 : 0 : return strprintf("parent %s (wtxid=%s, sender=%d) + child %s (wtxid=%s, sender=%d)",
81 : 0 : m_txns.front()->GetHash().ToString(),
82 [ # # # # ]: 0 : m_txns.front()->GetWitnessHash().ToString(),
83 : 0 : m_senders.front(),
84 [ # # ]: 0 : m_txns.back()->GetHash().ToString(),
85 [ # # ]: 0 : m_txns.back()->GetWitnessHash().ToString(),
86 [ # # ]: 0 : m_senders.back());
87 : : }
88 : : };
89 : : struct RejectedTxTodo
90 : : {
91 : : bool m_should_add_extra_compact_tx;
92 : : std::vector<Txid> m_unique_parents;
93 : : std::optional<PackageToValidate> m_package_to_validate;
94 : : };
95 : :
96 : :
97 : : /**
98 : : * Class responsible for deciding what transactions to request and, once
99 : : * downloaded, whether and how to validate them. It is also responsible for
100 : : * deciding what transaction packages to validate and how to resolve orphan
101 : : * transactions. Its data structures include TxRequestTracker for scheduling
102 : : * requests, rolling bloom filters for remembering transactions that have
103 : : * already been {accepted, rejected, confirmed}, an orphanage, and a registry of
104 : : * each peer's transaction relay-related information.
105 : : *
106 : : * Caller needs to interact with TxDownloadManager:
107 : : * - ValidationInterface callbacks.
108 : : * - When a potential transaction relay peer connects or disconnects.
109 : : * - When a transaction or package is accepted or rejected from mempool
110 : : * - When a inv, notfound, or tx message is received
111 : : * - To get instructions for which getdata messages to send
112 : : *
113 : : * This class is not thread-safe. Access must be synchronized using an
114 : : * external mutex.
115 : : */
116 : : class TxDownloadManager {
117 : : const util::NotNullUniquePtr<TxDownloadManagerImpl> m_impl;
118 : :
119 : : public:
120 : : explicit TxDownloadManager(const TxDownloadOptions& options);
121 : : ~TxDownloadManager();
122 : :
123 : : // Responses to chain events. TxDownloadManager is not an actual client of ValidationInterface, these are called through PeerManager.
124 : : void ActiveTipChange();
125 : : void BlockConnected(const std::shared_ptr<const CBlock>& pblock);
126 : : void BlockDisconnected();
127 : :
128 : : /** Creates a new PeerInfo. Saves the connection info to calculate tx announcement delays later. */
129 : : void ConnectedPeer(NodeId nodeid, const TxDownloadConnectionInfo& info);
130 : :
131 : : /** Deletes all txrequest announcements and orphans for a given peer. */
132 : : void DisconnectedPeer(NodeId nodeid);
133 : :
134 : : /** Consider adding this tx hash to txrequest. Should be called whenever a new inv has been received.
135 : : * Also called internally when a transaction is missing parents so that we can request them.
136 : : * Returns true if this was a dropped inv (p2p_inv=true and we already have the tx), false otherwise. */
137 : : bool AddTxAnnouncement(NodeId peer, const GenTxid& gtxid, std::chrono::microseconds now);
138 : :
139 : : /** Get getdata requests to send. */
140 : : std::vector<GenTxid> GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time);
141 : :
142 : : /** Should be called when a notfound for a tx has been received. */
143 : : void ReceivedNotFound(NodeId nodeid, const std::vector<GenTxid>& gtxids);
144 : :
145 : : /** Respond to successful transaction submission to mempool */
146 : : void MempoolAcceptedTx(const CTransactionRef& tx);
147 : :
148 : : /** Respond to transaction rejected from mempool */
149 : : RejectedTxTodo MempoolRejectedTx(const CTransactionRef& ptx, const TxValidationState& state, NodeId nodeid, bool first_time_failure);
150 : :
151 : : /** Respond to package rejected from mempool */
152 : : void MempoolRejectedPackage(const Package& package);
153 : :
154 : : /** Marks a tx as ReceivedResponse in txrequest and checks whether AlreadyHaveTx.
155 : : * Return a bool indicating whether this tx should be validated. If false, optionally, a
156 : : * PackageToValidate. */
157 : : std::pair<bool, std::optional<PackageToValidate>> ReceivedTx(NodeId nodeid, const CTransactionRef& ptx);
158 : :
159 : : /** Whether there are any orphans to reconsider for this peer. */
160 : : bool HaveMoreWork(NodeId nodeid) const;
161 : :
162 : : /** Returns next orphan tx to consider, or nullptr if none exist. */
163 : : CTransactionRef GetTxToReconsider(NodeId nodeid);
164 : :
165 : : /** Check that all data structures are empty. */
166 : : void CheckIsEmpty() const;
167 : :
168 : : /** Check that all data structures that track per-peer information have nothing for this peer. */
169 : : void CheckIsEmpty(NodeId nodeid) const;
170 : :
171 : : /** Wrapper for TxOrphanage::GetOrphanTransactions */
172 : : std::vector<TxOrphanage::OrphanInfo> GetOrphanTransactions() const;
173 : : };
174 : : } // namespace node
175 : : #endif // BITCOIN_NODE_TXDOWNLOADMAN_H
|