Bitcoin Core 29.99.0
P2P Digital Currency
Classes | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
node::TxOrphanageImpl Class Referencefinal
Inheritance diagram for node::TxOrphanageImpl:
[legend]
Collaboration diagram for node::TxOrphanageImpl:
[legend]

Classes

struct  Announcement
 One orphan announcement. More...
 
struct  ByPeer
 
struct  ByPeerViewExtractor
 
struct  ByWtxid
 
struct  OrphanIndices
 
struct  PeerDoSInfo
 
struct  WtxidExtractor
 

Public Member Functions

 TxOrphanageImpl ()=default
 
 TxOrphanageImpl (Count max_global_latency_score, Usage reserved_peer_usage)
 
 ~TxOrphanageImpl () noexcept override=default
 
TxOrphanage::Count CountAnnouncements () const override
 Number of announcements, i.e. More...
 
TxOrphanage::Count CountUniqueOrphans () const override
 Number of unique orphans (by wtxid). More...
 
TxOrphanage::Count AnnouncementsFromPeer (NodeId peer) const override
 Number of orphans stored from this peer. More...
 
TxOrphanage::Count LatencyScoreFromPeer (NodeId peer) const override
 Latency score of transactions announced by this peer. More...
 
TxOrphanage::Usage UsageByPeer (NodeId peer) const override
 Total usage (weight) of orphans for which this peer is an announcer. More...
 
TxOrphanage::Count MaxGlobalLatencyScore () const override
 Get the maximum global latency score allowed. More...
 
TxOrphanage::Count TotalLatencyScore () const override
 Get the total latency score of all orphans. More...
 
TxOrphanage::Usage ReservedPeerUsage () const override
 Get the reserved usage per peer. More...
 
TxOrphanage::Count MaxPeerLatencyScore () const override
 Maximum allowed (deduplicated) latency score for all transactions (see Announcement::GetLatencyScore()). More...
 
TxOrphanage::Usage MaxGlobalUsage () const override
 Maximum allowed (deduplicated) memory usage for all transactions (see Announcement::GetMemUsage()). More...
 
bool AddTx (const CTransactionRef &tx, NodeId peer) override
 Add a new orphan transaction. More...
 
bool AddAnnouncer (const Wtxid &wtxid, NodeId peer) override
 Add an additional announcer to an orphan if it exists. More...
 
CTransactionRef GetTx (const Wtxid &wtxid) const override
 Get a transaction by its witness txid. More...
 
bool HaveTx (const Wtxid &wtxid) const override
 Check if we already have an orphan transaction (by wtxid only) More...
 
bool HaveTxFromPeer (const Wtxid &wtxid, NodeId peer) const override
 Check if a {tx, peer} exists in the orphanage. More...
 
CTransactionRef GetTxToReconsider (NodeId peer) override
 If there is a tx that can be reconsidered, return it and set it back to non-reconsiderable. More...
 
bool EraseTx (const Wtxid &wtxid) override
 Erase an orphan by wtxid, including all announcements if there are multiple. More...
 
void EraseForPeer (NodeId peer) override
 Erase all entries by this peer. More...
 
void EraseForBlock (const CBlock &block) override
 Erase all orphans included in or invalidated by a new block. More...
 
std::vector< std::pair< Wtxid, NodeId > > AddChildrenToWorkSet (const CTransaction &tx, FastRandomContext &rng) override
 Add any orphans that list a particular tx as a parent into the from peer's work set. More...
 
bool HaveTxToReconsider (NodeId peer) override
 Return whether there is a tx that can be reconsidered. More...
 
std::vector< CTransactionRefGetChildrenFromSamePeer (const CTransactionRef &parent, NodeId nodeid) const override
 Get all children that spend from this tx and were received from nodeid. More...
 
std::vector< OrphanInfoGetOrphanTransactions () const override
 Get all orphan transactions. More...
 
TxOrphanage::Usage TotalOrphanUsage () const override
 Get the total usage (weight) of all orphans. More...
 
void SanityCheck () const override
 Check consistency between PeerOrphanInfo and m_orphans. More...
 
- Public Member Functions inherited from node::TxOrphanage
virtual ~TxOrphanage ()=default
 
virtual bool AddTx (const CTransactionRef &tx, NodeId peer)=0
 Add a new orphan transaction. More...
 
virtual bool AddAnnouncer (const Wtxid &wtxid, NodeId peer)=0
 Add an additional announcer to an orphan if it exists. More...
 
virtual CTransactionRef GetTx (const Wtxid &wtxid) const =0
 Get a transaction by its witness txid. More...
 
virtual bool HaveTx (const Wtxid &wtxid) const =0
 Check if we already have an orphan transaction (by wtxid only) More...
 
virtual bool HaveTxFromPeer (const Wtxid &wtxid, NodeId peer) const =0
 Check if a {tx, peer} exists in the orphanage. More...
 
virtual CTransactionRef GetTxToReconsider (NodeId peer)=0
 Extract a transaction from a peer's work set, and flip it back to non-reconsiderable. More...
 
virtual bool EraseTx (const Wtxid &wtxid)=0
 Erase an orphan by wtxid, including all announcements if there are multiple. More...
 
virtual void EraseForPeer (NodeId peer)=0
 Maybe erase all orphans announced by a peer (eg, after that peer disconnects). More...
 
virtual void EraseForBlock (const CBlock &block)=0
 Erase all orphans included in or invalidated by a new block. More...
 
virtual std::vector< std::pair< Wtxid, NodeId > > AddChildrenToWorkSet (const CTransaction &tx, FastRandomContext &rng)=0
 Add any orphans that list a particular tx as a parent into the from peer's work set. More...
 
virtual bool HaveTxToReconsider (NodeId peer)=0
 Does this peer have any work to do? More...
 
virtual std::vector< CTransactionRefGetChildrenFromSamePeer (const CTransactionRef &parent, NodeId nodeid) const =0
 Get all children that spend from this tx and were received from nodeid. More...
 
virtual std::vector< OrphanInfoGetOrphanTransactions () const =0
 Get all orphan transactions. More...
 
virtual Usage TotalOrphanUsage () const =0
 Get the total usage (weight) of all orphans. More...
 
virtual Usage UsageByPeer (NodeId peer) const =0
 Total usage (weight) of orphans for which this peer is an announcer. More...
 
virtual void SanityCheck () const =0
 Check consistency between PeerOrphanInfo and m_orphans. More...
 
virtual Count CountAnnouncements () const =0
 Number of announcements, i.e. More...
 
virtual Count CountUniqueOrphans () const =0
 Number of unique orphans (by wtxid). More...
 
virtual Count AnnouncementsFromPeer (NodeId peer) const =0
 Number of orphans stored from this peer. More...
 
virtual Count LatencyScoreFromPeer (NodeId peer) const =0
 Latency score of transactions announced by this peer. More...
 
virtual Count MaxGlobalLatencyScore () const =0
 Get the maximum global latency score allowed. More...
 
virtual Count TotalLatencyScore () const =0
 Get the total latency score of all orphans. More...
 
virtual Usage ReservedPeerUsage () const =0
 Get the reserved usage per peer. More...
 
virtual Count MaxPeerLatencyScore () const =0
 Get the maximum latency score allowed per peer. More...
 
virtual Usage MaxGlobalUsage () const =0
 Get the maximum global usage allowed. More...
 

Private Types

using SequenceNumber = uint64_t
 
using ByWtxidView = std::tuple< Wtxid, NodeId >
 
using ByPeerView = std::tuple< NodeId, bool, SequenceNumber >
 
using AnnouncementMap = boost::multi_index::multi_index_container< Announcement, OrphanIndices >
 
template<typename Tag >
using Iter = typename AnnouncementMap::index< Tag >::type::iterator
 

Private Member Functions

template<typename Tag >
void Erase (Iter< Tag > it)
 Erase from m_orphans and update m_peer_orphanage_info. More...
 
bool EraseTxInternal (const Wtxid &wtxid)
 Erase by wtxid. More...
 
bool IsUnique (Iter< ByWtxid > it) const
 Check if there is exactly one announcement with the same wtxid as it. More...
 
bool NeedsTrim () const
 Check if the orphanage needs trimming. More...
 
void LimitOrphans ()
 Limit the orphanage to MaxGlobalLatencyScore and MaxGlobalUsage. More...
 

Private Attributes

SequenceNumber m_current_sequence {0}
 Global sequence number, increment each time an announcement is added. More...
 
AnnouncementMap m_orphans
 
const TxOrphanage::Count m_max_global_latency_score {DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE}
 
const TxOrphanage::Usage m_reserved_usage_per_peer {DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER}
 
TxOrphanage::Count m_unique_orphans {0}
 Number of unique orphans by wtxid. More...
 
TxOrphanage::Usage m_unique_orphan_usage {0}
 Memory used by orphans (see Announcement::GetMemUsage()), deduplicated by wtxid. More...
 
TxOrphanage::Count m_unique_rounded_input_scores {0}
 The sum of each unique transaction's latency scores including the inputs only (see Announcement::GetLatencyScore but subtract 1 for the announcements themselves). More...
 
std::unordered_map< COutPoint, std::set< Wtxid >, SaltedOutpointHasherm_outpoint_to_orphan_wtxids
 Index from the parents' outputs to wtxids that exist in m_orphans. More...
 
std::set< Wtxidm_reconsiderable_wtxids
 Set of Wtxids for which (exactly) one announcement with m_reconsider=true exists. More...
 
std::unordered_map< NodeId, PeerDoSInfom_peer_orphanage_info
 Store per-peer statistics. More...
 

Additional Inherited Members

- Public Types inherited from node::TxOrphanage
using Usage = int64_t
 
using Count = unsigned int
 

Detailed Description

Definition at line 29 of file txorphanage.cpp.

Member Typedef Documentation

◆ AnnouncementMap

using node::TxOrphanageImpl::AnnouncementMap = boost::multi_index::multi_index_container<Announcement, OrphanIndices>
private

Definition at line 99 of file txorphanage.cpp.

◆ ByPeerView

using node::TxOrphanageImpl::ByPeerView = std::tuple<NodeId, bool, SequenceNumber>
private

Definition at line 85 of file txorphanage.cpp.

◆ ByWtxidView

using node::TxOrphanageImpl::ByWtxidView = std::tuple<Wtxid, NodeId>
private

Definition at line 73 of file txorphanage.cpp.

◆ Iter

template<typename Tag >
using node::TxOrphanageImpl::Iter = typename AnnouncementMap::index<Tag>::type::iterator
private

Definition at line 101 of file txorphanage.cpp.

◆ SequenceNumber

using node::TxOrphanageImpl::SequenceNumber = uint64_t
private

Definition at line 31 of file txorphanage.cpp.

Constructor & Destructor Documentation

◆ TxOrphanageImpl() [1/2]

node::TxOrphanageImpl::TxOrphanageImpl ( )
default

◆ TxOrphanageImpl() [2/2]

node::TxOrphanageImpl::TxOrphanageImpl ( Count  max_global_latency_score,
Usage  reserved_peer_usage 
)
inline

Definition at line 192 of file txorphanage.cpp.

◆ ~TxOrphanageImpl()

node::TxOrphanageImpl::~TxOrphanageImpl ( )
overridedefaultnoexcept

Member Function Documentation

◆ AddAnnouncer()

bool node::TxOrphanageImpl::AddAnnouncer ( const Wtxid wtxid,
NodeId  peer 
)
overridevirtual

Add an additional announcer to an orphan if it exists.

Otherwise, do nothing.

Implements node::TxOrphanage.

Definition at line 354 of file txorphanage.cpp.

Here is the call graph for this function:

◆ AddChildrenToWorkSet()

std::vector< std::pair< Wtxid, NodeId > > node::TxOrphanageImpl::AddChildrenToWorkSet ( const CTransaction tx,
FastRandomContext rng 
)
overridevirtual

Add any orphans that list a particular tx as a parent into the from peer's work set.

Implements node::TxOrphanage.

Definition at line 527 of file txorphanage.cpp.

Here is the call graph for this function:

◆ AddTx()

bool node::TxOrphanageImpl::AddTx ( const CTransactionRef tx,
NodeId  peer 
)
overridevirtual

Add a new orphan transaction.

Implements node::TxOrphanage.

Definition at line 306 of file txorphanage.cpp.

Here is the call graph for this function:

◆ AnnouncementsFromPeer()

TxOrphanage::Count node::TxOrphanageImpl::AnnouncementsFromPeer ( NodeId  peer) const
overridevirtual

Number of orphans stored from this peer.

Implements node::TxOrphanage.

Definition at line 296 of file txorphanage.cpp.

◆ CountAnnouncements()

TxOrphanage::Count node::TxOrphanageImpl::CountAnnouncements ( ) const
overridevirtual

Number of announcements, i.e.

total size of m_orphans. Ones for the same wtxid are not de-duplicated. Not the same as TotalLatencyScore().

Implements node::TxOrphanage.

Definition at line 290 of file txorphanage.cpp.

◆ CountUniqueOrphans()

TxOrphanage::Count node::TxOrphanageImpl::CountUniqueOrphans ( ) const
overridevirtual

Number of unique orphans (by wtxid).

Implements node::TxOrphanage.

Definition at line 294 of file txorphanage.cpp.

Here is the caller graph for this function:

◆ Erase()

template<typename Tag >
void node::TxOrphanageImpl::Erase ( Iter< Tag >  it)
private

Erase from m_orphans and update m_peer_orphanage_info.

Definition at line 238 of file txorphanage.cpp.

Here is the call graph for this function:

◆ EraseForBlock()

void node::TxOrphanageImpl::EraseForBlock ( const CBlock block)
overridevirtual

Erase all orphans included in or invalidated by a new block.

Implements node::TxOrphanage.

Definition at line 610 of file txorphanage.cpp.

Here is the call graph for this function:

◆ EraseForPeer()

void node::TxOrphanageImpl::EraseForPeer ( NodeId  peer)
overridevirtual

Erase all entries by this peer.

Implements node::TxOrphanage.

Definition at line 416 of file txorphanage.cpp.

Here is the call graph for this function:

◆ EraseTx()

bool node::TxOrphanageImpl::EraseTx ( const Wtxid wtxid)
overridevirtual

Erase an orphan by wtxid, including all announcements if there are multiple.

Returns true if an orphan was erased, false if no tx with this wtxid exists.

Implements node::TxOrphanage.

Definition at line 405 of file txorphanage.cpp.

Here is the call graph for this function:

◆ EraseTxInternal()

bool node::TxOrphanageImpl::EraseTxInternal ( const Wtxid wtxid)
private

Erase by wtxid.

Definition at line 385 of file txorphanage.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetChildrenFromSamePeer()

std::vector< CTransactionRef > node::TxOrphanageImpl::GetChildrenFromSamePeer ( const CTransactionRef parent,
NodeId  nodeid 
) const
overridevirtual

Get all children that spend from this tx and were received from nodeid.

Sorted reconsiderable before non-reconsiderable, then from most recent to least recent.

Implements node::TxOrphanage.

Definition at line 645 of file txorphanage.cpp.

◆ GetOrphanTransactions()

std::vector< TxOrphanage::OrphanInfo > node::TxOrphanageImpl::GetOrphanTransactions ( ) const
overridevirtual

Get all orphan transactions.

Implements node::TxOrphanage.

Definition at line 672 of file txorphanage.cpp.

◆ GetTx()

CTransactionRef node::TxOrphanageImpl::GetTx ( const Wtxid wtxid) const
overridevirtual

Get a transaction by its witness txid.

Implements node::TxOrphanage.

Definition at line 573 of file txorphanage.cpp.

◆ GetTxToReconsider()

CTransactionRef node::TxOrphanageImpl::GetTxToReconsider ( NodeId  peer)
overridevirtual

If there is a tx that can be reconsidered, return it and set it back to non-reconsiderable.

Otherwise, return a nullptr.

Implements node::TxOrphanage.

Definition at line 587 of file txorphanage.cpp.

◆ HaveTx()

bool node::TxOrphanageImpl::HaveTx ( const Wtxid wtxid) const
overridevirtual

Check if we already have an orphan transaction (by wtxid only)

Implements node::TxOrphanage.

Definition at line 567 of file txorphanage.cpp.

Here is the caller graph for this function:

◆ HaveTxFromPeer()

bool node::TxOrphanageImpl::HaveTxFromPeer ( const Wtxid wtxid,
NodeId  peer 
) const
overridevirtual

Check if a {tx, peer} exists in the orphanage.

Implements node::TxOrphanage.

Definition at line 580 of file txorphanage.cpp.

◆ HaveTxToReconsider()

bool node::TxOrphanageImpl::HaveTxToReconsider ( NodeId  peer)
overridevirtual

Return whether there is a tx that can be reconsidered.

Implements node::TxOrphanage.

Definition at line 604 of file txorphanage.cpp.

◆ IsUnique()

bool node::TxOrphanageImpl::IsUnique ( Iter< ByWtxid it) const
private

Check if there is exactly one announcement with the same wtxid as it.

Definition at line 274 of file txorphanage.cpp.

Here is the caller graph for this function:

◆ LatencyScoreFromPeer()

TxOrphanage::Count node::TxOrphanageImpl::LatencyScoreFromPeer ( NodeId  peer) const
overridevirtual

Latency score of transactions announced by this peer.

Implements node::TxOrphanage.

Definition at line 301 of file txorphanage.cpp.

◆ LimitOrphans()

void node::TxOrphanageImpl::LimitOrphans ( )
private

Limit the orphanage to MaxGlobalLatencyScore and MaxGlobalUsage.

If the data structure needs trimming, evicts announcements by selecting the DoSiest peer and evicting its oldest announcement (sorting non-reconsiderable orphans first, to give reconsiderable orphans a greater chance of being processed).

Does nothing if no global limits are exceeded. This eviction strategy effectively "reserves" an amount of announcements and space for each peer. The reserved amount is protected from eviction even if there are peers spamming the orphanage.

Definition at line 442 of file txorphanage.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MaxGlobalLatencyScore()

TxOrphanage::Count node::TxOrphanageImpl::MaxGlobalLatencyScore ( ) const
overridevirtual

Get the maximum global latency score allowed.

Implements node::TxOrphanage.

Definition at line 765 of file txorphanage.cpp.

Here is the caller graph for this function:

◆ MaxGlobalUsage()

TxOrphanage::Usage node::TxOrphanageImpl::MaxGlobalUsage ( ) const
overridevirtual

Maximum allowed (deduplicated) memory usage for all transactions (see Announcement::GetMemUsage()).

Dynamic based on number of peers. More peers means more allowed memory usage. The number of peers times ReservedPeerUsage() adds up to MaxGlobalUsage(). As long as every peer's m_total_usage / ReservedPeerUsage() < 1, MaxGlobalUsage() is not exceeded.

Implements node::TxOrphanage.

Definition at line 769 of file txorphanage.cpp.

Here is the caller graph for this function:

◆ MaxPeerLatencyScore()

TxOrphanage::Count node::TxOrphanageImpl::MaxPeerLatencyScore ( ) const
overridevirtual

Maximum allowed (deduplicated) latency score for all transactions (see Announcement::GetLatencyScore()).

Dynamic based on number of peers. Each peer has an equal amount, but the global maximum latency score stays constant. The number of peers times MaxPeerLatencyScore() (rounded) adds up to MaxGlobalLatencyScore(). As long as every peer's m_total_latency_score / MaxPeerLatencyScore() < 1, MaxGlobalLatencyScore() is not exceeded.

Implements node::TxOrphanage.

Definition at line 768 of file txorphanage.cpp.

Here is the caller graph for this function:

◆ NeedsTrim()

bool node::TxOrphanageImpl::NeedsTrim ( ) const
private

Check if the orphanage needs trimming.

Definition at line 771 of file txorphanage.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ReservedPeerUsage()

TxOrphanage::Usage node::TxOrphanageImpl::ReservedPeerUsage ( ) const
overridevirtual

Get the reserved usage per peer.

Implements node::TxOrphanage.

Definition at line 767 of file txorphanage.cpp.

Here is the caller graph for this function:

◆ SanityCheck()

void node::TxOrphanageImpl::SanityCheck ( ) const
overridevirtual

Check consistency between PeerOrphanInfo and m_orphans.

Recalculate counters and ensure they match what is cached.

Implements node::TxOrphanage.

Definition at line 695 of file txorphanage.cpp.

Here is the call graph for this function:

◆ TotalLatencyScore()

TxOrphanage::Count node::TxOrphanageImpl::TotalLatencyScore ( ) const
overridevirtual

Get the total latency score of all orphans.

Implements node::TxOrphanage.

Definition at line 766 of file txorphanage.cpp.

Here is the caller graph for this function:

◆ TotalOrphanUsage()

TxOrphanage::Usage node::TxOrphanageImpl::TotalOrphanUsage ( ) const
overridevirtual

Get the total usage (weight) of all orphans.

If an orphan has multiple announcers, its usage is only counted once within this total.

Implements node::TxOrphanage.

Definition at line 292 of file txorphanage.cpp.

Here is the caller graph for this function:

◆ UsageByPeer()

TxOrphanage::Usage node::TxOrphanageImpl::UsageByPeer ( NodeId  peer) const
overridevirtual

Total usage (weight) of orphans for which this peer is an announcer.

If an orphan has multiple announcers, its weight will be accounted for in each PeerOrphanInfo, so the total of all peers' UsageByPeer() may be larger than TotalOrphanUsage(). Similarly, UsageByPeer() may be far higher than ReservedPeerUsage(), particularly if many peers have provided the same orphans.

Implements node::TxOrphanage.

Definition at line 284 of file txorphanage.cpp.

Member Data Documentation

◆ m_current_sequence

SequenceNumber node::TxOrphanageImpl::m_current_sequence {0}
private

Global sequence number, increment each time an announcement is added.

Definition at line 33 of file txorphanage.cpp.

◆ m_max_global_latency_score

const TxOrphanage::Count node::TxOrphanageImpl::m_max_global_latency_score {DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE}
private

Definition at line 104 of file txorphanage.cpp.

◆ m_orphans

AnnouncementMap node::TxOrphanageImpl::m_orphans
private

Definition at line 102 of file txorphanage.cpp.

◆ m_outpoint_to_orphan_wtxids

std::unordered_map<COutPoint, std::set<Wtxid>, SaltedOutpointHasher> node::TxOrphanageImpl::m_outpoint_to_orphan_wtxids
private

Index from the parents' outputs to wtxids that exist in m_orphans.

Used to find children of a transaction that can be reconsidered and to remove entries that conflict with a block.

Definition at line 120 of file txorphanage.cpp.

◆ m_peer_orphanage_info

std::unordered_map<NodeId, PeerDoSInfo> node::TxOrphanageImpl::m_peer_orphanage_info
private

Store per-peer statistics.

Used to determine each peer's DoS score. The size of this map is used to determine the number of peers and thus global {latency score, memory} limits.

Definition at line 172 of file txorphanage.cpp.

◆ m_reconsiderable_wtxids

std::set<Wtxid> node::TxOrphanageImpl::m_reconsiderable_wtxids
private

Set of Wtxids for which (exactly) one announcement with m_reconsider=true exists.

Definition at line 123 of file txorphanage.cpp.

◆ m_reserved_usage_per_peer

const TxOrphanage::Usage node::TxOrphanageImpl::m_reserved_usage_per_peer {DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER}
private

Definition at line 105 of file txorphanage.cpp.

◆ m_unique_orphan_usage

TxOrphanage::Usage node::TxOrphanageImpl::m_unique_orphan_usage {0}
private

Memory used by orphans (see Announcement::GetMemUsage()), deduplicated by wtxid.

Definition at line 111 of file txorphanage.cpp.

◆ m_unique_orphans

TxOrphanage::Count node::TxOrphanageImpl::m_unique_orphans {0}
private

Number of unique orphans by wtxid.

Less than or equal to the number of entries in m_orphans.

Definition at line 108 of file txorphanage.cpp.

◆ m_unique_rounded_input_scores

TxOrphanage::Count node::TxOrphanageImpl::m_unique_rounded_input_scores {0}
private

The sum of each unique transaction's latency scores including the inputs only (see Announcement::GetLatencyScore but subtract 1 for the announcements themselves).

The total orphanage's latency score is given by this value + the number of entries in m_orphans.

Definition at line 116 of file txorphanage.cpp.


The documentation for this class was generated from the following file: