Bitcoin Core  22.99.0
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 
21 class TxOrphanage {
22 public:
25 
27  bool HaveTx(const GenTxid& gtxid) const LOCKS_EXCLUDED(::g_cs_orphans);
28 
32  std::pair<CTransactionRef, NodeId> GetTx(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
33 
36 
39 
41  void EraseForBlock(const CBlock& block) LOCKS_EXCLUDED(::g_cs_orphans);
42 
44  unsigned int LimitOrphans(unsigned int max_orphans) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
45 
48  void AddChildrenToWorkSet(const CTransaction& tx, std::set<uint256>& orphan_work_set) const EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
49 
52  {
53  LOCK(::g_cs_orphans);
54  return m_orphans.size();
55  }
56 
57 protected:
58  struct OrphanTx {
61  int64_t nTimeExpire;
62  size_t list_pos;
63  };
64 
67  std::map<uint256, OrphanTx> m_orphans GUARDED_BY(g_cs_orphans);
68 
69  using OrphanMap = decltype(m_orphans);
70 
72  {
73  template<typename I>
74  bool operator()(const I& a, const I& b) const
75  {
76  return &(*a) < &(*b);
77  }
78  };
79 
82  std::map<COutPoint, std::set<OrphanMap::iterator, IteratorComparator>> m_outpoint_to_orphan_it GUARDED_BY(g_cs_orphans);
83 
85  std::vector<OrphanMap::iterator> m_orphan_list GUARDED_BY(g_cs_orphans);
86 
89  std::map<uint256, OrphanMap::iterator> m_wtxid_to_orphan_it GUARDED_BY(g_cs_orphans);
90 };
91 
92 #endif // BITCOIN_TXORPHANAGE_H
NodeId
int64_t NodeId
Definition: net.h:88
block.h
TxOrphanage::OrphanTx
Definition: txorphanage.h:58
TxOrphanage::EraseTx
int EraseTx(const uint256 &txid) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
Erase an orphan by txid.
Definition: txorphanage.cpp:56
GenTxid
A generic txid reference (txid or wtxid).
Definition: transaction.h:390
TxOrphanage::GetTx
bool HaveTx(const GenTxid &gtxid) const LOCKS_EXCLUDED(std::pair< CTransactionRef, NodeId > GetTx(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
Check if we already have an orphan transaction (by txid or wtxid)
Definition: txorphanage.h:32
sync.h
transaction.h
AnnotatedMixin< std::recursive_mutex >
TxOrphanage::OrphanTx::list_pos
size_t list_pos
Definition: txorphanage.h:62
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
TxOrphanage::LimitOrphans
void EraseForBlock(const CBlock &block) LOCKS_EXCLUDED(unsigned int LimitOrphans(unsigned int max_orphans) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
Erase all orphans included in or invalidated by a new block.
Definition: txorphanage.h:44
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:259
TxOrphanage::AddChildrenToWorkSet
void AddChildrenToWorkSet(const CTransaction &tx, std::set< uint256 > &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...
Definition: txorphanage.cpp:141
TxOrphanage::IteratorComparator::operator()
bool operator()(const I &a, const I &b) const
Definition: txorphanage.h:74
TxOrphanage::OrphanMap
decltype(m_orphans) OrphanMap
Definition: txorphanage.h:69
TxOrphanage::IteratorComparator
Definition: txorphanage.h:71
g_cs_orphans
RecursiveMutex g_cs_orphans
Guards orphan transactions and extra txs for compact blocks.
Definition: txorphanage.cpp:18
TxOrphanage::OrphanTx::tx
CTransactionRef tx
Definition: txorphanage.h:59
uint256
256-bit opaque blob.
Definition: uint256.h:124
TxOrphanage::Size
size_t Size() LOCKS_EXCLUDED(
Return how many entries exist in the orphange.
Definition: txorphanage.h:51
CBlock
Definition: block.h:62
TxOrphanage::OrphanTx::nTimeExpire
int64_t nTimeExpire
Definition: txorphanage.h:61
LOCKS_EXCLUDED
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:48
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
LOCK
#define LOCK(cs)
Definition: sync.h:232
TxOrphanage::OrphanTx::fromPeer
NodeId fromPeer
Definition: txorphanage.h:60
TxOrphanage::GUARDED_BY
std::map< uint256, OrphanTx > m_orphans GUARDED_BY(g_cs_orphans)
Map from txid to orphan transaction record.
TxOrphanage::AddTx
bool AddTx(const CTransactionRef &tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
Add a new orphan transaction.
Definition: txorphanage.cpp:20
TxOrphanage
A class to track orphan transactions (failed on TX_MISSING_INPUTS) Since we cannot distinguish orphan...
Definition: txorphanage.h:21
TxOrphanage::EraseForPeer
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
net.h