Bitcoin Core  29.1.0
P2P Digital Currency
txdownloadman_impl.h
Go to the documentation of this file.
1 // Copyright (c) 2024
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 
7 #include <node/txdownloadman.h>
8 
9 #include <common/bloom.h>
10 #include <consensus/validation.h>
11 #include <kernel/chain.h>
12 #include <net.h>
13 #include <primitives/transaction.h>
14 #include <policy/packages.h>
15 #include <txorphanage.h>
16 #include <txrequest.h>
17 
18 class CTxMemPool;
19 namespace node {
21 public:
23 
28 
63  std::unique_ptr<CRollingBloomFilter> m_lazy_recent_rejects{nullptr};
64 
66  {
67  if (!m_lazy_recent_rejects) {
68  m_lazy_recent_rejects = std::make_unique<CRollingBloomFilter>(120'000, 0.000'001);
69  }
70 
71  return *m_lazy_recent_rejects;
72  }
73 
94  std::unique_ptr<CRollingBloomFilter> m_lazy_recent_rejects_reconsiderable{nullptr};
95 
97  {
99  m_lazy_recent_rejects_reconsiderable = std::make_unique<CRollingBloomFilter>(120'000, 0.000'001);
100  }
101 
103  }
104 
105  /*
106  * Filter for transactions that have been recently confirmed.
107  * We use this to avoid requesting transactions that have already been
108  * confirmed.
109  *
110  * Blocks don't typically have more than 4000 transactions, so this should
111  * be at least six blocks (~1 hr) worth of transactions that we can store,
112  * inserting both a txid and wtxid for every observed transaction.
113  * If the number of transactions appearing in a block goes up, or if we are
114  * seeing getdata requests more than an hour after initial announcement, we
115  * can increase this number.
116  * The false positive rate of 1/1M should come out to less than 1
117  * transaction per day that would be inadvertently ignored (which is the
118  * same probability that we have in the reject filter).
119  */
120  std::unique_ptr<CRollingBloomFilter> m_lazy_recent_confirmed_transactions{nullptr};
121 
123  {
125  m_lazy_recent_confirmed_transactions = std::make_unique<CRollingBloomFilter>(48'000, 0.000'001);
126  }
127 
129  }
130 
131  TxDownloadManagerImpl(const TxDownloadOptions& options) : m_opts{options}, m_txrequest{options.m_deterministic_txrequest} {}
132 
133  struct PeerInfo {
136 
138  };
139 
142  std::map<NodeId, PeerInfo> m_peer_info;
143 
145  uint32_t m_num_wtxid_peers{0};
146 
147  void ActiveTipChange();
148  void BlockConnected(const std::shared_ptr<const CBlock>& pblock);
149  void BlockDisconnected();
150 
158  bool AlreadyHaveTx(const GenTxid& gtxid, bool include_reconsiderable);
159 
160  void ConnectedPeer(NodeId nodeid, const TxDownloadConnectionInfo& info);
161  void DisconnectedPeer(NodeId nodeid);
162 
166  bool AddTxAnnouncement(NodeId peer, const GenTxid& gtxid, std::chrono::microseconds now);
167 
169  std::vector<GenTxid> GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time);
170 
172  void ReceivedNotFound(NodeId nodeid, const std::vector<uint256>& txhashes);
173 
177  std::optional<PackageToValidate> Find1P1CPackage(const CTransactionRef& ptx, NodeId nodeid);
178 
179  void MempoolAcceptedTx(const CTransactionRef& tx);
180  RejectedTxTodo MempoolRejectedTx(const CTransactionRef& ptx, const TxValidationState& state, NodeId nodeid, bool first_time_failure);
181  void MempoolRejectedPackage(const Package& package);
182 
183  std::pair<bool, std::optional<PackageToValidate>> ReceivedTx(NodeId nodeid, const CTransactionRef& ptx);
184 
185  bool HaveMoreWork(NodeId nodeid);
187 
188  void CheckIsEmpty();
189  void CheckIsEmpty(NodeId nodeid);
190 
191  std::vector<TxOrphanage::OrphanTxBase> GetOrphanTransactions() const;
192 
193 protected:
195  std::vector<Txid> GetUniqueParents(const CTransaction& tx);
196 
201  bool MaybeAddOrphanResolutionCandidate(const std::vector<Txid>& unique_parents, const Wtxid& wtxid, NodeId nodeid, std::chrono::microseconds now);
202 };
203 } // namespace node
204 #endif // BITCOIN_NODE_TXDOWNLOADMAN_IMPL_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
PeerInfo(const TxDownloadConnectionInfo &info)
std::pair< bool, std::optional< PackageToValidate > > ReceivedTx(NodeId nodeid, const CTransactionRef &ptx)
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...
Data structure to keep track of, and schedule, transaction downloads from peers.
Definition: txrequest.h:96
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, skipping any combinations that have already been tried.
CRollingBloomFilter & RecentConfirmedTransactionsFilter()
TxRequestTracker m_txrequest
Tracks candidates for requesting and downloading transaction data.
TxOrphanage m_orphanage
Manages unvalidated tx data (orphan transactions for which we are downloading ancestors).
A class to track orphan transactions (failed on TX_MISSING_INPUTS) Since we cannot distinguish orphan...
Definition: txorphanage.h:29
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
Definition: packages.h:50
RejectedTxTodo MempoolRejectedTx(const CTransactionRef &ptx, const TxValidationState &state, NodeId nodeid, bool first_time_failure)
void BlockConnected(const std::shared_ptr< const CBlock > &pblock)
void MempoolRejectedPackage(const Package &package)
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set. ...
Definition: bloom.h:108
bool AddTxAnnouncement(NodeId peer, const GenTxid &gtxid, std::chrono::microseconds now)
Consider adding this tx hash to 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...
std::vector< Txid > GetUniqueParents(const CTransaction &tx)
Helper for getting deduplicated vector of Txids in vin.
void ReceivedNotFound(NodeId nodeid, const std::vector< uint256 > &txhashes)
Marks a tx as ReceivedResponse in txrequest.
CRollingBloomFilter & RecentRejectsFilter()
std::map< NodeId, PeerInfo > m_peer_info
Information for all of the peers we may download transactions from.
CRollingBloomFilter & RecentRejectsReconsiderableFilter()
int64_t NodeId
Definition: net.h:97
TxDownloadManagerImpl(const TxDownloadOptions &options)
Definition: messages.h:20
std::unique_ptr< CRollingBloomFilter > m_lazy_recent_rejects
Filter for transactions that were recently rejected by the mempool.
std::vector< GenTxid > GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time)
Get getdata requests to send.
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:303
std::unique_ptr< CRollingBloomFilter > m_lazy_recent_confirmed_transactions
const TxDownloadConnectionInfo m_connection_info
Information relevant to scheduling tx requests.
uint32_t m_num_wtxid_peers
Number of wtxid relay peers we have in m_peer_info.
void MempoolAcceptedTx(const CTransactionRef &tx)
CTransactionRef GetTxToReconsider(NodeId nodeid)
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:295
std::vector< TxOrphanage::OrphanTxBase > GetOrphanTransactions() const
void ConnectedPeer(NodeId nodeid, const TxDownloadConnectionInfo &info)
bool AlreadyHaveTx(const GenTxid &gtxid, bool include_reconsiderable)
Check whether we already have this gtxid in:
void DisconnectedPeer(NodeId nodeid)
A generic txid reference (txid or wtxid).
Definition: transaction.h:427
transaction_identifier represents the two canonical transaction identifier types (txid, wtxid).