Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2015 The Bitcoin Core developers 3 : // Copyright (c) 2015-2021 The PIVX Core developers 4 : // Distributed under the MIT software license, see the accompanying 5 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 : 7 : #ifndef PIVX_NET_PROCESSING_H 8 : #define PIVX_NET_PROCESSING_H 9 : 10 : #include "net.h" 11 : #include "validationinterface.h" 12 : 13 : extern RecursiveMutex cs_main; // !TODO: change mutex to cs_orphans 14 : 15 : /** Default for -maxorphantx, maximum number of orphan transactions kept in memory */ 16 : static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 25; 17 : /** Expiration time for orphan transactions in seconds */ 18 : static const int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60; 19 : /** Minimum time between orphan transactions expire time checks in seconds */ 20 : static const int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60; 21 : /** Default for -blockspamfilter, use header spam filter */ 22 : static const bool DEFAULT_BLOCK_SPAM_FILTER = true; 23 : /** Default for -blockspamfiltermaxsize, maximum size of the list of indexes in the block spam filter */ 24 : static const unsigned int DEFAULT_BLOCK_SPAM_FILTER_MAX_SIZE = 100; 25 : /** Default for -blockspamfiltermaxavg, maximum average size of an index occurrence in the block spam filter */ 26 : static const unsigned int DEFAULT_BLOCK_SPAM_FILTER_MAX_AVG = 10; 27 : 28 : /** Average delay between trickled inventory transmissions in seconds. 29 : * Blocks and whitelisted receivers bypass this, outbound peers get half this delay. */ 30 : static const unsigned int INVENTORY_BROADCAST_INTERVAL = 5; 31 : /** Maximum number of inventory items to send per transmission. 32 : * Limits the impact of low-fee transaction floods. */ 33 : static const unsigned int INVENTORY_BROADCAST_MAX = 7 * INVENTORY_BROADCAST_INTERVAL; 34 : 35 : class PeerLogicValidation : public CValidationInterface, public NetEventsInterface { 36 : private: 37 : CConnman* connman; 38 : 39 : public: 40 : explicit PeerLogicValidation(CConnman* connman); 41 484 : ~PeerLogicValidation() = default; 42 : 43 : void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex) override; 44 : void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override; 45 : void BlockChecked(const CBlock& block, const CValidationState& state) override; 46 : 47 : 48 : void InitializeNode(CNode* pnode) override; 49 : void FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) override; 50 : /** Process protocol messages received from a given node */ 51 : bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override; 52 : /** 53 : * Send queued protocol messages to be sent to a give node. 54 : * 55 : * @param[in] pto The node which we are sending messages to. 56 : * @param[in] interrupt Interrupt condition for processing threads 57 : * @return True if there is more work to be done 58 : */ 59 : bool SendMessages(CNode* pto, std::atomic<bool>& interrupt) override EXCLUSIVE_LOCKS_REQUIRED(pto->cs_sendProcessing); 60 : }; 61 : 62 122746 : struct CNodeStateStats { 63 : int nMisbehavior; 64 : int nSyncHeight; 65 : int nCommonHeight; 66 : std::vector<int> vHeightInFlight; 67 : uint64_t m_addr_processed = 0; 68 : uint64_t m_addr_rate_limited = 0; 69 : }; 70 : 71 : /** Get statistics from node state */ 72 : bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats); 73 : /** Increase a node's misbehavior score. */ 74 : void Misbehaving(NodeId nodeid, int howmuch, const std::string& message="") EXCLUSIVE_LOCKS_REQUIRED(cs_main); 75 : bool IsBanned(NodeId nodeid); 76 : 77 : 78 : using SecondsDouble = std::chrono::duration<double, std::chrono::seconds::period>; 79 : /** 80 : * Helper to count the seconds in any std::chrono::duration type 81 : */ 82 9 : inline double CountSecondsDouble(SecondsDouble t) { return t.count(); } 83 : 84 : #endif // PIVX_NET_PROCESSING_H