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>
|
|
|
|
#include <validationinterface.h>
|
|
|
|
#include <consensus/params.h>
|
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)
|
2016-06-20 14:45:34 +02:00
|
|
|
/** Expiration time for orphan transactions in seconds */
|
|
|
|
static const int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60;
|
|
|
|
/** Minimum time between orphan transactions expire time checks in seconds */
|
|
|
|
static const int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60;
|
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;
|
2016-06-20 14:45:34 +02:00
|
|
|
|
2017-08-23 16:21:08 +02:00
|
|
|
/** Headers download timeout expressed in microseconds
|
|
|
|
* Timeout = base + per_header * (expected number of headers) */
|
|
|
|
static constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_BASE = 15 * 60 * 1000000; // 15 minutes
|
|
|
|
static constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER = 1000; // 1ms/header
|
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
|
|
|
/** Protect at least this many outbound peers from disconnection due to slow/
|
|
|
|
* behind headers chain.
|
|
|
|
*/
|
|
|
|
static constexpr int32_t MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT = 4;
|
|
|
|
/** Timeout for (unprotected) outbound peers to sync to our chainwork, in seconds */
|
|
|
|
static constexpr int64_t CHAIN_SYNC_TIMEOUT = 20 * 60; // 20 minutes
|
2017-08-23 16:21:08 +02:00
|
|
|
|
2017-11-02 20:13:17 +01:00
|
|
|
/** How frequently to check for stale tips, in seconds */
|
2019-09-26 20:53:22 +02:00
|
|
|
static constexpr int64_t STALE_CHECK_INTERVAL = 150; // 2.5 minutes (~block interval)
|
2017-11-02 20:13:17 +01:00
|
|
|
/** How frequently to check for extra outbound peers and disconnect, in seconds */
|
|
|
|
static constexpr int64_t EXTRA_PEER_CHECK_INTERVAL = 45;
|
|
|
|
/** Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict, in seconds */
|
|
|
|
static constexpr int64_t MINIMUM_CONNECT_TIME = 30;
|
|
|
|
|
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:
|
2017-11-02 20:13:17 +01:00
|
|
|
explicit PeerLogicValidation(CConnman* connmanIn, CScheduler &scheduler);
|
2017-08-09 02:19:06 +02:00
|
|
|
|
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;
|
|
|
|
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override;
|
|
|
|
void BlockChecked(const CBlock& block, const CValidationState& state) override;
|
|
|
|
void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) override;
|
2017-09-08 01:00:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
void InitializeNode(CNode* pnode) override;
|
|
|
|
void FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) override;
|
|
|
|
/** Process protocol messages received from a given node */
|
|
|
|
bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override;
|
|
|
|
/**
|
|
|
|
* Send queued protocol messages to be sent to a give node.
|
|
|
|
*
|
|
|
|
* @param[in] pto The node which we are sending messages to.
|
|
|
|
* @param[in] interrupt Interrupt condition for processing threads
|
|
|
|
* @return True if there is more work to be done
|
|
|
|
*/
|
|
|
|
bool SendMessages(CNode* pto, std::atomic<bool>& interrupt) override;
|
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);
|
2017-11-02 20:13:17 +01:00
|
|
|
void CheckForStaleTipAndEvictPeers(const Consensus::Params &consensusParams);
|
|
|
|
void EvictExtraOutboundPeers(int64_t time_in_seconds);
|
|
|
|
|
|
|
|
private:
|
|
|
|
int64_t m_stale_tip_check_time; //! Next time to check for stale tip
|
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);
|
|
|
|
/** Increase a node's misbehavior score. */
|
2018-02-06 12:34:17 +01:00
|
|
|
void Misbehaving(NodeId nodeid, int howmuch, const std::string& message="");
|
2018-10-26 15:59:00 +02:00
|
|
|
bool IsBanned(NodeId nodeid);
|
2017-08-09 02:19:06 +02:00
|
|
|
|
2020-04-06 13:25:38 +02:00
|
|
|
void EraseObjectRequest(const uint256& hash);
|
|
|
|
void RequestObject(NodeId nodeId, const CInv& inv, int64_t nNow);
|
|
|
|
|
2017-08-09 02:19:06 +02:00
|
|
|
#endif // BITCOIN_NET_PROCESSING_H
|