2017-08-09 02:19:06 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
|
|
// Copyright (c) 2009-2015 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
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <consensus/params.h>
|
2022-04-17 12:52:51 +02:00
|
|
|
#include <net.h>
|
2018-09-27 17:18:12 +02:00
|
|
|
#include <sync.h>
|
2022-04-17 12:52:51 +02:00
|
|
|
#include <validationinterface.h>
|
|
|
|
|
2023-04-28 07:17:42 +02:00
|
|
|
class BlockTransactionsRequest;
|
|
|
|
class BlockValidationState;
|
2023-02-16 07:34:06 +01:00
|
|
|
class CAddrMan;
|
2023-04-28 07:17:42 +02:00
|
|
|
class CBlockHeader;
|
2022-04-17 12:52:51 +02:00
|
|
|
class CTxMemPool;
|
2022-05-05 20:07:00 +02:00
|
|
|
class ChainstateManager;
|
2023-04-28 07:17:42 +02:00
|
|
|
class TxValidationState;
|
2022-11-07 19:09:44 +01:00
|
|
|
struct LLMQContext;
|
2022-09-22 13:14:48 +02:00
|
|
|
|
2018-09-27 17:18:12 +02:00
|
|
|
extern CCriticalSection cs_main;
|
2017-08-09 02:19:06 +02:00
|
|
|
|
2019-09-30 15:34:13 +02:00
|
|
|
/** Default for -maxorphantxsize, maximum size in megabytes the orphan map can grow before entries are removed */
|
|
|
|
static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS_SIZE = 10; // this allows around 100 TXs of max size (and many more of normal size)
|
2019-09-25 13:03:45 +02:00
|
|
|
/** Default number of orphan+recently-replaced txn to keep around for block reconstruction */
|
|
|
|
static const unsigned int DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN = 100;
|
2021-07-18 06:20:16 +02:00
|
|
|
static const bool DEFAULT_PEERBLOOMFILTERS = true;
|
2021-09-19 06:31:43 +02:00
|
|
|
static const bool DEFAULT_PEERBLOCKFILTERS = false;
|
2020-04-19 13:04:31 +02:00
|
|
|
|
2023-04-27 09:46:47 +02:00
|
|
|
struct CNodeStateStats {
|
|
|
|
int m_misbehavior_score = 0;
|
|
|
|
int nSyncHeight = -1;
|
|
|
|
int nCommonHeight = -1;
|
|
|
|
std::vector<int> vHeightInFlight;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data structure for an individual peer. This struct is not protected by
|
|
|
|
* cs_main since it does not contain validation-critical data.
|
|
|
|
*
|
|
|
|
* Memory is owned by shared pointers and this object is destructed when
|
|
|
|
* the refcount drops to zero.
|
|
|
|
*
|
|
|
|
* TODO: move most members from CNodeState to this structure.
|
|
|
|
* TODO: move remaining application-layer data members from CNode to this structure.
|
|
|
|
*/
|
|
|
|
struct Peer {
|
|
|
|
/** Same id as the CNode object for this peer */
|
|
|
|
const NodeId m_id{0};
|
|
|
|
|
|
|
|
/** Protects misbehavior data members */
|
|
|
|
Mutex m_misbehavior_mutex;
|
|
|
|
/** Accumulated misbehavior score for this peer */
|
|
|
|
int m_misbehavior_score GUARDED_BY(m_misbehavior_mutex){0};
|
|
|
|
/** Whether this peer should be disconnected and marked as discouraged (unless it has the noban permission). */
|
|
|
|
bool m_should_discourage GUARDED_BY(m_misbehavior_mutex){false};
|
|
|
|
|
|
|
|
/** Set of txids to reconsider once their parent transactions have been accepted **/
|
|
|
|
std::set<uint256> m_orphan_work_set GUARDED_BY(g_cs_orphans);
|
|
|
|
|
|
|
|
/** Protects m_getdata_requests **/
|
|
|
|
Mutex m_getdata_requests_mutex;
|
|
|
|
/** Work queue of items requested by this peer **/
|
|
|
|
std::deque<CInv> m_getdata_requests GUARDED_BY(m_getdata_requests_mutex);
|
|
|
|
|
|
|
|
explicit Peer(NodeId id) : m_id(id) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
using PeerRef = std::shared_ptr<Peer>;
|
|
|
|
|
2023-04-28 07:17:42 +02:00
|
|
|
class PeerManager final : public CValidationInterface, public NetEventsInterface {
|
2017-08-09 02:19:06 +02:00
|
|
|
public:
|
2023-04-28 07:17:42 +02:00
|
|
|
PeerManager(const CChainParams& chainparams, CConnman& connman, CAddrMan& addrman, BanMan* banman, CScheduler &scheduler,
|
2023-04-27 09:21:41 +02:00
|
|
|
ChainstateManager& chainman, CTxMemPool& pool, std::unique_ptr<LLMQContext>& llmq_ctx, bool ignore_incoming_txs);
|
2017-08-09 02:19:06 +02:00
|
|
|
|
2018-03-06 21:45:41 +01:00
|
|
|
/**
|
|
|
|
* Overridden from CValidationInterface.
|
|
|
|
*/
|
2020-03-19 17:09:15 +01:00
|
|
|
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override;
|
2020-01-31 14:42:50 +01:00
|
|
|
void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) override;
|
2018-03-06 21:45:41 +01:00
|
|
|
/**
|
|
|
|
* Overridden from CValidationInterface.
|
|
|
|
*/
|
2017-04-10 21:06:42 +02:00
|
|
|
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override;
|
2018-03-06 21:45:41 +01:00
|
|
|
/**
|
|
|
|
* Overridden from CValidationInterface.
|
|
|
|
*/
|
2019-10-30 15:27:22 +01:00
|
|
|
void BlockChecked(const CBlock& block, const BlockValidationState& state) override;
|
2018-03-06 21:45:41 +01:00
|
|
|
/**
|
|
|
|
* Overridden from CValidationInterface.
|
|
|
|
*/
|
2017-04-10 21:06:42 +02:00
|
|
|
void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) override;
|
2017-09-08 01:00:49 +02:00
|
|
|
|
2018-03-06 21:45:41 +01:00
|
|
|
/** Initialize a peer by adding it to mapNodeState and pushing a message requesting its version */
|
2017-09-08 01:00:49 +02:00
|
|
|
void InitializeNode(CNode* pnode) override;
|
2018-03-06 21:45:41 +01:00
|
|
|
/** Handle removal of a peer by updating various state and removing it from mapNodeState */
|
2023-02-16 07:34:06 +01:00
|
|
|
void FinalizeNode(const CNode& node) override;
|
2018-07-09 16:40:37 +02:00
|
|
|
/**
|
|
|
|
* Process protocol messages received from a given node
|
|
|
|
*
|
|
|
|
* @param[in] pfrom The node which we have received messages from.
|
|
|
|
* @param[in] interrupt Interrupt condition for processing threads
|
|
|
|
*/
|
2020-06-18 20:51:24 +02:00
|
|
|
bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override;
|
2017-09-08 01:00:49 +02:00
|
|
|
/**
|
|
|
|
* Send queued protocol messages to be sent to a give node.
|
|
|
|
*
|
|
|
|
* @param[in] pto The node which we are sending messages to.
|
|
|
|
* @return True if there is more work to be done
|
|
|
|
*/
|
2018-07-09 16:40:37 +02:00
|
|
|
bool SendMessages(CNode* pto) override EXCLUSIVE_LOCKS_REQUIRED(pto->cs_sendProcessing);
|
Merge #11490: Disconnect from outbound peers with bad headers chains
e065249 Add unit test for outbound peer eviction (Suhas Daftuar)
5a6d00c Permit disconnection of outbound peers on bad/slow chains (Suhas Daftuar)
c60fd71 Disconnecting from bad outbound peers in IBD (Suhas Daftuar)
Pull request description:
The first commit will disconnect an outbound peer that serves us a headers chain with insufficient work while we're in IBD.
The second commit introduces a way to disconnect outbound peers whose chains fall out of sync with ours:
For a given outbound peer, we check whether their best known block (which is known from the blocks they announce to us) has at least as much work as our tip. If it doesn't, we set a 20 minute timeout, and if we still haven't heard about a block with as much work as our tip had when we set the timeout, then we send a single getheaders message, and wait 2 more minutes. If after two minutes their best known block has insufficient work, we disconnect that peer.
We protect 4 of our outbound peers (who provide some "good" headers chains, ie a chain with at least as much work as our tip at some point) from being subject to this logic, to prevent excessive network topology changes as a result of this algorithm, while still ensuring that we have a reasonable number of nodes not known to be on bogus chains.
We also don't require our peers to be on the same chain as us, to prevent accidental partitioning of the network in the event of a chain split. Note that if our peers are ever on a more work chain than our tip, then we will download and validate it, and then either reorg to it, or learn of a consensus incompatibility with that peer and disconnect. This PR is designed to protect against peers that are on a less work chain which we may never try to download and validate.
Tree-SHA512: 2e0169a1dd8a7fb95980573ac4a201924bffdd724c19afcab5efcef076fdbe1f2cec7dc5f5d7e0a6327216f56d3828884f73642e00c8534b56ec2bb4c854a656
2017-10-26 21:53:19 +02:00
|
|
|
|
2018-03-06 21:45:41 +01:00
|
|
|
/** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
|
2020-05-22 17:52:08 +02:00
|
|
|
void ConsiderEviction(CNode& pto, int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
2018-03-06 21:45:41 +01:00
|
|
|
/** Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound */
|
2017-11-02 20:13:17 +01:00
|
|
|
void CheckForStaleTipAndEvictPeers(const Consensus::Params &consensusParams);
|
2018-03-06 21:45:41 +01:00
|
|
|
/** If we have extra outbound peers, try to disconnect the one with the oldest block announcement */
|
2018-09-27 17:18:12 +02:00
|
|
|
void EvictExtraOutboundPeers(int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
2022-05-06 06:04:50 +02:00
|
|
|
/** Retrieve unbroadcast transactions from the mempool and reattempt sending to peers */
|
|
|
|
void ReattemptInitialBroadcast(CScheduler& scheduler) const;
|
2020-08-12 12:38:56 +02:00
|
|
|
/** Process a single message from a peer. Public for fuzz testing */
|
2023-04-28 07:17:42 +02:00
|
|
|
void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv, int64_t nTimeReceived, const std::atomic<bool>& interruptMsgProc);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increment peer's misbehavior score. If the new value surpasses banscore (specified on startup or by default), mark node to be discouraged, meaning the peer might be disconnected & added to the discouragement filter.
|
|
|
|
*/
|
|
|
|
void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message = "");
|
2017-11-02 20:13:17 +01:00
|
|
|
|
2023-04-27 09:46:47 +02:00
|
|
|
/** Get statistics from node state */
|
|
|
|
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats);
|
|
|
|
|
2023-04-28 08:36:19 +02:00
|
|
|
/** Set the best height */
|
|
|
|
void SetBestHeight(int height) { m_best_height = height; };
|
|
|
|
|
2023-04-27 09:21:41 +02:00
|
|
|
/** Whether this node ignores txs received over p2p. */
|
|
|
|
bool IgnoresIncomingTxs() { return m_ignore_incoming_txs; };
|
|
|
|
|
2023-04-27 09:46:47 +02:00
|
|
|
bool IsBanned(NodeId pnode) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
|
|
|
|
2017-11-02 20:13:17 +01:00
|
|
|
private:
|
2023-04-27 09:46:47 +02:00
|
|
|
/** Get a shared pointer to the Peer object.
|
|
|
|
* May return an empty shared_ptr if the Peer object can't be found. */
|
|
|
|
PeerRef GetPeerRef(NodeId id) const;
|
|
|
|
|
|
|
|
/** Get a shared pointer to the Peer object and remove it from m_peer_map.
|
|
|
|
* May return an empty shared_ptr if the Peer object can't be found. */
|
|
|
|
PeerRef RemovePeer(NodeId id);
|
|
|
|
|
2023-04-28 07:17:42 +02:00
|
|
|
/**
|
|
|
|
* Potentially mark a node discouraged based on the contents of a BlockValidationState object
|
|
|
|
*
|
|
|
|
* @param[in] via_compact_block this bool is passed in because net_processing should
|
|
|
|
* punish peers differently depending on whether the data was provided in a compact
|
|
|
|
* block message or not. If the compact block had a valid header, but contained invalid
|
|
|
|
* txs, the peer should not be punished. See BIP 152.
|
|
|
|
*
|
|
|
|
* @return Returns true if the peer was punished (probably disconnected)
|
|
|
|
*/
|
|
|
|
bool MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState& state,
|
|
|
|
bool via_compact_block, const std::string& message = "");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Potentially ban a node based on the contents of a TxValidationState object
|
|
|
|
*
|
|
|
|
* @return Returns true if the peer was punished (probably disconnected)
|
|
|
|
*
|
|
|
|
* Changes here may need to be reflected in TxRelayMayResultInDisconnect().
|
|
|
|
*/
|
|
|
|
bool MaybePunishNodeForTx(NodeId nodeid, const TxValidationState& state, const std::string& message = "");
|
|
|
|
|
|
|
|
/** Maybe disconnect a peer and discourage future connections from its address.
|
|
|
|
*
|
|
|
|
* @param[in] pnode The node to check.
|
|
|
|
* @return True if the peer was marked for disconnection in this function
|
|
|
|
*/
|
|
|
|
bool MaybeDiscourageAndDisconnect(CNode& pnode);
|
|
|
|
|
|
|
|
void ProcessOrphanTx(std::set<uint256>& orphan_work_set)
|
|
|
|
EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_cs_orphans);
|
|
|
|
/** Process a single headers message from a peer. */
|
|
|
|
void ProcessHeadersMessage(CNode& pfrom, const std::vector<CBlockHeader>& headers, bool via_compact_block);
|
|
|
|
|
|
|
|
void SendBlockTransactions(CNode& pfrom, const CBlock& block, const BlockTransactionsRequest& req);
|
|
|
|
|
2023-04-27 09:21:41 +02:00
|
|
|
/** Send a version message to a peer */
|
|
|
|
void PushNodeVersion(CNode& pnode, int64_t nTime);
|
|
|
|
|
2023-04-28 07:17:42 +02:00
|
|
|
const CChainParams& m_chainparams;
|
|
|
|
CConnman& m_connman;
|
|
|
|
/** Pointer to this node's banman. May be nullptr - check existence before dereferencing. */
|
|
|
|
BanMan* const m_banman;
|
|
|
|
CAddrMan& m_addrman;
|
|
|
|
ChainstateManager& m_chainman;
|
|
|
|
CTxMemPool& m_mempool;
|
|
|
|
std::unique_ptr<LLMQContext>& m_llmq_ctx;
|
|
|
|
|
2023-04-28 08:36:19 +02:00
|
|
|
/** The height of the best chain */
|
|
|
|
std::atomic<int> m_best_height{-1};
|
|
|
|
|
2018-10-01 03:13:42 +02:00
|
|
|
int64_t m_stale_tip_check_time; //!< Next time to check for stale tip
|
2017-08-09 02:19:06 +02:00
|
|
|
|
2023-04-28 08:36:19 +02:00
|
|
|
/** Whether this node is running in blocks only mode */
|
2023-04-27 09:21:41 +02:00
|
|
|
const bool m_ignore_incoming_txs;
|
|
|
|
|
2023-04-27 09:46:47 +02:00
|
|
|
/** Protects m_peer_map */
|
|
|
|
mutable Mutex m_peer_mutex;
|
|
|
|
/**
|
|
|
|
* Map of all Peer objects, keyed by peer id. This map is protected
|
|
|
|
* by the m_peer_mutex. Once a shared pointer reference is
|
|
|
|
* taken, the lock may be released. Individual fields are protected by
|
|
|
|
* their own locks.
|
|
|
|
*/
|
|
|
|
std::map<NodeId, PeerRef> m_peer_map GUARDED_BY(m_peer_mutex);
|
2017-08-09 02:19:06 +02:00
|
|
|
};
|
|
|
|
|
2021-07-10 16:46:33 +02:00
|
|
|
void EraseObjectRequest(NodeId nodeId, const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
|
|
|
void RequestObject(NodeId nodeId, const CInv& inv, std::chrono::microseconds current_time, bool fForce=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
|
|
|
size_t GetRequestedObjectCount(NodeId nodeId) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
2020-04-06 13:25:38 +02:00
|
|
|
|
2021-12-12 13:27:10 +01:00
|
|
|
/** Relay transaction to every node */
|
|
|
|
void RelayTransaction(const uint256&, const CConnman& connman);
|
|
|
|
|
2017-08-09 02:19:06 +02:00
|
|
|
#endif // BITCOIN_NET_PROCESSING_H
|