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 <txrequest.h>
13 #include <validationinterface.h>
14 
17 class CBlockHeader;
18 class CChainParams;
19 class CTxMemPool;
20 class ChainstateManager;
21 class TxValidationState;
22 
23 extern RecursiveMutex cs_main;
25 
27 static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100;
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;
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 
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;
48  void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override;
52  void BlockChecked(const CBlock& block, const BlockValidationState& state) override;
56  void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) override;
57 
59  void InitializeNode(CNode* pnode) override;
61  void FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) override;
68  bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override;
76 
78  void ConsiderEviction(CNode& pto, int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
82  void EvictExtraOutboundPeers(int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
84  void ReattemptInitialBroadcast(CScheduler& scheduler) const;
85 
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 
95  void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message);
96 
97 private:
108  bool MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState& state,
109  bool via_compact_block, const std::string& message = "");
110 
116  bool MaybePunishNodeForTx(NodeId nodeid, const TxValidationState& state, const std::string& message = "");
117 
123  bool MaybeDiscourageAndDisconnect(CNode& pnode);
124 
125  void ProcessOrphanTx(std::set<uint256>& orphan_work_set) EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_cs_orphans);
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 
134  void AddTxAnnouncement(const CNode& node, const GenTxid& gtxid, std::chrono::microseconds current_time)
135  EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
136 
140  BanMan* const m_banman;
143  TxRequestTracker m_txrequest GUARDED_BY(::cs_main);
144 
146 };
147 
149  int m_misbehavior_score = 0;
150  int nSyncHeight = -1;
151  int nCommonHeight = -1;
152  std::vector<int> vHeightInFlight;
153 };
154 
156 bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats);
157 
159 void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman& connman) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
160 
161 #endif // BITCOIN_NET_PROCESSING_H
bool MaybeDiscourageAndDisconnect(CNode &pnode)
Maybe disconnect a peer and discourage future connections from its address.
int64_t m_stale_tip_check_time
Next time to check for stale tip.
Definition: banman.h:57
void InitializeNode(CNode *pnode) override
Initialize a peer by adding it to mapNodeState and pushing a message requesting its version...
Definition: block.h:62
Data structure to keep track of, and schedule, transaction downloads from peers.
Definition: txrequest.h:96
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:781
void ProcessOrphanTx(std::set< uint256 > &orphan_work_set) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Reconsider orphan transactions after a parent has been accepted to the mempool.
Interface for message handling.
Definition: net.h:615
RecursiveMutex g_cs_orphans
Guards orphan transactions and extra txs for compact blocks.
bool MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState &state, bool via_compact_block, const std::string &message="")
Potentially mark a node discouraged based on the contents of a BlockValidationState object...
void ProcessHeadersMessage(CNode &pfrom, const std::vector< CBlockHeader > &headers, bool via_compact_block)
Process a single headers message from a peer.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
Definition: chainparams.h:52
PeerManager(const CChainParams &chainparams, CConnman &connman, BanMan *banman, CScheduler &scheduler, ChainstateManager &chainman, CTxMemPool &pool)
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:202
Implement this to subscribe to events generated in validation.
RecursiveMutex cs_sendProcessing
Definition: net.h:858
static const bool DEFAULT_PEERBLOCKFILTERS
bool MaybePunishNodeForTx(NodeId nodeid, const TxValidationState &state, const std::string &message="")
Potentially disconnect and discourage a node based on the contents of a TxValidationState object...
void CheckForStaleTipAndEvictPeers()
Evict extra outbound peers.
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats)
Get statistics from node state.
void BlockConnected(const std::shared_ptr< const CBlock > &pblock, const CBlockIndex *pindexConnected) override
Overridden from CValidationInterface.
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override
Overridden from CValidationInterface.
void BlockDisconnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being disconnected.
bool ProcessMessages(CNode *pfrom, std::atomic< bool > &interrupt) override
Process protocol messages received from a given node.
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:129
int64_t NodeId
Definition: net.h:92
Definition: net.h:187
CConnman & m_connman
static const int DISCOURAGEMENT_THRESHOLD
Threshold for marking a node to be discouraged, e.g.
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.
void SendBlockTransactions(CNode &pfrom, const CBlock &block, const BlockTransactionsRequest &req)
void AddTxAnnouncement(const CNode &node, const GenTxid &gtxid, std::chrono::microseconds current_time) EXCLUSIVE_LOCKS_REQUIRED(const CChainParams & m_chainparams
Register with TxRequestTracker that an INV has been received from a peer.
void Misbehaving(const NodeId pnode, const int howmuch, const std::string &message)
Increment peer&#39;s misbehavior score.
void FinalizeNode(NodeId nodeid, bool &fUpdateConnectionTime) override
Handle removal of a peer by updating various state and removing it from mapNodeState.
bool SendMessages(CNode *pto) override EXCLUSIVE_LOCKS_REQUIRED(pto -> cs_sendProcessing)
Send queued protocol messages to be sent to a give node.
void RelayTransaction(const uint256 &txid, const uint256 &wtxid, const CConnman &connman) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Relay transaction to every node.
ChainstateManager & m_chainman
CTxMemPool & m_mempool
256-bit opaque blob.
Definition: uint256.h:124
static const bool DEFAULT_PEERBLOOMFILTERS
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:488
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.
Information about a peer.
Definition: net.h:834
std::vector< int > vHeightInFlight
Simple class for background tasks that should be run periodically or once "after a while"...
Definition: scheduler.h:32
void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr< const CBlock > &pblock) override
Overridden from CValidationInterface.
static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS
Default for -maxorphantx, maximum number of orphan transactions kept in memory.
BanMan *const m_banman
Pointer to this node&#39;s banman.
void BlockChecked(const CBlock &block, const BlockValidationState &state) override
Overridden from CValidationInterface.
void ReattemptInitialBroadcast(CScheduler &scheduler) const
Retrieve unbroadcast transactions from the mempool and reattempt sending to peers.
TxRequestTracker m_txrequest GUARDED_BY(::cs_main)
A generic txid reference (txid or wtxid).
Definition: transaction.h:400
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:20
void ProcessMessage(CNode &pfrom, const std::string &msg_type, CDataStream &vRecv, const std::chrono::microseconds time_received, const std::atomic< bool > &interruptMsgProc)
Process a single message from a peer.