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 <net.h>
|
2020-04-16 00:26:07 +02:00
|
|
|
#include <validation.h>
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <validationinterface.h>
|
|
|
|
#include <consensus/params.h>
|
2018-09-27 17:18:12 +02:00
|
|
|
#include <sync.h>
|
|
|
|
|
|
|
|
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;
|
2020-04-19 13:04:31 +02:00
|
|
|
/** Default for BIP61 (sending reject messages) */
|
|
|
|
static constexpr bool DEFAULT_ENABLE_BIP61 = true;
|
|
|
|
|
2017-09-08 01:00:49 +02:00
|
|
|
class PeerLogicValidation : public CValidationInterface, public NetEventsInterface {
|
2017-08-09 02:19:06 +02:00
|
|
|
private:
|
2017-11-02 20:13:17 +01:00
|
|
|
CConnman* const connman;
|
2017-08-09 02:19:06 +02:00
|
|
|
|
|
|
|
public:
|
2018-07-14 05:07:19 +02:00
|
|
|
explicit PeerLogicValidation(CConnman* connmanIn, CScheduler &scheduler, bool enable_bip61);
|
2017-08-09 02:19:06 +02:00
|
|
|
|
2018-03-06 21:45:41 +01:00
|
|
|
/**
|
|
|
|
* Overridden from CValidationInterface.
|
|
|
|
*/
|
2017-04-10 21:06:42 +02:00
|
|
|
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected, const std::vector<CTransactionRef>& vtxConflicted) 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.
|
|
|
|
*/
|
2017-04-10 21:06:42 +02:00
|
|
|
void BlockChecked(const CBlock& block, const CValidationState& 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 */
|
2017-09-08 01:00:49 +02:00
|
|
|
void FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) 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 */
|
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
|
|
|
void ConsiderEviction(CNode *pto, int64_t time_in_seconds);
|
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);
|
2017-11-02 20:13:17 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
int64_t m_stale_tip_check_time; //! Next time to check for stale tip
|
2018-07-14 05:07:19 +02:00
|
|
|
|
|
|
|
/** Enable BIP61 (sending reject messages) */
|
|
|
|
const bool m_enable_bip61;
|
2017-08-09 02:19:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CNodeStateStats {
|
|
|
|
int nMisbehavior;
|
|
|
|
int nSyncHeight;
|
|
|
|
int nCommonHeight;
|
|
|
|
std::vector<int> vHeightInFlight;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Get statistics from node state */
|
|
|
|
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats);
|
2018-10-26 15:59:00 +02:00
|
|
|
bool IsBanned(NodeId nodeid);
|
2017-08-09 02:19:06 +02:00
|
|
|
|
2020-04-16 00:14:13 +02:00
|
|
|
// Upstream moved this into net_processing.cpp (13417), however since we use Misbehaving in a number of dash specific
|
|
|
|
// files such as mnauth.cpp and governance.cpp it makes sense to keep it in the header
|
|
|
|
/** Increase a node's misbehavior score. */
|
2020-04-16 00:26:07 +02:00
|
|
|
void Misbehaving(NodeId nodeid, int howmuch, const std::string& message="") EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
2020-04-16 00:14:13 +02:00
|
|
|
|
2020-04-07 13:25:29 +02:00
|
|
|
void EraseObjectRequest(NodeId nodeId, const CInv& inv);
|
2020-04-08 14:55:27 +02:00
|
|
|
void RequestObject(NodeId nodeId, const CInv& inv, std::chrono::microseconds current_time, bool fForce=false);
|
2020-04-06 13:58:01 +02:00
|
|
|
size_t GetRequestedObjectCount(NodeId nodeId);
|
2020-04-06 13:25:38 +02:00
|
|
|
|
2017-08-09 02:19:06 +02:00
|
|
|
#endif // BITCOIN_NET_PROCESSING_H
|