Bitcoin Core  31.0.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-present 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/amount.h>
10 #include <net.h>
11 #include <node/txorphanage.h>
12 #include <private_broadcast.h>
13 #include <protocol.h>
14 #include <threadsafety.h>
15 #include <uint256.h>
16 #include <util/expected.h>
17 #include <validationinterface.h>
18 
19 #include <atomic>
20 #include <chrono>
21 #include <cstdint>
22 #include <memory>
23 #include <optional>
24 #include <string>
25 #include <vector>
26 
27 class AddrMan;
28 class CTxMemPool;
29 class ChainstateManager;
30 class BanMan;
31 class CBlockIndex;
32 class CScheduler;
33 class DataStream;
34 class uint256;
35 
36 namespace node {
37 class Warnings;
38 } // namespace node
39 
41 static constexpr bool DEFAULT_TXRECONCILIATION_ENABLE{false};
44 static const uint32_t DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN{100};
45 static const bool DEFAULT_PEERBLOOMFILTERS = false;
46 static const bool DEFAULT_PEERBLOCKFILTERS = false;
48 static const unsigned int MAX_CMPCTBLOCKS_INFLIGHT_PER_BLOCK = 3;
51 static const unsigned int MAX_HEADERS_RESULTS = 2000;
52 
54  int nSyncHeight = -1;
55  int nCommonHeight = -1;
57  std::chrono::microseconds m_ping_wait;
58  std::vector<int> vHeightInFlight;
60  int m_inv_to_send = 0;
61  uint64_t m_last_inv_seq{0};
63  uint64_t m_addr_processed = 0;
64  uint64_t m_addr_rate_limited = 0;
65  bool m_addr_relay_enabled{false};
67  int64_t presync_height{-1};
68  std::chrono::seconds time_offset{0};
69 };
70 
72  std::chrono::seconds median_outbound_time_offset{0s};
73  bool ignores_incoming_txs{false};
74 };
75 
77 {
78 public:
79  struct Options {
88  bool capture_messages{false};
91  bool deterministic_rng{false};
97  };
98 
99  static std::unique_ptr<PeerManager> make(CConnman& connman, AddrMan& addrman,
100  BanMan* banman, ChainstateManager& chainman,
101  CTxMemPool& pool, node::Warnings& warnings, Options opts);
102  virtual ~PeerManager() = default;
103 
110  virtual util::Expected<void, std::string> FetchBlock(NodeId peer_id, const CBlockIndex& block_index) = 0;
111 
113  virtual void StartScheduledTasks(CScheduler& scheduler) = 0;
114 
116  virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const = 0;
117 
118  virtual std::vector<node::TxOrphanage::OrphanInfo> GetOrphanTransactions() = 0;
119 
121  virtual PeerManagerInfo GetInfo() const = 0;
122 
124  virtual std::vector<PrivateBroadcast::TxBroadcastInfo> GetPrivateBroadcastInfo() const = 0;
125 
136  virtual std::vector<CTransactionRef> AbortPrivateBroadcast(const uint256& id) = 0;
137 
144  virtual void InitiateTxBroadcastToAll(const Txid& txid, const Wtxid& wtxid) = 0;
145 
150  virtual void InitiateTxBroadcastPrivate(const CTransactionRef& tx) = 0;
151 
153  virtual void SendPings() = 0;
154 
156  virtual void SetBestBlock(int height, std::chrono::seconds time) = 0;
157 
158  /* Public for unit testing. */
159  virtual void UnitTestMisbehaving(NodeId peer_id) = 0;
160 
165  virtual void CheckForStaleTipAndEvictPeers() = 0;
166 
168  virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) = 0;
169 
191  virtual ServiceFlags GetDesirableServiceFlags(ServiceFlags services) const = 0;
192 };
193 
194 #endif // BITCOIN_NET_PROCESSING_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
bool reconcile_txs
Whether transaction reconciliation protocol is enabled.
static constexpr bool DEFAULT_PRIVATE_BROADCAST
Default for -privatebroadcast.
Definition: net.h:89
Definition: banman.h:63
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
virtual void InitiateTxBroadcastPrivate(const CTransactionRef &tx)=0
Initiate a private transaction broadcast.
Interface for managing multiple Chainstate objects, where each chainstate is associated with chainsta...
Definition: validation.h:939
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
virtual util::Expected< void, std::string > FetchBlock(NodeId peer_id, const CBlockIndex &block_index)=0
Attempt to manually fetch block from a given peer.
static std::unique_ptr< PeerManager > make(CConnman &connman, AddrMan &addrman, BanMan *banman, ChainstateManager &chainman, CTxMemPool &pool, node::Warnings &warnings, Options opts)
virtual std::vector< PrivateBroadcast::TxBroadcastInfo > GetPrivateBroadcastInfo() const =0
Get info about transactions currently being privately broadcast.
bool private_broadcast
Whether private broadcast is used for sending transactions.
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:1027
Implement this to subscribe to events generated in validation and mempool.
Stochastic address manager.
Definition: addrman.h:88
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.
CAmount m_fee_filter_received
virtual std::vector< node::TxOrphanage::OrphanInfo > GetOrphanTransactions()=0
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:132
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:103
int64_t presync_height
Definition: net.h:1071
std::chrono::microseconds m_ping_wait
Definition: messages.h:21
bool capture_messages
Whether all P2P messages are captured to disk.
256-bit opaque blob.
Definition: uint256.h:195
ServiceFlags their_services
static const bool DEFAULT_PEERBLOOMFILTERS
The util::Expected class provides a standard way for low-level functions to return either error value...
Definition: expected.h:44
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:186
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:93
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 UnitTestMisbehaving(NodeId peer_id)=0
virtual void InitiateTxBroadcastToAll(const Txid &txid, const Wtxid &wtxid)=0
Initiate a transaction broadcast to eligible peers.
uint64_t m_last_inv_seq
static const bool DEFAULT_BLOCKSONLY
Default for blocks only.
Definition: net.h:85
std::vector< int > vHeightInFlight
Simple class for background tasks that should be run periodically or once "after a while"...
Definition: scheduler.h:39
virtual std::vector< CTransactionRef > AbortPrivateBroadcast(const uint256 &id)=0
Abort private broadcast attempts for transactions currently being privately broadcast.
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.