|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
|
|
// Copyright (c) 2009-2022 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_NET_PROCESSING_H
|
|
|
|
#define BITCOIN_NET_PROCESSING_H
|
|
|
|
|
|
|
|
#include <net.h>
|
|
|
|
#include <validationinterface.h>
|
|
|
|
|
|
|
|
class AddrMan;
|
|
|
|
class CChainParams;
|
|
|
|
class CTxMemPool;
|
|
|
|
class ChainstateManager;
|
|
|
|
|
|
|
|
/** Default for -maxorphantx, maximum number of orphan transactions kept in memory */
|
|
|
|
static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100;
|
|
|
|
/** Default number of orphan+recently-replaced txn to keep around for block reconstruction */
|
|
|
|
static const unsigned int DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN = 100;
|
|
|
|
static const bool DEFAULT_PEERBLOOMFILTERS = false;
|
|
|
|
static const bool DEFAULT_PEERBLOCKFILTERS = false;
|
|
|
|
/** Threshold for marking a node to be discouraged, e.g. disconnected and added to the discouragement filter. */
|
|
|
|
static const int DISCOURAGEMENT_THRESHOLD{100};
|
|
|
|
/** Maximum number of outstanding CMPCTBLOCK requests for the same block. */
|
|
|
|
static const unsigned int MAX_CMPCTBLOCKS_INFLIGHT_PER_BLOCK = 3;
|
|
|
|
|
|
|
|
struct CNodeStateStats {
|
|
|
|
int nSyncHeight = -1;
|
|
|
|
int nCommonHeight = -1;
|
|
|
|
int m_starting_height = -1;
|
|
|
|
std::chrono::microseconds m_ping_wait;
|
|
|
|
std::vector<int> vHeightInFlight;
|
|
|
|
bool m_relay_txs;
|
|
|
|
CAmount m_fee_filter_received;
|
|
|
|
uint64_t m_addr_processed = 0;
|
|
|
|
uint64_t m_addr_rate_limited = 0;
|
|
|
|
bool m_addr_relay_enabled{false};
|
|
|
|
ServiceFlags their_services;
|
|
|
|
int64_t presync_height{-1};
|
|
|
|
};
|
|
|
|
|
|
|
|
class PeerManager : public CValidationInterface, public NetEventsInterface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static std::unique_ptr<PeerManager> make(CConnman& connman, AddrMan& addrman,
|
|
|
|
BanMan* banman, ChainstateManager& chainman,
|
|
|
|
CTxMemPool& pool, bool ignore_incoming_txs);
|
|
|
|
virtual ~PeerManager() { }
|
Permit disconnection of outbound peers on bad/slow chains
Currently we have no rotation of outbound peers. If an outbound peer
stops serving us blocks, or is on a consensus-incompatible chain with
less work than our tip (but otherwise valid headers), then we will never
disconnect that peer, even though that peer is using one of our 8
outbound connection slots. Because we rely on our outbound peers to
find an honest node in order to reach consensus, allowing an
incompatible peer to occupy one of those slots is undesirable,
particularly if it is possible for all such slots to be occupied by such
peers.
Protect against this by always checking to see if a peer's best known
block has less work than our tip, and if so, set a 20 minute timeout --
if the peer is still not known to have caught up to a chain with as much
work as ours after 20 minutes, then send a single getheaders message,
wait 2 more minutes, and if a better header hasn't been received by then,
disconnect that peer.
Note:
- we do not require that our peer sync to the same tip as ours, just an
equal or greater work tip. (Doing otherwise would risk partitioning the
network in the event of a chain split, and is also unnecessary.)
- we pick 4 of our outbound peers and do not subject them to this logic,
to be more conservative. We don't wish to permit temporary network
issues (or an attacker) to excessively disrupt network topology.
7 years ago
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to manually fetch block from a given peer. We must already have the header.
|
|
|
|
*
|
|
|
|
* @param[in] peer_id The peer id
|
|
|
|
* @param[in] block_index The blockindex
|
|
|
|
* @returns std::nullopt if a request was successfully made, otherwise an error message
|
|
|
|
*/
|
|
|
|
virtual std::optional<std::string> FetchBlock(NodeId peer_id, const CBlockIndex& block_index) = 0;
|
|
|
|
|
|
|
|
/** Begin running background tasks, should only be called once */
|
|
|
|
virtual void StartScheduledTasks(CScheduler& scheduler) = 0;
|
|
|
|
|
|
|
|
/** Get statistics from node state */
|
|
|
|
virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const = 0;
|
|
|
|
|
|
|
|
/** Whether this node ignores txs received over p2p. */
|
|
|
|
virtual bool IgnoresIncomingTxs() = 0;
|
|
|
|
|
|
|
|
/** Relay transaction to all peers. */
|
|
|
|
virtual void RelayTransaction(const uint256& txid, const uint256& wtxid) = 0;
|
|
|
|
|
|
|
|
/** Send ping message to all peers */
|
|
|
|
virtual void SendPings() = 0;
|
|
|
|
|
|
|
|
/** Set the best height */
|
|
|
|
virtual void SetBestHeight(int height) = 0;
|
|
|
|
|
|
|
|
/* Public for unit testing. */
|
|
|
|
virtual void UnitTestMisbehaving(NodeId peer_id, int howmuch) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound.
|
|
|
|
* Public for unit testing.
|
|
|
|
*/
|
|
|
|
virtual void CheckForStaleTipAndEvictPeers() = 0;
|
|
|
|
|
|
|
|
/** Process a single message from a peer. Public for fuzz testing */
|
|
|
|
virtual void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
|
|
|
|
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) = 0;
|
|
|
|
|
|
|
|
/** This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp */
|
|
|
|
virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // BITCOIN_NET_PROCESSING_H
|