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