Bitcoin Core  28.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 <net.h>
9 #include <primitives/block.h>
10 #include <primitives/transaction.h>
11 #include <sync.h>
12 #include <util/time.h>
13 
14 #include <map>
15 #include <set>
16 
18 static constexpr auto ORPHAN_TX_EXPIRE_TIME{20min};
20 static constexpr auto ORPHAN_TX_EXPIRE_INTERVAL{5min};
21 
28 class TxOrphanage {
29 public:
31  bool AddTx(const CTransactionRef& tx, NodeId peer);
32 
34  bool HaveTx(const Wtxid& wtxid) const;
35 
42 
44  int EraseTx(const Wtxid& wtxid);
45 
47  void EraseForPeer(NodeId peer);
48 
50  void EraseForBlock(const CBlock& block);
51 
53  void LimitOrphans(unsigned int max_orphans, FastRandomContext& rng);
54 
56  void AddChildrenToWorkSet(const CTransaction& tx);
57 
59  bool HaveTxToReconsider(NodeId peer);
60 
63  std::vector<CTransactionRef> GetChildrenFromSamePeer(const CTransactionRef& parent, NodeId nodeid) const;
64 
67  std::vector<std::pair<CTransactionRef, NodeId>> GetChildrenFromDifferentPeer(const CTransactionRef& parent, NodeId nodeid) const;
68 
70  size_t Size()
71  {
72  return m_orphans.size();
73  }
74 
75 protected:
76  struct OrphanTx {
80  size_t list_pos;
81  };
82 
85  std::map<Wtxid, OrphanTx> m_orphans;
86 
88  std::map<NodeId, std::set<Wtxid>> m_peer_work_set;
89 
90  using OrphanMap = decltype(m_orphans);
91 
93  {
94  template<typename I>
95  bool operator()(const I& a, const I& b) const
96  {
97  return a->first < b->first;
98  }
99  };
100 
103  std::map<COutPoint, std::set<OrphanMap::iterator, IteratorComparator>> m_outpoint_to_orphan_it;
104 
106  std::vector<OrphanMap::iterator> m_orphan_list;
107 
110 };
111 
112 #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:48
static constexpr auto ORPHAN_TX_EXPIRE_TIME
Expiration time for orphan transactions.
Definition: txorphanage.h:18
Definition: block.h:68
size_t Size()
Return how many entries exist in the orphange.
Definition: txorphanage.h:70
A class to track orphan transactions (failed on TX_MISSING_INPUTS) Since we cannot distinguish orphan...
Definition: txorphanage.h:28
NodeSeconds nTimeExpire
Definition: txorphanage.h:79
std::vector< CTransactionRef > GetChildrenFromSamePeer(const CTransactionRef &parent, NodeId nodeid) const
Get all children that spend from this tx and were received from nodeid.
std::map< NodeId, std::set< Wtxid > > m_peer_work_set
Which peer provided the orphans that need to be reconsidered.
Definition: txorphanage.h:88
std::map< Wtxid, OrphanTx > m_orphans
Map from wtxid to orphan transaction record.
Definition: txorphanage.h:85
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:23
void LimitOrphans(unsigned int max_orphans, FastRandomContext &rng)
Limit the orphanage to the given maximum.
void EraseForPeer(NodeId peer)
Erase all orphans announced by a peer (eg, after that peer disconnects)
Definition: txorphanage.cpp:83
CTransactionRef tx
Definition: txorphanage.h:77
Fast randomness source.
Definition: random.h:376
std::vector< std::pair< CTransactionRef, NodeId > > GetChildrenFromDifferentPeer(const CTransactionRef &parent, NodeId nodeid) const
Get all children that spend from this tx but were not received from nodeid.
int64_t NodeId
Definition: net.h:97
void AddChildrenToWorkSet(const CTransaction &tx)
Add any orphans that list a particular tx as a parent into the from peer&#39;s work set.
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:109
decltype(m_orphans) OrphanMap
Definition: txorphanage.h:90
static constexpr auto ORPHAN_TX_EXPIRE_INTERVAL
Minimum time between orphan transactions expire time checks.
Definition: txorphanage.h:20
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:103
std::vector< OrphanMap::iterator > m_orphan_list
Orphan transactions in vector for quick random eviction.
Definition: txorphanage.h:106
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:95
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)
transaction_identifier represents the two canonical transaction identifier types (txid, wtxid).