Bitcoin ABC  0.29.2
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 
15 
22 class TxOrphanage {
23 public:
25  bool AddTx(const CTransactionRef &tx, NodeId peer)
27 
29  bool HaveTx(const TxId &txid) const LOCKS_EXCLUDED(g_cs_orphans);
30 
35  std::pair<CTransactionRef, NodeId> GetTx(const TxId &txid) const
37 
40 
45 
47  void EraseForBlock(const CBlock &block) LOCKS_EXCLUDED(g_cs_orphans);
48 
50  unsigned int LimitOrphans(unsigned int max_orphans)
52 
58  void AddChildrenToWorkSet(const CTransaction &tx,
59  std::set<TxId> &orphan_work_set) const
61 
64  LOCK(::g_cs_orphans);
65  return m_orphans.size();
66  }
67 
68 protected:
69  struct OrphanTx {
72  int64_t nTimeExpire;
73  size_t list_pos;
74  };
75 
80  std::map<TxId, OrphanTx> m_orphans GUARDED_BY(g_cs_orphans);
81 
82  using OrphanMap = decltype(m_orphans);
83 
85  template <typename I> bool operator()(const I &a, const I &b) const {
86  return &(*a) < &(*b);
87  }
88  };
89 
94  std::map<COutPoint, std::set<OrphanMap ::iterator, IteratorComparator>>
95  m_outpoint_to_orphan_it GUARDED_BY(g_cs_orphans);
96 
98  std::vector<OrphanMap::iterator> m_orphan_list GUARDED_BY(g_cs_orphans);
99 };
100 
101 #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
decltype(m_orphans) OrphanMap
Definition: txorphanage.h:82
bool HaveTx(const TxId &txid) const LOCKS_EXCLUDED(g_cs_orphans)
Check if we already have an orphan transaction.
std::pair< CTransactionRef, NodeId > GetTx(const TxId &txid) const EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
Get an orphan transaction and its originating peer (Transaction ref will be nullptr if not found)
size_t Size() LOCKS_EXCLUDED(
Return how many entries exist in the orphange.
Definition: txorphanage.h:63
void EraseForBlock(const CBlock &block) LOCKS_EXCLUDED(g_cs_orphans)
Erase all orphans included in or invalidated by a new block.
std::map< TxId, OrphanTx > m_orphans GUARDED_BY(g_cs_orphans)
Map from txid to orphan transaction record.
std::map< COutPoint, std::set< OrphanMap ::iterator, IteratorComparator > > m_outpoint_to_orphan_it GUARDED_BY(g_cs_orphans)
Index from the parents' COutPoint into the m_orphans.
void EraseForPeer(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
Erase all orphans announced by a peer (eg, after that peer disconnects)
Definition: txorphanage.cpp:88
std::vector< OrphanMap::iterator > m_orphan_list GUARDED_BY(g_cs_orphans)
Orphan transactions in vector for quick random eviction.
bool AddTx(const CTransactionRef &tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
Add a new orphan transaction.
Definition: txorphanage.cpp:20
unsigned int LimitOrphans(unsigned int max_orphans) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
Limit the orphanage to the given maximum.
void AddChildrenToWorkSet(const CTransaction &tx, std::set< TxId > &orphan_work_set) const EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
Add any orphans that list a particular tx as a parent into a peer's work set (ie orphans that may hav...
int EraseTx(const TxId &txid) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
Erase an orphan by txid.
Definition: txorphanage.cpp:56
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:85
CTransactionRef tx
Definition: txorphanage.h:70
#define LOCK(cs)
Definition: sync.h:306
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:55
RecursiveMutex g_cs_orphans
Guards orphan transactions and extra txs for compact blocks.
Definition: txorphanage.cpp:18