Branch data Line data Source code
1 : : // Copyright (c) 2021-2022 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 : : #include <util/time.h>
13 : :
14 : : #include <map>
15 : : #include <set>
16 : :
17 : : /** Expiration time for orphan transactions */
18 : : static constexpr auto ORPHAN_TX_EXPIRE_TIME{20min};
19 : : /** Minimum time between orphan transactions expire time checks */
20 : : static constexpr auto ORPHAN_TX_EXPIRE_INTERVAL{5min};
21 : :
22 : : /** A class to track orphan transactions (failed on TX_MISSING_INPUTS)
23 : : * Since we cannot distinguish orphans from bad transactions with
24 : : * non-existent inputs, we heavily limit the number of orphans
25 : : * we keep and the duration we keep them for.
26 : : * Not thread-safe. Requires external synchronization.
27 : : */
28 : : class TxOrphanage {
29 : : public:
30 : : /** Add a new orphan transaction */
31 : : bool AddTx(const CTransactionRef& tx, NodeId peer);
32 : :
33 : : /** Add an additional announcer to an orphan if it exists. Otherwise, do nothing. */
34 : : bool AddAnnouncer(const Wtxid& wtxid, NodeId peer);
35 : :
36 : : CTransactionRef GetTx(const Wtxid& wtxid) const;
37 : :
38 : : /** Check if we already have an orphan transaction (by wtxid only) */
39 : : bool HaveTx(const Wtxid& wtxid) const;
40 : :
41 : : /** Check if a {tx, peer} exists in the orphanage.*/
42 : : bool HaveTxFromPeer(const Wtxid& wtxid, NodeId peer) const;
43 : :
44 : : /** Extract a transaction from a peer's work set
45 : : * Returns nullptr if there are no transactions to work on.
46 : : * Otherwise returns the transaction reference, and removes
47 : : * it from the work set.
48 : : */
49 : : CTransactionRef GetTxToReconsider(NodeId peer);
50 : :
51 : : /** Erase an orphan by wtxid */
52 : : int EraseTx(const Wtxid& wtxid);
53 : :
54 : : /** Maybe erase all orphans announced by a peer (eg, after that peer disconnects). If an orphan
55 : : * has been announced by another peer, don't erase, just remove this peer from the list of announcers. */
56 : : void EraseForPeer(NodeId peer);
57 : :
58 : : /** Erase all orphans included in or invalidated by a new block */
59 : : void EraseForBlock(const CBlock& block);
60 : :
61 : : /** Limit the orphanage to the given maximum */
62 : : void LimitOrphans(unsigned int max_orphans, FastRandomContext& rng);
63 : :
64 : : /** Add any orphans that list a particular tx as a parent into the from peer's work set */
65 : : void AddChildrenToWorkSet(const CTransaction& tx);
66 : :
67 : : /** Does this peer have any work to do? */
68 : : bool HaveTxToReconsider(NodeId peer);
69 : :
70 : : /** Get all children that spend from this tx and were received from nodeid. Sorted from most
71 : : * recent to least recent. */
72 : : std::vector<CTransactionRef> GetChildrenFromSamePeer(const CTransactionRef& parent, NodeId nodeid) const;
73 : :
74 : : /** Return how many entries exist in the orphange */
75 : 892 : size_t Size() const
76 : : {
77 [ + + + - : 892 : return m_orphans.size();
+ - + - +
- + - + -
+ - + - +
- + - ]
[ - + ]
78 : : }
79 : :
80 : : /** Allows providing orphan information externally */
81 : : struct OrphanTxBase {
82 : : CTransactionRef tx;
83 : : /** Peers added with AddTx or AddAnnouncer. */
84 : : std::set<NodeId> announcers;
85 : : NodeSeconds nTimeExpire;
86 : : };
87 : :
88 : : std::vector<OrphanTxBase> GetOrphanTransactions() const;
89 : :
90 : : protected:
91 [ - + ]: 1656 : struct OrphanTx : public OrphanTxBase {
92 : : size_t list_pos;
93 : : };
94 : :
95 : : /** Map from wtxid to orphan transaction record. Limited by
96 : : * -maxorphantx/DEFAULT_MAX_ORPHAN_TRANSACTIONS */
97 : : std::map<Wtxid, OrphanTx> m_orphans;
98 : :
99 : : /** Which peer provided the orphans that need to be reconsidered */
100 : : std::map<NodeId, std::set<Wtxid>> m_peer_work_set;
101 : :
102 : : using OrphanMap = decltype(m_orphans);
103 : :
104 : : struct IteratorComparator
105 : : {
106 : : template<typename I>
107 [ + - - + : 611 : bool operator()(const I& a, const I& b) const
+ + + + +
+ + + +
- ]
108 : : {
109 [ + - - + : 611 : return a->first < b->first;
+ + + + +
+ + + +
- ]
110 : : }
111 : : };
112 : :
113 : : /** Index from the parents' COutPoint into the m_orphans. Used
114 : : * to remove orphan transactions from the m_orphans */
115 : : std::map<COutPoint, std::set<OrphanMap::iterator, IteratorComparator>> m_outpoint_to_orphan_it;
116 : :
117 : : /** Orphan transactions in vector for quick random eviction */
118 : : std::vector<OrphanMap::iterator> m_orphan_list;
119 : :
120 : : /** Timestamp for the next scheduled sweep of expired orphans */
121 : : NodeSeconds m_next_sweep{0s};
122 : : };
123 : :
124 : : #endif // BITCOIN_TXORPHANAGE_H
|