Bitcoin Core 29.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_NODE_TXORPHANAGE_H
6#define BITCOIN_NODE_TXORPHANAGE_H
7
9#include <net.h>
10#include <primitives/block.h>
12#include <sync.h>
13#include <util/time.h>
14
15#include <map>
16#include <set>
17
18namespace node {
20static constexpr int64_t DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER{404'000};
23static constexpr unsigned int DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE{3000};
24
39public:
40 using Usage = int64_t;
41 using Count = unsigned int;
42
44 struct OrphanInfo {
47 std::set<NodeId> announcers;
48
49 // Constructor with moved announcers
50 OrphanInfo(CTransactionRef tx, std::set<NodeId>&& announcers) :
51 tx(std::move(tx)),
53 {}
54 };
55
56 virtual ~TxOrphanage() = default;
57
59 virtual bool AddTx(const CTransactionRef& tx, NodeId peer) = 0;
60
62 virtual bool AddAnnouncer(const Wtxid& wtxid, NodeId peer) = 0;
63
65 virtual CTransactionRef GetTx(const Wtxid& wtxid) const = 0;
66
68 virtual bool HaveTx(const Wtxid& wtxid) const = 0;
69
71 virtual bool HaveTxFromPeer(const Wtxid& wtxid, NodeId peer) const = 0;
72
79
82 virtual bool EraseTx(const Wtxid& wtxid) = 0;
83
86 virtual void EraseForPeer(NodeId peer) = 0;
87
89 virtual void EraseForBlock(const CBlock& block) = 0;
90
92 virtual std::vector<std::pair<Wtxid, NodeId>> AddChildrenToWorkSet(const CTransaction& tx, FastRandomContext& rng) = 0;
93
95 virtual bool HaveTxToReconsider(NodeId peer) = 0;
96
99 virtual std::vector<CTransactionRef> GetChildrenFromSamePeer(const CTransactionRef& parent, NodeId nodeid) const = 0;
100
102 virtual std::vector<OrphanInfo> GetOrphanTransactions() const = 0;
103
106 virtual Usage TotalOrphanUsage() const = 0;
107
112 virtual Usage UsageByPeer(NodeId peer) const = 0;
113
116 virtual void SanityCheck() const = 0;
117
120 virtual Count CountAnnouncements() const = 0;
121
123 virtual Count CountUniqueOrphans() const = 0;
124
126 virtual Count AnnouncementsFromPeer(NodeId peer) const = 0;
127
129 virtual Count LatencyScoreFromPeer(NodeId peer) const = 0;
130
132 virtual Count MaxGlobalLatencyScore() const = 0;
133
135 virtual Count TotalLatencyScore() const = 0;
136
138 virtual Usage ReservedPeerUsage() const = 0;
139
141 virtual Count MaxPeerLatencyScore() const = 0;
142
144 virtual Usage MaxGlobalUsage() const = 0;
145};
146
148std::unique_ptr<TxOrphanage> MakeTxOrphanage() noexcept;
149std::unique_ptr<TxOrphanage> MakeTxOrphanage(TxOrphanage::Count max_global_latency_score, TxOrphanage::Usage reserved_peer_usage) noexcept;
150} // namespace node
151#endif // BITCOIN_NODE_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:386
A class to track orphan transactions (failed on TX_MISSING_INPUTS) Since we cannot distinguish orphan...
Definition: txorphanage.h:38
virtual void SanityCheck() const =0
Check consistency between PeerOrphanInfo and m_orphans.
virtual Count CountAnnouncements() const =0
Number of announcements, i.e.
virtual std::vector< CTransactionRef > GetChildrenFromSamePeer(const CTransactionRef &parent, NodeId nodeid) const =0
Get all children that spend from this tx and were received from nodeid.
virtual bool HaveTxToReconsider(NodeId peer)=0
Does this peer have any work to do?
virtual bool AddTx(const CTransactionRef &tx, NodeId peer)=0
Add a new orphan transaction.
virtual Usage MaxGlobalUsage() const =0
Get the maximum global usage allowed.
virtual Count AnnouncementsFromPeer(NodeId peer) const =0
Number of orphans stored from this peer.
virtual Count CountUniqueOrphans() const =0
Number of unique orphans (by wtxid).
virtual std::vector< OrphanInfo > GetOrphanTransactions() const =0
Get all orphan transactions.
virtual Usage TotalOrphanUsage() const =0
Get the total usage (weight) of all orphans.
virtual void EraseForBlock(const CBlock &block)=0
Erase all orphans included in or invalidated by a new block.
virtual Count TotalLatencyScore() const =0
Get the total latency score of all orphans.
virtual ~TxOrphanage()=default
virtual std::vector< std::pair< Wtxid, NodeId > > AddChildrenToWorkSet(const CTransaction &tx, FastRandomContext &rng)=0
Add any orphans that list a particular tx as a parent into the from peer's work set.
virtual Usage UsageByPeer(NodeId peer) const =0
Total usage (weight) of orphans for which this peer is an announcer.
unsigned int Count
Definition: txorphanage.h:41
virtual bool AddAnnouncer(const Wtxid &wtxid, NodeId peer)=0
Add an additional announcer to an orphan if it exists.
virtual Count MaxGlobalLatencyScore() const =0
Get the maximum global latency score allowed.
virtual CTransactionRef GetTx(const Wtxid &wtxid) const =0
Get a transaction by its witness txid.
virtual Count MaxPeerLatencyScore() const =0
Get the maximum latency score allowed per peer.
virtual bool HaveTxFromPeer(const Wtxid &wtxid, NodeId peer) const =0
Check if a {tx, peer} exists in the orphanage.
virtual void EraseForPeer(NodeId peer)=0
Maybe erase all orphans announced by a peer (eg, after that peer disconnects).
virtual CTransactionRef GetTxToReconsider(NodeId peer)=0
Extract a transaction from a peer's work set, and flip it back to non-reconsiderable.
virtual Usage ReservedPeerUsage() const =0
Get the reserved usage per peer.
virtual bool HaveTx(const Wtxid &wtxid) const =0
Check if we already have an orphan transaction (by wtxid only)
virtual Count LatencyScoreFromPeer(NodeId peer) const =0
Latency score of transactions announced by this peer.
virtual bool EraseTx(const Wtxid &wtxid)=0
Erase an orphan by wtxid, including all announcements if there are multiple.
transaction_identifier represents the two canonical transaction identifier types (txid,...
Definition: messages.h:20
std::unique_ptr< TxOrphanage > MakeTxOrphanage() noexcept
Create a new TxOrphanage instance.
static constexpr unsigned int DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE
Default value for TxOrphanage::m_max_global_latency_score.
Definition: txorphanage.h:23
static constexpr int64_t DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER
Default value for TxOrphanage::m_reserved_usage_per_peer.
Definition: txorphanage.h:20
int64_t NodeId
Definition: net.h:98
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
Allows providing orphan information externally.
Definition: txorphanage.h:44
std::set< NodeId > announcers
Peers added with AddTx or AddAnnouncer.
Definition: txorphanage.h:47
OrphanInfo(CTransactionRef tx, std::set< NodeId > &&announcers)
Definition: txorphanage.h:50