From 6bd4963c069bfd0af420e8a3fb724c3b693a1e76 Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Sun, 31 Jan 2021 23:42:00 +1000 Subject: txorphanage: Move functions and data into class Collects all the orphan handling globals into a single member var in net_processing, and ensures access is encapuslated into the interface functions. Also adds doxygen comments for methods. --- src/txorphanage.h | 69 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 23 deletions(-) (limited to 'src/txorphanage.h') diff --git a/src/txorphanage.h b/src/txorphanage.h index f2ffbf3d67..6b9837815b 100644 --- a/src/txorphanage.h +++ b/src/txorphanage.h @@ -13,30 +13,48 @@ /** Guards orphan transactions and extra txs for compact blocks */ extern RecursiveMutex g_cs_orphans; -struct COrphanTx { - // When modifying, adapt the copy of this definition in tests/DoS_tests. - CTransactionRef tx; - NodeId fromPeer; - int64_t nTimeExpire; - size_t list_pos; -}; +/** Data structure to keep track of orphan transactions + */ +class TxOrphanage { +public: + /** Add a new orphan transaction */ + bool AddTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); + + /** Check if we already have an orphan transaction (by txid or wtxid) */ + bool HaveTx(const GenTxid& gtxid) const EXCLUSIVE_LOCKS_REQUIRED(!g_cs_orphans); + + /** Get the details of an orphan transaction (returns nullptr if not found) */ + std::pair GetTx(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); -int EraseOrphanTx(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); -void EraseOrphansFor(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); -void EraseOrphansForBlock(const CBlock& block) EXCLUSIVE_LOCKS_REQUIRED(!g_cs_orphans); -unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); -void AddChildrenToWorkSet(const CTransaction& tx, std::set& orphan_work_set) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); -bool HaveOrphanTx(const GenTxid& gtxid) EXCLUSIVE_LOCKS_REQUIRED(!g_cs_orphans); -std::pair GetOrphanTx(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); -bool OrphanageAddTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); + /** Erase an orphan by txid */ + int EraseTx(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); -/** Map from txid to orphan transaction record. Limited by - * -maxorphantx/DEFAULT_MAX_ORPHAN_TRANSACTIONS */ -extern std::map mapOrphanTransactions GUARDED_BY(g_cs_orphans); + /** Erase all orphans announced by a peer (eg, after that peer disconnects) */ + void EraseForPeer(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); -/** Index from wtxid into the mapOrphanTransactions to lookup orphan - * transactions using their witness ids. */ -extern std::map::iterator> g_orphans_by_wtxid GUARDED_BY(g_cs_orphans); + /** Erase all orphans included in / invalidated by a new block */ + void EraseForBlock(const CBlock& block) EXCLUSIVE_LOCKS_REQUIRED(!g_cs_orphans); + + /** Limit the orphanage to the given maximum */ + unsigned int LimitOrphans(unsigned int nMaxOrphans) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); + + /** Add any orphans that list a particular tx as a parent into a peer's work set + * (ie orphans that may have found their final missing parent, and so should be reconsidered for the mempool) */ + void AddChildrenToWorkSet(const CTransaction& tx, std::set& orphan_work_set) const EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); + +protected: + struct COrphanTx { + CTransactionRef tx; + NodeId fromPeer; + int64_t nTimeExpire; + size_t list_pos; + }; + + /** Map from txid to orphan transaction record. Limited by + * -maxorphantx/DEFAULT_MAX_ORPHAN_TRANSACTIONS */ + std::map mapOrphanTransactions GUARDED_BY(g_cs_orphans); + + using OrphanMap = decltype(mapOrphanTransactions); struct IteratorComparator { @@ -49,9 +67,14 @@ extern std::map::iterator> g_orphans_by_wt /** Index from the parents' COutPoint into the mapOrphanTransactions. Used * to remove orphan transactions from the mapOrphanTransactions */ - extern std::map::iterator, IteratorComparator>> mapOrphanTransactionsByPrev GUARDED_BY(g_cs_orphans); + std::map> mapOrphanTransactionsByPrev GUARDED_BY(g_cs_orphans); /** Orphan transactions in vector for quick random eviction */ - extern std::vector::iterator> g_orphan_list GUARDED_BY(g_cs_orphans); + std::vector g_orphan_list GUARDED_BY(g_cs_orphans); + + /** Index from wtxid into the mapOrphanTransactions to lookup orphan + * transactions using their witness ids. */ + std::map g_orphans_by_wtxid GUARDED_BY(g_cs_orphans); +}; #endif // BITCOIN_TXORPHANAGE_H -- cgit v1.2.3