/Users/eugenesiegel/btc/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 <protocol.h> |
12 | | #include <threadsafety.h> |
13 | | #include <txorphanage.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 for -maxorphantx, maximum number of orphan transactions kept in memory */ |
40 | | static const uint32_t DEFAULT_MAX_ORPHAN_TRANSACTIONS{100}; |
41 | | /** Default number of non-mempool transactions to keep around for block reconstruction. Includes |
42 | | orphan, replaced, and rejected transactions. */ |
43 | | static const uint32_t DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN{100}; |
44 | | static const bool DEFAULT_PEERBLOOMFILTERS = false; |
45 | | static const bool DEFAULT_PEERBLOCKFILTERS = false; |
46 | | /** Maximum number of outstanding CMPCTBLOCK requests for the same block. */ |
47 | | static const unsigned int MAX_CMPCTBLOCKS_INFLIGHT_PER_BLOCK = 3; |
48 | | /** Number of headers sent in one getheaders result. We rely on the assumption that if a peer sends |
49 | | * less than this number, we reached its tip. Changing this value is a protocol upgrade. */ |
50 | | static const unsigned int MAX_HEADERS_RESULTS = 2000; |
51 | | /** The compactblocks version we support. See BIP 152. */ |
52 | | static constexpr uint64_t CMPCTBLOCKS_VERSION{2}; |
53 | | |
54 | | struct CNodeStateStats { |
55 | | int nSyncHeight = -1; |
56 | | int nCommonHeight = -1; |
57 | | int m_starting_height = -1; |
58 | | std::chrono::microseconds m_ping_wait; |
59 | | std::vector<int> vHeightInFlight; |
60 | | bool m_relay_txs; |
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 | | //! Maximum number of orphan transactions kept in memory |
84 | | uint32_t max_orphan_txs{DEFAULT_MAX_ORPHAN_TRANSACTIONS}; |
85 | | //! Number of non-mempool transactions to keep around for block reconstruction. Includes |
86 | | //! orphan, replaced, and rejected transactions. |
87 | | uint32_t max_extra_txs{DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN}; |
88 | | //! Whether all P2P messages are captured to disk |
89 | | bool capture_messages{false}; |
90 | | //! Whether or not the internal RNG behaves deterministically (this is |
91 | | //! a test-only option). |
92 | | bool deterministic_rng{false}; |
93 | | //! Number of headers sent in one getheaders message result (this is |
94 | | //! a test-only option). |
95 | | uint32_t max_headers_result{MAX_HEADERS_RESULTS}; |
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 | 7.28k | 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 | | * @returns std::nullopt if a request was successfully made, otherwise an error message |
109 | | */ |
110 | | virtual std::optional<std::string> FetchBlock(NodeId peer_id, const CBlockIndex& block_index) = 0; |
111 | | |
112 | | /** Begin running background tasks, should only be called once */ |
113 | | virtual void StartScheduledTasks(CScheduler& scheduler) = 0; |
114 | | |
115 | | /** Get statistics from node state */ |
116 | | virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const = 0; |
117 | | |
118 | | virtual std::vector<TxOrphanage::OrphanTxBase> GetOrphanTransactions() = 0; |
119 | | |
120 | | /** Get peer manager info. */ |
121 | | virtual PeerManagerInfo GetInfo() const = 0; |
122 | | |
123 | | /** Relay transaction to all peers. */ |
124 | | virtual void RelayTransaction(const uint256& txid, const uint256& wtxid) = 0; |
125 | | |
126 | | /** Send ping message to all peers */ |
127 | | virtual void SendPings() = 0; |
128 | | |
129 | | /** Set the height of the best block and its time (seconds since epoch). */ |
130 | | virtual void SetBestBlock(int height, std::chrono::seconds time) = 0; |
131 | | |
132 | | /* Public for unit testing. */ |
133 | | virtual void UnitTestMisbehaving(NodeId peer_id) = 0; |
134 | | |
135 | | /** |
136 | | * Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound. |
137 | | * Public for unit testing. |
138 | | */ |
139 | | virtual void CheckForStaleTipAndEvictPeers() = 0; |
140 | | |
141 | | /** Process a single message from a peer. Public for fuzz testing */ |
142 | | virtual void ProcessMessage(CNode& pfrom, const std::string& msg_type, DataStream& vRecv, |
143 | | const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) = 0; |
144 | | |
145 | | /** This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp */ |
146 | | virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) = 0; |
147 | | |
148 | | /** |
149 | | * Gets the set of service flags which are "desirable" for a given peer. |
150 | | * |
151 | | * These are the flags which are required for a peer to support for them |
152 | | * to be "interesting" to us, ie for us to wish to use one of our few |
153 | | * outbound connection slots for or for us to wish to prioritize keeping |
154 | | * their connection around. |
155 | | * |
156 | | * Relevant service flags may be peer- and state-specific in that the |
157 | | * version of the peer may determine which flags are required (eg in the |
158 | | * case of NODE_NETWORK_LIMITED where we seek out NODE_NETWORK peers |
159 | | * unless they set NODE_NETWORK_LIMITED and we are out of IBD, in which |
160 | | * case NODE_NETWORK_LIMITED suffices). |
161 | | * |
162 | | * Thus, generally, avoid calling with 'services' == NODE_NONE, unless |
163 | | * state-specific flags must absolutely be avoided. When called with |
164 | | * 'services' == NODE_NONE, the returned desirable service flags are |
165 | | * guaranteed to not change dependent on state - ie they are suitable for |
166 | | * use when describing peers which we know to be desirable, but for which |
167 | | * we do not have a confirmed set of service flags. |
168 | | */ |
169 | | virtual ServiceFlags GetDesirableServiceFlags(ServiceFlags services) const = 0; |
170 | | }; |
171 | | |
172 | | #endif // BITCOIN_NET_PROCESSING_H |