Bitcoin Core  21.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 
50 protected:
51  struct OrphanTx {
54  int64_t nTimeExpire;
55  size_t list_pos;
56  };
57 
60  std::map<uint256, OrphanTx> m_orphans GUARDED_BY(g_cs_orphans);
61 
62  using OrphanMap = decltype(m_orphans);
63 
65  {
66  template<typename I>
67  bool operator()(const I& a, const I& b) const
68  {
69  return &(*a) < &(*b);
70  }
71  };
72 
75  std::map<COutPoint, std::set<OrphanMap::iterator, IteratorComparator>> m_outpoint_to_orphan_it GUARDED_BY(g_cs_orphans);
76 
78  std::vector<OrphanMap::iterator> m_orphan_list GUARDED_BY(g_cs_orphans);
79 
82  std::map<uint256, OrphanMap::iterator> m_wtxid_to_orphan_it GUARDED_BY(g_cs_orphans);
83 };
84 
85 #endif // BITCOIN_TXORPHANAGE_H
NodeId
int64_t NodeId
Definition: net.h:90
block.h
TxOrphanage::OrphanTx
Definition: txorphanage.h:51
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:55
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:67
TxOrphanage::OrphanMap
decltype(m_orphans) OrphanMap
Definition: txorphanage.h:62
TxOrphanage::IteratorComparator
Definition: txorphanage.h:64
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:52
uint256
256-bit opaque blob.
Definition: uint256.h:124
CBlock
Definition: block.h:62
TxOrphanage::OrphanTx::nTimeExpire
int64_t nTimeExpire
Definition: txorphanage.h:54
LOCKS_EXCLUDED
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:48
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
TxOrphanage::OrphanTx::fromPeer
NodeId fromPeer
Definition: txorphanage.h:53
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