1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 <consensus/params.h>
12 #include <sync.h>
13 
14 extern CCriticalSection cs_main;
15 
16 /** Default for -maxorphantx, maximum number of orphan transactions kept in memory */
17 static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100;
18 /** Default number of orphan+recently-replaced txn to keep around for block reconstruction */
19 static const unsigned int DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN = 100;
20 /** Default for BIP61 (sending reject messages) */
21 static constexpr bool DEFAULT_ENABLE_BIP61{true};
22 
23 class PeerLogicValidation final : public CValidationInterface, public NetEventsInterface {
24 private:
25     CConnman* const connman;
26     BanMan* const m_banman;
27 
28     bool SendRejectsAndCheckIfBanned(CNode* pnode, bool enable_bip61) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
29 public:
30     PeerLogicValidation(CConnman* connman, BanMan* banman, CScheduler &scheduler, bool enable_bip61);
31 
32     /**
33      * Overridden from CValidationInterface.
34      */
35     void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected, const std::vector<CTransactionRef>& vtxConflicted) override;
36     /**
37      * Overridden from CValidationInterface.
38      */
39     void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override;
40     /**
41      * Overridden from CValidationInterface.
42      */
43     void BlockChecked(const CBlock& block, const CValidationState& state) override;
44     /**
45      * Overridden from CValidationInterface.
46      */
47     void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) override;
48 
49     /** Initialize a peer by adding it to mapNodeState and pushing a message requesting its version */
50     void InitializeNode(CNode* pnode) override;
51     /** Handle removal of a peer by updating various state and removing it from mapNodeState */
52     void FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) override;
53     /**
54     * Process protocol messages received from a given node
55     *
56     * @param[in]   pfrom           The node which we have received messages from.
57     * @param[in]   interrupt       Interrupt condition for processing threads
58     */
59     bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override;
60     /**
61     * Send queued protocol messages to be sent to a give node.
62     *
63     * @param[in]   pto             The node which we are sending messages to.
64     * @return                      True if there is more work to be done
65     */
66     bool SendMessages(CNode* pto) override EXCLUSIVE_LOCKS_REQUIRED(pto->cs_sendProcessing);
67 
68     /** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
69     void ConsiderEviction(CNode *pto, int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
70     /** Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound */
71     void CheckForStaleTipAndEvictPeers(const Consensus::Params &consensusParams);
72     /** If we have extra outbound peers, try to disconnect the one with the oldest block announcement */
73     void EvictExtraOutboundPeers(int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
74 
75 private:
76     int64_t m_stale_tip_check_time; //!< Next time to check for stale tip
77 
78     /** Enable BIP61 (sending reject messages) */
79     const bool m_enable_bip61;
80 };
81 
82 struct CNodeStateStats {
83     int nMisbehavior = 0;
84     int nSyncHeight = -1;
85     int nCommonHeight = -1;
86     std::vector<int> vHeightInFlight;
87 };
88 
89 /** Get statistics from node state */
90 bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats);
91 
92 #endif // BITCOIN_NET_PROCESSING_H
93