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 : : /** Check if we already have an orphan transaction (by wtxid only) */
34 : : bool HaveTx(const Wtxid& wtxid) const;
35 : :
36 : : /** Extract a transaction from a peer's work set
37 : : * Returns nullptr if there are no transactions to work on.
38 : : * Otherwise returns the transaction reference, and removes
39 : : * it from the work set.
40 : : */
41 : : CTransactionRef GetTxToReconsider(NodeId peer);
42 : :
43 : : /** Erase an orphan by wtxid */
44 : : int EraseTx(const Wtxid& wtxid);
45 : :
46 : : /** Erase all orphans announced by a peer (eg, after that peer disconnects) */
47 : : void EraseForPeer(NodeId peer);
48 : :
49 : : /** Erase all orphans included in or invalidated by a new block */
50 : : void EraseForBlock(const CBlock& block);
51 : :
52 : : /** Limit the orphanage to the given maximum */
53 : : void LimitOrphans(unsigned int max_orphans, FastRandomContext& rng);
54 : :
55 : : /** Add any orphans that list a particular tx as a parent into the from peer's work set */
56 : : void AddChildrenToWorkSet(const CTransaction& tx);
57 : :
58 : : /** Does this peer have any work to do? */
59 : : bool HaveTxToReconsider(NodeId peer);
60 : :
61 : : /** Get all children that spend from this tx and were received from nodeid. Sorted from most
62 : : * recent to least recent. */
63 : : std::vector<CTransactionRef> GetChildrenFromSamePeer(const CTransactionRef& parent, NodeId nodeid) const;
64 : :
65 : : /** Get all children that spend from this tx but were not received from nodeid. Also return
66 : : * which peer provided each tx. */
67 : : std::vector<std::pair<CTransactionRef, NodeId>> GetChildrenFromDifferentPeer(const CTransactionRef& parent, NodeId nodeid) const;
68 : :
69 : : /** Return how many entries exist in the orphange */
70 : 879 : size_t Size() const
71 : : {
72 [ - + ]: 879 : return m_orphans.size();
73 : : }
74 : :
75 : : /** Allows providing orphan information externally */
76 [ - - - - : 41936 : struct OrphanTxBase {
- + - - ]
[ + - ]
77 : : CTransactionRef tx;
78 : : NodeId fromPeer;
79 : : NodeSeconds nTimeExpire;
80 : : };
81 : :
82 : : std::vector<OrphanTxBase> GetOrphanTransactions() const;
83 : :
84 : : protected:
85 [ + - ][ - - : 1560 : struct OrphanTx : public OrphanTxBase {
- + - + -
- ]
86 : : size_t list_pos;
87 : : };
88 : :
89 : : /** Map from wtxid to orphan transaction record. Limited by
90 : : * -maxorphantx/DEFAULT_MAX_ORPHAN_TRANSACTIONS */
91 : : std::map<Wtxid, OrphanTx> m_orphans;
92 : :
93 : : /** Which peer provided the orphans that need to be reconsidered */
94 : : std::map<NodeId, std::set<Wtxid>> m_peer_work_set;
95 : :
96 : : using OrphanMap = decltype(m_orphans);
97 : :
98 : : struct IteratorComparator
99 : : {
100 : : template<typename I>
101 [ - - - - : 525 : bool operator()(const I& a, const I& b) const
- + - - -
- - - - -
- - - - -
- - - - +
+ - - + +
+ + + + +
+ + + - ]
102 : : {
103 [ - - - - : 525 : return a->first < b->first;
- + - - -
- - - - -
- - - - -
- - - - +
+ - - + +
+ + + + +
+ + + - ]
104 : : }
105 : : };
106 : :
107 : : /** Index from the parents' COutPoint into the m_orphans. Used
108 : : * to remove orphan transactions from the m_orphans */
109 : : std::map<COutPoint, std::set<OrphanMap::iterator, IteratorComparator>> m_outpoint_to_orphan_it;
110 : :
111 : : /** Orphan transactions in vector for quick random eviction */
112 : : std::vector<OrphanMap::iterator> m_orphan_list;
113 : :
114 : : /** Timestamp for the next scheduled sweep of expired orphans */
115 : : NodeSeconds m_next_sweep{0s};
116 : : };
117 : :
118 : : #endif // BITCOIN_TXORPHANAGE_H
|