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