Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2021 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 : #include <net_processing.h>
7 :
8 : #include <addrman.h>
9 : #include <banman.h>
10 : #include <blockencodings.h>
11 : #include <blockfilter.h>
12 : #include <chainparams.h>
13 : #include <consensus/amount.h>
14 : #include <consensus/validation.h>
15 : #include <hash.h>
16 : #include <index/blockfilterindex.h>
17 : #include <index/txindex.h>
18 : #include <merkleblock.h>
19 : #include <net_types.h>
20 : #include <netbase.h>
21 : #include <netmessagemaker.h>
22 : #include <node/blockstorage.h>
23 : #include <node/txreconciliation.h>
24 : #include <policy/policy.h>
25 : #include <policy/settings.h>
26 : #include <primitives/block.h>
27 : #include <primitives/transaction.h>
28 : #include <random.h>
29 : #include <scheduler.h>
30 : #include <streams.h>
31 : #include <sync.h>
32 : #include <timedata.h>
33 : #include <tinyformat.h>
34 : #include <txmempool.h>
35 : #include <txorphanage.h>
36 : #include <util/check.h>
37 : #include <util/std23.h>
38 : #include <util/strencodings.h>
39 : #include <util/system.h>
40 : #include <util/trace.h>
41 : #include <validation.h>
42 :
43 : #include <chainlock/chainlock.h>
44 : #include <chainlock/handler.h>
45 : #include <coinjoin/coinjoin.h>
46 : #include <coinjoin/walletman.h>
47 : #include <evo/deterministicmns.h>
48 : #include <evo/mnauth.h>
49 : #include <evo/smldiff.h>
50 : #include <instantsend/instantsend.h>
51 : #include <instantsend/lock.h>
52 : #include <llmq/blockprocessor.h>
53 : #include <llmq/commitment.h>
54 : #include <llmq/context.h>
55 : #include <llmq/options.h>
56 : #include <llmq/quorumsman.h>
57 : #include <llmq/signhash.h>
58 : #include <llmq/signing.h>
59 : #include <llmq/snapshot.h>
60 : #include <masternode/meta.h>
61 : #include <masternode/sync.h>
62 : #include <msg_result.h>
63 : #include <spork.h>
64 : #include <stats/client.h>
65 :
66 : #include <algorithm>
67 : #include <atomic>
68 : #include <chrono>
69 : #include <future>
70 : #include <list>
71 : #include <memory>
72 : #include <optional>
73 : #include <ranges>
74 : #include <typeinfo>
75 : #include <vector>
76 :
77 : using node::ReadBlockFromDisk;
78 : using node::fImporting;
79 : using node::fPruneMode;
80 : using node::fReindex;
81 :
82 : /** Maximum number of in-flight objects from a peer */
83 : static constexpr int32_t MAX_PEER_OBJECT_IN_FLIGHT = 100;
84 : /** Maximum number of announced objects from a peer */
85 : static constexpr int32_t MAX_PEER_OBJECT_ANNOUNCEMENTS = 2 * MAX_INV_SZ;
86 : /** How many microseconds to delay requesting transactions from inbound peers */
87 : static constexpr auto INBOUND_PEER_TX_DELAY{2s};
88 : /** How long to wait before downloading a transaction from an additional peer */
89 : static constexpr auto GETDATA_TX_INTERVAL{60s};
90 : /** Maximum delay for transaction requests to avoid biasing some peers over others. */
91 : static constexpr auto MAX_GETDATA_RANDOM_DELAY{2s};
92 : /** How long to wait (expiry * factor microseconds) before expiring an in-flight getdata request to a peer */
93 : static constexpr int64_t TX_EXPIRY_INTERVAL_FACTOR = 10;
94 : static_assert(INBOUND_PEER_TX_DELAY >= MAX_GETDATA_RANDOM_DELAY,
95 : "To preserve security, MAX_GETDATA_RANDOM_DELAY should not exceed INBOUND_PEER_DELAY");
96 : /** Limit to avoid sending big packets. Not used in processing incoming GETDATA for compatibility */
97 : static const unsigned int MAX_GETDATA_SZ = 1000;
98 :
99 : /** How long to cache transactions in mapRelay for normal relay */
100 : static constexpr auto RELAY_TX_CACHE_TIME = 15min;
101 : /** How long a transaction has to be in the mempool before it can unconditionally be relayed (even when not in mapRelay). */
102 : static constexpr auto UNCONDITIONAL_RELAY_DELAY = 2min;
103 : /** Headers download timeout.
104 : * Timeout = base + per_header * (expected number of headers) */
105 : static constexpr auto HEADERS_DOWNLOAD_TIMEOUT_BASE = 15min;
106 : static constexpr auto HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER = 1ms;
107 : /** How long to wait for a peer to respond to a getheaders request */
108 : static constexpr auto HEADERS_RESPONSE_TIME{2min};
109 : /** Protect at least this many outbound peers from disconnection due to slow/
110 : * behind headers chain.
111 : */
112 : static constexpr int32_t MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT = 4;
113 : /** Timeout for (unprotected) outbound peers to sync to our chainwork */
114 : static constexpr auto CHAIN_SYNC_TIMEOUT{20min};
115 : /** How frequently to check for stale tips */
116 : static constexpr auto STALE_CHECK_INTERVAL{150s}; // 2.5 minutes (~block interval)
117 : /** How frequently to check for extra outbound peers and disconnect */
118 : static constexpr auto EXTRA_PEER_CHECK_INTERVAL{45s};
119 : /** Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict */
120 : static constexpr auto MINIMUM_CONNECT_TIME{30s};
121 : /** SHA256("main address relay")[0:8] */
122 : static constexpr uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL;
123 : /// Age after which a stale block will no longer be served if requested as
124 : /// protection against fingerprinting. Set to one month, denominated in seconds.
125 : static constexpr int STALE_RELAY_AGE_LIMIT = 30 * 24 * 60 * 60;
126 : /// Age after which a block is considered historical for purposes of rate
127 : /// limiting block relay. Set to one week, denominated in seconds.
128 : static constexpr int HISTORICAL_BLOCK_AGE = 7 * 24 * 60 * 60;
129 : /** Time between pings automatically sent out for latency probing and keepalive */
130 : static constexpr auto PING_INTERVAL{2min};
131 : /** The maximum number of entries in a locator */
132 : static const unsigned int MAX_LOCATOR_SZ = 101;
133 : /** Number of blocks that can be requested at any given time from a single peer. */
134 : static const int MAX_BLOCKS_IN_TRANSIT_PER_PEER = 16;
135 : /** Default time during which a peer must stall block download progress before being disconnected.
136 : * the actual timeout is increased temporarily if peers are disconnected for hitting the timeout */
137 : static constexpr auto BLOCK_STALLING_TIMEOUT_DEFAULT{2s};
138 : /** Maximum timeout for stalling block download. */
139 : static constexpr auto BLOCK_STALLING_TIMEOUT_MAX{64s};
140 : /** Maximum depth of blocks we're willing to serve as compact blocks to peers
141 : * when requested. For older blocks, a regular BLOCK response will be sent. */
142 : static const int MAX_CMPCTBLOCK_DEPTH = 5;
143 : /** Maximum depth of blocks we're willing to respond to GETBLOCKTXN requests for. */
144 : static const int MAX_BLOCKTXN_DEPTH = 10;
145 : /** Size of the "block download window": how far ahead of our current height do we fetch?
146 : * Larger windows tolerate larger download speed differences between peer, but increase the potential
147 : * degree of disordering of blocks on disk (which make reindexing and pruning harder). We'll probably
148 : * want to make this a per-peer adaptive value at some point. */
149 : static const unsigned int BLOCK_DOWNLOAD_WINDOW = 1024;
150 : /** Block download timeout base, expressed in multiples of the block interval (i.e. 10 min) */
151 : static constexpr double BLOCK_DOWNLOAD_TIMEOUT_BASE = 1;
152 : /** Additional block download timeout per parallel downloading peer (i.e. 5 min) */
153 : static constexpr double BLOCK_DOWNLOAD_TIMEOUT_PER_PEER = 0.5;
154 : /** Maximum number of headers to announce when relaying blocks with headers message.*/
155 : static const unsigned int MAX_BLOCKS_TO_ANNOUNCE = 8;
156 : /** Maximum number of unconnecting headers announcements before DoS score */
157 : static const int MAX_UNCONNECTING_HEADERS = 10;
158 : /** Minimum blocks required to signal NODE_NETWORK_LIMITED */
159 : static const unsigned int NODE_NETWORK_LIMITED_MIN_BLOCKS = 288;
160 : /** Average delay between local address broadcasts */
161 : static constexpr auto AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL{24h};
162 : /** Average delay between peer address broadcasts */
163 : static constexpr auto AVG_ADDRESS_BROADCAST_INTERVAL{30s};
164 : /** Delay between rotating the peers we relay a particular address to */
165 : static constexpr auto ROTATE_ADDR_RELAY_DEST_INTERVAL{24h};
166 : /** Average delay between trickled inventory transmissions for inbound peers.
167 : * Blocks and peers with NetPermissionFlags::NoBan permission bypass this. */
168 : static constexpr auto INBOUND_INVENTORY_BROADCAST_INTERVAL{5s};
169 : /** Average delay between trickled inventory transmissions for outbound peers.
170 : * Use a smaller delay as there is less privacy concern for them.
171 : * Blocks and peers with NetPermissionFlags::NoBan permission bypass this.
172 : * Masternode outbound peers get half this delay. */
173 : static constexpr auto OUTBOUND_INVENTORY_BROADCAST_INTERVAL{2s};
174 : /** Maximum rate of inventory items to send per second.
175 : * Limits the impact of low-fee transaction floods.
176 : * We have 4 times smaller block times in Dash, so we need to push 4 times more invs per 1MB. */
177 : static constexpr unsigned int INVENTORY_BROADCAST_PER_SECOND = 7;
178 : /** Maximum number of inventory items to send per transmission. */
179 : static constexpr unsigned int INVENTORY_BROADCAST_MAX_PER_1MB_BLOCK = 4 * INVENTORY_BROADCAST_PER_SECOND * count_seconds(INBOUND_INVENTORY_BROADCAST_INTERVAL);
180 : /** The number of most recently announced transactions a peer can request. */
181 : static constexpr unsigned int INVENTORY_MAX_RECENT_RELAY = 3500;
182 : /** Verify that INVENTORY_MAX_RECENT_RELAY is enough to cache everything typically
183 : * relayed before unconditional relay from the mempool kicks in. This is only a
184 : * lower bound, and it should be larger to account for higher inv rate to outbound
185 : * peers, and random variations in the broadcast mechanism. */
186 : static_assert(INVENTORY_MAX_RECENT_RELAY >= INVENTORY_BROADCAST_PER_SECOND * UNCONDITIONAL_RELAY_DELAY / std::chrono::seconds{1}, "INVENTORY_RELAY_MAX too low");
187 : /** Maximum number of compact filters that may be requested with one getcfilters. See BIP 157. */
188 : static constexpr uint32_t MAX_GETCFILTERS_SIZE = 1000;
189 : /** Maximum number of cf hashes that may be requested with one getcfheaders. See BIP 157. */
190 : static constexpr uint32_t MAX_GETCFHEADERS_SIZE = 2000;
191 : /** the maximum percentage of addresses from our addrman to return in response to a getaddr message. */
192 : static constexpr size_t MAX_PCT_ADDR_TO_SEND = 23;
193 : /** The maximum number of address records permitted in an ADDR message. */
194 : static constexpr size_t MAX_ADDR_TO_SEND{1000};
195 : /** The maximum rate of address records we're willing to process on average. Can be bypassed using
196 : * the NetPermissionFlags::Addr permission. */
197 : static constexpr double MAX_ADDR_RATE_PER_SECOND{0.1};
198 : /** The soft limit of the address processing token bucket (the regular MAX_ADDR_RATE_PER_SECOND
199 : * based increments won't go above this, but the MAX_ADDR_TO_SEND increment following GETADDR
200 : * is exempt from this limit). */
201 : static constexpr size_t MAX_ADDR_PROCESSING_TOKEN_BUCKET{MAX_ADDR_TO_SEND};
202 : /** The compactblocks version we support. See BIP 152. */
203 : static constexpr uint64_t CMPCTBLOCKS_VERSION{1};
204 :
205 : // Internal stuff
206 : namespace {
207 : /** Blocks that are in flight, and that are in the queue to be downloaded. */
208 : struct QueuedBlock {
209 : /** BlockIndex. We must have this since we only request blocks when we've already validated the header. */
210 : const CBlockIndex* pindex;
211 : /** Optional, used for CMPCTBLOCK downloads */
212 : std::unique_ptr<PartiallyDownloadedBlock> partialBlock;
213 : };
214 :
215 : /**
216 : * Data structure for an individual peer. This struct is not protected by
217 : * cs_main since it does not contain validation-critical data.
218 : *
219 : * Memory is owned by shared pointers and this object is destructed when
220 : * the refcount drops to zero.
221 : *
222 : * Mutexes inside this struct must not be held when locking m_peer_mutex.
223 : *
224 : * TODO: move most members from CNodeState to this structure.
225 : * TODO: move remaining application-layer data members from CNode to this structure.
226 : */
227 : struct Peer {
228 : /** Same id as the CNode object for this peer */
229 : const NodeId m_id{0};
230 :
231 : /** Services we offered to this peer.
232 : *
233 : * This is supplied by CConnman during peer initialization. It's const
234 : * because there is no protocol defined for renegotiating services
235 : * initially offered to a peer. The set of local services we offer should
236 : * not change after initialization.
237 : *
238 : * An interesting example of this is NODE_NETWORK and initial block
239 : * download: a node which starts up from scratch doesn't have any blocks
240 : * to serve, but still advertises NODE_NETWORK because it will eventually
241 : * fulfill this role after IBD completes. P2P code is written in such a
242 : * way that it can gracefully handle peers who don't make good on their
243 : * service advertisements. */
244 : const ServiceFlags m_our_services;
245 : /** Services this peer offered to us. */
246 9973 : std::atomic<ServiceFlags> m_their_services{NODE_NONE};
247 :
248 : /** Protects misbehavior data members */
249 : Mutex m_misbehavior_mutex;
250 : /** Accumulated misbehavior score for this peer */
251 9973 : int m_misbehavior_score GUARDED_BY(m_misbehavior_mutex){0};
252 : /** Whether this peer should be disconnected and marked as discouraged (unless it has NetPermissionFlags::NoBan permission). */
253 9973 : bool m_should_discourage GUARDED_BY(m_misbehavior_mutex){false};
254 :
255 : /** Protects block inventory data members */
256 : Mutex m_block_inv_mutex;
257 : /** List of blocks that we'll anounce via an `inv` message.
258 : * There is no final sorting before sending, as they are always sent
259 : * immediately and in the order requested. */
260 : std::vector<uint256> m_blocks_for_inv_relay GUARDED_BY(m_block_inv_mutex);
261 : /** Unfiltered list of blocks that we'd like to announce via a `headers`
262 : * message. If we can't announce via a `headers` message, we'll fall back to
263 : * announcing via `inv`. */
264 : std::vector<uint256> m_blocks_for_headers_relay GUARDED_BY(m_block_inv_mutex);
265 :
266 : /** The final block hash that we sent in an `inv` message to this peer.
267 : * When the peer requests this block, we send an `inv` message to trigger
268 : * the peer to request the next sequence of block hashes.
269 : * Most peers use headers-first syncing, which doesn't use this mechanism */
270 9973 : uint256 m_continuation_block GUARDED_BY(m_block_inv_mutex) {};
271 :
272 : /** This peer's reported block height when we connected */
273 9973 : std::atomic<int> m_starting_height{-1};
274 :
275 : /** The pong reply we're expecting, or 0 if no pong expected. */
276 9973 : std::atomic<uint64_t> m_ping_nonce_sent{0};
277 : /** When the last ping was sent, or 0 if no ping was ever sent */
278 9973 : std::atomic<std::chrono::microseconds> m_ping_start{0us};
279 : /** Whether a ping has been requested by the user */
280 9973 : std::atomic<bool> m_ping_queued{false};
281 : /** Whether the peer has requested to receive llmq recovered signatures */
282 9973 : std::atomic<bool> m_wants_recsigs{false};
283 :
284 29919 : struct TxRelay {
285 : mutable RecursiveMutex m_bloom_filter_mutex;
286 : /** Whether we relay transactions to this peer. */
287 9973 : bool m_relay_txs GUARDED_BY(m_bloom_filter_mutex){false};
288 : /** A bloom filter for which transactions to announce to the peer. See BIP37. */
289 9973 : std::unique_ptr<CBloomFilter> m_bloom_filter PT_GUARDED_BY(m_bloom_filter_mutex) GUARDED_BY(m_bloom_filter_mutex){nullptr};
290 :
291 : mutable RecursiveMutex m_tx_inventory_mutex;
292 : /** A filter of all the txids that the peer has announced to
293 : * us or we have announced to the peer. We use this to avoid announcing
294 : * the same txid to a peer that already has the transaction. */
295 9973 : CRollingBloomFilter m_tx_inventory_known_filter GUARDED_BY(m_tx_inventory_mutex){50000, 0.000001};
296 : /** Set of transaction ids we still have to announce. We use the
297 : * mempool to sort transactions in dependency order before relay, so
298 : * this does not have to be sorted. */
299 : std::set<uint256> m_tx_inventory_to_send GUARDED_BY(m_tx_inventory_mutex);
300 : /** List of non-tx/non-block inventory items */
301 : std::vector<CInv> vInventoryOtherToSend GUARDED_BY(m_tx_inventory_mutex);
302 : /** Whether the peer has requested us to send our complete mempool. Only
303 : * permitted if the peer has NetPermissionFlags::Mempool or we advertise
304 : * NODE_BLOOM. See BIP35. */
305 9973 : bool m_send_mempool GUARDED_BY(m_tx_inventory_mutex){false};
306 : /** The last time a BIP35 `mempool` request was serviced. */
307 9973 : std::atomic<std::chrono::seconds> m_last_mempool_req{0s};
308 : /** The next time after which we will send an `inv` message containing
309 : * transaction announcements to this peer. */
310 9973 : std::chrono::microseconds m_next_inv_send_time GUARDED_BY(NetEventsInterface::g_msgproc_mutex){0};
311 : };
312 :
313 : /**
314 : * (Bitcoin) Initializes a TxRelay struct for this peer. Can be called at most once for a peer.
315 : * (Dash) Enables the flag that allows GetTxRelay() to return m_tx_relay */
316 8964 : TxRelay* SetTxRelay() EXCLUSIVE_LOCKS_REQUIRED(!m_tx_relay_mutex)
317 : {
318 8964 : LOCK(m_tx_relay_mutex);
319 8964 : Assume(!m_can_tx_relay);
320 8964 : m_can_tx_relay = true;
321 8964 : return m_tx_relay.get();
322 8964 : };
323 :
324 2997419 : TxRelay* GetInvRelay() EXCLUSIVE_LOCKS_REQUIRED(!m_tx_relay_mutex)
325 : {
326 5994837 : return WITH_LOCK(m_tx_relay_mutex, return m_tx_relay.get());
327 : }
328 :
329 4980926 : TxRelay* GetTxRelay() EXCLUSIVE_LOCKS_REQUIRED(!m_tx_relay_mutex)
330 : {
331 4980926 : LOCK(m_tx_relay_mutex);
332 4980926 : return m_can_tx_relay ? m_tx_relay.get() : nullptr;
333 4980926 : };
334 75 : const TxRelay* GetTxRelay() const EXCLUSIVE_LOCKS_REQUIRED(!m_tx_relay_mutex)
335 : {
336 75 : LOCK(m_tx_relay_mutex);
337 75 : return m_can_tx_relay ? m_tx_relay.get() : nullptr;
338 75 : };
339 :
340 : /** A vector of addresses to send to the peer, limited to MAX_ADDR_TO_SEND. */
341 : std::vector<CAddress> m_addrs_to_send GUARDED_BY(NetEventsInterface::g_msgproc_mutex);
342 : /** Probabilistic filter to track recent addr messages relayed with this
343 : * peer. Used to avoid relaying redundant addresses to this peer.
344 : *
345 : * We initialize this filter for outbound peers (other than
346 : * block-relay-only connections) or when an inbound peer sends us an
347 : * address related message (ADDR, ADDRV2, GETADDR).
348 : *
349 : * Presence of this filter must correlate with m_addr_relay_enabled.
350 : **/
351 : std::unique_ptr<CRollingBloomFilter> m_addr_known GUARDED_BY(NetEventsInterface::g_msgproc_mutex);
352 : /** Whether we are participating in address relay with this connection.
353 : *
354 : * We set this bool to true for outbound peers (other than
355 : * block-relay-only connections), or when an inbound peer sends us an
356 : * address related message (ADDR, ADDRV2, GETADDR).
357 : *
358 : * We use this bool to decide whether a peer is eligible for gossiping
359 : * addr messages. This avoids relaying to peers that are unlikely to
360 : * forward them, effectively blackholing self announcements. Reasons
361 : * peers might support addr relay on the link include that they connected
362 : * to us as a block-relay-only peer or they are a light client.
363 : *
364 : * This field must correlate with whether m_addr_known has been
365 : * initialized.*/
366 9973 : std::atomic_bool m_addr_relay_enabled{false};
367 : /** Whether a getaddr request to this peer is outstanding. */
368 9973 : bool m_getaddr_sent GUARDED_BY(NetEventsInterface::g_msgproc_mutex){false};
369 : /** Guards address sending timers. */
370 : mutable Mutex m_addr_send_times_mutex;
371 : /** Time point to send the next ADDR message to this peer. */
372 9973 : std::chrono::microseconds m_next_addr_send GUARDED_BY(m_addr_send_times_mutex){0};
373 : /** Time point to possibly re-announce our local address to this peer. */
374 9973 : std::chrono::microseconds m_next_local_addr_send GUARDED_BY(m_addr_send_times_mutex){0};
375 : /** Whether the peer has signaled support for receiving ADDRv2 (BIP155)
376 : * messages, indicating a preference to receive ADDRv2 instead of ADDR ones. */
377 9973 : std::atomic_bool m_wants_addrv2{false};
378 :
379 : enum class WantsDSQ {
380 : NONE, // Peer doesn't want DSQs
381 : INV, // Peer will be notified of DSQs over Inventory System (see: DSQ_INV_VERSION)
382 : ALL, // Peer will be notified of all DSQs, by simply sending them the DSQ
383 : };
384 :
385 9973 : std::atomic<WantsDSQ> m_wants_dsq{WantsDSQ::NONE};
386 :
387 : /** Whether this peer has already sent us a getaddr message. */
388 9973 : bool m_getaddr_recvd GUARDED_BY(NetEventsInterface::g_msgproc_mutex){false};
389 : /** Whether this peer has already requested sporks. */
390 9973 : bool m_getsporks_recvd GUARDED_BY(NetEventsInterface::g_msgproc_mutex){false};
391 : /** Hashes of active sporks sent in the last getsporks response. */
392 9973 : std::vector<uint256> m_getsporks_last_response GUARDED_BY(NetEventsInterface::g_msgproc_mutex){};
393 : /** Number of addresses that can be processed from this peer. Start at 1 to
394 : * permit self-announcement. */
395 9973 : double m_addr_token_bucket GUARDED_BY(NetEventsInterface::g_msgproc_mutex){1.0};
396 : /** When m_addr_token_bucket was last updated */
397 9973 : std::chrono::microseconds m_addr_token_timestamp GUARDED_BY(NetEventsInterface::g_msgproc_mutex){GetTime<std::chrono::microseconds>()};
398 : /** Total number of addresses that were dropped due to rate limiting. */
399 9973 : std::atomic<uint64_t> m_addr_rate_limited{0};
400 : /** Total number of addresses that were processed (excludes rate-limited ones). */
401 9973 : std::atomic<uint64_t> m_addr_processed{0};
402 :
403 : /** Whether we've sent this peer a getheaders in response to an inv prior to initial-headers-sync completing */
404 9973 : bool m_inv_triggered_getheaders_before_sync GUARDED_BY(NetEventsInterface::g_msgproc_mutex){false};
405 :
406 : /** Protects m_getdata_requests **/
407 : Mutex m_getdata_requests_mutex;
408 : /** Work queue of items requested by this peer **/
409 : std::deque<CInv> m_getdata_requests GUARDED_BY(m_getdata_requests_mutex);
410 :
411 : /** Time of the last getheaders message to this peer */
412 9973 : NodeClock::time_point m_last_getheaders_timestamp GUARDED_BY(NetEventsInterface::g_msgproc_mutex){};
413 :
414 69811 : explicit Peer(NodeId id, ServiceFlags our_services)
415 9973 : : m_id(id)
416 9973 : , m_our_services{our_services}
417 19946 : {}
418 :
419 : private:
420 : mutable Mutex m_tx_relay_mutex;
421 :
422 : /** Transaction relay data.
423 : * (Bitcoin) Transaction relay data. May be a nullptr.
424 : * (Dash) Always initialized but selectively available through GetTxRelay()
425 : * (non-transaction relay should use GetInvRelay(), which will provide
426 : * unconditional access) */
427 9973 : std::unique_ptr<TxRelay> m_tx_relay GUARDED_BY(m_tx_relay_mutex){std::make_unique<TxRelay>()};
428 : /** Whether a peer can relay transactions */
429 9973 : bool m_can_tx_relay GUARDED_BY(m_tx_relay_mutex) {false};
430 : };
431 :
432 : using PeerRef = std::shared_ptr<Peer>;
433 :
434 : /**
435 : * Maintain validation-specific state about nodes, protected by cs_main, instead
436 : * by CNode's own locks. This simplifies asynchronous operation, where
437 : * processing of incoming data is done after the ProcessMessage call returns,
438 : * and we're no longer holding the node's locks.
439 : */
440 : struct CNodeState {
441 : //! The best known block we know this peer has announced.
442 9973 : const CBlockIndex* pindexBestKnownBlock{nullptr};
443 : //! The hash of the last unknown block this peer has announced.
444 9973 : uint256 hashLastUnknownBlock{};
445 : //! The last full block we both have.
446 9973 : const CBlockIndex* pindexLastCommonBlock{nullptr};
447 : //! The best header we have sent our peer.
448 9973 : const CBlockIndex* pindexBestHeaderSent{nullptr};
449 : //! Length of current-streak of unconnecting headers announcements
450 9973 : int nUnconnectingHeaders{0};
451 : //! Whether we've started headers synchronization with this peer.
452 9973 : bool fSyncStarted{false};
453 : //! When to potentially disconnect peer for stalling headers download
454 9973 : std::chrono::microseconds m_headers_sync_timeout{0us};
455 : //! Since when we're stalling block download progress (in microseconds), or 0.
456 9973 : std::chrono::microseconds m_stalling_since{0us};
457 : std::list<QueuedBlock> vBlocksInFlight;
458 : //! When the first entry in vBlocksInFlight started downloading. Don't care when vBlocksInFlight is empty.
459 9973 : std::chrono::microseconds m_downloading_since{0us};
460 9973 : int nBlocksInFlight{0};
461 : //! Whether we consider this a preferred download peer.
462 9973 : bool fPreferredDownload{false};
463 : //! Whether this peer wants invs or headers (when possible) for block announcements.
464 9973 : bool fPreferHeaders{false};
465 : //! Whether this peer wants invs or compressed headers (when possible) for block announcements.
466 9973 : bool fPreferHeadersCompressed{false};
467 : /** Whether this peer wants invs or cmpctblocks (when possible) for block announcements. */
468 9973 : bool m_requested_hb_cmpctblocks{false};
469 : /** Whether this peer will send us cmpctblocks if we request them. */
470 9973 : bool m_provides_cmpctblocks{false};
471 :
472 : /** State used to enforce CHAIN_SYNC_TIMEOUT and EXTRA_PEER_CHECK_INTERVAL logic.
473 : *
474 : * Both are only in effect for outbound, non-manual, non-protected connections.
475 : * Any peer protected (m_protect = true) is not chosen for eviction. A peer is
476 : * marked as protected if all of these are true:
477 : * - its connection type is IsBlockOnlyConn() == false
478 : * - it gave us a valid connecting header
479 : * - we haven't reached MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT yet
480 : * - its chain tip has at least as much work as ours
481 : *
482 : * CHAIN_SYNC_TIMEOUT: if a peer's best known block has less work than our tip,
483 : * set a timeout CHAIN_SYNC_TIMEOUT in the future:
484 : * - If at timeout their best known block now has more work than our tip
485 : * when the timeout was set, then either reset the timeout or clear it
486 : * (after comparing against our current tip's work)
487 : * - If at timeout their best known block still has less work than our
488 : * tip did when the timeout was set, then send a getheaders message,
489 : * and set a shorter timeout, HEADERS_RESPONSE_TIME seconds in future.
490 : * If their best known block is still behind when that new timeout is
491 : * reached, disconnect.
492 : *
493 : * EXTRA_PEER_CHECK_INTERVAL: after each interval, if we have too many outbound peers,
494 : * drop the outbound one that least recently announced us a new block.
495 : */
496 9973 : struct ChainSyncTimeoutState {
497 : //! A timeout used for checking whether our peer has sufficiently synced
498 9973 : std::chrono::seconds m_timeout{0s};
499 : //! A header with the work we require on our peer's chain
500 9973 : const CBlockIndex* m_work_header{nullptr};
501 : //! After timeout is reached, set to true after sending getheaders
502 9973 : bool m_sent_getheaders{false};
503 : //! Whether this peer is protected from disconnection due to a bad/slow chain
504 9973 : bool m_protect{false};
505 : };
506 :
507 : ChainSyncTimeoutState m_chain_sync;
508 :
509 : //! Time of last new block announcement
510 9973 : int64_t m_last_block_announcement{0};
511 :
512 : /*
513 : * State associated with objects download.
514 : *
515 : * Tx download algorithm:
516 : *
517 : * When inv comes in, queue up (process_time, inv) inside the peer's
518 : * CNodeState (m_object_process_time) as long as m_object_announced for the peer
519 : * isn't too big (MAX_PEER_OBJECT_ANNOUNCEMENTS).
520 : *
521 : * The process_time for a objects is set to nNow for outbound peers,
522 : * nNow + 2 seconds for inbound peers. This is the time at which we'll
523 : * consider trying to request the objects from the peer in
524 : * SendMessages(). The delay for inbound peers is to allow outbound peers
525 : * a chance to announce before we request from inbound peers, to prevent
526 : * an adversary from using inbound connections to blind us to a
527 : * objects (InvBlock).
528 : *
529 : * When we call SendMessages() for a given peer,
530 : * we will loop over the objects in m_object_process_time, looking
531 : * at the objects whose process_time <= nNow. We'll request each
532 : * such objects that we don't have already and that hasn't been
533 : * requested from another peer recently, up until we hit the
534 : * MAX_PEER_OBJECT_IN_FLIGHT limit for the peer. Then we'll update
535 : * g_already_asked_for for each requested inv, storing the time of the
536 : * GETDATA request. We use g_already_asked_for to coordinate objects
537 : * requests amongst our peers.
538 : *
539 : * For objects that we still need but we have already recently
540 : * requested from some other peer, we'll reinsert (process_time, inv)
541 : * back into the peer's m_object_process_time at the point in the future at
542 : * which the most recent GETDATA request would time out (ie
543 : * GetObjectInterval + the request time stored in g_already_asked_for).
544 : * We add an additional delay for inbound peers, again to prefer
545 : * attempting download from outbound peers first.
546 : * We also add an extra small random delay up to 2 seconds
547 : * to avoid biasing some peers over others. (e.g., due to fixed ordering
548 : * of peer processing in ThreadMessageHandler).
549 : *
550 : * When we receive a objects from a peer, we remove the inv from the
551 : * peer's m_object_in_flight set and from their recently announced set
552 : * (m_object_announced). We also clear g_already_asked_for for that entry, so
553 : * that if somehow the objects is not accepted but also not added to
554 : * the reject filter, then we will eventually redownload from other
555 : * peers.
556 : */
557 9973 : struct ObjectDownloadState {
558 : /* Track when to attempt download of announced objects (process
559 : * time in micros -> inv)
560 : */
561 : std::multimap<std::chrono::microseconds, CInv> m_object_process_time;
562 :
563 : //! Store all the objects a peer has recently announced
564 : std::set<CInv> m_object_announced;
565 :
566 : //! Store objects which were requested by us, with timestamp
567 : std::map<CInv, std::chrono::microseconds> m_object_in_flight;
568 :
569 : //! Periodically check for stuck getdata requests
570 9973 : std::chrono::microseconds m_check_expiry_timer{0};
571 : };
572 :
573 : ObjectDownloadState m_object_download;
574 :
575 : //! Whether this peer is an inbound connection
576 : const bool m_is_inbound;
577 :
578 : //! A rolling bloom filter of all announced tx CInvs to this peer.
579 9973 : CRollingBloomFilter m_recently_announced_invs = CRollingBloomFilter{INVENTORY_MAX_RECENT_RELAY, 0.000001};
580 :
581 29919 : CNodeState(bool is_inbound) : m_is_inbound(is_inbound) {}
582 : };
583 :
584 : class PeerManagerImpl final : public PeerManager
585 : {
586 : public:
587 : PeerManagerImpl(const CChainParams& chainparams, CConnman& connman, AddrMan& addrman, BanMan* banman,
588 : CDSTXManager& dstxman, ChainstateManager& chainman, CTxMemPool& pool,
589 : CMasternodeMetaMan& mn_metaman, CMasternodeSync& mn_sync,
590 : CSporkManager& sporkman, const chainlock::Chainlocks& chainlocks,
591 : chainlock::ChainlockHandler& clhandler,
592 : CActiveMasternodeManager* nodeman,
593 : const std::unique_ptr<CDeterministicMNManager>& dmnman,
594 : const std::unique_ptr<CJWalletManager>& cj_walletman,
595 : const std::unique_ptr<LLMQContext>& llmq_ctx, bool ignore_incoming_txs);
596 :
597 9120 : ~PeerManagerImpl()
598 6080 : {
599 3040 : RemoveHandlers();
600 9120 : }
601 :
602 : /** Overridden from CValidationInterface. */
603 : void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override
604 : EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_recent_confirmed_transactions_mutex);
605 : void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) override
606 : EXCLUSIVE_LOCKS_REQUIRED(!m_recent_confirmed_transactions_mutex);
607 : void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override
608 : EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
609 : void BlockChecked(const CBlock& block, const BlockValidationState& state) override
610 : EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
611 : void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) override
612 : EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex);
613 :
614 : /** Implement NetEventsInterface */
615 : void InitializeNode(CNode& node, ServiceFlags our_services) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
616 : void FinalizeNode(const CNode& node) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
617 : bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override
618 : EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_recent_confirmed_transactions_mutex, !m_most_recent_block_mutex, g_msgproc_mutex);
619 : bool SendMessages(CNode* pto) override
620 : EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_recent_confirmed_transactions_mutex, !m_most_recent_block_mutex, g_msgproc_mutex);
621 :
622 : /** Implement PeerManager */
623 : void StartScheduledTasks(CScheduler& scheduler) override;
624 : void CheckForStaleTipAndEvictPeers() override;
625 : std::optional<std::string> FetchBlock(NodeId peer_id, const CBlockIndex& block_index) override
626 : EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
627 : bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
628 4199 : bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; }
629 : void SendPings() override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);;
630 : void PushInventory(NodeId nodeid, const CInv& inv) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
631 : void RelayInv(const CInv& inv) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
632 : void RelayInv(const CInv& inv, const int minProtoVersion) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
633 : void RelayTransaction(const uint256& txid) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
634 : void RelayRecoveredSig(const llmq::CRecoveredSig& sig, bool proactive_relay) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
635 : void RelayDSQ(const CCoinJoinQueue& queue) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
636 2831 : void SetBestHeight(int height) override { m_best_height = height; };
637 : void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message = "") override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
638 : void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
639 : const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) override
640 : EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_recent_confirmed_transactions_mutex, !m_most_recent_block_mutex, g_msgproc_mutex);
641 : void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) override;
642 : bool IsBanned(NodeId pnode) override EXCLUSIVE_LOCKS_REQUIRED(cs_main, !m_peer_mutex);
643 : size_t GetRequestedObjectCount(NodeId nodeid) const override EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
644 :
645 : /** Implements external handlers logic */
646 : void AddExtraHandler(std::unique_ptr<NetHandler>&& handler) override;
647 : void RemoveHandlers() override;
648 : void StartHandlers() override;
649 : void StopHandlers() override;
650 : void InterruptHandlers() override;
651 : void ScheduleHandlers(CScheduler& scheduler) override;
652 :
653 : /** Implement PeerManagerInternal */
654 : void PeerMisbehaving(const NodeId pnode, const int howmuch, const std::string& message = "") override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
655 : bool PeerIsBanned(const NodeId node_id) override EXCLUSIVE_LOCKS_REQUIRED(cs_main, !m_peer_mutex);
656 : void PeerEraseObjectRequest(const NodeId nodeid, const CInv& inv) override EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
657 : void PeerPushInventory(NodeId nodeid, const CInv& inv) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
658 : void PeerRelayInv(const CInv& inv) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
659 : void PeerRelayInvFiltered(const CInv& inv, const CTransaction& relatedTx) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
660 : void PeerRelayInvFiltered(const CInv& inv, const uint256& relatedTxHash) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
661 : void PeerRelayDSQ(const CCoinJoinQueue& queue) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
662 : void PeerRelayTransaction(const uint256& txid) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
663 : void PeerRelayRecoveredSig(const llmq::CRecoveredSig& sig, bool proactive_relay) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
664 : void PeerAskPeersForTransaction(const uint256& txid) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
665 : size_t PeerGetRequestedObjectCount(NodeId nodeid) const override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, ::cs_main);
666 : void PeerPostProcessMessage(MessageProcessingResult&& ret) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
667 :
668 : private:
669 : void _RelayTransaction(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(cs_main, !m_peer_mutex);
670 :
671 : /** Ask peers that have a transaction in their inventory to relay it to us. */
672 : void AskPeersForTransaction(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
673 :
674 : /** Relay inventories to peers that find it relevant */
675 : void RelayInvFiltered(const CInv& inv, const CTransaction& relatedTx) EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
676 :
677 : /**
678 : * This overload will not update node filters, use it only for the cases
679 : * when other messages will update related transaction data in filters
680 : */
681 : void RelayInvFiltered(const CInv& inv, const uint256& relatedTxHash) EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
682 :
683 : void EraseObjectRequest(NodeId nodeid, const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
684 :
685 : void RequestObject(NodeId nodeid, const CInv& inv, std::chrono::microseconds current_time, bool fForce = false)
686 : EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
687 :
688 : /** Helper to process result of external handlers of message */
689 : void PostProcessMessage(MessageProcessingResult&& ret, NodeId node) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
690 :
691 : /** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
692 : void ConsiderEviction(CNode& pto, Peer& peer, std::chrono::seconds time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_msgproc_mutex);
693 :
694 : /** If we have extra outbound peers, try to disconnect the one with the oldest block announcement */
695 : void EvictExtraOutboundPeers(std::chrono::seconds now) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
696 :
697 : /** Retrieve unbroadcast transactions from the mempool and reattempt sending to peers */
698 : void ReattemptInitialBroadcast(CScheduler& scheduler) EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
699 :
700 : /**
701 : * Private implementation of IsInvInFilter which does not call GetPeerRef; to be prefered when the PeerRef is available.
702 : */
703 : bool IsInvInFilter(const Peer& peer, const uint256& hash) const;
704 :
705 : /** Get a shared pointer to the Peer object.
706 : * May return an empty shared_ptr if the Peer object can't be found. */
707 : PeerRef GetPeerRef(NodeId id) const EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
708 :
709 : /** Get a shared pointer to the Peer object and remove it from m_peer_map.
710 : * May return an empty shared_ptr if the Peer object can't be found. */
711 : PeerRef RemovePeer(NodeId id) EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
712 :
713 : /**
714 : * Potentially mark a node discouraged based on the contents of a BlockValidationState object
715 : *
716 : * @param[in] via_compact_block this bool is passed in because net_processing should
717 : * punish peers differently depending on whether the data was provided in a compact
718 : * block message or not. If the compact block had a valid header, but contained invalid
719 : * txs, the peer should not be punished. See BIP 152.
720 : *
721 : * @return Returns true if the peer was punished (probably disconnected)
722 : */
723 : bool MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState& state,
724 : bool via_compact_block, const std::string& message = "")
725 : EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
726 :
727 : /**
728 : * Potentially ban a node based on the contents of a TxValidationState object
729 : *
730 : * @return Returns true if the peer was punished (probably disconnected)
731 : *
732 : * Changes here may need to be reflected in TxRelayMayResultInDisconnect().
733 : */
734 : bool MaybePunishNodeForTx(NodeId nodeid, const TxValidationState& state)
735 : EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
736 :
737 : /** Maybe disconnect a peer and discourage future connections from its address.
738 : *
739 : * @param[in] pnode The node to check.
740 : * @param[in] peer The peer object to check.
741 : * @return True if the peer was marked for disconnection in this function
742 : */
743 : bool MaybeDiscourageAndDisconnect(CNode& pnode, Peer& peer);
744 :
745 : /**
746 : * Reconsider orphan transactions after a parent has been accepted to the mempool.
747 : *
748 : * @param[in] node_id The peer whose orphan transactions we will reconsider. Generally only one
749 : * orphan will be reconsidered on each call of this function. This set
750 : * may be added to if accepting an orphan causes its children to be
751 : * reconsidered.
752 : * @return True if there are still orphans in this peer's work set.
753 : */
754 : bool ProcessOrphanTx(NodeId node_id)
755 : EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, cs_main);
756 : /** Process a single headers message from a peer. */
757 : void ProcessHeadersMessage(CNode& pfrom, Peer& peer,
758 : const std::vector<CBlockHeader>& headers,
759 : bool via_compact_block)
760 : EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, g_msgproc_mutex);
761 : [[nodiscard]] MessageProcessingResult ProcessPlatformBanMessage(NodeId node, std::string_view msg_type, CDataStream& vRecv)
762 : EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, g_msgproc_mutex);
763 :
764 : /** Various helpers for headers processing, invoked by ProcessHeadersMessage() */
765 : /** Deal with state tracking and headers sync for peers that send the
766 : * occasional non-connecting header (this can happen due to BIP 130 headers
767 : * announcements for blocks interacting with the 2hr (MAX_FUTURE_BLOCK_TIME) rule). */
768 : void HandleFewUnconnectingHeaders(CNode& pfrom, Peer& peer, const std::vector<CBlockHeader>& headers)
769 : EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, g_msgproc_mutex);
770 : /** Return true if the headers connect to each other, false otherwise */
771 : bool CheckHeadersAreContinuous(const std::vector<CBlockHeader>& headers) const;
772 : /** Request further headers from this peer with a given locator.
773 : * We don't issue a getheaders message if we have a recent one outstanding.
774 : * This returns true if a getheaders is actually sent, and false otherwise.
775 : */
776 : bool MaybeSendGetHeaders(CNode& pfrom, const std::string& msg_type, const CBlockLocator& locator, Peer& peer)
777 : EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
778 : /** Potentially fetch blocks from this peer upon receipt of a new headers tip */
779 : void HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex& last_header);
780 : /** Update peer state based on received headers message */
781 : void UpdatePeerStateForReceivedHeaders(CNode& pfrom, const CBlockIndex& last_header, bool received_new_header, bool may_have_more_headers);
782 :
783 : void SendBlockTransactions(CNode& pfrom, const CBlock& block, const BlockTransactionsRequest& req)
784 : EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
785 :
786 : /** Send a version message to a peer */
787 : void PushNodeVersion(CNode& pnode, const Peer& peer);
788 :
789 : /** Send a ping message every PING_INTERVAL or if requested via RPC. May
790 : * mark the peer to be disconnected if a ping has timed out.
791 : * We use mockable time for ping timeouts, so setmocktime may cause pings
792 : * to time out. */
793 : void MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::microseconds now);
794 :
795 : /** Send `addr` messages on a regular schedule. */
796 : void MaybeSendAddr(CNode& node, Peer& peer, std::chrono::microseconds current_time)
797 : EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
798 :
799 : /** Relay (gossip) an address to a few randomly chosen nodes.
800 : *
801 : * @param[in] originator The id of the peer that sent us the address. We don't want to relay it back.
802 : * @param[in] addr Address to relay.
803 : * @param[in] fReachable Whether the address' network is reachable. We relay unreachable
804 : * addresses less.
805 : */
806 : void RelayAddress(NodeId originator, const CAddress& addr, bool fReachable)
807 : EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, g_msgproc_mutex);
808 :
809 : const CChainParams& m_chainparams;
810 : CConnman& m_connman;
811 : AddrMan& m_addrman;
812 : /** Pointer to this node's banman. May be nullptr - check existence before dereferencing. */
813 : BanMan* const m_banman;
814 : CDSTXManager& m_dstxman;
815 : ChainstateManager& m_chainman;
816 : CTxMemPool& m_mempool;
817 : std::unique_ptr<TxReconciliationTracker> m_txreconciliation;
818 : CActiveMasternodeManager* const m_nodeman; //!< null if non-masternode mode; non-null implies masternode mode
819 : const std::unique_ptr<CDeterministicMNManager>& m_dmnman;
820 : const std::unique_ptr<CJWalletManager>& m_cj_walletman;
821 : const std::unique_ptr<LLMQContext>& m_llmq_ctx;
822 : CMasternodeMetaMan& m_mn_metaman;
823 : CMasternodeSync& m_mn_sync;
824 : CSporkManager& m_sporkman;
825 : const chainlock::Chainlocks& m_chainlocks;
826 : // TODO: consider further refactoring ChainlockHandler to NetHandler to avoid boiler code in PeerManager
827 : chainlock::ChainlockHandler& m_clhandler;
828 :
829 : /** The height of the best chain */
830 3040 : std::atomic<int> m_best_height{-1};
831 :
832 : /** Next time to check for stale tip */
833 3040 : std::chrono::seconds m_stale_tip_check_time GUARDED_BY(cs_main){0s};
834 :
835 : /** Whether this node is running in -blocksonly mode */
836 : const bool m_ignore_incoming_txs;
837 :
838 : bool RejectIncomingTxs(const CNode& peer) const;
839 :
840 : /** Whether we've completed initial sync yet, for determining when to turn
841 : * on extra block-relay-only peers. */
842 3040 : bool m_initial_sync_finished GUARDED_BY(cs_main){false};
843 :
844 : /** Protects m_peer_map. This mutex must not be locked while holding a lock
845 : * on any of the mutexes inside a Peer object. */
846 : mutable SharedMutex m_peer_mutex;
847 : /**
848 : * Map of all Peer objects, keyed by peer id. This map is protected
849 : * by the m_peer_mutex. Once a shared pointer reference is
850 : * taken, the lock may be released. Individual fields are protected by
851 : * their own locks.
852 : */
853 : std::map<NodeId, PeerRef> m_peer_map GUARDED_BY(m_peer_mutex);
854 :
855 : /** Map maintaining per-node state. */
856 : std::map<NodeId, CNodeState> m_node_states GUARDED_BY(cs_main);
857 :
858 : /** Get a pointer to a const CNodeState, used when not mutating the CNodeState object. */
859 : const CNodeState* State(NodeId pnode) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
860 : /** Get a pointer to a mutable CNodeState. */
861 : CNodeState* State(NodeId pnode) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
862 :
863 3040 : std::atomic<std::chrono::microseconds> m_next_inv_to_inbounds{0us};
864 :
865 : /** Check whether the last unknown block a peer advertised is not yet known. */
866 : void ProcessBlockAvailability(NodeId nodeid) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
867 : /** Update tracking information about which blocks a peer is assumed to have. */
868 : void UpdateBlockAvailability(NodeId nodeid, const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
869 : bool CanDirectFetch() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
870 :
871 : /**
872 : * To prevent fingerprinting attacks, only send blocks/headers outside of the
873 : * active chain if they are no more than a month older (both in time, and in
874 : * best equivalent proof of work) than the best header chain we know about and
875 : * we fully-validated them at some point.
876 : */
877 : bool BlockRequestAllowed(const CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
878 : bool AlreadyHaveBlock(const uint256& block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
879 : void ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv& inv, llmq::CInstantSendManager& isman) EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex);
880 :
881 : /**
882 : * Validation logic for compact filters request handling.
883 : *
884 : * May disconnect from the peer in the case of a bad request.
885 : *
886 : * @param[in] node The node that we received the request from
887 : * @param[in] peer The peer that we received the request from
888 : * @param[in] filter_type The filter type the request is for. Must be basic filters.
889 : * @param[in] start_height The start height for the request
890 : * @param[in] stop_hash The stop_hash for the request
891 : * @param[in] max_height_diff The maximum number of items permitted to request, as specified in BIP 157
892 : * @param[out] stop_index The CBlockIndex for the stop_hash block, if the request can be serviced.
893 : * @param[out] filter_index The filter index, if the request can be serviced.
894 : * @return True if the request can be serviced.
895 : */
896 : bool PrepareBlockFilterRequest(CNode& node, Peer& peer,
897 : BlockFilterType filter_type, uint32_t start_height,
898 : const uint256& stop_hash, uint32_t max_height_diff,
899 : const CBlockIndex*& stop_index,
900 : BlockFilterIndex*& filter_index);
901 :
902 : /**
903 : * Handle a cfilters request.
904 : *
905 : * May disconnect from the peer in the case of a bad request.
906 : *
907 : * @param[in] node The node that we received the request from
908 : * @param[in] peer The peer that we received the request from
909 : * @param[in] vRecv The raw message received
910 : */
911 : void ProcessGetCFilters(CNode& node, Peer& peer, CDataStream& vRecv);
912 :
913 : /**
914 : * Handle a cfheaders request.
915 : *
916 : * May disconnect from the peer in the case of a bad request.
917 : *
918 : * @param[in] node The node that we received the request from
919 : * @param[in] peer The peer that we received the request from
920 : * @param[in] vRecv The raw message received
921 : */
922 : void ProcessGetCFHeaders(CNode& node, Peer& peer, CDataStream& vRecv);
923 :
924 : /**
925 : * Handle a getcfcheckpt request.
926 : *
927 : * May disconnect from the peer in the case of a bad request.
928 : *
929 : * @param[in] node The node that we received the request from
930 : * @param[in] peer The peer that we received the request from
931 : * @param[in] vRecv The raw message received
932 : */
933 : void ProcessGetCFCheckPt(CNode& node, Peer& peer, CDataStream& vRecv);
934 :
935 : /** Checks if address relay is permitted with peer. If needed, initializes
936 : * the m_addr_known bloom filter and sets m_addr_relay_enabled to true.
937 : *
938 : * @return True if address relay is enabled with peer
939 : * False if address relay is disallowed
940 : */
941 : bool SetupAddressRelay(const CNode& node, Peer& peer) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
942 :
943 : void AddAddressKnown(Peer& peer, const CAddress& addr) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
944 : void PushAddress(Peer& peer, const CAddress& addr, FastRandomContext& insecure_rand) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
945 :
946 : /** Number of nodes with fSyncStarted. */
947 3040 : int nSyncStarted GUARDED_BY(cs_main) = 0;
948 :
949 : /** Hash of the last block we received via INV */
950 3040 : uint256 m_last_block_inv_triggering_headers_sync GUARDED_BY(g_msgproc_mutex){};
951 :
952 : /**
953 : * Sources of received blocks, saved to be able punish them when processing
954 : * happens afterwards.
955 : * Set mapBlockSource[hash].second to false if the node should not be
956 : * punished if the block is invalid.
957 : */
958 : std::map<uint256, std::pair<NodeId, bool>> mapBlockSource GUARDED_BY(cs_main);
959 :
960 : /** Number of outbound peers with m_chain_sync.m_protect. */
961 3040 : int m_outbound_peers_with_protect_from_disconnect GUARDED_BY(cs_main) = 0;
962 :
963 : /** Number of preferable block download peers. */
964 3040 : int m_num_preferred_download_peers GUARDED_BY(cs_main){0};
965 :
966 : /** Stalling timeout for blocks in IBD */
967 3040 : std::atomic<std::chrono::seconds> m_block_stalling_timeout{BLOCK_STALLING_TIMEOUT_DEFAULT};
968 :
969 : bool AlreadyHave(const CInv& inv)
970 : EXCLUSIVE_LOCKS_REQUIRED(cs_main, !m_recent_confirmed_transactions_mutex);
971 :
972 : /**
973 : * Filter for transactions that were recently rejected by the mempool.
974 : * These are not rerequested until the chain tip changes, at which point
975 : * the entire filter is reset.
976 : *
977 : * Without this filter we'd be re-requesting txs from each of our peers,
978 : * increasing bandwidth consumption considerably. For instance, with 100
979 : * peers, half of which relay a tx we don't accept, that might be a 50x
980 : * bandwidth increase. A flooding attacker attempting to roll-over the
981 : * filter using minimum-sized, 60byte, transactions might manage to send
982 : * 1000/sec if we have fast peers, so we pick 120,000 to give our peers a
983 : * two minute window to send invs to us.
984 : *
985 : * Decreasing the false positive rate is fairly cheap, so we pick one in a
986 : * million to make it highly unlikely for users to have issues with this
987 : * filter.
988 : *
989 : * Memory used: 1.3 MB
990 : */
991 3040 : CRollingBloomFilter m_recent_rejects GUARDED_BY(::cs_main){120'000, 0.000'001};
992 : uint256 hashRecentRejectsChainTip GUARDED_BY(cs_main);
993 :
994 : /*
995 : * Filter for transactions that have been recently confirmed.
996 : * We use this to avoid requesting transactions that have already been
997 : * confirnmed.
998 : *
999 : * Blocks don't typically have more than 4000 transactions, so this should
1000 : * be at least six blocks (~1 hr) worth of transactions that we can store,
1001 : * inserting both a txid and wtxid for every observed transaction.
1002 : * If the number of transactions appearing in a block goes up, or if we are
1003 : * seeing getdata requests more than an hour after initial announcement, we
1004 : * can increase this number.
1005 : * The false positive rate of 1/1M should come out to less than 1
1006 : * transaction per day that would be inadvertently ignored (which is the
1007 : * same probability that we have in the reject filter).
1008 : */
1009 : Mutex m_recent_confirmed_transactions_mutex;
1010 3040 : CRollingBloomFilter m_recent_confirmed_transactions GUARDED_BY(m_recent_confirmed_transactions_mutex){48'000, 0.000'001};
1011 :
1012 : /**
1013 : * For sending `inv`s to inbound peers, we use a single (exponentially
1014 : * distributed) timer for all peers. If we used a separate timer for each
1015 : * peer, a spy node could make multiple inbound connections to us to
1016 : * accurately determine when we received the transaction (and potentially
1017 : * determine the transaction's origin). */
1018 : std::chrono::microseconds NextInvToInbounds(std::chrono::microseconds now,
1019 : std::chrono::seconds average_interval);
1020 :
1021 :
1022 : // All of the following cache a recent block, and are protected by m_most_recent_block_mutex
1023 : Mutex m_most_recent_block_mutex;
1024 : std::shared_ptr<const CBlock> m_most_recent_block GUARDED_BY(m_most_recent_block_mutex);
1025 : std::shared_ptr<const CBlockHeaderAndShortTxIDs> m_most_recent_compact_block GUARDED_BY(m_most_recent_block_mutex);
1026 : uint256 m_most_recent_block_hash GUARDED_BY(m_most_recent_block_mutex);
1027 :
1028 : /** Height of the highest block announced using BIP 152 high-bandwidth mode. */
1029 3040 : int m_highest_fast_announce GUARDED_BY(::cs_main){0};
1030 :
1031 : /** Have we requested this block from a peer */
1032 : bool IsBlockRequested(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1033 :
1034 : /** Remove this block from our tracked requested blocks. Called if:
1035 : * - the block has been recieved from a peer
1036 : * - the request for the block has timed out
1037 : * If "from_peer" is specified, then only remove the block if it is in
1038 : * flight from that peer (to avoid one peer's network traffic from
1039 : * affecting another's state).
1040 : */
1041 : void RemoveBlockRequest(const uint256& hash, std::optional<NodeId> from_peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1042 :
1043 : /* Mark a block as in flight
1044 : * Returns false, still setting pit, if the block was already in flight from the same peer
1045 : * pit will only be valid as long as the same cs_main lock is being held
1046 : */
1047 : bool BlockRequested(NodeId nodeid, const CBlockIndex& block, std::list<QueuedBlock>::iterator** pit = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1048 :
1049 : bool TipMayBeStale() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1050 :
1051 : /** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
1052 : * at most count entries.
1053 : */
1054 : void FindNextBlocksToDownload(const Peer& peer, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1055 :
1056 : std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> > mapBlocksInFlight GUARDED_BY(cs_main);
1057 :
1058 : /** When our tip was last updated. */
1059 3040 : std::atomic<std::chrono::seconds> m_last_tip_update{0s};
1060 :
1061 : /** Determine whether or not a peer can request a transaction, and return it (or nullptr if not found or not allowed). */
1062 : CTransactionRef FindTxForGetData(const CNode* peer, const uint256& txid, const std::chrono::seconds mempool_req, const std::chrono::seconds now) LOCKS_EXCLUDED(cs_main);
1063 :
1064 : void ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic<bool>& interruptMsgProc)
1065 : EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex, peer.m_getdata_requests_mutex) LOCKS_EXCLUDED(::cs_main);
1066 :
1067 : /** Process a new block. Perform any post-processing housekeeping */
1068 : void ProcessBlock(CNode& from, const std::shared_ptr<const CBlock>& pblock, bool force_processing);
1069 :
1070 : /** Relay map (txid -> CTransactionRef) */
1071 : typedef std::map<uint256, CTransactionRef> MapRelay;
1072 : MapRelay mapRelay GUARDED_BY(cs_main);
1073 : /** Expiration-time ordered list of (expire time, relay map entry) pairs. */
1074 : std::deque<std::pair<std::chrono::microseconds, MapRelay::iterator>> g_relay_expiration GUARDED_BY(cs_main);
1075 :
1076 : /**
1077 : * When a peer sends us a valid block, instruct it to announce blocks to us
1078 : * using CMPCTBLOCK if possible by adding its nodeid to the end of
1079 : * lNodesAnnouncingHeaderAndIDs, and keeping that list under a certain size by
1080 : * removing the first element if necessary.
1081 : */
1082 : void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1083 :
1084 : /** Stack of nodes which we have set to announce using compact blocks */
1085 : std::list<NodeId> lNodesAnnouncingHeaderAndIDs GUARDED_BY(cs_main);
1086 :
1087 : /** Number of peers from which we're downloading blocks. */
1088 3040 : int m_peers_downloading_from GUARDED_BY(cs_main) = 0;
1089 :
1090 : /** Storage for orphan information */
1091 : TxOrphanage m_orphanage;
1092 :
1093 : void AddToCompactExtraTransactions(const CTransactionRef& tx) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
1094 :
1095 : /** Orphan/conflicted/etc transactions that are kept for compact block reconstruction.
1096 : * The last -blockreconstructionextratxn/DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN of
1097 : * these are kept in a ring buffer */
1098 : std::vector<std::pair<uint256, CTransactionRef>> vExtraTxnForCompact GUARDED_BY(g_msgproc_mutex);
1099 : /** Offset into vExtraTxnForCompact to insert the next tx */
1100 3040 : size_t vExtraTxnForCompactIt GUARDED_BY(g_msgproc_mutex) = 0;
1101 :
1102 : std::vector<std::unique_ptr<NetHandler>> m_handlers;
1103 : };
1104 :
1105 : // Keeps track of the time (in microseconds) when transactions were requested last time
1106 3308 : unordered_limitedmap<uint256, std::chrono::microseconds, StaticSaltedHasher> g_already_asked_for(MAX_INV_SZ, MAX_INV_SZ * 2);
1107 3308 : unordered_limitedmap<uint256, std::chrono::microseconds, StaticSaltedHasher> g_erased_object_requests(MAX_INV_SZ, MAX_INV_SZ * 2);
1108 :
1109 14134157 : const CNodeState* PeerManagerImpl::State(NodeId pnode) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1110 : {
1111 14134157 : std::map<NodeId, CNodeState>::const_iterator it = m_node_states.find(pnode);
1112 14134157 : if (it == m_node_states.end())
1113 17611 : return nullptr;
1114 14116546 : return &it->second;
1115 14134157 : }
1116 :
1117 14038569 : CNodeState* PeerManagerImpl::State(NodeId pnode) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1118 : {
1119 14038569 : return const_cast<CNodeState*>(std::as_const(*this).State(pnode));
1120 : }
1121 :
1122 : /**
1123 : * Whether the peer supports the address. For example, a peer that does not
1124 : * implement BIP155 cannot receive Tor v3 addresses because it requires
1125 : * ADDRv2 (BIP155) encoding.
1126 : */
1127 38976 : static bool IsAddrCompatible(const Peer& peer, const CAddress& addr)
1128 : {
1129 38976 : return peer.m_wants_addrv2 || addr.IsAddrV1Compatible();
1130 : }
1131 :
1132 2554 : void PeerManagerImpl::AddAddressKnown(Peer& peer, const CAddress& addr)
1133 : {
1134 2554 : assert(peer.m_addr_known);
1135 2554 : peer.m_addr_known->insert(addr.GetKey());
1136 2554 : }
1137 :
1138 37989 : void PeerManagerImpl::PushAddress(Peer& peer, const CAddress& addr, FastRandomContext& insecure_rand)
1139 : {
1140 : // Known checking here is only to save space from duplicates.
1141 : // Before sending, we'll filter it again for known addresses that were
1142 : // added after addresses were pushed.
1143 37989 : assert(peer.m_addr_known);
1144 75941 : if (addr.IsValid() && !peer.m_addr_known->contains(addr.GetKey()) && IsAddrCompatible(peer, addr)) {
1145 37952 : if (peer.m_addrs_to_send.size() >= MAX_ADDR_TO_SEND) {
1146 0 : peer.m_addrs_to_send[insecure_rand.randrange(peer.m_addrs_to_send.size())] = addr;
1147 0 : } else {
1148 37952 : peer.m_addrs_to_send.push_back(addr);
1149 : }
1150 37952 : }
1151 37989 : }
1152 :
1153 210818 : static void AddKnownInv(Peer& peer, const uint256& hash)
1154 : {
1155 210818 : auto inv_relay = peer.GetInvRelay();
1156 210818 : assert(inv_relay);
1157 :
1158 210818 : LOCK(inv_relay->m_tx_inventory_mutex);
1159 210818 : inv_relay->m_tx_inventory_known_filter.insert(hash);
1160 210818 : }
1161 :
1162 : /** Whether this peer can serve us blocks. */
1163 3470949 : static bool CanServeBlocks(const Peer& peer)
1164 : {
1165 3470949 : return peer.m_their_services & (NODE_NETWORK|NODE_NETWORK_LIMITED);
1166 : }
1167 :
1168 : /* Whether this peer supports compressed headers (DIP 25) */
1169 120225 : static bool UsesCompressedHeaders(const Peer& peer)
1170 : {
1171 120225 : return peer.m_their_services & NODE_HEADERS_COMPRESSED;
1172 : }
1173 :
1174 : /** Whether this peer can only serve limited recent blocks (e.g. because
1175 : * it prunes old blocks) */
1176 2265862 : static bool IsLimitedPeer(const Peer& peer)
1177 : {
1178 2268584 : return (!(peer.m_their_services & NODE_NETWORK) &&
1179 2722 : (peer.m_their_services & NODE_NETWORK_LIMITED));
1180 : }
1181 :
1182 : /** Get maximum number of headers that can be included in one batch */
1183 301031 : static uint16_t GetHeadersLimit(const CNode& pfrom, bool compressed)
1184 : {
1185 301031 : if (pfrom.GetCommonVersion() >= INCREASE_MAX_HEADERS2_VERSION && compressed) {
1186 300008 : return MAX_HEADERS_COMPRESSED_RESULT;
1187 : }
1188 1023 : return MAX_HEADERS_UNCOMPRESSED_RESULT;
1189 301031 : }
1190 :
1191 : // Returns true when peer is a verified masternode that has opted in to receive recsigs.
1192 : // Such peers participate in the signing flow that populates creatingInstantSendLocks, so
1193 : // they can reconstruct an ISDLOCK locally from the recsig and don't need the ISDLOCK inv.
1194 : // Non-MN peers (e.g. nodes running with -watchquorums) also opt in to recsigs via
1195 : // QSENDRECSIGS but still need ISDLOCK invs because they don't run the signing flow.
1196 3001 : static bool PeerReconstructsISLockFromRecsig(const CNode& pnode, const Peer& peer)
1197 : {
1198 3001 : return peer.m_wants_recsigs && !pnode.GetVerifiedProRegTxHash().IsNull();
1199 : }
1200 :
1201 308378 : static void PushInv(Peer& peer, const CInv& inv)
1202 : {
1203 308378 : auto inv_relay = peer.GetInvRelay();
1204 308378 : assert(inv_relay);
1205 :
1206 : ASSERT_IF_DEBUG(inv.type != MSG_BLOCK);
1207 308378 : if (inv.type == MSG_BLOCK) {
1208 0 : LogPrintf("%s -- WARNING: using PushInv for BLOCK inv, peer=%d\n", __func__, peer.m_id);
1209 0 : return;
1210 : }
1211 :
1212 308378 : LOCK(inv_relay->m_tx_inventory_mutex);
1213 308378 : if (inv_relay->m_tx_inventory_known_filter.contains(inv.hash)) {
1214 68508 : LogPrint(BCLog::NET, "%s -- skipping known inv: %s peer=%d\n", __func__, inv.ToString(), peer.m_id);
1215 68508 : return;
1216 : }
1217 239857 : LogPrint(BCLog::NET, "%s -- adding new inv: %s peer=%d\n", __func__, inv.ToString(), peer.m_id);
1218 239862 : if (inv.type == MSG_TX || inv.type == MSG_DSTX) {
1219 49885 : inv_relay->m_tx_inventory_to_send.insert(inv.hash);
1220 49885 : return;
1221 : }
1222 189977 : inv_relay->vInventoryOtherToSend.push_back(inv);
1223 308388 : }
1224 :
1225 30115 : std::chrono::microseconds PeerManagerImpl::NextInvToInbounds(std::chrono::microseconds now,
1226 : std::chrono::seconds average_interval)
1227 : {
1228 30115 : if (m_next_inv_to_inbounds.load() < now) {
1229 : // If this function were called from multiple threads simultaneously
1230 : // it would possible that both update the next send variable, and return a different result to their caller.
1231 : // This is not possible in practice as only the net processing thread invokes this function.
1232 10024 : m_next_inv_to_inbounds = GetExponentialRand(now, average_interval);
1233 10024 : }
1234 30115 : return m_next_inv_to_inbounds;
1235 : }
1236 :
1237 1049085 : bool PeerManagerImpl::IsBlockRequested(const uint256& hash)
1238 : {
1239 1049085 : return mapBlocksInFlight.find(hash) != mapBlocksInFlight.end();
1240 : }
1241 :
1242 456573 : void PeerManagerImpl::RemoveBlockRequest(const uint256& hash, std::optional<NodeId> from_peer)
1243 : {
1244 456573 : auto it = mapBlocksInFlight.find(hash);
1245 456573 : if (it == mapBlocksInFlight.end()) {
1246 : // Block was not requested
1247 305418 : return;
1248 : }
1249 :
1250 450490 : auto [node_id, list_it] = it->second;
1251 :
1252 151155 : if (from_peer && node_id != *from_peer) {
1253 : // Block was requested by another peer
1254 0 : return;
1255 : }
1256 :
1257 151155 : CNodeState *state = State(node_id);
1258 151155 : assert(state != nullptr);
1259 :
1260 302310 : if (state->vBlocksInFlight.begin() == list_it) {
1261 : // First block on the queue was received, update the start download time for the next one
1262 148180 : state->m_downloading_since = std::max(state->m_downloading_since, GetTime<std::chrono::microseconds>());
1263 148180 : }
1264 302310 : state->vBlocksInFlight.erase(list_it);
1265 :
1266 151155 : state->nBlocksInFlight--;
1267 151155 : if (state->nBlocksInFlight == 0) {
1268 : // Last validated block on the queue was received.
1269 93267 : m_peers_downloading_from--;
1270 93267 : }
1271 151155 : state->m_stalling_since = 0us;
1272 151155 : mapBlocksInFlight.erase(it);
1273 456573 : }
1274 :
1275 152930 : bool PeerManagerImpl::BlockRequested(NodeId nodeid, const CBlockIndex& block, std::list<QueuedBlock>::iterator **pit)
1276 : {
1277 152930 : const uint256& hash{block.GetBlockHash()};
1278 :
1279 152930 : CNodeState *state = State(nodeid);
1280 152930 : assert(state != nullptr);
1281 :
1282 : // Short-circuit most stuff in case it is from the same node
1283 152930 : std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
1284 152930 : if (itInFlight != mapBlocksInFlight.end() && itInFlight->second.first == nodeid) {
1285 1574 : if (pit) {
1286 1574 : *pit = &itInFlight->second.second;
1287 1574 : }
1288 1574 : return false;
1289 : }
1290 :
1291 : // Make sure it's not listed somewhere already.
1292 151356 : RemoveBlockRequest(hash, std::nullopt);
1293 :
1294 302712 : std::list<QueuedBlock>::iterator it = state->vBlocksInFlight.insert(state->vBlocksInFlight.end(),
1295 151356 : {&block, std::unique_ptr<PartiallyDownloadedBlock>(pit ? new PartiallyDownloadedBlock(&m_mempool) : nullptr)});
1296 151356 : state->nBlocksInFlight++;
1297 151356 : if (state->nBlocksInFlight == 1) {
1298 : // We're starting a block download (batch) from this peer.
1299 93320 : state->m_downloading_since = GetTime<std::chrono::microseconds>();
1300 93320 : m_peers_downloading_from++;
1301 93320 : }
1302 151356 : itInFlight = mapBlocksInFlight.insert(std::make_pair(hash, std::make_pair(nodeid, it))).first;
1303 151356 : if (pit) {
1304 95796 : *pit = &itInFlight->second.second;
1305 95796 : }
1306 151356 : return true;
1307 152930 : }
1308 :
1309 96144 : void PeerManagerImpl::MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid)
1310 : {
1311 96144 : AssertLockHeld(cs_main);
1312 :
1313 : // When in -blocksonly mode, never request high-bandwidth mode from peers. Our
1314 : // mempool will not contain the transactions necessary to reconstruct the
1315 : // compact block.
1316 96144 : if (m_ignore_incoming_txs) return;
1317 :
1318 96142 : CNodeState* nodestate = State(nodeid);
1319 96142 : if (!nodestate || !nodestate->m_provides_cmpctblocks) {
1320 : // Don't request compact blocks if the peer has not signalled support
1321 4747 : return;
1322 : }
1323 :
1324 91395 : int num_outbound_hb_peers = 0;
1325 114039 : for (std::list<NodeId>::iterator it = lNodesAnnouncingHeaderAndIDs.begin(); it != lNodesAnnouncingHeaderAndIDs.end(); it++) {
1326 112318 : if (*it == nodeid) {
1327 89674 : lNodesAnnouncingHeaderAndIDs.erase(it);
1328 89674 : lNodesAnnouncingHeaderAndIDs.push_back(nodeid);
1329 89674 : return;
1330 : }
1331 22644 : CNodeState *state = State(*it);
1332 22644 : if (state != nullptr && !state->m_is_inbound) ++num_outbound_hb_peers;
1333 22644 : }
1334 1721 : if (nodestate->m_is_inbound) {
1335 : // If we're adding an inbound HB peer, make sure we're not removing
1336 : // our last outbound HB peer in the process.
1337 457 : if (lNodesAnnouncingHeaderAndIDs.size() >= 3 && num_outbound_hb_peers == 1) {
1338 16 : CNodeState *remove_node = State(lNodesAnnouncingHeaderAndIDs.front());
1339 16 : if (remove_node != nullptr && !remove_node->m_is_inbound) {
1340 : // Put the HB outbound peer in the second slot, so that it
1341 : // doesn't get removed.
1342 6 : std::swap(lNodesAnnouncingHeaderAndIDs.front(), *std::next(lNodesAnnouncingHeaderAndIDs.begin()));
1343 6 : }
1344 16 : }
1345 457 : }
1346 3442 : m_connman.ForNode(nodeid, [this](CNode* pfrom) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
1347 1721 : AssertLockHeld(::cs_main);
1348 1721 : m_connman.PushMessage(pfrom, CNetMsgMaker(pfrom->GetCommonVersion()).Make(NetMsgType::SENDCMPCT, /*high_bandwidth=*/true, /*version=*/CMPCTBLOCKS_VERSION));
1349 : // save BIP152 bandwidth state: we select peer to be high-bandwidth
1350 1721 : pfrom->m_bip152_highbandwidth_to = true;
1351 1721 : lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId());
1352 1721 : return true;
1353 0 : });
1354 1721 : if (lNodesAnnouncingHeaderAndIDs.size() > 3) {
1355 : // As per BIP152, we only get 3 of our peers to announce
1356 : // blocks using compact encodings.
1357 101 : m_connman.ForNode(lNodesAnnouncingHeaderAndIDs.front(), [this](CNode* pnodeStop){
1358 16 : m_connman.PushMessage(pnodeStop, CNetMsgMaker(pnodeStop->GetCommonVersion()).Make(NetMsgType::SENDCMPCT, /*high_bandwidth=*/false, /*version=*/CMPCTBLOCKS_VERSION));
1359 : // save BIP152 bandwidth state: we select peer to be low-bandwidth
1360 16 : pnodeStop->m_bip152_highbandwidth_to = false;
1361 16 : return true;
1362 0 : });
1363 85 : lNodesAnnouncingHeaderAndIDs.pop_front();
1364 85 : }
1365 96144 : }
1366 :
1367 3318 : bool PeerManagerImpl::TipMayBeStale()
1368 : {
1369 3318 : AssertLockHeld(cs_main);
1370 3318 : const Consensus::Params& consensusParams = m_chainparams.GetConsensus();
1371 3318 : if (m_last_tip_update.load() == 0s) {
1372 84 : m_last_tip_update = GetTime<std::chrono::seconds>();
1373 84 : }
1374 3318 : return m_last_tip_update.load() < GetTime<std::chrono::seconds>() - std::chrono::seconds{consensusParams.nPowTargetSpacing * 3} && mapBlocksInFlight.empty();
1375 : }
1376 :
1377 217109 : bool PeerManagerImpl::CanDirectFetch()
1378 : {
1379 217109 : return m_chainman.ActiveChain().Tip()->GetBlockTime() > GetAdjustedTime() - m_chainparams.GetConsensus().nPowTargetSpacing * 20;
1380 : }
1381 :
1382 777878 : static bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1383 : {
1384 777878 : if (state->pindexBestKnownBlock && pindex == state->pindexBestKnownBlock->GetAncestor(pindex->nHeight))
1385 268867 : return true;
1386 509011 : if (state->pindexBestHeaderSent && pindex == state->pindexBestHeaderSent->GetAncestor(pindex->nHeight))
1387 257364 : return true;
1388 251647 : return false;
1389 777878 : }
1390 :
1391 5303322 : void PeerManagerImpl::ProcessBlockAvailability(NodeId nodeid)
1392 : {
1393 5303322 : CNodeState *state = State(nodeid);
1394 5303322 : assert(state != nullptr);
1395 :
1396 5303322 : if (!state->hashLastUnknownBlock.IsNull()) {
1397 1277 : const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(state->hashLastUnknownBlock);
1398 1277 : if (pindex && pindex->nChainWork > 0) {
1399 127 : if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
1400 127 : state->pindexBestKnownBlock = pindex;
1401 127 : }
1402 127 : state->hashLastUnknownBlock.SetNull();
1403 127 : }
1404 1277 : }
1405 5303322 : }
1406 :
1407 224892 : void PeerManagerImpl::UpdateBlockAvailability(NodeId nodeid, const uint256 &hash)
1408 : {
1409 224892 : CNodeState *state = State(nodeid);
1410 224892 : assert(state != nullptr);
1411 :
1412 224892 : ProcessBlockAvailability(nodeid);
1413 :
1414 224892 : const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(hash);
1415 224892 : if (pindex && pindex->nChainWork > 0) {
1416 : // An actually better block was announced.
1417 224501 : if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
1418 219578 : state->pindexBestKnownBlock = pindex;
1419 219578 : }
1420 224501 : } else {
1421 : // An unknown block was announced; just assume that the latest one is the best one.
1422 391 : state->hashLastUnknownBlock = hash;
1423 : }
1424 224892 : }
1425 :
1426 2298581 : void PeerManagerImpl::FindNextBlocksToDownload(const Peer& peer, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller)
1427 : {
1428 2298581 : if (count == 0)
1429 0 : return;
1430 :
1431 2298581 : vBlocks.reserve(vBlocks.size() + count);
1432 2298581 : CNodeState *state = State(peer.m_id);
1433 2298581 : assert(state != nullptr);
1434 :
1435 : // Make sure pindexBestKnownBlock is up to date, we'll need it.
1436 2298581 : ProcessBlockAvailability(peer.m_id);
1437 :
1438 2298581 : if (state->pindexBestKnownBlock == nullptr || state->pindexBestKnownBlock->nChainWork < m_chainman.ActiveChain().Tip()->nChainWork || state->pindexBestKnownBlock->nChainWork < nMinimumChainWork) {
1439 : // This peer has nothing interesting.
1440 1124457 : return;
1441 : }
1442 :
1443 1174124 : if (state->pindexLastCommonBlock == nullptr) {
1444 : // Bootstrap quickly by guessing a parent of our best tip is the forking point.
1445 : // Guessing wrong in either direction is not a problem.
1446 6231 : state->pindexLastCommonBlock = m_chainman.ActiveChain()[std::min(state->pindexBestKnownBlock->nHeight, m_chainman.ActiveChain().Height())];
1447 6231 : }
1448 :
1449 : // If the peer reorganized, our previous pindexLastCommonBlock may not be an ancestor
1450 : // of its current tip anymore. Go back enough to fix that.
1451 1174124 : state->pindexLastCommonBlock = LastCommonAncestor(state->pindexLastCommonBlock, state->pindexBestKnownBlock);
1452 1174124 : if (state->pindexLastCommonBlock == state->pindexBestKnownBlock)
1453 892277 : return;
1454 :
1455 281847 : std::vector<const CBlockIndex*> vToFetch;
1456 281847 : const CBlockIndex *pindexWalk = state->pindexLastCommonBlock;
1457 : // Never fetch further than the best block we know the peer has, or more than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last
1458 : // linked block we have in common with this peer. The +1 is so we can detect stalling, namely if we would be able to
1459 : // download that next block if the window were 1 larger.
1460 281847 : int nWindowEnd = state->pindexLastCommonBlock->nHeight + BLOCK_DOWNLOAD_WINDOW;
1461 281847 : int nMaxHeight = std::min<int>(state->pindexBestKnownBlock->nHeight, nWindowEnd + 1);
1462 281847 : NodeId waitingfor = -1;
1463 555055 : while (pindexWalk->nHeight < nMaxHeight) {
1464 : // Read up to 128 (or more, if more blocks than that are needed) successors of pindexWalk (towards
1465 : // pindexBestKnownBlock) into vToFetch. We fetch 128, because CBlockIndex::GetAncestor may be as expensive
1466 : // as iterating over ~100 CBlockIndex* entries anyway.
1467 304351 : int nToFetch = std::min(nMaxHeight - pindexWalk->nHeight, std::max<int>(count - vBlocks.size(), 128));
1468 304351 : vToFetch.resize(nToFetch);
1469 304351 : pindexWalk = state->pindexBestKnownBlock->GetAncestor(pindexWalk->nHeight + nToFetch);
1470 304351 : vToFetch[nToFetch - 1] = pindexWalk;
1471 6414611 : for (unsigned int i = nToFetch - 1; i > 0; i--) {
1472 6110260 : vToFetch[i - 1] = vToFetch[i]->pprev;
1473 6110260 : }
1474 :
1475 : // Iterate over those blocks in vToFetch (in forward direction), adding the ones that
1476 : // are not yet downloaded and not in flight to vBlocks. In the meantime, update
1477 : // pindexLastCommonBlock as long as all ancestors are already downloaded, or if it's
1478 : // already part of our chain (and therefore don't need it even if pruned).
1479 4467307 : for (const CBlockIndex* pindex : vToFetch) {
1480 4194099 : if (!pindex->IsValid(BLOCK_VALID_TREE)) {
1481 : // We consider the chain that this peer is on invalid.
1482 2984 : return;
1483 : }
1484 4191115 : if (pindex->nStatus & BLOCK_HAVE_DATA || m_chainman.ActiveChain().Contains(pindex)) {
1485 3283089 : if (pindex->HaveTxsDownloaded())
1486 335596 : state->pindexLastCommonBlock = pindex;
1487 4191115 : } else if (!IsBlockRequested(pindex->GetBlockHash())) {
1488 : // The block is not already downloaded, and not yet in flight.
1489 33783 : if (pindex->nHeight > nWindowEnd) {
1490 : // We reached the end of the window.
1491 545 : if (vBlocks.size() == 0 && waitingfor != peer.m_id) {
1492 : // We aren't able to fetch anything, but we would be if the download window was one larger.
1493 386 : nodeStaller = waitingfor;
1494 386 : }
1495 545 : return;
1496 : }
1497 33238 : vBlocks.push_back(pindex);
1498 33238 : if (vBlocks.size() == count) {
1499 27614 : return;
1500 : }
1501 879867 : } else if (waitingfor == -1) {
1502 : // This is the first already-in-flight block.
1503 142940 : waitingfor = mapBlocksInFlight[pindex->GetBlockHash()].first;
1504 142940 : }
1505 : }
1506 : }
1507 2298581 : }
1508 : } // namespace
1509 :
1510 9786 : void PeerManagerImpl::PushNodeVersion(CNode& pnode, const Peer& peer)
1511 : {
1512 9786 : const auto& params = Params();
1513 :
1514 9786 : uint64_t my_services{peer.m_our_services};
1515 9786 : const int64_t nTime{count_seconds(GetTime<std::chrono::seconds>())};
1516 9786 : uint64_t nonce = pnode.GetLocalNonce();
1517 9786 : const int nNodeStartingHeight{m_best_height};
1518 9786 : NodeId nodeid = pnode.GetId();
1519 9786 : CAddress addr = pnode.addr;
1520 :
1521 9786 : CService addr_you = addr.IsRoutable() && !IsProxy(addr) && addr.IsAddrV1Compatible() ? addr : CService();
1522 9786 : uint64_t your_services{addr.nServices};
1523 :
1524 9786 : uint256 mnauthChallenge;
1525 9786 : GetRandBytes({mnauthChallenge.begin(), mnauthChallenge.size()});
1526 9786 : pnode.SetSentMNAuthChallenge(mnauthChallenge);
1527 :
1528 9786 : int nProtocolVersion = PROTOCOL_VERSION;
1529 19552 : if (params.NetworkIDString() != CBaseChainParams::MAIN && gArgs.IsArgSet("-pushversion")) {
1530 28 : nProtocolVersion = gArgs.GetIntArg("-pushversion", PROTOCOL_VERSION);
1531 28 : }
1532 :
1533 9786 : const bool tx_relay{!RejectIncomingTxs(pnode)};
1534 19572 : m_connman.PushMessage(&pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERSION, nProtocolVersion, my_services, nTime,
1535 : your_services, addr_you, // Together the pre-version-31402 serialization of CAddress "addrYou" (without nTime)
1536 9786 : my_services, CService(), // Together the pre-version-31402 serialization of CAddress "addrMe" (without nTime)
1537 9786 : nonce, strSubVersion, nNodeStartingHeight, tx_relay, mnauthChallenge, pnode.m_masternode_connection.load()));
1538 :
1539 9786 : if (fLogIPs) {
1540 8 : LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, them=%s, txrelay=%d, peer=%d\n", nProtocolVersion, nNodeStartingHeight, addr_you.ToStringAddrPort(), tx_relay, nodeid);
1541 8 : } else {
1542 9778 : LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, txrelay=%d, peer=%d\n", nProtocolVersion, nNodeStartingHeight, tx_relay, nodeid);
1543 : }
1544 9786 : }
1545 :
1546 83650 : void PeerManagerImpl::EraseObjectRequest(NodeId nodeid, const CInv& inv)
1547 : {
1548 83650 : AssertLockHeld(cs_main);
1549 :
1550 83650 : CNodeState* state = State(nodeid);
1551 83650 : if (state == nullptr)
1552 0 : return;
1553 :
1554 83650 : LogPrint(BCLog::NET, "%s -- inv=(%s)\n", __func__, inv.ToString());
1555 83650 : g_already_asked_for.erase(inv.hash);
1556 83650 : g_erased_object_requests.insert(std::make_pair(inv.hash, GetTime<std::chrono::microseconds>()));
1557 :
1558 83650 : state->m_object_download.m_object_announced.erase(inv);
1559 83650 : state->m_object_download.m_object_in_flight.erase(inv);
1560 83650 : }
1561 :
1562 138955 : std::chrono::microseconds GetObjectRequestTime(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1563 : {
1564 138955 : AssertLockHeld(cs_main);
1565 138955 : auto it = g_already_asked_for.find(inv.hash);
1566 138955 : if (it != g_already_asked_for.end()) {
1567 15560 : return it->second;
1568 : }
1569 123395 : return {};
1570 138955 : }
1571 :
1572 51696 : void UpdateObjectRequestTime(const CInv& inv, std::chrono::microseconds request_time) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1573 : {
1574 51696 : AssertLockHeld(cs_main);
1575 51696 : auto it = g_already_asked_for.find(inv.hash);
1576 51696 : if (it == g_already_asked_for.end()) {
1577 51574 : g_already_asked_for.insert(std::make_pair(inv.hash, request_time));
1578 51574 : } else {
1579 122 : g_already_asked_for.update(it, request_time);
1580 : }
1581 51696 : }
1582 :
1583 98919 : std::chrono::microseconds GetObjectInterval(int invType)
1584 : {
1585 : // some messages need to be re-requested faster when the first announcing peer did not answer to GETDATA
1586 98919 : switch(invType)
1587 : {
1588 : case MSG_QUORUM_RECOVERED_SIG:
1589 7480 : return 15s;
1590 : case MSG_CLSIG:
1591 14205 : return 5s;
1592 : case MSG_ISDLOCK:
1593 354 : return 10s;
1594 : default:
1595 76880 : return GETDATA_TX_INTERVAL;
1596 : }
1597 98919 : }
1598 :
1599 31785 : std::chrono::microseconds GetObjectExpiryInterval(int invType)
1600 : {
1601 31785 : return GetObjectInterval(invType) * TX_EXPIRY_INTERVAL_FACTOR;
1602 : }
1603 :
1604 12589 : std::chrono::microseconds GetObjectRandomDelay(int invType)
1605 : {
1606 12589 : if (invType == MSG_TX) {
1607 742 : return GetRandMicros(MAX_GETDATA_RANDOM_DELAY);
1608 : }
1609 11847 : return {};
1610 12589 : }
1611 :
1612 84410 : std::chrono::microseconds CalculateObjectGetDataTime(const CInv& inv, std::chrono::microseconds current_time, bool is_masternode, bool use_inbound_delay) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1613 : {
1614 84410 : AssertLockHeld(cs_main);
1615 : std::chrono::microseconds process_time;
1616 84410 : const auto last_request_time = GetObjectRequestTime(inv);
1617 : // First time requesting this tx
1618 84410 : if (last_request_time.count() == 0) {
1619 71821 : process_time = current_time;
1620 71821 : } else {
1621 : // Randomize the delay to avoid biasing some peers over others (such as due to
1622 : // fixed ordering of peer processing in ThreadMessageHandler)
1623 12589 : process_time = last_request_time + GetObjectInterval(inv.type) + GetObjectRandomDelay(inv.type);
1624 : }
1625 :
1626 : // We delay processing announcements from inbound peers
1627 84410 : if (inv.IsMsgTx() && !is_masternode && use_inbound_delay) process_time += INBOUND_PEER_TX_DELAY;
1628 :
1629 84410 : return process_time;
1630 : }
1631 :
1632 81584 : void PeerManagerImpl::RequestObject(NodeId nodeid, const CInv& inv, std::chrono::microseconds current_time, bool fForce)
1633 : {
1634 81584 : AssertLockHeld(cs_main);
1635 :
1636 81584 : CNodeState* state = State(nodeid);
1637 81584 : if (state == nullptr)
1638 0 : return;
1639 :
1640 81584 : CNodeState::ObjectDownloadState& peer_download_state = state->m_object_download;
1641 163168 : if (peer_download_state.m_object_announced.size() >= MAX_PEER_OBJECT_ANNOUNCEMENTS ||
1642 81584 : peer_download_state.m_object_process_time.size() >= MAX_PEER_OBJECT_ANNOUNCEMENTS ||
1643 81584 : peer_download_state.m_object_announced.count(inv)) {
1644 : // Too many queued announcements from this peer, or we already have
1645 : // this announcement
1646 23 : return;
1647 : }
1648 81561 : peer_download_state.m_object_announced.insert(inv);
1649 :
1650 : // Calculate the time to try requesting this transaction. Use
1651 : // fPreferredDownload as a proxy for outbound peers.
1652 163122 : std::chrono::microseconds process_time = CalculateObjectGetDataTime(inv, current_time, /*is_masternode=*/m_nodeman != nullptr,
1653 81561 : !state->fPreferredDownload);
1654 :
1655 81561 : peer_download_state.m_object_process_time.emplace(process_time, inv);
1656 :
1657 81561 : if (fForce) {
1658 : // make sure this object is actually requested ASAP
1659 22 : g_erased_object_requests.erase(inv.hash);
1660 22 : g_already_asked_for.erase(inv.hash);
1661 22 : }
1662 :
1663 81561 : LogPrint(BCLog::NET, "%s -- inv=(%s), current_time=%d, process_time=%d, delta=%d\n", __func__, inv.ToString(), current_time.count(), process_time.count(), (process_time - current_time).count());
1664 81584 : }
1665 :
1666 9448 : size_t PeerManagerImpl::GetRequestedObjectCount(NodeId nodeid) const
1667 : {
1668 9448 : AssertLockHeld(cs_main);
1669 :
1670 9448 : const CNodeState* state = State(nodeid);
1671 9448 : if (state == nullptr)
1672 0 : return 0;
1673 :
1674 9448 : return state->m_object_download.m_object_process_time.size();
1675 9448 : }
1676 :
1677 17802 : void PeerManagerImpl::AddExtraHandler(std::unique_ptr<NetHandler>&& handler)
1678 : {
1679 17802 : assert(handler != nullptr);
1680 17802 : if (auto i = dynamic_cast<CValidationInterface*>(handler.get()); i != nullptr) {
1681 8571 : RegisterValidationInterface(i);
1682 8571 : }
1683 17802 : m_handlers.emplace_back(std::move(handler));
1684 17802 : }
1685 :
1686 3040 : void PeerManagerImpl::RemoveHandlers()
1687 : {
1688 3040 : InterruptHandlers();
1689 3040 : StopHandlers();
1690 3040 : m_handlers.clear();
1691 3040 : }
1692 :
1693 2831 : void PeerManagerImpl::StartHandlers()
1694 : {
1695 20477 : for (auto& handler : m_handlers) {
1696 17646 : handler->Start();
1697 : }
1698 2831 : }
1699 :
1700 5897 : void PeerManagerImpl::StopHandlers()
1701 : {
1702 41501 : for (auto& handler : m_handlers) {
1703 35604 : if (auto i = dynamic_cast<CValidationInterface*>(handler.get()); i != nullptr) {
1704 17142 : UnregisterValidationInterface(i);
1705 17142 : }
1706 35604 : handler->Stop();
1707 : }
1708 5897 : }
1709 :
1710 5897 : void PeerManagerImpl::InterruptHandlers()
1711 : {
1712 41501 : for (auto& handler : m_handlers) {
1713 35604 : handler->Interrupt();
1714 : }
1715 5897 : }
1716 :
1717 2831 : void PeerManagerImpl::ScheduleHandlers(CScheduler& scheduler)
1718 : {
1719 20477 : for (auto& handler : m_handlers) {
1720 17646 : handler->Schedule(scheduler);
1721 : }
1722 2831 : }
1723 :
1724 1 : void PeerManagerImpl::UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds)
1725 : {
1726 1 : LOCK(cs_main);
1727 1 : CNodeState *state = State(node);
1728 1 : if (state) state->m_last_block_announcement = time_in_seconds;
1729 1 : }
1730 :
1731 9973 : void PeerManagerImpl::InitializeNode(CNode& node, ServiceFlags our_services) {
1732 9973 : NodeId nodeid = node.GetId();
1733 : {
1734 9973 : LOCK(cs_main);
1735 9973 : m_node_states.emplace_hint(m_node_states.end(), std::piecewise_construct, std::forward_as_tuple(nodeid), std::forward_as_tuple(node.IsInboundConn()));
1736 9973 : }
1737 9973 : PeerRef peer = std::make_shared<Peer>(nodeid, our_services);
1738 : {
1739 9973 : LOCK(m_peer_mutex);
1740 9973 : m_peer_map.emplace_hint(m_peer_map.end(), nodeid, peer);
1741 9973 : }
1742 9973 : if (!node.IsInboundConn()) {
1743 4810 : PushNodeVersion(node, *peer);
1744 4810 : }
1745 9973 : }
1746 :
1747 1450 : void PeerManagerImpl::ReattemptInitialBroadcast(CScheduler& scheduler)
1748 : {
1749 1450 : std::set<uint256> unbroadcast_txids = m_mempool.GetUnbroadcastTxs();
1750 :
1751 1524 : for (const uint256& txid : unbroadcast_txids) {
1752 74 : CTransactionRef tx = m_mempool.get(txid);
1753 :
1754 74 : if (tx != nullptr) {
1755 74 : LOCK(cs_main);
1756 74 : _RelayTransaction(txid);
1757 74 : } else {
1758 0 : m_mempool.RemoveUnbroadcastTx(txid, true);
1759 : }
1760 74 : }
1761 :
1762 : // Schedule next run for 10-15 minutes in the future.
1763 : // We add randomness on every cycle to avoid the possibility of P2P fingerprinting.
1764 1450 : const std::chrono::milliseconds delta = 10min + GetRandMillis(5min);
1765 2585 : scheduler.scheduleFromNow([&] { ReattemptInitialBroadcast(scheduler); }, delta);
1766 1450 : }
1767 :
1768 9972 : void PeerManagerImpl::FinalizeNode(const CNode& node) {
1769 9972 : NodeId nodeid = node.GetId();
1770 9972 : int misbehavior{0};
1771 9972 : LOCK(cs_main);
1772 : {
1773 : {
1774 : // We remove the PeerRef from g_peer_map here, but we don't always
1775 : // destruct the Peer. Sometimes another thread is still holding a
1776 : // PeerRef, so the refcount is >= 1. Be careful not to do any
1777 : // processing here that assumes Peer won't be changed before it's
1778 : // destructed.
1779 9972 : PeerRef peer = RemovePeer(nodeid);
1780 9972 : assert(peer != nullptr);
1781 19944 : misbehavior = WITH_LOCK(peer->m_misbehavior_mutex, return peer->m_misbehavior_score);
1782 9972 : }
1783 9972 : CNodeState *state = State(nodeid);
1784 9972 : assert(state != nullptr);
1785 :
1786 9972 : if (state->fSyncStarted)
1787 8018 : nSyncStarted--;
1788 :
1789 10173 : for (const QueuedBlock& entry : state->vBlocksInFlight) {
1790 201 : mapBlocksInFlight.erase(entry.pindex->GetBlockHash());
1791 : }
1792 9972 : m_orphanage.EraseForPeer(nodeid);
1793 9972 : if (m_txreconciliation) m_txreconciliation->ForgetPeer(nodeid);
1794 9972 : m_num_preferred_download_peers -= state->fPreferredDownload;
1795 9972 : m_peers_downloading_from -= (state->nBlocksInFlight != 0);
1796 9972 : assert(m_peers_downloading_from >= 0);
1797 9972 : m_outbound_peers_with_protect_from_disconnect -= state->m_chain_sync.m_protect;
1798 9972 : assert(m_outbound_peers_with_protect_from_disconnect >= 0);
1799 :
1800 9972 : m_node_states.erase(nodeid);
1801 :
1802 9972 : if (m_node_states.empty()) {
1803 : // Do a consistency check after the last peer is removed.
1804 2286 : assert(mapBlocksInFlight.empty());
1805 2286 : assert(m_num_preferred_download_peers == 0);
1806 2286 : assert(m_peers_downloading_from == 0);
1807 2286 : assert(m_outbound_peers_with_protect_from_disconnect == 0);
1808 2286 : assert(m_orphanage.Size() == 0);
1809 2286 : }
1810 : } // cs_main
1811 :
1812 9972 : if (node.fSuccessfullyConnected && misbehavior == 0 && !node.IsBlockOnlyConn() && !node.IsInboundConn()) {
1813 : // Only change visible addrman state for full outbound peers. We don't
1814 : // call Connected() for feeler connections since they don't have
1815 : // fSuccessfullyConnected set.
1816 3960 : m_addrman.Connected(node.addr);
1817 3960 : }
1818 :
1819 9972 : LogPrint(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid);
1820 9972 : }
1821 :
1822 7826872 : PeerRef PeerManagerImpl::GetPeerRef(NodeId id) const
1823 : {
1824 7826872 : READ_LOCK(m_peer_mutex);
1825 7826872 : auto it = m_peer_map.find(id);
1826 7826845 : return it != m_peer_map.end() ? it->second : nullptr;
1827 7826884 : }
1828 :
1829 9972 : PeerRef PeerManagerImpl::RemovePeer(NodeId id)
1830 : {
1831 9972 : PeerRef ret;
1832 9972 : LOCK(m_peer_mutex);
1833 9972 : auto it = m_peer_map.find(id);
1834 9972 : if (it != m_peer_map.end()) {
1835 9972 : ret = std::move(it->second);
1836 9972 : m_peer_map.erase(it);
1837 9972 : }
1838 9972 : return ret;
1839 9972 : }
1840 :
1841 86140 : bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const
1842 : {
1843 : {
1844 86140 : LOCK(cs_main);
1845 86140 : const CNodeState* state = State(nodeid);
1846 86140 : if (state == nullptr)
1847 0 : return false;
1848 86140 : stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
1849 86140 : stats.nCommonHeight = state->pindexLastCommonBlock ? state->pindexLastCommonBlock->nHeight : -1;
1850 105038 : for (const QueuedBlock& queue : state->vBlocksInFlight) {
1851 18898 : if (queue.pindex)
1852 18898 : stats.vHeightInFlight.push_back(queue.pindex->nHeight);
1853 : }
1854 86140 : }
1855 :
1856 86140 : PeerRef peer = GetPeerRef(nodeid);
1857 86140 : if (peer == nullptr) return false;
1858 172280 : stats.m_misbehavior_score = WITH_LOCK(peer->m_misbehavior_mutex, return peer->m_misbehavior_score);
1859 86140 : stats.their_services = peer->m_their_services;
1860 86140 : stats.m_starting_height = peer->m_starting_height;
1861 : // It is common for nodes with good ping times to suddenly become lagged,
1862 : // due to a new block arriving or other large transfer.
1863 : // Merely reporting pingtime might fool the caller into thinking the node was still responsive,
1864 : // since pingtime does not update until the ping is complete, which might take a while.
1865 : // So, if a ping is taking an unusually long time in flight,
1866 : // the caller can immediately detect that this is happening.
1867 86140 : auto ping_wait{0us};
1868 86140 : if ((0 != peer->m_ping_nonce_sent) && (0 != peer->m_ping_start.load().count())) {
1869 3172 : ping_wait = GetTime<std::chrono::microseconds>() - peer->m_ping_start.load();
1870 3172 : }
1871 :
1872 86140 : if (auto tx_relay = peer->GetTxRelay(); tx_relay != nullptr) {
1873 171922 : stats.m_relay_txs = WITH_LOCK(tx_relay->m_bloom_filter_mutex, return tx_relay->m_relay_txs);
1874 85961 : } else {
1875 179 : stats.m_relay_txs = false;
1876 : }
1877 :
1878 86140 : stats.m_ping_wait = ping_wait;
1879 86140 : stats.m_addr_processed = peer->m_addr_processed.load();
1880 86140 : stats.m_addr_rate_limited = peer->m_addr_rate_limited.load();
1881 86140 : stats.m_addr_relay_enabled = peer->m_addr_relay_enabled.load();
1882 :
1883 86140 : return true;
1884 86140 : }
1885 :
1886 1946 : void PeerManagerImpl::AddToCompactExtraTransactions(const CTransactionRef& tx)
1887 : {
1888 1946 : size_t max_extra_txn = gArgs.GetIntArg("-blockreconstructionextratxn", DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN);
1889 1946 : if (max_extra_txn <= 0)
1890 0 : return;
1891 1946 : if (!vExtraTxnForCompact.size())
1892 24 : vExtraTxnForCompact.resize(max_extra_txn);
1893 1946 : vExtraTxnForCompact[vExtraTxnForCompactIt] = std::make_pair(tx->GetHash(), tx);
1894 1946 : vExtraTxnForCompactIt = (vExtraTxnForCompactIt + 1) % max_extra_txn;
1895 1946 : }
1896 :
1897 3588 : void PeerManagerImpl::Misbehaving(const NodeId pnode, const int howmuch, const std::string& message)
1898 : {
1899 3588 : assert(howmuch > 0);
1900 :
1901 3588 : PeerRef peer = GetPeerRef(pnode);
1902 3588 : if (peer == nullptr) return;
1903 :
1904 3586 : LOCK(peer->m_misbehavior_mutex);
1905 3586 : const int score_before{peer->m_misbehavior_score};
1906 3586 : peer->m_misbehavior_score += howmuch;
1907 3586 : const int score_now{peer->m_misbehavior_score};
1908 :
1909 3586 : const std::string message_prefixed = message.empty() ? "" : (": " + message);
1910 3586 : std::string warning;
1911 :
1912 3586 : if (score_now >= DISCOURAGEMENT_THRESHOLD && score_before < DISCOURAGEMENT_THRESHOLD) {
1913 265 : warning = " DISCOURAGE THRESHOLD EXCEEDED";
1914 265 : peer->m_should_discourage = true;
1915 265 : ::g_stats_client->inc("misbehavior.banned", 1.0f);
1916 265 : } else {
1917 3321 : ::g_stats_client->count("misbehavior.amount", howmuch, 1.0);
1918 : }
1919 :
1920 3586 : LogPrint(BCLog::NET, "Misbehaving: peer=%d (%d -> %d)%s%s\n",
1921 : pnode, score_before, score_now, warning, message_prefixed);
1922 3588 : }
1923 :
1924 277161 : bool PeerManagerImpl::IsBanned(NodeId pnode)
1925 : {
1926 277161 : PeerRef peer = GetPeerRef(pnode);
1927 277161 : if (peer == nullptr)
1928 7763 : return false;
1929 269398 : LOCK(peer->m_misbehavior_mutex);
1930 269398 : if (peer->m_should_discourage) {
1931 2 : return true;
1932 : }
1933 269396 : return false;
1934 277161 : }
1935 :
1936 3529 : bool PeerManagerImpl::MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState& state,
1937 : bool via_compact_block, const std::string& message)
1938 : {
1939 3529 : switch (state.GetResult()) {
1940 : case BlockValidationResult::BLOCK_RESULT_UNSET:
1941 112 : break;
1942 : // The node is providing invalid data:
1943 : case BlockValidationResult::BLOCK_CONSENSUS:
1944 : case BlockValidationResult::BLOCK_MUTATED:
1945 3058 : if (!via_compact_block) {
1946 3027 : Misbehaving(nodeid, 100, message);
1947 3027 : return true;
1948 : }
1949 31 : break;
1950 : case BlockValidationResult::BLOCK_CACHED_INVALID:
1951 : {
1952 272 : LOCK(cs_main);
1953 272 : CNodeState *node_state = State(nodeid);
1954 272 : if (node_state == nullptr) {
1955 0 : break;
1956 : }
1957 :
1958 : // Discourage outbound (but not inbound) peers if on an invalid chain.
1959 : // Exempt HB compact block peers. Manual connections are always protected from discouragement.
1960 272 : if (!via_compact_block && !node_state->m_is_inbound) {
1961 9 : Misbehaving(nodeid, 100, message);
1962 9 : return true;
1963 : }
1964 263 : break;
1965 272 : }
1966 : case BlockValidationResult::BLOCK_INVALID_HEADER:
1967 : case BlockValidationResult::BLOCK_CHECKPOINT:
1968 : case BlockValidationResult::BLOCK_INVALID_PREV:
1969 27 : Misbehaving(nodeid, 100, message);
1970 27 : return true;
1971 : // Conflicting (but not necessarily invalid) data or different policy:
1972 : case BlockValidationResult::BLOCK_MISSING_PREV:
1973 : case BlockValidationResult::BLOCK_CHAINLOCK:
1974 53 : Misbehaving(nodeid, 10, message);
1975 53 : return true;
1976 : case BlockValidationResult::BLOCK_RECENT_CONSENSUS_CHANGE:
1977 : case BlockValidationResult::BLOCK_TIME_FUTURE:
1978 7 : break;
1979 : }
1980 413 : if (message != "") {
1981 263 : LogPrint(BCLog::NET, "peer=%d: %s\n", nodeid, message);
1982 263 : }
1983 413 : return false;
1984 3529 : }
1985 :
1986 1989 : bool PeerManagerImpl::MaybePunishNodeForTx(NodeId nodeid, const TxValidationState& state) {
1987 1989 : switch (state.GetResult()) {
1988 : case TxValidationResult::TX_RESULT_UNSET:
1989 0 : break;
1990 : // The node is providing invalid data:
1991 : case TxValidationResult::TX_CONSENSUS:
1992 44 : Misbehaving(nodeid, 100);
1993 44 : return true;
1994 : // Conflicting (but not necessarily invalid) data or different policy:
1995 : case TxValidationResult::TX_RECENT_CONSENSUS_CHANGE:
1996 : case TxValidationResult::TX_INPUTS_NOT_STANDARD:
1997 : case TxValidationResult::TX_NOT_STANDARD:
1998 : case TxValidationResult::TX_MISSING_INPUTS:
1999 : case TxValidationResult::TX_PREMATURE_SPEND:
2000 : case TxValidationResult::TX_CONFLICT:
2001 : case TxValidationResult::TX_MEMPOOL_POLICY:
2002 : case TxValidationResult::TX_NO_MEMPOOL:
2003 : // moved from BLOCK
2004 : case TxValidationResult::TX_BAD_SPECIAL:
2005 : case TxValidationResult::TX_CONFLICT_LOCK:
2006 1945 : break;
2007 : }
2008 1945 : return false;
2009 1989 : }
2010 :
2011 54769 : bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex)
2012 : {
2013 54769 : AssertLockHeld(cs_main);
2014 54769 : if (m_chainman.ActiveChain().Contains(pindex)) return true;
2015 38 : return pindex->IsValid(BLOCK_VALID_SCRIPTS) && (m_chainman.m_best_header != nullptr) &&
2016 17 : (m_chainman.m_best_header->GetBlockTime() - pindex->GetBlockTime() < STALE_RELAY_AGE_LIMIT) &&
2017 13 : (GetBlockProofEquivalentTime(*m_chainman.m_best_header, *pindex, *m_chainman.m_best_header, m_chainparams.GetConsensus()) < STALE_RELAY_AGE_LIMIT);
2018 54769 : }
2019 :
2020 6 : std::optional<std::string> PeerManagerImpl::FetchBlock(NodeId peer_id, const CBlockIndex& block_index)
2021 : {
2022 6 : if (fImporting) return "Importing...";
2023 6 : if (fReindex) return "Reindexing...";
2024 :
2025 : // Ensure this peer exists and hasn't been disconnected
2026 6 : PeerRef peer = GetPeerRef(peer_id);
2027 6 : if (peer == nullptr) return "Peer does not exist";
2028 :
2029 2 : LOCK(cs_main);
2030 : // Mark block as in-flight unless it already is (for this peer).
2031 : // If the peer does not send us a block, vBlocksInFlight remains non-empty,
2032 : // causing us to timeout and disconnect.
2033 : // If a block was already in-flight for a different peer, its BLOCKTXN
2034 : // response will be dropped.
2035 2 : if (!BlockRequested(peer_id, block_index)) return "Already requested from this peer";
2036 :
2037 : // Construct message to request the block
2038 2 : const uint256& hash{block_index.GetBlockHash()};
2039 2 : std::vector<CInv> invs{CInv(MSG_BLOCK, hash)};
2040 :
2041 : // Send block request message to the peer
2042 4 : bool success = m_connman.ForNode(peer_id, [this, &invs](CNode* node) {
2043 2 : const CNetMsgMaker msgMaker(node->GetCommonVersion());
2044 2 : this->m_connman.PushMessage(node, msgMaker.Make(NetMsgType::GETDATA, invs));
2045 2 : return true;
2046 0 : });
2047 :
2048 2 : if (!success) return "Peer not fully connected";
2049 :
2050 2 : LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n",
2051 : hash.ToString(), peer_id);
2052 2 : return std::nullopt;
2053 6 : }
2054 :
2055 3040 : std::unique_ptr<PeerManager> PeerManager::make(const CChainParams& chainparams, CConnman& connman, AddrMan& addrman,
2056 : BanMan* banman, CDSTXManager& dstxman, ChainstateManager& chainman,
2057 : CTxMemPool& pool, CMasternodeMetaMan& mn_metaman,
2058 : CMasternodeSync& mn_sync,
2059 : CSporkManager& sporkman, const chainlock::Chainlocks& chainlocks,
2060 : chainlock::ChainlockHandler& clhandler,
2061 : CActiveMasternodeManager* nodeman,
2062 : const std::unique_ptr<CDeterministicMNManager>& dmnman,
2063 : const std::unique_ptr<CJWalletManager>& cj_walletman,
2064 : const std::unique_ptr<LLMQContext>& llmq_ctx, bool ignore_incoming_txs)
2065 : {
2066 3040 : return std::make_unique<PeerManagerImpl>(chainparams, connman, addrman, banman, dstxman, chainman, pool, mn_metaman, mn_sync, sporkman, chainlocks, clhandler, nodeman, dmnman, cj_walletman, llmq_ctx, ignore_incoming_txs);
2067 : }
2068 :
2069 12160 : PeerManagerImpl::PeerManagerImpl(const CChainParams& chainparams, CConnman& connman, AddrMan& addrman, BanMan* banman,
2070 : CDSTXManager& dstxman, ChainstateManager& chainman, CTxMemPool& pool,
2071 : CMasternodeMetaMan& mn_metaman, CMasternodeSync& mn_sync,
2072 : CSporkManager& sporkman,
2073 : const chainlock::Chainlocks& chainlocks,
2074 : chainlock::ChainlockHandler& clhandler,
2075 : CActiveMasternodeManager* nodeman,
2076 : const std::unique_ptr<CDeterministicMNManager>& dmnman,
2077 : const std::unique_ptr<CJWalletManager>& cj_walletman,
2078 : const std::unique_ptr<LLMQContext>& llmq_ctx, bool ignore_incoming_txs)
2079 3040 : : m_chainparams(chainparams),
2080 3040 : m_connman(connman),
2081 3040 : m_addrman(addrman),
2082 3040 : m_banman(banman),
2083 3040 : m_dstxman(dstxman),
2084 3040 : m_chainman(chainman),
2085 3040 : m_mempool(pool),
2086 3040 : m_nodeman(nodeman),
2087 3040 : m_dmnman(dmnman),
2088 3040 : m_cj_walletman(cj_walletman),
2089 3040 : m_llmq_ctx(llmq_ctx),
2090 3040 : m_mn_metaman(mn_metaman),
2091 3040 : m_mn_sync(mn_sync),
2092 3040 : m_sporkman(sporkman),
2093 3040 : m_chainlocks(chainlocks),
2094 3040 : m_clhandler{clhandler},
2095 3040 : m_ignore_incoming_txs(ignore_incoming_txs)
2096 6080 : {
2097 : // While Erlay support is incomplete, it must be enabled explicitly via -txreconciliation.
2098 : // This argument can go away after Erlay support is complete.
2099 3040 : if (gArgs.GetBoolArg("-txreconciliation", DEFAULT_TXRECONCILIATION_ENABLE)) {
2100 10 : m_txreconciliation = std::make_unique<TxReconciliationTracker>(TXRECONCILIATION_VERSION);
2101 10 : }
2102 6080 : }
2103 :
2104 2821 : void PeerManagerImpl::StartScheduledTasks(CScheduler& scheduler)
2105 : {
2106 : // Stale tip checking and peer eviction are on two different timers, but we
2107 : // don't want them to get out of sync due to drift in the scheduler, so we
2108 : // combine them in one function and schedule at the quicker (peer-eviction)
2109 : // timer.
2110 : static_assert(EXTRA_PEER_CHECK_INTERVAL < STALE_CHECK_INTERVAL, "peer eviction timer should be less than stale tip check timer");
2111 8887 : scheduler.scheduleEvery([this] { this->CheckForStaleTipAndEvictPeers(); }, std::chrono::seconds{EXTRA_PEER_CHECK_INTERVAL});
2112 :
2113 : // schedule next run for 10-15 minutes in the future
2114 2821 : const std::chrono::milliseconds delta = 10min + GetRandMillis(5min);
2115 3136 : scheduler.scheduleFromNow([&] { ReattemptInitialBroadcast(scheduler); }, delta);
2116 2821 : }
2117 :
2118 : /**
2119 : * Evict orphan txn pool entries based on a newly connected
2120 : * block. Also save the time of the last tip update and
2121 : * possibly reduce dynamic block stalling timeout.
2122 : */
2123 228002 : void PeerManagerImpl::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
2124 : {
2125 : // Candidates are sourced from a block and therefore cannot be attributed to a peer, we use -1 as the identifier
2126 228002 : bool have_candidates{true};
2127 : {
2128 228002 : LOCK(::cs_main);
2129 228002 : m_orphanage.SetCandidatesByBlock(*pblock);
2130 : // Keep processing as valid orphans may enable processing of their descendants
2131 456016 : while (have_candidates) {
2132 228014 : have_candidates = ProcessOrphanTx(/*node_id=*/-1);
2133 : }
2134 228002 : }
2135 :
2136 228002 : m_orphanage.EraseForBlock(*pblock);
2137 228002 : m_last_tip_update = GetTime<std::chrono::seconds>();
2138 :
2139 : {
2140 228002 : LOCK(m_recent_confirmed_transactions_mutex);
2141 721023 : for (const auto& ptx : pblock->vtx) {
2142 493021 : m_recent_confirmed_transactions.insert(ptx->GetHash());
2143 : }
2144 228002 : }
2145 :
2146 : // In case the dynamic timeout was doubled once or more, reduce it slowly back to its default value
2147 228002 : auto stalling_timeout = m_block_stalling_timeout.load();
2148 228002 : Assume(stalling_timeout >= BLOCK_STALLING_TIMEOUT_DEFAULT);
2149 228002 : if (stalling_timeout != BLOCK_STALLING_TIMEOUT_DEFAULT) {
2150 32 : const auto new_timeout = std::max(std::chrono::duration_cast<std::chrono::seconds>(stalling_timeout * 0.85), BLOCK_STALLING_TIMEOUT_DEFAULT);
2151 32 : if (m_block_stalling_timeout.compare_exchange_strong(stalling_timeout, new_timeout)) {
2152 32 : LogPrint(BCLog::NET, "Decreased stalling timeout to %d seconds\n", count_seconds(new_timeout));
2153 32 : }
2154 32 : }
2155 228002 : }
2156 :
2157 14230 : void PeerManagerImpl::BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex)
2158 : {
2159 : // To avoid relay problems with transactions that were previously
2160 : // confirmed, clear our filter of recently confirmed transactions whenever
2161 : // there's a reorg.
2162 : // This means that in a 1-block reorg (where 1 block is disconnected and
2163 : // then another block reconnected), our filter will drop to having only one
2164 : // block's worth of transactions in it, but that should be fine, since
2165 : // presumably the most common case of relaying a confirmed transaction
2166 : // should be just after a new block containing it is found.
2167 14230 : LOCK(m_recent_confirmed_transactions_mutex);
2168 14230 : m_recent_confirmed_transactions.reset();
2169 14230 : }
2170 :
2171 : /**
2172 : * Maintain state about the best-seen block and fast-announce a compact block
2173 : * to compatible peers.
2174 : */
2175 201149 : void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) {
2176 201149 : auto pcmpctblock = std::make_shared<const CBlockHeaderAndShortTxIDs>(*pblock);
2177 201149 : const CNetMsgMaker msgMaker(PROTOCOL_VERSION);
2178 :
2179 201149 : LOCK(cs_main);
2180 :
2181 201149 : if (pindex->nHeight <= m_highest_fast_announce)
2182 4230 : return;
2183 196919 : m_highest_fast_announce = pindex->nHeight;
2184 :
2185 196919 : uint256 hashBlock(pblock->GetHash());
2186 196919 : const std::shared_future<CSerializedNetMsg> lazy_ser{
2187 239272 : std::async(std::launch::deferred, [&] { return msgMaker.Make(NetMsgType::CMPCTBLOCK, *pcmpctblock); })};
2188 :
2189 : {
2190 196919 : LOCK(m_most_recent_block_mutex);
2191 196919 : m_most_recent_block_hash = hashBlock;
2192 196919 : m_most_recent_block = pblock;
2193 196919 : m_most_recent_compact_block = pcmpctblock;
2194 196919 : }
2195 :
2196 674539 : m_connman.ForEachNode([this, pindex, &lazy_ser, &hashBlock](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
2197 477620 : AssertLockHeld(::cs_main);
2198 : // TODO: Avoid the repeated-serialization here
2199 477620 : if (pnode->fDisconnect)
2200 0 : return;
2201 477620 : ProcessBlockAvailability(pnode->GetId());
2202 477620 : CNodeState &state = *State(pnode->GetId());
2203 : // If the peer has, or we announced to them the previous block already,
2204 : // but we don't think they have this one, go ahead and announce it
2205 477620 : if (state.m_requested_hb_cmpctblocks && !PeerHasHeader(&state, pindex) && PeerHasHeader(&state, pindex->pprev)) {
2206 99670 : LogPrint(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", "PeerManager::NewPoWValidBlock",
2207 : hashBlock.ToString(), pnode->GetId());
2208 :
2209 99670 : const CSerializedNetMsg& ser_cmpctblock{lazy_ser.get()};
2210 99670 : m_connman.PushMessage(pnode, ser_cmpctblock.Copy());
2211 99670 : state.pindexBestHeaderSent = pindex;
2212 99670 : }
2213 477620 : });
2214 201149 : }
2215 :
2216 : /**
2217 : * Update our best height and announce any block hashes which weren't previously
2218 : * in m_chainman.ActiveChain() to our peers.
2219 : */
2220 220688 : void PeerManagerImpl::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload){
2221 220688 : m_best_height = pindexNew->nHeight;
2222 :
2223 220688 : SetServiceFlagsIBDCache(!fInitialDownload);
2224 :
2225 : // Don't relay inventory during initial block download.
2226 220688 : if (fInitialDownload) return;
2227 :
2228 : // Find the hashes of all blocks that weren't previously in the best chain.
2229 204231 : std::vector<uint256> vHashes;
2230 204231 : const CBlockIndex *pindexToAnnounce = pindexNew;
2231 410815 : while (pindexToAnnounce != pindexFork) {
2232 206885 : vHashes.push_back(pindexToAnnounce->GetBlockHash());
2233 206885 : pindexToAnnounce = pindexToAnnounce->pprev;
2234 206885 : if (vHashes.size() == MAX_BLOCKS_TO_ANNOUNCE) {
2235 : // Limit announcements in case of a huge reorganization.
2236 : // Rely on the peer's synchronization mechanism in that case.
2237 301 : break;
2238 : }
2239 : }
2240 :
2241 : // Relay to all peers
2242 : // TODO: Move CanRelay() to Peer and migrate to iteration through m_peer_map
2243 701366 : m_connman.ForEachNode([this, &vHashes](CNode* pnode) {
2244 497135 : if (!pnode->CanRelay()) return;
2245 :
2246 477599 : PeerRef peer = GetPeerRef(pnode->GetId());
2247 477599 : if (peer == nullptr) return;
2248 :
2249 477599 : LOCK(peer->m_block_inv_mutex);
2250 960258 : for (const uint256& hash : vHashes | std::views::reverse) {
2251 482659 : peer->m_blocks_for_headers_relay.push_back(hash);
2252 : }
2253 497135 : });
2254 204231 : m_connman.WakeMessageHandler();
2255 220688 : }
2256 :
2257 : /**
2258 : * Handle invalid block rejection and consequent peer discouragement, maintain which
2259 : * peers announce compact blocks.
2260 : */
2261 231964 : void PeerManagerImpl::BlockChecked(const CBlock& block, const BlockValidationState& state)
2262 : {
2263 231964 : LOCK(cs_main);
2264 :
2265 231964 : const uint256 hash(block.GetHash());
2266 231964 : std::map<uint256, std::pair<NodeId, bool> >::iterator it = mapBlockSource.find(hash);
2267 :
2268 : // If the block failed validation, we know where it came from and we're still connected
2269 : // to that peer, maybe punish.
2270 235437 : if (state.IsInvalid() &&
2271 3473 : it != mapBlockSource.end() &&
2272 3235 : State(it->second.first)) {
2273 3235 : MaybePunishNodeForBlock(/*nodeid=*/ it->second.first, state, /*via_compact_block=*/ !it->second.second);
2274 3235 : }
2275 : // Check that:
2276 : // 1. The block is valid
2277 : // 2. We're not in initial block download
2278 : // 3. This is currently the best block we're aware of. We haven't updated
2279 : // the tip yet so we have no way to check this directly here. Instead we
2280 : // just check that there are currently no other blocks in flight.
2281 440670 : else if (state.IsValid() &&
2282 228491 : !m_chainman.ActiveChainstate().IsInitialBlockDownload() &&
2283 211941 : mapBlocksInFlight.count(hash) == mapBlocksInFlight.size()) {
2284 163515 : if (it != mapBlockSource.end()) {
2285 96144 : MaybeSetPeerAsAnnouncingHeaderAndIDs(it->second.first);
2286 96144 : }
2287 163515 : }
2288 231964 : if (it != mapBlockSource.end())
2289 152211 : mapBlockSource.erase(it);
2290 231964 : }
2291 :
2292 : //////////////////////////////////////////////////////////////////////////////
2293 : //
2294 : // Messages
2295 : //
2296 :
2297 :
2298 266074 : bool PeerManagerImpl::AlreadyHave(const CInv& inv)
2299 : {
2300 266074 : switch (inv.type)
2301 : {
2302 : case MSG_TX:
2303 : case MSG_DSTX:
2304 : {
2305 75009 : if (m_chainman.ActiveChain().Tip()->GetBlockHash() != hashRecentRejectsChainTip)
2306 : {
2307 : // If the chain tip has changed previously rejected transactions
2308 : // might be now valid, e.g. due to a nLockTime'd tx becoming valid,
2309 : // or a double-spend. Reset the rejects filter and give those
2310 : // txs a second chance.
2311 2620 : hashRecentRejectsChainTip = m_chainman.ActiveChain().Tip()->GetBlockHash();
2312 2620 : m_recent_rejects.reset();
2313 2620 : }
2314 :
2315 75009 : if (m_orphanage.HaveTx(inv.hash)) return true;
2316 :
2317 : {
2318 74958 : LOCK(m_recent_confirmed_transactions_mutex);
2319 74958 : if (m_recent_confirmed_transactions.contains(inv.hash)) return true;
2320 74958 : }
2321 :
2322 : // When we receive an islock for a previously rejected transaction, we have to
2323 : // drop the first-seen tx (which such a locked transaction was conflicting with)
2324 : // and re-request the locked transaction (which did not make it into the mempool
2325 : // previously due to txn-mempool-conflict rule). This means that we must ignore
2326 : // m_recent_rejects filter for such locked txes here.
2327 : // We also ignore m_recent_rejects filter for DSTX-es because a malicious peer might
2328 : // relay a valid DSTX as a regular TX first which would skip all the specific checks
2329 : // but would cause such tx to be rejected by ATMP due to 0 fee. Ignoring it here
2330 : // should let DSTX to be propagated by honest peer later. Note, that a malicious
2331 : // masternode would not be able to exploit this to spam the network with specially
2332 : // crafted invalid DSTX-es and potentially cause high load cheaply, because
2333 : // corresponding checks in ProcessMessage won't let it to send DSTX-es too often.
2334 151983 : bool fIgnoreRecentRejects = inv.IsMsgDstx() ||
2335 72095 : m_llmq_ctx->isman->IsWaitingForTx(inv.hash) ||
2336 72028 : m_llmq_ctx->isman->IsLocked(inv.hash);
2337 :
2338 157196 : return (!fIgnoreRecentRejects && m_recent_rejects.contains(inv.hash)) ||
2339 77308 : (inv.IsMsgDstx() && static_cast<bool>(m_dstxman.GetDSTX(inv.hash))) ||
2340 140020 : m_mempool.exists(inv.hash) ||
2341 65345 : (g_txindex != nullptr && g_txindex->HasTx(inv.hash));
2342 : }
2343 :
2344 : /*
2345 : Dash Related Inventory Messages
2346 :
2347 : --
2348 :
2349 : We shouldn't update the sync times for each of the messages when we already have it.
2350 : We're going to be asking many nodes upfront for the full inventory list, so we'll get duplicates of these.
2351 : We want to only update the time on new hits, so that we can time out appropriately if needed.
2352 : */
2353 :
2354 : case MSG_SPORK:
2355 : {
2356 5215 : return m_sporkman.GetSporkByHash(inv.hash).has_value();
2357 : }
2358 :
2359 : case MSG_GOVERNANCE_OBJECT:
2360 : case MSG_GOVERNANCE_OBJECT_VOTE:
2361 26346 : for (const auto& handler : m_handlers) {
2362 23380 : if (handler->AlreadyHave(inv)) return true;
2363 : }
2364 2966 : return false;
2365 :
2366 : case MSG_QUORUM_FINAL_COMMITMENT:
2367 15528 : return m_llmq_ctx->quorum_block_processor->HasMineableCommitment(inv.hash);
2368 : case MSG_QUORUM_RECOVERED_SIG:
2369 : // TODO: move it to NetSigning
2370 66254 : return m_llmq_ctx->sigman->AlreadyHave(inv);
2371 : case MSG_CLSIG:
2372 58807 : return m_clhandler.AlreadyHave(inv);
2373 : // TODO: move it to NetInstantSend
2374 : case MSG_ISDLOCK:
2375 1050 : return m_llmq_ctx->isman->AlreadyHave(inv);
2376 : case MSG_PLATFORM_BAN:
2377 19 : return m_mn_metaman.AlreadyHavePlatformBan(inv.hash);
2378 :
2379 : case MSG_QUORUM_CONTRIB:
2380 : case MSG_QUORUM_COMPLAINT:
2381 : case MSG_QUORUM_JUSTIFICATION:
2382 : case MSG_QUORUM_PREMATURE_COMMITMENT:
2383 : case MSG_DSQ:
2384 40714 : if (m_cj_walletman && m_cj_walletman->hasQueue(inv.hash)) return true;
2385 284244 : for (const auto& handler : m_handlers) {
2386 253897 : if (handler->AlreadyHave(inv)) return true;
2387 : }
2388 30347 : return false;
2389 : }
2390 :
2391 : // Don't know what it is, just say we already got one
2392 0 : return true;
2393 270774 : }
2394 :
2395 1692 : bool PeerManagerImpl::AlreadyHaveBlock(const uint256& block_hash)
2396 : {
2397 1692 : return m_chainman.m_blockman.LookupBlockIndex(block_hash) != nullptr;
2398 : }
2399 :
2400 92 : void PeerManagerImpl::SendPings()
2401 : {
2402 92 : READ_LOCK(m_peer_mutex);
2403 466 : for(auto& it : m_peer_map) it.second->m_ping_queued = true;
2404 92 : }
2405 :
2406 31 : void PeerManagerImpl::AskPeersForTransaction(const uint256& txid)
2407 : {
2408 31 : std::vector<PeerRef> peersToAsk;
2409 31 : peersToAsk.reserve(4);
2410 :
2411 : {
2412 31 : READ_LOCK(m_peer_mutex);
2413 : // TODO consider prioritizing MNs again, once that flag is moved into Peer
2414 139 : for (const auto& [_, peer] : m_peer_map) {
2415 75 : if (peersToAsk.size() >= 4) {
2416 0 : break;
2417 : }
2418 75 : if (IsInvInFilter(*peer, txid)) {
2419 33 : peersToAsk.emplace_back(peer);
2420 33 : }
2421 : }
2422 31 : }
2423 : {
2424 31 : CInv inv(MSG_TX, txid);
2425 31 : LOCK(cs_main);
2426 64 : for (PeerRef& peer : peersToAsk) {
2427 33 : LogPrintf("PeerManagerImpl::%s -- txid=%s: asking other peer %d for correct TX\n", __func__,
2428 : txid.ToString(), peer->m_id);
2429 :
2430 33 : RequestObject(peer->m_id, inv, GetTime<std::chrono::microseconds>(), /*fForce=*/true);
2431 : }
2432 31 : }
2433 31 : }
2434 :
2435 75 : bool PeerManagerImpl::IsInvInFilter(const Peer& peer, const uint256& hash) const
2436 : {
2437 75 : if (auto tx_relay = peer.GetTxRelay(); tx_relay != nullptr) {
2438 75 : LOCK(tx_relay->m_tx_inventory_mutex);
2439 75 : return tx_relay->m_tx_inventory_known_filter.contains(hash);
2440 75 : }
2441 0 : return false;
2442 75 : }
2443 :
2444 34377 : void PeerManagerImpl::PushInventory(NodeId nodeid, const CInv& inv)
2445 : {
2446 : // TODO: Get rid of this function at some point
2447 34377 : PeerRef peer = GetPeerRef(nodeid);
2448 34377 : if (peer == nullptr)
2449 0 : return;
2450 34377 : PushInv(*peer, inv);
2451 34377 : }
2452 :
2453 0 : void PeerManagerImpl::RelayInv(const CInv& inv, const int minProtoVersion)
2454 : {
2455 : // TODO: Migrate to iteration through m_peer_map
2456 0 : m_connman.ForEachNode([&](CNode* pnode) {
2457 0 : if (pnode->nVersion < minProtoVersion || !pnode->CanRelay())
2458 0 : return;
2459 :
2460 0 : PeerRef peer = GetPeerRef(pnode->GetId());
2461 0 : if (peer == nullptr) return;
2462 0 : PushInv(*peer, inv);
2463 0 : });
2464 0 : }
2465 :
2466 21786 : void PeerManagerImpl::RelayInv(const CInv& inv)
2467 : {
2468 21786 : READ_LOCK(m_peer_mutex);
2469 136777 : for (const auto& [_, peer] : m_peer_map) {
2470 114992 : if (!peer->GetInvRelay()) continue;
2471 114991 : PushInv(*peer, inv);
2472 : }
2473 21788 : }
2474 :
2475 0 : void PeerManagerImpl::RelayDSQ(const CCoinJoinQueue& queue)
2476 : {
2477 0 : CInv inv{MSG_DSQ, queue.GetHash()};
2478 0 : std::vector<NodeId> nodes_send_all;
2479 : {
2480 0 : READ_LOCK(m_peer_mutex);
2481 0 : for (const auto& [nodeid, peer] : m_peer_map) {
2482 0 : switch (peer->m_wants_dsq) {
2483 : case Peer::WantsDSQ::NONE:
2484 0 : break;
2485 : case Peer::WantsDSQ::INV:
2486 0 : PushInv(*peer, inv);
2487 0 : break;
2488 : case Peer::WantsDSQ::ALL:
2489 0 : nodes_send_all.push_back(nodeid);
2490 0 : break;
2491 : }
2492 : }
2493 0 : }
2494 0 : for (auto nodeId : nodes_send_all) {
2495 0 : m_connman.ForNode(nodeId, [&](CNode* pnode) -> bool {
2496 0 : CNetMsgMaker msgMaker(pnode->GetCommonVersion());
2497 0 : m_connman.PushMessage(pnode, msgMaker.Make(NetMsgType::DSQUEUE, queue));
2498 0 : return true;
2499 0 : });
2500 : }
2501 0 : }
2502 :
2503 820 : void PeerManagerImpl::RelayInvFiltered(const CInv& inv, const CTransaction& relatedTx)
2504 : {
2505 : // TODO: Migrate to iteration through m_peer_map
2506 3824 : m_connman.ForEachNode([&](CNode* pnode) {
2507 3004 : if (!pnode->CanRelay()) return;
2508 :
2509 2927 : PeerRef peer = GetPeerRef(pnode->GetId());
2510 2927 : if (peer == nullptr) return;
2511 :
2512 2927 : auto tx_relay = peer->GetTxRelay();
2513 2927 : if (tx_relay == nullptr) {
2514 0 : return;
2515 : }
2516 :
2517 : {
2518 2927 : LOCK(tx_relay->m_bloom_filter_mutex);
2519 2927 : if (!tx_relay->m_relay_txs) {
2520 0 : return;
2521 : }
2522 2927 : if (tx_relay->m_bloom_filter && !tx_relay->m_bloom_filter->IsRelevantAndUpdate(relatedTx)) {
2523 0 : return;
2524 : }
2525 2927 : } // LOCK(tx_relay->m_bloom_filter_mutex)
2526 2927 : if (inv.type == MSG_ISDLOCK && PeerReconstructsISLockFromRecsig(*pnode, *peer)) {
2527 1544 : LogPrint(BCLog::NET, "%s -- skipping ISDLOCK inv (peer wants recsigs): %s peer=%d\n",
2528 : __func__, inv.ToString(), peer->m_id);
2529 1544 : return;
2530 : }
2531 1383 : PushInv(*peer, inv);
2532 3004 : });
2533 820 : }
2534 :
2535 31 : void PeerManagerImpl::RelayInvFiltered(const CInv& inv, const uint256& relatedTxHash)
2536 : {
2537 : // TODO: Migrate to iteration through m_peer_map
2538 106 : m_connman.ForEachNode([&](CNode* pnode) {
2539 75 : PeerRef peer = GetPeerRef(pnode->GetId());
2540 75 : if (peer == nullptr) return;
2541 :
2542 75 : auto tx_relay = peer->GetTxRelay();
2543 75 : if (!pnode->CanRelay() || tx_relay == nullptr) {
2544 1 : return;
2545 : }
2546 :
2547 : {
2548 74 : LOCK(tx_relay->m_bloom_filter_mutex);
2549 74 : if (!tx_relay->m_relay_txs) {
2550 0 : return;
2551 : }
2552 74 : if (tx_relay->m_bloom_filter && !tx_relay->m_bloom_filter->contains(relatedTxHash)) {
2553 0 : return;
2554 : }
2555 74 : } // LOCK(tx_relay->m_bloom_filter_mutex)
2556 74 : if (inv.type == MSG_ISDLOCK && PeerReconstructsISLockFromRecsig(*pnode, *peer)) {
2557 34 : LogPrint(BCLog::NET, "%s -- skipping ISDLOCK inv (peer wants recsigs): %s peer=%d\n",
2558 : __func__, inv.ToString(), peer->m_id);
2559 34 : return;
2560 : }
2561 40 : PushInv(*peer, inv);
2562 75 : });
2563 31 : }
2564 :
2565 16462 : void PeerManagerImpl::RelayTransaction(const uint256& txid)
2566 : {
2567 32924 : WITH_LOCK(cs_main, _RelayTransaction(txid));
2568 16462 : }
2569 :
2570 35570 : void PeerManagerImpl::_RelayTransaction(const uint256& txid)
2571 : {
2572 35570 : const CInv inv{m_dstxman.GetDSTX(txid) ? MSG_DSTX : MSG_TX, txid};
2573 35570 : READ_LOCK(m_peer_mutex);
2574 105276 : for(auto& it : m_peer_map) {
2575 69706 : Peer& peer = *it.second;
2576 69706 : auto tx_relay = peer.GetTxRelay();
2577 69706 : if (!tx_relay) continue;
2578 :
2579 69703 : PushInv(peer, inv);
2580 : };
2581 35570 : }
2582 :
2583 27204 : void PeerManagerImpl::RelayRecoveredSig(const llmq::CRecoveredSig& sig, bool proactive_relay)
2584 : {
2585 27204 : if (proactive_relay) {
2586 : // We were the peer that recovered this; avoid a bunch of `inv` -> `GetData` spam by proactively sending
2587 52021 : m_connman.ForEachNode([this, &sig](CNode* pnode) -> bool {
2588 : // Skip nodes that don't want recovered signatures
2589 43118 : PeerRef peer = GetPeerRef(pnode->GetId());
2590 43118 : if (peer == nullptr || !peer->m_wants_recsigs) return true;
2591 30892 : CNetMsgMaker msgMaker(pnode->GetCommonVersion());
2592 30892 : m_connman.PushMessage(pnode, msgMaker.Make(NetMsgType::QSIGREC, sig));
2593 30892 : return true;
2594 43118 : });
2595 8903 : return;
2596 : }
2597 18301 : const CInv inv{MSG_QUORUM_RECOVERED_SIG, sig.GetHash()};
2598 18301 : READ_LOCK(m_peer_mutex);
2599 133307 : for (const auto& [_, peer] : m_peer_map) {
2600 115006 : if (peer->m_wants_recsigs) {
2601 87875 : PushInv(*peer, inv);
2602 87875 : }
2603 : }
2604 27204 : }
2605 :
2606 100 : void PeerManagerImpl::RelayAddress(NodeId originator,
2607 : const CAddress& addr,
2608 : bool fReachable)
2609 : {
2610 : // We choose the same nodes within a given 24h window (if the list of connected
2611 : // nodes does not change) and we don't relay to nodes that already know an
2612 : // address. So within 24h we will likely relay a given address once. This is to
2613 : // prevent a peer from unjustly giving their address better propagation by sending
2614 : // it to us repeatedly.
2615 :
2616 100 : if (!fReachable && !addr.IsRelayable()) return;
2617 :
2618 : // Relay to a limited number of other nodes
2619 : // Use deterministic randomness to send to the same nodes for 24 hours
2620 : // at a time so the m_addr_knowns of the chosen nodes prevent repeats
2621 100 : const uint64_t hash_addr{CServiceHash(0, 0)(addr)};
2622 100 : const auto current_time{GetTime<std::chrono::seconds>()};
2623 : // Adding address hash makes exact rotation time different per address, while preserving periodicity.
2624 100 : const uint64_t time_addr{(static_cast<uint64_t>(count_seconds(current_time)) + hash_addr) / count_seconds(ROTATE_ADDR_RELAY_DEST_INTERVAL)};
2625 200 : const CSipHasher hasher{m_connman.GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY)
2626 100 : .Write(hash_addr)
2627 100 : .Write(time_addr)};
2628 100 : FastRandomContext insecure_rand;
2629 :
2630 : // Relay reachable addresses to 2 peers. Unreachable addresses are relayed randomly to 1 or 2 peers.
2631 100 : unsigned int nRelayNodes = (fReachable || (hasher.Finalize() & 1)) ? 2 : 1;
2632 :
2633 100 : std::array<std::pair<uint64_t, Peer*>, 2> best{{{0, nullptr}, {0, nullptr}}};
2634 100 : assert(nRelayNodes <= best.size());
2635 :
2636 100 : READ_LOCK(m_peer_mutex);
2637 :
2638 2639 : for (auto& [id, peer] : m_peer_map) {
2639 1132 : if (peer->m_addr_relay_enabled && id != originator && IsAddrCompatible(*peer, addr)) {
2640 2048 : uint64_t hashKey = CSipHasher(hasher).Write(id).Finalize();
2641 2411 : for (unsigned int i = 0; i < nRelayNodes; i++) {
2642 1770 : if (hashKey > best[i].first) {
2643 383 : std::copy(best.begin() + i, best.begin() + nRelayNodes - 1, best.begin() + i + 1);
2644 383 : best[i] = std::make_pair(hashKey, peer.get());
2645 383 : break;
2646 : }
2647 1387 : }
2648 1024 : }
2649 : };
2650 :
2651 264 : for (unsigned int i = 0; i < nRelayNodes && best[i].first != 0; i++) {
2652 164 : PushAddress(*best[i].second, addr, insecure_rand);
2653 164 : }
2654 100 : }
2655 :
2656 57373 : void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv& inv, llmq::CInstantSendManager& isman)
2657 : {
2658 57373 : std::shared_ptr<const CBlock> a_recent_block;
2659 57373 : std::shared_ptr<const CBlockHeaderAndShortTxIDs> a_recent_compact_block;
2660 : {
2661 57373 : LOCK(m_most_recent_block_mutex);
2662 54733 : a_recent_block = m_most_recent_block;
2663 54733 : a_recent_compact_block = m_most_recent_compact_block;
2664 54733 : }
2665 :
2666 54733 : bool need_activate_chain = false;
2667 : {
2668 54733 : LOCK(cs_main);
2669 54733 : const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(inv.hash);
2670 54733 : if (pindex) {
2671 55032 : if (pindex->HaveTxsDownloaded() && !pindex->IsValid(BLOCK_VALID_SCRIPTS) &&
2672 299 : pindex->IsValid(BLOCK_VALID_TREE)) {
2673 : // If we have the block and all of its parents, but have not yet validated it,
2674 : // we might be in the middle of connecting it (ie in the unlock of cs_main
2675 : // before ActivateBestChain but after AcceptBlock).
2676 : // In this case, we need to run ActivateBestChain prior to checking the relay
2677 : // conditions below.
2678 299 : need_activate_chain = true;
2679 299 : }
2680 54733 : }
2681 54733 : } // release cs_main before calling ActivateBestChain
2682 54733 : if (need_activate_chain) {
2683 299 : BlockValidationState state;
2684 299 : if (!m_chainman.ActiveChainstate().ActivateBestChain(state, a_recent_block)) {
2685 0 : LogPrint(BCLog::NET, "failed to activate chain (%s)\n", state.ToString());
2686 0 : }
2687 299 : }
2688 :
2689 54733 : LOCK(cs_main);
2690 57373 : const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(inv.hash);
2691 54733 : if (!pindex) {
2692 0 : return;
2693 : }
2694 54733 : if (!BlockRequestAllowed(pindex)) {
2695 2 : LogPrint(BCLog::NET, "%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom.GetId());
2696 2 : return;
2697 : }
2698 54731 : const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
2699 : // disconnect node in case we have reached the outbound limit for serving historical blocks
2700 54740 : if (m_connman.OutboundTargetReached(true) &&
2701 669 : (((m_chainman.m_best_header != nullptr) && (m_chainman.m_best_header->GetBlockTime() - pindex->GetBlockTime() > HISTORICAL_BLOCK_AGE)) || inv.IsMsgFilteredBlk()) &&
2702 1329 : !pfrom.HasPermission(NetPermissionFlags::Download) // nodes with the download permission may exceed target
2703 : ) {
2704 6 : LogPrint(BCLog::NET, "historical block serving limit reached, disconnect peer=%d\n", pfrom.GetId());
2705 6 : pfrom.fDisconnect = true;
2706 6 : return;
2707 : }
2708 : // Avoid leaking prune-height by never sending blocks below the NODE_NETWORK_LIMITED threshold
2709 54117 : if (!pfrom.HasPermission(NetPermissionFlags::NoBan) && (
2710 50935 : (((peer.m_our_services & NODE_NETWORK_LIMITED) == NODE_NETWORK_LIMITED) && ((peer.m_our_services & NODE_NETWORK) != NODE_NETWORK) && (m_chainman.ActiveChain().Tip()->nHeight - pindex->nHeight > (int)NODE_NETWORK_LIMITED_MIN_BLOCKS + 2 /* add two blocks buffer extension for possible races */) )
2711 : )) {
2712 8 : LogPrint(BCLog::NET, "Ignore block request below NODE_NETWORK_LIMITED threshold, disconnect peer=%d\n", pfrom.GetId());
2713 : //disconnect node and prevent it from stalling (would otherwise wait for the missing block)
2714 8 : pfrom.fDisconnect = true;
2715 8 : return;
2716 : }
2717 : // Pruned nodes may have deleted the block, so check whether
2718 : // it's available before trying to send.
2719 54717 : if (!(pindex->nStatus & BLOCK_HAVE_DATA)) {
2720 0 : return;
2721 : }
2722 54717 : std::shared_ptr<const CBlock> pblock;
2723 108935 : if (a_recent_block && a_recent_block->GetHash() == pindex->GetBlockHash()) {
2724 6585 : pblock = a_recent_block;
2725 6585 : } else {
2726 : // Send block from disk
2727 48132 : std::shared_ptr<CBlock> pblockRead = std::make_shared<CBlock>();
2728 48132 : if (!ReadBlockFromDisk(*pblockRead, pindex, m_chainparams.GetConsensus()))
2729 0 : assert(!"cannot load block from disk");
2730 48132 : pblock = pblockRead;
2731 48132 : }
2732 54717 : if (pblock) {
2733 54717 : if (inv.IsMsgBlk()) {
2734 52689 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::BLOCK, *pblock));
2735 54717 : } else if (inv.IsMsgFilteredBlk()) {
2736 14 : bool sendMerkleBlock = false;
2737 14 : CMerkleBlock merkleBlock;
2738 14 : if (auto tx_relay = peer.GetTxRelay(); tx_relay != nullptr) {
2739 14 : LOCK(tx_relay->m_bloom_filter_mutex);
2740 14 : if (tx_relay->m_bloom_filter) {
2741 8 : sendMerkleBlock = true;
2742 8 : merkleBlock = CMerkleBlock(*pblock, *tx_relay->m_bloom_filter);
2743 8 : }
2744 14 : }
2745 14 : if (sendMerkleBlock) {
2746 8 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::MERKLEBLOCK, merkleBlock));
2747 : // CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
2748 : // This avoids hurting performance by pointlessly requiring a round-trip
2749 : // Note that there is currently no way for a node to request any single transactions we didn't send here -
2750 : // they must either disconnect and retry or request the full block.
2751 : // Thus, the protocol spec specified allows for us to provide duplicate txn here,
2752 : // however we MUST always provide at least what the remote peer needs
2753 : typedef std::pair<unsigned int, uint256> PairType;
2754 12 : for (PairType &pair : merkleBlock.vMatchedTxn) {
2755 4 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::TX, *pblock->vtx[pair.first]));
2756 : }
2757 12 : for (PairType &pair : merkleBlock.vMatchedTxn) {
2758 4 : auto islock = isman.GetInstantSendLockByTxid(pair.second);
2759 4 : if (islock != nullptr) {
2760 0 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::ISDLOCK, *islock));
2761 0 : }
2762 4 : }
2763 8 : }
2764 : // else
2765 : // no response
2766 2028 : } else if (inv.IsMsgCmpctBlk()) {
2767 : // If a peer is asking for old blocks, we're almost guaranteed
2768 : // they won't have a useful mempool to match against a compact block,
2769 : // and we don't feel like constructing the object for them, so
2770 : // instead we respond with the full, non-compact block.
2771 4028 : if (CanDirectFetch() &&
2772 2014 : pindex->nHeight >= m_chainman.ActiveChain().Height() - MAX_CMPCTBLOCK_DEPTH) {
2773 3569 : if (a_recent_compact_block &&
2774 1775 : a_recent_compact_block->header.GetHash() == pindex->GetBlockHash()) {
2775 713 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::CMPCTBLOCK, *a_recent_compact_block));
2776 713 : } else {
2777 1081 : CBlockHeaderAndShortTxIDs cmpctblock{*pblock};
2778 1081 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::CMPCTBLOCK, cmpctblock));
2779 1081 : }
2780 1794 : } else {
2781 220 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::BLOCK, *pblock));
2782 : }
2783 2014 : }
2784 54717 : }
2785 :
2786 : {
2787 54717 : LOCK(peer.m_block_inv_mutex);
2788 : // Trigger the peer node to send a getblocks request for the next batch of inventory
2789 54717 : if (inv.hash == peer.m_continuation_block) {
2790 : // Send immediately. This must send even if redundant,
2791 : // and we want it right after the last block so they don't
2792 : // wait for other stuff first.
2793 0 : std::vector<CInv> vInv;
2794 0 : vInv.emplace_back(MSG_BLOCK, m_chainman.ActiveChain().Tip()->GetBlockHash());
2795 0 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::INV, vInv));
2796 0 : peer.m_continuation_block.SetNull();
2797 0 : }
2798 54717 : }
2799 65293 : }
2800 :
2801 : //! Determine whether or not a peer can request a transaction, and return it (or nullptr if not found or not allowed).
2802 20107 : CTransactionRef PeerManagerImpl::FindTxForGetData(const CNode* peer, const uint256& txid, const std::chrono::seconds mempool_req, const std::chrono::seconds now)
2803 : {
2804 20107 : auto txinfo = m_mempool.info(txid);
2805 20107 : if (txinfo.tx) {
2806 : // If a TX could have been INVed in reply to a MEMPOOL request,
2807 : // or is older than UNCONDITIONAL_RELAY_DELAY, permit the request
2808 : // unconditionally.
2809 19659 : if ((mempool_req.count() && txinfo.m_time <= mempool_req) || txinfo.m_time <= now - UNCONDITIONAL_RELAY_DELAY) {
2810 333 : return std::move(txinfo.tx);
2811 : }
2812 19326 : }
2813 :
2814 : {
2815 19774 : LOCK(cs_main);
2816 :
2817 : // Otherwise, the transaction must have been announced recently.
2818 19774 : if (State(peer->GetId())->m_recently_announced_invs.contains(txid)) {
2819 : // If it was, it can be relayed from either the mempool...
2820 19770 : if (txinfo.tx) return std::move(txinfo.tx);
2821 : // ... or the relay pool.
2822 448 : auto mi = mapRelay.find(txid);
2823 448 : if (mi != mapRelay.end()) return mi->second;
2824 0 : }
2825 19774 : }
2826 :
2827 4 : return {};
2828 20107 : }
2829 :
2830 95372 : void PeerManagerImpl::ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic<bool>& interruptMsgProc)
2831 : {
2832 95372 : AssertLockNotHeld(cs_main);
2833 :
2834 95372 : auto tx_relay = peer.GetTxRelay();
2835 :
2836 95372 : std::deque<CInv>::iterator it = peer.m_getdata_requests.begin();
2837 95372 : std::vector<CInv> vNotFound;
2838 95372 : const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
2839 :
2840 95372 : const auto now{GetTime<std::chrono::seconds>()};
2841 : // Get last mempool request time
2842 95372 : const auto mempool_req = tx_relay != nullptr ? tx_relay->m_last_mempool_req.load()
2843 0 : : std::chrono::seconds::min();
2844 :
2845 : // Process as many TX items from the front of the getdata queue as
2846 : // possible, since they're common and it's efficient to batch process
2847 : // them.
2848 254655 : while (it != peer.m_getdata_requests.end() && it->IsKnownType()) {
2849 107007 : if (interruptMsgProc)
2850 0 : return;
2851 : // The send buffer provides backpressure. If there's no space in
2852 : // the buffer, pause processing until the next call.
2853 107007 : if (pfrom.fPauseSend)
2854 0 : break;
2855 :
2856 107007 : const CInv &inv = *it;
2857 :
2858 107007 : if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK || inv.type == MSG_CMPCT_BLOCK) {
2859 54733 : break;
2860 : }
2861 52274 : ++it;
2862 :
2863 52274 : if (tx_relay == nullptr && NetMessageViolatesBlocksOnly(inv.GetCommand())) {
2864 : // Note that if we receive a getdata for non-block messages
2865 : // from a block-relay-only outbound peer that violate the policy,
2866 : // we skip such getdata messages from this peer
2867 0 : continue;
2868 : }
2869 :
2870 52274 : bool push = false;
2871 52274 : if (inv.IsGenTxMsg()) {
2872 20107 : CTransactionRef tx = FindTxForGetData(&pfrom, inv.hash, mempool_req, now);
2873 20107 : if (tx) {
2874 20103 : CCoinJoinBroadcastTx dstx;
2875 20103 : if (inv.IsMsgDstx()) {
2876 0 : dstx = m_dstxman.GetDSTX(inv.hash);
2877 0 : }
2878 20103 : if (dstx) {
2879 0 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::DSTX, dstx));
2880 0 : } else {
2881 20103 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::TX, *tx));
2882 : }
2883 20103 : m_mempool.RemoveUnbroadcastTx(tx->GetHash());
2884 20103 : push = true;
2885 :
2886 : // As we're going to send tx, make sure its unconfirmed parents are made requestable.
2887 20103 : std::vector<uint256> parent_ids_to_add;
2888 : {
2889 20103 : LOCK(m_mempool.cs);
2890 20103 : auto tx_iter = m_mempool.GetIter(tx->GetHash());
2891 20103 : if (tx_iter) {
2892 19641 : const CTxMemPoolEntry::Parents& parents = (*tx_iter)->GetMemPoolParentsConst();
2893 19641 : parent_ids_to_add.reserve(parents.size());
2894 20719 : for (const CTxMemPoolEntry& parent : parents) {
2895 1078 : if (parent.GetTime() > now - UNCONDITIONAL_RELAY_DELAY) {
2896 1053 : parent_ids_to_add.push_back(parent.GetTx().GetHash());
2897 1053 : }
2898 : }
2899 19641 : }
2900 20103 : }
2901 :
2902 21156 : for (const uint256& parent_txid : parent_ids_to_add) {
2903 : // Relaying a transaction with a recent but unconfirmed parent.
2904 2106 : if (WITH_LOCK(tx_relay->m_tx_inventory_mutex, return !tx_relay->m_tx_inventory_known_filter.contains(parent_txid))) {
2905 0 : LOCK(cs_main);
2906 0 : State(pfrom.GetId())->m_recently_announced_invs.insert(parent_txid);
2907 0 : }
2908 : }
2909 20103 : }
2910 20107 : }
2911 :
2912 52274 : if (!push && inv.type == MSG_SPORK) {
2913 3550 : if (auto opt_spork = m_sporkman.GetSporkByHash(inv.hash)) {
2914 1775 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::SPORK, *opt_spork));
2915 1775 : push = true;
2916 1775 : }
2917 1775 : }
2918 :
2919 52274 : if (!push && (inv.type == MSG_QUORUM_FINAL_COMMITMENT)) {
2920 2715 : llmq::CFinalCommitment o;
2921 5430 : if (m_llmq_ctx->quorum_block_processor->GetMineableCommitmentByHash(
2922 2715 : inv.hash, o)) {
2923 2713 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::QFCOMMITMENT, o));
2924 2713 : push = true;
2925 2713 : }
2926 2715 : }
2927 :
2928 52274 : if (!push && (inv.type == MSG_QUORUM_RECOVERED_SIG)) {
2929 2691 : llmq::CRecoveredSig o;
2930 2691 : if (m_llmq_ctx->sigman->GetRecoveredSigForGetData(inv.hash, o)) {
2931 2691 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::QSIGREC, o));
2932 2691 : push = true;
2933 2691 : }
2934 2691 : }
2935 :
2936 52274 : if (!push && (inv.type == MSG_CLSIG)) {
2937 9570 : chainlock::ChainLockSig o;
2938 9570 : if (m_chainlocks.GetChainLockByHash(inv.hash, o)) {
2939 9436 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::CLSIG, o));
2940 9436 : push = true;
2941 9436 : }
2942 9570 : }
2943 :
2944 52274 : if (!push && inv.type == MSG_ISDLOCK) {
2945 333 : instantsend::InstantSendLock o;
2946 333 : if (m_llmq_ctx->isman->GetInstantSendLockByHash(inv.hash, o)) {
2947 333 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::ISDLOCK, o));
2948 333 : push = true;
2949 333 : }
2950 333 : }
2951 52274 : if (!push && inv.type == MSG_DSQ) {
2952 0 : auto opt_dsq = m_cj_walletman ? m_cj_walletman->getQueueFromHash(inv.hash) : std::nullopt;
2953 0 : if (opt_dsq.has_value()) {
2954 0 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::DSQUEUE, *opt_dsq));
2955 0 : push = true;
2956 0 : }
2957 0 : }
2958 52274 : if (!push && inv.type == MSG_PLATFORM_BAN) {
2959 10 : auto opt_platform_ban = m_mn_metaman.GetPlatformBan(inv.hash);
2960 10 : if (opt_platform_ban.has_value()) {
2961 10 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::PLATFORMBAN, *opt_platform_ban));
2962 10 : push = true;
2963 10 : }
2964 10 : }
2965 387927 : for (auto& handler : m_handlers) {
2966 335653 : if (!push) {
2967 64342 : push = handler->ProcessGetData(pfrom, inv, m_connman, msgMaker);
2968 64342 : }
2969 : }
2970 :
2971 52274 : if (!push) {
2972 602 : vNotFound.push_back(inv);
2973 602 : }
2974 : }
2975 :
2976 : // Only process one BLOCK item per call, since they're uncommon and can be
2977 : // expensive to process.
2978 95372 : if (it != peer.m_getdata_requests.end() && !pfrom.fPauseSend) {
2979 54735 : const CInv &inv = *it++;
2980 54735 : if (inv.IsGenBlkMsg()) {
2981 54733 : ProcessGetBlockData(pfrom, peer, inv, *m_llmq_ctx->isman);
2982 54733 : }
2983 : // else: If the first item on the queue is an unknown type, we erase it
2984 : // and continue processing the queue on the next call.
2985 54735 : }
2986 :
2987 95372 : peer.m_getdata_requests.erase(peer.m_getdata_requests.begin(), it);
2988 :
2989 95372 : if (!vNotFound.empty()) {
2990 : // Let the peer know that we didn't find what it asked for, so it doesn't
2991 : // have to wait around forever.
2992 : // SPV clients care about this message: it's needed when they are
2993 : // recursively walking the dependencies of relevant unconfirmed
2994 : // transactions. SPV clients want to do that because they want to know
2995 : // about (and store and rebroadcast and risk analyze) the dependencies
2996 : // of transactions relevant to them, without having to download the
2997 : // entire memory pool.
2998 : // Also, other nodes can use these messages to automatically request a
2999 : // transaction from some other peer that announced it, and stop
3000 : // waiting for us to respond.
3001 : // In normal operation, we often send NOTFOUND messages for parents of
3002 : // transactions that we relay; if a peer is missing a parent, they may
3003 : // assume we have them and request the parents from us.
3004 512 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::NOTFOUND, vNotFound));
3005 512 : }
3006 95372 : }
3007 :
3008 29126 : void PeerManagerImpl::SendBlockTransactions(CNode& pfrom, const CBlock& block, const BlockTransactionsRequest& req) {
3009 29126 : BlockTransactions resp(req);
3010 112352 : for (size_t i = 0; i < req.indexes.size(); i++) {
3011 83228 : if (req.indexes[i] >= block.vtx.size()) {
3012 2 : Misbehaving(pfrom.GetId(), 100, "getblocktxn with out-of-bounds tx indices");
3013 2 : return;
3014 : }
3015 83226 : resp.txn[i] = block.vtx[req.indexes[i]];
3016 83226 : }
3017 29124 : CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
3018 29124 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::BLOCKTXN, resp));
3019 29126 : }
3020 :
3021 : /**
3022 : * Special handling for unconnecting headers that might be part of a block
3023 : * announcement.
3024 : *
3025 : * We'll send a getheaders message in response to try to connect the chain.
3026 : *
3027 : * The peer can send up to MAX_UNCONNECTING_HEADERS in a row that
3028 : * don't connect before given DoS points.
3029 : *
3030 : * Once a headers message is received that is valid and does connect,
3031 : * nUnconnectingHeaders gets reset back to 0.
3032 : */
3033 281 : void PeerManagerImpl::HandleFewUnconnectingHeaders(CNode& pfrom, Peer& peer,
3034 : const std::vector<CBlockHeader>& headers)
3035 : {
3036 281 : LOCK(cs_main);
3037 281 : CNodeState *nodestate = State(pfrom.GetId());
3038 :
3039 281 : nodestate->nUnconnectingHeaders++;
3040 : // Try to fill in the missing headers.
3041 281 : std::string msg_type = UsesCompressedHeaders(peer) ? NetMsgType::GETHEADERS2 : NetMsgType::GETHEADERS;
3042 281 : if (MaybeSendGetHeaders(pfrom, msg_type, m_chainman.ActiveChain().GetLocator(m_chainman.m_best_header), peer)) {
3043 281 : LogPrint(BCLog::NET, "received header %s: missing prev block %s, sending %s (%d) to end (peer=%d, nUnconnectingHeaders=%d)\n",
3044 : headers[0].GetHash().ToString(),
3045 : headers[0].hashPrevBlock.ToString(),
3046 : msg_type,
3047 : m_chainman.m_best_header->nHeight,
3048 : pfrom.GetId(), nodestate->nUnconnectingHeaders);
3049 281 : }
3050 : // Set hashLastUnknownBlock for this peer, so that if we
3051 : // eventually get the headers - even from a different peer -
3052 : // we can use this peer to download.
3053 281 : UpdateBlockAvailability(pfrom.GetId(), headers.back().GetHash());
3054 :
3055 : // The peer may just be broken, so periodically assign DoS points if this
3056 : // condition persists.
3057 281 : if (nodestate->nUnconnectingHeaders % MAX_UNCONNECTING_HEADERS == 0) {
3058 20 : Misbehaving(pfrom.GetId(), 20, strprintf("%d non-connecting headers", nodestate->nUnconnectingHeaders));
3059 20 : }
3060 281 : }
3061 :
3062 102800 : bool PeerManagerImpl::CheckHeadersAreContinuous(const std::vector<CBlockHeader>& headers) const
3063 : {
3064 102800 : uint256 hashLastBlock;
3065 358178 : for (const CBlockHeader& header : headers) {
3066 255380 : if (!hashLastBlock.IsNull() && header.hashPrevBlock != hashLastBlock) {
3067 2 : return false;
3068 : }
3069 255378 : hashLastBlock = header.GetHash();
3070 : }
3071 102798 : return true;
3072 102800 : }
3073 :
3074 8671 : bool PeerManagerImpl::MaybeSendGetHeaders(CNode& pfrom, const std::string& msg_type, const CBlockLocator& locator, Peer& peer)
3075 : {
3076 8671 : assert(msg_type == NetMsgType::GETHEADERS || msg_type == NetMsgType::GETHEADERS2);
3077 :
3078 8671 : const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
3079 :
3080 8671 : const auto current_time = NodeClock::now();
3081 :
3082 : // Only allow a new getheaders message to go out if we don't have a recent
3083 : // one already in-flight
3084 8671 : if (current_time - peer.m_last_getheaders_timestamp > HEADERS_RESPONSE_TIME) {
3085 8583 : m_connman.PushMessage(&pfrom, msgMaker.Make(msg_type, locator, uint256()));
3086 8583 : peer.m_last_getheaders_timestamp = current_time;
3087 8583 : return true;
3088 : }
3089 88 : return false;
3090 8671 : }
3091 :
3092 : /*
3093 : * Given a new headers tip ending in last_header, potentially request blocks towards that tip.
3094 : * We require that the given tip have at least as much work as our tip, and for
3095 : * our current tip to be "close to synced" (see CanDirectFetch()).
3096 : */
3097 102556 : void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex& last_header)
3098 : {
3099 102556 : const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
3100 :
3101 102556 : LOCK(cs_main);
3102 102556 : CNodeState *nodestate = State(pfrom.GetId());
3103 :
3104 102556 : if (CanDirectFetch() && last_header.IsValid(BLOCK_VALID_TREE) && m_chainman.ActiveChain().Tip()->nChainWork <= last_header.nChainWork) {
3105 78999 : std::vector<const CBlockIndex*> vToFetch;
3106 78999 : const CBlockIndex* pindexWalk{&last_header};
3107 : // Calculate all the blocks we'd need to switch to last_header, up to a limit.
3108 163341 : while (pindexWalk && !m_chainman.ActiveChain().Contains(pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
3109 167269 : if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
3110 82927 : !IsBlockRequested(pindexWalk->GetBlockHash())) {
3111 : // We don't have this block, and it's not yet in flight.
3112 23917 : vToFetch.push_back(pindexWalk);
3113 23917 : }
3114 84342 : pindexWalk = pindexWalk->pprev;
3115 : }
3116 : // If pindexWalk still isn't on our main chain, we're looking at a
3117 : // very large reorg at a time we think we're close to caught up to
3118 : // the main chain -- this shouldn't really happen. Bail out on the
3119 : // direct fetch and rely on parallel download instead.
3120 78999 : if (!m_chainman.ActiveChain().Contains(pindexWalk)) {
3121 63 : LogPrint(BCLog::NET, "Large reorg, won't direct fetch to %s (%d)\n",
3122 : last_header.GetBlockHash().ToString(),
3123 : last_header.nHeight);
3124 63 : } else {
3125 78936 : std::vector<CInv> vGetData;
3126 : // Download as much as possible, from earliest to latest.
3127 101256 : for (const CBlockIndex *pindex : vToFetch | std::views::reverse) {
3128 22409 : if (nodestate->nBlocksInFlight >= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
3129 : // Can't download any more from this peer
3130 89 : break;
3131 : }
3132 22320 : vGetData.emplace_back(MSG_BLOCK, pindex->GetBlockHash());
3133 22320 : BlockRequested(pfrom.GetId(), *pindex);
3134 22320 : LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n",
3135 : pindex->GetBlockHash().ToString(), pfrom.GetId());
3136 : }
3137 78936 : if (vGetData.size() > 1) {
3138 1064 : LogPrint(BCLog::NET, "Downloading blocks toward %s (%d) via headers direct fetch\n",
3139 : last_header.GetBlockHash().ToString(),
3140 : last_header.nHeight);
3141 1064 : }
3142 78936 : if (vGetData.size() > 0) {
3143 22519 : if (!m_ignore_incoming_txs &&
3144 20238 : nodestate->m_provides_cmpctblocks &&
3145 19678 : vGetData.size() == 1 &&
3146 18805 : mapBlocksInFlight.size() == 1 &&
3147 2279 : last_header.pprev->IsValid(BLOCK_VALID_CHAIN)) {
3148 : // In any case, we want to download using a compact block, not a regular one
3149 2018 : vGetData[0] = CInv(MSG_CMPCT_BLOCK, vGetData[0].hash);
3150 2018 : }
3151 20240 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, vGetData));
3152 20240 : }
3153 78936 : }
3154 78999 : }
3155 102556 : }
3156 :
3157 : /**
3158 : * Given receipt of headers from a peer ending in last_header, along with
3159 : * whether that header was new and whether the headers message was full,
3160 : * update the state we keep for the peer.
3161 : */
3162 102556 : void PeerManagerImpl::UpdatePeerStateForReceivedHeaders(CNode& pfrom,
3163 : const CBlockIndex& last_header, bool received_new_header, bool may_have_more_headers)
3164 : {
3165 102556 : LOCK(cs_main);
3166 102556 : CNodeState *nodestate = State(pfrom.GetId());
3167 102556 : if (nodestate->nUnconnectingHeaders > 0) {
3168 44 : LogPrint(BCLog::NET, "peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n", pfrom.GetId(), nodestate->nUnconnectingHeaders);
3169 44 : }
3170 102556 : nodestate->nUnconnectingHeaders = 0;
3171 :
3172 102556 : UpdateBlockAvailability(pfrom.GetId(), last_header.GetBlockHash());
3173 :
3174 : // From here, pindexBestKnownBlock should be guaranteed to be non-null,
3175 : // because it is set in UpdateBlockAvailability. Some nullptr checks
3176 : // are still present, however, as belt-and-suspenders.
3177 :
3178 102556 : if (received_new_header && last_header.nChainWork > m_chainman.ActiveChain().Tip()->nChainWork) {
3179 5104 : nodestate->m_last_block_announcement = GetTime();
3180 5104 : }
3181 :
3182 : // If we're in IBD, we want outbound peers that will serve us a useful
3183 : // chain. Disconnect peers that are on chains with insufficient work.
3184 102556 : if (m_chainman.ActiveChainstate().IsInitialBlockDownload() && !may_have_more_headers) {
3185 : // If the peer has no more headers to give us, then we know we have
3186 : // their tip.
3187 224 : if (nodestate->pindexBestKnownBlock && nodestate->pindexBestKnownBlock->nChainWork < nMinimumChainWork) {
3188 : // This peer has too little work on their headers chain to help
3189 : // us sync -- disconnect if it is an outbound disconnection
3190 : // candidate.
3191 : // Note: We compare their tip to nMinimumChainWork (rather than
3192 : // m_chainman.ActiveChain().Tip()) because we won't start block download
3193 : // until we have a headers chain that has at least
3194 : // nMinimumChainWork, even if a peer has a chain past our tip,
3195 : // as an anti-DoS measure.
3196 80 : if (pfrom.IsOutboundOrBlockRelayConn()) {
3197 0 : LogPrintf("Disconnecting outbound peer %d -- headers chain has insufficient work\n", pfrom.GetId());
3198 0 : pfrom.fDisconnect = true;
3199 0 : }
3200 80 : }
3201 224 : }
3202 :
3203 : // If this is an outbound full-relay peer, check to see if we should protect
3204 : // it from the bad/lagging chain logic.
3205 : // Note that outbound block-relay peers are excluded from this protection, and
3206 : // thus always subject to eviction under the bad/lagging chain logic.
3207 : // See ChainSyncTimeoutState.
3208 102556 : if (!pfrom.fDisconnect && pfrom.IsFullOutboundConn() && nodestate->pindexBestKnownBlock != nullptr) {
3209 28615 : if (m_outbound_peers_with_protect_from_disconnect < MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT && nodestate->pindexBestKnownBlock->nChainWork >= m_chainman.ActiveChain().Tip()->nChainWork && !nodestate->m_chain_sync.m_protect) {
3210 1465 : LogPrint(BCLog::NET, "Protecting outbound peer=%d from eviction\n", pfrom.GetId());
3211 1465 : nodestate->m_chain_sync.m_protect = true;
3212 1465 : ++m_outbound_peers_with_protect_from_disconnect;
3213 1465 : }
3214 28615 : }
3215 102556 : }
3216 :
3217 104579 : void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
3218 : const std::vector<CBlockHeader>& headers,
3219 : bool via_compact_block)
3220 : {
3221 104579 : const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
3222 104579 : size_t nCount = headers.size();
3223 :
3224 104579 : if (nCount == 0) {
3225 : // Nothing interesting. Stop asking this peers for more headers.
3226 1498 : return;
3227 : }
3228 :
3229 103081 : const CBlockIndex *pindexLast = nullptr;
3230 :
3231 : // Do these headers connect to something in our block index?
3232 206162 : bool headers_connect_blockindex{WITH_LOCK(::cs_main, return m_chainman.m_blockman.LookupBlockIndex(headers[0].hashPrevBlock) != nullptr)};
3233 :
3234 103081 : if (!headers_connect_blockindex) {
3235 281 : if (nCount <= MAX_BLOCKS_TO_ANNOUNCE) {
3236 : // If this looks like it could be a BIP 130 block announcement, use
3237 : // special logic for handling headers that don't connect, as this
3238 : // could be benign.
3239 281 : HandleFewUnconnectingHeaders(pfrom, peer, headers);
3240 281 : } else {
3241 0 : Misbehaving(pfrom.GetId(), 10, "invalid header received");
3242 : }
3243 281 : return;
3244 : }
3245 :
3246 : // At this point, the headers connect to something in our block index.
3247 102800 : if (!CheckHeadersAreContinuous(headers)) {
3248 2 : Misbehaving(pfrom.GetId(), 20, "non-continuous headers sequence");
3249 2 : return;
3250 : }
3251 :
3252 : // If we don't have the last header, then this peer will have given us
3253 : // something new (if these headers are valid).
3254 205596 : bool received_new_header{WITH_LOCK(::cs_main, return m_chainman.m_blockman.LookupBlockIndex(headers.back().GetHash()) == nullptr)};
3255 :
3256 102798 : BlockValidationState state;
3257 102798 : if (!m_chainman.ProcessNewBlockHeaders(headers, state, &pindexLast)) {
3258 242 : if (state.IsInvalid()) {
3259 242 : MaybePunishNodeForBlock(pfrom.GetId(), state, via_compact_block, "invalid header received");
3260 242 : return;
3261 : }
3262 0 : }
3263 :
3264 : // Consider fetching more headers.
3265 102556 : const bool uses_compressed = UsesCompressedHeaders(peer);
3266 102556 : const std::string msg_type = uses_compressed ? NetMsgType::GETHEADERS2 : NetMsgType::GETHEADERS;
3267 102556 : if (nCount == GetHeadersLimit(pfrom, uses_compressed)) {
3268 : // Headers message had its maximum size; the peer may have more headers.
3269 0 : if (MaybeSendGetHeaders(pfrom, msg_type, m_chainman.ActiveChain().GetLocator(pindexLast), peer)) {
3270 0 : LogPrint(BCLog::NET, "more %s (%d) to end to peer=%d (startheight:%d)\n",
3271 : msg_type, pindexLast->nHeight, pfrom.GetId(), peer.m_starting_height);
3272 0 : }
3273 0 : }
3274 :
3275 102556 : UpdatePeerStateForReceivedHeaders(pfrom, *pindexLast, received_new_header, nCount == GetHeadersLimit(pfrom, uses_compressed));
3276 :
3277 : // Consider immediately downloading blocks.
3278 102556 : HeadersDirectFetchBlocks(pfrom, peer, *pindexLast);
3279 :
3280 : return;
3281 104579 : }
3282 :
3283 3919123 : bool PeerManagerImpl::ProcessOrphanTx(NodeId node_id)
3284 : {
3285 3919123 : AssertLockHeld(cs_main);
3286 :
3287 3919123 : CTransactionRef porphanTx = nullptr;
3288 3919123 : NodeId from_peer = -1;
3289 3919123 : bool more = false;
3290 :
3291 3919123 : while (CTransactionRef porphanTx = m_orphanage.GetTxToReconsider(node_id, from_peer, more)) {
3292 35 : const MempoolAcceptResult result = m_chainman.ProcessTransaction(porphanTx);
3293 35 : const TxValidationState& state = result.m_state;
3294 35 : const uint256& orphanHash = porphanTx->GetHash();
3295 :
3296 35 : if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
3297 16 : LogPrint(BCLog::MEMPOOL, " accepted orphan tx %s\n", orphanHash.ToString());
3298 16 : _RelayTransaction(porphanTx->GetHash());
3299 16 : m_orphanage.AddChildrenToWorkSet(*porphanTx, node_id);
3300 16 : m_orphanage.EraseTx(orphanHash);
3301 16 : break;
3302 19 : } else if (state.GetResult() != TxValidationResult::TX_MISSING_INPUTS) {
3303 19 : if (state.IsInvalid()) {
3304 19 : LogPrint(BCLog::MEMPOOL, " invalid orphan tx %s from peer=%d. %s\n",
3305 : orphanHash.ToString(),
3306 : from_peer,
3307 : state.ToString());
3308 : // Maybe punish peer that gave us an invalid orphan tx
3309 19 : MaybePunishNodeForTx(from_peer, state);
3310 19 : }
3311 : // Has inputs but not accepted to mempool
3312 : // Probably non-standard or insufficient fee
3313 19 : LogPrint(BCLog::MEMPOOL, " removed orphan tx %s\n", orphanHash.ToString());
3314 19 : m_recent_rejects.insert(orphanHash);
3315 19 : m_orphanage.EraseTx(orphanHash);
3316 19 : break;
3317 : }
3318 3919123 : }
3319 :
3320 : // We could either have more to process from the existing work set or processing this
3321 : // orphan has given us more potential work
3322 3919123 : return more || m_orphanage.HaveMoreWork(node_id);
3323 3919123 : }
3324 :
3325 30 : bool PeerManagerImpl::PrepareBlockFilterRequest(CNode& node, Peer& peer,
3326 : BlockFilterType filter_type, uint32_t start_height,
3327 : const uint256& stop_hash, uint32_t max_height_diff,
3328 : const CBlockIndex*& stop_index,
3329 : BlockFilterIndex*& filter_index)
3330 : {
3331 30 : const bool supported_filter_type =
3332 30 : (filter_type == BlockFilterType::BASIC_FILTER &&
3333 28 : (peer.m_our_services & NODE_COMPACT_FILTERS));
3334 30 : if (!supported_filter_type) {
3335 8 : LogPrint(BCLog::NET, "peer %d requested unsupported block filter type: %d\n",
3336 : node.GetId(), static_cast<uint8_t>(filter_type));
3337 8 : node.fDisconnect = true;
3338 8 : return false;
3339 : }
3340 :
3341 : {
3342 22 : LOCK(cs_main);
3343 22 : stop_index = m_chainman.m_blockman.LookupBlockIndex(stop_hash);
3344 :
3345 : // Check that the stop block exists and the peer would be allowed to fetch it.
3346 22 : if (!stop_index || !BlockRequestAllowed(stop_index)) {
3347 2 : LogPrint(BCLog::NET, "peer %d requested invalid block hash: %s\n",
3348 : node.GetId(), stop_hash.ToString());
3349 2 : node.fDisconnect = true;
3350 2 : return false;
3351 : }
3352 22 : }
3353 :
3354 20 : uint32_t stop_height = stop_index->nHeight;
3355 20 : if (start_height > stop_height) {
3356 2 : LogPrint(BCLog::NET, "peer %d sent invalid getcfilters/getcfheaders with " /* Continued */
3357 : "start height %d and stop height %d\n",
3358 : node.GetId(), start_height, stop_height);
3359 2 : node.fDisconnect = true;
3360 2 : return false;
3361 : }
3362 18 : if (stop_height - start_height >= max_height_diff) {
3363 4 : LogPrint(BCLog::NET, "peer %d requested too many cfilters/cfheaders: %d / %d\n",
3364 : node.GetId(), stop_height - start_height + 1, max_height_diff);
3365 4 : node.fDisconnect = true;
3366 4 : return false;
3367 : }
3368 :
3369 14 : filter_index = GetBlockFilterIndex(filter_type);
3370 14 : if (!filter_index) {
3371 0 : LogPrint(BCLog::NET, "Filter index for supported type %s not found\n", BlockFilterTypeName(filter_type));
3372 0 : return false;
3373 : }
3374 :
3375 14 : return true;
3376 30 : }
3377 :
3378 8 : void PeerManagerImpl::ProcessGetCFilters(CNode& node,Peer& peer, CDataStream& vRecv)
3379 : {
3380 : uint8_t filter_type_ser;
3381 : uint32_t start_height;
3382 8 : uint256 stop_hash;
3383 :
3384 8 : vRecv >> filter_type_ser >> start_height >> stop_hash;
3385 :
3386 8 : const BlockFilterType filter_type = static_cast<BlockFilterType>(filter_type_ser);
3387 :
3388 : const CBlockIndex* stop_index;
3389 : BlockFilterIndex* filter_index;
3390 8 : if (!PrepareBlockFilterRequest(node, peer, filter_type, start_height, stop_hash,
3391 : MAX_GETCFILTERS_SIZE, stop_index, filter_index)) {
3392 4 : return;
3393 : }
3394 :
3395 4 : std::vector<BlockFilter> filters;
3396 4 : if (!filter_index->LookupFilterRange(start_height, stop_index, filters)) {
3397 0 : LogPrint(BCLog::NET, "Failed to find block filter in index: filter_type=%s, start_height=%d, stop_hash=%s\n",
3398 : BlockFilterTypeName(filter_type), start_height, stop_hash.ToString());
3399 0 : return;
3400 : }
3401 :
3402 26 : for (const auto& filter : filters) {
3403 44 : CSerializedNetMsg msg = CNetMsgMaker(node.GetCommonVersion())
3404 22 : .Make(NetMsgType::CFILTER, filter);
3405 22 : m_connman.PushMessage(&node, std::move(msg));
3406 22 : }
3407 8 : }
3408 :
3409 10 : void PeerManagerImpl::ProcessGetCFHeaders(CNode& node, Peer& peer, CDataStream& vRecv)
3410 : {
3411 : uint8_t filter_type_ser;
3412 : uint32_t start_height;
3413 10 : uint256 stop_hash;
3414 :
3415 10 : vRecv >> filter_type_ser >> start_height >> stop_hash;
3416 :
3417 10 : const BlockFilterType filter_type = static_cast<BlockFilterType>(filter_type_ser);
3418 :
3419 : const CBlockIndex* stop_index;
3420 : BlockFilterIndex* filter_index;
3421 10 : if (!PrepareBlockFilterRequest(node, peer, filter_type, start_height, stop_hash,
3422 : MAX_GETCFHEADERS_SIZE, stop_index, filter_index)) {
3423 6 : return;
3424 : }
3425 :
3426 4 : uint256 prev_header;
3427 4 : if (start_height > 0) {
3428 4 : const CBlockIndex* const prev_block =
3429 4 : stop_index->GetAncestor(static_cast<int>(start_height - 1));
3430 4 : if (!filter_index->LookupFilterHeader(prev_block, prev_header)) {
3431 0 : LogPrint(BCLog::NET, "Failed to find block filter header in index: filter_type=%s, block_hash=%s\n",
3432 : BlockFilterTypeName(filter_type), prev_block->GetBlockHash().ToString());
3433 0 : return;
3434 : }
3435 4 : }
3436 :
3437 4 : std::vector<uint256> filter_hashes;
3438 4 : if (!filter_index->LookupFilterHashRange(start_height, stop_index, filter_hashes)) {
3439 0 : LogPrint(BCLog::NET, "Failed to find block filter hashes in index: filter_type=%s, start_height=%d, stop_hash=%s\n",
3440 : BlockFilterTypeName(filter_type), start_height, stop_hash.ToString());
3441 0 : return;
3442 : }
3443 :
3444 8 : CSerializedNetMsg msg = CNetMsgMaker(node.GetCommonVersion())
3445 8 : .Make(NetMsgType::CFHEADERS,
3446 : filter_type_ser,
3447 4 : stop_index->GetBlockHash(),
3448 : prev_header,
3449 : filter_hashes);
3450 4 : m_connman.PushMessage(&node, std::move(msg));
3451 10 : }
3452 :
3453 12 : void PeerManagerImpl::ProcessGetCFCheckPt(CNode& node, Peer& peer, CDataStream& vRecv)
3454 : {
3455 : uint8_t filter_type_ser;
3456 12 : uint256 stop_hash;
3457 :
3458 12 : vRecv >> filter_type_ser >> stop_hash;
3459 :
3460 12 : const BlockFilterType filter_type = static_cast<BlockFilterType>(filter_type_ser);
3461 :
3462 : const CBlockIndex* stop_index;
3463 : BlockFilterIndex* filter_index;
3464 24 : if (!PrepareBlockFilterRequest(node, peer, filter_type, /*start_height=*/0, stop_hash,
3465 12 : /*max_height_diff=*/std::numeric_limits<uint32_t>::max(),
3466 : stop_index, filter_index)) {
3467 6 : return;
3468 : }
3469 :
3470 6 : std::vector<uint256> headers(stop_index->nHeight / CFCHECKPT_INTERVAL);
3471 :
3472 : // Populate headers.
3473 6 : const CBlockIndex* block_index = stop_index;
3474 14 : for (int i = headers.size() - 1; i >= 0; i--) {
3475 8 : int height = (i + 1) * CFCHECKPT_INTERVAL;
3476 8 : block_index = block_index->GetAncestor(height);
3477 :
3478 8 : if (!filter_index->LookupFilterHeader(block_index, headers[i])) {
3479 0 : LogPrint(BCLog::NET, "Failed to find block filter header in index: filter_type=%s, block_hash=%s\n",
3480 : BlockFilterTypeName(filter_type), block_index->GetBlockHash().ToString());
3481 0 : return;
3482 : }
3483 8 : }
3484 :
3485 12 : CSerializedNetMsg msg = CNetMsgMaker(node.GetCommonVersion())
3486 12 : .Make(NetMsgType::CFCHECKPT,
3487 : filter_type_ser,
3488 6 : stop_index->GetBlockHash(),
3489 : headers);
3490 6 : m_connman.PushMessage(&node, std::move(msg));
3491 12 : }
3492 :
3493 : // Misbehavior penalty to apply to the relaying peer; NONE means no penalty.
3494 : enum class DSTXValidationScore : int {
3495 : NONE = 0,
3496 : UNKNOWN_MASTERNODE = 1,
3497 : INVALID = 10,
3498 : };
3499 :
3500 : // do_return signals the caller to stop further processing of the DSTX.
3501 : struct DSTXValidationResult {
3502 : DSTXValidationScore score;
3503 : bool do_return;
3504 : };
3505 :
3506 204 : static DSTXValidationResult ValidateDSTX(CDeterministicMNManager& dmnman, CDSTXManager& dstxman, ChainstateManager& chainman,
3507 : CMasternodeMetaMan& mn_metaman, CTxMemPool& mempool, CCoinJoinBroadcastTx& dstx, uint256 hashTx)
3508 : {
3509 204 : assert(mn_metaman.IsValid());
3510 :
3511 204 : if (!dstx.IsValidStructure()) {
3512 2 : LogPrint(BCLog::COINJOIN, "DSTX -- Invalid DSTX structure: %s\n", hashTx.ToString());
3513 2 : return {DSTXValidationScore::INVALID, true};
3514 : }
3515 202 : if (dstxman.GetDSTX(hashTx)) {
3516 0 : LogPrint(BCLog::COINJOIN, "DSTX -- Already have %s, skipping...\n", hashTx.ToString());
3517 0 : return {DSTXValidationScore::NONE, true}; // not an error
3518 : }
3519 :
3520 202 : const CBlockIndex* pindex{nullptr};
3521 202 : CDeterministicMNCPtr dmn{nullptr};
3522 : {
3523 202 : LOCK(cs_main);
3524 202 : pindex = chainman.ActiveChain().Tip();
3525 202 : }
3526 : // It could be that a MN is no longer in the list but its DSTX is not yet mined.
3527 : // Try to find a MN up to 24 blocks deep to make sure such dstx-es are relayed and processed correctly.
3528 202 : if (dstx.masternodeOutpoint.IsNull()) {
3529 606 : for (int i = 0; i < 24 && pindex; ++i) {
3530 404 : dmn = dmnman.GetListForBlock(pindex).GetMN(dstx.m_protxHash);
3531 404 : if (dmn) {
3532 0 : dstx.masternodeOutpoint = dmn->collateralOutpoint;
3533 0 : break;
3534 : }
3535 404 : pindex = pindex->pprev;
3536 404 : }
3537 202 : } else {
3538 0 : for (int i = 0; i < 24 && pindex; ++i) {
3539 0 : dmn = dmnman.GetListForBlock(pindex).GetMNByCollateral(dstx.masternodeOutpoint);
3540 0 : if (dmn) {
3541 0 : dstx.m_protxHash = dmn->proTxHash;
3542 0 : break;
3543 : }
3544 0 : pindex = pindex->pprev;
3545 0 : }
3546 : }
3547 :
3548 202 : if (!dmn) {
3549 202 : LogPrint(BCLog::COINJOIN, "DSTX -- Can't find masternode %s to verify %s\n", dstx.masternodeOutpoint.ToStringShort(), hashTx.ToString());
3550 : // We can't verify the signature here, so apply only a small penalty.
3551 : // The MN may have been removed very recently, but a peer flooding us with
3552 : // unverifiable DSTX-es should still eventually be discouraged.
3553 202 : return {DSTXValidationScore::UNKNOWN_MASTERNODE, true};
3554 : }
3555 :
3556 0 : if (!mn_metaman.IsValidForMixingTxes(dmn->proTxHash)) {
3557 0 : LogPrint(BCLog::COINJOIN, "DSTX -- Masternode %s is sending too many transactions %s\n", dstx.masternodeOutpoint.ToStringShort(), hashTx.ToString());
3558 0 : return {DSTXValidationScore::NONE, true};
3559 : // TODO: Not an error? Could it be that someone is relaying old DSTXes
3560 : // we have no idea about (e.g we were offline)? How to handle them?
3561 : }
3562 :
3563 0 : if (!dstx.CheckSignature(dmn->pdmnState->pubKeyOperator.Get())) {
3564 0 : LogPrint(BCLog::COINJOIN, "DSTX -- CheckSignature() failed for %s\n", hashTx.ToString());
3565 0 : return {DSTXValidationScore::INVALID, true};
3566 : }
3567 :
3568 0 : LogPrint(BCLog::COINJOIN, "DSTX -- Got Masternode transaction %s\n", hashTx.ToString());
3569 0 : mempool.PrioritiseTransaction(hashTx, 0.1*COIN);
3570 0 : mn_metaman.DisallowMixing(dmn->proTxHash);
3571 :
3572 0 : return {DSTXValidationScore::NONE, false};
3573 204 : }
3574 :
3575 155716 : void PeerManagerImpl::ProcessBlock(CNode& node, const std::shared_ptr<const CBlock>& block, bool force_processing)
3576 : {
3577 155716 : bool new_block{false};
3578 155716 : m_chainman.ProcessNewBlock(block, force_processing, &new_block);
3579 155716 : if (new_block) {
3580 149499 : node.m_last_block_time = GetTime<std::chrono::seconds>();
3581 : // In case this block came from a different peer than we requested
3582 : // from, we can erase the block request now anyway (as we just stored
3583 : // this block to disk).
3584 149499 : LOCK(cs_main);
3585 149499 : RemoveBlockRequest(block->GetHash(), std::nullopt);
3586 149499 : } else {
3587 6217 : LOCK(cs_main);
3588 6217 : mapBlockSource.erase(block->GetHash());
3589 6217 : }
3590 155716 : }
3591 :
3592 330660 : void PeerManagerImpl::PostProcessMessage(MessageProcessingResult&& result, NodeId node)
3593 : {
3594 330660 : if (result.m_error) {
3595 4 : Misbehaving(node, result.m_error->score, result.m_error->message);
3596 4 : }
3597 330660 : if (result.m_to_erase) {
3598 5154 : WITH_LOCK(cs_main, EraseObjectRequest(node, result.m_to_erase.value()));
3599 2577 : }
3600 330660 : for (const auto& tx : result.m_transactions) {
3601 0 : WITH_LOCK(cs_main, _RelayTransaction(tx));
3602 : }
3603 341782 : for (const auto& inv : result.m_inventory) {
3604 11122 : RelayInv(inv);
3605 : }
3606 330660 : }
3607 :
3608 105757 : MessageProcessingResult PeerManagerImpl::ProcessPlatformBanMessage(NodeId node, std::string_view msg_type, CDataStream& vRecv)
3609 : {
3610 105757 : if (msg_type != NetMsgType::PLATFORMBAN) return {};
3611 :
3612 : // Do nothing if node is out of sync
3613 10 : if (!m_mn_sync.IsBlockchainSynced()) {
3614 0 : return {};
3615 : }
3616 :
3617 10 : PlatformBanMessage ban_msg;
3618 10 : vRecv >> ban_msg;
3619 :
3620 10 : const uint256 hash = ban_msg.GetHash();
3621 :
3622 10 : LogPrintf("PLATFORMBAN -- hash: %s protx_hash: %s height: %d peer=%d\n", hash.ToString(), ban_msg.m_protx_hash.ToString(), ban_msg.m_requested_height, node);
3623 :
3624 10 : MessageProcessingResult ret{};
3625 10 : ret.m_to_erase = CInv{MSG_PLATFORM_BAN, hash};
3626 :
3627 10 : const auto list = Assert(m_dmnman)->GetListAtChainTip();
3628 10 : auto dmn = list.GetMN(ban_msg.m_protx_hash);
3629 10 : if (!dmn) {
3630 : // small P2P penalty (1), as the evonode may have very recently been removed
3631 0 : ret.m_error = MisbehavingError{1};
3632 0 : return ret;
3633 : }
3634 10 : if (dmn->nType != MnType::Evo) {
3635 : // Ban node, P2P penalty (100) if protx_hash is associated with a regular node not an evonode
3636 0 : LogPrintf("PLATFORMBAN -- hash: %s protx_hash: %s unexpected type of node\n", hash.ToString(), ban_msg.m_protx_hash.ToString());
3637 0 : ret.m_error = MisbehavingError{100};
3638 0 : return ret;
3639 : }
3640 : static constexpr int PLATFORM_BAN_WINDOW_BLOCKS = 576;
3641 20 : int tipHeight = WITH_LOCK(cs_main, return m_chainman.ActiveChainstate().m_chain.Height());
3642 10 : if (tipHeight < ban_msg.m_requested_height || tipHeight - PLATFORM_BAN_WINDOW_BLOCKS > ban_msg.m_requested_height) {
3643 : // m_requested_height is inside the range [TipHeight - PLATFORM_BAN_WINDOW_BLOCKS - 5, TipHeight + 5]
3644 0 : LogPrintf("PLATFORMBAN -- hash: %s protx_hash: %s unexpected height: %d tip: %d\n", hash.ToString(), ban_msg.m_protx_hash.ToString(), ban_msg.m_requested_height, tipHeight);
3645 0 : if (tipHeight + 5 < ban_msg.m_requested_height || tipHeight - PLATFORM_BAN_WINDOW_BLOCKS - 5 > ban_msg.m_requested_height) {
3646 : // m_requested_height is outside the range [TipHeight - PLATFORM_BAN_WINDOW_BLOCKS - 5, TipHeight + 5]
3647 0 : ret.m_error = MisbehavingError{10};
3648 0 : return ret;
3649 : }
3650 0 : ret.m_error = MisbehavingError{1};
3651 0 : return ret;
3652 : }
3653 :
3654 10 : Consensus::LLMQType llmq_type = Params().GetConsensus().llmqTypePlatform;
3655 10 : auto quorum = m_llmq_ctx->qman->GetQuorum(llmq_type, ban_msg.m_quorum_hash);
3656 10 : if (!quorum) {
3657 0 : LogPrintf("PLATFORMBAN -- hash: %s protx_hash: %s missing quorum_hash: %s llmq_type: %d\n", hash.ToString(), ban_msg.m_protx_hash.ToString(), ban_msg.m_quorum_hash.ToString(), std23::to_underlying(llmq_type));
3658 0 : ret.m_error = MisbehavingError{100};
3659 0 : return ret;
3660 : }
3661 :
3662 10 : const std::string PLATFORM_BAN_REQUESTID_PREFIX = "PlatformPoSeBan";
3663 10 : const auto data = std::make_pair(ban_msg.m_protx_hash, ban_msg.m_requested_height);
3664 10 : const uint256 request_id = ::SerializeHash(std::make_pair(PLATFORM_BAN_REQUESTID_PREFIX, data));
3665 10 : const uint256 msg_hash = ::SerializeHash(data);
3666 :
3667 10 : auto signHash = llmq::SignHash(llmq_type, quorum->qc->quorumHash, request_id, msg_hash);
3668 :
3669 10 : if (!ban_msg.m_signature.VerifyInsecure(quorum->qc->quorumPublicKey, signHash.Get())) {
3670 0 : LogPrintf("PLATFORMBAN -- hash: %s protx_hash: %s request_id: %s msg_hash: %s sig validation failed\n", hash.ToString(), ban_msg.m_protx_hash.ToString(), request_id.ToString(), msg_hash.ToString());
3671 0 : ret.m_error = MisbehavingError{100};
3672 0 : return ret;
3673 : }
3674 :
3675 : // At this point, the outgoing message serialization version can't change.
3676 10 : if (m_mn_metaman.SetPlatformBan(hash, std::move(ban_msg))) {
3677 10 : LogPrintf("PLATFORMBAN -- hash: %s forward message to other nodes\n", hash.ToString());
3678 10 : ret.m_inventory.emplace_back(MSG_PLATFORM_BAN, hash);
3679 10 : }
3680 10 : return ret;
3681 105767 : }
3682 :
3683 854398 : void PeerManagerImpl::ProcessMessage(
3684 : CNode& pfrom,
3685 : const std::string& msg_type,
3686 : CDataStream& vRecv,
3687 : const std::chrono::microseconds time_received,
3688 : const std::atomic<bool>& interruptMsgProc)
3689 : {
3690 854398 : AssertLockHeld(g_msgproc_mutex);
3691 :
3692 854413 : LogPrint(BCLog::NET, "received: %s (%u bytes) peer=%d\n", SanitizeString(msg_type), vRecv.size(), pfrom.GetId());
3693 854398 : ::g_stats_client->inc("message.received." + SanitizeString(msg_type), 1.0f);
3694 :
3695 854398 : const bool is_masternode = m_nodeman != nullptr;
3696 :
3697 854398 : PeerRef peer = GetPeerRef(pfrom.GetId());
3698 854398 : if (peer == nullptr) return;
3699 :
3700 854398 : if (msg_type == NetMsgType::VERSION) {
3701 9012 : if (pfrom.nVersion != 0) {
3702 2 : LogPrint(BCLog::NET, "redundant version message from peer=%d\n", pfrom.GetId());
3703 2 : return;
3704 : }
3705 :
3706 : int64_t nTime;
3707 9010 : CService addrMe;
3708 9010 : uint64_t nNonce = 1;
3709 : ServiceFlags nServices;
3710 : int nVersion;
3711 9010 : std::string cleanSubVer;
3712 9010 : int starting_height = -1;
3713 9010 : bool fRelay = true;
3714 :
3715 9010 : vRecv >> nVersion >> Using<CustomUintFormatter<8>>(nServices) >> nTime;
3716 9010 : if (nTime < 0) {
3717 0 : nTime = 0;
3718 0 : }
3719 9010 : vRecv.ignore(8); // Ignore the addrMe service bits sent by the peer
3720 9010 : vRecv >> addrMe;
3721 9010 : if (!pfrom.IsInboundConn())
3722 : {
3723 : // Overwrites potentially existing services. In contrast to this,
3724 : // unvalidated services received via gossip relay in ADDR/ADDRV2
3725 : // messages are only ever added but cannot replace existing ones.
3726 4032 : m_addrman.SetServices(pfrom.addr, nServices);
3727 4032 : }
3728 9010 : if (pfrom.ExpectServicesFromConn() && !HasAllDesirableServiceFlags(nServices))
3729 : {
3730 0 : LogPrint(BCLog::NET, "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom.GetId(), nServices, GetDesirableServiceFlags(nServices));
3731 0 : pfrom.fDisconnect = true;
3732 0 : return;
3733 : }
3734 :
3735 9010 : if (nVersion < MIN_PEER_PROTO_VERSION) {
3736 : // disconnect from peers older than this proto version
3737 2 : LogPrint(BCLog::NET, "peer=%d using obsolete version %i; disconnecting\n", pfrom.GetId(), nVersion);
3738 2 : pfrom.fDisconnect = true;
3739 2 : return;
3740 : }
3741 :
3742 9008 : if (!vRecv.empty()) {
3743 : // The version message includes information about the sending node which we don't use:
3744 : // - 8 bytes (service bits)
3745 : // - 16 bytes (ipv6 address)
3746 : // - 2 bytes (port)
3747 9007 : vRecv.ignore(26);
3748 9007 : vRecv >> nNonce;
3749 9007 : }
3750 9008 : if (!vRecv.empty()) {
3751 9007 : std::string strSubVer;
3752 9007 : vRecv >> LIMITED_STRING(strSubVer, MAX_SUBVERSION_LENGTH);
3753 9007 : cleanSubVer = SanitizeString(strSubVer);
3754 9007 : }
3755 9008 : if (!vRecv.empty()) {
3756 9007 : vRecv >> starting_height;
3757 9007 : }
3758 9008 : if (!vRecv.empty())
3759 9007 : vRecv >> fRelay;
3760 9008 : if (!vRecv.empty()) {
3761 7763 : uint256 receivedMNAuthChallenge;
3762 7763 : vRecv >> receivedMNAuthChallenge;
3763 7763 : pfrom.SetReceivedMNAuthChallenge(receivedMNAuthChallenge);
3764 7763 : }
3765 9008 : if (!vRecv.empty()) {
3766 7763 : bool fOtherMasternode = false;
3767 7763 : vRecv >> fOtherMasternode;
3768 7763 : if (pfrom.IsInboundConn()) {
3769 3877 : pfrom.m_masternode_connection = fOtherMasternode;
3770 3877 : if (fOtherMasternode) {
3771 1926 : LogPrint(BCLog::NET_NETCONN, "peer=%d is an inbound masternode connection, not relaying anything to it\n", pfrom.GetId());
3772 1926 : if (!is_masternode) {
3773 0 : LogPrint(BCLog::NET_NETCONN, "but we're not a masternode, disconnecting\n");
3774 0 : pfrom.fDisconnect = true;
3775 0 : return;
3776 : }
3777 1926 : }
3778 3877 : }
3779 7763 : }
3780 : // Disconnect if we connected to ourself
3781 9008 : if (pfrom.IsInboundConn() && !m_connman.CheckIncomingNonce(nNonce))
3782 : {
3783 0 : LogPrintf("connected to self at %s, disconnecting\n", pfrom.addr.ToStringAddrPort());
3784 0 : pfrom.fDisconnect = true;
3785 0 : return;
3786 : }
3787 :
3788 9008 : if (pfrom.IsInboundConn() && addrMe.IsRoutable())
3789 : {
3790 0 : SeenLocal(addrMe);
3791 0 : }
3792 :
3793 : // Be shy and don't send version until we hear
3794 9008 : if (pfrom.IsInboundConn()) {
3795 4976 : PushNodeVersion(pfrom, *peer);
3796 4976 : }
3797 :
3798 9008 : if (Params().NetworkIDString() == CBaseChainParams::DEVNET) {
3799 6 : if (cleanSubVer.find(strprintf("devnet.%s", gArgs.GetDevNetName())) == std::string::npos) {
3800 0 : LogPrintf("connected to wrong devnet. Reported version is %s, expected devnet name is %s\n", cleanSubVer, gArgs.GetDevNetName());
3801 0 : if (!pfrom.IsInboundConn())
3802 0 : Misbehaving(pfrom.GetId(), 100); // don't try to connect again
3803 : else
3804 0 : Misbehaving(pfrom.GetId(), 1); // whover connected, might just have made a mistake, don't ban him immediately
3805 0 : pfrom.fDisconnect = true;
3806 0 : return;
3807 : }
3808 6 : }
3809 :
3810 : // Change version
3811 9008 : const int greatest_common_version = std::min(nVersion, PROTOCOL_VERSION);
3812 9008 : pfrom.SetCommonVersion(greatest_common_version);
3813 9008 : pfrom.nVersion = nVersion;
3814 :
3815 9008 : const CNetMsgMaker msg_maker(greatest_common_version);
3816 : // Signal ADDRv2 support (BIP155).
3817 9008 : if (greatest_common_version >= ADDRV2_PROTO_VERSION) {
3818 : // BIP155 defines addrv2 and sendaddrv2 for all protocol versions, but some
3819 : // implementations reject messages they don't know. As a courtesy, don't send
3820 : // it to nodes with a version before ADDRV2_PROTO_VERSION.
3821 9008 : m_connman.PushMessage(&pfrom, msg_maker.Make(NetMsgType::SENDADDRV2));
3822 9008 : }
3823 :
3824 9008 : pfrom.m_has_all_wanted_services = HasAllDesirableServiceFlags(nServices);
3825 9008 : peer->m_their_services = nServices;
3826 9008 : pfrom.SetAddrLocal(addrMe);
3827 : {
3828 9008 : LOCK(pfrom.m_subver_mutex);
3829 9008 : pfrom.cleanSubVer = cleanSubVer;
3830 9008 : }
3831 9008 : peer->m_starting_height = starting_height;
3832 :
3833 : // We only initialize the Peer::TxRelay m_relay_txs data structure if:
3834 : // - this isn't an outbound block-relay-only connection, and
3835 : // - this isn't an outbound feeler connection, and
3836 : // - fRelay=true (the peer wishes to receive transaction announcements)
3837 : // or we're offering NODE_BLOOM to this peer. NODE_BLOOM means that
3838 : // the peer may turn on transaction relay later.
3839 9014 : if (!pfrom.IsBlockOnlyConn() &&
3840 8968 : !pfrom.IsFeelerConn() &&
3841 8964 : (fRelay || (peer->m_our_services & NODE_BLOOM))) {
3842 8964 : auto* const tx_relay = peer->SetTxRelay();
3843 : {
3844 8964 : LOCK(tx_relay->m_bloom_filter_mutex);
3845 8964 : tx_relay->m_relay_txs = fRelay; // set to true after we get the first filter* message
3846 8964 : }
3847 8964 : if (fRelay) pfrom.m_relays_txs = true;
3848 8964 : }
3849 :
3850 9008 : if (greatest_common_version >= INCREASE_MAX_HEADERS2_VERSION && m_txreconciliation) {
3851 : // Per BIP-330, we announce txreconciliation support if:
3852 : // - protocol version per the peer's VERSION message supports INCREASE_MAX_HEADERS2_VERSION;
3853 : // - transaction relay is supported per the peer's VERSION message
3854 : // - this is not a block-relay-only connection and not a feeler
3855 : // - this is not an addr fetch connection;
3856 : // - we are not in -blocksonly mode.
3857 28 : const auto* tx_relay = peer->GetTxRelay();
3858 68 : if (tx_relay && WITH_LOCK(tx_relay->m_bloom_filter_mutex, return tx_relay->m_relay_txs) &&
3859 18 : !pfrom.IsAddrFetchConn() && !m_ignore_incoming_txs) {
3860 14 : const uint64_t recon_salt = m_txreconciliation->PreRegisterPeer(pfrom.GetId());
3861 14 : m_connman.PushMessage(&pfrom, msg_maker.Make(NetMsgType::SENDTXRCNCL,
3862 : TXRECONCILIATION_VERSION, recon_salt));
3863 14 : }
3864 28 : }
3865 :
3866 9008 : m_connman.PushMessage(&pfrom, msg_maker.Make(NetMsgType::VERACK));
3867 :
3868 : // Potentially mark this peer as a preferred download peer.
3869 : {
3870 9008 : LOCK(cs_main);
3871 9008 : CNodeState* state = State(pfrom.GetId());
3872 13452 : state->fPreferredDownload = (!pfrom.IsInboundConn() || pfrom.HasPermission(NetPermissionFlags::NoBan)) && !pfrom.IsAddrFetchConn() && CanServeBlocks(*peer);
3873 9008 : m_num_preferred_download_peers += state->fPreferredDownload;
3874 9008 : }
3875 :
3876 : // Attempt to initialize address relay for outbound peers and use result
3877 : // to decide whether to send GETADDR, so that we don't send it to
3878 : // inbound or outbound block-relay-only peers.
3879 9008 : bool send_getaddr{false};
3880 9008 : if (!pfrom.IsInboundConn()) {
3881 4032 : send_getaddr = SetupAddressRelay(pfrom, *peer);
3882 4032 : }
3883 9008 : if (send_getaddr) {
3884 : // Do a one-time address fetch to help populate/update our addrman.
3885 : // If we're starting up for the first time, our addrman may be pretty
3886 : // empty, so this mechanism is important to help us connect to the network.
3887 : // We skip this for block-relay-only peers. We want to avoid
3888 : // potentially leaking addr information and we do not want to
3889 : // indicate to the peer that we will participate in addr relay.
3890 3992 : m_connman.PushMessage(&pfrom, CNetMsgMaker(greatest_common_version).Make(NetMsgType::GETADDR));
3891 3992 : peer->m_getaddr_sent = true;
3892 : // When requesting a getaddr, accept an additional MAX_ADDR_TO_SEND addresses in response
3893 : // (bypassing the MAX_ADDR_PROCESSING_TOKEN_BUCKET limit).
3894 3992 : peer->m_addr_token_bucket += MAX_ADDR_TO_SEND;
3895 3992 : }
3896 :
3897 9008 : if (!pfrom.IsInboundConn()) {
3898 : // For non-inbound connections, we update the addrman to record
3899 : // connection success so that addrman will have an up-to-date
3900 : // notion of which peers are online and available.
3901 : //
3902 : // While we strive to not leak information about block-relay-only
3903 : // connections via the addrman, not moving an address to the tried
3904 : // table is also potentially detrimental because new-table entries
3905 : // are subject to eviction in the event of addrman collisions. We
3906 : // mitigate the information-leak by never calling
3907 : // AddrMan::Connected() on block-relay-only peers; see
3908 : // FinalizeNode().
3909 : //
3910 : // This moves an address from New to Tried table in Addrman,
3911 : // resolves tried-table collisions, etc.
3912 4032 : m_addrman.Good(pfrom.addr);
3913 4032 : }
3914 :
3915 9008 : std::string remoteAddr;
3916 9008 : if (fLogIPs)
3917 4 : remoteAddr = ", peeraddr=" + pfrom.addr.ToStringAddrPort();
3918 :
3919 9008 : const auto mapped_as{m_connman.GetMappedAS(pfrom.addr)};
3920 9008 : LogPrint(BCLog::NET, "receive version message: %s: version %d, blocks=%d, us=%s, txrelay=%d, peer=%d%s%s\n",
3921 : cleanSubVer, pfrom.nVersion,
3922 : peer->m_starting_height, addrMe.ToStringAddrPort(), fRelay, pfrom.GetId(),
3923 : remoteAddr, (mapped_as ? strprintf(", mapped_as=%d", mapped_as) : ""));
3924 :
3925 9008 : int64_t nTimeOffset = nTime - GetTime();
3926 9008 : pfrom.nTimeOffset = nTimeOffset;
3927 9008 : if (!pfrom.IsInboundConn()) {
3928 : // Don't use timedata samples from inbound peers to make it
3929 : // harder for others to tamper with our adjusted time.
3930 4032 : AddTimeData(pfrom.addr, nTimeOffset);
3931 4032 : }
3932 :
3933 : // Feeler connections exist only to verify if address is online.
3934 9008 : if (pfrom.IsFeelerConn()) {
3935 4 : LogPrint(BCLog::NET_NETCONN, "feeler connection completed peer=%d; disconnecting\n", pfrom.GetId());
3936 4 : pfrom.fDisconnect = true;
3937 4 : }
3938 : return;
3939 9010 : }
3940 :
3941 845386 : if (pfrom.nVersion == 0) {
3942 : // Must have a version message before anything else
3943 12 : LogPrint(BCLog::NET, "non-version message before version handshake. Message \"%s\" from peer=%d\n", SanitizeString(msg_type), pfrom.GetId());
3944 12 : return;
3945 : }
3946 :
3947 : // At this point, the outgoing message serialization version can't change.
3948 845374 : const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
3949 :
3950 845374 : if (msg_type == NetMsgType::VERACK) {
3951 8984 : if (pfrom.fSuccessfullyConnected) {
3952 0 : LogPrint(BCLog::NET, "ignoring redundant verack message from peer=%d\n", pfrom.GetId());
3953 0 : return;
3954 : }
3955 :
3956 : // Log successful connections unconditionally for outbound, but not for inbound as those
3957 : // can be triggered by an attacker at high rate.
3958 8984 : if (!pfrom.IsInboundConn() || LogAcceptCategory(BCLog::NET, BCLog::Level::Debug)) {
3959 8984 : const auto mapped_as{m_connman.GetMappedAS(pfrom.addr)};
3960 8984 : LogPrintf("New %s %s peer connected: version: %d, blocks=%d, peer=%d%s%s\n",
3961 : pfrom.ConnectionTypeAsString(),
3962 : TransportTypeAsString(pfrom.m_transport->GetInfo().transport_type),
3963 : pfrom.nVersion.load(), peer->m_starting_height,
3964 : pfrom.GetId(), (fLogIPs ? strprintf(", peeraddr=%s", pfrom.addr.ToStringAddrPort()) : ""),
3965 : (mapped_as ? strprintf(", mapped_as=%d", mapped_as) : ""));
3966 8984 : }
3967 :
3968 8984 : if (is_masternode && !pfrom.m_masternode_probe_connection) {
3969 4631 : CMNAuth::PushMNAUTH(pfrom, m_connman, *Assert(m_nodeman));
3970 4631 : }
3971 :
3972 : // Tell our peer we prefer to receive headers rather than inv's
3973 : // We send this to non-NODE NETWORK peers as well, because even
3974 : // non-NODE NETWORK peers can announce blocks (such as pruning
3975 : // nodes)
3976 8984 : m_connman.PushMessage(&pfrom, msgMaker.Make(UsesCompressedHeaders(*peer) ? NetMsgType::SENDHEADERS2 : NetMsgType::SENDHEADERS));
3977 :
3978 8984 : if (pfrom.CanRelay()) {
3979 : // Tell our peer we are willing to provide version 1 cmpctblocks.
3980 : // However, we do not request new block announcements using
3981 : // cmpctblock messages.
3982 : // We send this to non-NODE NETWORK peers as well, because
3983 : // they may wish to request compact blocks from us
3984 5136 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::SENDCMPCT, /*high_bandwidth=*/false, /*version=*/CMPCTBLOCKS_VERSION));
3985 5136 : }
3986 :
3987 8984 : if (!RejectIncomingTxs(pfrom)) {
3988 : // Tell our peer that he should send us CoinJoin queue messages
3989 8932 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::SENDDSQUEUE, true));
3990 : // Tell our peer that he should send us intra-quorum messages
3991 8932 : const auto tip_mn_list = Assert(m_dmnman)->GetListAtChainTip();
3992 8932 : if (m_llmq_ctx->qman->IsWatching() && m_connman.IsMasternodeQuorumNode(&pfrom, tip_mn_list)) {
3993 9 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::QWATCH));
3994 9 : }
3995 8932 : }
3996 :
3997 8984 : if (m_txreconciliation) {
3998 20 : if (pfrom.nVersion < INCREASE_MAX_HEADERS2_VERSION || !m_txreconciliation->IsPeerRegistered(pfrom.GetId())) {
3999 : // We could have optimistically pre-registered/registered the peer. In that case,
4000 : // we should forget about the reconciliation state here if the node version is below
4001 : // our minimum supported version.
4002 20 : m_txreconciliation->ForgetPeer(pfrom.GetId());
4003 20 : }
4004 20 : }
4005 :
4006 8984 : pfrom.fSuccessfullyConnected = true;
4007 8984 : return;
4008 : }
4009 :
4010 836390 : if (msg_type == NetMsgType::SENDHEADERS) {
4011 6 : LOCK(cs_main);
4012 6 : State(pfrom.GetId())->fPreferHeaders = true;
4013 : return;
4014 6 : }
4015 :
4016 836384 : if (msg_type == NetMsgType::SENDHEADERS2) {
4017 7456 : LOCK(cs_main);
4018 7456 : State(pfrom.GetId())->fPreferHeadersCompressed = true;
4019 : return;
4020 7456 : }
4021 :
4022 828928 : if (msg_type == NetMsgType::SENDCMPCT) {
4023 5679 : bool sendcmpct_hb{false};
4024 5679 : uint64_t sendcmpct_version{0};
4025 5679 : vRecv >> sendcmpct_hb >> sendcmpct_version;
4026 :
4027 5679 : if (sendcmpct_version != CMPCTBLOCKS_VERSION) return;
4028 :
4029 5667 : LOCK(cs_main);
4030 5667 : CNodeState *nodestate = State(pfrom.GetId());
4031 5667 : nodestate->m_provides_cmpctblocks = true;
4032 5667 : nodestate->m_requested_hb_cmpctblocks = sendcmpct_hb;
4033 : // save whether peer selects us as BIP152 high-bandwidth peer
4034 : // (receiving sendcmpct(1) signals high-bandwidth, sendcmpct(0) low-bandwidth)
4035 5667 : pfrom.m_bip152_highbandwidth_from = sendcmpct_hb;
4036 : return;
4037 5667 : }
4038 :
4039 : // BIP155 defines feature negotiation of addrv2 and sendaddrv2, which must happen
4040 : // between VERSION and VERACK.
4041 823249 : if (msg_type == NetMsgType::SENDADDRV2) {
4042 7765 : if (pfrom.GetCommonVersion() < ADDRV2_PROTO_VERSION) {
4043 : // Ignore previous implementations
4044 0 : return;
4045 : }
4046 7765 : if (pfrom.fSuccessfullyConnected) {
4047 : // Disconnect peers that send a SENDADDRV2 message after VERACK.
4048 0 : LogPrint(BCLog::NET_NETCONN, "sendaddrv2 received after verack from peer=%d; disconnecting\n", pfrom.GetId());
4049 0 : pfrom.fDisconnect = true;
4050 0 : return;
4051 : }
4052 7765 : peer->m_wants_addrv2 = true;
4053 7765 : return;
4054 : }
4055 :
4056 : // Received from a peer demonstrating readiness to announce transactions via reconciliations.
4057 : // This feature negotiation must happen between VERSION and VERACK to avoid relay problems
4058 : // from switching announcement protocols after the connection is up.
4059 815484 : if (msg_type == NetMsgType::SENDTXRCNCL) {
4060 16 : if (!m_txreconciliation) {
4061 2 : LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "sendtxrcncl from peer=%d ignored, as our node does not have txreconciliation enabled\n", pfrom.GetId());
4062 2 : return;
4063 : }
4064 :
4065 14 : if (pfrom.fSuccessfullyConnected) {
4066 2 : LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "sendtxrcncl received after verack from peer=%d; disconnecting\n", pfrom.GetId());
4067 2 : pfrom.fDisconnect = true;
4068 2 : return;
4069 : }
4070 :
4071 : // Peer must not offer us reconciliations if we specified no tx relay support in VERSION.
4072 12 : if (RejectIncomingTxs(pfrom)) {
4073 2 : LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "sendtxrcncl received from peer=%d to which we indicated no tx relay; disconnecting\n", pfrom.GetId());
4074 2 : pfrom.fDisconnect = true;
4075 2 : return;
4076 : }
4077 :
4078 : // Peer must not offer us reconciliations if they specified no tx relay support in VERSION.
4079 : // This flag might also be false in other cases, but the RejectIncomingTxs check above
4080 : // eliminates them, so that this flag fully represents what we are looking for.
4081 10 : const auto* tx_relay = peer->GetTxRelay();
4082 20 : if (!tx_relay || !WITH_LOCK(tx_relay->m_bloom_filter_mutex, return tx_relay->m_relay_txs)) {
4083 0 : LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "sendtxrcncl received from peer=%d which indicated no tx relay to us; disconnecting\n", pfrom.GetId());
4084 0 : pfrom.fDisconnect = true;
4085 0 : return;
4086 : }
4087 :
4088 : uint32_t peer_txreconcl_version;
4089 : uint64_t remote_salt;
4090 10 : vRecv >> peer_txreconcl_version >> remote_salt;
4091 :
4092 10 : const ReconciliationRegisterResult result = m_txreconciliation->RegisterPeer(pfrom.GetId(), pfrom.IsInboundConn(),
4093 10 : peer_txreconcl_version, remote_salt);
4094 10 : switch (result) {
4095 : case ReconciliationRegisterResult::NOT_FOUND:
4096 2 : LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "Ignore unexpected txreconciliation signal from peer=%d\n", pfrom.GetId());
4097 2 : break;
4098 : case ReconciliationRegisterResult::SUCCESS:
4099 4 : break;
4100 : case ReconciliationRegisterResult::ALREADY_REGISTERED:
4101 2 : LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "txreconciliation protocol violation from peer=%d (sendtxrcncl received from already registered peer); disconnecting\n", pfrom.GetId());
4102 2 : pfrom.fDisconnect = true;
4103 2 : return;
4104 : case ReconciliationRegisterResult::PROTOCOL_VIOLATION:
4105 2 : LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "txreconciliation protocol violation from peer=%d; disconnecting\n", pfrom.GetId());
4106 2 : pfrom.fDisconnect = true;
4107 2 : return;
4108 : }
4109 6 : return;
4110 : }
4111 :
4112 815468 : if (!pfrom.fSuccessfullyConnected) {
4113 16 : LogPrint(BCLog::NET, "Unsupported message \"%s\" prior to verack from peer=%d\n", SanitizeString(msg_type), pfrom.GetId());
4114 16 : return;
4115 : }
4116 :
4117 815452 : if (pfrom.nTimeFirstMessageReceived.load() == 0s) {
4118 : // First message after VERSION/VERACK
4119 8982 : pfrom.nTimeFirstMessageReceived = GetTime<std::chrono::seconds>();
4120 8982 : pfrom.fFirstMessageIsMNAUTH = msg_type == NetMsgType::MNAUTH;
4121 : // Note: do not break the flow here
4122 :
4123 8982 : if (pfrom.m_masternode_probe_connection && !pfrom.fFirstMessageIsMNAUTH) {
4124 0 : LogPrint(BCLog::NET, "connection is a masternode probe but first received message is not MNAUTH, peer=%d\n", pfrom.GetId());
4125 0 : pfrom.fDisconnect = true;
4126 0 : return;
4127 : }
4128 8982 : }
4129 :
4130 : // Stop processing non-block data early in blocks only mode and for block-relay-only peers
4131 815452 : if (RejectIncomingTxs(pfrom) && NetMessageViolatesBlocksOnly(msg_type)) {
4132 4 : LogPrint(BCLog::NET, "%s sent in violation of protocol peer=%d\n", msg_type, pfrom.GetId());
4133 4 : pfrom.fDisconnect = true;
4134 4 : return;
4135 : }
4136 :
4137 815448 : if (msg_type == NetMsgType::ADDR || msg_type == NetMsgType::ADDRV2) {
4138 110 : int stream_version = vRecv.GetVersion();
4139 110 : if (msg_type == NetMsgType::ADDRV2) {
4140 : // Add ADDRV2_FORMAT to the version so that the CNetAddr and CAddress
4141 : // unserialize methods know that an address in v2 format is coming.
4142 4 : stream_version |= ADDRV2_FORMAT;
4143 4 : }
4144 :
4145 110 : OverrideStream<CDataStream> s(&vRecv, vRecv.GetType(), stream_version);
4146 110 : std::vector<CAddress> vAddr;
4147 :
4148 110 : s >> vAddr;
4149 :
4150 98 : if (!SetupAddressRelay(pfrom, *peer)) {
4151 10 : LogPrint(BCLog::NET, "ignoring %s message from %s peer=%d\n", msg_type, pfrom.ConnectionTypeAsString(), pfrom.GetId());
4152 10 : return;
4153 : }
4154 :
4155 88 : if (vAddr.size() > MAX_ADDR_TO_SEND)
4156 : {
4157 4 : Misbehaving(pfrom.GetId(), 20, strprintf("%s message size = %u", msg_type, vAddr.size()));
4158 4 : return;
4159 : }
4160 :
4161 : // Store the new addresses
4162 84 : std::vector<CAddress> vAddrOk;
4163 84 : const auto current_a_time{Now<NodeSeconds>()};
4164 :
4165 : // Update/increment addr rate limiting bucket.
4166 84 : const auto current_time{GetTime<std::chrono::microseconds>()};
4167 84 : if (peer->m_addr_token_bucket < MAX_ADDR_PROCESSING_TOKEN_BUCKET) {
4168 : // Don't increment bucket if it's already full
4169 76 : const auto time_diff = std::max(current_time - peer->m_addr_token_timestamp, 0us);
4170 76 : const double increment = Ticks<SecondsDouble>(time_diff) * MAX_ADDR_RATE_PER_SECOND;
4171 76 : peer->m_addr_token_bucket = std::min<double>(peer->m_addr_token_bucket + increment, MAX_ADDR_PROCESSING_TOKEN_BUCKET);
4172 76 : }
4173 84 : peer->m_addr_token_timestamp = current_time;
4174 :
4175 84 : const bool rate_limited = !pfrom.HasPermission(NetPermissionFlags::Addr);
4176 84 : uint64_t num_proc = 0;
4177 84 : uint64_t num_rate_limit = 0;
4178 84 : Shuffle(vAddr.begin(), vAddr.end(), FastRandomContext());
4179 6634 : for (CAddress& addr : vAddr)
4180 : {
4181 6550 : if (interruptMsgProc)
4182 0 : return;
4183 :
4184 : // Apply rate limiting.
4185 6550 : if (peer->m_addr_token_bucket < 1.0) {
4186 4036 : if (rate_limited) {
4187 3996 : ++num_rate_limit;
4188 3996 : continue;
4189 : }
4190 40 : } else {
4191 2514 : peer->m_addr_token_bucket -= 1.0;
4192 : }
4193 : // We only bother storing full nodes, though this may include
4194 : // things which we would not make an outbound connection to, in
4195 : // part because we may make feeler connections to them.
4196 2554 : if (!MayHaveUsefulAddressDB(addr.nServices) && !HasAllDesirableServiceFlags(addr.nServices))
4197 0 : continue;
4198 :
4199 2554 : if (addr.nTime <= NodeSeconds{100000000s} || addr.nTime > current_a_time + 10min) {
4200 6 : addr.nTime = current_a_time - 5 * 24h;
4201 6 : }
4202 2554 : AddAddressKnown(*peer, addr);
4203 2554 : if (m_banman && (m_banman->IsDiscouraged(addr) || m_banman->IsBanned(addr))) {
4204 : // Do not process banned/discouraged addresses beyond remembering we received them
4205 0 : continue;
4206 : }
4207 2554 : ++num_proc;
4208 2554 : const bool reachable{g_reachable_nets.Contains(addr)};
4209 2654 : if (addr.nTime > current_a_time - 10min && !peer->m_getaddr_sent && vAddr.size() <= 10 && addr.IsRoutable()) {
4210 : // Relay to a limited number of other nodes
4211 100 : RelayAddress(pfrom.GetId(), addr, reachable);
4212 100 : }
4213 : // Do not store addresses outside our network
4214 2554 : if (reachable) {
4215 2550 : vAddrOk.push_back(addr);
4216 2550 : }
4217 : }
4218 84 : peer->m_addr_processed += num_proc;
4219 84 : peer->m_addr_rate_limited += num_rate_limit;
4220 84 : LogPrint(BCLog::NET, "Received addr: %u addresses (%u processed, %u rate-limited) from peer=%d\n",
4221 : vAddr.size(), num_proc, num_rate_limit, pfrom.GetId());
4222 :
4223 84 : m_addrman.Add(vAddrOk, pfrom.addr, 2h);
4224 84 : if (vAddr.size() < 1000) peer->m_getaddr_sent = false;
4225 :
4226 : // AddrFetch: Require multiple addresses to avoid disconnecting on self-announcements
4227 84 : if (pfrom.IsAddrFetchConn() && vAddr.size() > 1) {
4228 2 : LogPrint(BCLog::NET_NETCONN, "addrfetch connection completed peer=%d; disconnecting\n", pfrom.GetId());
4229 2 : pfrom.fDisconnect = true;
4230 2 : }
4231 84 : return;
4232 110 : }
4233 :
4234 815338 : if (msg_type == NetMsgType::SENDDSQUEUE)
4235 : {
4236 : bool b;
4237 7437 : vRecv >> b;
4238 7437 : if (!b) {
4239 0 : peer->m_wants_dsq = Peer::WantsDSQ::NONE;
4240 7437 : } else if (pfrom.GetCommonVersion() < DSQ_INV_VERSION) {
4241 0 : peer->m_wants_dsq = Peer::WantsDSQ::ALL;
4242 0 : } else {
4243 7437 : peer->m_wants_dsq = Peer::WantsDSQ::INV;
4244 : }
4245 7437 : return;
4246 : }
4247 :
4248 :
4249 807901 : if (msg_type == NetMsgType::QSENDRECSIGS) {
4250 : bool b;
4251 3056 : vRecv >> b;
4252 3056 : peer->m_wants_recsigs = b;
4253 3056 : return;
4254 : }
4255 :
4256 804845 : if (msg_type == NetMsgType::INV) {
4257 126925 : std::vector<CInv> vInv;
4258 126925 : vRecv >> vInv;
4259 126925 : if (vInv.size() > MAX_INV_SZ)
4260 : {
4261 2 : Misbehaving(pfrom.GetId(), 20, strprintf("inv message size = %u", vInv.size()));
4262 2 : return;
4263 : }
4264 :
4265 126923 : const bool reject_tx_invs{RejectIncomingTxs(pfrom)};
4266 :
4267 126923 : LOCK(cs_main);
4268 :
4269 126923 : const auto current_time{GetTime<std::chrono::microseconds>()};
4270 126923 : uint256* best_block{nullptr};
4271 :
4272 314433 : for (CInv& inv : vInv) {
4273 187514 : if(!inv.IsKnownType()) {
4274 0 : LogPrint(BCLog::NET, "got inv of unknown type %d: %s peer=%d\n", inv.type, inv.hash.ToString(), pfrom.GetId());
4275 0 : continue;
4276 : }
4277 :
4278 187514 : if (interruptMsgProc) return;
4279 :
4280 187514 : if (inv.IsMsgBlk()) {
4281 1692 : const bool fAlreadyHave = AlreadyHaveBlock(inv.hash);
4282 1692 : LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
4283 1692 : ::g_stats_client->inc(strprintf("message.received.inv_%s", inv.GetCommand()), 1.0f);
4284 :
4285 1692 : UpdateBlockAvailability(pfrom.GetId(), inv.hash);
4286 1692 : if (!fAlreadyHave && !fImporting && !fReindex && !IsBlockRequested(inv.hash)) {
4287 : // Headers-first is the primary method of announcement on
4288 : // the network. If a node fell back to sending blocks by
4289 : // inv, it may be for a re-org, or because we haven't
4290 : // completed initial headers sync. The final block hash
4291 : // provided should be the highest, so send a getheaders and
4292 : // then fetch the blocks we need to catch up.
4293 110 : best_block = &inv.hash;
4294 110 : }
4295 1692 : } else {
4296 185826 : if (reject_tx_invs && NetMessageViolatesBlocksOnly(inv.GetCommand())) {
4297 4 : LogPrint(BCLog::NET, "%s (%s) inv sent in violation of protocol, disconnecting peer=%d\n", inv.GetCommand(), inv.hash.ToString(), pfrom.GetId());
4298 4 : pfrom.fDisconnect = true;
4299 4 : return;
4300 : }
4301 :
4302 185818 : const bool fAlreadyHave = AlreadyHave(inv);
4303 185818 : LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
4304 185818 : ::g_stats_client->inc(strprintf("message.received.inv_%s", inv.GetCommand()), 1.0f);
4305 :
4306 185818 : static std::set<int> allowWhileInIBDObjs = {
4307 : MSG_SPORK
4308 : };
4309 :
4310 185818 : AddKnownInv(*peer, inv.hash);
4311 185818 : if (!fAlreadyHave) {
4312 77911 : if (reject_tx_invs && inv.type == MSG_ISDLOCK) {
4313 0 : if (pfrom.GetCommonVersion() <= ADDRV2_PROTO_VERSION) {
4314 : // It's ok to receive these invs, we just ignore them
4315 : // and do not request corresponding objects.
4316 0 : continue;
4317 : }
4318 : // Peers with newer versions should never send us these invs when we are in blocks-relay-only mode
4319 0 : LogPrint(BCLog::NET, "%s (%s) inv sent in violation of protocol, disconnecting peer=%d\n", inv.GetCommand(), inv.hash.ToString(), pfrom.GetId());
4320 0 : pfrom.fDisconnect = true;
4321 0 : return;
4322 : }
4323 77911 : bool allowWhileInIBD = allowWhileInIBDObjs.count(inv.type);
4324 77911 : if (allowWhileInIBD || !m_chainman.ActiveChainstate().IsInitialBlockDownload()) {
4325 77909 : RequestObject(pfrom.GetId(), inv, current_time);
4326 77909 : }
4327 77911 : }
4328 : }
4329 : }
4330 126919 : if (best_block != nullptr) {
4331 : // If we haven't started initial headers-sync with this peer, then
4332 : // consider sending a getheaders now. On initial startup, there's a
4333 : // reliability vs bandwidth tradeoff, where we are only trying to do
4334 : // initial headers sync with one peer at a time, with a long
4335 : // timeout (at which point, if the sync hasn't completed, we will
4336 : // disconnect the peer and then choose another). In the meantime,
4337 : // as new blocks are found, we are willing to add one new peer per
4338 : // block to sync with as well, to sync quicker in the case where
4339 : // our initial peer is unresponsive (but less bandwidth than we'd
4340 : // use if we turned on sync with all peers).
4341 110 : CNodeState& state{*Assert(State(pfrom.GetId()))};
4342 110 : if (state.fSyncStarted || (!peer->m_inv_triggered_getheaders_before_sync && *best_block != m_last_block_inv_triggering_headers_sync)) {
4343 70 : std::string msg_type = UsesCompressedHeaders(*peer) ? NetMsgType::GETHEADERS2 : NetMsgType::GETHEADERS;
4344 70 : if (MaybeSendGetHeaders(pfrom, msg_type, m_chainman.ActiveChain().GetLocator(m_chainman.m_best_header), *peer)) {
4345 54 : LogPrint(BCLog::NET, "%s (%d) %s to peer=%d\n",
4346 : msg_type, m_chainman.m_best_header->nHeight, best_block->ToString(),
4347 : pfrom.GetId());
4348 54 : }
4349 70 : if (!state.fSyncStarted) {
4350 9 : peer->m_inv_triggered_getheaders_before_sync = true;
4351 : // Update the last block hash that triggered a new headers
4352 : // sync, so that we don't turn on headers sync with more
4353 : // than 1 new peer every new block.
4354 9 : m_last_block_inv_triggering_headers_sync = *best_block;
4355 9 : }
4356 70 : }
4357 110 : }
4358 :
4359 126919 : return;
4360 126925 : }
4361 :
4362 677920 : if (msg_type == NetMsgType::GETDATA) {
4363 91932 : std::vector<CInv> vInv;
4364 91932 : vRecv >> vInv;
4365 91932 : if (vInv.size() > MAX_INV_SZ)
4366 : {
4367 :
4368 2 : Misbehaving(pfrom.GetId(), 20, strprintf("getdata message size = %u", vInv.size()));
4369 2 : return;
4370 : }
4371 :
4372 91930 : LogPrint(BCLog::NET, "received getdata (%u invsz) peer=%d\n", vInv.size(), pfrom.GetId());
4373 :
4374 91930 : if (vInv.size() > 0) {
4375 91930 : LogPrint(BCLog::NET, "received getdata for: %s peer=%d\n", vInv[0].ToString(), pfrom.GetId());
4376 91930 : }
4377 :
4378 : {
4379 91930 : LOCK(peer->m_getdata_requests_mutex);
4380 91930 : peer->m_getdata_requests.insert(peer->m_getdata_requests.end(), vInv.begin(), vInv.end());
4381 91930 : ProcessGetData(pfrom, *peer, interruptMsgProc);
4382 91930 : }
4383 91930 : return;
4384 91932 : }
4385 :
4386 585988 : if (msg_type == NetMsgType::GETBLOCKS) {
4387 12 : CBlockLocator locator;
4388 12 : uint256 hashStop;
4389 12 : vRecv >> locator >> hashStop;
4390 :
4391 12 : if (locator.vHave.size() > MAX_LOCATOR_SZ) {
4392 2 : LogPrint(BCLog::NET, "getblocks locator size %lld > %d, disconnect peer=%d\n", locator.vHave.size(), MAX_LOCATOR_SZ, pfrom.GetId());
4393 2 : pfrom.fDisconnect = true;
4394 2 : return;
4395 : }
4396 :
4397 : // We might have announced the currently-being-connected tip using a
4398 : // compact block, which resulted in the peer sending a getblocks
4399 : // request, which we would otherwise respond to without the new block.
4400 : // To avoid this situation we simply verify that we are on our best
4401 : // known chain now. This is super overkill, but we handle it better
4402 : // for getheaders requests, and there are no known nodes which support
4403 : // compact blocks but still use getblocks to request blocks.
4404 : {
4405 10 : std::shared_ptr<const CBlock> a_recent_block;
4406 : {
4407 10 : LOCK(m_most_recent_block_mutex);
4408 10 : a_recent_block = m_most_recent_block;
4409 10 : }
4410 10 : BlockValidationState state;
4411 10 : if (!m_chainman.ActiveChainstate().ActivateBestChain(state, a_recent_block)) {
4412 0 : LogPrint(BCLog::NET, "failed to activate chain (%s)\n", state.ToString());
4413 0 : }
4414 10 : }
4415 :
4416 10 : LOCK(cs_main);
4417 :
4418 : // Find the last block the caller has in the main chain
4419 10 : const CBlockIndex* pindex = m_chainman.ActiveChainstate().FindForkInGlobalIndex(locator);
4420 :
4421 : // Send the rest of the chain
4422 10 : if (pindex)
4423 10 : pindex = m_chainman.ActiveChain().Next(pindex);
4424 10 : int nLimit = 500;
4425 10 : LogPrint(BCLog::NET, "getblocks %d to %s limit %d from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), nLimit, pfrom.GetId());
4426 84 : for (; pindex; pindex = m_chainman.ActiveChain().Next(pindex))
4427 : {
4428 74 : if (pindex->GetBlockHash() == hashStop)
4429 : {
4430 0 : LogPrint(BCLog::NET, " getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
4431 0 : break;
4432 : }
4433 : // If pruning, don't inv blocks unless we have on disk and are likely to still have
4434 : // for some reasonable time window (1 hour) that block relay might require.
4435 74 : const int nPrunedBlocksLikelyToHave = MIN_BLOCKS_TO_KEEP - 3600 / m_chainparams.GetConsensus().nPowTargetSpacing;
4436 74 : if (fPruneMode && (!(pindex->nStatus & BLOCK_HAVE_DATA) || pindex->nHeight <= m_chainman.ActiveChain().Tip()->nHeight - nPrunedBlocksLikelyToHave))
4437 : {
4438 0 : LogPrint(BCLog::NET, " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
4439 0 : break;
4440 : }
4441 74 : if (pfrom.CanRelay()) {
4442 148 : WITH_LOCK(peer->m_block_inv_mutex, peer->m_blocks_for_inv_relay.push_back(pindex->GetBlockHash()));
4443 74 : }
4444 74 : if (--nLimit <= 0) {
4445 : // When this block is requested, we'll send an inv that'll
4446 : // trigger the peer to getblocks the next batch of inventory.
4447 0 : LogPrint(BCLog::NET, " getblocks stopping at limit %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
4448 0 : WITH_LOCK(peer->m_block_inv_mutex, {peer->m_continuation_block = pindex->GetBlockHash();});
4449 0 : break;
4450 : }
4451 74 : }
4452 : return;
4453 12 : }
4454 :
4455 585976 : if (msg_type == NetMsgType::GETBLOCKTXN) {
4456 29138 : BlockTransactionsRequest req;
4457 29138 : vRecv >> req;
4458 :
4459 29138 : std::shared_ptr<const CBlock> recent_block;
4460 : {
4461 29138 : LOCK(m_most_recent_block_mutex);
4462 29138 : if (m_most_recent_block_hash == req.blockhash)
4463 16760 : recent_block = m_most_recent_block;
4464 : // Unlock m_most_recent_block_mutex to avoid cs_main lock inversion
4465 29138 : }
4466 29138 : if (recent_block) {
4467 16760 : SendBlockTransactions(pfrom, *recent_block, req);
4468 16760 : return;
4469 : }
4470 :
4471 : {
4472 12378 : LOCK(cs_main);
4473 :
4474 12378 : const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(req.blockhash);
4475 12378 : if (!pindex || !(pindex->nStatus & BLOCK_HAVE_DATA)) {
4476 2 : LogPrint(BCLog::NET, "Peer %d sent us a getblocktxn for a block we don't have\n", pfrom.GetId());
4477 2 : return;
4478 : }
4479 :
4480 12376 : if (pindex->nHeight >= m_chainman.ActiveChain().Height() - MAX_BLOCKTXN_DEPTH) {
4481 12366 : CBlock block;
4482 12366 : bool ret = ReadBlockFromDisk(block, pindex, m_chainparams.GetConsensus());
4483 12366 : assert(ret);
4484 :
4485 12366 : SendBlockTransactions(pfrom, block, req);
4486 : return;
4487 12366 : }
4488 12378 : }
4489 :
4490 : // If an older block is requested (should never happen in practice,
4491 : // but can happen in tests) send a block response instead of a
4492 : // blocktxn response. Sending a full block response instead of a
4493 : // small blocktxn response is preferable in the case where a peer
4494 : // might maliciously send lots of getblocktxn requests to trigger
4495 : // expensive disk reads, because it will require the peer to
4496 : // actually receive all the data read from disk over the network.
4497 10 : LogPrint(BCLog::NET, "Peer %d sent us a getblocktxn for a block > %i deep\n", pfrom.GetId(), MAX_BLOCKTXN_DEPTH);
4498 10 : CInv inv{MSG_BLOCK, req.blockhash};
4499 20 : WITH_LOCK(peer->m_getdata_requests_mutex, peer->m_getdata_requests.push_back(inv));
4500 : // The message processing loop will go around again (without pausing) and we'll respond then (without cs_main)
4501 10 : return;
4502 29138 : }
4503 :
4504 556838 : if (msg_type == NetMsgType::GETHEADERS || msg_type == NetMsgType::GETHEADERS2) {
4505 7125 : CBlockLocator locator;
4506 7125 : uint256 hashStop;
4507 7125 : vRecv >> locator >> hashStop;
4508 :
4509 7125 : if (locator.vHave.size() > MAX_LOCATOR_SZ) {
4510 2 : LogPrint(BCLog::NET, "%s locator size %lld > %d, disconnect peer=%d\n", msg_type, locator.vHave.size(), MAX_LOCATOR_SZ, pfrom.GetId());
4511 2 : pfrom.fDisconnect = true;
4512 2 : return;
4513 : }
4514 :
4515 7123 : if (fImporting || fReindex) {
4516 0 : LogPrint(BCLog::NET, "Ignoring %s from peer=%d while importing/reindexing\n", msg_type, pfrom.GetId());
4517 0 : return;
4518 : }
4519 :
4520 7123 : LOCK(cs_main);
4521 :
4522 : // Note that if we were to be on a chain that forks from the checkpointed
4523 : // chain, then serving those headers to a peer that has seen the
4524 : // checkpointed chain would cause that peer to disconnect us. Requiring
4525 : // that our chainwork exceed nMinimumChainWork is a protection against
4526 : // being fed a bogus chain when we started up for the first time and
4527 : // getting partitioned off the honest network for serving that chain to
4528 : // others.
4529 7137 : if (m_chainman.ActiveTip() == nullptr ||
4530 7123 : (m_chainman.ActiveTip()->nChainWork < nMinimumChainWork && !pfrom.HasPermission(NetPermissionFlags::Download))) {
4531 14 : LogPrint(BCLog::NET, "Ignoring %s from peer=%d because active chain has too little work; sending empty response\n", msg_type, pfrom.GetId());
4532 : // Just respond with an empty headers message, to tell the peer to
4533 : // go away but not treat us as unresponsive.
4534 14 : std::string ret_type = UsesCompressedHeaders(*peer) ? NetMsgType::HEADERS2 : NetMsgType::HEADERS;
4535 14 : m_connman.PushMessage(&pfrom, msgMaker.Make(ret_type, std::vector<CBlock>()));
4536 : return;
4537 14 : }
4538 :
4539 7109 : CNodeState *nodestate = State(pfrom.GetId());
4540 7109 : const CBlockIndex* pindex = nullptr;
4541 7109 : if (locator.IsNull())
4542 : {
4543 : // If locator is null, return the hashStop block
4544 16 : pindex = m_chainman.m_blockman.LookupBlockIndex(hashStop);
4545 16 : if (!pindex) {
4546 0 : return;
4547 : }
4548 :
4549 16 : if (!BlockRequestAllowed(pindex)) {
4550 6 : LogPrint(BCLog::NET, "%s: ignoring request from peer=%i for old block header that isn't in the main chain\n", __func__, pfrom.GetId());
4551 6 : return;
4552 : }
4553 10 : }
4554 : else
4555 : {
4556 : // Find the last block the caller has in the main chain
4557 7093 : pindex = m_chainman.ActiveChainstate().FindForkInGlobalIndex(locator);
4558 7093 : if (pindex)
4559 7093 : pindex = m_chainman.ActiveChain().Next(pindex);
4560 : }
4561 :
4562 14206 : const auto send_headers = [this /* for m_connman */, &hashStop, &pindex, &nodestate, &pfrom, &msgMaker](auto msg_type_internal, auto& v_headers, auto callback) {
4563 7103 : int nLimit = GetHeadersLimit(pfrom, msg_type_internal == NetMsgType::HEADERS2);
4564 63086 : for (; pindex; pindex = m_chainman.ActiveChain().Next(pindex)) {
4565 56005 : v_headers.emplace_back(callback(pindex));
4566 :
4567 56005 : if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
4568 22 : break;
4569 55983 : }
4570 : // pindex can be nullptr either if we sent m_chainman.ActiveChain().Tip() OR
4571 : // if our peer has m_chainman.ActiveChain().Tip() (and thus we are sending an empty
4572 : // headers message). In both cases it's safe to update
4573 : // pindexBestHeaderSent to be our tip.
4574 : //
4575 : // It is important that we simply reset the BestHeaderSent value here,
4576 : // and not max(BestHeaderSent, newHeaderSent). We might have announced
4577 : // the currently-being-connected tip using a compact block, which
4578 : // resulted in the peer sending a headers request, which we respond to
4579 : // without the new block. By resetting the BestHeaderSent, we ensure we
4580 : // will re-announce the new block via headers (or compact blocks again)
4581 : // in the SendMessages logic.
4582 7103 : nodestate->pindexBestHeaderSent = pindex ? pindex : m_chainman.ActiveChain().Tip();
4583 7103 : m_connman.PushMessage(&pfrom, msgMaker.Make(msg_type_internal, v_headers));
4584 7103 : };
4585 :
4586 7103 : LogPrint(BCLog::NET, "%s %d to %s from peer=%d\n", msg_type, (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), pfrom.GetId());
4587 7103 : if (msg_type == NetMsgType::GETHEADERS) {
4588 : // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
4589 48 : std::vector<CBlock> v_headers;
4590 228 : send_headers(NetMsgType::HEADERS, v_headers, [](const auto block_pindex) { return block_pindex->GetBlockHeader(); });
4591 7103 : } else if (msg_type == NetMsgType::GETHEADERS2) {
4592 : // Keeps track of the last 7 unique version blocks
4593 7055 : std::list<int32_t> last_unique_versions;
4594 7055 : std::vector<CompressibleBlockHeader> v_headers;
4595 :
4596 62880 : send_headers(NetMsgType::HEADERS2, v_headers, [&v_headers, &last_unique_versions](const auto block_pindex) {
4597 55825 : CompressibleBlockHeader compressible_header{block_pindex->GetBlockHeader()};
4598 55825 : if (!v_headers.empty()) compressible_header.Compress(v_headers, last_unique_versions); // first block is always uncompressed
4599 55825 : return compressible_header;
4600 : });
4601 7055 : }
4602 7103 : return;
4603 7125 : }
4604 :
4605 549713 : if (msg_type == NetMsgType::TX || msg_type == NetMsgType::DSTX) {
4606 : // Stop processing the transaction early if we are still in IBD since we don't
4607 : // have enough information to validate it yet. Sending unsolicited transactions
4608 : // is not considered a protocol violation, so don't punish the peer.
4609 21290 : if (m_chainman.ActiveChainstate().IsInitialBlockDownload()) return;
4610 :
4611 21288 : CTransactionRef ptx;
4612 21288 : CCoinJoinBroadcastTx dstx;
4613 21288 : int nInvType = MSG_TX;
4614 :
4615 : // Read data and assign inv type
4616 21288 : if(msg_type == NetMsgType::TX) {
4617 21084 : vRecv >> ptx;
4618 21288 : } else if (msg_type == NetMsgType::DSTX) {
4619 204 : vRecv >> dstx;
4620 204 : ptx = dstx.tx;
4621 204 : nInvType = MSG_DSTX;
4622 204 : }
4623 21288 : const CTransaction& tx = *ptx;
4624 :
4625 21288 : const uint256& txid = ptx->GetHash();
4626 21288 : AddKnownInv(*peer, txid);
4627 :
4628 21288 : CInv inv(nInvType, tx.GetHash());
4629 : {
4630 21288 : LOCK(cs_main);
4631 21288 : EraseObjectRequest(pfrom.GetId(), inv);
4632 21288 : }
4633 :
4634 : // Process custom logic, no matter if tx will be accepted to mempool later or not
4635 21288 : if (nInvType == MSG_DSTX) {
4636 204 : uint256 hashTx = tx.GetHash();
4637 204 : const auto result = ValidateDSTX(*m_dmnman, m_dstxman, m_chainman, m_mn_metaman, m_mempool, dstx, hashTx);
4638 204 : if (result.do_return) {
4639 204 : if (result.score != DSTXValidationScore::NONE) {
4640 204 : Misbehaving(pfrom.GetId(), static_cast<int>(result.score), "invalid dstx");
4641 204 : }
4642 204 : return;
4643 : }
4644 0 : }
4645 :
4646 21084 : LOCK(cs_main);
4647 :
4648 21084 : if (AlreadyHave(inv)) {
4649 98 : if (pfrom.HasPermission(NetPermissionFlags::ForceRelay)) {
4650 : // Always relay transactions received from peers with forcerelay permission, even
4651 : // if they were already in the mempool,
4652 : // allowing the node to function as a gateway for
4653 : // nodes hidden behind it.
4654 4 : if (!m_mempool.exists(tx.GetHash())) {
4655 2 : LogPrintf("Not relaying non-mempool transaction %s from forcerelay peer=%d\n", tx.GetHash().ToString(), pfrom.GetId());
4656 2 : } else {
4657 2 : LogPrintf("Force relaying tx %s from peer=%d\n", tx.GetHash().ToString(), pfrom.GetId());
4658 2 : _RelayTransaction(tx.GetHash());
4659 : }
4660 4 : }
4661 98 : return;
4662 : }
4663 :
4664 20986 : const MempoolAcceptResult result = m_chainman.ProcessTransaction(ptx);
4665 20986 : const TxValidationState& state = result.m_state;
4666 :
4667 20986 : if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
4668 : // Process custom txes, this changes AlreadyHave to "true"
4669 19016 : if (nInvType == MSG_DSTX) {
4670 0 : LogPrint(BCLog::COINJOIN, "DSTX -- Masternode transaction accepted, txid=%s, peer=%d\n",
4671 : tx.GetHash().ToString(), pfrom.GetId());
4672 0 : m_dstxman.AddDSTX(dstx);
4673 0 : }
4674 :
4675 19016 : _RelayTransaction(tx.GetHash());
4676 19016 : m_orphanage.AddChildrenToWorkSet(tx, peer->m_id);
4677 :
4678 19016 : pfrom.m_last_tx_time = GetTime<std::chrono::seconds>();
4679 :
4680 19016 : LogPrint(BCLog::MEMPOOL, "AcceptToMemoryPool: peer=%d: accepted %s (poolsz %u txn, %u kB)\n",
4681 : pfrom.GetId(),
4682 : tx.GetHash().ToString(),
4683 : m_mempool.size(), m_mempool.DynamicMemoryUsage() / 1000);
4684 :
4685 : // Recursively process any orphan transactions that depended on this one
4686 19016 : ProcessOrphanTx(peer->m_id);
4687 19016 : }
4688 1970 : else if (state.GetResult() == TxValidationResult::TX_MISSING_INPUTS)
4689 : {
4690 1876 : bool fRejectedParents = false; // It may be the case that the orphans parents have all been rejected
4691 :
4692 : // Deduplicate parent txids, so that we don't have to loop over
4693 : // the same parent txid more than once down below.
4694 1876 : std::vector<uint256> unique_parents;
4695 1876 : unique_parents.reserve(tx.vin.size());
4696 3756 : for (const CTxIn& txin : tx.vin) {
4697 : // We start with all parents, and then remove duplicates below.
4698 1880 : unique_parents.push_back(txin.prevout.hash);
4699 : }
4700 1876 : std::sort(unique_parents.begin(), unique_parents.end());
4701 1876 : unique_parents.erase(std::unique(unique_parents.begin(), unique_parents.end()), unique_parents.end());
4702 3732 : for (const uint256& parent_txid : unique_parents) {
4703 1880 : if (m_recent_rejects.contains(parent_txid)) {
4704 24 : fRejectedParents = true;
4705 24 : break;
4706 : }
4707 : }
4708 1876 : if (!fRejectedParents) {
4709 1852 : const auto current_time{GetTime<std::chrono::microseconds>()};
4710 :
4711 3708 : for (const uint256& parent_txid : unique_parents) {
4712 1856 : CInv _inv(MSG_TX, parent_txid);
4713 1856 : AddKnownInv(*peer, _inv.hash);
4714 1856 : if (!AlreadyHave(_inv)) RequestObject(pfrom.GetId(), _inv, current_time);
4715 : // We don't know if the previous tx was a regular or a mixing one, try both
4716 1856 : CInv _inv2(MSG_DSTX, parent_txid);
4717 1856 : AddKnownInv(*peer, _inv2.hash);
4718 1856 : if (!AlreadyHave(_inv2)) RequestObject(pfrom.GetId(), _inv2, current_time);
4719 : }
4720 :
4721 1852 : if (m_orphanage.AddTx(ptx, pfrom.GetId())) {
4722 1852 : AddToCompactExtraTransactions(ptx);
4723 1852 : }
4724 :
4725 : // DoS prevention: do not allow m_orphans to grow unbounded (see CVE-2012-3789)
4726 1852 : unsigned int nMaxOrphanTxSize = (unsigned int)std::max((int64_t)0, gArgs.GetIntArg("-maxorphantxsize", DEFAULT_MAX_ORPHAN_TRANSACTIONS_SIZE)) * 1000000;
4727 1852 : m_orphanage.LimitOrphans(nMaxOrphanTxSize);
4728 1852 : } else {
4729 24 : LogPrint(BCLog::MEMPOOL, "not keeping orphan with rejected parents %s\n",tx.GetHash().ToString());
4730 : // We will continue to reject this tx since it has rejected
4731 : // parents so avoid re-requesting it from other peers.
4732 24 : m_recent_rejects.insert(tx.GetHash());
4733 24 : m_llmq_ctx->isman->TransactionIsRemoved(ptx);
4734 : }
4735 1876 : } else {
4736 94 : m_recent_rejects.insert(tx.GetHash());
4737 94 : if (RecursiveDynamicUsage(*ptx) < 100000) {
4738 94 : AddToCompactExtraTransactions(ptx);
4739 94 : }
4740 : }
4741 :
4742 : // If a tx has been detected by m_recent_rejects, we will have reached
4743 : // this point and the tx will have been ignored. Because we haven't
4744 : // submitted the tx to our mempool, we won't have computed a DoS
4745 : // score for it or determined exactly why we consider it invalid.
4746 : //
4747 : // This means we won't penalize any peer subsequently relaying a DoSy
4748 : // tx (even if we penalized the first peer who gave it to us) because
4749 : // we have to account for m_recent_rejects showing false positives. In
4750 : // other words, we shouldn't penalize a peer if we aren't *sure* they
4751 : // submitted a DoSy tx.
4752 : //
4753 : // Note that m_recent_rejects doesn't just record DoSy or invalid
4754 : // transactions, but any tx not accepted by the m_mempool, which may be
4755 : // due to node policy (vs. consensus). So we can't blanket penalize a
4756 : // peer simply for relaying a tx that our m_recent_rejects has caught,
4757 : // regardless of false positives.
4758 :
4759 20986 : if (state.IsInvalid()) {
4760 1970 : LogPrint(BCLog::MEMPOOLREJ, "%s from peer=%d was not accepted: %s\n", tx.GetHash().ToString(),
4761 : pfrom.GetId(),
4762 : state.ToString());
4763 1970 : MaybePunishNodeForTx(pfrom.GetId(), state);
4764 1970 : m_llmq_ctx->isman->TransactionIsRemoved(ptx);
4765 1970 : }
4766 : return;
4767 21288 : }
4768 :
4769 528423 : if (msg_type == NetMsgType::CMPCTBLOCK)
4770 : {
4771 : // Ignore cmpctblock received while importing
4772 120701 : if (fImporting || fReindex) {
4773 0 : LogPrint(BCLog::NET, "Unexpected cmpctblock message received from peer %d\n", pfrom.GetId());
4774 0 : return;
4775 : }
4776 :
4777 120701 : CBlockHeaderAndShortTxIDs cmpctblock;
4778 120701 : vRecv >> cmpctblock;
4779 :
4780 120701 : bool received_new_header = false;
4781 120701 : const auto blockhash = cmpctblock.header.GetHash();
4782 :
4783 : {
4784 120701 : LOCK(cs_main);
4785 :
4786 120701 : if (!m_chainman.m_blockman.LookupBlockIndex(cmpctblock.header.hashPrevBlock)) {
4787 : // Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
4788 286 : if (!m_chainman.ActiveChainstate().IsInitialBlockDownload()) {
4789 286 : std::string ret_val = UsesCompressedHeaders(*peer) ? NetMsgType::GETHEADERS2 : NetMsgType::GETHEADERS;
4790 286 : MaybeSendGetHeaders(pfrom, ret_val, m_chainman.ActiveChain().GetLocator(m_chainman.m_best_header), *peer);
4791 286 : }
4792 286 : return;
4793 : }
4794 :
4795 120415 : if (!m_chainman.m_blockman.LookupBlockIndex(blockhash)) {
4796 112236 : received_new_header = true;
4797 112236 : }
4798 120701 : }
4799 :
4800 121441 : const CBlockIndex *pindex = nullptr;
4801 121441 : BlockValidationState state;
4802 121441 : if (!m_chainman.ProcessNewBlockHeaders({cmpctblock.header}, state, &pindex)) {
4803 52 : if (state.IsInvalid()) {
4804 52 : MaybePunishNodeForBlock(pfrom.GetId(), state, /*via_compact_block=*/true, "invalid header via cmpctblock");
4805 52 : return;
4806 : }
4807 0 : }
4808 :
4809 120363 : if (received_new_header) {
4810 112232 : LogInfo("Saw new cmpctblock header hash=%s peer=%d\n",
4811 : blockhash.ToString(), pfrom.GetId());
4812 112232 : }
4813 :
4814 : // When we succeed in decoding a block's txids from a cmpctblock
4815 : // message we typically jump to the BLOCKTXN handling code, with a
4816 : // dummy (empty) BLOCKTXN message, to re-use the logic there in
4817 : // completing processing of the putative block (without cs_main).
4818 120363 : bool fProcessBLOCKTXN = false;
4819 120363 : CDataStream blockTxnMsg(SER_NETWORK, PROTOCOL_VERSION);
4820 :
4821 : // If we end up treating this as a plain headers message, call that as well
4822 : // without cs_main.
4823 121389 : bool fRevertToHeaderProcessing = false;
4824 :
4825 : // Keep a CBlock for "optimistic" compactblock reconstructions (see
4826 : // below)
4827 121389 : std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
4828 121389 : bool fBlockReconstructed = false;
4829 :
4830 : {
4831 121389 : LOCK(cs_main);
4832 : // If AcceptBlockHeader returned true, it set pindex
4833 121389 : assert(pindex);
4834 121389 : UpdateBlockAvailability(pfrom.GetId(), pindex->GetBlockHash());
4835 :
4836 120363 : CNodeState *nodestate = State(pfrom.GetId());
4837 :
4838 : // If this was a new header with more work than our tip, update the
4839 : // peer's last block announcement time
4840 120363 : if (received_new_header && pindex->nChainWork > m_chainman.ActiveChain().Tip()->nChainWork) {
4841 111648 : nodestate->m_last_block_announcement = GetTime();
4842 111648 : }
4843 :
4844 120363 : std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator blockInFlightIt = mapBlocksInFlight.find(pindex->GetBlockHash());
4845 120363 : bool fAlreadyInFlight = blockInFlightIt != mapBlocksInFlight.end();
4846 :
4847 120363 : if (pindex->nStatus & BLOCK_HAVE_DATA) // Nothing to do here
4848 6009 : return;
4849 :
4850 114354 : if (pindex->nChainWork <= m_chainman.ActiveChain().Tip()->nChainWork || // We know something better
4851 113755 : pindex->nTx != 0) { // We had this block at some point, but pruned it
4852 599 : if (fAlreadyInFlight) {
4853 : // We requested this block for some reason, but our mempool will probably be useless
4854 : // so we just grab the block via normal getdata
4855 15 : std::vector<CInv> vInv(1);
4856 15 : vInv[0] = CInv(MSG_BLOCK, blockhash);
4857 15 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
4858 15 : }
4859 599 : return;
4860 : }
4861 :
4862 : // If we're not close to tip yet, give up and let parallel block fetch work its magic
4863 113755 : if (!fAlreadyInFlight && !CanDirectFetch())
4864 85 : return;
4865 :
4866 : // We want to be a bit conservative just to be extra careful about DoS
4867 : // possibilities in compact block processing...
4868 113670 : if (pindex->nHeight <= m_chainman.ActiveChain().Height() + 2) {
4869 99968 : if ((!fAlreadyInFlight && nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) ||
4870 2085 : (fAlreadyInFlight && blockInFlightIt->second.first == pfrom.GetId())) {
4871 97883 : std::list<QueuedBlock>::iterator *queuedBlockIt = nullptr;
4872 97883 : if (!BlockRequested(pfrom.GetId(), *pindex, &queuedBlockIt)) {
4873 1574 : if (!(*queuedBlockIt)->partialBlock)
4874 1574 : (*queuedBlockIt)->partialBlock.reset(new PartiallyDownloadedBlock(&m_mempool));
4875 : else {
4876 : // The block was already in flight using compact blocks from the same peer
4877 0 : LogPrint(BCLog::NET, "Peer sent us compact block we were already syncing!\n");
4878 0 : return;
4879 : }
4880 1574 : }
4881 :
4882 97370 : PartiallyDownloadedBlock& partialBlock = *(*queuedBlockIt)->partialBlock;
4883 97370 : ReadStatus status = partialBlock.InitData(cmpctblock, vExtraTxnForCompact);
4884 97370 : if (status == READ_STATUS_INVALID) {
4885 2 : RemoveBlockRequest(pindex->GetBlockHash(), pfrom.GetId()); // Reset in-flight state in case Misbehaving does not result in a disconnect
4886 2 : Misbehaving(pfrom.GetId(), 100, "invalid compact block");
4887 2 : return;
4888 97368 : } else if (status == READ_STATUS_FAILED) {
4889 : // Duplicate txindexes, the block is now in-flight, so just request it
4890 0 : std::vector<CInv> vInv(1);
4891 0 : vInv[0] = CInv(MSG_BLOCK, blockhash);
4892 0 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
4893 : return;
4894 0 : }
4895 :
4896 97368 : BlockTransactionsRequest req;
4897 296685 : for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
4898 199317 : if (!partialBlock.IsTxAvailable(i))
4899 83576 : req.indexes.push_back(i);
4900 199317 : }
4901 97368 : if (req.indexes.empty()) {
4902 : // Dirty hack to jump to BLOCKTXN code (TODO: move message handling into their own functions)
4903 68112 : BlockTransactions txn;
4904 68112 : txn.blockhash = blockhash;
4905 68112 : blockTxnMsg << txn;
4906 68112 : fProcessBLOCKTXN = true;
4907 68112 : } else {
4908 29256 : req.blockhash = pindex->GetBlockHash();
4909 29256 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETBLOCKTXN, req));
4910 : }
4911 97368 : } else {
4912 : // This block is either already in flight from a different
4913 : // peer, or this peer has too many blocks outstanding to
4914 : // download from.
4915 : // Optimistically try to reconstruct anyway since we might be
4916 : // able to without any round trips.
4917 0 : PartiallyDownloadedBlock tempBlock(&m_mempool);
4918 513 : ReadStatus status = tempBlock.InitData(cmpctblock, vExtraTxnForCompact);
4919 513 : if (status != READ_STATUS_OK) {
4920 : // TODO: don't ignore failures
4921 2 : return;
4922 : }
4923 511 : std::vector<CTransactionRef> dummy;
4924 511 : status = tempBlock.FillBlock(*pblock, dummy);
4925 511 : if (status == READ_STATUS_OK) {
4926 346 : fBlockReconstructed = true;
4927 346 : }
4928 513 : }
4929 97879 : } else {
4930 15787 : if (fAlreadyInFlight) {
4931 : // We requested this block, but its far into the future, so our
4932 : // mempool will probably be useless - request the block normally
4933 22 : std::vector<CInv> vInv(1);
4934 22 : vInv[0] = CInv(MSG_BLOCK, blockhash);
4935 22 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
4936 : return;
4937 22 : } else {
4938 : // If this was an announce-cmpctblock, we want the same treatment as a header message
4939 15765 : fRevertToHeaderProcessing = true;
4940 : }
4941 : }
4942 120363 : } // cs_main
4943 :
4944 113644 : if (fProcessBLOCKTXN)
4945 68112 : return ProcessMessage(pfrom, NetMsgType::BLOCKTXN, blockTxnMsg, time_received, interruptMsgProc);
4946 :
4947 45532 : if (fRevertToHeaderProcessing) {
4948 : // Headers received from HB compact block peers are permitted to be
4949 : // relayed before full validation (see BIP 152), so we don't want to disconnect
4950 : // the peer if the header turns out to be for an invalid block.
4951 : // Note that if a peer tries to build on an invalid chain, that
4952 : // will be detected and the peer will be disconnected/discouraged.
4953 15765 : return ProcessHeadersMessage(pfrom, *peer, {cmpctblock.header}, /*via_compact_block=*/true);
4954 : }
4955 :
4956 29767 : if (fBlockReconstructed) {
4957 : // If we got here, we were able to optimistically reconstruct a
4958 : // block that is in flight from some other peer.
4959 : {
4960 346 : LOCK(cs_main);
4961 346 : mapBlockSource.emplace(pblock->GetHash(), std::make_pair(pfrom.GetId(), false));
4962 346 : }
4963 : // Setting force_processing to true means that we bypass some of
4964 : // our anti-DoS protections in AcceptBlock, which filters
4965 : // unrequested blocks that might be trying to waste our resources
4966 : // (eg disk space). Because we only try to reconstruct blocks when
4967 : // we're close to caught up (via the CanDirectFetch() requirement
4968 : // above, combined with the behavior of not requesting blocks until
4969 : // we have a chain with at least nMinimumChainWork), and we ignore
4970 : // compact blocks with less work than our tip, it is safe to treat
4971 : // reconstructed compact blocks as having been requested.
4972 346 : ProcessBlock(pfrom, pblock, /*force_processing=*/true);
4973 346 : LOCK(cs_main); // hold cs_main for CBlockIndex::IsValid()
4974 346 : if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS)) {
4975 : // Clear download state for this block, which is in
4976 : // process from some other peer. We do this after calling
4977 : // ProcessNewBlock so that a malleated cmpctblock announcement
4978 : // can't be used to interfere with block relay.
4979 346 : RemoveBlockRequest(pblock->GetHash(), std::nullopt);
4980 346 : }
4981 346 : }
4982 29767 : return;
4983 121155 : }
4984 :
4985 407722 : if (msg_type == NetMsgType::BLOCKTXN)
4986 : {
4987 : // Ignore blocktxn received while importing
4988 97353 : if (fImporting || fReindex) {
4989 0 : LogPrint(BCLog::NET, "Unexpected blocktxn message received from peer %d\n", pfrom.GetId());
4990 0 : return;
4991 : }
4992 :
4993 97353 : BlockTransactions resp;
4994 97353 : vRecv >> resp;
4995 :
4996 97353 : std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
4997 97353 : bool fBlockRead = false;
4998 : {
4999 97353 : LOCK(cs_main);
5000 :
5001 97353 : std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator it = mapBlocksInFlight.find(resp.blockhash);
5002 194703 : if (it == mapBlocksInFlight.end() || !it->second.second->partialBlock ||
5003 97350 : it->second.first != pfrom.GetId()) {
5004 3 : LogPrint(BCLog::NET, "Peer %d sent us block transactions for block we weren't expecting\n", pfrom.GetId());
5005 3 : return;
5006 : }
5007 :
5008 97350 : PartiallyDownloadedBlock& partialBlock = *it->second.second->partialBlock;
5009 97350 : ReadStatus status = partialBlock.FillBlock(*pblock, resp.txn);
5010 97350 : if (status == READ_STATUS_INVALID) {
5011 0 : RemoveBlockRequest(resp.blockhash, pfrom.GetId()); // Reset in-flight state in case Misbehaving does not result in a disconnect
5012 0 : Misbehaving(pfrom.GetId(), 100, "invalid compact block/non-matching block transactions");
5013 0 : return;
5014 97350 : } else if (status == READ_STATUS_FAILED) {
5015 : // Might have collided, fall back to getdata now :(
5016 2 : std::vector<CInv> invs;
5017 2 : invs.push_back(CInv(MSG_BLOCK, resp.blockhash));
5018 2 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, invs));
5019 2 : } else {
5020 : // Block is either okay, or possibly we received
5021 : // READ_STATUS_CHECKBLOCK_FAILED.
5022 : // Note that CheckBlock can only fail for one of a few reasons:
5023 : // 1. bad-proof-of-work (impossible here, because we've already
5024 : // accepted the header)
5025 : // 2. merkleroot doesn't match the transactions given (already
5026 : // caught in FillBlock with READ_STATUS_FAILED, so
5027 : // impossible here)
5028 : // 3. the block is otherwise invalid (eg invalid coinbase,
5029 : // block is too big, too many legacy sigops, etc).
5030 : // So if CheckBlock failed, #3 is the only possibility.
5031 : // Under BIP 152, we don't discourage the peer unless proof of work is
5032 : // invalid (we don't require all the stateless checks to have
5033 : // been run). This is handled below, so just treat this as
5034 : // though the block was successfully read, and rely on the
5035 : // handling in ProcessNewBlock to ensure the block index is
5036 : // updated, etc.
5037 97348 : RemoveBlockRequest(resp.blockhash, pfrom.GetId()); // it is now an empty pointer
5038 97348 : fBlockRead = true;
5039 : // mapBlockSource is used for potentially punishing peers and
5040 : // updating which peers send us compact blocks, so the race
5041 : // between here and cs_main in ProcessNewBlock is fine.
5042 : // BIP 152 permits peers to relay compact blocks after validating
5043 : // the header only; we should not punish peers if the block turns
5044 : // out to be invalid.
5045 97348 : mapBlockSource.emplace(resp.blockhash, std::make_pair(pfrom.GetId(), false));
5046 : }
5047 97353 : } // Don't hold cs_main when we call into ProcessNewBlock
5048 97350 : if (fBlockRead) {
5049 : // Since we requested this block (it was in mapBlocksInFlight), force it to be processed,
5050 : // even if it would not be a candidate for new tip (missing previous block, chain not long enough, etc)
5051 : // This bypasses some anti-DoS logic in AcceptBlock (eg to prevent
5052 : // disk-space attacks), but this should be safe due to the
5053 : // protections in the compact block handler -- see related comment
5054 : // in compact block optimistic reconstruction handling.
5055 97348 : ProcessBlock(pfrom, pblock, /*force_processing=*/true);
5056 97348 : }
5057 97350 : return;
5058 97353 : }
5059 :
5060 310369 : if (msg_type == NetMsgType::HEADERS || msg_type == NetMsgType::HEADERS2) {
5061 : // Ignore headers received while importing
5062 88816 : if (fImporting || fReindex) {
5063 0 : LogPrint(BCLog::NET, "Unexpected headers message received from peer %d\n", pfrom.GetId());
5064 0 : return;
5065 : }
5066 :
5067 : // Assume that this is in response to any outstanding getheaders
5068 : // request we may have sent, and clear out the time of our last request
5069 88816 : peer->m_last_getheaders_timestamp = {};
5070 :
5071 88816 : std::vector<CBlockHeader> headers;
5072 :
5073 : // Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
5074 88816 : unsigned int nCount = ReadCompactSize(vRecv);
5075 88816 : if (nCount > GetHeadersLimit(pfrom, msg_type == NetMsgType::HEADERS2)) {
5076 2 : Misbehaving(pfrom.GetId(), 20, strprintf("headers message size = %u", nCount));
5077 2 : return;
5078 : }
5079 :
5080 88814 : if (msg_type == NetMsgType::HEADERS) {
5081 909 : headers.resize(nCount);
5082 61567 : for (unsigned int n = 0; n < nCount; n++) {
5083 60658 : vRecv >> headers[n];
5084 60658 : ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
5085 60658 : }
5086 88814 : } else if (msg_type == NetMsgType::HEADERS2) {
5087 87905 : std::list<int32_t> last_unique_versions;
5088 267152 : for (unsigned int n = 0; n < nCount; n++) {
5089 179247 : CompressibleBlockHeader block_header_compressed;
5090 179247 : vRecv >> block_header_compressed;
5091 179247 : block_header_compressed.Uncompress(headers, last_unique_versions);
5092 179247 : headers.push_back(block_header_compressed);
5093 179247 : }
5094 87905 : }
5095 :
5096 88814 : return ProcessHeadersMessage(pfrom, *peer, headers, /*via_compact_block=*/false);
5097 88816 : }
5098 :
5099 221553 : if (msg_type == NetMsgType::BLOCK)
5100 : {
5101 : // Ignore block received while importing
5102 58025 : if (fImporting || fReindex) {
5103 0 : LogPrint(BCLog::NET, "Unexpected block message received from peer %d\n", pfrom.GetId());
5104 0 : return;
5105 : }
5106 :
5107 58025 : std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
5108 58025 : vRecv >> *pblock;
5109 :
5110 58022 : LogPrint(BCLog::NET, "received block %s peer=%d\n", pblock->GetHash().ToString(), pfrom.GetId());
5111 :
5112 58022 : bool forceProcessing = false;
5113 58022 : const uint256 hash(pblock->GetHash());
5114 : {
5115 58022 : LOCK(cs_main);
5116 : // Always process the block if we requested it, since we may
5117 : // need it even when it's not a candidate for a new best tip.
5118 58022 : forceProcessing = IsBlockRequested(hash);
5119 58022 : RemoveBlockRequest(hash, pfrom.GetId());
5120 : // mapBlockSource is only used for punishing peers and setting
5121 : // which peers send us compact blocks, so the race between here and
5122 : // cs_main in ProcessNewBlock is fine.
5123 58022 : mapBlockSource.emplace(hash, std::make_pair(pfrom.GetId(), true));
5124 58022 : }
5125 58022 : ProcessBlock(pfrom, pblock, forceProcessing);
5126 : return;
5127 58025 : }
5128 :
5129 163528 : if (msg_type == NetMsgType::GETADDR) {
5130 : // This asymmetric behavior for inbound and outbound connections was introduced
5131 : // to prevent a fingerprinting attack: an attacker can send specific fake addresses
5132 : // to users' AddrMan and later request them by sending getaddr messages.
5133 : // Making nodes which are behind NAT and can only make outgoing connections ignore
5134 : // the getaddr message mitigates the attack.
5135 5010 : if (!pfrom.IsInboundConn()) {
5136 18 : LogPrint(BCLog::NET, "Ignoring \"getaddr\" from %s connection. peer=%d\n", pfrom.ConnectionTypeAsString(), pfrom.GetId());
5137 18 : return;
5138 : }
5139 :
5140 : // Since this must be an inbound connection, SetupAddressRelay will
5141 : // never fail.
5142 4992 : Assume(SetupAddressRelay(pfrom, *peer));
5143 :
5144 : // Only send one GetAddr response per connection to reduce resource waste
5145 : // and discourage addr stamping of INV announcements.
5146 4992 : if (peer->m_getaddr_recvd) {
5147 38 : LogPrint(BCLog::NET, "Ignoring repeated \"getaddr\". peer=%d\n", pfrom.GetId());
5148 38 : return;
5149 : }
5150 4954 : peer->m_getaddr_recvd = true;
5151 :
5152 4954 : peer->m_addrs_to_send.clear();
5153 4954 : std::vector<CAddress> vAddr;
5154 4954 : if (pfrom.HasPermission(NetPermissionFlags::Addr)) {
5155 58 : vAddr = m_connman.GetAddresses(MAX_ADDR_TO_SEND, MAX_PCT_ADDR_TO_SEND, /*network=*/std::nullopt);
5156 58 : } else {
5157 4896 : vAddr = m_connman.GetAddresses(pfrom, MAX_ADDR_TO_SEND, MAX_PCT_ADDR_TO_SEND);
5158 : }
5159 4954 : FastRandomContext insecure_rand;
5160 42778 : for (const CAddress &addr : vAddr) {
5161 37824 : PushAddress(*peer, addr, insecure_rand);
5162 : }
5163 : return;
5164 4954 : }
5165 :
5166 158518 : if (msg_type == NetMsgType::MEMPOOL) {
5167 : // Only process received mempool messages if we advertise NODE_BLOOM
5168 : // or if the peer has mempool permissions.
5169 141 : if (!(peer->m_our_services & NODE_BLOOM) && !pfrom.HasPermission(NetPermissionFlags::Mempool))
5170 : {
5171 2 : if (!pfrom.HasPermission(NetPermissionFlags::NoBan))
5172 : {
5173 2 : LogPrint(BCLog::NET, "mempool request with bloom filters disabled, disconnect peer=%d\n", pfrom.GetId());
5174 2 : pfrom.fDisconnect = true;
5175 2 : }
5176 2 : return;
5177 : }
5178 :
5179 139 : if (m_connman.OutboundTargetReached(false) && !pfrom.HasPermission(NetPermissionFlags::Mempool))
5180 : {
5181 0 : if (!pfrom.HasPermission(NetPermissionFlags::NoBan))
5182 : {
5183 0 : LogPrint(BCLog::NET, "mempool request with bandwidth limit reached, disconnect peer=%d\n", pfrom.GetId());
5184 0 : pfrom.fDisconnect = true;
5185 0 : }
5186 0 : return;
5187 : }
5188 :
5189 139 : if (auto tx_relay = peer->GetTxRelay(); tx_relay != nullptr) {
5190 139 : LOCK(tx_relay->m_tx_inventory_mutex);
5191 139 : tx_relay->m_send_mempool = true;
5192 139 : }
5193 139 : return;
5194 : }
5195 :
5196 158377 : if (msg_type == NetMsgType::PING) {
5197 26756 : uint64_t nonce = 0;
5198 26756 : vRecv >> nonce;
5199 : // Echo the message back with the nonce. This allows for two useful features:
5200 : //
5201 : // 1) A remote node can quickly check if the connection is operational
5202 : // 2) Remote nodes can measure the latency of the network thread. If this node
5203 : // is overloaded it won't respond to pings quickly and the remote node can
5204 : // avoid sending us more work, like chain download requests.
5205 : //
5206 : // The nonce stops the remote getting confused between different pings: without
5207 : // it, if the remote node sends a ping once per second and this node takes 5
5208 : // seconds to respond to each, the 5th ping the remote sends would appear to
5209 : // return very quickly.
5210 26756 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::PONG, nonce));
5211 26756 : return;
5212 : }
5213 :
5214 131621 : if (msg_type == NetMsgType::PONG) {
5215 21884 : const auto ping_end = time_received;
5216 21884 : uint64_t nonce = 0;
5217 21884 : size_t nAvail = vRecv.in_avail();
5218 21884 : bool bPingFinished = false;
5219 21884 : std::string sProblem;
5220 :
5221 21884 : if (nAvail >= sizeof(nonce)) {
5222 21882 : vRecv >> nonce;
5223 :
5224 : // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
5225 21882 : if (peer->m_ping_nonce_sent != 0) {
5226 21880 : if (nonce == peer->m_ping_nonce_sent) {
5227 : // Matching pong received, this ping is no longer outstanding
5228 21874 : bPingFinished = true;
5229 21874 : const auto ping_time = ping_end - peer->m_ping_start.load();
5230 21874 : if (ping_time.count() >= 0) {
5231 : // Let connman know about this successful ping-pong
5232 21874 : pfrom.PongReceived(ping_time);
5233 21874 : } else {
5234 : // This should never happen
5235 0 : sProblem = "Timing mishap";
5236 : }
5237 21874 : } else {
5238 : // Nonce mismatches are normal when pings are overlapping
5239 6 : sProblem = "Nonce mismatch";
5240 6 : if (nonce == 0) {
5241 : // This is most likely a bug in another implementation somewhere; cancel this ping
5242 2 : bPingFinished = true;
5243 2 : sProblem = "Nonce zero";
5244 2 : }
5245 : }
5246 21880 : } else {
5247 2 : sProblem = "Unsolicited pong without ping";
5248 : }
5249 21882 : } else {
5250 : // This is most likely a bug in another implementation somewhere; cancel this ping
5251 2 : bPingFinished = true;
5252 2 : sProblem = "Short payload";
5253 : }
5254 :
5255 21884 : if (!(sProblem.empty())) {
5256 10 : LogPrint(BCLog::NET, "pong peer=%d: %s, %x expected, %x received, %u bytes\n",
5257 : pfrom.GetId(),
5258 : sProblem,
5259 : peer->m_ping_nonce_sent,
5260 : nonce,
5261 : nAvail);
5262 10 : }
5263 21884 : if (bPingFinished) {
5264 21878 : peer->m_ping_nonce_sent = 0;
5265 21878 : }
5266 : return;
5267 21884 : }
5268 :
5269 109737 : if (msg_type == NetMsgType::FILTERLOAD) {
5270 20 : if (!(peer->m_our_services & NODE_BLOOM)) {
5271 2 : LogPrint(BCLog::NET_NETCONN, "filterload received despite not offering bloom services from peer=%d; disconnecting\n", pfrom.GetId());
5272 2 : pfrom.fDisconnect = true;
5273 2 : return;
5274 : }
5275 18 : CBloomFilter filter;
5276 18 : vRecv >> filter;
5277 :
5278 18 : if (!filter.IsWithinSizeConstraints())
5279 : {
5280 : // There is no excuse for sending a too-large filter
5281 4 : Misbehaving(pfrom.GetId(), 100, "too-large bloom filter");
5282 18 : } else if (auto tx_relay = peer->GetTxRelay(); tx_relay != nullptr) {
5283 : {
5284 14 : LOCK(tx_relay->m_bloom_filter_mutex);
5285 14 : tx_relay->m_bloom_filter.reset(new CBloomFilter(filter));
5286 14 : tx_relay->m_relay_txs = true;
5287 14 : }
5288 14 : pfrom.m_bloom_filter_loaded = true;
5289 14 : pfrom.m_relays_txs = true;
5290 14 : }
5291 : return;
5292 18 : }
5293 :
5294 109717 : if (msg_type == NetMsgType::FILTERADD) {
5295 14 : if (!(peer->m_our_services & NODE_BLOOM)) {
5296 2 : LogPrint(BCLog::NET_NETCONN, "filteradd received despite not offering bloom services from peer=%d; disconnecting\n", pfrom.GetId());
5297 2 : pfrom.fDisconnect = true;
5298 2 : return;
5299 : }
5300 12 : std::vector<unsigned char> vData;
5301 12 : vRecv >> vData;
5302 :
5303 : // Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
5304 : // and thus, the maximum size any matched object can have) in a filteradd message
5305 12 : bool bad = false;
5306 12 : if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE) {
5307 2 : bad = true;
5308 12 : } else if (auto tx_relay = peer->GetTxRelay(); tx_relay != nullptr) {
5309 10 : LOCK(tx_relay->m_bloom_filter_mutex);
5310 10 : if (tx_relay->m_bloom_filter) {
5311 6 : tx_relay->m_bloom_filter->insert(vData);
5312 6 : } else {
5313 4 : bad = true;
5314 : }
5315 10 : }
5316 12 : if (bad) {
5317 6 : Misbehaving(pfrom.GetId(), 100, "bad filteradd message");
5318 6 : }
5319 : return;
5320 12 : }
5321 :
5322 109703 : if (msg_type == NetMsgType::FILTERCLEAR) {
5323 10 : if (!(peer->m_our_services & NODE_BLOOM)) {
5324 2 : LogPrint(BCLog::NET_NETCONN, "filterclear received despite not offering bloom services from peer=%d; disconnecting\n", pfrom.GetId());
5325 2 : pfrom.fDisconnect = true;
5326 2 : return;
5327 : }
5328 8 : auto tx_relay = peer->GetTxRelay();
5329 8 : if (!tx_relay) return;
5330 :
5331 : {
5332 8 : LOCK(tx_relay->m_bloom_filter_mutex);
5333 8 : tx_relay->m_bloom_filter = nullptr;
5334 8 : tx_relay->m_relay_txs = true;
5335 8 : }
5336 8 : pfrom.m_bloom_filter_loaded = false;
5337 8 : pfrom.m_relays_txs = true;
5338 8 : return;
5339 : }
5340 :
5341 109693 : if (msg_type == NetMsgType::GETMNLISTDIFF) {
5342 49 : CGetSimplifiedMNListDiff cmd;
5343 49 : vRecv >> cmd;
5344 :
5345 49 : LOCK(cs_main);
5346 :
5347 49 : CSimplifiedMNListDiff mnListDiff;
5348 49 : std::string strError;
5349 98 : if (BuildSimplifiedMNListDiff(*m_dmnman, m_chainman, *m_llmq_ctx->quorum_block_processor, *m_llmq_ctx->qman,
5350 49 : cmd.baseBlockHash, cmd.blockHash, mnListDiff, strError))
5351 : {
5352 49 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::MNLISTDIFF, mnListDiff));
5353 49 : } else {
5354 0 : strError = strprintf("getmnlistdiff failed for baseBlockHash=%s, blockHash=%s. error=%s", cmd.baseBlockHash.ToString(), cmd.blockHash.ToString(), strError);
5355 0 : Misbehaving(pfrom.GetId(), 1, strError);
5356 : }
5357 : return;
5358 49 : }
5359 :
5360 109644 : if (msg_type == NetMsgType::GETCFILTERS) {
5361 8 : ProcessGetCFilters(pfrom, *peer, vRecv);
5362 8 : return;
5363 : }
5364 :
5365 109636 : if (msg_type == NetMsgType::GETCFHEADERS) {
5366 10 : ProcessGetCFHeaders(pfrom, *peer, vRecv);
5367 10 : return;
5368 : }
5369 :
5370 109626 : if (msg_type == NetMsgType::GETCFCHECKPT) {
5371 12 : ProcessGetCFCheckPt(pfrom, *peer, vRecv);
5372 12 : return;
5373 : }
5374 :
5375 :
5376 109614 : if (msg_type == NetMsgType::MNLISTDIFF) {
5377 : // we have never requested this
5378 0 : Misbehaving(pfrom.GetId(), 100, strprintf("received not-requested mnlistdiff. peer=%d", pfrom.GetId()));
5379 0 : return;
5380 : }
5381 :
5382 109614 : if (msg_type == NetMsgType::GETQUORUMROTATIONINFO) {
5383 0 : llmq::CGetQuorumRotationInfo cmd;
5384 0 : vRecv >> cmd;
5385 :
5386 0 : LOCK(cs_main);
5387 :
5388 0 : llmq::CQuorumRotationInfo quorumRotationInfoRet;
5389 0 : std::string strError;
5390 0 : bool use_legacy_construction = pfrom.GetCommonVersion() < EFFICIENT_QRINFO_VERSION;;
5391 0 : if (BuildQuorumRotationInfo(*m_dmnman, *m_llmq_ctx->qsnapman, m_chainman, *m_llmq_ctx->qman, *m_llmq_ctx->quorum_block_processor, cmd, use_legacy_construction, quorumRotationInfoRet, strError)) {
5392 0 : m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::QUORUMROTATIONINFO, quorumRotationInfoRet));
5393 0 : } else {
5394 0 : strError = strprintf("getquorumrotationinfo failed for size(baseBlockHashes)=%d, blockRequestHash=%s. error=%s", cmd.baseBlockHashes.size(), cmd.blockRequestHash.ToString(), strError);
5395 0 : Misbehaving(pfrom.GetId(), 1, strError);
5396 : }
5397 : return;
5398 0 : }
5399 :
5400 109614 : if (msg_type == NetMsgType::SPORK) {
5401 2391 : CSporkMessage spork;
5402 2391 : vRecv >> spork;
5403 :
5404 2391 : uint256 hash = spork.GetHash();
5405 2391 : CInv spork_inv{MSG_SPORK, hash};
5406 4782 : WITH_LOCK(::cs_main, EraseObjectRequest(pfrom.GetId(), spork_inv));
5407 2391 : auto opt_signer = m_sporkman.GetValidSporkSigner(spork);
5408 2391 : if (!opt_signer) {
5409 0 : Misbehaving(pfrom.GetId(), 100, strprintf("invalid spork received. peer=%d", pfrom.GetId()));
5410 0 : return;
5411 : }
5412 2391 : if (m_sporkman.ProcessSpork(spork, *opt_signer, strprintf(" peer=%d", pfrom.GetId()))) {
5413 1982 : RelayInv(spork_inv);
5414 1982 : }
5415 2391 : return;
5416 2391 : }
5417 :
5418 107223 : if (msg_type == NetMsgType::GETSPORKS) {
5419 : // For 'getsporks', active sporks is sent to the requesting peer.
5420 941 : auto active_sporks = m_sporkman.ActiveSporks();
5421 941 : std::vector<uint256> active_spork_hashes;
5422 1433 : for (const auto& pair : active_sporks) {
5423 1170 : for (const auto& spork_pair : pair.second) {
5424 678 : active_spork_hashes.push_back(spork_pair.second.GetHash());
5425 : }
5426 : }
5427 941 : std::sort(active_spork_hashes.begin(), active_spork_hashes.end());
5428 :
5429 : // Ignore repeated requests only while the active spork set is unchanged.
5430 : // Functional tests and some peers request sporks again after a spork
5431 : // update; those requests must receive the newer active set.
5432 941 : if (peer->m_getsporks_recvd && peer->m_getsporks_last_response == active_spork_hashes) {
5433 55 : LogPrint(BCLog::NET, "Ignoring repeated \"getsporks\". peer=%d\n", pfrom.GetId());
5434 55 : return;
5435 : }
5436 886 : peer->m_getsporks_recvd = true;
5437 886 : peer->m_getsporks_last_response = active_spork_hashes;
5438 :
5439 1367 : for (const auto& pair : active_sporks) {
5440 1148 : for (const auto& signerSporkPair : pair.second) {
5441 667 : m_connman.PushMessage(&pfrom, CNetMsgMaker(pfrom.GetCommonVersion()).Make(NetMsgType::SPORK, signerSporkPair.second));
5442 : }
5443 : }
5444 886 : return;
5445 941 : }
5446 :
5447 106282 : if (msg_type == NetMsgType::QUORUMROTATIONINFO) {
5448 : // we have never requested this
5449 0 : Misbehaving(pfrom.GetId(), 100, strprintf("received not-requested quorumrotationinfo. peer=%d", pfrom.GetId()));
5450 0 : return;
5451 : }
5452 106282 : if (msg_type == NetMsgType::NOTFOUND) {
5453 : // Remove the NOTFOUND transactions from the peer
5454 513 : LOCK(cs_main);
5455 513 : CNodeState *state = State(pfrom.GetId());
5456 513 : std::vector<CInv> vInv;
5457 513 : vRecv >> vInv;
5458 513 : if (vInv.size() <= MAX_PEER_OBJECT_IN_FLIGHT + MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
5459 1116 : for (CInv &inv : vInv) {
5460 603 : if (inv.IsKnownType()) {
5461 : // If we receive a NOTFOUND message for a txid we requested, erase
5462 : // it from our data structures for this peer.
5463 603 : auto in_flight_it = state->m_object_download.m_object_in_flight.find(inv);
5464 603 : if (in_flight_it == state->m_object_download.m_object_in_flight.end()) {
5465 : // Skip any further work if this is a spurious NOTFOUND
5466 : // message.
5467 3 : continue;
5468 : }
5469 600 : state->m_object_download.m_object_in_flight.erase(in_flight_it);
5470 600 : state->m_object_download.m_object_announced.erase(inv);
5471 600 : }
5472 : }
5473 513 : }
5474 : return;
5475 513 : }
5476 :
5477 105769 : bool found = false;
5478 105769 : const std::vector<std::string> &allMessages = getAllNetMessageTypes();
5479 6368521 : for (const std::string& msg : allMessages) {
5480 6368509 : if(msg == msg_type) {
5481 105757 : found = true;
5482 105757 : break;
5483 : }
5484 : }
5485 :
5486 105769 : if (found)
5487 : {
5488 : // probably one the extensions
5489 105757 : if (m_cj_walletman) {
5490 4420 : PostProcessMessage(m_cj_walletman->processMessage(pfrom, m_chainman.ActiveChainstate(), m_connman, m_mempool, msg_type, vRecv), pfrom.GetId());
5491 4420 : }
5492 105757 : PostProcessMessage(CMNAuth::ProcessMessage(pfrom, peer->m_their_services, m_connman, m_mn_metaman, m_nodeman, m_mn_sync, m_dmnman->GetListAtChainTip(), msg_type, vRecv), pfrom.GetId());
5493 105757 : PostProcessMessage(m_llmq_ctx->quorum_block_processor->ProcessMessage(pfrom, msg_type, vRecv), pfrom.GetId());
5494 105757 : PostProcessMessage(ProcessPlatformBanMessage(pfrom.GetId(), msg_type, vRecv), pfrom.GetId());
5495 :
5496 105757 : if (msg_type == NetMsgType::CLSIG) {
5497 8972 : if (m_chainlocks.IsEnabled()) {
5498 8966 : chainlock::ChainLockSig clsig;
5499 8966 : vRecv >> clsig;
5500 8966 : const uint256& hash = ::SerializeHash(clsig);
5501 17932 : WITH_LOCK(::cs_main, EraseObjectRequest(pfrom.GetId(), CInv{MSG_CLSIG, hash}));
5502 8966 : PostProcessMessage(m_clhandler.ProcessNewChainLock(pfrom.GetId(), clsig, *m_llmq_ctx->qman, hash), pfrom.GetId());
5503 8966 : }
5504 8972 : return; // CLSIG
5505 : }
5506 :
5507 771632 : for (const auto& handler : m_handlers) {
5508 674847 : handler->ProcessMessage(pfrom, msg_type, vRecv);
5509 : }
5510 96785 : return;
5511 : }
5512 :
5513 : // Ignore unknown commands for extensibility
5514 12 : LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", SanitizeString(msg_type), pfrom.GetId());
5515 :
5516 12 : return;
5517 858971 : }
5518 :
5519 2375425 : bool PeerManagerImpl::MaybeDiscourageAndDisconnect(CNode& pnode, Peer& peer)
5520 : {
5521 : {
5522 2375425 : LOCK(peer.m_misbehavior_mutex);
5523 :
5524 : // There's nothing to do if the m_should_discourage flag isn't set
5525 2375425 : if (!peer.m_should_discourage) return false;
5526 :
5527 264 : peer.m_should_discourage = false;
5528 2375425 : } // peer.m_misbehavior_mutex
5529 :
5530 264 : if (pnode.HasPermission(NetPermissionFlags::NoBan)) {
5531 : // We never disconnect or discourage peers for bad behavior if they have NetPermissionFlags::NoBan permission
5532 18 : LogPrintf("Warning: not punishing noban peer %d!\n", peer.m_id);
5533 18 : return false;
5534 : }
5535 :
5536 246 : if (pnode.IsManualConn()) {
5537 : // We never disconnect or discourage manual peers for bad behavior
5538 9 : LogPrintf("Warning: not punishing manually connected peer %d!\n", peer.m_id);
5539 9 : return false;
5540 : }
5541 :
5542 237 : if (pnode.addr.IsLocal()) {
5543 : // We disconnect local peers for bad behavior but don't discourage (since that would discourage
5544 : // all peers on the same local address)
5545 233 : LogPrint(BCLog::NET, "Warning: disconnecting but not discouraging %s peer %d!\n",
5546 : pnode.m_inbound_onion ? "inbound onion" : "local", peer.m_id);
5547 233 : pnode.fDisconnect = true;
5548 233 : return true;
5549 : }
5550 :
5551 : // Normal case: Disconnect the peer and discourage all nodes sharing the address
5552 4 : LogPrint(BCLog::NET, "Disconnecting and discouraging peer %d!\n", peer.m_id);
5553 4 : if (m_banman) m_banman->Discourage(pnode.addr);
5554 4 : m_connman.DisconnectNode(pnode.addr);
5555 4 : return true;
5556 2375425 : }
5557 :
5558 3672093 : bool PeerManagerImpl::ProcessMessages(CNode* pfrom, std::atomic<bool>& interruptMsgProc)
5559 : {
5560 3672093 : AssertLockHeld(g_msgproc_mutex);
5561 :
5562 3672093 : PeerRef peer = GetPeerRef(pfrom->GetId());
5563 3672093 : if (peer == nullptr) return false;
5564 :
5565 : {
5566 3672093 : LOCK(peer->m_getdata_requests_mutex);
5567 3672093 : if (!peer->m_getdata_requests.empty()) {
5568 3442 : ProcessGetData(*pfrom, *peer, interruptMsgProc);
5569 3442 : }
5570 3672093 : }
5571 :
5572 : bool has_more_orphans;
5573 : {
5574 3672093 : LOCK(cs_main);
5575 3672093 : has_more_orphans = ProcessOrphanTx(peer->m_id);
5576 3672093 : }
5577 :
5578 3672093 : if (pfrom->fDisconnect)
5579 3 : return false;
5580 :
5581 3672090 : if (has_more_orphans) return true;
5582 :
5583 : // this maintains the order of responses
5584 : // and prevents m_getdata_requests to grow unbounded
5585 : {
5586 3672082 : LOCK(peer->m_getdata_requests_mutex);
5587 3672082 : if (!peer->m_getdata_requests.empty()) return true;
5588 3672082 : }
5589 :
5590 : // Don't bother if send buffer is too full to respond anyway
5591 3669763 : if (pfrom->fPauseSend) return false;
5592 :
5593 3669751 : auto poll_result{pfrom->PollMessage()};
5594 3669751 : if (!poll_result) {
5595 : // No message to process
5596 2883467 : return false;
5597 : }
5598 :
5599 786284 : CNetMessage& msg{poll_result->first};
5600 786284 : bool fMoreWork = poll_result->second;
5601 :
5602 : TRACE6(net, inbound_message,
5603 : pfrom->GetId(),
5604 : pfrom->m_addr_name.c_str(),
5605 : pfrom->ConnectionTypeAsString().c_str(),
5606 : msg.m_type.c_str(),
5607 : msg.m_recv.size(),
5608 : msg.m_recv.data()
5609 : );
5610 :
5611 786284 : if (gArgs.GetBoolArg("-capturemessages", false)) {
5612 14 : CaptureMessage(pfrom->addr, msg.m_type, MakeUCharSpan(msg.m_recv), /*is_incoming=*/true);
5613 14 : }
5614 :
5615 786284 : msg.SetVersion(pfrom->GetCommonVersion());
5616 :
5617 : try {
5618 786284 : ProcessMessage(*pfrom, msg.m_type, msg.m_recv, msg.m_time, interruptMsgProc);
5619 786269 : if (interruptMsgProc) return false;
5620 : {
5621 786253 : LOCK(peer->m_getdata_requests_mutex);
5622 786253 : if (!peer->m_getdata_requests.empty()) fMoreWork = true;
5623 786253 : }
5624 786268 : } catch (const std::exception& e) {
5625 15 : LogPrint(BCLog::NET, "%s(%s, %u bytes): Exception '%s' (%s) caught\n", __func__, SanitizeString(msg.m_type), msg.m_message_size, e.what(), typeid(e).name());
5626 15 : } catch (...) {
5627 0 : LogPrint(BCLog::NET, "%s(%s, %u bytes): Unknown exception caught\n", __func__, SanitizeString(msg.m_type), msg.m_message_size);
5628 15 : }
5629 :
5630 786268 : return fMoreWork;
5631 3672108 : }
5632 :
5633 2363231 : void PeerManagerImpl::ConsiderEviction(CNode& pto, Peer& peer, std::chrono::seconds time_in_seconds)
5634 : {
5635 2363231 : AssertLockHeld(cs_main);
5636 :
5637 2363231 : CNodeState &state = *State(pto.GetId());
5638 :
5639 2363231 : if (!state.m_chain_sync.m_protect && pto.IsOutboundOrBlockRelayConn() && state.fSyncStarted) {
5640 : // This is an outbound peer subject to disconnection if they don't
5641 : // announce a block with as much work as the current tip within
5642 : // CHAIN_SYNC_TIMEOUT + HEADERS_RESPONSE_TIME seconds (note: if
5643 : // their chain has more work than ours, we should sync to it,
5644 : // unless it's invalid, in which case we should find that out and
5645 : // disconnect from them elsewhere).
5646 33116 : if (state.pindexBestKnownBlock != nullptr && state.pindexBestKnownBlock->nChainWork >= m_chainman.ActiveChain().Tip()->nChainWork) {
5647 7899 : if (state.m_chain_sync.m_timeout != 0s) {
5648 1044 : state.m_chain_sync.m_timeout = 0s;
5649 1044 : state.m_chain_sync.m_work_header = nullptr;
5650 1044 : state.m_chain_sync.m_sent_getheaders = false;
5651 1044 : }
5652 33116 : } else if (state.m_chain_sync.m_timeout == 0s || (state.m_chain_sync.m_work_header != nullptr && state.pindexBestKnownBlock != nullptr && state.pindexBestKnownBlock->nChainWork >= state.m_chain_sync.m_work_header->nChainWork)) {
5653 : // Our best block known by this peer is behind our tip, and we're either noticing
5654 : // that for the first time, OR this peer was able to catch up to some earlier point
5655 : // where we checked against our tip.
5656 : // Either way, set a new timeout based on current tip.
5657 3078 : state.m_chain_sync.m_timeout = time_in_seconds + CHAIN_SYNC_TIMEOUT;
5658 3078 : state.m_chain_sync.m_work_header = m_chainman.ActiveChain().Tip();
5659 3078 : state.m_chain_sync.m_sent_getheaders = false;
5660 25217 : } else if (state.m_chain_sync.m_timeout > 0s && time_in_seconds > state.m_chain_sync.m_timeout) {
5661 : // No evidence yet that our peer has synced to a chain with work equal to that
5662 : // of our tip, when we first detected it was behind. Send a single getheaders
5663 : // message to give the peer a chance to update us.
5664 20 : if (state.m_chain_sync.m_sent_getheaders) {
5665 : // They've run out of time to catch up!
5666 5 : LogPrintf("Disconnecting outbound peer %d for old chain, best known block = %s\n", pto.GetId(), state.pindexBestKnownBlock != nullptr ? state.pindexBestKnownBlock->GetBlockHash().ToString() : "<none>");
5667 5 : pto.fDisconnect = true;
5668 5 : } else {
5669 15 : assert(state.m_chain_sync.m_work_header);
5670 : // Here, we assume that the getheaders message goes out,
5671 : // because it'll either go out or be skipped because of a
5672 : // getheaders in-flight already, in which case the peer should
5673 : // still respond to us with a sufficiently high work chain tip.
5674 15 : std::string msg_type = UsesCompressedHeaders(peer) ? NetMsgType::GETHEADERS2 : NetMsgType::GETHEADERS;
5675 30 : MaybeSendGetHeaders(pto,
5676 15 : msg_type, m_chainman.ActiveChain().GetLocator(state.m_chain_sync.m_work_header->pprev),
5677 15 : peer);
5678 15 : LogPrint(BCLog::NET, "sending %s to outbound peer=%d to verify chain work (current best known block:%s, benchmark blockhash: %s)\n", msg_type, pto.GetId(), state.pindexBestKnownBlock != nullptr ? state.pindexBestKnownBlock->GetBlockHash().ToString() : "<none>", state.m_chain_sync.m_work_header->GetBlockHash().ToString());
5679 15 : state.m_chain_sync.m_sent_getheaders = true;
5680 : // Bump the timeout to allow a response, which could clear the timeout
5681 : // (if the response shows the peer has synced), reset the timeout (if
5682 : // the peer syncs to the required work but not to our tip), or result
5683 : // in disconnect (if we advance to the timeout and pindexBestKnownBlock
5684 : // has not sufficiently progressed)
5685 15 : state.m_chain_sync.m_timeout = time_in_seconds + HEADERS_RESPONSE_TIME;
5686 15 : }
5687 20 : }
5688 33116 : }
5689 2363231 : }
5690 :
5691 6076 : void PeerManagerImpl::EvictExtraOutboundPeers(std::chrono::seconds now)
5692 : {
5693 : // If we have any extra block-relay-only peers, disconnect the youngest unless
5694 : // it's given us a block -- in which case, compare with the second-youngest, and
5695 : // out of those two, disconnect the peer who least recently gave us a block.
5696 : // The youngest block-relay-only peer would be the extra peer we connected
5697 : // to temporarily in order to sync our tip; see net.cpp.
5698 : // Note that we use higher nodeid as a measure for most recent connection.
5699 6076 : if (m_connman.GetExtraBlockRelayCount() > 0) {
5700 3 : std::pair<NodeId, std::chrono::seconds> youngest_peer{-1, 0}, next_youngest_peer{-1, 0};
5701 :
5702 12 : m_connman.ForEachNode([&](CNode* pnode) {
5703 9 : if (!pnode->IsBlockOnlyConn() || pnode->fDisconnect) return;
5704 9 : if (pnode->GetId() > youngest_peer.first) {
5705 9 : next_youngest_peer = youngest_peer;
5706 9 : youngest_peer.first = pnode->GetId();
5707 9 : youngest_peer.second = pnode->m_last_block_time;
5708 9 : }
5709 9 : });
5710 3 : NodeId to_disconnect = youngest_peer.first;
5711 3 : if (youngest_peer.second > next_youngest_peer.second) {
5712 : // Our newest block-relay-only peer gave us a block more recently;
5713 : // disconnect our second youngest.
5714 1 : to_disconnect = next_youngest_peer.first;
5715 1 : }
5716 6 : m_connman.ForNode(to_disconnect, [&](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
5717 3 : AssertLockHeld(::cs_main);
5718 : // Make sure we're not getting a block right now, and that
5719 : // we've been connected long enough for this eviction to happen
5720 : // at all.
5721 : // Note that we only request blocks from a peer if we learn of a
5722 : // valid headers chain with at least as much work as our tip.
5723 3 : CNodeState *node_state = State(pnode->GetId());
5724 6 : if (node_state == nullptr ||
5725 3 : (now - pnode->m_connected >= MINIMUM_CONNECT_TIME && node_state->nBlocksInFlight == 0)) {
5726 2 : pnode->fDisconnect = true;
5727 2 : LogPrint(BCLog::NET, "disconnecting extra block-relay-only peer=%d (last block received at time %d)\n",
5728 : pnode->GetId(), count_seconds(pnode->m_last_block_time));
5729 2 : return true;
5730 : } else {
5731 1 : LogPrint(BCLog::NET, "keeping block-relay-only peer=%d chosen for eviction (connect time: %d, blocks_in_flight: %d)\n",
5732 : pnode->GetId(), count_seconds(pnode->m_connected), node_state->nBlocksInFlight);
5733 : }
5734 1 : return false;
5735 3 : });
5736 3 : }
5737 :
5738 : // Check whether we have too many outbound-full-relay peers
5739 6076 : if (m_connman.GetExtraFullOutboundCount() > 0) {
5740 : // If we have more outbound-full-relay peers than we target, disconnect one.
5741 : // Pick the outbound-full-relay peer that least recently announced
5742 : // us a new block, with ties broken by choosing the more recent
5743 : // connection (higher node id)
5744 : // Protect peers from eviction if we don't have another connection
5745 : // to their network, counting both outbound-full-relay and manual peers.
5746 4 : NodeId worst_peer = -1;
5747 4 : int64_t oldest_block_announcement = std::numeric_limits<int64_t>::max();
5748 :
5749 : // We want to prevent recently connected to Onion peers from being disconnected here, protect them as long as
5750 : // there are more non_onion nodes than onion nodes so far
5751 4 : size_t onion_count = 0;
5752 :
5753 42 : m_connman.ForEachNode([&](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_connman.GetNodesMutex()) {
5754 38 : AssertLockHeld(::cs_main);
5755 38 : if (pnode->addr.IsTor() && ++onion_count <= m_connman.GetMaxOutboundOnionNodeCount()) return;
5756 : // Don't disconnect masternodes just because they were slow in block announcement
5757 38 : if (pnode->m_masternode_connection) return;
5758 : // Only consider outbound-full-relay peers that are not already
5759 : // marked for disconnection
5760 38 : if (!pnode->IsFullOutboundConn() || pnode->fDisconnect) return;
5761 38 : CNodeState *state = State(pnode->GetId());
5762 38 : if (state == nullptr) return; // shouldn't be possible, but just in case
5763 : // Don't evict our protected peers
5764 38 : if (state->m_chain_sync.m_protect) return;
5765 : // If this is the only connection on a particular network that is
5766 : // OUTBOUND_FULL_RELAY or MANUAL, protect it.
5767 38 : if (!m_connman.MultipleManualOrFullOutboundConns(pnode->addr.GetNetwork())) return;
5768 37 : if (state->m_last_block_announcement < oldest_block_announcement || (state->m_last_block_announcement == oldest_block_announcement && pnode->GetId() > worst_peer)) {
5769 34 : worst_peer = pnode->GetId();
5770 34 : oldest_block_announcement = state->m_last_block_announcement;
5771 34 : }
5772 38 : });
5773 4 : if (worst_peer != -1) {
5774 8 : bool disconnected = m_connman.ForNode(worst_peer, [&](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
5775 4 : AssertLockHeld(::cs_main);
5776 :
5777 : // Only disconnect a peer that has been connected to us for
5778 : // some reasonable fraction of our check-frequency, to give
5779 : // it time for new information to have arrived.
5780 : // Also don't disconnect any peer we're trying to download a
5781 : // block from.
5782 4 : CNodeState &state = *State(pnode->GetId());
5783 4 : if (now - pnode->m_connected > MINIMUM_CONNECT_TIME && state.nBlocksInFlight == 0) {
5784 4 : LogPrint(BCLog::NET, "disconnecting extra outbound peer=%d (last block announcement received at time %d)\n", pnode->GetId(), oldest_block_announcement);
5785 4 : pnode->fDisconnect = true;
5786 4 : return true;
5787 : } else {
5788 0 : LogPrint(BCLog::NET, "keeping outbound peer=%d chosen for eviction (connect time: %d, blocks_in_flight: %d)\n",
5789 : pnode->GetId(), count_seconds(pnode->m_connected), state.nBlocksInFlight);
5790 0 : return false;
5791 : }
5792 4 : });
5793 4 : if (disconnected) {
5794 : // If we disconnected an extra peer, that means we successfully
5795 : // connected to at least one peer after the last time we
5796 : // detected a stale tip. Don't try any more extra peers until
5797 : // we next detect a stale tip, to limit the load we put on the
5798 : // network from these extra connections.
5799 4 : m_connman.SetTryNewOutboundPeer(false);
5800 4 : }
5801 4 : }
5802 4 : }
5803 6076 : }
5804 :
5805 6076 : void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
5806 : {
5807 6076 : LOCK(cs_main);
5808 :
5809 6076 : auto now{GetTime<std::chrono::seconds>()};
5810 :
5811 6076 : EvictExtraOutboundPeers(now);
5812 :
5813 6076 : if (now > m_stale_tip_check_time) {
5814 : // Check whether our tip is stale, and if so, allow using an extra
5815 : // outbound peer
5816 4624 : if (!fImporting && !fReindex && m_connman.GetNetworkActive() && m_connman.GetUseAddrmanOutgoing() && TipMayBeStale()) {
5817 1861 : LogPrintf("Potential stale tip detected, will try using extra outbound peer (last tip update: %d seconds ago)\n",
5818 : count_seconds(now - m_last_tip_update.load()));
5819 1861 : m_connman.SetTryNewOutboundPeer(true);
5820 4624 : } else if (m_connman.GetTryNewOutboundPeer()) {
5821 77 : m_connman.SetTryNewOutboundPeer(false);
5822 77 : }
5823 4624 : m_stale_tip_check_time = now + STALE_CHECK_INTERVAL;
5824 4624 : }
5825 :
5826 6076 : if (!m_initial_sync_finished && CanDirectFetch()) {
5827 854 : m_connman.StartExtraBlockRelayPeers();
5828 854 : m_initial_sync_finished = true;
5829 854 : }
5830 6076 : }
5831 :
5832 2363247 : void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::microseconds now)
5833 : {
5834 2363354 : if (m_connman.ShouldRunInactivityChecks(node_to, std::chrono::duration_cast<std::chrono::seconds>(now)) &&
5835 107 : peer.m_ping_nonce_sent &&
5836 15 : now > peer.m_ping_start.load() + TIMEOUT_INTERVAL)
5837 : {
5838 : // The ping timeout is using mocktime. To disable the check during
5839 : // testing, increase -peertimeout.
5840 2 : LogPrint(BCLog::NET, "ping timeout: %fs peer=%d\n", 0.000001 * count_microseconds(now - peer.m_ping_start.load()), peer.m_id);
5841 2 : node_to.fDisconnect = true;
5842 2 : return;
5843 : }
5844 :
5845 2363245 : const CNetMsgMaker msgMaker(node_to.GetCommonVersion());
5846 2363245 : bool pingSend = false;
5847 :
5848 2363245 : if (peer.m_ping_queued) {
5849 : // RPC ping request by user
5850 372 : pingSend = true;
5851 372 : }
5852 :
5853 2363245 : if (peer.m_ping_nonce_sent == 0 && now > peer.m_ping_start.load() + PING_INTERVAL) {
5854 : // Ping automatically sent as a latency probe & keepalive.
5855 21753 : pingSend = true;
5856 21753 : }
5857 :
5858 2363245 : if (pingSend) {
5859 : uint64_t nonce;
5860 22120 : do {
5861 22120 : nonce = GetRand<uint64_t>();
5862 22120 : } while (nonce == 0);
5863 22120 : peer.m_ping_queued = false;
5864 22120 : peer.m_ping_start = now;
5865 22120 : peer.m_ping_nonce_sent = nonce;
5866 22120 : m_connman.PushMessage(&node_to, msgMaker.Make(NetMsgType::PING, nonce));
5867 22120 : }
5868 2363247 : }
5869 :
5870 2363243 : void PeerManagerImpl::MaybeSendAddr(CNode& node, Peer& peer, std::chrono::microseconds current_time)
5871 : {
5872 : // Nothing to do for non-address-relay peers
5873 2363243 : if (!peer.m_addr_relay_enabled) return;
5874 :
5875 2358679 : LOCK(peer.m_addr_send_times_mutex);
5876 : // Periodically advertise our local address to the peer.
5877 2358679 : if (fListen && !m_chainman.ActiveChainstate().IsInitialBlockDownload() &&
5878 2306761 : peer.m_next_local_addr_send < current_time) {
5879 : // If we've sent before, clear the bloom filter for the peer, so that our
5880 : // self-announcement will actually go out.
5881 : // This might be unnecessary if the bloom filter has already rolled
5882 : // over since our last self-announcement, but there is only a small
5883 : // bandwidth cost that we can incur by doing this (which happens
5884 : // once a day on average).
5885 9029 : if (peer.m_next_local_addr_send != 0us) {
5886 676 : peer.m_addr_known->reset();
5887 676 : }
5888 9030 : if (std::optional<CService> local_service = GetLocalAddrForPeer(node)) {
5889 1 : CAddress local_addr{*local_service, peer.m_our_services, Now<NodeSeconds>()};
5890 1 : FastRandomContext insecure_rand;
5891 1 : PushAddress(peer, local_addr, insecure_rand);
5892 1 : }
5893 9029 : peer.m_next_local_addr_send = GetExponentialRand(current_time, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
5894 9029 : }
5895 :
5896 : // We sent an `addr` message to this peer recently. Nothing more to do.
5897 2358679 : if (current_time <= peer.m_next_addr_send) return;
5898 :
5899 29807 : peer.m_next_addr_send = GetExponentialRand(current_time, AVG_ADDRESS_BROADCAST_INTERVAL);
5900 :
5901 29807 : if (!Assume(peer.m_addrs_to_send.size() <= MAX_ADDR_TO_SEND)) {
5902 : // Should be impossible since we always check size before adding to
5903 : // m_addrs_to_send. Recover by trimming the vector.
5904 0 : peer.m_addrs_to_send.resize(MAX_ADDR_TO_SEND);
5905 0 : }
5906 :
5907 : // Remove addr records that the peer already knows about, and add new
5908 : // addrs to the m_addr_known filter on the same pass.
5909 67759 : auto addr_already_known = [&peer](const CAddress& addr) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) {
5910 37952 : bool ret = peer.m_addr_known->contains(addr.GetKey());
5911 37952 : if (!ret) peer.m_addr_known->insert(addr.GetKey());
5912 37952 : return ret;
5913 0 : };
5914 29807 : peer.m_addrs_to_send.erase(std::remove_if(peer.m_addrs_to_send.begin(), peer.m_addrs_to_send.end(), addr_already_known),
5915 29807 : peer.m_addrs_to_send.end());
5916 :
5917 : // No addr messages to send
5918 29807 : if (peer.m_addrs_to_send.empty()) return;
5919 :
5920 : const char* msg_type;
5921 : int make_flags;
5922 205 : if (peer.m_wants_addrv2) {
5923 2 : msg_type = NetMsgType::ADDRV2;
5924 2 : make_flags = ADDRV2_FORMAT;
5925 2 : } else {
5926 203 : msg_type = NetMsgType::ADDR;
5927 203 : make_flags = 0;
5928 : }
5929 205 : m_connman.PushMessage(&node, CNetMsgMaker(node.GetCommonVersion()).Make(make_flags, msg_type, peer.m_addrs_to_send));
5930 205 : peer.m_addrs_to_send.clear();
5931 :
5932 : // we only send the big addr message once
5933 205 : if (peer.m_addrs_to_send.capacity() > 40) {
5934 42 : peer.m_addrs_to_send.shrink_to_fit();
5935 42 : }
5936 2363243 : }
5937 :
5938 : namespace {
5939 : class CompareInvMempoolOrder
5940 : {
5941 : CTxMemPool* mp;
5942 : public:
5943 1786644 : explicit CompareInvMempoolOrder(CTxMemPool *_mempool)
5944 893322 : {
5945 893322 : mp = _mempool;
5946 1786644 : }
5947 :
5948 98609 : bool operator()(std::set<uint256>::iterator a, std::set<uint256>::iterator b)
5949 : {
5950 : /* As std::make_heap produces a max-heap, we want the entries with the
5951 : * fewest ancestors/highest fee to sort later. */
5952 98609 : return mp->CompareDepthAndScore(*b, *a);
5953 : }
5954 : };
5955 : } // namespace
5956 :
5957 961157 : bool PeerManagerImpl::RejectIncomingTxs(const CNode& peer) const
5958 : {
5959 : // block-relay-only peers may never send txs to us
5960 961157 : if (peer.IsBlockOnlyConn()) return true;
5961 960889 : if (peer.IsFeelerConn()) return true;
5962 : // In -blocksonly mode, peers need the 'relay' permission to send txs to us
5963 960885 : if (m_ignore_incoming_txs && !peer.HasPermission(NetPermissionFlags::Relay)) return true;
5964 960751 : return false;
5965 961157 : }
5966 :
5967 9122 : bool PeerManagerImpl::SetupAddressRelay(const CNode& node, Peer& peer)
5968 : {
5969 : // We don't participate in addr relay with outbound block-relay-only
5970 : // connections to prevent providing adversaries with the additional
5971 : // information of addr traffic to infer the link.
5972 9122 : if (node.IsBlockOnlyConn()) return false;
5973 :
5974 9072 : if (!peer.m_addr_relay_enabled.exchange(true)) {
5975 : // During version message processing (non-block-relay-only outbound peers)
5976 : // or on first addr-related message we have received (inbound peers), initialize
5977 : // m_addr_known.
5978 8950 : peer.m_addr_known = std::make_unique<CRollingBloomFilter>(5000, 0.001);
5979 8950 : }
5980 :
5981 9072 : return true;
5982 9122 : }
5983 :
5984 2375425 : bool PeerManagerImpl::SendMessages(CNode* pto)
5985 : {
5986 2375425 : AssertLockHeld(g_msgproc_mutex);
5987 :
5988 2375425 : assert(m_llmq_ctx);
5989 :
5990 2375425 : const bool is_masternode = m_nodeman != nullptr;
5991 :
5992 2375425 : PeerRef peer = GetPeerRef(pto->GetId());
5993 2375425 : if (!peer) return false;
5994 2375425 : const Consensus::Params& consensusParams = m_chainparams.GetConsensus();
5995 :
5996 : // We must call MaybeDiscourageAndDisconnect first, to ensure that we'll
5997 : // disconnect misbehaving peers even before the version handshake is complete.
5998 2375425 : if (MaybeDiscourageAndDisconnect(*pto, *peer)) return true;
5999 :
6000 : // Don't send anything until the version handshake is complete
6001 2375188 : if (!pto->fSuccessfullyConnected || pto->fDisconnect)
6002 11939 : return true;
6003 :
6004 : // If we get here, the outgoing message serialization version is set and can't change.
6005 2363249 : const CNetMsgMaker msgMaker(pto->GetCommonVersion());
6006 :
6007 2363249 : const auto current_time{GetTime<std::chrono::microseconds>()};
6008 :
6009 2363249 : if (pto->IsAddrFetchConn() && current_time - pto->m_connected > 10 * AVG_ADDRESS_BROADCAST_INTERVAL) {
6010 2 : LogPrint(BCLog::NET_NETCONN, "addrfetch connection timeout; disconnecting peer=%d\n", pto->GetId());
6011 2 : pto->fDisconnect = true;
6012 2 : return true;
6013 : }
6014 :
6015 2363247 : MaybeSendPing(*pto, *peer, current_time);
6016 :
6017 : // MaybeSendPing may have marked peer for disconnection
6018 2363247 : if (pto->fDisconnect) return true;
6019 :
6020 2363243 : MaybeSendAddr(*pto, *peer, current_time);
6021 :
6022 : {
6023 2363243 : LOCK(cs_main);
6024 :
6025 2363243 : CNodeState &state = *State(pto->GetId());
6026 :
6027 : // Start block sync
6028 2363243 : if (m_chainman.m_best_header == nullptr) {
6029 0 : m_chainman.m_best_header = m_chainman.ActiveChain().Tip();
6030 0 : }
6031 :
6032 : // Determine whether we might try initial headers sync or parallel
6033 : // block download from this peer -- this mostly affects behavior while
6034 : // in IBD (once out of IBD, we sync from all peers).
6035 2363243 : bool sync_blocks_and_headers_from_peer = false;
6036 2363243 : if (state.fPreferredDownload) {
6037 1336678 : sync_blocks_and_headers_from_peer = true;
6038 2363243 : } else if (CanServeBlocks(*peer) && !pto->IsAddrFetchConn()) {
6039 : // Typically this is an inbound peer. If we don't have any outbound
6040 : // peers, or if we aren't downloading any blocks from such peers,
6041 : // then allow block downloads from this peer, too.
6042 : // We prefer downloading blocks from outbound peers to avoid
6043 : // putting undue load on (say) some home user who is just making
6044 : // outbound connections to the network, but if our only source of
6045 : // the latest blocks is from an inbound peer, we have to be sure to
6046 : // eventually download it (and not just wait indefinitely for an
6047 : // outbound peer to have it).
6048 1023371 : if (m_num_preferred_download_peers == 0 || mapBlocksInFlight.empty()) {
6049 985323 : sync_blocks_and_headers_from_peer = true;
6050 985323 : }
6051 1023371 : }
6052 :
6053 2363243 : if (!state.fSyncStarted && CanServeBlocks(*peer) && !fImporting && !fReindex && pto->CanRelay()) {
6054 : // Only actively request headers from a single peer, unless we're close to end of initial download.
6055 12545 : if ((nSyncStarted == 0 && sync_blocks_and_headers_from_peer) || m_chainman.m_best_header->GetBlockTime() > GetAdjustedTime() - nMaxTipAge) {
6056 8019 : const CBlockIndex* pindexStart = m_chainman.m_best_header;
6057 : /* If possible, start at the block preceding the currently
6058 : best known header. This ensures that we always get a
6059 : non-empty list of headers back as long as the peer
6060 : is up-to-date. With a non-empty response, we can initialise
6061 : the peer's known best block. This wouldn't be possible
6062 : if we requested starting at m_chainman.m_best_header and
6063 : got back an empty response. */
6064 8019 : if (pindexStart->pprev)
6065 6939 : pindexStart = pindexStart->pprev;
6066 8019 : std::string msg_type = UsesCompressedHeaders(*peer) ? NetMsgType::GETHEADERS2 : NetMsgType::GETHEADERS;
6067 8019 : if (MaybeSendGetHeaders(*pto, msg_type, m_chainman.ActiveChain().GetLocator(pindexStart), *peer)) {
6068 8019 : LogPrint(BCLog::NET, "initial %s (%d) to peer=%d (startheight:%d)\n", msg_type, pindexStart->nHeight, pto->GetId(), peer->m_starting_height);
6069 :
6070 8019 : state.fSyncStarted = true;
6071 16038 : state.m_headers_sync_timeout = current_time + HEADERS_DOWNLOAD_TIMEOUT_BASE +
6072 : (
6073 : // Convert HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER to microseconds before scaling
6074 : // to maintain precision
6075 16038 : std::chrono::microseconds{HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER} *
6076 8019 : (GetAdjustedTime() - m_chainman.m_best_header->GetBlockTime()) / consensusParams.nPowTargetSpacing
6077 : );
6078 8019 : nSyncStarted++;
6079 8019 : }
6080 8019 : }
6081 12545 : }
6082 :
6083 : //
6084 : // Try sending block announcements via headers
6085 : //
6086 2363243 : if (pto->CanRelay()) {
6087 : // If we have no more than MAX_BLOCKS_TO_ANNOUNCE in our
6088 : // list of block hashes we're relaying, and our peer wants
6089 : // headers announcements, then find the first header
6090 : // not yet known to our peer but would connect, and send.
6091 : // If no header would connect, or if we have too many
6092 : // blocks, or if the peer doesn't want headers, just
6093 : // add all to the inv queue.
6094 2302229 : LOCK(peer->m_block_inv_mutex);
6095 2302229 : std::vector<CBlock> vHeaders;
6096 4604458 : bool fRevertToInv = ((!state.fPreferHeaders && !state.fPreferHeadersCompressed &&
6097 2302229 : (!state.m_requested_hb_cmpctblocks || peer->m_blocks_for_headers_relay.size() > 1)) ||
6098 2140349 : peer->m_blocks_for_headers_relay.size() > MAX_BLOCKS_TO_ANNOUNCE);
6099 2302229 : const CBlockIndex *pBestIndex = nullptr; // last header queued for delivery
6100 2302229 : ProcessBlockAvailability(pto->GetId()); // ensure pindexBestKnownBlock is up-to-date
6101 :
6102 2302229 : if (!fRevertToInv) {
6103 2140242 : bool fFoundStartingHeader = false;
6104 : // Try to find first header that our peer doesn't have, and
6105 : // then send all headers past that one. If we come across any
6106 : // headers that aren't on m_chainman.ActiveChain(), give up.
6107 2592354 : for (const uint256& hash : peer->m_blocks_for_headers_relay) {
6108 453579 : const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(hash);
6109 453579 : assert(pindex);
6110 453579 : if (m_chainman.ActiveChain()[pindex->nHeight] != pindex) {
6111 : // Bail out if we reorged away from this block
6112 100 : fRevertToInv = true;
6113 100 : break;
6114 : }
6115 453479 : if (pBestIndex != nullptr && pindex->pprev != pBestIndex) {
6116 : // This means that the list of blocks to announce don't
6117 : // connect to each other.
6118 : // This shouldn't really be possible to hit during
6119 : // regular operation (because reorgs should take us to
6120 : // a chain that has some block not on the prior chain,
6121 : // which should be caught by the prior check), but one
6122 : // way this could happen is by using invalidateblock /
6123 : // reconsiderblock repeatedly on the tip, causing it to
6124 : // be added multiple times to m_blocks_for_headers_relay.
6125 : // Robustly deal with this rare situation by reverting
6126 : // to an inv.
6127 0 : fRevertToInv = true;
6128 0 : break;
6129 : }
6130 453479 : pBestIndex = pindex;
6131 453479 : bool isPrevDevnetGenesisBlock = false;
6132 453479 : if (!consensusParams.hashDevnetGenesisBlock.IsNull() &&
6133 0 : pindex->pprev != nullptr &&
6134 0 : pindex->pprev->GetBlockHash() == consensusParams.hashDevnetGenesisBlock) {
6135 : // even though the devnet genesis block was never transferred through the wire and thus not
6136 : // appear anywhere in the node state where we track what other nodes have or not have, we can
6137 : // assume that the other node already knows the devnet genesis block
6138 0 : isPrevDevnetGenesisBlock = true;
6139 0 : }
6140 453479 : if (fFoundStartingHeader) {
6141 : // add this to the headers message
6142 42498 : vHeaders.emplace_back(pindex->GetBlockHeader());
6143 453479 : } else if (PeerHasHeader(&state, pindex)) {
6144 309351 : continue; // keep looking for the first new block
6145 101630 : } else if (pindex->pprev == nullptr || PeerHasHeader(&state, pindex->pprev) || isPrevDevnetGenesisBlock) {
6146 : // Peer doesn't have this header but they do have the prior one.
6147 : // Start sending headers.
6148 100263 : fFoundStartingHeader = true;
6149 100263 : vHeaders.emplace_back(pindex->GetBlockHeader());
6150 100263 : } else {
6151 : // Peer doesn't have this header or the prior one -- nothing will
6152 : // connect, so bail out.
6153 1367 : fRevertToInv = true;
6154 1367 : break;
6155 : }
6156 : }
6157 2140242 : }
6158 2302229 : if (!fRevertToInv && !vHeaders.empty()) {
6159 100263 : if (vHeaders.size() == 1 && state.m_requested_hb_cmpctblocks) {
6160 : // We only send up to 1 block as header-and-ids, as otherwise
6161 : // probably means we're doing an initial-ish-sync or they're slow
6162 19261 : LogPrint(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", __func__,
6163 : vHeaders.front().GetHash().ToString(), pto->GetId());
6164 :
6165 19261 : std::optional<CSerializedNetMsg> cached_cmpctblock_msg;
6166 : {
6167 19261 : LOCK(m_most_recent_block_mutex);
6168 19261 : if (m_most_recent_block_hash == pBestIndex->GetBlockHash()) {
6169 746 : cached_cmpctblock_msg = msgMaker.Make(NetMsgType::CMPCTBLOCK, *m_most_recent_compact_block);
6170 746 : }
6171 19261 : }
6172 19261 : if (cached_cmpctblock_msg.has_value()) {
6173 746 : m_connman.PushMessage(pto, std::move(cached_cmpctblock_msg.value()));
6174 746 : } else {
6175 18515 : CBlock block;
6176 18515 : bool ret = ReadBlockFromDisk(block, pBestIndex, consensusParams);
6177 18515 : assert(ret);
6178 18515 : CBlockHeaderAndShortTxIDs cmpctblock{block};
6179 18515 : m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::CMPCTBLOCK, cmpctblock));
6180 18515 : }
6181 19261 : state.pindexBestHeaderSent = pBestIndex;
6182 100263 : } else if (state.fPreferHeadersCompressed) {
6183 80434 : std::vector<CompressibleBlockHeader> vHeadersCompressed;
6184 80434 : std::list<int32_t> last_unique_versions;
6185 :
6186 : // Save other headers compressed
6187 203334 : std::for_each(vHeaders.cbegin(), vHeaders.cend(), [&vHeadersCompressed, &last_unique_versions](const auto& block) {
6188 122900 : CompressibleBlockHeader compressible_header{block.GetBlockHeader()};
6189 122900 : compressible_header.Compress(vHeadersCompressed, last_unique_versions);
6190 122900 : vHeadersCompressed.push_back(compressible_header);
6191 122900 : });
6192 :
6193 : // Push message to peer
6194 80434 : m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::HEADERS2, vHeadersCompressed));
6195 80434 : state.pindexBestHeaderSent = pBestIndex;
6196 81002 : } else if (state.fPreferHeaders) {
6197 568 : if (vHeaders.size() > 1) {
6198 8 : LogPrint(BCLog::NET, "%s: %u headers, range (%s, %s), to peer=%d\n", __func__,
6199 : vHeaders.size(),
6200 : vHeaders.front().GetHash().ToString(),
6201 : vHeaders.back().GetHash().ToString(), pto->GetId());
6202 8 : } else {
6203 560 : LogPrint(BCLog::NET, "%s: sending header %s to peer=%d\n", __func__,
6204 : vHeaders.front().GetHash().ToString(), pto->GetId());
6205 : }
6206 568 : m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
6207 568 : state.pindexBestHeaderSent = pBestIndex;
6208 568 : } else
6209 0 : fRevertToInv = true;
6210 100263 : }
6211 2302229 : if (fRevertToInv) {
6212 : // If falling back to using an inv, just try to inv the tip.
6213 : // The last entry in m_blocks_for_headers_relay was our tip at some point
6214 : // in the past.
6215 163454 : if (!peer->m_blocks_for_headers_relay.empty()) {
6216 24169 : const uint256& hashToAnnounce = peer->m_blocks_for_headers_relay.back();
6217 24169 : const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(hashToAnnounce);
6218 24169 : assert(pindex);
6219 :
6220 : // Warn if we're announcing a block that is not on the main chain.
6221 : // This should be very rare and could be optimized out.
6222 : // Just log for now.
6223 24169 : if (m_chainman.ActiveChain()[pindex->nHeight] != pindex) {
6224 104 : LogPrint(BCLog::NET, "Announcing block %s not on main chain (tip=%s)\n",
6225 : hashToAnnounce.ToString(), m_chainman.ActiveChain().Tip()->GetBlockHash().ToString());
6226 104 : }
6227 :
6228 : // If the peer's chain has this block, don't inv it back.
6229 24169 : if (!PeerHasHeader(&state, pindex)) {
6230 18616 : peer->m_blocks_for_inv_relay.push_back(hashToAnnounce);
6231 18616 : LogPrint(BCLog::NET, "%s: sending inv peer=%d hash=%s\n", __func__,
6232 : pto->GetId(), hashToAnnounce.ToString());
6233 18616 : }
6234 24169 : }
6235 163454 : }
6236 2302229 : peer->m_blocks_for_headers_relay.clear();
6237 2302229 : }
6238 :
6239 : //
6240 : // Message: inventory
6241 : //
6242 2363243 : std::vector<CInv> vInv;
6243 : {
6244 2363243 : LOCK(peer->m_block_inv_mutex);
6245 :
6246 2363243 : size_t reserve = INVENTORY_BROADCAST_MAX_PER_1MB_BLOCK * MaxBlockSize() / 1000000;
6247 2363243 : if (auto tx_relay = peer->GetTxRelay(); tx_relay != nullptr) {
6248 2361879 : LOCK(tx_relay->m_tx_inventory_mutex);
6249 2361879 : reserve = std::min<size_t>(tx_relay->m_tx_inventory_to_send.size(), reserve);
6250 2361879 : }
6251 2363243 : reserve = std::max<size_t>(reserve, peer->m_blocks_for_inv_relay.size());
6252 2363243 : reserve = std::min<size_t>(reserve, MAX_INV_SZ);
6253 2363243 : vInv.reserve(reserve);
6254 :
6255 : // Add blocks
6256 2381933 : for (const uint256& hash : peer->m_blocks_for_inv_relay) {
6257 18690 : vInv.emplace_back(MSG_BLOCK, hash);
6258 18690 : if (vInv.size() == MAX_INV_SZ) {
6259 0 : m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
6260 0 : vInv.clear();
6261 0 : }
6262 : }
6263 2363243 : peer->m_blocks_for_inv_relay.clear();
6264 2363243 : }
6265 :
6266 2550637 : auto queueAndMaybePushInv = [this, pto, peer, &vInv, &msgMaker](const CInv& invIn) {
6267 187394 : LogPrint(BCLog::NET, "SendMessages -- queued inv: %s index=%d peer=%d\n", invIn.ToString(), vInv.size(), pto->GetId());
6268 : // Responses to MEMPOOL requests bypass the m_recently_announced_invs filter.
6269 187394 : vInv.push_back(invIn);
6270 187394 : if (vInv.size() == MAX_INV_SZ) {
6271 0 : LogPrint(BCLog::NET, "SendMessages -- pushing invs: count=%d peer=%d\n", vInv.size(), pto->GetId());
6272 0 : m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
6273 0 : vInv.clear();
6274 0 : }
6275 187394 : };
6276 :
6277 2363243 : if (auto tx_relay = peer->GetTxRelay(); tx_relay != nullptr) {
6278 2361879 : LOCK(tx_relay->m_tx_inventory_mutex);
6279 : // Check whether periodic sends should happen
6280 : // Note: If this node is a Masternode sending to another Masternode, skip trickling for fast
6281 : // MN-to-MN propagation. However, MN-to-non-MN should still trickle to minimize information leakage
6282 2361879 : const bool peer_is_mn = !pto->GetVerifiedProRegTxHash().IsNull();
6283 2361879 : bool fSendTrickle = pto->HasPermission(NetPermissionFlags::NoBan) || (is_masternode && peer_is_mn);
6284 2361879 : if (tx_relay->m_next_inv_send_time < current_time) {
6285 77717 : fSendTrickle = true;
6286 77717 : if (pto->IsInboundConn()) {
6287 30115 : tx_relay->m_next_inv_send_time = NextInvToInbounds(current_time, INBOUND_INVENTORY_BROADCAST_INTERVAL);
6288 30115 : } else {
6289 : // Use half the delay for Masternode outbound peers, as there is less privacy concern for them.
6290 95204 : tx_relay->m_next_inv_send_time = pto->GetVerifiedProRegTxHash().IsNull() ?
6291 21781 : GetExponentialRand(current_time, OUTBOUND_INVENTORY_BROADCAST_INTERVAL) :
6292 25821 : GetExponentialRand(current_time, OUTBOUND_INVENTORY_BROADCAST_INTERVAL / 2);
6293 : }
6294 77717 : }
6295 :
6296 : // Time to send but the peer has requested we not relay transactions.
6297 2361879 : if (fSendTrickle) {
6298 893322 : LOCK(tx_relay->m_bloom_filter_mutex);
6299 893322 : if (!tx_relay->m_relay_txs) tx_relay->m_tx_inventory_to_send.clear();
6300 893322 : }
6301 :
6302 : // Respond to BIP35 mempool requests
6303 2361879 : if (fSendTrickle && tx_relay->m_send_mempool) {
6304 111 : auto vtxinfo = m_mempool.infoAll();
6305 111 : tx_relay->m_send_mempool = false;
6306 :
6307 111 : LOCK(tx_relay->m_bloom_filter_mutex);
6308 :
6309 : // Send invs for txes and corresponding IS-locks
6310 123 : for (const auto& txinfo : vtxinfo) {
6311 12 : const uint256& hash = txinfo.tx->GetHash();
6312 12 : tx_relay->m_tx_inventory_to_send.erase(hash);
6313 12 : if (tx_relay->m_bloom_filter && !tx_relay->m_bloom_filter->IsRelevantAndUpdate(*txinfo.tx)) continue;
6314 :
6315 12 : int nInvType = m_dstxman.GetDSTX(hash) ? MSG_DSTX : MSG_TX;
6316 12 : tx_relay->m_tx_inventory_known_filter.insert(hash);
6317 12 : queueAndMaybePushInv(CInv(nInvType, hash));
6318 :
6319 12 : const auto islock = m_llmq_ctx->isman->GetInstantSendLockByTxid(hash);
6320 12 : if (islock == nullptr) continue;
6321 0 : uint256 isLockHash{::SerializeHash(*islock)};
6322 0 : tx_relay->m_tx_inventory_known_filter.insert(isLockHash);
6323 0 : if (!PeerReconstructsISLockFromRecsig(*pto, *peer)) {
6324 0 : queueAndMaybePushInv(CInv(MSG_ISDLOCK, isLockHash));
6325 0 : }
6326 12 : }
6327 :
6328 : // Send an inv for the best ChainLock we have
6329 111 : const auto& clsig = m_chainlocks.GetBestChainLock();
6330 111 : if (!clsig.IsNull()) {
6331 0 : uint256 chainlockHash{::SerializeHash(clsig)};
6332 0 : tx_relay->m_tx_inventory_known_filter.insert(chainlockHash);
6333 0 : queueAndMaybePushInv(CInv(MSG_CLSIG, chainlockHash));
6334 0 : }
6335 111 : tx_relay->m_last_mempool_req = std::chrono::duration_cast<std::chrono::seconds>(current_time);
6336 111 : }
6337 :
6338 : // Determine transactions to relay
6339 2361879 : if (fSendTrickle) {
6340 893322 : LOCK(tx_relay->m_bloom_filter_mutex);
6341 :
6342 : // Produce a vector with all candidates for sending
6343 893322 : std::vector<std::set<uint256>::iterator> vInvTx;
6344 893322 : vInvTx.reserve(tx_relay->m_tx_inventory_to_send.size());
6345 939293 : for (std::set<uint256>::iterator it = tx_relay->m_tx_inventory_to_send.begin(); it != tx_relay->m_tx_inventory_to_send.end(); it++) {
6346 45971 : vInvTx.push_back(it);
6347 45971 : }
6348 : // Topologically and fee-rate sort the inventory we send for privacy and priority reasons.
6349 : // A heap is used so that not all items need sorting if only a few are being sent.
6350 893322 : CompareInvMempoolOrder compareInvMempoolOrder(&m_mempool);
6351 893322 : std::make_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
6352 : // No reason to drain out at many times the network's capacity,
6353 : // especially since we have many peers and some will draw much shorter delays.
6354 893322 : unsigned int nRelayedTransactions = 0;
6355 893322 : size_t broadcast_max{INVENTORY_BROADCAST_MAX_PER_1MB_BLOCK * MaxBlockSize() / 1000000 + (tx_relay->m_tx_inventory_to_send.size()/1000)*5};
6356 893322 : broadcast_max = std::min<size_t>(1000, broadcast_max);
6357 :
6358 939293 : while (!vInvTx.empty() && nRelayedTransactions < broadcast_max) {
6359 : // Fetch the top element from the heap
6360 45971 : std::pop_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
6361 45971 : std::set<uint256>::iterator it = vInvTx.back();
6362 45971 : vInvTx.pop_back();
6363 45971 : uint256 hash = *it;
6364 : // Remove it from the to-be-sent set
6365 45971 : tx_relay->m_tx_inventory_to_send.erase(it);
6366 : // Check if not in the filter already
6367 45971 : if (tx_relay->m_tx_inventory_known_filter.contains(hash)) {
6368 8025 : continue;
6369 : }
6370 : // Not in the mempool anymore? don't bother sending it.
6371 37946 : auto txinfo = m_mempool.info(hash);
6372 37946 : if (!txinfo.tx) {
6373 7701 : continue;
6374 : }
6375 30245 : if (tx_relay->m_bloom_filter && !tx_relay->m_bloom_filter->IsRelevantAndUpdate(*txinfo.tx)) continue;
6376 : // Send
6377 30241 : State(pto->GetId())->m_recently_announced_invs.insert(hash);
6378 30241 : nRelayedTransactions++;
6379 : {
6380 : // Expire old relay messages
6381 31308 : while (!g_relay_expiration.empty() && g_relay_expiration.front().first < current_time)
6382 : {
6383 1067 : mapRelay.erase(g_relay_expiration.front().second);
6384 1067 : g_relay_expiration.pop_front();
6385 : }
6386 :
6387 30241 : auto ret = mapRelay.emplace(hash, std::move(txinfo.tx));
6388 30241 : if (ret.second) {
6389 25333 : g_relay_expiration.emplace_back(current_time + RELAY_TX_CACHE_TIME, ret.first);
6390 25333 : }
6391 : }
6392 30241 : int nInvType = m_dstxman.GetDSTX(hash) ? MSG_DSTX : MSG_TX;
6393 30241 : tx_relay->m_tx_inventory_known_filter.insert(hash);
6394 30241 : queueAndMaybePushInv(CInv(nInvType, hash));
6395 37946 : }
6396 893322 : }
6397 2361879 : }
6398 : {
6399 2363243 : auto inv_relay = peer->GetInvRelay();
6400 :
6401 : // Send non-tx/non-block inventory items
6402 2363243 : LOCK2(inv_relay->m_tx_inventory_mutex, inv_relay->m_bloom_filter_mutex);
6403 :
6404 2363243 : bool fSendIS = inv_relay->m_relay_txs && !pto->IsBlockRelayOnly();
6405 :
6406 2552846 : for (const auto& inv : inv_relay->vInventoryOtherToSend) {
6407 189603 : if (!inv_relay->m_relay_txs && NetMessageViolatesBlocksOnly(inv.GetCommand())) {
6408 0 : continue;
6409 : }
6410 189603 : if (inv_relay->m_tx_inventory_known_filter.contains(inv.hash)) {
6411 32462 : continue;
6412 : }
6413 157141 : if (!fSendIS && inv.type == MSG_ISDLOCK) {
6414 0 : continue;
6415 : }
6416 157141 : inv_relay->m_tx_inventory_known_filter.insert(inv.hash);
6417 157141 : queueAndMaybePushInv(inv);
6418 : }
6419 2363243 : inv_relay->vInventoryOtherToSend.clear();
6420 2363243 : }
6421 2363243 : if (!vInv.empty())
6422 144803 : m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
6423 :
6424 : // Detect whether we're stalling
6425 2363243 : auto stalling_timeout = m_block_stalling_timeout.load();
6426 2363243 : if (state.m_stalling_since.count() && state.m_stalling_since < current_time - stalling_timeout) {
6427 : // Stalling only triggers when the block download window cannot move. During normal steady state,
6428 : // the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
6429 : // should only happen during initial block download.
6430 12 : LogPrintf("Peer=%d%s is stalling block download, disconnecting\n", pto->GetId(), fLogIPs ? strprintf(" peeraddr=%s", pto->addr.ToStringAddrPort()) : "");
6431 12 : pto->fDisconnect = true;
6432 : // Increase timeout for the next peer so that we don't disconnect multiple peers if our own
6433 : // bandwidth is insufficient.
6434 12 : const auto new_timeout = std::min(2 * stalling_timeout, BLOCK_STALLING_TIMEOUT_MAX);
6435 12 : if (stalling_timeout != new_timeout && m_block_stalling_timeout.compare_exchange_strong(stalling_timeout, new_timeout)) {
6436 12 : LogPrint(BCLog::NET, "Increased stalling timeout temporarily to %d seconds\n", count_seconds(new_timeout));
6437 12 : }
6438 12 : return true;
6439 : }
6440 : // In case there is a block that has been in flight from this peer for block_interval * (1 + 0.5 * N)
6441 : // (with N the number of peers from which we're downloading validated blocks), disconnect due to timeout.
6442 : // We compensate for other peers to prevent killing off peers due to our own downstream link
6443 : // being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
6444 : // to unreasonably increase our timeout.
6445 2363231 : if (state.vBlocksInFlight.size() > 0) {
6446 141210 : QueuedBlock &queuedBlock = state.vBlocksInFlight.front();
6447 141210 : int nOtherPeersWithValidatedDownloads = m_peers_downloading_from - 1;
6448 141210 : if (current_time > state.m_downloading_since + std::chrono::seconds{consensusParams.nPowTargetSpacing} * (BLOCK_DOWNLOAD_TIMEOUT_BASE + BLOCK_DOWNLOAD_TIMEOUT_PER_PEER * nOtherPeersWithValidatedDownloads)) {
6449 0 : LogPrintf("Timeout downloading block %s from peer=%d%s, disconnecting\n", queuedBlock.pindex->GetBlockHash().ToString(), pto->GetId(), fLogIPs ? strprintf(" peeraddr=%s", pto->addr.ToStringAddrPort()) : "");
6450 0 : pto->fDisconnect = true;
6451 0 : return true;
6452 : }
6453 141210 : }
6454 : // Check for headers sync timeouts
6455 2363231 : if (state.fSyncStarted && state.m_headers_sync_timeout < std::chrono::microseconds::max()) {
6456 : // Detect whether this is a stalling initial-headers-sync peer
6457 21930 : if (m_chainman.m_best_header->GetBlockTime() <= GetAdjustedTime() - nMaxTipAge) {
6458 14019 : if (current_time > state.m_headers_sync_timeout && nSyncStarted == 1 && (m_num_preferred_download_peers - state.fPreferredDownload >= 1)) {
6459 : // Disconnect a peer (without NetPermissionFlags::NoBan permission) if it is our only sync peer,
6460 : // and we have others we could be using instead.
6461 : // Note: If all our peers are inbound, then we won't
6462 : // disconnect our sync peer for stalling; we have bigger
6463 : // problems if we can't get any outbound peers.
6464 0 : if (!pto->HasPermission(NetPermissionFlags::NoBan)) {
6465 0 : LogPrintf("Timeout downloading headers from peer=%d%s, disconnecting\n", pto->GetId(), fLogIPs ? strprintf(" peeraddr=%s", pto->addr.ToStringAddrPort()) : "");
6466 0 : pto->fDisconnect = true;
6467 0 : return true;
6468 : } else {
6469 0 : LogPrintf("Timeout downloading headers from noban peer=%d%s, not disconnecting\n", pto->GetId(), fLogIPs ? strprintf(" peeraddr=%s", pto->addr.ToStringAddrPort()) : "");
6470 : // Reset the headers sync state so that we have a
6471 : // chance to try downloading from a different peer.
6472 : // Note: this will also result in at least one more
6473 : // getheaders message to be sent to
6474 : // this peer (eventually).
6475 0 : state.fSyncStarted = false;
6476 0 : nSyncStarted--;
6477 0 : state.m_headers_sync_timeout = 0us;
6478 : }
6479 0 : }
6480 14019 : } else {
6481 : // After we've caught up once, reset the timeout so we can't trigger
6482 : // disconnect later.
6483 7911 : state.m_headers_sync_timeout = std::chrono::microseconds::max();
6484 : }
6485 21930 : }
6486 :
6487 : // Check that outbound peers have reasonable chains
6488 : // GetTime() is used by this anti-DoS logic so we can test this using mocktime
6489 2363231 : ConsiderEviction(*pto, *peer, GetTime<std::chrono::seconds>());
6490 :
6491 : //
6492 : // Message: getdata (blocks)
6493 : //
6494 2363231 : std::vector<CInv> vGetData;
6495 2363231 : if (CanServeBlocks(*peer) && pto->CanRelay() && ((sync_blocks_and_headers_from_peer && !IsLimitedPeer(*peer)) || !m_chainman.ActiveChainstate().IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
6496 2298581 : std::vector<const CBlockIndex*> vToDownload;
6497 2298581 : NodeId staller = -1;
6498 2298581 : FindNextBlocksToDownload(*peer, MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller);
6499 2331819 : for (const CBlockIndex *pindex : vToDownload) {
6500 33238 : vGetData.emplace_back(MSG_BLOCK, pindex->GetBlockHash());
6501 33238 : BlockRequested(pto->GetId(), *pindex);
6502 33238 : LogPrint(BCLog::NET, "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
6503 : pindex->nHeight, pto->GetId());
6504 : }
6505 2298581 : if (state.nBlocksInFlight == 0 && staller != -1) {
6506 382 : if (State(staller)->m_stalling_since == 0us) {
6507 12 : State(staller)->m_stalling_since = current_time;
6508 12 : LogPrint(BCLog::NET, "Stall started peer=%d\n", staller);
6509 12 : }
6510 382 : }
6511 2298581 : }
6512 :
6513 : //
6514 : // Message: getdata (non-blocks)
6515 : //
6516 :
6517 : // For robustness, expire old requests after a long timeout, so that
6518 : // we can resume downloading objects from a peer even if they
6519 : // were unresponsive in the past.
6520 : // Eventually we should consider disconnecting peers, but this is
6521 : // conservative.
6522 2363231 : if (state.m_object_download.m_check_expiry_timer <= current_time) {
6523 16053 : for (auto it=state.m_object_download.m_object_in_flight.begin(); it != state.m_object_download.m_object_in_flight.end();) {
6524 321 : if (it->second <= current_time - GetObjectExpiryInterval(it->first.type)) {
6525 305 : LogPrint(BCLog::NET, "timeout of inflight object %s from peer=%d\n", it->first.ToString(), pto->GetId());
6526 305 : state.m_object_download.m_object_announced.erase(it->first);
6527 305 : state.m_object_download.m_object_in_flight.erase(it++);
6528 305 : } else {
6529 16 : ++it;
6530 : }
6531 : }
6532 : // On average, we do this check every GetObjectExpiryInterval. Randomize
6533 : // so that we're not doing this for all peers at the same time.
6534 15732 : state.m_object_download.m_check_expiry_timer = current_time + GetObjectExpiryInterval(MSG_TX)/2 + GetRandMicros(GetObjectExpiryInterval(MSG_TX));
6535 15732 : }
6536 :
6537 : // DASH this code also handles non-TXs (Dash specific messages)
6538 2363231 : auto& object_process_time = state.m_object_download.m_object_process_time;
6539 2440686 : while (!object_process_time.empty() && object_process_time.begin()->first <= current_time && state.m_object_download.m_object_in_flight.size() < MAX_PEER_OBJECT_IN_FLIGHT) {
6540 77455 : const CInv inv = object_process_time.begin()->second;
6541 : // Erase this entry from object_process_time (it may be added back for
6542 : // processing at a later time, see below)
6543 77455 : object_process_time.erase(object_process_time.begin());
6544 77455 : if (g_erased_object_requests.count(inv.hash)) {
6545 21995 : LogPrint(BCLog::NET, "%s -- GETDATA skipping inv=(%s), peer=%d\n", __func__, inv.ToString(), pto->GetId());
6546 21995 : state.m_object_download.m_object_announced.erase(inv);
6547 21995 : state.m_object_download.m_object_in_flight.erase(inv);
6548 21995 : continue;
6549 : }
6550 55460 : if (!AlreadyHave(inv)) {
6551 : // If this object was last requested more than GetObjectInterval ago,
6552 : // then request.
6553 54545 : const auto last_request_time = GetObjectRequestTime(inv);
6554 54545 : if (last_request_time <= current_time - GetObjectInterval(inv.type)) {
6555 51696 : LogPrint(BCLog::NET, "Requesting %s peer=%d\n", inv.ToString(), pto->GetId());
6556 51696 : vGetData.push_back(inv);
6557 51696 : if (vGetData.size() >= MAX_GETDATA_SZ) {
6558 0 : m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
6559 0 : vGetData.clear();
6560 0 : }
6561 51696 : UpdateObjectRequestTime(inv, current_time);
6562 51696 : state.m_object_download.m_object_in_flight.emplace(inv, current_time);
6563 51696 : } else {
6564 : // This object is in flight from someone else; queue
6565 : // up processing to happen after the download times out
6566 : // (with a slight delay for inbound peers, to prefer
6567 : // requests to outbound peers).
6568 2849 : const auto next_process_time = CalculateObjectGetDataTime(inv, current_time, is_masternode, !state.fPreferredDownload);
6569 2849 : object_process_time.emplace(next_process_time, inv);
6570 2849 : LogPrint(BCLog::NET, "%s -- GETDATA re-queue inv=(%s), next_process_time=%d, delta=%d, peer=%d\n", __func__, inv.ToString(), next_process_time.count(), (next_process_time - current_time).count(), pto->GetId());
6571 : }
6572 54545 : } else {
6573 : // We have already seen this object, no need to download.
6574 915 : state.m_object_download.m_object_announced.erase(inv);
6575 915 : state.m_object_download.m_object_in_flight.erase(inv);
6576 915 : LogPrint(BCLog::NET, "%s -- GETDATA already seen inv=(%s), peer=%d\n", __func__, inv.ToString(), pto->GetId());
6577 : }
6578 : }
6579 :
6580 :
6581 2363231 : if (!vGetData.empty()) {
6582 71438 : m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
6583 71438 : LogPrint(BCLog::NET, "SendMessages -- GETDATA -- pushed size = %lu peer=%d\n", vGetData.size(), pto->GetId());
6584 71438 : }
6585 2363243 : } // release cs_main
6586 2363231 : return true;
6587 2375425 : }
6588 :
6589 169 : void PeerManagerImpl::PeerMisbehaving(const NodeId pnode, const int howmuch, const std::string& message)
6590 : {
6591 169 : Misbehaving(pnode, howmuch, message);
6592 169 : }
6593 :
6594 277161 : bool PeerManagerImpl::PeerIsBanned(const NodeId node_id)
6595 : {
6596 277161 : return IsBanned(node_id);
6597 : }
6598 :
6599 48428 : void PeerManagerImpl::PeerEraseObjectRequest(const NodeId nodeid, const CInv& inv)
6600 : {
6601 48428 : EraseObjectRequest(nodeid, inv);
6602 48428 : }
6603 :
6604 34376 : void PeerManagerImpl::PeerPushInventory(NodeId nodeid, const CInv& inv)
6605 : {
6606 34376 : PushInventory(nodeid, inv);
6607 34376 : }
6608 :
6609 8277 : void PeerManagerImpl::PeerRelayInv(const CInv& inv)
6610 : {
6611 8277 : RelayInv(inv);
6612 8277 : }
6613 :
6614 820 : void PeerManagerImpl::PeerRelayInvFiltered(const CInv& inv, const CTransaction& relatedTx)
6615 : {
6616 820 : RelayInvFiltered(inv, relatedTx);
6617 820 : }
6618 :
6619 31 : void PeerManagerImpl::PeerRelayInvFiltered(const CInv& inv, const uint256& relatedTxHash)
6620 : {
6621 31 : RelayInvFiltered(inv, relatedTxHash);
6622 31 : }
6623 :
6624 0 : void PeerManagerImpl::PeerRelayDSQ(const CCoinJoinQueue& queue)
6625 : {
6626 0 : RelayDSQ(queue);
6627 0 : }
6628 :
6629 188 : void PeerManagerImpl::PeerRelayTransaction(const uint256& txid)
6630 : {
6631 188 : RelayTransaction(txid);
6632 188 : }
6633 :
6634 31 : void PeerManagerImpl::PeerAskPeersForTransaction(const uint256& txid)
6635 : {
6636 31 : AskPeersForTransaction(txid);
6637 31 : }
6638 :
6639 9448 : size_t PeerManagerImpl::PeerGetRequestedObjectCount(NodeId nodeid) const
6640 : {
6641 9448 : return GetRequestedObjectCount(nodeid);
6642 : }
6643 :
6644 0 : void PeerManagerImpl::PeerPostProcessMessage(MessageProcessingResult&& ret)
6645 : {
6646 0 : PostProcessMessage(std::move(ret), /*node=*/-1);
6647 0 : }
6648 :
6649 27204 : void PeerManagerImpl::PeerRelayRecoveredSig(const llmq::CRecoveredSig& sig, bool proactive_relay)
6650 : {
6651 27204 : RelayRecoveredSig(sig, proactive_relay);
6652 27204 : }
|