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