LCOV - code coverage report
Current view: top level - src - net_processing.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 4 4 100.0 %
Date: 2020-09-26 01:30:44 Functions: 4 10 40.0 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2020 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 <consensus/params.h>
      10             : #include <net.h>
      11             : #include <sync.h>
      12             : #include <validationinterface.h>
      13             : 
      14             : class BlockTransactionsRequest;
      15             : class BlockValidationState;
      16             : class CBlockHeader;
      17             : class CChainParams;
      18             : class CTxMemPool;
      19             : class ChainstateManager;
      20             : class TxValidationState;
      21             : 
      22             : extern RecursiveMutex cs_main;
      23             : extern RecursiveMutex g_cs_orphans;
      24             : 
      25             : /** Default for -maxorphantx, maximum number of orphan transactions kept in memory */
      26             : static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100;
      27             : /** Default number of orphan+recently-replaced txn to keep around for block reconstruction */
      28             : static const unsigned int DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN = 100;
      29             : static const bool DEFAULT_PEERBLOOMFILTERS = false;
      30             : static const bool DEFAULT_PEERBLOCKFILTERS = false;
      31             : /** Threshold for marking a node to be discouraged, e.g. disconnected and added to the discouragement filter. */
      32             : static const int DISCOURAGEMENT_THRESHOLD{100};
      33             : 
      34             : class PeerManager final : public CValidationInterface, public NetEventsInterface {
      35             : public:
      36             :     PeerManager(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
      37             :                 CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool);
      38             : 
      39             :     /**
      40             :      * Overridden from CValidationInterface.
      41             :      */
      42             :     void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override;
      43             :     void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) override;
      44             :     /**
      45             :      * Overridden from CValidationInterface.
      46             :      */
      47             :     void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override;
      48             :     /**
      49             :      * Overridden from CValidationInterface.
      50             :      */
      51             :     void BlockChecked(const CBlock& block, const BlockValidationState& state) override;
      52             :     /**
      53             :      * Overridden from CValidationInterface.
      54             :      */
      55             :     void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) override;
      56             : 
      57             :     /** Initialize a peer by adding it to mapNodeState and pushing a message requesting its version */
      58             :     void InitializeNode(CNode* pnode) override;
      59             :     /** Handle removal of a peer by updating various state and removing it from mapNodeState */
      60             :     void FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) override;
      61             :     /**
      62             :     * Process protocol messages received from a given node
      63             :     *
      64             :     * @param[in]   pfrom           The node which we have received messages from.
      65             :     * @param[in]   interrupt       Interrupt condition for processing threads
      66             :     */
      67             :     bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override;
      68             :     /**
      69             :     * Send queued protocol messages to be sent to a give node.
      70             :     *
      71             :     * @param[in]   pto             The node which we are sending messages to.
      72             :     * @return                      True if there is more work to be done
      73             :     */
      74             :     bool SendMessages(CNode* pto) override EXCLUSIVE_LOCKS_REQUIRED(pto->cs_sendProcessing);
      75             : 
      76             :     /** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
      77             :     void ConsiderEviction(CNode& pto, int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
      78             :     /** Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound */
      79             :     void CheckForStaleTipAndEvictPeers();
      80             :     /** If we have extra outbound peers, try to disconnect the one with the oldest block announcement */
      81             :     void EvictExtraOutboundPeers(int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
      82             :     /** Retrieve unbroadcast transactions from the mempool and reattempt sending to peers */
      83             :     void ReattemptInitialBroadcast(CScheduler& scheduler) const;
      84             : 
      85             :     /** Process a single message from a peer. Public for fuzz testing */
      86             :     void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
      87             :                         const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc);
      88             : 
      89             :     /**
      90             :      * Increment peer's misbehavior score. If the new value >= DISCOURAGEMENT_THRESHOLD, mark the node
      91             :      * to be discouraged, meaning the peer might be disconnected and added to the discouragement filter.
      92             :      * Public for unit testing.
      93             :      */
      94             :     void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message);
      95             : 
      96             : private:
      97             :     /**
      98             :      * Potentially mark a node discouraged based on the contents of a BlockValidationState object
      99             :      *
     100             :      * @param[in] via_compact_block this bool is passed in because net_processing should
     101             :      * punish peers differently depending on whether the data was provided in a compact
     102             :      * block message or not. If the compact block had a valid header, but contained invalid
     103             :      * txs, the peer should not be punished. See BIP 152.
     104             :      *
     105             :      * @return Returns true if the peer was punished (probably disconnected)
     106             :      */
     107             :     bool MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState& state,
     108             :                                  bool via_compact_block, const std::string& message = "");
     109             : 
     110             :     /**
     111             :      * Potentially disconnect and discourage a node based on the contents of a TxValidationState object
     112             :      *
     113             :      * @return Returns true if the peer was punished (probably disconnected)
     114             :      */
     115             :     bool MaybePunishNodeForTx(NodeId nodeid, const TxValidationState& state, const std::string& message = "");
     116             : 
     117             :     /** Maybe disconnect a peer and discourage future connections from its address.
     118             :      *
     119             :      * @param[in]   pnode     The node to check.
     120             :      * @return                True if the peer was marked for disconnection in this function
     121             :      */
     122             :     bool MaybeDiscourageAndDisconnect(CNode& pnode);
     123             : 
     124             :     void ProcessOrphanTx(std::set<uint256>& orphan_work_set, std::list<CTransactionRef>& removed_txn)
     125             :         EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_cs_orphans);
     126             :     /** Process a single headers message from a peer. */
     127             :     void ProcessHeadersMessage(CNode& pfrom, const std::vector<CBlockHeader>& headers, bool via_compact_block);
     128             : 
     129             :     void SendBlockTransactions(CNode& pfrom, const CBlock& block, const BlockTransactionsRequest& req);
     130             : 
     131             :     const CChainParams& m_chainparams;
     132             :     CConnman& m_connman;
     133             :     /** Pointer to this node's banman. May be nullptr - check existence before dereferencing. */
     134             :     BanMan* const m_banman;
     135             :     ChainstateManager& m_chainman;
     136             :     CTxMemPool& m_mempool;
     137             : 
     138             :     int64_t m_stale_tip_check_time; //!< Next time to check for stale tip
     139             : };
     140             : 
     141       34540 : struct CNodeStateStats {
     142        8635 :     int m_misbehavior_score = 0;
     143        8635 :     int nSyncHeight = -1;
     144        8635 :     int nCommonHeight = -1;
     145             :     std::vector<int> vHeightInFlight;
     146             : };
     147             : 
     148             : /** Get statistics from node state */
     149             : bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats);
     150             : 
     151             : /** Relay transaction to every node */
     152             : void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman& connman) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
     153             : 
     154             : #endif // BITCOIN_NET_PROCESSING_H

Generated by: LCOV version 1.15