Bitcoin Core 28.99.0
P2P Digital Currency
txorphanage.h
Go to the documentation of this file.
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>
11#include <sync.h>
12#include <util/time.h>
13
14#include <map>
15#include <set>
16
18static constexpr auto ORPHAN_TX_EXPIRE_TIME{20min};
20static constexpr auto ORPHAN_TX_EXPIRE_INTERVAL{5min};
21
29public:
31 bool AddTx(const CTransactionRef& tx, NodeId peer);
32
34 bool AddAnnouncer(const Wtxid& wtxid, NodeId peer);
35
36 CTransactionRef GetTx(const Wtxid& wtxid) const;
37
39 bool HaveTx(const Wtxid& wtxid) const;
40
42 bool HaveTxFromPeer(const Wtxid& wtxid, NodeId peer) const;
43
50
52 int EraseTx(const Wtxid& wtxid);
53
56 void EraseForPeer(NodeId peer);
57
59 void EraseForBlock(const CBlock& block);
60
62 void LimitOrphans(unsigned int max_orphans, FastRandomContext& rng);
63
65 void AddChildrenToWorkSet(const CTransaction& tx);
66
68 bool HaveTxToReconsider(NodeId peer);
69
72 std::vector<CTransactionRef> GetChildrenFromSamePeer(const CTransactionRef& parent, NodeId nodeid) const;
73
75 size_t Size() const
76 {
77 return m_orphans.size();
78 }
79
81 struct OrphanTxBase {
84 std::set<NodeId> announcers;
86 };
87
88 std::vector<OrphanTxBase> GetOrphanTransactions() const;
89
90protected:
91 struct OrphanTx : public OrphanTxBase {
92 size_t list_pos;
93 };
94
97 std::map<Wtxid, OrphanTx> m_orphans;
98
100 std::map<NodeId, std::set<Wtxid>> m_peer_work_set;
101
102 using OrphanMap = decltype(m_orphans);
103
105 {
106 template<typename I>
107 bool operator()(const I& a, const I& b) const
108 {
109 return a->first < b->first;
110 }
111 };
112
115 std::map<COutPoint, std::set<OrphanMap::iterator, IteratorComparator>> m_outpoint_to_orphan_it;
116
118 std::vector<OrphanMap::iterator> m_orphan_list;
119
122};
123
124#endif // BITCOIN_TXORPHANAGE_H
Definition: block.h:69
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:296
Fast randomness source.
Definition: random.h:377
A class to track orphan transactions (failed on TX_MISSING_INPUTS) Since we cannot distinguish orphan...
Definition: txorphanage.h:28
bool AddTx(const CTransactionRef &tx, NodeId peer)
Add a new orphan transaction.
Definition: txorphanage.cpp:15
void EraseForPeer(NodeId peer)
Maybe erase all orphans announced by a peer (eg, after that peer disconnects).
void AddChildrenToWorkSet(const CTransaction &tx)
Add any orphans that list a particular tx as a parent into the from peer's work set.
std::vector< OrphanTxBase > GetOrphanTransactions() const
decltype(m_orphans) OrphanMap
Definition: txorphanage.h:102
void LimitOrphans(unsigned int max_orphans, FastRandomContext &rng)
Limit the orphanage to the given maximum.
int EraseTx(const Wtxid &wtxid)
Erase an orphan by wtxid.
Definition: txorphanage.cpp:65
void EraseForBlock(const CBlock &block)
Erase all orphans included in or invalidated by a new block.
std::map< NodeId, std::set< Wtxid > > m_peer_work_set
Which peer provided the orphans that need to be reconsidered.
Definition: txorphanage.h:100
std::map< COutPoint, std::set< OrphanMap::iterator, IteratorComparator > > m_outpoint_to_orphan_it
Index from the parents' COutPoint into the m_orphans.
Definition: txorphanage.h:115
size_t Size() const
Return how many entries exist in the orphange.
Definition: txorphanage.h:75
std::vector< OrphanMap::iterator > m_orphan_list
Orphan transactions in vector for quick random eviction.
Definition: txorphanage.h:118
CTransactionRef GetTx(const Wtxid &wtxid) const
bool AddAnnouncer(const Wtxid &wtxid, NodeId peer)
Add an additional announcer to an orphan if it exists.
Definition: txorphanage.cpp:51
std::vector< CTransactionRef > GetChildrenFromSamePeer(const CTransactionRef &parent, NodeId nodeid) const
Get all children that spend from this tx and were received from nodeid.
std::map< Wtxid, OrphanTx > m_orphans
Map from wtxid to orphan transaction record.
Definition: txorphanage.h:97
bool HaveTx(const Wtxid &wtxid) const
Check if we already have an orphan transaction (by wtxid only)
bool HaveTxToReconsider(NodeId peer)
Does this peer have any work to do?
CTransactionRef GetTxToReconsider(NodeId peer)
Extract a transaction from a peer's work set Returns nullptr if there are no transactions to work on.
NodeSeconds m_next_sweep
Timestamp for the next scheduled sweep of expired orphans.
Definition: txorphanage.h:121
bool HaveTxFromPeer(const Wtxid &wtxid, NodeId peer) const
Check if a {tx, peer} exists in the orphanage.
transaction_identifier represents the two canonical transaction identifier types (txid,...
int64_t NodeId
Definition: net.h:97
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
bool operator()(const I &a, const I &b) const
Definition: txorphanage.h:107
Allows providing orphan information externally.
Definition: txorphanage.h:81
CTransactionRef tx
Definition: txorphanage.h:82
std::set< NodeId > announcers
Peers added with AddTx or AddAnnouncer.
Definition: txorphanage.h:84
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25
static constexpr auto ORPHAN_TX_EXPIRE_TIME
Expiration time for orphan transactions.
Definition: txorphanage.h:18
static constexpr auto ORPHAN_TX_EXPIRE_INTERVAL
Minimum time between orphan transactions expire time checks.
Definition: txorphanage.h:20