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 protected:
51     struct OrphanTx {
52         CTransactionRef tx;
53         NodeId fromPeer;
54         int64_t nTimeExpire;
55         size_t list_pos;
56     };
57 
58     /** Map from txid to orphan transaction record. Limited by
59      *  -maxorphantx/DEFAULT_MAX_ORPHAN_TRANSACTIONS */
60     std::map<uint256, OrphanTx> m_orphans GUARDED_BY(g_cs_orphans);
61 
62     using OrphanMap = decltype(m_orphans);
63 
64     struct IteratorComparator
65     {
66         template<typename I>
operatorIteratorComparator67         bool operator()(const I& a, const I& b) const
68         {
69             return &(*a) < &(*b);
70         }
71     };
72 
73     /** Index from the parents' COutPoint into the m_orphans. Used
74      *  to remove orphan transactions from the m_orphans */
75     std::map<COutPoint, std::set<OrphanMap::iterator, IteratorComparator>> m_outpoint_to_orphan_it GUARDED_BY(g_cs_orphans);
76 
77     /** Orphan transactions in vector for quick random eviction */
78     std::vector<OrphanMap::iterator> m_orphan_list GUARDED_BY(g_cs_orphans);
79 
80     /** Index from wtxid into the m_orphans to lookup orphan
81      *  transactions using their witness ids. */
82     std::map<uint256, OrphanMap::iterator> m_wtxid_to_orphan_it GUARDED_BY(g_cs_orphans);
83 };
84 
85 #endif // BITCOIN_TXORPHANAGE_H
86