Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-present The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 : :
6 : : #ifndef BITCOIN_NET_PROCESSING_H
7 : : #define BITCOIN_NET_PROCESSING_H
8 : :
9 : : #include <consensus/amount.h>
10 : : #include <net.h>
11 : : #include <node/txorphanage.h>
12 : : #include <node/types.h>
13 : : #include <private_broadcast.h>
14 : : #include <protocol.h>
15 : : #include <uint256.h>
16 : : #include <util/expected.h>
17 : : #include <validationinterface.h>
18 : :
19 : : #include <atomic>
20 : : #include <chrono>
21 : : #include <cstdint>
22 : : #include <functional>
23 : : #include <memory>
24 : : #include <optional>
25 : : #include <string>
26 : : #include <vector>
27 : :
28 : : class AddrMan;
29 : : class BlockFilterIndex;
30 : : enum class BlockFilterType : uint8_t;
31 : : class CTxMemPool;
32 : : class ChainstateManager;
33 : : class BanMan;
34 : : class CBlockIndex;
35 : : class CScheduler;
36 : : class DataStream;
37 : : class uint256;
38 : :
39 : : namespace node {
40 : : class Warnings;
41 : : } // namespace node
42 : :
43 : : /** Whether transaction reconciliation protocol should be enabled by default. */
44 : : inline constexpr bool DEFAULT_TXRECONCILIATION_ENABLE{false};
45 : : /** Default number of non-mempool transactions to keep around for block reconstruction. Includes
46 : : orphan, replaced, and rejected transactions. */
47 : : inline constexpr uint32_t DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN{100};
48 : : /** Default maximum per-second rate for sending transaction inventory to peers. */
49 : : inline constexpr unsigned int DEFAULT_TX_SEND_RATE{14};
50 : : inline constexpr bool DEFAULT_PEERBLOOMFILTERS = false;
51 : : inline constexpr bool DEFAULT_PEERBLOCKFILTERS = false;
52 : : /** Maximum number of outstanding CMPCTBLOCK requests for the same block. */
53 : : inline constexpr unsigned int MAX_CMPCTBLOCKS_INFLIGHT_PER_BLOCK = 3;
54 : : /** Number of headers sent in one getheaders result. We rely on the assumption that if a peer sends
55 : : * less than this number, we reached its tip. Changing this value is a protocol upgrade. */
56 : : inline constexpr unsigned int MAX_HEADERS_RESULTS = 2000;
57 : : /** The compactblocks version we support. See BIP 152. */
58 : : inline constexpr uint64_t CMPCTBLOCKS_VERSION{2};
59 : :
60 [ + - ]: 37830 : struct CNodeStateStats {
61 : : int nSyncHeight = -1;
62 : : int nCommonHeight = -1;
63 : : NodeClock::duration m_ping_wait;
64 : : std::vector<int> vHeightInFlight;
65 : : bool m_relay_txs;
66 : : int m_inv_to_send = 0;
67 : : uint64_t m_last_inv_seq{0};
68 : : CAmount m_fee_filter_received;
69 : : uint64_t m_addr_processed = 0;
70 : : uint64_t m_addr_rate_limited = 0;
71 : : bool m_addr_relay_enabled{false};
72 : : ServiceFlags their_services;
73 : : int64_t presync_height{-1};
74 : : std::chrono::seconds time_offset{0};
75 : : NodeClock::time_point m_last_block_announcement;
76 : : };
77 : :
78 : : struct PeerManagerInfo {
79 : : struct InvBucketInfo {
80 : : size_t backlog_count{0};
81 : : double count_bucket{0};
82 : : double size_bucket{0};
83 : : };
84 : :
85 : : std::chrono::seconds median_outbound_time_offset{0s};
86 : : bool ignores_incoming_txs{false};
87 : : bool private_broadcast{DEFAULT_PRIVATE_BROADCAST};
88 : : unsigned int tx_send_rate{0};
89 : : InvBucketInfo inbound_bucket;
90 : : InvBucketInfo outbound_bucket;
91 : : };
92 : :
93 : 1369 : class PeerManager : public CValidationInterface, public NetEventsInterface
94 : : {
95 : : public:
96 [ + - # # ]: 4159 : struct Options {
[ + - + - ]
97 : : //! Whether this node is running in -blocksonly mode
98 : : bool ignore_incoming_txs{DEFAULT_BLOCKSONLY};
99 : : //! Whether transaction reconciliation protocol is enabled
100 : : bool reconcile_txs{DEFAULT_TXRECONCILIATION_ENABLE};
101 : : //! Number of non-mempool transactions to keep around for block reconstruction. Includes
102 : : //! orphan, replaced, and rejected transactions.
103 : : uint32_t max_extra_txs{DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN};
104 : : //! Whether all P2P messages are captured to disk
105 : : bool capture_messages{false};
106 : : //! Whether or not the internal RNG behaves deterministically (this is
107 : : //! a test-only option).
108 : : bool deterministic_rng{false};
109 : : //! Number of headers sent in one getheaders message result (this is
110 : : //! a test-only option).
111 : : uint32_t max_headers_result{MAX_HEADERS_RESULTS};
112 : : //! Whether private broadcast is used for sending transactions.
113 : : bool private_broadcast{DEFAULT_PRIVATE_BROADCAST};
114 : : //! Maximum per-second rate for sending transaction inventory to peers.
115 : : unsigned int tx_send_rate{DEFAULT_TX_SEND_RATE};
116 : : //! Return the running block filter index for a filter type, if enabled.
117 : : std::function<BlockFilterIndex*(BlockFilterType)> get_block_filter_index{[](BlockFilterType) { return nullptr; }};
118 : : };
119 : :
120 : : static std::unique_ptr<PeerManager> make(CConnman& connman, AddrMan& addrman,
121 : : BanMan* banman, ChainstateManager& chainman,
122 : : CTxMemPool& pool, node::Warnings& warnings, Options opts);
123 : 0 : virtual ~PeerManager() = default;
124 : :
125 : : /**
126 : : * Attempt to manually fetch block from a given peer. We must already have the header.
127 : : *
128 : : * @param[in] peer_id The peer id
129 : : * @param[in] block_index The blockindex
130 : : */
131 : : virtual util::Expected<void, std::string> FetchBlock(NodeId peer_id, const CBlockIndex& block_index) = 0;
132 : :
133 : : /** Begin running background tasks, should only be called once */
134 : : virtual void StartScheduledTasks(CScheduler& scheduler) = 0;
135 : :
136 : : /** Get statistics from node state */
137 : : virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const = 0;
138 : :
139 : : virtual std::vector<node::TxOrphanage::OrphanInfo> GetOrphanTransactions() = 0;
140 : :
141 : : /** Get peer manager info. */
142 : : virtual PeerManagerInfo GetInfo() const = 0;
143 : :
144 : : /** Get info about transactions currently being privately broadcast. */
145 : : virtual std::vector<PrivateBroadcast::TxBroadcastInfo> GetPrivateBroadcastInfo() const = 0;
146 : :
147 : : /**
148 : : * Abort private broadcast attempts for transactions currently being privately broadcast.
149 : : *
150 : : * @param[in] id A transaction identifier. It will be matched against both txid and wtxid for
151 : : * all transactions in the private broadcast queue.
152 : : *
153 : : * @return Transactions removed from the private broadcast queue. If the provided id matches a
154 : : * txid that corresponds to multiple transactions with different wtxids, multiple
155 : : * transactions may be returned.
156 : : */
157 : : virtual std::vector<CTransactionRef> AbortPrivateBroadcast(const uint256& id) = 0;
158 : :
159 : : /**
160 : : * Initiate a transaction broadcast to eligible peers.
161 : : * Queue the witness transaction id to the inbound and outbound inv backlogs.
162 : : * Later, depending on `-txsendrate`, `Peer::TxRelay::m_next_inv_send_time` and if
163 : : * the transaction is in the mempool, an `INV` about it may be sent to the peer.
164 : : */
165 : : virtual void InitiateTxBroadcastToAll(const Wtxid& wtxid) = 0;
166 : :
167 : : /**
168 : : * Initiate a private transaction broadcast. This is done
169 : : * asynchronously via short-lived connections to peers on privacy networks.
170 : : * @retval node::TransactionError::OK The transaction is scheduled for private broadcast (or was already scheduled).
171 : : * @retval node::TransactionError::PRIVATE_BROADCAST_FULL Rejected because the private broadcast queue is full.
172 : : */
173 : : [[nodiscard]] virtual node::TransactionError InitiateTxBroadcastPrivate(const CTransactionRef& tx) = 0;
174 : :
175 : : /** Send ping message to all peers */
176 : : virtual void SendPings() = 0;
177 : :
178 : : /** Set the height of the best block and its time (seconds since epoch). */
179 : : virtual void SetBestBlock(int height, std::chrono::seconds time) = 0;
180 : :
181 : : /* Public for unit testing. */
182 : : virtual void UnitTestMisbehaving(NodeId peer_id) = 0;
183 : :
184 : : /**
185 : : * Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound.
186 : : * Public for unit testing.
187 : : */
188 : : virtual void CheckForStaleTipAndEvictPeers() = 0;
189 : :
190 : : /** This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp */
191 : : virtual void UpdateLastBlockAnnounceTime(NodeId node, NodeClock::time_point time) = 0;
192 : :
193 : : /**
194 : : * Gets the set of service flags which are "desirable" for a given peer.
195 : : *
196 : : * These are the flags which are required for a peer to support for them
197 : : * to be "interesting" to us, ie for us to wish to use one of our few
198 : : * outbound connection slots for or for us to wish to prioritize keeping
199 : : * their connection around.
200 : : *
201 : : * Relevant service flags may be peer- and state-specific in that the
202 : : * version of the peer may determine which flags are required (eg in the
203 : : * case of NODE_NETWORK_LIMITED where we seek out NODE_NETWORK peers
204 : : * unless they set NODE_NETWORK_LIMITED and we are out of IBD, in which
205 : : * case NODE_NETWORK_LIMITED suffices).
206 : : *
207 : : * Thus, generally, avoid calling with 'services' == NODE_NONE, unless
208 : : * state-specific flags must absolutely be avoided. When called with
209 : : * 'services' == NODE_NONE, the returned desirable service flags are
210 : : * guaranteed to not change dependent on state - ie they are suitable for
211 : : * use when describing peers which we know to be desirable, but for which
212 : : * we do not have a confirmed set of service flags.
213 : : */
214 : : virtual ServiceFlags GetDesirableServiceFlags(ServiceFlags services) const = 0;
215 : : };
216 : :
217 : : #endif // BITCOIN_NET_PROCESSING_H
|