Bitcoin
Classes | Namespaces | Typedefs | Functions | Variables
net_processing.cpp File Reference
#include <net_processing.h>
#include <addrman.h>
#include <banman.h>
#include <arith_uint256.h>
#include <blockencodings.h>
#include <chainparams.h>
#include <consensus/validation.h>
#include <hash.h>
#include <validation.h>
#include <merkleblock.h>
#include <netmessagemaker.h>
#include <netbase.h>
#include <policy/fees.h>
#include <policy/policy.h>
#include <primitives/block.h>
#include <primitives/transaction.h>
#include <random.h>
#include <reverse_iterator.h>
#include <scheduler.h>
#include <tinyformat.h>
#include <txmempool.h>
#include <util/system.h>
#include <util/strencodings.h>
#include <util/validation.h>
#include <memory>

Classes

struct  COrphanTx
 
struct  anonymous_namespace{net_processing.cpp}::QueuedBlock
 
struct  anonymous_namespace{net_processing.cpp}::IteratorComparator
 
struct  anonymous_namespace{net_processing.cpp}::CBlockReject
 
struct  anonymous_namespace{net_processing.cpp}::CNodeState
 
struct  anonymous_namespace{net_processing.cpp}::CNodeState::ChainSyncTimeoutState
 
struct  anonymous_namespace{net_processing.cpp}::CNodeState::TxDownloadState
 
class  anonymous_namespace{net_processing.cpp}::CompareInvMempoolOrder
 
class  CNetProcessingCleanup
 

Namespaces

 anonymous_namespace{net_processing.cpp}
 

Typedefs

typedef std::map< uint256, CTransactionRefanonymous_namespace{net_processing.cpp}::MapRelay
 

Functions

std::map< uint256, COrphanTx > mapOrphanTransactions GUARDED_BY (g_cs_orphans)
 
void EraseOrphansFor (NodeId peer)
 
void Misbehaving (NodeId nodeid, int howmuch, const std::string &message="") EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
int nSyncStarted anonymous_namespace{net_processing.cpp}::GUARDED_BY (cs_main)=0
 
std::atomic< int64_tanonymous_namespace{net_processing.cpp}::g_last_tip_update (0)
 
std::map< COutPoint, std::set< std::map< uint256, COrphanTx >::iterator, IteratorComparator > > mapOrphanTransactionsByPrev anonymous_namespace{net_processing.cpp}::GUARDED_BY (g_cs_orphans)=0
 For random eviction. More...
 
static CNodeState * anonymous_namespace{net_processing.cpp}::State (NodeId pnode) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
static void anonymous_namespace{net_processing.cpp}::UpdatePreferredDownload (CNode *node, CNodeState *state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
static void anonymous_namespace{net_processing.cpp}::PushNodeVersion (CNode *pnode, CConnman *connman, int64_t nTime)
 
static bool anonymous_namespace{net_processing.cpp}::MarkBlockAsReceived (const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
static bool anonymous_namespace{net_processing.cpp}::MarkBlockAsInFlight (NodeId nodeid, const uint256 &hash, const CBlockIndex *pindex=nullptr, std::list< QueuedBlock >::iterator **pit=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
static void anonymous_namespace{net_processing.cpp}::ProcessBlockAvailability (NodeId nodeid) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
static void anonymous_namespace{net_processing.cpp}::UpdateBlockAvailability (NodeId nodeid, const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
static void anonymous_namespace{net_processing.cpp}::MaybeSetPeerAsAnnouncingHeaderAndIDs (NodeId nodeid, CConnman *connman) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
static bool anonymous_namespace{net_processing.cpp}::TipMayBeStale (const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
static bool anonymous_namespace{net_processing.cpp}::CanDirectFetch (const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
static bool anonymous_namespace{net_processing.cpp}::PeerHasHeader (CNodeState *state, const CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
static void anonymous_namespace{net_processing.cpp}::FindNextBlocksToDownload (NodeId nodeid, unsigned int count, std::vector< const CBlockIndex * > &vBlocks, NodeId &nodeStaller, const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
void anonymous_namespace{net_processing.cpp}::EraseTxRequest (const uint256 &txid) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
int64_t anonymous_namespace{net_processing.cpp}::GetTxRequestTime (const uint256 &txid) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
void anonymous_namespace{net_processing.cpp}::UpdateTxRequestTime (const uint256 &txid, int64_t request_time) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
int64_t anonymous_namespace{net_processing.cpp}::CalculateTxGetDataTime (const uint256 &txid, int64_t current_time, bool use_inbound_delay) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
void anonymous_namespace{net_processing.cpp}::RequestTx (CNodeState *state, const uint256 &txid, int64_t nNow) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
void UpdateLastBlockAnnounceTime (NodeId node, int64_t time_in_seconds)
 
static bool IsOutboundDisconnectionCandidate (const CNode *node)
 
bool GetNodeStateStats (NodeId nodeid, CNodeStateStats &stats)
 
static void AddToCompactExtraTransactions (const CTransactionRef &tx) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
 
bool AddOrphanTx (const CTransactionRef &tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
 
static int EraseOrphanTx (uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
 
unsigned int LimitOrphanTxSize (unsigned int nMaxOrphans)
 
static bool TxRelayMayResultInDisconnect (const CValidationState &state)
 
static bool MaybePunishNode (NodeId nodeid, const CValidationState &state, bool via_compact_block, const std::string &message="")
 
static bool BlockRequestAllowed (const CBlockIndex *pindex, const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
static std::shared_ptr< const CBlock > most_recent_block GUARDED_BY (cs_most_recent_block)
 
static bool AlreadyHave (const CInv &inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
static void RelayTransaction (const CTransaction &tx, CConnman *connman)
 
static void RelayAddress (const CAddress &addr, bool fReachable, CConnman *connman)
 
static void ProcessGetBlockData (CNode *pfrom, const CChainParams &chainparams, const CInv &inv, CConnman *connman)
 
static void ProcessGetData (CNode *pfrom, const CChainParams &chainparams, CConnman *connman, const std::atomic< bool > &interruptMsgProc) LOCKS_EXCLUDED(cs_main)
 
static uint32_t GetFetchFlags (CNode *pfrom) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
static void SendBlockTransactions (const CBlock &block, const BlockTransactionsRequest &req, CNode *pfrom, CConnman *connman)
 
static bool ProcessHeadersMessage (CNode *pfrom, CConnman *connman, const std::vector< CBlockHeader > &headers, const CChainParams &chainparams, bool via_compact_block)
 
static void ProcessOrphanTx (CConnman *connman, std::set< uint256 > &orphan_work_set, std::list< CTransactionRef > &removed_txn) EXCLUSIVE_LOCKS_REQUIRED(cs_main
 
 AssertLockHeld (g_cs_orphans)
 
 while (!done &&!orphan_work_set.empty())
 
static bool ProcessMessage (CNode *pfrom, const std::string &strCommand, CDataStream &vRecv, int64_t nTimeReceived, const CChainParams &chainparams, CConnman *connman, const std::atomic< bool > &interruptMsgProc, bool enable_bip61)
 

Variables

static constexpr int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60
 
static constexpr int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60
 
static constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_BASE = 15 * 60 * 1000000
 
static constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER = 1000
 
static constexpr int32_t MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT = 4
 
static constexpr int64_t CHAIN_SYNC_TIMEOUT = 20 * 60
 
static constexpr int64_t STALE_CHECK_INTERVAL = 10 * 60
 
static constexpr int64_t EXTRA_PEER_CHECK_INTERVAL = 45
 
static constexpr int64_t MINIMUM_CONNECT_TIME = 30
 
static constexpr uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL
 
static constexpr int STALE_RELAY_AGE_LIMIT = 30 * 24 * 60 * 60
 
static constexpr int HISTORICAL_BLOCK_AGE = 7 * 24 * 60 * 60
 
static constexpr int32_t MAX_PEER_TX_IN_FLIGHT = 100
 
static constexpr int32_t MAX_PEER_TX_ANNOUNCEMENTS = 2 * MAX_INV_SZ
 
static constexpr int64_t INBOUND_PEER_TX_DELAY = 2 * 1000000
 
static constexpr int64_t GETDATA_TX_INTERVAL = 60 * 1000000
 
static constexpr int64_t MAX_GETDATA_RANDOM_DELAY = 2 * 1000000
 
static constexpr int64_t TX_EXPIRY_INTERVAL = 10 * GETDATA_TX_INTERVAL
 
static const unsigned int MAX_GETDATA_SZ = 1000
 
CCriticalSection g_cs_orphans
 
static constexpr unsigned int AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL = 24 * 60 * 60
 
static const unsigned int AVG_ADDRESS_BROADCAST_INTERVAL = 30
 
static const unsigned int INVENTORY_BROADCAST_INTERVAL = 5
 
static constexpr unsigned int INVENTORY_BROADCAST_MAX = 7 * INVENTORY_BROADCAST_INTERVAL
 
static constexpr unsigned int AVG_FEEFILTER_BROADCAST_INTERVAL = 10 * 60
 
static constexpr unsigned int MAX_FEEFILTER_CHANGE_DELAY = 5 * 60
 
limitedmap< uint256, int64_t > g_already_asked_for GUARDED_BY() anonymous_namespace{net_processing.cpp}::cs_main (MAX_INV_SZ)
 
static CCriticalSection cs_most_recent_block
 
std::set< NodeIdsetMisbehaving
 
bool done = false
 
static CNetProcessingCleanup instance_of_cnetprocessingcleanup
 

Function Documentation

◆ AddOrphanTx()

bool AddOrphanTx ( const CTransactionRef tx,
NodeId  peer 
)

◆ AddToCompactExtraTransactions()

static void AddToCompactExtraTransactions ( const CTransactionRef tx)
static

◆ AlreadyHave()

static bool AlreadyHave ( const CInv inv)
static

◆ AssertLockHeld()

AssertLockHeld ( g_cs_orphans  )

◆ BlockRequestAllowed()

static bool BlockRequestAllowed ( const CBlockIndex pindex,
const Consensus::Params consensusParams 
)
static

◆ EraseOrphansFor()

void EraseOrphansFor ( NodeId  peer)

◆ EraseOrphanTx()

static int EraseOrphanTx ( uint256  hash)
static

◆ GetFetchFlags()

static uint32_t GetFetchFlags ( CNode pfrom)
static

◆ GetNodeStateStats()

bool GetNodeStateStats ( NodeId  nodeid,
CNodeStateStats stats 
)

Get statistics from node state

◆ GUARDED_BY() [1/2]

std::map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY ( g_cs_orphans  )

◆ GUARDED_BY() [2/2]

static bool fWitnessesPresentInMostRecentCompactBlock GUARDED_BY ( cs_most_recent_block  )
static

◆ IsOutboundDisconnectionCandidate()

static bool IsOutboundDisconnectionCandidate ( const CNode node)
static

◆ LimitOrphanTxSize()

unsigned int LimitOrphanTxSize ( unsigned int  nMaxOrphans)

◆ MaybePunishNode()

static bool MaybePunishNode ( NodeId  nodeid,
const CValidationState state,
bool  via_compact_block,
const std::string &  message = "" 
)
static

Potentially ban a node based on the contents of a CValidationState object

Parameters
[in]via_compact_blockthis 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.
Returns
Returns true if the peer was punished (probably disconnected)

Changes here may need to be reflected in TxRelayMayResultInDisconnect().

◆ Misbehaving()

void Misbehaving ( NodeId  pnode,
int  howmuch,
const std::string &  message = "" 
)

Increase a node's misbehavior score.

Mark a misbehaving peer to be banned depending upon the value of -banscore.

◆ ProcessGetBlockData()

static void ProcessGetBlockData ( CNode pfrom,
const CChainParams chainparams,
const CInv inv,
CConnman connman 
)
static

◆ ProcessGetData()

static void ProcessGetData ( CNode pfrom,
const CChainParams chainparams,
CConnman connman,
const std::atomic< bool > &  interruptMsgProc 
)
static

◆ ProcessHeadersMessage()

static bool ProcessHeadersMessage ( CNode pfrom,
CConnman connman,
const std::vector< CBlockHeader > &  headers,
const CChainParams chainparams,
bool  via_compact_block 
)
static

◆ ProcessMessage()

static bool ProcessMessage ( CNode pfrom,
const std::string &  strCommand,
CDataStream vRecv,
int64_t  nTimeReceived,
const CChainParams chainparams,
CConnman connman,
const std::atomic< bool > &  interruptMsgProc,
bool  enable_bip61 
)
static

◆ ProcessOrphanTx()

static void ProcessOrphanTx ( CConnman connman,
std::set< uint256 > &  orphan_work_set,
std::list< CTransactionRef > &  removed_txn 
)
static

◆ RelayAddress()

static void RelayAddress ( const CAddress addr,
bool  fReachable,
CConnman connman 
)
static

◆ RelayTransaction()

static void RelayTransaction ( const CTransaction tx,
CConnman connman 
)
static

◆ SendBlockTransactions()

static void SendBlockTransactions ( const CBlock block,
const BlockTransactionsRequest req,
CNode pfrom,
CConnman connman 
)
inlinestatic

◆ TxRelayMayResultInDisconnect()

static bool TxRelayMayResultInDisconnect ( const CValidationState state)
static

Returns true if the given validation state result may result in a peer banning/disconnecting us. We use this to determine which unaccepted transactions from a whitelisted peer that we can safely relay.

◆ UpdateLastBlockAnnounceTime()

void UpdateLastBlockAnnounceTime ( NodeId  node,
int64_t  time_in_seconds 
)

◆ while()

while ( !done &&!orphan_work_set.  empty())

Variable Documentation

◆ AVG_ADDRESS_BROADCAST_INTERVAL

const unsigned int AVG_ADDRESS_BROADCAST_INTERVAL = 30
static

Average delay between peer address broadcasts in seconds.

◆ AVG_FEEFILTER_BROADCAST_INTERVAL

constexpr unsigned int AVG_FEEFILTER_BROADCAST_INTERVAL = 10 * 60
static

Average delay between feefilter broadcasts in seconds.

◆ AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL

constexpr unsigned int AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL = 24 * 60 * 60
static

Average delay between local address broadcasts in seconds.

◆ CHAIN_SYNC_TIMEOUT

constexpr int64_t CHAIN_SYNC_TIMEOUT = 20 * 60
static

Timeout for (unprotected) outbound peers to sync to our chainwork, in seconds

◆ cs_most_recent_block

CCriticalSection cs_most_recent_block
static

◆ done

bool done = false

◆ EXTRA_PEER_CHECK_INTERVAL

constexpr int64_t EXTRA_PEER_CHECK_INTERVAL = 45
static

How frequently to check for extra outbound peers and disconnect, in seconds

◆ g_cs_orphans

static void g_cs_orphans
Initial value:
{
limitedmap< uint256, int64_t > g_already_asked_for GUARDED_BY() cs_main(MAX_INV_SZ)
AssertLockHeld(g_cs_orphans)

◆ GETDATA_TX_INTERVAL

constexpr int64_t GETDATA_TX_INTERVAL = 60 * 1000000
static

How long to wait (in microseconds) before downloading a transaction from an additional peer

◆ HEADERS_DOWNLOAD_TIMEOUT_BASE

constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_BASE = 15 * 60 * 1000000
static

Headers download timeout expressed in microseconds Timeout = base + per_header * (expected number of headers)

◆ HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER

constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER = 1000
static

◆ HISTORICAL_BLOCK_AGE

constexpr int HISTORICAL_BLOCK_AGE = 7 * 24 * 60 * 60
static

Age after which a block is considered historical for purposes of rate limiting block relay. Set to one week, denominated in seconds.

◆ INBOUND_PEER_TX_DELAY

constexpr int64_t INBOUND_PEER_TX_DELAY = 2 * 1000000
static

How many microseconds to delay requesting transactions from inbound peers

◆ instance_of_cnetprocessingcleanup

CNetProcessingCleanup instance_of_cnetprocessingcleanup
static

◆ INVENTORY_BROADCAST_INTERVAL

const unsigned int INVENTORY_BROADCAST_INTERVAL = 5
static

Average delay between trickled inventory transmissions in seconds. Blocks and whitelisted receivers bypass this, outbound peers get half this delay.

◆ INVENTORY_BROADCAST_MAX

constexpr unsigned int INVENTORY_BROADCAST_MAX = 7 * INVENTORY_BROADCAST_INTERVAL
static

Maximum number of inventory items to send per transmission. Limits the impact of low-fee transaction floods.

◆ MAX_FEEFILTER_CHANGE_DELAY

constexpr unsigned int MAX_FEEFILTER_CHANGE_DELAY = 5 * 60
static

Maximum feefilter broadcast delay after significant change.

◆ MAX_GETDATA_RANDOM_DELAY

constexpr int64_t MAX_GETDATA_RANDOM_DELAY = 2 * 1000000
static

Maximum delay (in microseconds) for transaction requests to avoid biasing some peers over others.

◆ MAX_GETDATA_SZ

const unsigned int MAX_GETDATA_SZ = 1000
static

Limit to avoid sending big packets. Not used in processing incoming GETDATA for compatibility

◆ MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT

constexpr int32_t MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT = 4
static

Protect at least this many outbound peers from disconnection due to slow/ behind headers chain.

◆ MAX_PEER_TX_ANNOUNCEMENTS

constexpr int32_t MAX_PEER_TX_ANNOUNCEMENTS = 2 * MAX_INV_SZ
static

Maximum number of announced transactions from a peer

◆ MAX_PEER_TX_IN_FLIGHT

constexpr int32_t MAX_PEER_TX_IN_FLIGHT = 100
static

Maximum number of in-flight transactions from a peer

◆ MINIMUM_CONNECT_TIME

constexpr int64_t MINIMUM_CONNECT_TIME = 30
static

Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict, in seconds

◆ ORPHAN_TX_EXPIRE_INTERVAL

constexpr int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60
static

Minimum time between orphan transactions expire time checks in seconds

◆ ORPHAN_TX_EXPIRE_TIME

constexpr int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60
static

Expiration time for orphan transactions in seconds

◆ RANDOMIZER_ID_ADDRESS_RELAY

constexpr uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL
static

SHA256("main address relay")[0:8]

◆ setMisbehaving

std::set<NodeId> setMisbehaving

◆ STALE_CHECK_INTERVAL

constexpr int64_t STALE_CHECK_INTERVAL = 10 * 60
static

How frequently to check for stale tips, in seconds

◆ STALE_RELAY_AGE_LIMIT

constexpr int STALE_RELAY_AGE_LIMIT = 30 * 24 * 60 * 60
static

Age after which a stale block will no longer be served if requested as protection against fingerprinting. Set to one month, denominated in seconds.

◆ TX_EXPIRY_INTERVAL

constexpr int64_t TX_EXPIRY_INTERVAL = 10 * GETDATA_TX_INTERVAL
static

How long to wait (in microseconds) before expiring an in-flight getdata request to a peer