Bitcoin ABC  0.26.3
P2P Digital Currency
txorphanage.h
Go to the documentation of this file.
1 // Copyright (c) 2021 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 
22 class TxOrphanage {
23 public:
25  bool AddTx(const CTransactionRef &tx, NodeId peer)
27 
29  bool HaveTx(const TxId &txid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
30 
40 
42  int EraseTx(const TxId &txid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
43 
48 
51 
53  unsigned int LimitOrphans(unsigned int max_orphans)
55 
60  void AddChildrenToWorkSet(const CTransaction &tx)
62 
65 
68  LOCK(m_mutex);
69  return m_orphans.size();
70  }
71 
72 protected:
74  mutable Mutex m_mutex;
75 
76  struct OrphanTx {
79  int64_t nTimeExpire;
80  size_t list_pos;
81  };
82 
87  std::map<TxId, OrphanTx> m_orphans GUARDED_BY(m_mutex);
88 
90  std::map<NodeId, std::set<TxId>> m_peer_work_set GUARDED_BY(m_mutex);
91 
92  using OrphanMap = decltype(m_orphans);
93 
95  template <typename I> bool operator()(const I &a, const I &b) const {
96  return &(*a) < &(*b);
97  }
98  };
99 
104  std::map<COutPoint, std::set<OrphanMap ::iterator, IteratorComparator>>
105  m_outpoint_to_orphan_it GUARDED_BY(m_mutex);
106 
108  std::vector<OrphanMap::iterator> m_orphan_list GUARDED_BY(m_mutex);
109 
112 };
113 
114 #endif // BITCOIN_TXORPHANAGE_H
Definition: block.h:60
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:192
A class to track orphan transactions (failed on TX_MISSING_INPUTS) Since we cannot distinguish orphan...
Definition: txorphanage.h:22
bool HaveTxToReconsider(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Does this peer have any work to do?
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.
decltype(m_orphans) OrphanMap
Definition: txorphanage.h:92
unsigned int LimitOrphans(unsigned int max_orphans) 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:74
bool AddTx(const CTransactionRef &tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Add a new orphan transaction.
Definition: txorphanage.cpp:18
std::map< NodeId, std::set< TxId > > m_peer_work_set GUARDED_BY(m_mutex)
Which peer provided the orphans that need to be reconsidered.
int EraseTxNoLock(const TxId &txid) EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Erase an orphan by txid.
Definition: txorphanage.cpp:59
size_t Size() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Return how many entries exist in the orphange.
Definition: txorphanage.h:67
int EraseTx(const TxId &txid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Erase an orphan by txid.
Definition: txorphanage.cpp:54
bool HaveTx(const TxId &txid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Check if we already have an orphan transaction.
CTransactionRef GetTxToReconsider(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Extract a transaction from a peer's work set.
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 EraseForPeer(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Erase all orphans announced by a peer (eg, after that peer disconnects)
Definition: txorphanage.cpp:92
std::map< TxId, OrphanTx > m_orphans GUARDED_BY(m_mutex)
Map from txid to orphan transaction record.
std::vector< OrphanMap::iterator > m_orphan_list GUARDED_BY(m_mutex)
Orphan transactions in vector for quick random eviction.
int64_t NodeId
Definition: nodeid.h:10
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
A TxId is the identifier of a transaction.
Definition: txid.h:14
bool operator()(const I &a, const I &b) const
Definition: txorphanage.h:95
CTransactionRef tx
Definition: txorphanage.h:77
#define LOCK(cs)
Definition: sync.h:306
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56