Bitcoin Core  29.1.0
P2P Digital Currency
txorphanage.h
Go to the documentation of this file.
1 // Copyright (c) 2021-2022 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_TXORPHANAGE_H
6 #define BITCOIN_TXORPHANAGE_H
7 
8 #include <consensus/validation.h>
9 #include <net.h>
10 #include <primitives/block.h>
11 #include <primitives/transaction.h>
12 #include <sync.h>
13 #include <util/time.h>
14 
15 #include <map>
16 #include <set>
17 
19 static constexpr auto ORPHAN_TX_EXPIRE_TIME{20min};
21 static constexpr auto ORPHAN_TX_EXPIRE_INTERVAL{5min};
22 
29 class TxOrphanage {
30 public:
32  bool AddTx(const CTransactionRef& tx, NodeId peer);
33 
35  bool AddAnnouncer(const Wtxid& wtxid, NodeId peer);
36 
37  CTransactionRef GetTx(const Wtxid& wtxid) const;
38 
40  bool HaveTx(const Wtxid& wtxid) const;
41 
43  bool HaveTxFromPeer(const Wtxid& wtxid, NodeId peer) const;
44 
51 
53  int EraseTx(const Wtxid& wtxid);
54 
57  void EraseForPeer(NodeId peer);
58 
60  void EraseForBlock(const CBlock& block);
61 
63  void LimitOrphans(unsigned int max_orphans, FastRandomContext& rng);
64 
67 
69  bool HaveTxToReconsider(NodeId peer);
70 
73  std::vector<CTransactionRef> GetChildrenFromSamePeer(const CTransactionRef& parent, NodeId nodeid) const;
74 
76  size_t Size() const
77  {
78  return m_orphans.size();
79  }
80 
82  struct OrphanTxBase {
85  std::set<NodeId> announcers;
87 
89  unsigned int GetUsage() const {
90  return GetTransactionWeight(*tx);
91  }
92  };
93 
94  std::vector<OrphanTxBase> GetOrphanTransactions() const;
95 
98  unsigned int TotalOrphanUsage() const { return m_total_orphan_usage; }
99 
103  unsigned int UsageByPeer(NodeId peer) const {
104  auto peer_it = m_peer_orphanage_info.find(peer);
105  return peer_it == m_peer_orphanage_info.end() ? 0 : peer_it->second.m_total_usage;
106  }
107 
110  void SanityCheck() const;
111 
112 protected:
113  struct OrphanTx : public OrphanTxBase {
114  size_t list_pos;
115  };
116 
118  unsigned int m_total_orphan_usage{0};
119 
122  unsigned int m_total_announcements{0};
123 
126  std::map<Wtxid, OrphanTx> m_orphans;
127 
128  struct PeerOrphanInfo {
133  std::set<Wtxid> m_work_set;
134 
140  unsigned int m_total_usage{0};
141  };
142  std::map<NodeId, PeerOrphanInfo> m_peer_orphanage_info;
143 
144  using OrphanMap = decltype(m_orphans);
145 
147  {
148  template<typename I>
149  bool operator()(const I& a, const I& b) const
150  {
151  return a->first < b->first;
152  }
153  };
154 
157  std::map<COutPoint, std::set<OrphanMap::iterator, IteratorComparator>> m_outpoint_to_orphan_it;
158 
160  std::vector<OrphanMap::iterator> m_orphan_list;
161 
164 };
165 
166 #endif // BITCOIN_TXORPHANAGE_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
CTransactionRef GetTxToReconsider(NodeId peer)
Extract a transaction from a peer&#39;s work set Returns nullptr if there are no transactions to work on...
int EraseTx(const Wtxid &wtxid)
Erase an orphan by wtxid.
Definition: txorphanage.cpp:72
bool AddAnnouncer(const Wtxid &wtxid, NodeId peer)
Add an additional announcer to an orphan if it exists.
Definition: txorphanage.cpp:55
unsigned int TotalOrphanUsage() const
Get the total usage (weight) of all orphans.
Definition: txorphanage.h:98
unsigned int GetUsage() const
Get the weight of this transaction, an approximation of its memory usage.
Definition: txorphanage.h:89
static constexpr auto ORPHAN_TX_EXPIRE_TIME
Expiration time for orphan transactions.
Definition: txorphanage.h:19
Definition: block.h:68
A class to track orphan transactions (failed on TX_MISSING_INPUTS) Since we cannot distinguish orphan...
Definition: txorphanage.h:29
std::vector< CTransactionRef > GetChildrenFromSamePeer(const CTransactionRef &parent, NodeId nodeid) const
Get all children that spend from this tx and were received from nodeid.
std::set< Wtxid > m_work_set
List of transactions that should be reconsidered: added to in AddChildrenToWorkSet, removed from one-by-one with each call to GetTxToReconsider.
Definition: txorphanage.h:133
std::set< NodeId > announcers
Peers added with AddTx or AddAnnouncer.
Definition: txorphanage.h:85
void AddChildrenToWorkSet(const CTransaction &tx, FastRandomContext &rng)
Add any orphans that list a particular tx as a parent into the from peer&#39;s work set.
static int32_t GetTransactionWeight(const CTransaction &tx)
Definition: validation.h:133
std::map< Wtxid, OrphanTx > m_orphans
Map from wtxid to orphan transaction record.
Definition: txorphanage.h:126
unsigned int m_total_orphan_usage
Total usage (weight) of all entries in m_orphans.
Definition: txorphanage.h:118
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25
CTransactionRef GetTx(const Wtxid &wtxid) const
unsigned int UsageByPeer(NodeId peer) const
Total usage (weight) of orphans for which this peer is an announcer.
Definition: txorphanage.h:103
void SanityCheck() const
Check consistency between PeerOrphanInfo and m_orphans.
void LimitOrphans(unsigned int max_orphans, FastRandomContext &rng)
Limit the orphanage to the given maximum.
void EraseForPeer(NodeId peer)
Maybe erase all orphans announced by a peer (eg, after that peer disconnects).
Fast randomness source.
Definition: random.h:376
int64_t NodeId
Definition: net.h:97
bool HaveTxFromPeer(const Wtxid &wtxid, NodeId peer) const
Check if a {tx, peer} exists in the orphanage.
bool HaveTxToReconsider(NodeId peer)
Does this peer have any work to do?
bool AddTx(const CTransactionRef &tx, NodeId peer)
Add a new orphan transaction.
Definition: txorphanage.cpp:15
NodeSeconds m_next_sweep
Timestamp for the next scheduled sweep of expired orphans.
Definition: txorphanage.h:163
std::vector< OrphanTxBase > GetOrphanTransactions() const
decltype(m_orphans) OrphanMap
Definition: txorphanage.h:144
static constexpr auto ORPHAN_TX_EXPIRE_INTERVAL
Minimum time between orphan transactions expire time checks.
Definition: txorphanage.h:21
std::map< COutPoint, std::set< OrphanMap::iterator, IteratorComparator > > m_outpoint_to_orphan_it
Index from the parents&#39; COutPoint into the m_orphans.
Definition: txorphanage.h:157
unsigned int m_total_usage
Total weight of orphans for which this peer is an announcer.
Definition: txorphanage.h:140
std::vector< OrphanMap::iterator > m_orphan_list
Orphan transactions in vector for quick random eviction.
Definition: txorphanage.h:160
size_t Size() const
Return how many entries exist in the orphange.
Definition: txorphanage.h:76
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:295
bool operator()(const I &a, const I &b) const
Definition: txorphanage.h:149
std::map< NodeId, PeerOrphanInfo > m_peer_orphanage_info
Definition: txorphanage.h:142
Allows providing orphan information externally.
Definition: txorphanage.h:82
void EraseForBlock(const CBlock &block)
Erase all orphans included in or invalidated by a new block.
bool HaveTx(const Wtxid &wtxid) const
Check if we already have an orphan transaction (by wtxid only)
unsigned int m_total_announcements
Total number of <peer, tx> pairs.
Definition: txorphanage.h:122
CTransactionRef tx
Definition: txorphanage.h:83
transaction_identifier represents the two canonical transaction identifier types (txid, wtxid).