24 return m_impl->m_txrequest;
27 return m_impl->RecentRejectsFilter();
30 return m_impl->RecentConfirmedTransactionsFilter();
36 const std::shared_ptr<const CBlock> &pblock) {
37 m_impl->BlockConnected(pblock);
40 m_impl->BlockDisconnected();
44 m_impl->ConnectedPeer(nodeid, info);
47 m_impl->DisconnectedPeer(nodeid);
50 std::chrono::microseconds now) {
51 return m_impl->AddTxAnnouncement(peer, txid, now);
55 std::chrono::microseconds current_time) {
56 return m_impl->GetRequestsToSend(nodeid, current_time);
59 const std::vector<TxId> &txids) {
60 m_impl->ReceivedNotFound(nodeid, txids);
63 m_impl->MempoolAcceptedTx(tx);
68 NodeId nodeid,
bool first_time_failure) {
69 return m_impl->MempoolRejectedTx(ptx, state, nodeid, first_time_failure);
72 m_impl->MempoolRejectedPackage(package);
74std::pair<bool, std::optional<PackageToValidate>>
76 return m_impl->ReceivedTx(nodeid, ptx);
79 return m_impl->HaveMoreWork(nodeid);
82 return m_impl->GetTxToReconsider(nodeid);
88 m_impl->CheckIsEmpty(nodeid);
90std::vector<TxOrphanage::PoolTx>
92 return m_impl->GetOrphanTransactions();
102 const std::shared_ptr<const CBlock> &pblock) {
129 bool include_reconsiderable) {
131 return orphanage.HaveTx(txid);
138 return conflicting.HaveTx(txid);
143 if (include_reconsiderable &&
177 std::chrono::microseconds now) {
184 return orphanage.
GetTx(txid);
187 std::erase_if(unique_parents, [&](
const auto &parent_txid) {
195 if (unique_parents.empty()) {
222 const auto &info = it->second.m_connection_info;
224 info.m_relay_permissions)) {
234 info.m_relay_permissions);
242 const std::vector<TxId> &unique_parents,
const TxId &txid,
NodeId nodeid,
243 std::chrono::microseconds now) {
250 return orphanage.HaveTxFromPeer(txid, nodeid);
256 const auto &info = peer_entry.m_connection_info;
261 if (!info.m_relay_permissions) {
275 info.m_relay_permissions);
280 for (
const auto &parent_txid : unique_parents) {
284 "added peer=%d as a candidate for resolving orphan %s\n", nodeid,
290 NodeId nodeid, std::chrono::microseconds current_time) {
291 std::vector<TxId> requests;
292 std::vector<std::pair<NodeId, TxId>> expired;
295 for (
const auto &entry : expired) {
297 entry.second.ToString(), entry.first);
299 for (
const TxId &txid : requestable) {
304 requests.emplace_back(txid);
319 const std::vector<TxId> &txids) {
320 for (
const auto &txid : txids) {
327std::optional<PackageToValidate>
330 const auto &parent_txid{ptx->GetId()};
346 for (
const auto &child : cpfp_candidates_same_peer) {
347 Package maybe_cpfp_package{ptx, child};
366 orphanage.
EraseTx(tx->GetId());
372 std::vector<TxId> unique_parents;
373 unique_parents.reserve(tx.vin.size());
374 for (
const CTxIn &txin : tx.vin) {
376 unique_parents.push_back(txin.prevout.GetTxId());
379 std::sort(unique_parents.begin(), unique_parents.end());
380 unique_parents.erase(
381 std::unique(unique_parents.begin(), unique_parents.end()),
382 unique_parents.end());
384 return unique_parents;
389 bool first_time_failure) {
390 const CTransaction &tx{*ptx};
391 const TxId &txid = ptx->GetId();
394 bool add_extra_compact_tx{first_time_failure};
396 std::vector<TxId> unique_parents;
398 std::optional<PackageToValidate> package_to_validate;
407 bool fRejectedParents =
false;
419 std::optional<TxId> rejected_parent_reconsiderable;
420 for (
const TxId &parent_txid : unique_parents) {
422 fRejectedParents =
true;
433 if (rejected_parent_reconsiderable.has_value()) {
434 fRejectedParents =
true;
437 rejected_parent_reconsiderable = parent_txid;
440 if (!fRejectedParents) {
445 std::erase_if(unique_parents, [&](
const auto &parent_txid) {
449 const auto now{GetTime<std::chrono::microseconds>()};
455 return orphanage.
HaveTx(txid);
462 std::vector<NodeId> orphan_resolution_candidates{nodeid};
464 orphan_resolution_candidates);
466 for (
const auto &candidate : orphan_resolution_candidates) {
471 orphanage.
AddTx(ptx, candidate);
484 unsigned int nEvicted =
491 "orphanage overflow, removed %u tx\n", nEvicted);
495 unique_parents.clear();
497 "not keeping orphan with rejected parents %s\n",
498 tx.GetId().ToString());
514 if (first_time_failure) {
520 "tx %s failed but reconsiderable, looking for child in "
536 return orphanage.EraseTx(txid);
543 .m_unique_parents = std::move(unique_parents),
544 .m_package_to_validate =
545 std::move(package_to_validate)};
552std::pair<bool, std::optional<PackageToValidate>>
554 const TxId &txid = ptx->GetId();
576 return {
false, std::nullopt};
583 "found tx %s in reconsiderable rejects, looking for child in "
589 return {
true, std::nullopt};
609 return orphanage.Size();
613std::vector<TxOrphanage::PoolTx>
#define Assume(val)
Assume is the identity function.
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set.
void insert(Span< const uint8_t > vKey)
bool contains(Span< const uint8_t > vKey) const
bool exists(const TxId &txid) const
auto withOrphanage(Callable &&func) const EXCLUSIVE_LOCKS_REQUIRED(!cs_orphanage)
auto withConflicting(Callable &&func) const EXCLUSIVE_LOCKS_REQUIRED(!cs_conflicting)
std::vector< InvId > GetRequestable(NodeId peer, std::chrono::microseconds now, std::vector< std::pair< NodeId, InvId > > *expired)
Find the invids to request now from peer.
size_t Size() const
Count how many announcements are being tracked in total across all peers and inventory ids.
void RequestedData(NodeId peer, const InvId &invid, std::chrono::microseconds expiry)
Marks an inventory as requested, with a specified expiry.
void ForgetInvId(const InvId &invid)
Deletes all announcements for a given invid.
void DisconnectedPeer(NodeId peer)
Deletes all announcements for a given peer.
size_t Count(NodeId peer) const
Count how many announcements a peer has (REQUESTED, CANDIDATE, and COMPLETED combined).
void ReceivedInv(NodeId peer, const InvId &invid, bool preferred, std::chrono::microseconds reqtime)
Adds a new CANDIDATE announcement.
void ReceivedResponse(NodeId peer, const InvId &invid)
Converts a CANDIDATE or REQUESTED announcement to a COMPLETED one.
void GetCandidatePeers(const InvId &invid, std::vector< NodeId > &result_peers) const
For some invid, finds all peers with non-COMPLETED announcements and appends them to result_peers.
std::vector< PoolTx > GetOrphanTransactions() const
void EraseForPeer(NodeId peer)
Maybe erase all txs announced by a peer (eg, after that peer disconnects).
void EraseForBlock(const CBlock &block)
Erase all txs included in or invalidated by a new block.
CTransactionRef GetTx(const TxId &txid) const
CTransactionRef GetTxToReconsider(NodeId peer)
Extract a transaction from a peer's work set.
bool AddAnnouncer(const TxId &txid, NodeId peer)
Add an additional announcer to a tx if it exists.
void AddChildrenToWorkSet(const CTransaction &tx, FastRandomContext &rng)
Add any tx that list a particular tx as a parent into the from peer's work set.
bool AddTx(const CTransactionRef &tx, NodeId peer)
Add a new transaction to the pool.
unsigned int LimitTxs(unsigned int max_txs, FastRandomContext &rng)
Limit the txs to the given maximum.
std::vector< CTransactionRef > GetChildrenFromSamePeer(const CTransactionRef &parent, NodeId nodeid) const
Get all children that spend from this tx and were received from nodeid.
bool HaveTx(const TxId &txid) const
Check if we already have an the transaction.
bool HaveTxToReconsider(NodeId peer)
Does this peer have any work to do?
int EraseTx(const TxId &txid)
Erase a tx by txid.
std::string ToString() const
const std::unique_ptr< TxDownloadManagerImpl > m_impl
std::vector< TxId > GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time)
Get getdata requests to send.
InvRequestTracker< TxId > & GetTxRequestRef()
std::pair< bool, std::optional< PackageToValidate > > ReceivedTx(NodeId nodeid, const CTransactionRef &ptx)
Marks a tx as ReceivedResponse in txrequest and checks whether AlreadyHaveTx.
std::vector< TxOrphanage::PoolTx > GetOrphanTransactions() const
Wrapper for TxOrphanage::GetOrphanTransactions.
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.
bool AddTxAnnouncement(NodeId peer, const TxId &txid, std::chrono::microseconds now)
Consider adding this tx hash to txrequest.
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.
CRollingBloomFilter & RecentRejectsFilter()
RejectedTxTodo MempoolRejectedTx(const CTransactionRef &ptx, const TxValidationState &state, NodeId nodeid, bool first_time_failure)
Respond to transaction rejected from mempool.
TxDownloadManager(const TxDownloadOptions &options)
CRollingBloomFilter & RecentConfirmedTransactionsFilter()
void ReceivedNotFound(NodeId nodeid, const std::vector< TxId > &txids)
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.
InvRequestTracker< TxId > m_txrequest
Tracks candidates for requesting and downloading transaction data.
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,...
CRollingBloomFilter & RecentConfirmedTransactionsFilter()
void DisconnectedPeer(NodeId nodeid)
bool HaveMoreWork(NodeId nodeid)
bool MaybeAddOrphanResolutionCandidate(const std::vector< TxId > &unique_parents, const TxId &txid, NodeId nodeid, std::chrono::microseconds now)
If this peer is an orphan resolution candidate for this transaction, treat the unique_parents as anno...
RejectedTxTodo MempoolRejectedTx(const CTransactionRef &ptx, const TxValidationState &state, NodeId nodeid, bool first_time_failure)
std::pair< bool, std::optional< PackageToValidate > > ReceivedTx(NodeId nodeid, const CTransactionRef &ptx)
void MempoolAcceptedTx(const CTransactionRef &tx)
void ReceivedNotFound(NodeId nodeid, const std::vector< TxId > &txids)
Marks a tx as ReceivedResponse in txrequest.
void ConnectedPeer(NodeId nodeid, const TxDownloadConnectionInfo &info)
std::map< NodeId, PeerInfo > m_peer_info
Information for all of the peers we may download transactions from.
std::vector< TxId > GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time)
Get getdata requests to send.
bool AlreadyHaveTx(const TxId &txid, bool include_reconsiderable)
Check whether we already have this txid in:
std::vector< TxOrphanage::PoolTx > GetOrphanTransactions() const
void BlockConnected(const std::shared_ptr< const CBlock > &pblock)
CTransactionRef GetTxToReconsider(NodeId nodeid)
std::vector< TxId > GetUniqueParents(const CTransaction &tx)
Helper for getting deduplicated vector of Txids in vin.
CRollingBloomFilter & RecentRejectsPackageReconsiderableFilter()
bool AddTxAnnouncement(NodeId peer, const TxId &txid, std::chrono::microseconds now)
Consider adding this tx hash to txrequest.
CRollingBloomFilter & RecentRejectsFilter()
void MempoolRejectedPackage(const Package &package)
@ TX_MISSING_INPUTS
transaction was missing some of its inputs
@ TX_PACKAGE_RECONSIDERABLE
fails some policy, but might be acceptable if submitted in a (different) package
std::chrono::microseconds ComputeRequestTime(const InvRequestTracker< InvId > &requestTracker, NodeId nodeid, const DataRequestParameters &requestParams, std::chrono::microseconds current_time, bool preferred, bool bypass_limits)
Compute the request time for this announcement, current time plus delays for:
bool TooManyAnnouncements(const InvRequestTracker< InvId > &requestTracker, NodeId nodeid, const DataRequestParameters &requestParams, bool bypass_limits)
Whether the peer already has too many announcements tracked to accept a new one.
#define LogDebug(category,...)
static constexpr DataRequestParameters TX_REQUEST_PARAMS
Parameters for the tx requests, see DataRequestParameters.
Implement std::hash so RCUPtr can be used as a key for maps or sets.
uint256 GetPackageHash(const Package &package)
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
std::shared_ptr< const CTransaction > CTransactionRef
const size_t max_peer_announcements
Maximum number of inventories to consider for requesting, per peer.
const std::chrono::microseconds getdata_interval
How long to wait (in microseconds) before a data request from an additional peer.
A TxId is the identifier of a transaction.
bool m_should_add_extra_compact_tx
const uint32_t m_max_orphan_txs
Maximum number of transactions allowed in orphanage.
const CTxMemPool & m_mempool
Read-only reference to mempool.