Bitcoin Core  29.1.0
P2P Digital Currency
net_processing.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 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 <net.h>
10 #include <txorphanage.h>
11 #include <validationinterface.h>
12 
13 #include <chrono>
14 
15 class AddrMan;
16 class CChainParams;
17 class CTxMemPool;
18 class ChainstateManager;
19 
20 namespace node {
21 class Warnings;
22 } // namespace node
23 
25 static constexpr bool DEFAULT_TXRECONCILIATION_ENABLE{false};
27 static const uint32_t DEFAULT_MAX_ORPHAN_TRANSACTIONS{100};
30 static const uint32_t DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN{100};
31 static const bool DEFAULT_PEERBLOOMFILTERS = false;
32 static const bool DEFAULT_PEERBLOCKFILTERS = false;
34 static const unsigned int MAX_CMPCTBLOCKS_INFLIGHT_PER_BLOCK = 3;
37 static const unsigned int MAX_HEADERS_RESULTS = 2000;
38 
40  int nSyncHeight = -1;
41  int nCommonHeight = -1;
43  std::chrono::microseconds m_ping_wait;
44  std::vector<int> vHeightInFlight;
47  uint64_t m_addr_processed = 0;
48  uint64_t m_addr_rate_limited = 0;
49  bool m_addr_relay_enabled{false};
51  int64_t presync_height{-1};
52  std::chrono::seconds time_offset{0};
53 };
54 
56  std::chrono::seconds median_outbound_time_offset{0s};
57  bool ignores_incoming_txs{false};
58 };
59 
61 {
62 public:
63  struct Options {
74  bool capture_messages{false};
77  bool deterministic_rng{false};
81  };
82 
83  static std::unique_ptr<PeerManager> make(CConnman& connman, AddrMan& addrman,
84  BanMan* banman, ChainstateManager& chainman,
85  CTxMemPool& pool, node::Warnings& warnings, Options opts);
86  virtual ~PeerManager() = default;
87 
95  virtual std::optional<std::string> FetchBlock(NodeId peer_id, const CBlockIndex& block_index) = 0;
96 
98  virtual void StartScheduledTasks(CScheduler& scheduler) = 0;
99 
101  virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const = 0;
102 
103  virtual std::vector<TxOrphanage::OrphanTxBase> GetOrphanTransactions() = 0;
104 
106  virtual PeerManagerInfo GetInfo() const = 0;
107 
109  virtual void RelayTransaction(const uint256& txid, const uint256& wtxid) = 0;
110 
112  virtual void SendPings() = 0;
113 
115  virtual void SetBestBlock(int height, std::chrono::seconds time) = 0;
116 
117  /* Public for unit testing. */
118  virtual void UnitTestMisbehaving(NodeId peer_id) = 0;
119 
124  virtual void CheckForStaleTipAndEvictPeers() = 0;
125 
127  virtual void ProcessMessage(CNode& pfrom, const std::string& msg_type, DataStream& vRecv,
128  const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) = 0;
129 
131  virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) = 0;
132 
154  virtual ServiceFlags GetDesirableServiceFlags(ServiceFlags services) const = 0;
155 };
156 
157 #endif // BITCOIN_NET_PROCESSING_H
static Mutex g_msgproc_mutex
Mutex for anything that is only accessed via the msg processing thread.
Definition: net.h:1011
bool reconcile_txs
Whether transaction reconciliation protocol is enabled.
Definition: banman.h:58
ServiceFlags
nServices flags
Definition: protocol.h:309
virtual void StartScheduledTasks(CScheduler &scheduler)=0
Begin running background tasks, should only be called once.
uint64_t m_addr_rate_limited
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:865
virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds)=0
This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp.
Manages warning messages within a node.
Definition: warnings.h:39
static std::unique_ptr< PeerManager > make(CConnman &connman, AddrMan &addrman, BanMan *banman, ChainstateManager &chainman, CTxMemPool &pool, node::Warnings &warnings, Options opts)
uint32_t max_orphan_txs
Maximum number of orphan transactions kept in memory.
uint32_t max_headers_result
Number of headers sent in one getheaders message result (this is a test-only option).
Interface for message handling.
Definition: net.h:1007
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
Definition: chainparams.h:80
Implement this to subscribe to events generated in validation and mempool.
Stochastic address manager.
Definition: addrman.h:88
virtual std::vector< TxOrphanage::OrphanTxBase > GetOrphanTransactions()=0
static const bool DEFAULT_PEERBLOCKFILTERS
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
bool deterministic_rng
Whether or not the internal RNG behaves deterministically (this is a test-only option).
std::chrono::seconds median_outbound_time_offset
static const unsigned int MAX_HEADERS_RESULTS
Number of headers sent in one getheaders result.
virtual PeerManagerInfo GetInfo() const =0
Get peer manager info.
virtual void ProcessMessage(CNode &pfrom, const std::string &msg_type, DataStream &vRecv, const std::chrono::microseconds time_received, const std::atomic< bool > &interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex)=0
Process a single message from a peer.
CAmount m_fee_filter_received
uint64_t m_addr_processed
static const unsigned int MAX_CMPCTBLOCKS_INFLIGHT_PER_BLOCK
Maximum number of outstanding CMPCTBLOCK requests for the same block.
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:146
virtual void SetBestBlock(int height, std::chrono::seconds time)=0
Set the height of the best block and its time (seconds since epoch).
virtual ServiceFlags GetDesirableServiceFlags(ServiceFlags services) const =0
Gets the set of service flags which are "desirable" for a given peer.
int64_t NodeId
Definition: net.h:97
int64_t presync_height
Definition: net.h:1051
virtual std::optional< std::string > FetchBlock(NodeId peer_id, const CBlockIndex &block_index)=0
Attempt to manually fetch block from a given peer.
std::chrono::microseconds m_ping_wait
Definition: messages.h:20
bool capture_messages
Whether all P2P messages are captured to disk.
256-bit opaque blob.
Definition: uint256.h:201
ServiceFlags their_services
static const bool DEFAULT_PEERBLOOMFILTERS
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
std::chrono::seconds time_offset
virtual void CheckForStaleTipAndEvictPeers()=0
Evict extra outbound peers.
virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) const =0
Get statistics from node state.
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:303
virtual void SendPings()=0
Send ping message to all peers.
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:140
static const uint32_t DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN
Default number of non-mempool transactions to keep around for block reconstruction.
static constexpr bool DEFAULT_TXRECONCILIATION_ENABLE
Whether transaction reconciliation protocol should be enabled by default.
virtual void RelayTransaction(const uint256 &txid, const uint256 &wtxid)=0
Relay transaction to all peers.
virtual void UnitTestMisbehaving(NodeId peer_id)=0
static const uint32_t DEFAULT_MAX_ORPHAN_TRANSACTIONS
Default for -maxorphantx, maximum number of orphan transactions kept in memory.
static const bool DEFAULT_BLOCKSONLY
Default for blocks only.
Definition: net.h:81
Information about a peer.
Definition: net.h:672
std::vector< int > vHeightInFlight
Simple class for background tasks that should be run periodically or once "after a while"...
Definition: scheduler.h:39
bool ignore_incoming_txs
Whether this node is running in -blocksonly mode.
virtual ~PeerManager()=default
uint32_t max_extra_txs
Number of non-mempool transactions to keep around for block reconstruction.