Bitcoin ABC  0.22.13
P2P Digital Currency
net_processing.h
Go to the documentation of this file.
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.
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 extern RecursiveMutex cs_main;
16 
19 class CBlockHeader;
20 class CTxMemPool;
21 class ChainstateManager;
22 class Config;
23 class TxValidationState;
24 
29 static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100;
34 static const unsigned int DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN = 100;
35 static const bool DEFAULT_PEERBLOCKFILTERS = false;
38 static const int DISCOURAGEMENT_THRESHOLD{100};
39 
40 class PeerManager final : public CValidationInterface,
41  public NetEventsInterface {
42 public:
43  PeerManager(const CChainParams &chainparams, CConnman &connman,
44  BanMan *banman, CScheduler &scheduler,
45  ChainstateManager &chainman, CTxMemPool &pool);
46 
50  void BlockConnected(const std::shared_ptr<const CBlock> &pblock,
51  const CBlockIndex *pindexConnected) override;
52  void BlockDisconnected(const std::shared_ptr<const CBlock> &block,
53  const CBlockIndex *pindex) override;
57  void UpdatedBlockTip(const CBlockIndex *pindexNew,
58  const CBlockIndex *pindexFork,
59  bool fInitialDownload) override;
63  void BlockChecked(const CBlock &block,
64  const BlockValidationState &state) override;
68  void NewPoWValidBlock(const CBlockIndex *pindex,
69  const std::shared_ptr<const CBlock> &pblock) override;
70 
75  void InitializeNode(const Config &config, CNode *pnode) override;
80  void FinalizeNode(const Config &config, NodeId nodeid,
81  bool &fUpdateConnectionTime) override;
85  bool ProcessMessages(const Config &config, CNode *pfrom,
86  std::atomic<bool> &interrupt) override;
94  bool SendMessages(const Config &config, CNode *pto,
95  std::atomic<bool> &interrupt) override
97 
102  void ConsiderEviction(CNode &pto, int64_t time_in_seconds)
103  EXCLUSIVE_LOCKS_REQUIRED(cs_main);
108  void
109  CheckForStaleTipAndEvictPeers(const Consensus::Params &consensusParams);
114  void EvictExtraOutboundPeers(int64_t time_in_seconds)
115  EXCLUSIVE_LOCKS_REQUIRED(cs_main);
116 
118  void ProcessMessage(const Config &config, CNode &pfrom,
119  const std::string &msg_type, CDataStream &vRecv,
120  int64_t nTimeReceived,
121  const std::atomic<bool> &interruptMsgProc);
122 
127  void ReattemptInitialBroadcast(CScheduler &scheduler) const;
128 
129 private:
142  bool MaybePunishNodeForBlock(NodeId nodeid,
143  const BlockValidationState &state,
144  bool via_compact_block,
145  const std::string &message = "");
146 
153  bool MaybePunishNodeForTx(NodeId nodeid, const TxValidationState &state,
154  const std::string &message = "");
155 
164  bool MaybeDiscourageAndDisconnect(CNode &pnode);
165 
166  void ProcessOrphanTx(const Config &config, std::set<TxId> &orphan_work_set)
167  EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_cs_orphans);
169  void ProcessHeadersMessage(const Config &config, CNode &pfrom,
170  const std::vector<CBlockHeader> &headers,
171  bool via_compact_block);
172 
173  void SendBlockTransactions(CNode &pfrom, const CBlock &block,
174  const BlockTransactionsRequest &req);
175 
182  BanMan *const m_banman;
185 
188 };
189 
191  int m_misbehavior_score = 0;
192  int nSyncHeight = -1;
193  int nCommonHeight = -1;
194  std::vector<int> vHeightInFlight;
195 };
196 
198 bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats);
204 void Misbehaving(const NodeId nodeid, const int howmuch,
205  const std::string &message = "");
206 
208 void RelayTransaction(const TxId &txid, const CConnman &connman);
209 
210 #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:58
Definition: block.h:62
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:1023
void InitializeNode(const Config &config, CNode *pnode) override
Initialize a peer by adding it to mapNodeState and pushing a message requesting its version...
Interface for message handling.
Definition: net.h:567
RecursiveMutex g_cs_orphans
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 FinalizeNode(const Config &config, NodeId nodeid, bool &fUpdateConnectionTime) override
Handle removal of a peer by updating various state and removing it from mapNodeState.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
Definition: chainparams.h:47
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:196
void ProcessMessage(const Config &config, CNode &pfrom, const std::string &msg_type, CDataStream &vRecv, int64_t nTimeReceived, const std::atomic< bool > &interruptMsgProc)
Process a single message from a peer.
Implement this to subscribe to events generated in validation.
RecursiveMutex cs_sendProcessing
Definition: net.h:831
static const bool DEFAULT_PEERBLOCKFILTERS
void ProcessOrphanTx(const Config &config, std::set< TxId > &orphan_work_set) EXCLUSIVE_LOCKS_REQUIRED(cs_main
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...
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.
Definition: config.h:19
void Misbehaving(const NodeId nodeid, const int howmuch, const std::string &message="")
Increment peer&#39;s misbehavior score.
const CChainParams & m_chainparams
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.
void CheckForStaleTipAndEvictPeers(const Consensus::Params &consensusParams)
Evict extra outbound peers.
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
Global state.
Definition: validation.cpp:95
int64_t NodeId
Definition: net.h:99
Definition: net.h:165
CConnman & m_connman
static const int DISCOURAGEMENT_THRESHOLD
Threshold for marking a node to be discouraged, e.g.
bool ProcessMessages(const Config &config, CNode *pfrom, std::atomic< bool > &interrupt) override
Process protocol messages received from a given node.
bool SendMessages(const Config &config, CNode *pto, std::atomic< bool > &interrupt) override EXCLUSIVE_LOCKS_REQUIRED(pto -> cs_sendProcessing)
Send queued protocol messages to be sent to a give node.
Parameters that influence chain consensus.
Definition: params.h:59
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)
ChainstateManager & m_chainman
CTxMemPool & m_mempool
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:468
void ProcessHeadersMessage(const Config &config, CNode &pfrom, const std::vector< CBlockHeader > &headers, bool via_compact_block)
Process a single headers message from a peer.
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: blockindex.h:23
A TxId is the identifier of a transaction.
Definition: txid.h:14
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:806
std::vector< int > vHeightInFlight
void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr< const CBlock > &pblock) override
Overridden from CValidationInterface.
void RelayTransaction(const TxId &txid, const CConnman &connman)
Relay transaction to every node.
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.
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22