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