1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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.
6 #ifndef BITCOIN_NET_PROCESSING_H
7 #define BITCOIN_NET_PROCESSING_H
10 #include "validationinterface.h"
11 #include "consensus/params.h"
13 /** Default for -maxorphantx, maximum number of orphan transactions kept in memory */
14 static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS
= 100;
15 /** Expiration time for orphan transactions in seconds */
16 static const int64_t ORPHAN_TX_EXPIRE_TIME
= 20 * 60;
17 /** Minimum time between orphan transactions expire time checks in seconds */
18 static const int64_t ORPHAN_TX_EXPIRE_INTERVAL
= 5 * 60;
19 /** Default number of orphan+recently-replaced txn to keep around for block reconstruction */
20 static const unsigned int DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN
= 100;
21 /** Headers download timeout expressed in microseconds
22 * Timeout = base + per_header * (expected number of headers) */
23 static constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_BASE
= 15 * 60 * 1000000; // 15 minutes
24 static constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER
= 1000; // 1ms/header
25 /** Protect at least this many outbound peers from disconnection due to slow/
26 * behind headers chain.
28 static constexpr int32_t MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT
= 4;
29 /** Timeout for (unprotected) outbound peers to sync to our chainwork, in seconds */
30 static constexpr int64_t CHAIN_SYNC_TIMEOUT
= 20 * 60; // 20 minutes
31 /** How frequently to check for stale tips, in seconds */
32 static constexpr int64_t STALE_CHECK_INTERVAL
= 10 * 60; // 10 minutes
33 /** How frequently to check for extra outbound peers and disconnect, in seconds */
34 static constexpr int64_t EXTRA_PEER_CHECK_INTERVAL
= 45;
35 /** Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict, in seconds */
36 static constexpr int64_t MINIMUM_CONNECT_TIME
= 30;
38 class PeerLogicValidation
: public CValidationInterface
, public NetEventsInterface
{
40 CConnman
* const connman
;
43 explicit PeerLogicValidation(CConnman
* connman
, CScheduler
&scheduler
);
45 void BlockConnected(const std::shared_ptr
<const CBlock
>& pblock
, const CBlockIndex
* pindexConnected
, const std::vector
<CTransactionRef
>& vtxConflicted
) override
;
46 void UpdatedBlockTip(const CBlockIndex
*pindexNew
, const CBlockIndex
*pindexFork
, bool fInitialDownload
) override
;
47 void BlockChecked(const CBlock
& block
, const CValidationState
& state
) override
;
48 void NewPoWValidBlock(const CBlockIndex
*pindex
, const std::shared_ptr
<const CBlock
>& pblock
) override
;
51 void InitializeNode(CNode
* pnode
) override
;
52 void FinalizeNode(NodeId nodeid
, bool& fUpdateConnectionTime
) override
;
53 /** Process protocol messages received from a given node */
54 bool ProcessMessages(CNode
* pfrom
, std::atomic
<bool>& interrupt
) override
;
56 * Send queued protocol messages to be sent to a give node.
58 * @param[in] pto The node which we are sending messages to.
59 * @param[in] interrupt Interrupt condition for processing threads
60 * @return True if there is more work to be done
62 bool SendMessages(CNode
* pto
, std::atomic
<bool>& interrupt
) override
;
64 void ConsiderEviction(CNode
*pto
, int64_t time_in_seconds
);
65 void CheckForStaleTipAndEvictPeers(const Consensus::Params
&consensusParams
);
66 void EvictExtraOutboundPeers(int64_t time_in_seconds
);
69 int64_t m_stale_tip_check_time
; //! Next time to check for stale tip
72 struct CNodeStateStats
{
76 std::vector
<int> vHeightInFlight
;
79 /** Get statistics from node state */
80 bool GetNodeStateStats(NodeId nodeid
, CNodeStateStats
&stats
);
81 /** Increase a node's misbehavior score. */
82 void Misbehaving(NodeId nodeid
, int howmuch
);
84 #endif // BITCOIN_NET_PROCESSING_H