Bitcoin Core 31.99.0
P2P Digital Currency
txdownloadman_impl.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#ifndef BITCOIN_NODE_TXDOWNLOADMAN_IMPL_H
5#define BITCOIN_NODE_TXDOWNLOADMAN_IMPL_H
6
8
9#include <common/bloom.h>
11#include <kernel/chain.h>
12#include <net.h>
13#include <node/txorphanage.h>
15#include <policy/packages.h>
16#include <random.h>
17#include <txrequest.h>
18
19class CTxMemPool;
20namespace node {
22public:
25
27 std::unique_ptr<TxOrphanage> m_orphanage;
30
65 std::unique_ptr<CRollingBloomFilter> m_lazy_recent_rejects{nullptr};
66
68 {
70 m_lazy_recent_rejects = std::make_unique<CRollingBloomFilter>(120'000, 0.000'001);
71 }
72
74 }
75
96 std::unique_ptr<CRollingBloomFilter> m_lazy_recent_rejects_reconsiderable{nullptr};
97
99 {
101 m_lazy_recent_rejects_reconsiderable = std::make_unique<CRollingBloomFilter>(120'000, 0.000'001);
102 }
103
105 }
106
107 /*
108 * Filter for transactions that have been recently confirmed.
109 * We use this to avoid requesting transactions that have already been
110 * confirmed.
111 *
112 * Blocks don't typically have more than 4000 transactions, so this should
113 * be at least six blocks (~1 hr) worth of transactions that we can store,
114 * inserting both a txid and wtxid for every observed transaction.
115 * If the number of transactions appearing in a block goes up, or if we are
116 * seeing getdata requests more than an hour after initial announcement, we
117 * can increase this number.
118 * The false positive rate of 1/1M should come out to less than 1
119 * transaction per day that would be inadvertently ignored (which is the
120 * same probability that we have in the reject filter).
121 */
122 std::unique_ptr<CRollingBloomFilter> m_lazy_recent_confirmed_transactions{nullptr};
123
125 {
127 m_lazy_recent_confirmed_transactions = std::make_unique<CRollingBloomFilter>(48'000, 0.000'001);
128 }
129
131 }
132
134 : m_mempool{options.m_mempool},
135 m_rng{options.m_deterministic_txrequest},
137 m_txrequest{options.m_deterministic_txrequest}
138 {}
139
140 struct PeerInfo {
143
145 };
146
149 std::map<NodeId, PeerInfo> m_peer_info;
150
152 uint32_t m_num_wtxid_peers{0};
153
154 void ActiveTipChange();
155 void BlockConnected(const std::shared_ptr<const CBlock>& pblock);
156 void BlockDisconnected();
157
165 bool AlreadyHaveTx(const GenTxid& gtxid, bool include_reconsiderable);
166
167 void ConnectedPeer(NodeId nodeid, const TxDownloadConnectionInfo& info);
168 void DisconnectedPeer(NodeId nodeid);
169
173 bool AddTxAnnouncement(NodeId peer, const GenTxid& gtxid, std::chrono::microseconds now);
174
176 std::vector<GenTxid> GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time);
177
179 void ReceivedNotFound(NodeId nodeid, const std::vector<GenTxid>& gtxids);
180
184 std::optional<PackageToValidate> Find1P1CPackage(const CTransactionRef& ptx, NodeId nodeid);
185
186 void MempoolAcceptedTx(const CTransactionRef& tx);
187 RejectedTxTodo MempoolRejectedTx(const CTransactionRef& ptx, const TxValidationState& state, NodeId nodeid, bool first_time_failure);
188 void MempoolRejectedPackage(const Package& package);
189
190 std::pair<bool, std::optional<PackageToValidate>> ReceivedTx(NodeId nodeid, const CTransactionRef& ptx);
191
192 bool HaveMoreWork(NodeId nodeid);
194
195 void CheckIsEmpty();
196 void CheckIsEmpty(NodeId nodeid);
197
198 std::vector<TxOrphanage::OrphanInfo> GetOrphanTransactions() const;
199
200protected:
202 std::vector<Txid> GetUniqueParents(const CTransaction& tx);
203
208 bool MaybeAddOrphanResolutionCandidate(const std::vector<Txid>& unique_parents, const Wtxid& wtxid, NodeId nodeid, std::chrono::microseconds now);
209};
210} // namespace node
211#endif // BITCOIN_NODE_TXDOWNLOADMAN_IMPL_H
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set.
Definition: bloom.h:110
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:281
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:187
Fast randomness source.
Definition: random.h:386
Data structure to keep track of, and schedule, transaction downloads from peers.
Definition: txrequest.h:100
std::unique_ptr< CRollingBloomFilter > m_lazy_recent_confirmed_transactions
std::optional< PackageToValidate > Find1P1CPackage(const CTransactionRef &ptx, NodeId nodeid)
Look for a child of this transaction in the orphanage to form a 1-parent-1-child package,...
bool AddTxAnnouncement(NodeId peer, const GenTxid &gtxid, std::chrono::microseconds now)
Consider adding this tx hash to txrequest.
CRollingBloomFilter & RecentConfirmedTransactionsFilter()
std::unique_ptr< TxOrphanage > m_orphanage
Manages unvalidated tx data (orphan transactions for which we are downloading ancestors).
std::unique_ptr< CRollingBloomFilter > m_lazy_recent_rejects
Filter for transactions that were recently rejected by the mempool.
void DisconnectedPeer(NodeId nodeid)
bool MaybeAddOrphanResolutionCandidate(const std::vector< Txid > &unique_parents, const Wtxid &wtxid, NodeId nodeid, std::chrono::microseconds now)
If this peer is an orphan resolution candidate for this transaction, treat the unique_parents as anno...
void ReceivedNotFound(NodeId nodeid, const std::vector< GenTxid > &gtxids)
Marks a tx as ReceivedResponse in txrequest.
std::unique_ptr< CRollingBloomFilter > m_lazy_recent_rejects_reconsiderable
Filter for: (1) wtxids of transactions that were recently rejected by the mempool but are eligible fo...
TxRequestTracker m_txrequest
Tracks candidates for requesting and downloading transaction data.
std::pair< bool, std::optional< PackageToValidate > > ReceivedTx(NodeId nodeid, const CTransactionRef &ptx)
void MempoolAcceptedTx(const CTransactionRef &tx)
CRollingBloomFilter & RecentRejectsReconsiderableFilter()
RejectedTxTodo MempoolRejectedTx(const CTransactionRef &ptx, const TxValidationState &state, NodeId nodeid, bool first_time_failure)
uint32_t m_num_wtxid_peers
Number of wtxid relay peers we have in m_peer_info.
void ConnectedPeer(NodeId nodeid, const TxDownloadConnectionInfo &info)
bool AlreadyHaveTx(const GenTxid &gtxid, bool include_reconsiderable)
Check whether we already have this gtxid in:
std::map< NodeId, PeerInfo > m_peer_info
Information for all of the peers we may download transactions from.
std::vector< TxOrphanage::OrphanInfo > GetOrphanTransactions() const
TxDownloadManagerImpl(const TxDownloadOptions &options)
std::vector< GenTxid > GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time)
Get getdata requests to send.
std::vector< Txid > GetUniqueParents(const CTransaction &tx)
Helper for getting deduplicated vector of Txids in vin.
void BlockConnected(const std::shared_ptr< const CBlock > &pblock)
CTransactionRef GetTxToReconsider(NodeId nodeid)
CRollingBloomFilter & RecentRejectsFilter()
void MempoolRejectedPackage(const Package &package)
Definition: messages.h:21
std::unique_ptr< TxOrphanage > MakeTxOrphanage() noexcept
Create a new TxOrphanage instance.
int64_t NodeId
Definition: net.h:105
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
Definition: packages.h:45
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
PeerInfo(const TxDownloadConnectionInfo &info)
const TxDownloadConnectionInfo m_connection_info
Information relevant to scheduling tx requests.