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