Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-present The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 : :
6 : : #ifndef BITCOIN_NET_PROCESSING_H
7 : : #define BITCOIN_NET_PROCESSING_H
8 : :
9 : : #include <consensus/amount.h>
10 : : #include <net.h>
11 : : #include <node/txorphanage.h>
12 : : #include <protocol.h>
13 : : #include <threadsafety.h>
14 : : #include <validationinterface.h>
15 : :
16 : : #include <atomic>
17 : : #include <chrono>
18 : : #include <cstdint>
19 : : #include <memory>
20 : : #include <optional>
21 : : #include <string>
22 : : #include <vector>
23 : :
24 : : class AddrMan;
25 : : class CTxMemPool;
26 : : class ChainstateManager;
27 : : class BanMan;
28 : : class CBlockIndex;
29 : : class CScheduler;
30 : : class DataStream;
31 : : class uint256;
32 : :
33 : : namespace node {
34 : : class Warnings;
35 : : } // namespace node
36 : :
37 : : /** Whether transaction reconciliation protocol should be enabled by default. */
38 : : static constexpr bool DEFAULT_TXRECONCILIATION_ENABLE{false};
39 : : /** Default number of non-mempool transactions to keep around for block reconstruction. Includes
40 : : orphan, replaced, and rejected transactions. */
41 : : static const uint32_t DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN{100};
42 : : static const bool DEFAULT_PEERBLOOMFILTERS = false;
43 : : static const bool DEFAULT_PEERBLOCKFILTERS = false;
44 : : /** Maximum number of outstanding CMPCTBLOCK requests for the same block. */
45 : : static const unsigned int MAX_CMPCTBLOCKS_INFLIGHT_PER_BLOCK = 3;
46 : : /** Number of headers sent in one getheaders result. We rely on the assumption that if a peer sends
47 : : * less than this number, we reached its tip. Changing this value is a protocol upgrade. */
48 : : static const unsigned int MAX_HEADERS_RESULTS = 2000;
49 : :
50 [ + - ]: 30452 : struct CNodeStateStats {
51 : : int nSyncHeight = -1;
52 : : int nCommonHeight = -1;
53 : : int m_starting_height = -1;
54 : : std::chrono::microseconds m_ping_wait;
55 : : std::vector<int> vHeightInFlight;
56 : : bool m_relay_txs;
57 : : CAmount m_fee_filter_received;
58 : : uint64_t m_addr_processed = 0;
59 : : uint64_t m_addr_rate_limited = 0;
60 : : bool m_addr_relay_enabled{false};
61 : : ServiceFlags their_services;
62 : : int64_t presync_height{-1};
63 : : std::chrono::seconds time_offset{0};
64 : : };
65 : :
66 : : struct PeerManagerInfo {
67 : : std::chrono::seconds median_outbound_time_offset{0s};
68 : : bool ignores_incoming_txs{false};
69 : : };
70 : :
71 : 12071 : class PeerManager : public CValidationInterface, public NetEventsInterface
72 : : {
73 : : public:
74 : : struct Options {
75 : : //! Whether this node is running in -blocksonly mode
76 : : bool ignore_incoming_txs{DEFAULT_BLOCKSONLY};
77 : : //! Whether transaction reconciliation protocol is enabled
78 : : bool reconcile_txs{DEFAULT_TXRECONCILIATION_ENABLE};
79 : : //! Number of non-mempool transactions to keep around for block reconstruction. Includes
80 : : //! orphan, replaced, and rejected transactions.
81 : : uint32_t max_extra_txs{DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN};
82 : : //! Whether all P2P messages are captured to disk
83 : : bool capture_messages{false};
84 : : //! Whether or not the internal RNG behaves deterministically (this is
85 : : //! a test-only option).
86 : : bool deterministic_rng{false};
87 : : //! Number of headers sent in one getheaders message result (this is
88 : : //! a test-only option).
89 : : uint32_t max_headers_result{MAX_HEADERS_RESULTS};
90 : : };
91 : :
92 : : static std::unique_ptr<PeerManager> make(CConnman& connman, AddrMan& addrman,
93 : : BanMan* banman, ChainstateManager& chainman,
94 : : CTxMemPool& pool, node::Warnings& warnings, Options opts);
95 : 0 : virtual ~PeerManager() = default;
96 : :
97 : : /**
98 : : * Attempt to manually fetch block from a given peer. We must already have the header.
99 : : *
100 : : * @param[in] peer_id The peer id
101 : : * @param[in] block_index The blockindex
102 : : * @returns std::nullopt if a request was successfully made, otherwise an error message
103 : : */
104 : : virtual std::optional<std::string> FetchBlock(NodeId peer_id, const CBlockIndex& block_index) = 0;
105 : :
106 : : /** Begin running background tasks, should only be called once */
107 : : virtual void StartScheduledTasks(CScheduler& scheduler) = 0;
108 : :
109 : : /** Get statistics from node state */
110 : : virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const = 0;
111 : :
112 : : virtual std::vector<node::TxOrphanage::OrphanTxBase> GetOrphanTransactions() = 0;
113 : :
114 : : /** Get peer manager info. */
115 : : virtual PeerManagerInfo GetInfo() const = 0;
116 : :
117 : : /** Relay transaction to all peers. */
118 : : virtual void RelayTransaction(const Txid& txid, const Wtxid& wtxid) = 0;
119 : :
120 : : /** Send ping message to all peers */
121 : : virtual void SendPings() = 0;
122 : :
123 : : /** Set the height of the best block and its time (seconds since epoch). */
124 : : virtual void SetBestBlock(int height, std::chrono::seconds time) = 0;
125 : :
126 : : /* Public for unit testing. */
127 : : virtual void UnitTestMisbehaving(NodeId peer_id) = 0;
128 : :
129 : : /**
130 : : * Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound.
131 : : * Public for unit testing.
132 : : */
133 : : virtual void CheckForStaleTipAndEvictPeers() = 0;
134 : :
135 : : /** Process a single message from a peer. Public for fuzz testing */
136 : : virtual void ProcessMessage(CNode& pfrom, const std::string& msg_type, DataStream& vRecv,
137 : : const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) = 0;
138 : :
139 : : /** This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp */
140 : : virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) = 0;
141 : :
142 : : /**
143 : : * Gets the set of service flags which are "desirable" for a given peer.
144 : : *
145 : : * These are the flags which are required for a peer to support for them
146 : : * to be "interesting" to us, ie for us to wish to use one of our few
147 : : * outbound connection slots for or for us to wish to prioritize keeping
148 : : * their connection around.
149 : : *
150 : : * Relevant service flags may be peer- and state-specific in that the
151 : : * version of the peer may determine which flags are required (eg in the
152 : : * case of NODE_NETWORK_LIMITED where we seek out NODE_NETWORK peers
153 : : * unless they set NODE_NETWORK_LIMITED and we are out of IBD, in which
154 : : * case NODE_NETWORK_LIMITED suffices).
155 : : *
156 : : * Thus, generally, avoid calling with 'services' == NODE_NONE, unless
157 : : * state-specific flags must absolutely be avoided. When called with
158 : : * 'services' == NODE_NONE, the returned desirable service flags are
159 : : * guaranteed to not change dependent on state - ie they are suitable for
160 : : * use when describing peers which we know to be desirable, but for which
161 : : * we do not have a confirmed set of service flags.
162 : : */
163 : : virtual ServiceFlags GetDesirableServiceFlags(ServiceFlags services) const = 0;
164 : : };
165 : :
166 : : #endif // BITCOIN_NET_PROCESSING_H
|