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