Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2021 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 : #include <version.h> 12 : 13 : #include <msg_result.h> 14 : 15 : #include <atomic> 16 : 17 : class AddrMan; 18 : class CActiveMasternodeManager; 19 : class CCoinJoinQueue; 20 : class CDeterministicMNManager; 21 : class CDSTXManager; 22 : class ChainstateManager; 23 : class CInv; 24 : class CJWalletManager; 25 : class CMasternodeMetaMan; 26 : class CMasternodeSync; 27 : class CNetMsgMaker; 28 : class CSporkManager; 29 : class CTransaction; 30 : class CTxMemPool; 31 : struct LLMQContext; 32 : namespace chainlock { 33 : class Chainlocks; 34 : class ChainlockHandler; 35 : } // namespace chainlock 36 : 37 : /** Default for -maxorphantxsize, maximum size in megabytes the orphan map can grow before entries are removed */ 38 : static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS_SIZE = 10; // this allows around 100 TXs of max size (and many more of normal size) 39 : /** Default number of orphan+recently-replaced txn to keep around for block reconstruction */ 40 : static const unsigned int DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN = 100; 41 : static const bool DEFAULT_PEERBLOOMFILTERS = true; 42 : static const bool DEFAULT_PEERBLOCKFILTERS = false; 43 : /** Threshold for marking a node to be discouraged, e.g. disconnected and added to the discouragement filter. */ 44 : static const int DISCOURAGEMENT_THRESHOLD{100}; 45 : 46 172280 : struct CNodeStateStats { 47 86140 : int m_misbehavior_score = 0; 48 86140 : int nSyncHeight = -1; 49 86140 : int nCommonHeight = -1; 50 86140 : int m_starting_height = -1; 51 : std::chrono::microseconds m_ping_wait; 52 : std::vector<int> vHeightInFlight; 53 : bool m_relay_txs; 54 86140 : uint64_t m_addr_processed = 0; 55 86140 : uint64_t m_addr_rate_limited = 0; 56 86140 : bool m_addr_relay_enabled{false}; 57 : ServiceFlags their_services; 58 : }; 59 : 60 : class PeerManagerInternal 61 : { 62 : public: 63 : virtual void PeerMisbehaving(const NodeId pnode, const int howmuch, const std::string& message = "") = 0; 64 : virtual bool PeerIsBanned(const NodeId node_id) = 0; 65 : virtual void PeerEraseObjectRequest(const NodeId nodeid, const CInv& inv) = 0; 66 : virtual void PeerPushInventory(NodeId nodeid, const CInv& inv) = 0; 67 : virtual void PeerRelayInv(const CInv& inv) = 0; 68 : virtual void PeerRelayInvFiltered(const CInv& inv, const CTransaction& relatedTx) = 0; 69 : virtual void PeerRelayInvFiltered(const CInv& inv, const uint256& relatedTxHash) = 0; 70 : virtual void PeerRelayTransaction(const uint256& txid) = 0; 71 : virtual void PeerRelayDSQ(const CCoinJoinQueue& queue) = 0; 72 : virtual void PeerRelayRecoveredSig(const llmq::CRecoveredSig& sig, bool proactive_relay) = 0; 73 : virtual void PeerAskPeersForTransaction(const uint256& txid) = 0; 74 : virtual size_t PeerGetRequestedObjectCount(NodeId nodeid) const = 0; 75 : virtual void PeerPostProcessMessage(MessageProcessingResult&& ret) = 0; 76 : }; 77 : 78 : class NetHandler 79 : { 80 : public: 81 17802 : NetHandler(PeerManagerInternal* peer_manager) : m_peer_manager{Assert(peer_manager)} {} 82 17802 : virtual ~NetHandler() { 83 17802 : Interrupt(); 84 17802 : Stop(); 85 17802 : } 86 : 87 8487 : virtual void Start() {} 88 34932 : virtual void Stop() {} 89 34932 : virtual void Interrupt() {} 90 11324 : virtual void Schedule(CScheduler& scheduler) {} 91 : 92 0 : virtual void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv) {} 93 : 94 : // It returns true, if NetHandler has a responsibility about having this type of inventory and has corresponding data. 95 166343 : virtual bool AlreadyHave(const CInv& inv) { return false; } 96 : 97 : // It should return true, if there's data has been pushed 98 47501 : virtual bool ProcessGetData(CNode& pfrom, const CInv& inv, CConnman& connman, const CNetMsgMaker& msgMaker) { return false; } 99 : protected: 100 : PeerManagerInternal* m_peer_manager; 101 : }; 102 : 103 : 104 : class PeerManager : public CValidationInterface, public NetEventsInterface, public PeerManagerInternal 105 : { 106 : public: 107 : /** 108 : * @param nodeman Non-null iff masternode mode is on; null otherwise. Used both 109 : * as the masternode-mode indicator and for direct access. 110 : */ 111 : static std::unique_ptr<PeerManager> make(const CChainParams& chainparams, CConnman& connman, AddrMan& addrman, 112 : BanMan* banman, CDSTXManager& dstxman, ChainstateManager& chainman, 113 : CTxMemPool& pool, CMasternodeMetaMan& mn_metaman, CMasternodeSync& mn_sync, 114 : CSporkManager& sporkman, 115 : const chainlock::Chainlocks& chainlocks, 116 : chainlock::ChainlockHandler& clhandler, 117 : CActiveMasternodeManager* nodeman, 118 : const std::unique_ptr<CDeterministicMNManager>& dmnman, 119 : const std::unique_ptr<CJWalletManager>& cj_walletman, 120 : const std::unique_ptr<LLMQContext>& llmq_ctx, bool ignore_incoming_txs); 121 3040 : virtual ~PeerManager() { } 122 : 123 : /** 124 : * Attempt to manually fetch block from a given peer. We must already have the header. 125 : * 126 : * @param[in] peer_id The peer id 127 : * @param[in] block_index The blockindex 128 : * @returns std::nullopt if a request was successfully made, otherwise an error message 129 : */ 130 : virtual std::optional<std::string> FetchBlock(NodeId peer_id, const CBlockIndex& block_index) = 0; 131 : 132 : /** Begin running background tasks, should only be called once */ 133 : virtual void StartScheduledTasks(CScheduler& scheduler) = 0; 134 : 135 : /** Get statistics from node state */ 136 : virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const = 0; 137 : 138 : /** Whether this node ignores txs received over p2p. */ 139 : virtual bool IgnoresIncomingTxs() = 0; 140 : 141 : /** Send ping message to all peers */ 142 : virtual void SendPings() = 0; 143 : 144 : /** Broadcast inventory message to a specific peer. */ 145 : virtual void PushInventory(NodeId nodeid, const CInv& inv) = 0; 146 : 147 : /** Relay DSQ based on peer preference */ 148 : virtual void RelayDSQ(const CCoinJoinQueue& queue) = 0; 149 : 150 : /** Relay inventories to all peers */ 151 : virtual void RelayInv(const CInv& inv) = 0; 152 : virtual void RelayInv(const CInv& inv, const int minProtoVersion) = 0; 153 : 154 : /** Relay transaction to all peers. */ 155 : virtual void RelayTransaction(const uint256& txid) = 0; 156 : 157 : /** Relay recovered sigs to all interested peers */ 158 : virtual void RelayRecoveredSig(const llmq::CRecoveredSig& sig, bool proactive_relay) = 0; 159 : 160 : /** Set the best height */ 161 : virtual void SetBestHeight(int height) = 0; 162 : 163 : /** 164 : * Increment peer's misbehavior score. If the new value surpasses DISCOURAGEMENT_THRESHOLD (specified on startup or by default), mark node to be discouraged, meaning the peer might be disconnected & added to the discouragement filter. 165 : */ 166 : virtual void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message = "") = 0; 167 : 168 : /** 169 : * Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound. 170 : * Public for unit testing. 171 : */ 172 : virtual void CheckForStaleTipAndEvictPeers() = 0; 173 : 174 : /** Process a single message from a peer. Public for fuzz testing */ 175 : virtual void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv, 176 : const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) = 0; 177 : 178 : /** Finish message processing. Used for some specific messages */ 179 : virtual void PostProcessMessage(MessageProcessingResult&& ret, NodeId node = -1) = 0; 180 : 181 : /** This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp */ 182 : virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) = 0; 183 : 184 : virtual bool IsBanned(NodeId pnode) = 0; 185 : 186 : virtual size_t GetRequestedObjectCount(NodeId nodeid) const = 0; 187 : 188 : virtual void AddExtraHandler(std::unique_ptr<NetHandler>&& handler) = 0; 189 : virtual void RemoveHandlers() = 0; 190 : virtual void StartHandlers() = 0; 191 : virtual void StopHandlers() = 0; 192 : virtual void InterruptHandlers() = 0; 193 : virtual void ScheduleHandlers(CScheduler& scheduler) = 0; 194 : }; 195 : 196 : #endif // BITCOIN_NET_PROCESSING_H