Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2024-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_NODE_TXDOWNLOADMAN_H6#define BITCOIN_NODE_TXDOWNLOADMAN_H8#include <net.h>9#include <node/txorphanage.h>10#include <policy/packages.h>12#include <cstdint>13#include <memory>15class CBlock;16class CRollingBloomFilter;17class CTxMemPool;18class GenTxid;19class TxRequestTracker;20namespace node {21class TxDownloadManagerImpl;23/** Maximum number of in-flight transaction requests from a peer. It is not a hard limit, but the threshold at which24* point the OVERLOADED_PEER_TX_DELAY kicks in. */25inline constexpr int32_t MAX_PEER_TX_REQUEST_IN_FLIGHT = 100;26/** Maximum number of transactions to consider for requesting, per peer. It provides a reasonable DoS limit to27* per-peer memory usage spent on announcements, while covering peers continuously sending INVs at the maximum28* rate (by our own policy, see DEFAULT_TX_SEND_RATE) for several minutes, while not receiving29* the actual transaction (from any peer) in response to requests for them. */30inline constexpr int32_t MAX_PEER_TX_ANNOUNCEMENTS = 5000;31/** How long to delay requesting transactions via txids, if we have wtxid-relaying peers */32inline constexpr auto TXID_RELAY_DELAY{2s};33/** How long to delay requesting transactions from non-preferred peers */34inline constexpr auto NONPREF_PEER_TX_DELAY{2s};35/** How long to delay requesting transactions from overloaded peers (see MAX_PEER_TX_REQUEST_IN_FLIGHT). */36inline constexpr auto OVERLOADED_PEER_TX_DELAY{2s};37/** How long to wait before downloading a transaction from an additional peer */38inline constexpr auto GETDATA_TX_INTERVAL{60s};39struct TxDownloadOptions {40/** Read-only reference to mempool. */41const CTxMemPool& m_mempool;42/** Instantiate TxRequestTracker as deterministic (used for tests). */43bool m_deterministic_txrequest{false};44};45struct TxDownloadConnectionInfo {46/** Whether this peer is preferred for transaction download. */47const bool m_preferred;48/** Whether this peer has Relay permissions. */49const bool m_relay_permissions;50/** Whether this peer supports wtxid relay. */51const bool m_wtxid_relay;52};53struct PackageToValidate {54Package m_txns;55std::vector<NodeId> m_senders;56/** Construct a 1-parent-1-child package. */57explicit PackageToValidate(const CTransactionRef& parent,58const CTransactionRef& child,59NodeId parent_sender,60NodeId child_sender) :61m_txns{parent, child},62m_senders{parent_sender, child_sender}63{}65// Move ctor66PackageToValidate(PackageToValidate&& other) : m_txns{std::move(other.m_txns)}, m_senders{std::move(other.m_senders)} {}67// Copy ctor68PackageToValidate(const PackageToValidate& other) = default;70// Move assignment71PackageToValidate& operator=(PackageToValidate&& other) {72this->m_txns = std::move(other.m_txns);73this->m_senders = std::move(other.m_senders);74return *this;75}77std::string ToString() const {78Assume(m_txns.size() == 2);79return strprintf("parent %s (wtxid=%s, sender=%d) + child %s (wtxid=%s, sender=%d)",80m_txns.front()->GetHash().ToString(),81m_txns.front()->GetWitnessHash().ToString(),82m_senders.front(),83m_txns.back()->GetHash().ToString(),84m_txns.back()->GetWitnessHash().ToString(),85m_senders.back());86}87};88struct RejectedTxTodo89{90bool m_should_add_extra_compact_tx;91std::vector<Txid> m_unique_parents;92std::optional<PackageToValidate> m_package_to_validate;93};96/**97* Class responsible for deciding what transactions to request and, once98* downloaded, whether and how to validate them. It is also responsible for99* deciding what transaction packages to validate and how to resolve orphan100* transactions. Its data structures include TxRequestTracker for scheduling101* requests, rolling bloom filters for remembering transactions that have102* already been {accepted, rejected, confirmed}, an orphanage, and a registry of103* each peer's transaction relay-related information.104*105* Caller needs to interact with TxDownloadManager:106* - ValidationInterface callbacks.107* - When a potential transaction relay peer connects or disconnects.108* - When a transaction or package is accepted or rejected from mempool109* - When a inv, notfound, or tx message is received110* - To get instructions for which getdata messages to send111*112* This class is not thread-safe. Access must be synchronized using an113* external mutex.114*/115class TxDownloadManager {116const std::unique_ptr<TxDownloadManagerImpl> m_impl;118public:119explicit TxDownloadManager(const TxDownloadOptions& options);120~TxDownloadManager();122// Responses to chain events. TxDownloadManager is not an actual client of ValidationInterface, these are called through PeerManager.123void ActiveTipChange();124void BlockConnected(const std::shared_ptr<const CBlock>& pblock);125void BlockDisconnected();127/** Creates a new PeerInfo. Saves the connection info to calculate tx announcement delays later. */128void ConnectedPeer(NodeId nodeid, const TxDownloadConnectionInfo& info);130/** Deletes all txrequest announcements and orphans for a given peer. */131void DisconnectedPeer(NodeId nodeid);133/** Consider adding this tx hash to txrequest. Should be called whenever a new inv has been received.134* Also called internally when a transaction is missing parents so that we can request them.135* Returns true if this was a dropped inv (p2p_inv=true and we already have the tx), false otherwise. */136bool AddTxAnnouncement(NodeId peer, const GenTxid& gtxid, std::chrono::microseconds now);138/** Get getdata requests to send. */139std::vector<GenTxid> GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time);141/** Should be called when a notfound for a tx has been received. */142void ReceivedNotFound(NodeId nodeid, const std::vector<GenTxid>& gtxids);144/** Respond to successful transaction submission to mempool */145void MempoolAcceptedTx(const CTransactionRef& tx);147/** Respond to transaction rejected from mempool */148RejectedTxTodo MempoolRejectedTx(const CTransactionRef& ptx, const TxValidationState& state, NodeId nodeid, bool first_time_failure);150/** Respond to package rejected from mempool */151void MempoolRejectedPackage(const Package& package);153/** Marks a tx as ReceivedResponse in txrequest and checks whether AlreadyHaveTx.154* Return a bool indicating whether this tx should be validated. If false, optionally, a155* PackageToValidate. */156std::pair<bool, std::optional<PackageToValidate>> ReceivedTx(NodeId nodeid, const CTransactionRef& ptx);158/** Whether there are any orphans to reconsider for this peer. */159bool HaveMoreWork(NodeId nodeid) const;161/** Returns next orphan tx to consider, or nullptr if none exist. */162CTransactionRef GetTxToReconsider(NodeId nodeid);164/** Check that all data structures are empty. */165void CheckIsEmpty() const;167/** Check that all data structures that track per-peer information have nothing for this peer. */168void CheckIsEmpty(NodeId nodeid) const;170/** Wrapper for TxOrphanage::GetOrphanTransactions */171std::vector<TxOrphanage::OrphanInfo> GetOrphanTransactions() const;172};173} // namespace node174#endif // BITCOIN_NODE_TXDOWNLOADMAN_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.