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