Bitcoin Core  27.99.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 
13 #include <map>
14 #include <set>
15 
21 class TxOrphanage {
22 public:
25 
27  bool HaveTx(const Wtxid& wtxid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
28 
35 
37  int EraseTx(const Wtxid& wtxid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
38 
41 
44 
46  void LimitOrphans(unsigned int max_orphans, FastRandomContext& rng) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
47 
50 
53 
56  std::vector<CTransactionRef> GetChildrenFromSamePeer(const CTransactionRef& parent, NodeId nodeid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
57 
60  std::vector<std::pair<CTransactionRef, NodeId>> GetChildrenFromDifferentPeer(const CTransactionRef& parent, NodeId nodeid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
61 
64  {
65  LOCK(m_mutex);
66  return m_orphans.size();
67  }
68 
69 protected:
71  mutable Mutex m_mutex;
72 
73  struct OrphanTx {
76  int64_t nTimeExpire;
77  size_t list_pos;
78  };
79 
82  std::map<Wtxid, OrphanTx> m_orphans GUARDED_BY(m_mutex);
83 
85  std::map<NodeId, std::set<Wtxid>> m_peer_work_set GUARDED_BY(m_mutex);
86 
87  using OrphanMap = decltype(m_orphans);
88 
90  {
91  template<typename I>
92  bool operator()(const I& a, const I& b) const
93  {
94  return &(*a) < &(*b);
95  }
96  };
97 
100  std::map<COutPoint, std::set<OrphanMap::iterator, IteratorComparator>> m_outpoint_to_orphan_it GUARDED_BY(m_mutex);
101 
103  std::vector<OrphanMap::iterator> m_orphan_list GUARDED_BY(m_mutex);
104 
107 };
108 
109 #endif // BITCOIN_TXORPHANAGE_H
Definition: block.h:69
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:296
Fast randomness source.
Definition: random.h:145
A class to track orphan transactions (failed on TX_MISSING_INPUTS) Since we cannot distinguish orphan...
Definition: txorphanage.h:21
bool HaveTxToReconsider(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Does this peer have any work to do?
std::map< NodeId, std::set< Wtxid > > m_peer_work_set GUARDED_BY(m_mutex)
Which peer provided the orphans that need to be reconsidered.
std::map< Wtxid, OrphanTx > m_orphans GUARDED_BY(m_mutex)
Map from wtxid to orphan transaction record.
void AddChildrenToWorkSet(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Add any orphans that list a particular tx as a parent into the from peer's work set.
int EraseTxNoLock(const Wtxid &wtxid) EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Erase an orphan by wtxid.
Definition: txorphanage.cpp:61
decltype(m_orphans) OrphanMap
Definition: txorphanage.h:87
std::map< COutPoint, std::set< OrphanMap::iterator, IteratorComparator > > m_outpoint_to_orphan_it GUARDED_BY(m_mutex)
Index from the parents' COutPoint into the m_orphans.
void LimitOrphans(unsigned int max_orphans, FastRandomContext &rng) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Limit the orphanage to the given maximum.
void EraseForBlock(const CBlock &block) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Erase all orphans included in or invalidated by a new block.
Mutex m_mutex
Guards orphan transactions.
Definition: txorphanage.h:71
bool AddTx(const CTransactionRef &tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Add a new orphan transaction.
Definition: txorphanage.cpp:20
std::vector< std::pair< CTransactionRef, NodeId > > GetChildrenFromDifferentPeer(const CTransactionRef &parent, NodeId nodeid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Get all children that spend from this tx but were not received from nodeid.
size_t Size() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Return how many entries exist in the orphange.
Definition: txorphanage.h:63
bool HaveTx(const Wtxid &wtxid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Check if we already have an orphan transaction (by wtxid only)
CTransactionRef GetTxToReconsider(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Extract a transaction from a peer's work set Returns nullptr if there are no transactions to work on.
void EraseForPeer(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Erase all orphans announced by a peer (eg, after that peer disconnects)
Definition: txorphanage.cpp:97
std::vector< CTransactionRef > GetChildrenFromSamePeer(const CTransactionRef &parent, NodeId nodeid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Get all children that spend from this tx and were received from nodeid.
int EraseTx(const Wtxid &wtxid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Erase an orphan by wtxid.
Definition: txorphanage.cpp:55
std::vector< OrphanMap::iterator > m_orphan_list GUARDED_BY(m_mutex)
Orphan transactions in vector for quick random eviction.
transaction_identifier represents the two canonical transaction identifier types (txid,...
int64_t NodeId
Definition: net.h:97
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
bool operator()(const I &a, const I &b) const
Definition: txorphanage.h:92
CTransactionRef tx
Definition: txorphanage.h:74
#define LOCK(cs)
Definition: sync.h:257
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49