LCOV - code coverage report
Current view: top level - src - txorphanage.h (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 5 5
Test Date: 2024-08-28 04:44:32 Functions: - 0 0
Branches: 25.5 % 94 24

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2021-2022 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #ifndef BITCOIN_TXORPHANAGE_H
       6                 :             : #define BITCOIN_TXORPHANAGE_H
       7                 :             : 
       8                 :             : #include <net.h>
       9                 :             : #include <primitives/block.h>
      10                 :             : #include <primitives/transaction.h>
      11                 :             : #include <sync.h>
      12                 :             : #include <util/time.h>
      13                 :             : 
      14                 :             : #include <map>
      15                 :             : #include <set>
      16                 :             : 
      17                 :             : /** Expiration time for orphan transactions */
      18                 :             : static constexpr auto ORPHAN_TX_EXPIRE_TIME{20min};
      19                 :             : /** Minimum time between orphan transactions expire time checks */
      20                 :             : static constexpr auto ORPHAN_TX_EXPIRE_INTERVAL{5min};
      21                 :             : 
      22                 :             : /** A class to track orphan transactions (failed on TX_MISSING_INPUTS)
      23                 :             :  * Since we cannot distinguish orphans from bad transactions with
      24                 :             :  * non-existent inputs, we heavily limit the number of orphans
      25                 :             :  * we keep and the duration we keep them for.
      26                 :             :  * Not thread-safe. Requires external synchronization.
      27                 :             :  */
      28                 :             : class TxOrphanage {
      29                 :             : public:
      30                 :             :     /** Add a new orphan transaction */
      31                 :             :     bool AddTx(const CTransactionRef& tx, NodeId peer);
      32                 :             : 
      33                 :             :     /** Check if we already have an orphan transaction (by wtxid only) */
      34                 :             :     bool HaveTx(const Wtxid& wtxid) const;
      35                 :             : 
      36                 :             :     /** Extract a transaction from a peer's work set
      37                 :             :      *  Returns nullptr if there are no transactions to work on.
      38                 :             :      *  Otherwise returns the transaction reference, and removes
      39                 :             :      *  it from the work set.
      40                 :             :      */
      41                 :             :     CTransactionRef GetTxToReconsider(NodeId peer);
      42                 :             : 
      43                 :             :     /** Erase an orphan by wtxid */
      44                 :             :     int EraseTx(const Wtxid& wtxid);
      45                 :             : 
      46                 :             :     /** Erase all orphans announced by a peer (eg, after that peer disconnects) */
      47                 :             :     void EraseForPeer(NodeId peer);
      48                 :             : 
      49                 :             :     /** Erase all orphans included in or invalidated by a new block */
      50                 :             :     void EraseForBlock(const CBlock& block);
      51                 :             : 
      52                 :             :     /** Limit the orphanage to the given maximum */
      53                 :             :     void LimitOrphans(unsigned int max_orphans, FastRandomContext& rng);
      54                 :             : 
      55                 :             :     /** Add any orphans that list a particular tx as a parent into the from peer's work set */
      56                 :             :     void AddChildrenToWorkSet(const CTransaction& tx);
      57                 :             : 
      58                 :             :     /** Does this peer have any work to do? */
      59                 :             :     bool HaveTxToReconsider(NodeId peer);
      60                 :             : 
      61                 :             :     /** Get all children that spend from this tx and were received from nodeid. Sorted from most
      62                 :             :      * recent to least recent. */
      63                 :             :     std::vector<CTransactionRef> GetChildrenFromSamePeer(const CTransactionRef& parent, NodeId nodeid) const;
      64                 :             : 
      65                 :             :     /** Get all children that spend from this tx but were not received from nodeid. Also return
      66                 :             :      * which peer provided each tx. */
      67                 :             :     std::vector<std::pair<CTransactionRef, NodeId>> GetChildrenFromDifferentPeer(const CTransactionRef& parent, NodeId nodeid) const;
      68                 :             : 
      69                 :             :     /** Return how many entries exist in the orphange */
      70                 :           6 :     size_t Size()
      71                 :             :     {
      72         [ -  + ]:           6 :         return m_orphans.size();
      73                 :             :     }
      74                 :             : 
      75                 :             : protected:
      76   [ +  -  #  #  :         273 :     struct OrphanTx {
             #  #  #  # ]
           [ -  -  -  +  
             -  +  -  - ]
      77                 :             :         CTransactionRef tx;
      78                 :             :         NodeId fromPeer;
      79                 :             :         NodeSeconds nTimeExpire;
      80                 :             :         size_t list_pos;
      81                 :             :     };
      82                 :             : 
      83                 :             :     /** Map from wtxid to orphan transaction record. Limited by
      84                 :             :      *  -maxorphantx/DEFAULT_MAX_ORPHAN_TRANSACTIONS */
      85                 :             :     std::map<Wtxid, OrphanTx> m_orphans;
      86                 :             : 
      87                 :             :     /** Which peer provided the orphans that need to be reconsidered */
      88                 :             :     std::map<NodeId, std::set<Wtxid>> m_peer_work_set;
      89                 :             : 
      90                 :             :     using OrphanMap = decltype(m_orphans);
      91                 :             : 
      92                 :             :     struct IteratorComparator
      93                 :             :     {
      94                 :             :         template<typename I>
      95   [ -  -  -  -  :         108 :         bool operator()(const I& a, const I& b) const
          -  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  +  
          -  -  -  -  -  
          +  +  +  +  +  
             +  +  +  - ]
      96                 :             :         {
      97   [ -  -  -  -  :         108 :             return a->first < b->first;
          -  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  +  
          -  -  -  -  -  
          +  +  +  +  +  
             +  +  +  - ]
      98                 :             :         }
      99                 :             :     };
     100                 :             : 
     101                 :             :     /** Index from the parents' COutPoint into the m_orphans. Used
     102                 :             :      *  to remove orphan transactions from the m_orphans */
     103                 :             :     std::map<COutPoint, std::set<OrphanMap::iterator, IteratorComparator>> m_outpoint_to_orphan_it;
     104                 :             : 
     105                 :             :     /** Orphan transactions in vector for quick random eviction */
     106                 :             :     std::vector<OrphanMap::iterator> m_orphan_list;
     107                 :             : 
     108                 :             :     /** Timestamp for the next scheduled sweep of expired orphans */
     109                 :             :     NodeSeconds m_next_sweep{0s};
     110                 :             : };
     111                 :             : 
     112                 :             : #endif // BITCOIN_TXORPHANAGE_H
        

Generated by: LCOV version 2.0-1