Bitcoin ABC  0.24.7
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 <invrequest.h>
11 #include <net.h>
12 #include <sync.h>
13 #include <validationinterface.h>
14 
15 extern RecursiveMutex cs_main;
17 
18 namespace avalanche {
19 struct ProofId;
20 }
21 
24 class CBlockHeader;
25 class CTxMemPool;
26 class ChainstateManager;
27 class Config;
28 class TxValidationState;
29 
34 static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100;
39 static const unsigned int DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN = 100;
40 static const bool DEFAULT_PEERBLOCKFILTERS = false;
43 static const int DISCOURAGEMENT_THRESHOLD{100};
44 
45 class PeerManager final : public CValidationInterface,
46  public NetEventsInterface {
47 public:
48  PeerManager(const CChainParams &chainparams, CConnman &connman,
49  BanMan *banman, CScheduler &scheduler,
50  ChainstateManager &chainman, CTxMemPool &pool);
51 
55  void BlockConnected(const std::shared_ptr<const CBlock> &pblock,
56  const CBlockIndex *pindexConnected) override;
57  void BlockDisconnected(const std::shared_ptr<const CBlock> &block,
58  const CBlockIndex *pindex) override;
62  void UpdatedBlockTip(const CBlockIndex *pindexNew,
63  const CBlockIndex *pindexFork,
64  bool fInitialDownload) override;
68  void BlockChecked(const CBlock &block,
69  const BlockValidationState &state) override;
73  void NewPoWValidBlock(const CBlockIndex *pindex,
74  const std::shared_ptr<const CBlock> &pblock) override;
75 
80  void InitializeNode(const Config &config, CNode *pnode) override;
85  void FinalizeNode(const Config &config, NodeId nodeid,
86  bool &fUpdateConnectionTime) override;
90  bool ProcessMessages(const Config &config, CNode *pfrom,
91  std::atomic<bool> &interrupt) override;
99  bool SendMessages(const Config &config, CNode *pto,
100  std::atomic<bool> &interrupt) override
102 
107  void ConsiderEviction(CNode &pto, int64_t time_in_seconds)
118  void EvictExtraOutboundPeers(int64_t time_in_seconds)
120 
122  void ProcessMessage(const Config &config, CNode &pfrom,
123  const std::string &msg_type, CDataStream &vRecv,
124  const std::chrono::microseconds time_received,
125  const std::atomic<bool> &interruptMsgProc);
126 
133  void Misbehaving(const NodeId pnode, const int howmuch,
134  const std::string &message);
135 
140  void ReattemptInitialBroadcast(CScheduler &scheduler) const;
141 
145  void UpdateAvalancheStatistics() const;
146 
147 private:
148  // overloaded variant of above to operate on CNode*s
149  void Misbehaving(const CNode &node, int howmuch,
150  const std::string &message) {
151  Misbehaving(node.GetId(), howmuch, message);
152  }
153 
166  bool MaybePunishNodeForBlock(NodeId nodeid,
167  const BlockValidationState &state,
168  bool via_compact_block,
169  const std::string &message = "");
170 
177  bool MaybePunishNodeForTx(NodeId nodeid, const TxValidationState &state,
178  const std::string &message = "");
179 
188  bool MaybeDiscourageAndDisconnect(CNode &pnode);
189 
190  void ProcessOrphanTx(const Config &config, std::set<TxId> &orphan_work_set)
193  void ProcessHeadersMessage(const Config &config, CNode &pfrom,
194  const std::vector<CBlockHeader> &headers,
195  bool via_compact_block);
196 
197  void SendBlockTransactions(CNode &pfrom, const CBlock &block,
198  const BlockTransactionsRequest &req);
199 
205  void AddTxAnnouncement(const CNode &node, const TxId &txid,
206  std::chrono::microseconds current_time)
208 
214  void
215  AddProofAnnouncement(const CNode &node, const avalanche::ProofId &proofid,
216  std::chrono::microseconds current_time, bool preferred)
218 
225  BanMan *const m_banman;
228  InvRequestTracker<TxId> m_txrequest GUARDED_BY(::cs_main);
229 
231  InvRequestTracker<avalanche::ProofId>
232  m_proofrequest GUARDED_BY(cs_proofrequest);
233 
236 };
237 
239  int m_misbehavior_score = 0;
240  int nSyncHeight = -1;
241  int nCommonHeight = -1;
242  std::vector<int> vHeightInFlight;
243 };
244 
246 bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats);
247 
249 void RelayTransaction(const TxId &txid, const CConnman &connman);
250 
252 void RelayProof(const avalanche::ProofId &proofid, const CConnman &connman);
253 
254 #endif // BITCOIN_NET_PROCESSING_H
CValidationInterface
Implement this to subscribe to events generated in validation.
Definition: validationinterface.h:83
PeerManager::ConsiderEviction
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've been behind our tip.
Definition: net_processing.cpp:4998
g_cs_orphans
RecursiveMutex g_cs_orphans
Guards orphan transactions and extra txs for compact blocks.
Definition: net_processing.cpp:291
CNodeStateStats::vHeightInFlight
std::vector< int > vHeightInFlight
Definition: net_processing.h:242
CScheduler
Simple class for background tasks that should be run periodically or once "after a while".
Definition: scheduler.h:35
PeerManager::BlockChecked
void BlockChecked(const CBlock &block, const BlockValidationState &state) override
Overridden from CValidationInterface.
Definition: net_processing.cpp:1779
PeerManager::m_connman
CConnman & m_connman
Definition: net_processing.h:220
PeerManager::ProcessOrphanTx
void ProcessOrphanTx(const Config &config, std::set< TxId > &orphan_work_set) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Reconsider orphan transactions after a parent has been accepted to the mempool.
NetEventsInterface
Interface for message handling.
Definition: net.h:679
PeerManager::UpdateAvalancheStatistics
void UpdateAvalancheStatistics() const
Update the avalanche statistics for all the nodes.
Definition: net_processing.cpp:1167
PeerManager::g_cs_orphans
void g_cs_orphans
Definition: net_processing.h:191
PeerManager::SendMessages
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.
Definition: net_processing.cpp:5211
PeerManager::EvictExtraOutboundPeers
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.
Definition: net_processing.cpp:5083
CNode::GetId
NodeId GetId() const
Definition: net.h:1283
CBlockHeader
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22
BlockValidationState
Definition: validation.h:138
RelayTransaction
void RelayTransaction(const TxId &txid, const CConnman &connman)
Relay transaction to every node.
Definition: net_processing.cpp:1862
avalanche
Definition: avalanche.h:11
PeerManager::ProcessMessage
void ProcessMessage(const Config &config, 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.
Definition: net_processing.cpp:2871
sync.h
PeerManager::m_chainparams
const CChainParams & m_chainparams
Definition: net_processing.h:219
CTxMemPool
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:495
PeerManager::BlockDisconnected
void BlockDisconnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being disconnected.
Definition: net_processing.cpp:1656
CNode
Information about a peer.
Definition: net.h:926
PeerManager::m_stale_tip_check_time
int64_t m_stale_tip_check_time
Next time to check for stale tip.
Definition: net_processing.h:235
validationinterface.h
CChainParams
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:47
avalanche::ProofId
Definition: proofid.h:17
AnnotatedMixin< std::recursive_mutex >
PeerManager::Misbehaving
void Misbehaving(const CNode &node, int howmuch, const std::string &message)
Definition: net_processing.h:149
invrequest.h
cs_main
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:103
PeerManager::SendBlockTransactions
void SendBlockTransactions(CNode &pfrom, const CBlock &block, const BlockTransactionsRequest &req)
Definition: net_processing.cpp:2308
PeerManager::cs_proofrequest
Mutex cs_proofrequest
Definition: net_processing.h:230
DEFAULT_MAX_ORPHAN_TRANSACTIONS
static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS
Default for -maxorphantx, maximum number of orphan transactions kept in memory.
Definition: net_processing.h:34
PeerManager::Misbehaving
void Misbehaving(const NodeId pnode, const int howmuch, const std::string &message)
Increment peer's misbehavior score.
Definition: net_processing.cpp:1407
PeerManager::BlockConnected
void BlockConnected(const std::shared_ptr< const CBlock > &pblock, const CBlockIndex *pindexConnected) override
Overridden from CValidationInterface.
Definition: net_processing.cpp:1603
TxValidationState
Definition: validation.h:137
PeerManager::MaybePunishNodeForBlock
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.
Definition: net_processing.cpp:1435
PeerManager::m_mempool
CTxMemPool & m_mempool
Definition: net_processing.h:227
Config
Definition: config.h:17
PeerManager::NewPoWValidBlock
void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr< const CBlock > &pblock) override
Overridden from CValidationInterface.
Definition: net_processing.cpp:1683
BlockTransactionsRequest
Definition: blockencodings.h:40
DEFAULT_PEERBLOCKFILTERS
static const bool DEFAULT_PEERBLOCKFILTERS
Definition: net_processing.h:40
PeerManager::AddProofAnnouncement
void AddTxAnnouncement(const CNode &node, const TxId &txid, std::chrono::microseconds current_time) EXCLUSIVE_LOCKS_REQUIRED(void AddProofAnnouncement(const CNode &node, const avalanche::ProofId &proofid, std::chrono::microseconds current_time, bool preferred) EXCLUSIVE_LOCKS_REQUIRED(cs_proofrequest)
Register with InvRequestTracker that a TX INV has been received from a peer.
Definition: net_processing.h:215
GetNodeStateStats
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats)
Get statistics from node state.
Definition: net_processing.cpp:1230
PeerManager::CheckForStaleTipAndEvictPeers
void CheckForStaleTipAndEvictPeers()
Evict extra outbound peers.
Definition: net_processing.cpp:5168
DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN
static const unsigned int DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN
Default number of orphan+recently-replaced txn to keep around for block reconstruction.
Definition: net_processing.h:39
PeerManager
Definition: net_processing.h:45
RelayProof
void RelayProof(const avalanche::ProofId &proofid, const CConnman &connman)
Relay proof to every node.
Definition: net_processing.cpp:1867
TxId
A TxId is the identifier of a transaction.
Definition: txid.h:14
PeerManager::GUARDED_BY
InvRequestTracker< TxId > m_txrequest GUARDED_BY(::cs_main)
PeerManager::ProcessHeadersMessage
void ProcessHeadersMessage(const Config &config, CNode &pfrom, const std::vector< CBlockHeader > &headers, bool via_compact_block)
Process a single headers message from a peer.
Definition: net_processing.cpp:2326
BanMan
Definition: banman.h:58
ChainstateManager
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:1099
PeerManager::ProcessMessages
bool ProcessMessages(const Config &config, CNode *pfrom, std::atomic< bool > &interrupt) override
Process protocol messages received from a given node.
Definition: net_processing.cpp:4858
CBlock
Definition: block.h:55
CConnman
Definition: net.h:221
CNode::cs_sendProcessing
RecursiveMutex cs_sendProcessing
Definition: net.h:951
PeerManager::MaybePunishNodeForTx
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.
Definition: net_processing.cpp:1493
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
PeerManager::MaybeDiscourageAndDisconnect
bool MaybeDiscourageAndDisconnect(CNode &pnode)
Maybe disconnect a peer and discourage future connections from its address.
Definition: net_processing.cpp:4807
PeerManager::FinalizeNode
void FinalizeNode(const Config &config, NodeId nodeid, bool &fUpdateConnectionTime) override
Handle removal of a peer by updating various state and removing it from mapNodeState.
Definition: net_processing.cpp:1175
PeerManager::PeerManager
PeerManager(const CChainParams &chainparams, CConnman &connman, BanMan *banman, CScheduler &scheduler, ChainstateManager &chainman, CTxMemPool &pool)
Definition: net_processing.cpp:1544
params.h
PeerManager::UpdatedBlockTip
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override
Overridden from CValidationInterface.
Definition: net_processing.cpp:1738
DISCOURAGEMENT_THRESHOLD
static const int DISCOURAGEMENT_THRESHOLD
Threshold for marking a node to be discouraged, e.g.
Definition: net_processing.h:43
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:197
NodeId
int64_t NodeId
Definition: nodeid.h:10
PeerManager::m_banman
BanMan *const m_banman
Pointer to this node's banman.
Definition: net_processing.h:225
CNodeStateStats
Definition: net_processing.h:238
net.h
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:23
PeerManager::InitializeNode
void InitializeNode(const Config &config, CNode *pnode) override
Initialize a peer by adding it to mapNodeState and pushing a message requesting its version.
Definition: net_processing.cpp:1107
PeerManager::m_chainman
ChainstateManager & m_chainman
Definition: net_processing.h:226
PeerManager::ReattemptInitialBroadcast
void ReattemptInitialBroadcast(CScheduler &scheduler) const
Retrieve unbroadcast transactions from the mempool and reattempt sending to peers.
Definition: net_processing.cpp:1130
InvRequestTracker
Definition: invrequest.h:159