Branch data Line data Source code
# 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 : : /** Guards orphan transactions and extra txs for compact blocks */ # 14 : : extern RecursiveMutex g_cs_orphans; # 15 : : # 16 : : /** A class to track orphan transactions (failed on TX_MISSING_INPUTS) # 17 : : * Since we cannot distinguish orphans from bad transactions with # 18 : : * non-existent inputs, we heavily limit the number of orphans # 19 : : * we keep and the duration we keep them for. # 20 : : */ # 21 : : class TxOrphanage { # 22 : : public: # 23 : : /** Add a new orphan transaction */ # 24 : : bool AddTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); # 25 : : # 26 : : /** Check if we already have an orphan transaction (by txid or wtxid) */ # 27 : : bool HaveTx(const GenTxid& gtxid) const LOCKS_EXCLUDED(::g_cs_orphans); # 28 : : # 29 : : /** Get an orphan transaction and its originating peer # 30 : : * (Transaction ref will be nullptr if not found) # 31 : : */ # 32 : : std::pair<CTransactionRef, NodeId> GetTx(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); # 33 : : # 34 : : /** Erase an orphan by txid */ # 35 : : int EraseTx(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); # 36 : : # 37 : : /** Erase all orphans announced by a peer (eg, after that peer disconnects) */ # 38 : : void EraseForPeer(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); # 39 : : # 40 : : /** Erase all orphans included in or invalidated by a new block */ # 41 : : void EraseForBlock(const CBlock& block) LOCKS_EXCLUDED(::g_cs_orphans); # 42 : : # 43 : : /** Limit the orphanage to the given maximum */ # 44 : : unsigned int LimitOrphans(unsigned int max_orphans) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); # 45 : : # 46 : : /** Add any orphans that list a particular tx as a parent into a peer's work set # 47 : : * (ie orphans that may have found their final missing parent, and so should be reconsidered for the mempool) */ # 48 : : void AddChildrenToWorkSet(const CTransaction& tx, std::set<uint256>& orphan_work_set) const EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); # 49 : : # 50 : : /** Return how many entries exist in the orphange */ # 51 : : size_t Size() LOCKS_EXCLUDED(::g_cs_orphans) # 52 : 640 : { # 53 : 640 : LOCK(::g_cs_orphans); # 54 : 640 : return m_orphans.size(); # 55 : 640 : } # 56 : : # 57 : : protected: # 58 : : struct OrphanTx { # 59 : : CTransactionRef tx; # 60 : : NodeId fromPeer; # 61 : : int64_t nTimeExpire; # 62 : : size_t list_pos; # 63 : : }; # 64 : : # 65 : : /** Map from txid to orphan transaction record. Limited by # 66 : : * -maxorphantx/DEFAULT_MAX_ORPHAN_TRANSACTIONS */ # 67 : : std::map<uint256, OrphanTx> m_orphans GUARDED_BY(g_cs_orphans); # 68 : : # 69 : : using OrphanMap = decltype(m_orphans); # 70 : : # 71 : : struct IteratorComparator # 72 : : { # 73 : : template<typename I> # 74 : : bool operator()(const I& a, const I& b) const # 75 : 578 : { # 76 : 578 : return &(*a) < &(*b); # 77 : 578 : } # 78 : : }; # 79 : : # 80 : : /** Index from the parents' COutPoint into the m_orphans. Used # 81 : : * to remove orphan transactions from the m_orphans */ # 82 : : std::map<COutPoint, std::set<OrphanMap::iterator, IteratorComparator>> m_outpoint_to_orphan_it GUARDED_BY(g_cs_orphans); # 83 : : # 84 : : /** Orphan transactions in vector for quick random eviction */ # 85 : : std::vector<OrphanMap::iterator> m_orphan_list GUARDED_BY(g_cs_orphans); # 86 : : # 87 : : /** Index from wtxid into the m_orphans to lookup orphan # 88 : : * transactions using their witness ids. */ # 89 : : std::map<uint256, OrphanMap::iterator> m_wtxid_to_orphan_it GUARDED_BY(g_cs_orphans); # 90 : : }; # 91 : : # 92 : : #endif // BITCOIN_TXORPHANAGE_H