Bitcoin Core  0.20.99
P2P Digital Currency
net_processing.h
Go to the documentation of this file.
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 CTxMemPool;
15 class ChainstateManager;
16 
17 extern RecursiveMutex cs_main;
19 
21 static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100;
23 static const unsigned int DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN = 100;
24 static const bool DEFAULT_PEERBLOOMFILTERS = false;
25 static const bool DEFAULT_PEERBLOCKFILTERS = false;
27 static const int DISCOURAGEMENT_THRESHOLD{100};
28 
30 private:
31  CConnman* const connman;
33  BanMan* const m_banman;
36 
38 
39 public:
40  PeerLogicValidation(CConnman* connman, BanMan* banman, CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool);
41 
45  void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override;
46  void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) override;
50  void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override;
54  void BlockChecked(const CBlock& block, const BlockValidationState& state) override;
58  void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) override;
59 
61  void InitializeNode(CNode* pnode) override;
63  void FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) override;
70  bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override;
77  bool SendMessages(CNode* pto) override EXCLUSIVE_LOCKS_REQUIRED(pto->cs_sendProcessing);
78 
80  void ConsiderEviction(CNode& pto, int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
82  void CheckForStaleTipAndEvictPeers(const Consensus::Params &consensusParams);
84  void EvictExtraOutboundPeers(int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
86  void ReattemptInitialBroadcast(CScheduler& scheduler) const;
87 
88 private:
90 };
91 
93  int nMisbehavior = 0;
94  int nSyncHeight = -1;
95  int nCommonHeight = -1;
96  std::vector<int> vHeightInFlight;
97 };
98 
100 bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats);
101 
103 void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman& connman) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
104 
105 #endif // BITCOIN_NET_PROCESSING_H
Definition: banman.h:57
ChainstateManager & m_chainman
CConnman *const connman
Definition: block.h:62
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override
Overridden from CValidationInterface.
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:749
void CheckForStaleTipAndEvictPeers(const Consensus::Params &consensusParams)
Evict extra outbound peers.
bool SendMessages(CNode *pto) override EXCLUSIVE_LOCKS_REQUIRED(pto -> cs_sendProcessing)
Send queued protocol messages to be sent to a give node.
CTxMemPool & m_mempool
Interface for message handling.
Definition: net.h:539
RecursiveMutex g_cs_orphans
Implement this to subscribe to events generated in validation.
bool ProcessMessages(CNode *pfrom, std::atomic< bool > &interrupt) override
Process protocol messages received from a given node.
static const bool DEFAULT_PEERBLOCKFILTERS
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats)
Get statistics from node state.
BanMan *const m_banman
Pointer to this node&#39;s banman.
void ReattemptInitialBroadcast(CScheduler &scheduler) const
Retrieve unbroadcast transactions from the mempool and reattempt sending to peers.
bool MaybeDiscourageAndDisconnect(CNode &pnode)
Maybe disconnect a peer and discourage future connections from its address.
void ConsiderEviction(CNode &pto, int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Consider evicting an outbound peer based on the amount of time they&#39;ve been behind our tip...
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:128
int64_t NodeId
Definition: net.h:94
Definition: net.h:122
static const int DISCOURAGEMENT_THRESHOLD
Threshold for marking a node to be discouraged, e.g.
Parameters that influence chain consensus.
Definition: params.h:45
void EvictExtraOutboundPeers(int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
If we have extra outbound peers, try to disconnect the one with the oldest block announcement.
int64_t m_stale_tip_check_time
Next time to check for stale tip.
void RelayTransaction(const uint256 &txid, const uint256 &wtxid, const CConnman &connman) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Relay transaction to every node.
256-bit opaque blob.
Definition: uint256.h:123
static const bool DEFAULT_PEERBLOOMFILTERS
void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr< const CBlock > &pblock) override
Overridden from CValidationInterface.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:53
void BlockDisconnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being disconnected.
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:461
void BlockConnected(const std::shared_ptr< const CBlock > &pblock, const CBlockIndex *pindexConnected) override
Overridden from CValidationInterface.
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:137
static const unsigned int DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN
Default number of orphan+recently-replaced txn to keep around for block reconstruction.
PeerLogicValidation(CConnman *connman, BanMan *banman, CScheduler &scheduler, ChainstateManager &chainman, CTxMemPool &pool)
void FinalizeNode(NodeId nodeid, bool &fUpdateConnectionTime) override
Handle removal of a peer by updating various state and removing it from mapNodeState.
Information about a peer.
Definition: net.h:750
std::vector< int > vHeightInFlight
Simple class for background tasks that should be run periodically or once "after a while"...
Definition: scheduler.h:32
void InitializeNode(CNode *pnode) override
Initialize a peer by adding it to mapNodeState and pushing a message requesting its version...
static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS
Default for -maxorphantx, maximum number of orphan transactions kept in memory.
void BlockChecked(const CBlock &block, const BlockValidationState &state) override
Overridden from CValidationInterface.