LCOV - code coverage report
Current view: top level - src - txgraph.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 0.0 % 957 0
Test Date: 2025-03-31 05:19:22 Functions: 0.0 % 75 0
Branches: 0.0 % 876 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 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                 :             : #include <txgraph.h>
       6                 :             : 
       7                 :             : #include <cluster_linearize.h>
       8                 :             : #include <random.h>
       9                 :             : #include <util/bitset.h>
      10                 :             : #include <util/check.h>
      11                 :             : #include <util/feefrac.h>
      12                 :             : 
      13                 :             : #include <compare>
      14                 :             : #include <memory>
      15                 :             : #include <set>
      16                 :             : #include <span>
      17                 :             : #include <utility>
      18                 :             : 
      19                 :             : namespace {
      20                 :             : 
      21                 :             : using namespace cluster_linearize;
      22                 :             : 
      23                 :             : /** The maximum number of levels a TxGraph can have (0 = main, 1 = staging). */
      24                 :             : static constexpr int MAX_LEVELS{2};
      25                 :             : 
      26                 :             : // Forward declare the TxGraph implementation class.
      27                 :             : class TxGraphImpl;
      28                 :             : 
      29                 :             : /** Position of a DepGraphIndex within a Cluster::m_linearization. */
      30                 :             : using LinearizationIndex = uint32_t;
      31                 :             : /** Position of a Cluster within Graph::ClusterSet::m_clusters. */
      32                 :             : using ClusterSetIndex = uint32_t;
      33                 :             : 
      34                 :             : /** Quality levels for cached cluster linearizations. */
      35                 :             : enum class QualityLevel
      36                 :             : {
      37                 :             :     /** This cluster may have multiple disconnected components, which are all NEEDS_RELINEARIZE. */
      38                 :             :     NEEDS_SPLIT,
      39                 :             :     /** This cluster may have multiple disconnected components, which are all ACCEPTABLE. */
      40                 :             :     NEEDS_SPLIT_ACCEPTABLE,
      41                 :             :     /** This cluster has undergone changes that warrant re-linearization. */
      42                 :             :     NEEDS_RELINEARIZE,
      43                 :             :     /** The minimal level of linearization has been performed, but it is not known to be optimal. */
      44                 :             :     ACCEPTABLE,
      45                 :             :     /** The linearization is known to be optimal. */
      46                 :             :     OPTIMAL,
      47                 :             :     /** This cluster is not registered in any ClusterSet::m_clusters.
      48                 :             :      *  This must be the last entry in QualityLevel as ClusterSet::m_clusters is sized using it. */
      49                 :             :     NONE,
      50                 :             : };
      51                 :             : 
      52                 :             : /** A grouping of connected transactions inside a TxGraphImpl::ClusterSet. */
      53                 :             : class Cluster
      54                 :             : {
      55                 :             :     friend class TxGraphImpl;
      56                 :             :     using GraphIndex = TxGraph::GraphIndex;
      57                 :             :     using SetType = BitSet<MAX_CLUSTER_COUNT_LIMIT>;
      58                 :             :     /** The DepGraph for this cluster, holding all feerates, and ancestors/descendants. */
      59                 :             :     DepGraph<SetType> m_depgraph;
      60                 :             :     /** m_mapping[i] gives the GraphIndex for the position i transaction in m_depgraph. Values for
      61                 :             :      *  positions i that do not exist in m_depgraph shouldn't ever be accessed and thus don't
      62                 :             :      *  matter. m_mapping.size() equals m_depgraph.PositionRange(). */
      63                 :             :     std::vector<GraphIndex> m_mapping;
      64                 :             :     /** The current linearization of the cluster. m_linearization.size() equals
      65                 :             :      *  m_depgraph.TxCount(). This is always kept topological. */
      66                 :             :     std::vector<DepGraphIndex> m_linearization;
      67                 :             :     /** The quality level of m_linearization. */
      68                 :             :     QualityLevel m_quality{QualityLevel::NONE};
      69                 :             :     /** Which position this Cluster has in Graph::ClusterSet::m_clusters[m_quality]. */
      70                 :             :     ClusterSetIndex m_setindex{ClusterSetIndex(-1)};
      71                 :             :     /** Which level this Cluster is at in the graph (-1=not inserted, 0=main, 1=staging). */
      72                 :             :     int m_level{-1};
      73                 :             : 
      74                 :             : public:
      75                 :             :     /** Construct an empty Cluster. */
      76                 :           0 :     Cluster() noexcept = default;
      77                 :             :     /** Construct a singleton Cluster. */
      78                 :             :     explicit Cluster(TxGraphImpl& graph, const FeePerWeight& feerate, GraphIndex graph_index) noexcept;
      79                 :             : 
      80                 :             :     // Cannot move or copy (would invalidate Cluster* in Locator and ClusterSet). */
      81                 :             :     Cluster(const Cluster&) = delete;
      82                 :             :     Cluster& operator=(const Cluster&) = delete;
      83                 :             :     Cluster(Cluster&&) = delete;
      84                 :             :     Cluster& operator=(Cluster&&) = delete;
      85                 :             : 
      86                 :             :     // Generic helper functions.
      87                 :             : 
      88                 :             :     /** Whether the linearization of this Cluster can be exposed. */
      89                 :           0 :     bool IsAcceptable(bool after_split = false) const noexcept
      90                 :             :     {
      91   [ #  #  #  #  :           0 :         return m_quality == QualityLevel::ACCEPTABLE || m_quality == QualityLevel::OPTIMAL ||
                   #  # ]
      92   [ #  #  #  # ]:           0 :                (after_split && m_quality == QualityLevel::NEEDS_SPLIT_ACCEPTABLE);
      93                 :             :     }
      94                 :             :     /** Whether the linearization of this Cluster is optimal. */
      95                 :           0 :     bool IsOptimal() const noexcept
      96                 :             :     {
      97                 :           0 :         return m_quality == QualityLevel::OPTIMAL;
      98                 :             :     }
      99                 :             :     /** Whether this cluster requires splitting. */
     100                 :           0 :     bool NeedsSplitting() const noexcept
     101                 :             :     {
     102                 :           0 :         return m_quality == QualityLevel::NEEDS_SPLIT ||
     103                 :             :                m_quality == QualityLevel::NEEDS_SPLIT_ACCEPTABLE;
     104                 :             :     }
     105                 :             :     /** Get the number of transactions in this Cluster. */
     106         [ #  # ]:           0 :     LinearizationIndex GetTxCount() const noexcept { return m_linearization.size(); }
     107                 :             :     /** Given a DepGraphIndex into this Cluster, find the corresponding GraphIndex. */
     108                 :           0 :     GraphIndex GetClusterEntry(DepGraphIndex index) const noexcept { return m_mapping[index]; }
     109                 :             :     /** Only called by Graph::SwapIndexes. */
     110                 :           0 :     void UpdateMapping(DepGraphIndex cluster_idx, GraphIndex graph_idx) noexcept { m_mapping[cluster_idx] = graph_idx; }
     111                 :             :     /** Push changes to Cluster and its linearization to the TxGraphImpl Entry objects. */
     112                 :             :     void Updated(TxGraphImpl& graph) noexcept;
     113                 :             :     /** Create a copy of this Cluster in staging, returning a pointer to it (used by PullIn). */
     114                 :             :     Cluster* CopyToStaging(TxGraphImpl& graph) const noexcept;
     115                 :             :     /** Get the list of Clusters in main that conflict with this one (which is assumed to be in staging). */
     116                 :             :     void GetConflicts(const TxGraphImpl& graph, std::vector<Cluster*>& out) const noexcept;
     117                 :             :     /** Mark all the Entry objects belonging to this staging Cluster as missing. The Cluster must be
     118                 :             :      *  deleted immediately after. */
     119                 :             :     void MakeStagingTransactionsMissing(TxGraphImpl& graph) noexcept;
     120                 :             :     /** Remove all transactions from a Cluster. */
     121                 :             :     void Clear(TxGraphImpl& graph) noexcept;
     122                 :             :     /** Change a Cluster's level from 1 (staging) to 0 (main). */
     123                 :             :     void MoveToMain(TxGraphImpl& graph) noexcept;
     124                 :             : 
     125                 :             :     // Functions that implement the Cluster-specific side of internal TxGraphImpl mutations.
     126                 :             : 
     127                 :             :     /** Apply all removals from the front of to_remove that apply to this Cluster, popping them
     128                 :             :      *  off. These must be at least one such entry. */
     129                 :             :     void ApplyRemovals(TxGraphImpl& graph, std::span<GraphIndex>& to_remove) noexcept;
     130                 :             :     /** Split this cluster (must have a NEEDS_SPLIT* quality). Returns whether to delete this
     131                 :             :      *  Cluster afterwards. */
     132                 :             :     [[nodiscard]] bool Split(TxGraphImpl& graph) noexcept;
     133                 :             :     /** Move all transactions from cluster to *this (as separate components). */
     134                 :             :     void Merge(TxGraphImpl& graph, Cluster& cluster) noexcept;
     135                 :             :     /** Given a span of (parent, child) pairs that all belong to this Cluster, apply them. */
     136                 :             :     void ApplyDependencies(TxGraphImpl& graph, std::span<std::pair<GraphIndex, GraphIndex>> to_apply) noexcept;
     137                 :             :     /** Improve the linearization of this Cluster. */
     138                 :             :     void Relinearize(TxGraphImpl& graph, uint64_t max_iters) noexcept;
     139                 :             : 
     140                 :             :     // Functions that implement the Cluster-specific side of public TxGraph functions.
     141                 :             : 
     142                 :             :     /** Process elements from the front of args that apply to this cluster, and append Refs for the
     143                 :             :      *  union of their ancestors to output. */
     144                 :             :     void GetAncestorRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept;
     145                 :             :     /** Process elements from the front of args that apply to this cluster, and append Refs for the
     146                 :             :      *  union of their descendants to output. */
     147                 :             :     void GetDescendantRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept;
     148                 :             :     /** Get a vector of Refs for all elements of this Cluster, in linearization order. */
     149                 :             :     std::vector<TxGraph::Ref*> GetClusterRefs(const TxGraphImpl& graph) noexcept;
     150                 :             :     /** Get the individual transaction feerate of a Cluster element. */
     151                 :             :     FeePerWeight GetIndividualFeerate(DepGraphIndex idx) noexcept;
     152                 :             :     /** Modify the fee of a Cluster element. */
     153                 :             :     void SetFee(TxGraphImpl& graph, DepGraphIndex idx, int64_t fee) noexcept;
     154                 :             : 
     155                 :             :     // Debugging functions.
     156                 :             : 
     157                 :             :     void SanityCheck(const TxGraphImpl& graph, int level) const;
     158                 :             : };
     159                 :             : 
     160                 :             : /** The transaction graph, including staged changes.
     161                 :             :  *
     162                 :             :  * The overall design of the data structure consists of 3 interlinked representations:
     163                 :             :  * - The transactions (held as a vector of TxGraphImpl::Entry inside TxGraphImpl).
     164                 :             :  * - The clusters (Cluster objects in per-quality vectors inside TxGraphImpl::ClusterSet).
     165                 :             :  * - The Refs (TxGraph::Ref objects, held externally by users of the TxGraph class)
     166                 :             :  *
     167                 :             :  * The Clusters are kept in one or two ClusterSet objects, one for the "main" graph, and one for
     168                 :             :  * the proposed changes ("staging"). If a transaction occurs in both, they share the same Entry,
     169                 :             :  * but there will be a separate Cluster per graph.
     170                 :             :  *
     171                 :             :  * Clusters and Refs contain the index of the Entry objects they refer to, and the Entry objects
     172                 :             :  * refer back to the Clusters and Refs the corresponding transaction is contained in.
     173                 :             :  *
     174                 :             :  * While redundant, this permits moving all of them independently, without invalidating things
     175                 :             :  * or costly iteration to fix up everything:
     176                 :             :  * - Entry objects can be moved to fill holes left by removed transactions in the Entry vector
     177                 :             :  *   (see TxGraphImpl::Compact).
     178                 :             :  * - Clusters can be rewritten continuously (removals can cause them to split, new dependencies
     179                 :             :  *   can cause them to be merged).
     180                 :             :  * - Ref objects can be held outside the class, while permitting them to be moved around, and
     181                 :             :  *   inherited from.
     182                 :             :  */
     183                 :             : class TxGraphImpl final : public TxGraph
     184                 :             : {
     185                 :             :     friend class Cluster;
     186                 :             : private:
     187                 :             :     /** Internal RNG. */
     188                 :             :     FastRandomContext m_rng;
     189                 :             :     /** This TxGraphImpl's maximum cluster count limit. */
     190                 :             :     const DepGraphIndex m_max_cluster_count;
     191                 :             : 
     192                 :             :     /** Information about one group of Clusters to be merged. */
     193                 :             :     struct GroupEntry
     194                 :             :     {
     195                 :             :         /** Where the clusters to be merged start in m_group_clusters. */
     196                 :             :         uint32_t m_cluster_offset;
     197                 :             :         /** How many clusters to merge. */
     198                 :             :         uint32_t m_cluster_count;
     199                 :             :         /** Where the dependencies for this cluster group in m_deps_to_add start. */
     200                 :             :         uint32_t m_deps_offset;
     201                 :             :         /** How many dependencies to add. */
     202                 :             :         uint32_t m_deps_count;
     203                 :             :     };
     204                 :             : 
     205                 :             :     /** Information about all groups of Clusters to be merged. */
     206                 :           0 :     struct GroupData
     207                 :             :     {
     208                 :             :         /** The groups of Clusters to be merged. */
     209                 :             :         std::vector<GroupEntry> m_groups;
     210                 :             :         /** Which clusters are to be merged. GroupEntry::m_cluster_offset indexes into this. */
     211                 :             :         std::vector<Cluster*> m_group_clusters;
     212                 :             :         /** Whether at least one of the groups cannot be applied because it would result in a
     213                 :             :          *  Cluster that violates the cluster count limit. */
     214                 :             :         bool m_group_oversized;
     215                 :             :     };
     216                 :             : 
     217                 :             :     /** The collection of all Clusters in main or staged. */
     218                 :             :     struct ClusterSet
     219                 :             :     {
     220                 :             :         /** The vectors of clusters, one vector per quality level. ClusterSetIndex indexes into each. */
     221                 :             :         std::array<std::vector<std::unique_ptr<Cluster>>, int(QualityLevel::NONE)> m_clusters;
     222                 :             :         /** Which removals have yet to be applied. */
     223                 :             :         std::vector<GraphIndex> m_to_remove;
     224                 :             :         /** Which dependencies are to be added ((parent,child) pairs). GroupData::m_deps_offset indexes
     225                 :             :          *  into this. */
     226                 :             :         std::vector<std::pair<GraphIndex, GraphIndex>> m_deps_to_add;
     227                 :             :         /** Information about the merges to be performed, if known. */
     228                 :             :         std::optional<GroupData> m_group_data = GroupData{};
     229                 :             :         /** Which entries were removed in this ClusterSet (so they can be wiped on abort). This
     230                 :             :          *  includes all entries which have an (R) removed locator at this level (staging only),
     231                 :             :          *  plus optionally any transaction in m_unlinked. */
     232                 :             :         std::vector<GraphIndex> m_removed;
     233                 :             :         /** Total number of transactions in this graph (sum of all transaction counts in all
     234                 :             :          *  Clusters, and for staging also those inherited from the main ClusterSet). */
     235                 :             :         GraphIndex m_txcount{0};
     236                 :             :         /** Whether this graph is oversized (if known). This roughly matches
     237                 :             :          *  m_group_data->m_group_oversized, but may be known even if m_group_data is not. */
     238                 :             :         std::optional<bool> m_oversized{false};
     239                 :             : 
     240                 :           0 :         ClusterSet() noexcept = default;
     241                 :             :     };
     242                 :             : 
     243                 :             :     /** The main ClusterSet. */
     244                 :             :     ClusterSet m_main_clusterset;
     245                 :             :     /** The staging ClusterSet, if any. */
     246                 :             :     std::optional<ClusterSet> m_staging_clusterset;
     247                 :             : 
     248                 :             :     /** A Locator that describes whether, where, and in which Cluster an Entry appears.
     249                 :             :      *  Every Entry has MAX_LEVELS locators, as it may appear in one Cluster per level.
     250                 :             :      *
     251                 :             :      *  Each level of a Locator is in one of three states:
     252                 :             :      *
     253                 :             :      *  - (P)resent: actually occurs in a Cluster at that level.
     254                 :             :      *
     255                 :             :      *  - (M)issing:
     256                 :             :      *    - In the main graph:    the transaction does not exist in main.
     257                 :             :      *    - In the staging graph: the transaction's existence is the same as in main. If it doesn't
     258                 :             :      *                            exist in main, (M) in staging means it does not exist there
     259                 :             :      *                            either. If it does exist in main, (M) in staging means the
     260                 :             :      *                            cluster it is in has not been modified in staging, and thus the
     261                 :             :      *                            transaction implicitly exists in staging too (without explicit
     262                 :             :      *                            Cluster object; see PullIn() to create it in staging too).
     263                 :             :      *
     264                 :             :      *  - (R)emoved: only possible in staging; it means the transaction exists in main, but is
     265                 :             :      *               removed in staging.
     266                 :             :      *
     267                 :             :      * The following combinations are possible:
     268                 :             :      * - (M,M): the transaction doesn't exist in either graph.
     269                 :             :      * - (P,M): the transaction exists in both, but only exists explicitly in a Cluster object in
     270                 :             :      *          main. Its existence in staging is inherited from main.
     271                 :             :      * - (P,P): the transaction exists in both, and is materialized in both. Thus, the clusters
     272                 :             :      *          and/or their linearizations may be different in main and staging.
     273                 :             :      * - (M,P): the transaction is added in staging, and does not exist in main.
     274                 :             :      * - (P,R): the transaction exists in main, but is removed in staging.
     275                 :             :      *
     276                 :             :      * When staging does not exist, only (M,M) and (P,M) are possible.
     277                 :             :      */
     278                 :             :     struct Locator
     279                 :             :     {
     280                 :             :         /** Which Cluster the Entry appears in (nullptr = missing). */
     281                 :             :         Cluster* cluster{nullptr};
     282                 :             :         /** Where in the Cluster it appears (if cluster == nullptr: 0 = missing, -1 = removed). */
     283                 :             :         DepGraphIndex index{0};
     284                 :             : 
     285                 :             :         /** Mark this Locator as missing (= same as lower level, or non-existing if level 0). */
     286                 :           0 :         void SetMissing() noexcept { cluster = nullptr; index = 0; }
     287                 :             :         /** Mark this Locator as removed (not allowed in level 0). */
     288                 :           0 :         void SetRemoved() noexcept { cluster = nullptr; index = DepGraphIndex(-1); }
     289                 :             :         /** Mark this Locator as present, in the specified Cluster. */
     290                 :           0 :         void SetPresent(Cluster* c, DepGraphIndex i) noexcept { cluster = c; index = i; }
     291                 :             :         /** Check if this Locator is missing. */
     292   [ #  #  #  # ]:           0 :         bool IsMissing() const noexcept { return cluster == nullptr && index == 0; }
     293                 :             :         /** Check if this Locator is removed. */
     294   [ #  #  #  #  :           0 :         bool IsRemoved() const noexcept { return cluster == nullptr && index == DepGraphIndex(-1); }
                   #  # ]
     295                 :             :         /** Check if this Locator is present (in some Cluster). */
     296         [ #  # ]:           0 :         bool IsPresent() const noexcept { return cluster != nullptr; }
     297                 :             :     };
     298                 :             : 
     299                 :             :     /** Internal information about each transaction in a TxGraphImpl. */
     300                 :           0 :     struct Entry
     301                 :             :     {
     302                 :             :         /** Pointer to the corresponding Ref object if any, or nullptr if unlinked. */
     303                 :             :         Ref* m_ref{nullptr};
     304                 :             :         /** Which Cluster and position therein this Entry appears in. ([0] = main, [1] = staged). */
     305                 :             :         Locator m_locator[MAX_LEVELS];
     306                 :             :         /** The chunk feerate of this transaction in main (if present in m_locator[0]). */
     307                 :             :         FeePerWeight m_main_chunk_feerate;
     308                 :             :         /** The position this transaction has in the main linearization (if present). */
     309                 :             :         LinearizationIndex m_main_lin_index;
     310                 :             :     };
     311                 :             : 
     312                 :             :     /** The set of all transactions (in all levels combined). GraphIndex values index into this. */
     313                 :             :     std::vector<Entry> m_entries;
     314                 :             : 
     315                 :             :     /** Set of Entries which have no linked Ref anymore. */
     316                 :             :     std::vector<GraphIndex> m_unlinked;
     317                 :             : 
     318                 :             : public:
     319                 :             :     /** Construct a new TxGraphImpl with the specified maximum cluster count. */
     320                 :           0 :     explicit TxGraphImpl(DepGraphIndex max_cluster_count) noexcept :
     321                 :           0 :         m_max_cluster_count(max_cluster_count)
     322                 :             :     {
     323                 :           0 :         Assume(max_cluster_count >= 1);
     324                 :           0 :         Assume(max_cluster_count <= MAX_CLUSTER_COUNT_LIMIT);
     325                 :           0 :     }
     326                 :             : 
     327                 :             :     /** Destructor. */
     328                 :             :     ~TxGraphImpl() noexcept;
     329                 :             : 
     330                 :             :     // Cannot move or copy (would invalidate TxGraphImpl* in Ref, MiningOrder, EvictionOrder).
     331                 :             :     TxGraphImpl(const TxGraphImpl&) = delete;
     332                 :             :     TxGraphImpl& operator=(const TxGraphImpl&) = delete;
     333                 :             :     TxGraphImpl(TxGraphImpl&&) = delete;
     334                 :             :     TxGraphImpl& operator=(TxGraphImpl&&) = delete;
     335                 :             : 
     336                 :             :     // Simple helper functions.
     337                 :             : 
     338                 :             :     /** Swap the Entry referred to by a and the one referred to by b. */
     339                 :             :     void SwapIndexes(GraphIndex a, GraphIndex b) noexcept;
     340                 :             :     /** If idx exists in the specified level ClusterSet (explicitly, or in the level below and not
     341                 :             :     *   removed), return the Cluster it is in. Otherwise, return nullptr. */
     342                 :             :     Cluster* FindCluster(GraphIndex idx, int level) const noexcept;
     343                 :             :     /** Extract a Cluster from its ClusterSet. */
     344                 :             :     std::unique_ptr<Cluster> ExtractCluster(int level, QualityLevel quality, ClusterSetIndex setindex) noexcept;
     345                 :             :     /** Delete a Cluster. */
     346                 :             :     void DeleteCluster(Cluster& cluster) noexcept;
     347                 :             :     /** Insert a Cluster into its ClusterSet. */
     348                 :             :     ClusterSetIndex InsertCluster(int level, std::unique_ptr<Cluster>&& cluster, QualityLevel quality) noexcept;
     349                 :             :     /** Change the QualityLevel of a Cluster (identified by old_quality and old_index). */
     350                 :             :     void SetClusterQuality(int level, QualityLevel old_quality, ClusterSetIndex old_index, QualityLevel new_quality) noexcept;
     351                 :             :     /** Get the index of the top level ClusterSet (staging if it exists, main otherwise). */
     352         [ #  # ]:           0 :     int GetTopLevel() const noexcept { return m_staging_clusterset.has_value(); }
     353                 :             :     /** Get the specified level (staging if it exists and main_only is not specified, main otherwise). */
     354   [ #  #  #  #  :           0 :     int GetSpecifiedLevel(bool main_only) const noexcept { return m_staging_clusterset.has_value() && !main_only; }
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     355                 :             :     /** Get a reference to the ClusterSet at the specified level (which must exist). */
     356                 :             :     ClusterSet& GetClusterSet(int level) noexcept;
     357                 :             :     const ClusterSet& GetClusterSet(int level) const noexcept;
     358                 :             :     /** Make a transaction not exist at a specified level. It must currently exist there. */
     359                 :             :     void ClearLocator(int level, GraphIndex index) noexcept;
     360                 :             :     /** Find which Clusters in main conflict with ones in staging. */
     361                 :             :     std::vector<Cluster*> GetConflicts() const noexcept;
     362                 :             : 
     363                 :             :     // Functions for handling Refs.
     364                 :             : 
     365                 :             :     /** Only called by Ref's move constructor/assignment to update Ref locations. */
     366                 :           0 :     void UpdateRef(GraphIndex idx, Ref& new_location) noexcept final
     367                 :             :     {
     368                 :           0 :         auto& entry = m_entries[idx];
     369                 :           0 :         Assume(entry.m_ref != nullptr);
     370                 :           0 :         entry.m_ref = &new_location;
     371                 :           0 :     }
     372                 :             : 
     373                 :             :     /** Only called by Ref::~Ref to unlink Refs, and Ref's move assignment. */
     374                 :           0 :     void UnlinkRef(GraphIndex idx) noexcept final
     375                 :             :     {
     376                 :           0 :         auto& entry = m_entries[idx];
     377                 :           0 :         Assume(entry.m_ref != nullptr);
     378                 :           0 :         entry.m_ref = nullptr;
     379                 :             :         // Mark the transaction as to be removed in all levels where it explicitly or implicitly
     380                 :             :         // exists.
     381                 :           0 :         bool exists_anywhere{false};
     382                 :           0 :         bool exists{false};
     383         [ #  # ]:           0 :         for (int level = 0; level <= GetTopLevel(); ++level) {
     384         [ #  # ]:           0 :             if (entry.m_locator[level].IsPresent()) {
     385                 :             :                 exists_anywhere = true;
     386                 :             :                 exists = true;
     387         [ #  # ]:           0 :             } else if (entry.m_locator[level].IsRemoved()) {
     388                 :             :                 exists = false;
     389                 :             :             }
     390         [ #  # ]:           0 :             if (exists) {
     391                 :           0 :                 auto& clusterset = GetClusterSet(level);
     392                 :           0 :                 clusterset.m_to_remove.push_back(idx);
     393                 :             :                 // Force recomputation of grouping data.
     394         [ #  # ]:           0 :                 clusterset.m_group_data = std::nullopt;
     395                 :             :                 // Do not wipe the oversized state of main if staging exists. The reason for this
     396                 :             :                 // is that the alternative would mean that cluster merges may need to be applied to
     397                 :             :                 // a formerly-oversized main graph while staging exists (to satisfy chunk feerate
     398                 :             :                 // queries into main, for example), and such merges could conflict with pulls of
     399                 :             :                 // some of their constituents into staging.
     400   [ #  #  #  # ]:           0 :                 if (level == GetTopLevel() && clusterset.m_oversized == true) {
     401                 :           0 :                     clusterset.m_oversized = std::nullopt;
     402                 :             :                 }
     403                 :             :             }
     404                 :             :         }
     405                 :           0 :         m_unlinked.push_back(idx);
     406         [ #  # ]:           0 :         if (!exists_anywhere) Compact();
     407                 :           0 :     }
     408                 :             : 
     409                 :             :     // Functions related to various normalization/application steps.
     410                 :             :     /** Get rid of unlinked Entry objects in m_entries, if possible (this changes the GraphIndex
     411                 :             :      *  values for remaining Entry objects, so this only does something when no to-be-applied
     412                 :             :      *  operations or staged removals referring to GraphIndexes remain). */
     413                 :             :     void Compact() noexcept;
     414                 :             :     /** If cluster is not in staging, copy it there, and return a pointer to it. This has no
     415                 :             :     *   effect if only a main graph exists, but if staging exists this modifies the locators of its
     416                 :             :     *   transactions from inherited (P,M) to explicit (P,P). */
     417                 :             :     Cluster* PullIn(Cluster* cluster) noexcept;
     418                 :             :     /** Apply all removals queued up in m_to_remove to the relevant Clusters (which get a
     419                 :             :      *  NEEDS_SPLIT* QualityLevel) up to the specified level. */
     420                 :             :     void ApplyRemovals(int up_to_level) noexcept;
     421                 :             :     /** Split an individual cluster. */
     422                 :             :     void Split(Cluster& cluster) noexcept;
     423                 :             :     /** Split all clusters that need splitting up to the specified level. */
     424                 :             :     void SplitAll(int up_to_level) noexcept;
     425                 :             :     /** Populate m_group_data based on m_deps_to_add in the specified level. */
     426                 :             :     void GroupClusters(int level) noexcept;
     427                 :             :     /** Merge the specified clusters. */
     428                 :             :     void Merge(std::span<Cluster*> to_merge) noexcept;
     429                 :             :     /** Apply all m_deps_to_add to the relevant Clusters in the specified level. */
     430                 :             :     void ApplyDependencies(int level) noexcept;
     431                 :             :     /** Make a specified Cluster have quality ACCEPTABLE or OPTIMAL. */
     432                 :             :     void MakeAcceptable(Cluster& cluster) noexcept;
     433                 :             :     /** Make all Clusters at the specified level have quality ACCEPTABLE or OPTIMAL. */
     434                 :             :     void MakeAllAcceptable(int level) noexcept;
     435                 :             : 
     436                 :             :     // Implementations for the public TxGraph interface.
     437                 :             : 
     438                 :             :     Ref AddTransaction(const FeePerWeight& feerate) noexcept final;
     439                 :             :     void RemoveTransaction(const Ref& arg) noexcept final;
     440                 :             :     void AddDependency(const Ref& parent, const Ref& child) noexcept final;
     441                 :             :     void SetTransactionFee(const Ref&, int64_t fee) noexcept final;
     442                 :             : 
     443                 :             :     void DoWork() noexcept final;
     444                 :             : 
     445                 :             :     void StartStaging() noexcept final;
     446                 :             :     void CommitStaging() noexcept final;
     447                 :             :     void AbortStaging() noexcept final;
     448                 :           0 :     bool HaveStaging() const noexcept final { return m_staging_clusterset.has_value(); }
     449                 :             : 
     450                 :             :     bool Exists(const Ref& arg, bool main_only = false) noexcept final;
     451                 :             :     FeePerWeight GetMainChunkFeerate(const Ref& arg) noexcept final;
     452                 :             :     FeePerWeight GetIndividualFeerate(const Ref& arg) noexcept final;
     453                 :             :     std::vector<Ref*> GetCluster(const Ref& arg, bool main_only = false) noexcept final;
     454                 :             :     std::vector<Ref*> GetAncestors(const Ref& arg, bool main_only = false) noexcept final;
     455                 :             :     std::vector<Ref*> GetDescendants(const Ref& arg, bool main_only = false) noexcept final;
     456                 :             :     std::vector<Ref*> GetAncestorsUnion(std::span<const Ref* const> args, bool main_only = false) noexcept final;
     457                 :             :     std::vector<Ref*> GetDescendantsUnion(std::span<const Ref* const> args, bool main_only = false) noexcept final;
     458                 :             :     GraphIndex GetTransactionCount(bool main_only = false) noexcept final;
     459                 :             :     bool IsOversized(bool main_only = false) noexcept final;
     460                 :             :     std::strong_ordering CompareMainOrder(const Ref& a, const Ref& b) noexcept final;
     461                 :             :     GraphIndex CountDistinctClusters(std::span<const Ref* const> refs, bool main_only = false) noexcept final;
     462                 :             : 
     463                 :             :     void SanityCheck() const final;
     464                 :             : };
     465                 :             : 
     466                 :           0 : TxGraphImpl::ClusterSet& TxGraphImpl::GetClusterSet(int level) noexcept
     467                 :             : {
     468         [ #  # ]:           0 :     if (level == 0) return m_main_clusterset;
     469                 :           0 :     Assume(level == 1);
     470                 :           0 :     Assume(m_staging_clusterset.has_value());
     471                 :           0 :     return *m_staging_clusterset;
     472                 :             : }
     473                 :             : 
     474                 :           0 : const TxGraphImpl::ClusterSet& TxGraphImpl::GetClusterSet(int level) const noexcept
     475                 :             : {
     476         [ #  # ]:           0 :     if (level == 0) return m_main_clusterset;
     477                 :           0 :     Assume(level == 1);
     478                 :           0 :     Assume(m_staging_clusterset.has_value());
     479                 :           0 :     return *m_staging_clusterset;
     480                 :             : }
     481                 :             : 
     482                 :           0 : void TxGraphImpl::ClearLocator(int level, GraphIndex idx) noexcept
     483                 :             : {
     484         [ #  # ]:           0 :     auto& entry = m_entries[idx];
     485                 :           0 :     auto& clusterset = GetClusterSet(level);
     486                 :           0 :     Assume(entry.m_locator[level].IsPresent());
     487                 :             :     // Change the locator from Present to Missing or Removed.
     488   [ #  #  #  # ]:           0 :     if (level == 0 || !entry.m_locator[level - 1].IsPresent()) {
     489                 :           0 :         entry.m_locator[level].SetMissing();
     490                 :             :     } else {
     491                 :           0 :         entry.m_locator[level].SetRemoved();
     492                 :           0 :         clusterset.m_removed.push_back(idx);
     493                 :             :     }
     494                 :             :     // Update the transaction count.
     495                 :           0 :     --clusterset.m_txcount;
     496                 :             :     // If clearing main, adjust the status of Locators of this transaction in staging, if it exists.
     497   [ #  #  #  # ]:           0 :     if (level == 0 && GetTopLevel() == 1) {
     498         [ #  # ]:           0 :         if (entry.m_locator[1].IsRemoved()) {
     499                 :           0 :             entry.m_locator[1].SetMissing();
     500         [ #  # ]:           0 :         } else if (!entry.m_locator[1].IsPresent()) {
     501                 :           0 :             --m_staging_clusterset->m_txcount;
     502                 :             :         }
     503                 :             :     }
     504                 :           0 : }
     505                 :             : 
     506                 :           0 : void Cluster::Updated(TxGraphImpl& graph) noexcept
     507                 :             : {
     508                 :             :     // Update all the Locators for this Cluster's Entry objects.
     509         [ #  # ]:           0 :     for (DepGraphIndex idx : m_linearization) {
     510                 :           0 :         auto& entry = graph.m_entries[m_mapping[idx]];
     511                 :           0 :         entry.m_locator[m_level].SetPresent(this, idx);
     512                 :             :     }
     513                 :             :     // If this is for the main graph (level = 0), and the Cluster's quality is ACCEPTABLE or
     514                 :             :     // OPTIMAL, compute its chunking and store its information in the Entry's m_main_lin_index
     515                 :             :     // and m_main_chunk_feerate. These fields are only accessed after making the entire graph
     516                 :             :     // ACCEPTABLE, so it is pointless to compute these if we haven't reached that quality level
     517                 :             :     // yet.
     518         [ #  # ]:           0 :     if (m_level == 0 && IsAcceptable()) {
     519                 :           0 :         LinearizationChunking chunking(m_depgraph, m_linearization);
     520                 :           0 :         LinearizationIndex lin_idx{0};
     521                 :             :         // Iterate over the chunks.
     522         [ #  # ]:           0 :         for (unsigned chunk_idx = 0; chunk_idx < chunking.NumChunksLeft(); ++chunk_idx) {
     523                 :           0 :             auto chunk = chunking.GetChunk(chunk_idx);
     524                 :           0 :             Assume(chunk.transactions.Any());
     525                 :             :             // Iterate over the transactions in the linearization, which must match those in chunk.
     526                 :           0 :             do {
     527         [ #  # ]:           0 :                 DepGraphIndex idx = m_linearization[lin_idx];
     528                 :           0 :                 GraphIndex graph_idx = m_mapping[idx];
     529                 :           0 :                 auto& entry = graph.m_entries[graph_idx];
     530                 :           0 :                 entry.m_main_lin_index = lin_idx++;
     531         [ #  # ]:           0 :                 entry.m_main_chunk_feerate = FeePerWeight::FromFeeFrac(chunk.feerate);
     532                 :           0 :                 Assume(chunk.transactions[idx]);
     533         [ #  # ]:           0 :                 chunk.transactions.Reset(idx);
     534         [ #  # ]:           0 :             } while(chunk.transactions.Any());
     535                 :             :         }
     536                 :           0 :     }
     537                 :           0 : }
     538                 :             : 
     539                 :           0 : void Cluster::GetConflicts(const TxGraphImpl& graph, std::vector<Cluster*>& out) const noexcept
     540                 :             : {
     541                 :           0 :     Assume(m_level == 1);
     542         [ #  # ]:           0 :     for (auto i : m_linearization) {
     543         [ #  # ]:           0 :         auto& entry = graph.m_entries[m_mapping[i]];
     544                 :             :         // For every transaction Entry in this Cluster, if it also exists in a lower-level Cluster,
     545                 :             :         // then that Cluster conflicts.
     546         [ #  # ]:           0 :         if (entry.m_locator[0].IsPresent()) {
     547                 :           0 :             out.push_back(entry.m_locator[0].cluster);
     548                 :             :         }
     549                 :             :     }
     550                 :           0 : }
     551                 :             : 
     552                 :           0 : std::vector<Cluster*> TxGraphImpl::GetConflicts() const noexcept
     553                 :             : {
     554                 :           0 :     Assume(GetTopLevel() == 1);
     555                 :           0 :     auto& clusterset = GetClusterSet(1);
     556                 :           0 :     std::vector<Cluster*> ret;
     557                 :             :     // All main Clusters containing transactions in m_removed (so (P,R) ones) are conflicts.
     558         [ #  # ]:           0 :     for (auto i : clusterset.m_removed) {
     559         [ #  # ]:           0 :         auto& entry = m_entries[i];
     560         [ #  # ]:           0 :         if (entry.m_locator[0].IsPresent()) {
     561                 :           0 :             ret.push_back(entry.m_locator[0].cluster);
     562                 :             :         }
     563                 :             :     }
     564                 :             :     // Then go over all Clusters at this level, and find their conflicts (the (P,P) ones).
     565         [ #  # ]:           0 :     for (int quality = 0; quality < int(QualityLevel::NONE); ++quality) {
     566                 :           0 :         auto& clusters = clusterset.m_clusters[quality];
     567         [ #  # ]:           0 :         for (const auto& cluster : clusters) {
     568                 :           0 :             cluster->GetConflicts(*this, ret);
     569                 :             :         }
     570                 :             :     }
     571                 :             :     // Deduplicate the result (the same Cluster may appear multiple times).
     572                 :           0 :     std::sort(ret.begin(), ret.end());
     573                 :           0 :     ret.erase(std::unique(ret.begin(), ret.end()), ret.end());
     574                 :           0 :     return ret;
     575                 :             : }
     576                 :             : 
     577                 :           0 : Cluster* Cluster::CopyToStaging(TxGraphImpl& graph) const noexcept
     578                 :             : {
     579                 :             :     // Construct an empty Cluster.
     580                 :           0 :     auto ret = std::make_unique<Cluster>();
     581                 :           0 :     auto ptr = ret.get();
     582                 :             :     // Copy depgraph, mapping, and linearization/
     583                 :           0 :     ptr->m_depgraph = m_depgraph;
     584                 :           0 :     ptr->m_mapping = m_mapping;
     585                 :           0 :     ptr->m_linearization = m_linearization;
     586                 :             :     // Insert the new Cluster into the graph.
     587                 :           0 :     graph.InsertCluster(1, std::move(ret), m_quality);
     588                 :             :     // Update its Locators.
     589                 :           0 :     ptr->Updated(graph);
     590                 :           0 :     return ptr;
     591                 :           0 : }
     592                 :             : 
     593                 :           0 : void Cluster::ApplyRemovals(TxGraphImpl& graph, std::span<GraphIndex>& to_remove) noexcept
     594                 :             : {
     595                 :             :     // Iterate over the prefix of to_remove that applies to this cluster.
     596                 :           0 :     Assume(!to_remove.empty());
     597                 :           0 :     SetType todo;
     598                 :           0 :     do {
     599         [ #  # ]:           0 :         GraphIndex idx = to_remove.front();
     600                 :           0 :         Assume(idx < graph.m_entries.size());
     601         [ #  # ]:           0 :         auto& entry = graph.m_entries[idx];
     602                 :           0 :         auto& locator = entry.m_locator[m_level];
     603                 :             :         // Stop once we hit an entry that applies to another Cluster.
     604         [ #  # ]:           0 :         if (locator.cluster != this) break;
     605                 :             :         // - Remember it in a set of to-remove DepGraphIndexes.
     606         [ #  # ]:           0 :         todo.Set(locator.index);
     607                 :             :         // - Remove from m_mapping. This isn't strictly necessary as unused positions in m_mapping
     608                 :             :         //   are just never accessed, but set it to -1 here to increase the ability to detect a bug
     609                 :             :         //   that causes it to be accessed regardless.
     610         [ #  # ]:           0 :         m_mapping[locator.index] = GraphIndex(-1);
     611                 :             :         // - Remove its linearization index from the Entry (if in main).
     612         [ #  # ]:           0 :         if (m_level == 0) {
     613                 :           0 :             entry.m_main_lin_index = LinearizationIndex(-1);
     614                 :             :         }
     615                 :             :         // - Mark it as missing/removed in the Entry's locator.
     616                 :           0 :         graph.ClearLocator(m_level, idx);
     617         [ #  # ]:           0 :         to_remove = to_remove.subspan(1);
     618         [ #  # ]:           0 :     } while(!to_remove.empty());
     619                 :             : 
     620                 :           0 :     auto quality = m_quality;
     621                 :           0 :     Assume(todo.Any());
     622                 :             :     // Wipe from the Cluster's DepGraph (this is O(n) regardless of the number of entries
     623                 :             :     // removed, so we benefit from batching all the removals).
     624                 :           0 :     m_depgraph.RemoveTransactions(todo);
     625                 :           0 :     m_mapping.resize(m_depgraph.PositionRange());
     626                 :             : 
     627                 :             :     // First remove all removals at the end of the linearization.
     628   [ #  #  #  # ]:           0 :     while (!m_linearization.empty() && todo[m_linearization.back()]) {
     629                 :           0 :         todo.Reset(m_linearization.back());
     630                 :           0 :         m_linearization.pop_back();
     631                 :             :     }
     632         [ #  # ]:           0 :     if (todo.None()) {
     633                 :             :         // If no further removals remain, and thus all removals were at the end, we may be able
     634                 :             :         // to leave the cluster at a better quality level.
     635         [ #  # ]:           0 :         if (IsAcceptable(/*after_split=*/true)) {
     636                 :             :             quality = QualityLevel::NEEDS_SPLIT_ACCEPTABLE;
     637                 :             :         } else {
     638                 :             :             quality = QualityLevel::NEEDS_SPLIT;
     639                 :             :         }
     640                 :             :     } else {
     641                 :             :         // If more removals remain, filter those out of m_linearization.
     642                 :           0 :         m_linearization.erase(std::remove_if(
     643                 :             :             m_linearization.begin(),
     644                 :             :             m_linearization.end(),
     645   [ #  #  #  #  :           0 :             [&](auto pos) { return todo[pos]; }), m_linearization.end());
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     646                 :           0 :         quality = QualityLevel::NEEDS_SPLIT;
     647                 :             :     }
     648                 :           0 :     graph.SetClusterQuality(m_level, m_quality, m_setindex, quality);
     649                 :           0 :     Updated(graph);
     650                 :           0 : }
     651                 :             : 
     652                 :           0 : void Cluster::Clear(TxGraphImpl& graph) noexcept
     653                 :             : {
     654         [ #  # ]:           0 :     for (auto i : m_linearization) {
     655                 :           0 :         graph.ClearLocator(m_level, m_mapping[i]);
     656                 :             :     }
     657                 :           0 :     m_depgraph = {};
     658         [ #  # ]:           0 :     m_linearization.clear();
     659         [ #  # ]:           0 :     m_mapping.clear();
     660                 :           0 : }
     661                 :             : 
     662                 :           0 : void Cluster::MoveToMain(TxGraphImpl& graph) noexcept
     663                 :             : {
     664                 :           0 :     Assume(m_level == 1);
     665         [ #  # ]:           0 :     for (auto i : m_linearization) {
     666                 :           0 :         GraphIndex idx = m_mapping[i];
     667                 :           0 :         auto& entry = graph.m_entries[idx];
     668                 :           0 :         entry.m_locator[1].SetMissing();
     669                 :             :     }
     670                 :           0 :     auto quality = m_quality;
     671                 :           0 :     auto cluster = graph.ExtractCluster(1, quality, m_setindex);
     672                 :           0 :     graph.InsertCluster(0, std::move(cluster), quality);
     673                 :           0 :     Updated(graph);
     674                 :           0 : }
     675                 :             : 
     676                 :           0 : bool Cluster::Split(TxGraphImpl& graph) noexcept
     677                 :             : {
     678                 :             :     // This function can only be called when the Cluster needs splitting.
     679                 :           0 :     Assume(NeedsSplitting());
     680                 :             :     // Determine the new quality the split-off Clusters will have.
     681         [ #  # ]:           0 :     QualityLevel new_quality = IsAcceptable(/*after_split=*/true) ? QualityLevel::ACCEPTABLE
     682                 :             :                                                                   : QualityLevel::NEEDS_RELINEARIZE;
     683                 :             :     // If we're going to produce ACCEPTABLE clusters (i.e., when in NEEDS_SPLIT_ACCEPTABLE), we
     684                 :             :     // need to post-linearize to make sure the split-out versions are all connected (as
     685                 :             :     // connectivity may have changed by removing part of the cluster). This could be done on each
     686                 :             :     // resulting split-out cluster separately, but it is simpler to do it once up front before
     687                 :             :     // splitting. This step is not necessary if the resulting clusters are NEEDS_RELINEARIZE, as
     688                 :             :     // they will be post-linearized anyway in MakeAcceptable().
     689                 :             :     if (new_quality == QualityLevel::ACCEPTABLE) {
     690                 :           0 :         PostLinearize(m_depgraph, m_linearization);
     691                 :             :     }
     692                 :             :     /** Which positions are still left in this Cluster. */
     693                 :           0 :     auto todo = m_depgraph.Positions();
     694                 :             :     /** Mapping from transaction positions in this Cluster to the Cluster where it ends up, and
     695                 :             :      *  its position therein. */
     696                 :           0 :     std::vector<std::pair<Cluster*, DepGraphIndex>> remap(m_depgraph.PositionRange());
     697                 :           0 :     std::vector<Cluster*> new_clusters;
     698                 :           0 :     bool first{true};
     699                 :             :     // Iterate over the connected components of this Cluster's m_depgraph.
     700         [ #  # ]:           0 :     while (todo.Any()) {
     701                 :           0 :         auto component = m_depgraph.FindConnectedComponent(todo);
     702         [ #  # ]:           0 :         if (first && component == todo) {
     703                 :             :             // The existing Cluster is an entire component. Leave it be, but update its quality.
     704                 :           0 :             Assume(todo == m_depgraph.Positions());
     705                 :           0 :             graph.SetClusterQuality(m_level, m_quality, m_setindex, new_quality);
     706                 :             :             // If this made the quality ACCEPTABLE or OPTIMAL, we need to compute and cache its
     707                 :             :             // chunking.
     708                 :           0 :             Updated(graph);
     709                 :           0 :             return false;
     710                 :             :         }
     711                 :           0 :         first = false;
     712                 :             :         // Construct a new Cluster to hold the found component.
     713                 :           0 :         auto new_cluster = std::make_unique<Cluster>();
     714                 :           0 :         new_clusters.push_back(new_cluster.get());
     715                 :             :         // Remember that all the component's transactions go to this new Cluster. The positions
     716                 :             :         // will be determined below, so use -1 for now.
     717   [ #  #  #  # ]:           0 :         for (auto i : component) {
     718         [ #  # ]:           0 :             remap[i] = {new_cluster.get(), DepGraphIndex(-1)};
     719                 :             :         }
     720                 :           0 :         graph.InsertCluster(m_level, std::move(new_cluster), new_quality);
     721                 :           0 :         todo -= component;
     722                 :           0 :     }
     723                 :             :     // Redistribute the transactions.
     724         [ #  # ]:           0 :     for (auto i : m_linearization) {
     725                 :             :         /** The cluster which transaction originally in position i is moved to. */
     726                 :           0 :         Cluster* new_cluster = remap[i].first;
     727                 :             :         // Copy the transaction to the new cluster's depgraph, and remember the position.
     728                 :           0 :         remap[i].second = new_cluster->m_depgraph.AddTransaction(m_depgraph.FeeRate(i));
     729                 :             :         // Create new mapping entry.
     730                 :           0 :         new_cluster->m_mapping.push_back(m_mapping[i]);
     731                 :             :         // Create a new linearization entry. As we're only appending transactions, they equal the
     732                 :             :         // DepGraphIndex.
     733                 :           0 :         new_cluster->m_linearization.push_back(remap[i].second);
     734                 :             :     }
     735                 :             :     // Redistribute the dependencies.
     736         [ #  # ]:           0 :     for (auto i : m_linearization) {
     737                 :             :         /** The cluster transaction in position i is moved to. */
     738                 :           0 :         Cluster* new_cluster = remap[i].first;
     739                 :             :         // Copy its parents, translating positions.
     740                 :           0 :         SetType new_parents;
     741   [ #  #  #  #  :           0 :         for (auto par : m_depgraph.GetReducedParents(i)) new_parents.Set(remap[par].second);
                   #  # ]
     742                 :           0 :         new_cluster->m_depgraph.AddDependencies(new_parents, remap[i].second);
     743                 :             :     }
     744                 :             :     // Update all the Locators of moved transactions.
     745         [ #  # ]:           0 :     for (Cluster* new_cluster : new_clusters) {
     746                 :           0 :         new_cluster->Updated(graph);
     747                 :             :     }
     748                 :             :     // Wipe this Cluster, and return that it needs to be deleted.
     749                 :           0 :     m_depgraph = DepGraph<SetType>{};
     750         [ #  # ]:           0 :     m_mapping.clear();
     751         [ #  # ]:           0 :     m_linearization.clear();
     752                 :             :     return true;
     753                 :           0 : }
     754                 :             : 
     755                 :           0 : void Cluster::Merge(TxGraphImpl& graph, Cluster& other) noexcept
     756                 :             : {
     757                 :             :     /** Vector to store the positions in this Cluster for each position in other. */
     758                 :           0 :     std::vector<DepGraphIndex> remap(other.m_depgraph.PositionRange());
     759                 :             :     // Iterate over all transactions in the other Cluster (the one being absorbed).
     760         [ #  # ]:           0 :     for (auto pos : other.m_linearization) {
     761                 :           0 :         auto idx = other.m_mapping[pos];
     762                 :             :         // Copy the transaction into this Cluster, and remember its position.
     763                 :           0 :         auto new_pos = m_depgraph.AddTransaction(other.m_depgraph.FeeRate(pos));
     764         [ #  # ]:           0 :         remap[pos] = new_pos;
     765         [ #  # ]:           0 :         if (new_pos == m_mapping.size()) {
     766                 :           0 :             m_mapping.push_back(idx);
     767                 :             :         } else {
     768                 :           0 :             m_mapping[new_pos] = idx;
     769                 :             :         }
     770                 :           0 :         m_linearization.push_back(new_pos);
     771                 :             :         // Copy the transaction's dependencies, translating them using remap. Note that since
     772                 :             :         // pos iterates over other.m_linearization, which is in topological order, all parents
     773                 :             :         // of pos should already be in remap.
     774                 :           0 :         SetType parents;
     775   [ #  #  #  # ]:           0 :         for (auto par : other.m_depgraph.GetReducedParents(pos)) {
     776         [ #  # ]:           0 :             parents.Set(remap[par]);
     777                 :             :         }
     778                 :           0 :         m_depgraph.AddDependencies(parents, remap[pos]);
     779                 :             :         // Update the transaction's Locator. There is no need to call Updated() to update chunk
     780                 :             :         // feerates, as Updated() will be invoked by Cluster::ApplyDependencies on the resulting
     781                 :             :         // merged Cluster later anyway).
     782                 :           0 :         graph.m_entries[idx].m_locator[m_level].SetPresent(this, new_pos);
     783                 :             :     }
     784                 :             :     // Purge the other Cluster, now that everything has been moved.
     785                 :           0 :     other.m_depgraph = DepGraph<SetType>{};
     786         [ #  # ]:           0 :     other.m_linearization.clear();
     787         [ #  # ]:           0 :     other.m_mapping.clear();
     788                 :           0 : }
     789                 :             : 
     790                 :           0 : void Cluster::ApplyDependencies(TxGraphImpl& graph, std::span<std::pair<GraphIndex, GraphIndex>> to_apply) noexcept
     791                 :             : {
     792                 :             :     // This function is invoked by TxGraphImpl::ApplyDependencies after merging groups of Clusters
     793                 :             :     // between which dependencies are added, which simply concatenates their linearizations. Invoke
     794                 :             :     // PostLinearize, which has the effect that the linearization becomes a merge-sort of the
     795                 :             :     // constituent linearizations. Do this here rather than in Cluster::Merge, because this
     796                 :             :     // function is only invoked once per merged Cluster, rather than once per constituent one.
     797                 :             :     // This concatenation + post-linearization could be replaced with an explicit merge-sort.
     798                 :           0 :     PostLinearize(m_depgraph, m_linearization);
     799                 :             : 
     800                 :             :     // Sort the list of dependencies to apply by child, so those can be applied in batch.
     801   [ #  #  #  #  :           0 :     std::sort(to_apply.begin(), to_apply.end(), [](auto& a, auto& b) { return a.second < b.second; });
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     802                 :             :     // Iterate over groups of to-be-added dependencies with the same child.
     803                 :           0 :     auto it = to_apply.begin();
     804         [ #  # ]:           0 :     while (it != to_apply.end()) {
     805                 :           0 :         auto& first_child = graph.m_entries[it->second].m_locator[m_level];
     806                 :           0 :         const auto child_idx = first_child.index;
     807                 :             :         // Iterate over all to-be-added dependencies within that same child, gather the relevant
     808                 :             :         // parents.
     809                 :           0 :         SetType parents;
     810         [ #  # ]:           0 :         while (it != to_apply.end()) {
     811         [ #  # ]:           0 :             auto& child = graph.m_entries[it->second].m_locator[m_level];
     812                 :           0 :             auto& parent = graph.m_entries[it->first].m_locator[m_level];
     813                 :           0 :             Assume(child.cluster == this && parent.cluster == this);
     814         [ #  # ]:           0 :             if (child.index != child_idx) break;
     815                 :           0 :             parents.Set(parent.index);
     816                 :           0 :             ++it;
     817                 :             :         }
     818                 :             :         // Push all dependencies to the underlying DepGraph. Note that this is O(N) in the size of
     819                 :             :         // the cluster, regardless of the number of parents being added, so batching them together
     820                 :             :         // has a performance benefit.
     821                 :           0 :         m_depgraph.AddDependencies(parents, child_idx);
     822                 :             :     }
     823                 :             : 
     824                 :             :     // Finally fix the linearization, as the new dependencies may have invalidated the
     825                 :             :     // linearization, and post-linearize it to fix up the worst problems with it.
     826                 :           0 :     FixLinearization(m_depgraph, m_linearization);
     827                 :           0 :     PostLinearize(m_depgraph, m_linearization);
     828                 :             : 
     829                 :             :     // Finally push the changes to graph.m_entries.
     830                 :           0 :     Updated(graph);
     831                 :           0 : }
     832                 :             : 
     833                 :           0 : TxGraphImpl::~TxGraphImpl() noexcept
     834                 :             : {
     835                 :             :     // If Refs outlive the TxGraphImpl they refer to, unlink them, so that their destructor does not
     836                 :             :     // try to reach into a non-existing TxGraphImpl anymore.
     837         [ #  # ]:           0 :     for (auto& entry : m_entries) {
     838         [ #  # ]:           0 :         if (entry.m_ref != nullptr) {
     839                 :           0 :             GetRefGraph(*entry.m_ref) = nullptr;
     840                 :             :         }
     841                 :             :     }
     842                 :           0 : }
     843                 :             : 
     844                 :           0 : std::unique_ptr<Cluster> TxGraphImpl::ExtractCluster(int level, QualityLevel quality, ClusterSetIndex setindex) noexcept
     845                 :             : {
     846                 :           0 :     Assume(quality != QualityLevel::NONE);
     847                 :             : 
     848                 :           0 :     auto& clusterset = GetClusterSet(level);
     849         [ #  # ]:           0 :     auto& quality_clusters = clusterset.m_clusters[int(quality)];
     850                 :           0 :     Assume(setindex < quality_clusters.size());
     851                 :             : 
     852                 :             :     // Extract the Cluster-owning unique_ptr.
     853         [ #  # ]:           0 :     std::unique_ptr<Cluster> ret = std::move(quality_clusters[setindex]);
     854         [ #  # ]:           0 :     ret->m_quality = QualityLevel::NONE;
     855                 :           0 :     ret->m_setindex = ClusterSetIndex(-1);
     856                 :           0 :     ret->m_level = -1;
     857                 :             : 
     858                 :             :     // Clean up space in quality_cluster.
     859         [ #  # ]:           0 :     auto max_setindex = quality_clusters.size() - 1;
     860         [ #  # ]:           0 :     if (setindex != max_setindex) {
     861                 :             :         // If the cluster was not the last element of quality_clusters, move that to take its place.
     862                 :           0 :         quality_clusters.back()->m_setindex = setindex;
     863                 :           0 :         quality_clusters.back()->m_level = level;
     864                 :           0 :         quality_clusters[setindex] = std::move(quality_clusters.back());
     865                 :             :     }
     866                 :             :     // The last element of quality_clusters is now unused; drop it.
     867                 :           0 :     quality_clusters.pop_back();
     868                 :             : 
     869                 :           0 :     return ret;
     870                 :             : }
     871                 :             : 
     872                 :           0 : ClusterSetIndex TxGraphImpl::InsertCluster(int level, std::unique_ptr<Cluster>&& cluster, QualityLevel quality) noexcept
     873                 :             : {
     874                 :             :     // Cannot insert with quality level NONE (as that would mean not inserted).
     875                 :           0 :     Assume(quality != QualityLevel::NONE);
     876                 :             :     // The passed-in Cluster must not currently be in the TxGraphImpl.
     877                 :           0 :     Assume(cluster->m_quality == QualityLevel::NONE);
     878                 :             : 
     879                 :             :     // Append it at the end of the relevant TxGraphImpl::m_cluster.
     880                 :           0 :     auto& clusterset = GetClusterSet(level);
     881                 :           0 :     auto& quality_clusters = clusterset.m_clusters[int(quality)];
     882                 :           0 :     ClusterSetIndex ret = quality_clusters.size();
     883                 :           0 :     cluster->m_quality = quality;
     884                 :           0 :     cluster->m_setindex = ret;
     885                 :           0 :     cluster->m_level = level;
     886                 :           0 :     quality_clusters.push_back(std::move(cluster));
     887                 :           0 :     return ret;
     888                 :             : }
     889                 :             : 
     890                 :           0 : void TxGraphImpl::SetClusterQuality(int level, QualityLevel old_quality, ClusterSetIndex old_index, QualityLevel new_quality) noexcept
     891                 :             : {
     892                 :           0 :     Assume(new_quality != QualityLevel::NONE);
     893                 :             : 
     894                 :             :     // Don't do anything if the quality did not change.
     895         [ #  # ]:           0 :     if (old_quality == new_quality) return;
     896                 :             :     // Extract the cluster from where it currently resides.
     897                 :           0 :     auto cluster_ptr = ExtractCluster(level, old_quality, old_index);
     898                 :             :     // And re-insert it where it belongs.
     899                 :           0 :     InsertCluster(level, std::move(cluster_ptr), new_quality);
     900                 :           0 : }
     901                 :             : 
     902                 :           0 : void TxGraphImpl::DeleteCluster(Cluster& cluster) noexcept
     903                 :             : {
     904                 :             :     // Extract the cluster from where it currently resides.
     905                 :           0 :     auto cluster_ptr = ExtractCluster(cluster.m_level, cluster.m_quality, cluster.m_setindex);
     906                 :             :     // And throw it away.
     907         [ #  # ]:           0 :     cluster_ptr.reset();
     908                 :           0 : }
     909                 :             : 
     910                 :           0 : Cluster* TxGraphImpl::FindCluster(GraphIndex idx, int level) const noexcept
     911                 :             : {
     912         [ #  # ]:           0 :     Assume(level >= 0 && level <= GetTopLevel());
     913                 :           0 :     auto& entry = m_entries[idx];
     914                 :             :     // Search the entry's locators from top to bottom.
     915         [ #  # ]:           0 :     for (int l = level; l >= 0; --l) {
     916                 :             :         // If the locator is missing, dig deeper; it may exist at a lower level and therefore be
     917                 :             :         // implicitly existing at this level too.
     918         [ #  # ]:           0 :         if (entry.m_locator[l].IsMissing()) continue;
     919                 :             :         // If the locator has the entry marked as explicitly removed, stop.
     920         [ #  # ]:           0 :         if (entry.m_locator[l].IsRemoved()) break;
     921                 :             :         // Otherwise, we have found the topmost ClusterSet that contains this entry.
     922                 :             :         return entry.m_locator[l].cluster;
     923                 :             :     }
     924                 :             :     // If no non-empty locator was found, or an explicitly removed was hit, return nothing.
     925                 :             :     return nullptr;
     926                 :             : }
     927                 :             : 
     928                 :           0 : Cluster* TxGraphImpl::PullIn(Cluster* cluster) noexcept
     929                 :             : {
     930         [ #  # ]:           0 :     int to_level = GetTopLevel();
     931         [ #  # ]:           0 :     if (to_level == 0) return cluster;
     932                 :           0 :     int level = cluster->m_level;
     933                 :           0 :     Assume(level <= to_level);
     934                 :             :     // Copy the Cluster from main to staging, if it's not already there.
     935         [ #  # ]:           0 :     if (level == 0) {
     936                 :             :         // Make the Cluster Acceptable before copying. This isn't strictly necessary, but doing it
     937                 :             :         // now avoids doing double work later.
     938                 :           0 :         MakeAcceptable(*cluster);
     939                 :           0 :         cluster = cluster->CopyToStaging(*this);
     940                 :             :     }
     941                 :             :     return cluster;
     942                 :             : }
     943                 :             : 
     944                 :           0 : void TxGraphImpl::ApplyRemovals(int up_to_level) noexcept
     945                 :             : {
     946         [ #  # ]:           0 :     Assume(up_to_level >= 0 && up_to_level <= GetTopLevel());
     947         [ #  # ]:           0 :     for (int level = 0; level <= up_to_level; ++level) {
     948                 :           0 :         auto& clusterset = GetClusterSet(level);
     949                 :           0 :         auto& to_remove = clusterset.m_to_remove;
     950                 :             :         // Skip if there is nothing to remove in this level.
     951         [ #  # ]:           0 :         if (to_remove.empty()) continue;
     952                 :             :         // Pull in all Clusters that are not in staging.
     953         [ #  # ]:           0 :         if (level == 1) {
     954         [ #  # ]:           0 :             for (GraphIndex index : to_remove) {
     955                 :           0 :                 auto cluster = FindCluster(index, level);
     956         [ #  # ]:           0 :                 if (cluster != nullptr) PullIn(cluster);
     957                 :             :             }
     958                 :             :         }
     959                 :             :         // Group the set of to-be-removed entries by Cluster*.
     960                 :           0 :         std::sort(to_remove.begin(), to_remove.end(), [&](GraphIndex a, GraphIndex b) noexcept {
     961                 :           0 :             return std::less{}(m_entries[a].m_locator[level].cluster, m_entries[b].m_locator[level].cluster);
     962                 :             :         });
     963                 :             :         // Process per Cluster.
     964                 :           0 :         std::span to_remove_span{to_remove};
     965         [ #  # ]:           0 :         while (!to_remove_span.empty()) {
     966         [ #  # ]:           0 :             Cluster* cluster = m_entries[to_remove_span.front()].m_locator[level].cluster;
     967         [ #  # ]:           0 :             if (cluster != nullptr) {
     968                 :             :                 // If the first to_remove_span entry's Cluster exists, hand to_remove_span to it, so it
     969                 :             :                 // can pop off whatever applies to it.
     970                 :           0 :                 cluster->ApplyRemovals(*this, to_remove_span);
     971                 :             :             } else {
     972                 :             :                 // Otherwise, skip this already-removed entry. This may happen when
     973                 :             :                 // RemoveTransaction was called twice on the same Ref, for example.
     974                 :           0 :                 to_remove_span = to_remove_span.subspan(1);
     975                 :             :             }
     976                 :             :         }
     977         [ #  # ]:           0 :         to_remove.clear();
     978                 :             :     }
     979                 :           0 :     Compact();
     980                 :           0 : }
     981                 :             : 
     982                 :           0 : void TxGraphImpl::SwapIndexes(GraphIndex a, GraphIndex b) noexcept
     983                 :             : {
     984                 :           0 :     Assume(a < m_entries.size());
     985                 :           0 :     Assume(b < m_entries.size());
     986                 :             :     // Swap the Entry objects.
     987                 :           0 :     std::swap(m_entries[a], m_entries[b]);
     988                 :             :     // Iterate over both objects.
     989         [ #  # ]:           0 :     for (int i = 0; i < 2; ++i) {
     990         [ #  # ]:           0 :         GraphIndex idx = i ? b : a;
     991         [ #  # ]:           0 :         Entry& entry = m_entries[idx];
     992                 :             :         // Update linked Ref, if any exists.
     993         [ #  # ]:           0 :         if (entry.m_ref) GetRefIndex(*entry.m_ref) = idx;
     994                 :             :         // Update the locators for both levels. The rest of the Entry information will not change,
     995                 :             :         // so no need to invoke Cluster::Updated().
     996         [ #  # ]:           0 :         for (int level = 0; level < MAX_LEVELS; ++level) {
     997                 :           0 :             Locator& locator = entry.m_locator[level];
     998         [ #  # ]:           0 :             if (locator.IsPresent()) {
     999                 :           0 :                 locator.cluster->UpdateMapping(locator.index, idx);
    1000                 :             :             }
    1001                 :             :         }
    1002                 :             :     }
    1003                 :           0 : }
    1004                 :             : 
    1005                 :           0 : void TxGraphImpl::Compact() noexcept
    1006                 :             : {
    1007                 :             :     // We cannot compact while any to-be-applied operations or staged removals remain as we'd need
    1008                 :             :     // to rewrite them. It is easier to delay the compaction until they have been applied.
    1009         [ #  # ]:           0 :     if (!m_main_clusterset.m_deps_to_add.empty()) return;
    1010         [ #  # ]:           0 :     if (!m_main_clusterset.m_to_remove.empty()) return;
    1011         [ #  # ]:           0 :     if (!m_main_clusterset.m_removed.empty()) return;
    1012         [ #  # ]:           0 :     if (m_staging_clusterset.has_value()) {
    1013         [ #  # ]:           0 :         if (!m_staging_clusterset->m_deps_to_add.empty()) return;
    1014         [ #  # ]:           0 :         if (!m_staging_clusterset->m_to_remove.empty()) return;
    1015         [ #  # ]:           0 :         if (!m_staging_clusterset->m_removed.empty()) return;
    1016                 :             :     }
    1017                 :             : 
    1018                 :             :     // Sort the GraphIndexes that need to be cleaned up. They are sorted in reverse, so the last
    1019                 :             :     // ones get processed first. This means earlier-processed GraphIndexes will not cause moving of
    1020                 :             :     // later-processed ones during the "swap with end of m_entries" step below (which might
    1021                 :             :     // invalidate them).
    1022                 :           0 :     std::sort(m_unlinked.begin(), m_unlinked.end(), std::greater{});
    1023                 :             : 
    1024                 :           0 :     auto last = GraphIndex(-1);
    1025         [ #  # ]:           0 :     for (GraphIndex idx : m_unlinked) {
    1026                 :             :         // m_unlinked should never contain the same GraphIndex twice (the code below would fail
    1027                 :             :         // if so, because GraphIndexes get invalidated by removing them).
    1028                 :           0 :         Assume(idx != last);
    1029                 :           0 :         last = idx;
    1030                 :             : 
    1031                 :             :         // Make sure the entry is unlinked.
    1032                 :           0 :         Entry& entry = m_entries[idx];
    1033                 :           0 :         Assume(entry.m_ref == nullptr);
    1034                 :             :         // Make sure the entry does not occur in the graph.
    1035         [ #  # ]:           0 :         for (int level = 0; level < MAX_LEVELS; ++level) {
    1036                 :           0 :             Assume(!entry.m_locator[level].IsPresent());
    1037                 :             :         }
    1038                 :             : 
    1039                 :             :         // Move the entry to the end.
    1040         [ #  # ]:           0 :         if (idx != m_entries.size() - 1) SwapIndexes(idx, m_entries.size() - 1);
    1041                 :             :         // Drop the entry for idx, now that it is at the end.
    1042                 :           0 :         m_entries.pop_back();
    1043                 :             :     }
    1044         [ #  # ]:           0 :     m_unlinked.clear();
    1045                 :             : }
    1046                 :             : 
    1047                 :           0 : void TxGraphImpl::Split(Cluster& cluster) noexcept
    1048                 :             : {
    1049                 :             :     // To split a Cluster, first make sure all removals are applied (as we might need to split
    1050                 :             :     // again afterwards otherwise).
    1051                 :           0 :     ApplyRemovals(cluster.m_level);
    1052                 :           0 :     bool del = cluster.Split(*this);
    1053         [ #  # ]:           0 :     if (del) {
    1054                 :             :         // Cluster::Split reports whether the Cluster is to be deleted.
    1055                 :           0 :         DeleteCluster(cluster);
    1056                 :             :     }
    1057                 :           0 : }
    1058                 :             : 
    1059                 :           0 : void TxGraphImpl::SplitAll(int up_to_level) noexcept
    1060                 :             : {
    1061         [ #  # ]:           0 :     Assume(up_to_level >= 0 && up_to_level <= GetTopLevel());
    1062                 :             :     // Before splitting all Cluster, first make sure all removals are applied.
    1063                 :           0 :     ApplyRemovals(up_to_level);
    1064         [ #  # ]:           0 :     for (int level = 0; level <= up_to_level; ++level) {
    1065         [ #  # ]:           0 :         for (auto quality : {QualityLevel::NEEDS_SPLIT, QualityLevel::NEEDS_SPLIT_ACCEPTABLE}) {
    1066                 :           0 :             auto& queue = GetClusterSet(level).m_clusters[int(quality)];
    1067         [ #  # ]:           0 :             while (!queue.empty()) {
    1068                 :           0 :                 Split(*queue.back().get());
    1069                 :             :             }
    1070                 :             :         }
    1071                 :             :     }
    1072                 :           0 : }
    1073                 :             : 
    1074                 :           0 : void TxGraphImpl::GroupClusters(int level) noexcept
    1075                 :             : {
    1076                 :           0 :     auto& clusterset = GetClusterSet(level);
    1077                 :             :     // If the groupings have been computed already, nothing is left to be done.
    1078         [ #  # ]:           0 :     if (clusterset.m_group_data.has_value()) return;
    1079                 :             : 
    1080                 :             :     // Before computing which Clusters need to be merged together, first apply all removals and
    1081                 :             :     // split the Clusters into connected components. If we would group first, we might end up
    1082                 :             :     // with inefficient and/or oversized Clusters which just end up being split again anyway.
    1083                 :           0 :     SplitAll(level);
    1084                 :             : 
    1085                 :             :     /** Annotated clusters: an entry for each Cluster, together with the representative for the
    1086                 :             :      *  partition it is in if known, or with nullptr if not yet known. */
    1087                 :           0 :     std::vector<std::pair<Cluster*, Cluster*>> an_clusters;
    1088                 :             :     /** Annotated dependencies: an entry for each m_deps_to_add entry (excluding ones that apply
    1089                 :             :      *  to removed transactions), together with the representative root of the partition of
    1090                 :             :      *  Clusters it applies to. */
    1091                 :           0 :     std::vector<std::pair<std::pair<GraphIndex, GraphIndex>, Cluster*>> an_deps;
    1092                 :             : 
    1093                 :             :     // Construct a an_clusters entry for every parent and child in the to-be-applied dependencies.
    1094   [ #  #  #  # ]:           0 :     for (const auto& [par, chl] : clusterset.m_deps_to_add) {
    1095                 :           0 :         auto par_cluster = FindCluster(par, level);
    1096                 :           0 :         auto chl_cluster = FindCluster(chl, level);
    1097                 :             :         // Skip dependencies for which the parent or child transaction is removed.
    1098   [ #  #  #  # ]:           0 :         if (par_cluster == nullptr || chl_cluster == nullptr) continue;
    1099                 :           0 :         an_clusters.emplace_back(par_cluster, nullptr);
    1100                 :             :         // Do not include a duplicate when parent and child are identical, as it'll be removed
    1101                 :             :         // below anyway.
    1102         [ #  # ]:           0 :         if (chl_cluster != par_cluster) an_clusters.emplace_back(chl_cluster, nullptr);
    1103                 :             :     }
    1104                 :             :     // Sort and deduplicate an_clusters, so we end up with a sorted list of all involved Clusters
    1105                 :             :     // to which dependencies apply.
    1106                 :           0 :     std::sort(an_clusters.begin(), an_clusters.end());
    1107                 :           0 :     an_clusters.erase(std::unique(an_clusters.begin(), an_clusters.end()), an_clusters.end());
    1108                 :             : 
    1109                 :             :     // Sort the dependencies by child Cluster.
    1110                 :           0 :     std::sort(clusterset.m_deps_to_add.begin(), clusterset.m_deps_to_add.end(), [&](auto& a, auto& b) noexcept {
    1111                 :           0 :         auto [_a_par, a_chl] = a;
    1112                 :           0 :         auto [_b_par, b_chl] = b;
    1113                 :           0 :         auto a_chl_cluster = FindCluster(a_chl, level);
    1114                 :           0 :         auto b_chl_cluster = FindCluster(b_chl, level);
    1115                 :           0 :         return std::less{}(a_chl_cluster, b_chl_cluster);
    1116                 :             :     });
    1117                 :             : 
    1118                 :             :     // Run the union-find algorithm to to find partitions of the input Clusters which need to be
    1119                 :             :     // grouped together. See https://en.wikipedia.org/wiki/Disjoint-set_data_structure.
    1120                 :           0 :     {
    1121                 :             :         /** Each PartitionData entry contains information about a single input Cluster. */
    1122                 :           0 :         struct PartitionData
    1123                 :             :         {
    1124                 :             :             /** The cluster this holds information for. */
    1125                 :             :             Cluster* cluster;
    1126                 :             :             /** All PartitionData entries belonging to the same partition are organized in a tree.
    1127                 :             :              *  Each element points to its parent, or to itself if it is the root. The root is then
    1128                 :             :              *  a representative for the entire tree, and can be found by walking upwards from any
    1129                 :             :              *  element. */
    1130                 :             :             PartitionData* parent;
    1131                 :             :             /** (only if this is a root, so when parent == this) An upper bound on the height of
    1132                 :             :              *  tree for this partition. */
    1133                 :             :             unsigned rank;
    1134                 :             :         };
    1135                 :             :         /** Information about each input Cluster. Sorted by Cluster* pointer. */
    1136                 :           0 :         std::vector<PartitionData> partition_data;
    1137                 :             : 
    1138                 :             :         /** Given a Cluster, find its corresponding PartitionData. */
    1139                 :           0 :         auto locate_fn = [&](Cluster* arg) noexcept -> PartitionData* {
    1140                 :           0 :             auto it = std::lower_bound(partition_data.begin(), partition_data.end(), arg,
    1141         [ #  # ]:           0 :                                        [](auto& a, Cluster* ptr) noexcept { return a.cluster < ptr; });
    1142                 :           0 :             Assume(it != partition_data.end());
    1143                 :           0 :             Assume(it->cluster == arg);
    1144                 :           0 :             return &*it;
    1145                 :           0 :         };
    1146                 :             : 
    1147                 :             :         /** Given a PartitionData, find the root of the tree it is in (its representative). */
    1148                 :           0 :         static constexpr auto find_root_fn = [](PartitionData* data) noexcept -> PartitionData* {
    1149   [ #  #  #  # ]:           0 :             while (data->parent != data) {
    1150                 :             :                 // Replace pointers to parents with pointers to grandparents.
    1151                 :             :                 // See https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Finding_set_representatives.
    1152                 :           0 :                 auto par = data->parent;
    1153                 :           0 :                 data->parent = par->parent;
    1154                 :           0 :                 data = par;
    1155                 :             :             }
    1156                 :           0 :             return data;
    1157                 :             :         };
    1158                 :             : 
    1159                 :             :         /** Given two PartitionDatas, union the partitions they are in, and return their
    1160                 :             :          *  representative. */
    1161                 :           0 :         static constexpr auto union_fn = [](PartitionData* arg1, PartitionData* arg2) noexcept {
    1162                 :             :             // Find the roots of the trees, and bail out if they are already equal (which would
    1163                 :             :             // mean they are in the same partition already).
    1164         [ #  # ]:           0 :             auto rep1 = find_root_fn(arg1);
    1165                 :           0 :             auto rep2 = find_root_fn(arg2);
    1166         [ #  # ]:           0 :             if (rep1 == rep2) return rep1;
    1167                 :             :             // Pick the lower-rank root to become a child of the higher-rank one.
    1168                 :             :             // See https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Union_by_rank.
    1169         [ #  # ]:           0 :             if (rep1->rank < rep2->rank) std::swap(rep1, rep2);
    1170                 :           0 :             rep2->parent = rep1;
    1171                 :           0 :             rep1->rank += (rep1->rank == rep2->rank);
    1172                 :           0 :             return rep1;
    1173                 :             :         };
    1174                 :             : 
    1175                 :             :         // Start by initializing every Cluster as its own singleton partition.
    1176                 :           0 :         partition_data.resize(an_clusters.size());
    1177         [ #  # ]:           0 :         for (size_t i = 0; i < an_clusters.size(); ++i) {
    1178                 :           0 :             partition_data[i].cluster = an_clusters[i].first;
    1179                 :           0 :             partition_data[i].parent = &partition_data[i];
    1180                 :           0 :             partition_data[i].rank = 0;
    1181                 :             :         }
    1182                 :             : 
    1183                 :             :         // Run through all parent/child pairs in m_deps_to_add, and union the
    1184                 :             :         // the partitions their Clusters are in.
    1185                 :           0 :         Cluster* last_chl_cluster{nullptr};
    1186                 :           0 :         PartitionData* last_partition{nullptr};
    1187   [ #  #  #  # ]:           0 :         for (const auto& [par, chl] : clusterset.m_deps_to_add) {
    1188                 :           0 :             auto par_cluster = FindCluster(par, level);
    1189                 :           0 :             auto chl_cluster = FindCluster(chl, level);
    1190                 :             :             // Nothing to do if parent and child are in the same Cluster.
    1191         [ #  # ]:           0 :             if (par_cluster == chl_cluster) continue;
    1192                 :             :             // Nothing to do if either parent or child transaction is removed already.
    1193         [ #  # ]:           0 :             if (par_cluster == nullptr || chl_cluster == nullptr) continue;
    1194                 :           0 :             Assume(par != chl);
    1195         [ #  # ]:           0 :             if (chl_cluster == last_chl_cluster) {
    1196                 :             :                 // If the child Clusters is the same as the previous iteration, union with the
    1197                 :             :                 // tree they were in, avoiding the need for another lookup. Note that m_deps_to_add
    1198                 :             :                 // is sorted by child Cluster, so batches with the same child are expected.
    1199                 :           0 :                 last_partition = union_fn(locate_fn(par_cluster), last_partition);
    1200                 :             :             } else {
    1201                 :           0 :                 last_chl_cluster = chl_cluster;
    1202                 :           0 :                 last_partition = union_fn(locate_fn(par_cluster), locate_fn(chl_cluster));
    1203                 :             :             }
    1204                 :             :         }
    1205                 :             : 
    1206                 :             :         // Populate the an_clusters and an_deps data structures with the list of input Clusters,
    1207                 :             :         // and the input dependencies, annotated with the representative of the Cluster partition
    1208                 :             :         // it applies to.
    1209                 :           0 :         an_deps.reserve(clusterset.m_deps_to_add.size());
    1210                 :           0 :         auto deps_it = clusterset.m_deps_to_add.begin();
    1211         [ #  # ]:           0 :         for (size_t i = 0; i < partition_data.size(); ++i) {
    1212                 :           0 :             auto& data = partition_data[i];
    1213                 :             :             // Find the representative of the partition Cluster i is in, and store it with the
    1214                 :             :             // Cluster.
    1215                 :           0 :             auto rep = find_root_fn(&data)->cluster;
    1216                 :           0 :             Assume(an_clusters[i].second == nullptr);
    1217                 :           0 :             an_clusters[i].second = rep;
    1218                 :             :             // Find all dependencies whose child Cluster is Cluster i, and annotate them with rep.
    1219         [ #  # ]:           0 :             while (deps_it != clusterset.m_deps_to_add.end()) {
    1220         [ #  # ]:           0 :                 auto [par, chl] = *deps_it;
    1221                 :           0 :                 auto chl_cluster = FindCluster(chl, level);
    1222         [ #  # ]:           0 :                 if (std::greater{}(chl_cluster, data.cluster)) break;
    1223                 :             :                 // Skip dependencies that apply to earlier Clusters (those necessary are for
    1224                 :             :                 // deleted transactions, as otherwise we'd have processed them already).
    1225         [ #  # ]:           0 :                 if (chl_cluster == data.cluster) {
    1226                 :           0 :                     auto par_cluster = FindCluster(par, level);
    1227                 :             :                     // Also filter out dependencies applying to a removed parent.
    1228         [ #  # ]:           0 :                     if (par_cluster != nullptr) an_deps.emplace_back(*deps_it, rep);
    1229                 :             :                 }
    1230                 :           0 :                 ++deps_it;
    1231                 :             :             }
    1232                 :             :         }
    1233                 :           0 :     }
    1234                 :             : 
    1235                 :             :     // Sort both an_clusters and an_deps by representative of the partition they are in, grouping
    1236                 :             :     // all those applying to the same partition together.
    1237   [ #  #  #  #  :           0 :     std::sort(an_deps.begin(), an_deps.end(), [](auto& a, auto& b) noexcept { return a.second < b.second; });
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1238   [ #  #  #  #  :           0 :     std::sort(an_clusters.begin(), an_clusters.end(), [](auto& a, auto& b) noexcept { return a.second < b.second; });
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1239                 :             : 
    1240                 :             :     // Translate the resulting cluster groups to the m_group_data structure, and the dependencies
    1241                 :             :     // back to m_deps_to_add.
    1242                 :           0 :     clusterset.m_group_data = GroupData{};
    1243                 :           0 :     clusterset.m_group_data->m_group_clusters.reserve(an_clusters.size());
    1244         [ #  # ]:           0 :     clusterset.m_group_data->m_group_oversized = false;
    1245         [ #  # ]:           0 :     clusterset.m_deps_to_add.clear();
    1246                 :           0 :     clusterset.m_deps_to_add.reserve(an_deps.size());
    1247                 :           0 :     auto an_deps_it = an_deps.begin();
    1248                 :           0 :     auto an_clusters_it = an_clusters.begin();
    1249         [ #  # ]:           0 :     while (an_clusters_it != an_clusters.end()) {
    1250                 :             :         // Process all clusters/dependencies belonging to the partition with representative rep.
    1251                 :           0 :         auto rep = an_clusters_it->second;
    1252                 :             :         // Create and initialize a new GroupData entry for the partition.
    1253                 :           0 :         auto& new_entry = clusterset.m_group_data->m_groups.emplace_back();
    1254                 :           0 :         new_entry.m_cluster_offset = clusterset.m_group_data->m_group_clusters.size();
    1255                 :           0 :         new_entry.m_cluster_count = 0;
    1256                 :           0 :         new_entry.m_deps_offset = clusterset.m_deps_to_add.size();
    1257                 :           0 :         new_entry.m_deps_count = 0;
    1258                 :           0 :         uint32_t total_count{0};
    1259                 :             :         // Add all its clusters to it (copying those from an_clusters to m_group_clusters).
    1260   [ #  #  #  # ]:           0 :         while (an_clusters_it != an_clusters.end() && an_clusters_it->second == rep) {
    1261                 :           0 :             clusterset.m_group_data->m_group_clusters.push_back(an_clusters_it->first);
    1262                 :           0 :             total_count += an_clusters_it->first->GetTxCount();
    1263                 :           0 :             ++an_clusters_it;
    1264                 :           0 :             ++new_entry.m_cluster_count;
    1265                 :             :         }
    1266                 :             :         // Add all its dependencies to it (copying those back from an_deps to m_deps_to_add).
    1267   [ #  #  #  # ]:           0 :         while (an_deps_it != an_deps.end() && an_deps_it->second == rep) {
    1268                 :           0 :             clusterset.m_deps_to_add.push_back(an_deps_it->first);
    1269                 :           0 :             ++an_deps_it;
    1270                 :           0 :             ++new_entry.m_deps_count;
    1271                 :             :         }
    1272                 :             :         // Detect oversizedness.
    1273         [ #  # ]:           0 :         if (total_count > m_max_cluster_count) {
    1274                 :           0 :             clusterset.m_group_data->m_group_oversized = true;
    1275                 :             :         }
    1276                 :             :     }
    1277                 :           0 :     Assume(an_deps_it == an_deps.end());
    1278                 :           0 :     Assume(an_clusters_it == an_clusters.end());
    1279                 :           0 :     clusterset.m_oversized = clusterset.m_group_data->m_group_oversized;
    1280                 :           0 :     Compact();
    1281                 :           0 : }
    1282                 :             : 
    1283                 :           0 : void TxGraphImpl::Merge(std::span<Cluster*> to_merge) noexcept
    1284                 :             : {
    1285         [ #  # ]:           0 :     Assume(!to_merge.empty());
    1286                 :             :     // Nothing to do if a group consists of just a single Cluster.
    1287         [ #  # ]:           0 :     if (to_merge.size() == 1) return;
    1288                 :             : 
    1289                 :             :     // Move the largest Cluster to the front of to_merge. As all transactions in other to-be-merged
    1290                 :             :     // Clusters will be moved to that one, putting the largest one first minimizes the number of
    1291                 :             :     // moves.
    1292                 :           0 :     size_t max_size_pos{0};
    1293                 :           0 :     DepGraphIndex max_size = to_merge[max_size_pos]->GetTxCount();
    1294         [ #  # ]:           0 :     for (size_t i = 1; i < to_merge.size(); ++i) {
    1295         [ #  # ]:           0 :         DepGraphIndex size = to_merge[i]->GetTxCount();
    1296         [ #  # ]:           0 :         if (size > max_size) {
    1297                 :           0 :             max_size_pos = i;
    1298                 :           0 :             max_size = size;
    1299                 :             :         }
    1300                 :             :     }
    1301         [ #  # ]:           0 :     if (max_size_pos != 0) std::swap(to_merge[0], to_merge[max_size_pos]);
    1302                 :             : 
    1303                 :             :     // Merge all further Clusters in the group into the first one, and delete them.
    1304         [ #  # ]:           0 :     for (size_t i = 1; i < to_merge.size(); ++i) {
    1305                 :           0 :         to_merge[0]->Merge(*this, *to_merge[i]);
    1306                 :           0 :         DeleteCluster(*to_merge[i]);
    1307                 :             :     }
    1308                 :             : }
    1309                 :             : 
    1310                 :           0 : void TxGraphImpl::ApplyDependencies(int level) noexcept
    1311                 :             : {
    1312                 :           0 :     auto& clusterset = GetClusterSet(level);
    1313                 :             :     // Do not bother computing groups if we already know the result will be oversized.
    1314         [ #  # ]:           0 :     if (clusterset.m_oversized == true) return;
    1315                 :             :     // Compute the groups of to-be-merged Clusters (which also applies all removals, and splits).
    1316                 :           0 :     GroupClusters(level);
    1317         [ #  # ]:           0 :     Assume(clusterset.m_group_data.has_value());
    1318                 :             :     // Nothing to do if there are no dependencies to be added.
    1319         [ #  # ]:           0 :     if (clusterset.m_deps_to_add.empty()) return;
    1320                 :             :     // Dependencies cannot be applied if it would result in oversized clusters.
    1321         [ #  # ]:           0 :     if (clusterset.m_group_data->m_group_oversized) return;
    1322                 :             : 
    1323                 :             :     // For each group of to-be-merged Clusters.
    1324         [ #  # ]:           0 :     for (const auto& group_entry : clusterset.m_group_data->m_groups) {
    1325         [ #  # ]:           0 :         auto cluster_span = std::span{clusterset.m_group_data->m_group_clusters}
    1326         [ #  # ]:           0 :                                 .subspan(group_entry.m_cluster_offset, group_entry.m_cluster_count);
    1327                 :             :         // Pull in all the Clusters that contain dependencies.
    1328         [ #  # ]:           0 :         if (level == 1) {
    1329         [ #  # ]:           0 :             for (Cluster*& cluster : cluster_span) {
    1330                 :           0 :                 cluster = PullIn(cluster);
    1331                 :             :             }
    1332                 :             :         }
    1333                 :             :         // Invoke Merge() to merge them into a single Cluster.
    1334                 :           0 :         Merge(cluster_span);
    1335                 :             :         // Actually apply all to-be-added dependencies (all parents and children from this grouping
    1336                 :             :         // belong to the same Cluster at this point because of the merging above).
    1337                 :           0 :         auto deps_span = std::span{clusterset.m_deps_to_add}
    1338                 :           0 :                              .subspan(group_entry.m_deps_offset, group_entry.m_deps_count);
    1339                 :           0 :         Assume(!deps_span.empty());
    1340                 :           0 :         const auto& loc = m_entries[deps_span[0].second].m_locator[level];
    1341                 :           0 :         Assume(loc.IsPresent());
    1342                 :           0 :         loc.cluster->ApplyDependencies(*this, deps_span);
    1343                 :             :     }
    1344                 :             : 
    1345                 :             :     // Wipe the list of to-be-added dependencies now that they are applied.
    1346         [ #  # ]:           0 :     clusterset.m_deps_to_add.clear();
    1347                 :           0 :     Compact();
    1348                 :             :     // Also no further Cluster mergings are needed (note that we clear, but don't set to
    1349                 :             :     // std::nullopt, as that would imply the groupings are unknown).
    1350                 :           0 :     clusterset.m_group_data = GroupData{};
    1351                 :             : }
    1352                 :             : 
    1353                 :           0 : void Cluster::Relinearize(TxGraphImpl& graph, uint64_t max_iters) noexcept
    1354                 :             : {
    1355                 :             :     // We can only relinearize Clusters that do not need splitting.
    1356                 :           0 :     Assume(!NeedsSplitting());
    1357                 :             :     // No work is required for Clusters which are already optimally linearized.
    1358         [ #  # ]:           0 :     if (IsOptimal()) return;
    1359                 :             :     // Invoke the actual linearization algorithm (passing in the existing one).
    1360                 :           0 :     uint64_t rng_seed = graph.m_rng.rand64();
    1361         [ #  # ]:           0 :     auto [linearization, optimal] = Linearize(m_depgraph, max_iters, rng_seed, m_linearization);
    1362                 :             :     // Postlinearize if the result isn't optimal already. This guarantees (among other things)
    1363                 :             :     // that the chunks of the resulting linearization are all connected.
    1364         [ #  # ]:           0 :     if (!optimal) PostLinearize(m_depgraph, linearization);
    1365                 :             :     // Update the linearization.
    1366                 :           0 :     m_linearization = std::move(linearization);
    1367                 :             :     // Update the Cluster's quality.
    1368         [ #  # ]:           0 :     auto new_quality = optimal ? QualityLevel::OPTIMAL : QualityLevel::ACCEPTABLE;
    1369                 :           0 :     graph.SetClusterQuality(m_level, m_quality, m_setindex, new_quality);
    1370                 :             :     // Update the Entry objects.
    1371                 :           0 :     Updated(graph);
    1372                 :           0 : }
    1373                 :             : 
    1374                 :           0 : void TxGraphImpl::MakeAcceptable(Cluster& cluster) noexcept
    1375                 :             : {
    1376                 :             :     // Relinearize the Cluster if needed.
    1377         [ #  # ]:           0 :     if (!cluster.NeedsSplitting() && !cluster.IsAcceptable()) {
    1378                 :           0 :         cluster.Relinearize(*this, 10000);
    1379                 :             :     }
    1380                 :           0 : }
    1381                 :             : 
    1382                 :           0 : void TxGraphImpl::MakeAllAcceptable(int level) noexcept
    1383                 :             : {
    1384                 :           0 :     ApplyDependencies(level);
    1385                 :           0 :     auto& clusterset = GetClusterSet(level);
    1386         [ #  # ]:           0 :     if (clusterset.m_oversized == true) return;
    1387                 :           0 :     auto& queue = clusterset.m_clusters[int(QualityLevel::NEEDS_RELINEARIZE)];
    1388         [ #  # ]:           0 :     while (!queue.empty()) {
    1389                 :           0 :         MakeAcceptable(*queue.back().get());
    1390                 :             :     }
    1391                 :             : }
    1392                 :             : 
    1393                 :           0 : Cluster::Cluster(TxGraphImpl& graph, const FeePerWeight& feerate, GraphIndex graph_index) noexcept
    1394                 :             : {
    1395                 :             :     // Create a new transaction in the DepGraph, and remember its position in m_mapping.
    1396                 :           0 :     auto cluster_idx = m_depgraph.AddTransaction(feerate);
    1397                 :           0 :     m_mapping.push_back(graph_index);
    1398                 :           0 :     m_linearization.push_back(cluster_idx);
    1399                 :           0 : }
    1400                 :             : 
    1401                 :           0 : TxGraph::Ref TxGraphImpl::AddTransaction(const FeePerWeight& feerate) noexcept
    1402                 :             : {
    1403                 :             :     // Construct a new Ref.
    1404                 :           0 :     Ref ret;
    1405                 :             :     // Construct a new Entry, and link it with the Ref.
    1406                 :           0 :     auto idx = m_entries.size();
    1407                 :           0 :     m_entries.emplace_back();
    1408                 :           0 :     auto& entry = m_entries.back();
    1409                 :           0 :     entry.m_ref = &ret;
    1410                 :           0 :     GetRefGraph(ret) = this;
    1411                 :           0 :     GetRefIndex(ret) = idx;
    1412                 :             :     // Construct a new singleton Cluster (which is necessarily optimally linearized).
    1413                 :           0 :     auto cluster = std::make_unique<Cluster>(*this, feerate, idx);
    1414                 :           0 :     auto cluster_ptr = cluster.get();
    1415                 :           0 :     int level = GetTopLevel();
    1416                 :           0 :     auto& clusterset = GetClusterSet(level);
    1417                 :           0 :     InsertCluster(level, std::move(cluster), QualityLevel::OPTIMAL);
    1418                 :           0 :     cluster_ptr->Updated(*this);
    1419                 :           0 :     ++clusterset.m_txcount;
    1420                 :             :     // Return the Ref.
    1421                 :           0 :     return ret;
    1422                 :           0 : }
    1423                 :             : 
    1424                 :           0 : void TxGraphImpl::RemoveTransaction(const Ref& arg) noexcept
    1425                 :             : {
    1426                 :             :     // Don't do anything if the Ref is empty (which may be indicative of the transaction already
    1427                 :             :     // having been removed).
    1428         [ #  # ]:           0 :     if (GetRefGraph(arg) == nullptr) return;
    1429                 :           0 :     Assume(GetRefGraph(arg) == this);
    1430                 :             :     // Find the Cluster the transaction is in, and stop if it isn't in any.
    1431         [ #  # ]:           0 :     int level = GetTopLevel();
    1432         [ #  # ]:           0 :     auto cluster = FindCluster(GetRefIndex(arg), level);
    1433         [ #  # ]:           0 :     if (cluster == nullptr) return;
    1434                 :             :     // Remember that the transaction is to be removed.
    1435                 :           0 :     auto& clusterset = GetClusterSet(level);
    1436                 :           0 :     clusterset.m_to_remove.push_back(GetRefIndex(arg));
    1437                 :             :     // Wipe m_group_data (as it will need to be recomputed).
    1438         [ #  # ]:           0 :     clusterset.m_group_data.reset();
    1439         [ #  # ]:           0 :     if (clusterset.m_oversized == true) clusterset.m_oversized = std::nullopt;
    1440                 :             : }
    1441                 :             : 
    1442                 :           0 : void TxGraphImpl::AddDependency(const Ref& parent, const Ref& child) noexcept
    1443                 :             : {
    1444                 :             :     // Don't do anything if either Ref is empty (which may be indicative of it having already been
    1445                 :             :     // removed).
    1446   [ #  #  #  # ]:           0 :     if (GetRefGraph(parent) == nullptr || GetRefGraph(child) == nullptr) return;
    1447                 :           0 :     Assume(GetRefGraph(parent) == this && GetRefGraph(child) == this);
    1448                 :             :     // Don't do anything if this is a dependency on self.
    1449         [ #  # ]:           0 :     if (GetRefIndex(parent) == GetRefIndex(child)) return;
    1450                 :             :     // Find the Cluster the parent and child transaction are in, and stop if either appears to be
    1451                 :             :     // already removed.
    1452         [ #  # ]:           0 :     int level = GetTopLevel();
    1453                 :           0 :     auto par_cluster = FindCluster(GetRefIndex(parent), level);
    1454         [ #  # ]:           0 :     if (par_cluster == nullptr) return;
    1455                 :           0 :     auto chl_cluster = FindCluster(GetRefIndex(child), level);
    1456         [ #  # ]:           0 :     if (chl_cluster == nullptr) return;
    1457                 :             :     // Remember that this dependency is to be applied.
    1458                 :           0 :     auto& clusterset = GetClusterSet(level);
    1459                 :           0 :     clusterset.m_deps_to_add.emplace_back(GetRefIndex(parent), GetRefIndex(child));
    1460                 :             :     // Wipe m_group_data (as it will need to be recomputed).
    1461         [ #  # ]:           0 :     clusterset.m_group_data.reset();
    1462         [ #  # ]:           0 :     if (clusterset.m_oversized == false) clusterset.m_oversized = std::nullopt;
    1463                 :             : }
    1464                 :             : 
    1465                 :           0 : bool TxGraphImpl::Exists(const Ref& arg, bool main_only) noexcept
    1466                 :             : {
    1467         [ #  # ]:           0 :     if (GetRefGraph(arg) == nullptr) return false;
    1468                 :           0 :     Assume(GetRefGraph(arg) == this);
    1469         [ #  # ]:           0 :     size_t level = GetSpecifiedLevel(main_only);
    1470                 :             :     // Make sure the transaction isn't scheduled for removal.
    1471                 :           0 :     ApplyRemovals(level);
    1472                 :           0 :     auto cluster = FindCluster(GetRefIndex(arg), level);
    1473                 :           0 :     return cluster != nullptr;
    1474                 :             : }
    1475                 :             : 
    1476                 :           0 : void Cluster::GetAncestorRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept
    1477                 :             : {
    1478                 :             :     /** The union of all ancestors to be returned. */
    1479                 :           0 :     SetType ancestors_union;
    1480                 :             :     // Process elements from the front of args, as long as they apply.
    1481         [ #  # ]:           0 :     while (!args.empty()) {
    1482         [ #  # ]:           0 :         if (args.front().first != this) break;
    1483                 :           0 :         ancestors_union |= m_depgraph.Ancestors(args.front().second);
    1484                 :           0 :         args = args.subspan(1);
    1485                 :             :     }
    1486         [ #  # ]:           0 :     Assume(ancestors_union.Any());
    1487                 :             :     // Translate all ancestors (in arbitrary order) to Refs (if they have any), and return them.
    1488   [ #  #  #  #  :           0 :     for (auto idx : ancestors_union) {
                   #  # ]
    1489                 :           0 :         const auto& entry = graph.m_entries[m_mapping[idx]];
    1490                 :           0 :         Assume(entry.m_ref != nullptr);
    1491                 :           0 :         output.push_back(entry.m_ref);
    1492                 :             :     }
    1493                 :           0 : }
    1494                 :             : 
    1495                 :           0 : void Cluster::GetDescendantRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept
    1496                 :             : {
    1497                 :             :     /** The union of all descendants to be returned. */
    1498                 :           0 :     SetType descendants_union;
    1499                 :             :     // Process elements from the front of args, as long as they apply.
    1500         [ #  # ]:           0 :     while (!args.empty()) {
    1501         [ #  # ]:           0 :         if (args.front().first != this) break;
    1502                 :           0 :         descendants_union |= m_depgraph.Descendants(args.front().second);
    1503                 :           0 :         args = args.subspan(1);
    1504                 :             :     }
    1505         [ #  # ]:           0 :     Assume(descendants_union.Any());
    1506                 :             :     // Translate all descendants (in arbitrary order) to Refs (if they have any), and return them.
    1507   [ #  #  #  #  :           0 :     for (auto idx : descendants_union) {
                   #  # ]
    1508                 :           0 :         const auto& entry = graph.m_entries[m_mapping[idx]];
    1509                 :           0 :         Assume(entry.m_ref != nullptr);
    1510                 :           0 :         output.push_back(entry.m_ref);
    1511                 :             :     }
    1512                 :           0 : }
    1513                 :             : 
    1514                 :           0 : std::vector<TxGraph::Ref*> Cluster::GetClusterRefs(const TxGraphImpl& graph) noexcept
    1515                 :             : {
    1516                 :           0 :     std::vector<TxGraph::Ref*> ret;
    1517                 :           0 :     ret.reserve(m_linearization.size());
    1518                 :             :     // Translate all transactions in the Cluster (in linearization order) to Refs.
    1519         [ #  # ]:           0 :     for (auto idx : m_linearization) {
    1520                 :           0 :         const auto& entry = graph.m_entries[m_mapping[idx]];
    1521                 :           0 :         Assume(entry.m_ref != nullptr);
    1522                 :           0 :         ret.push_back(entry.m_ref);
    1523                 :             :     }
    1524                 :           0 :     return ret;
    1525                 :             : }
    1526                 :             : 
    1527                 :           0 : FeePerWeight Cluster::GetIndividualFeerate(DepGraphIndex idx) noexcept
    1528                 :             : {
    1529                 :           0 :     return FeePerWeight::FromFeeFrac(m_depgraph.FeeRate(idx));
    1530                 :             : }
    1531                 :             : 
    1532                 :           0 : void Cluster::MakeStagingTransactionsMissing(TxGraphImpl& graph) noexcept
    1533                 :             : {
    1534                 :           0 :     Assume(m_level == 1);
    1535                 :             :     // Mark all transactions of a Cluster missing, needed when aborting staging, so that the
    1536                 :             :     // corresponding Locators don't retain references into aborted Clusters.
    1537         [ #  # ]:           0 :     for (auto ci : m_linearization) {
    1538                 :           0 :         GraphIndex idx = m_mapping[ci];
    1539                 :           0 :         auto& entry = graph.m_entries[idx];
    1540                 :           0 :         entry.m_locator[1].SetMissing();
    1541                 :             :     }
    1542                 :           0 : }
    1543                 :             : 
    1544                 :           0 : std::vector<TxGraph::Ref*> TxGraphImpl::GetAncestors(const Ref& arg, bool main_only) noexcept
    1545                 :             : {
    1546                 :             :     // Return the empty vector if the Ref is empty.
    1547         [ #  # ]:           0 :     if (GetRefGraph(arg) == nullptr) return {};
    1548                 :           0 :     Assume(GetRefGraph(arg) == this);
    1549                 :             :     // Apply all removals and dependencies, as the result might be incorrect otherwise.
    1550         [ #  # ]:           0 :     size_t level = GetSpecifiedLevel(main_only);
    1551                 :           0 :     ApplyDependencies(level);
    1552                 :             :     // Ancestry cannot be known if unapplied dependencies remain.
    1553                 :           0 :     Assume(GetClusterSet(level).m_deps_to_add.empty());
    1554                 :             :     // Find the Cluster the argument is in, and return the empty vector if it isn't in any.
    1555         [ #  # ]:           0 :     auto cluster = FindCluster(GetRefIndex(arg), level);
    1556         [ #  # ]:           0 :     if (cluster == nullptr) return {};
    1557                 :             :     // Dispatch to the Cluster.
    1558                 :           0 :     std::pair<Cluster*, DepGraphIndex> match = {cluster, m_entries[GetRefIndex(arg)].m_locator[cluster->m_level].index};
    1559                 :           0 :     auto matches = std::span(&match, 1);
    1560                 :           0 :     std::vector<TxGraph::Ref*> ret;
    1561                 :           0 :     cluster->GetAncestorRefs(*this, matches, ret);
    1562                 :           0 :     return ret;
    1563                 :           0 : }
    1564                 :             : 
    1565                 :           0 : std::vector<TxGraph::Ref*> TxGraphImpl::GetDescendants(const Ref& arg, bool main_only) noexcept
    1566                 :             : {
    1567                 :             :     // Return the empty vector if the Ref is empty.
    1568         [ #  # ]:           0 :     if (GetRefGraph(arg) == nullptr) return {};
    1569                 :           0 :     Assume(GetRefGraph(arg) == this);
    1570                 :             :     // Apply all removals and dependencies, as the result might be incorrect otherwise.
    1571         [ #  # ]:           0 :     size_t level = GetSpecifiedLevel(main_only);
    1572                 :           0 :     ApplyDependencies(level);
    1573                 :             :     // Ancestry cannot be known if unapplied dependencies remain.
    1574                 :           0 :     Assume(GetClusterSet(level).m_deps_to_add.empty());
    1575                 :             :     // Find the Cluster the argument is in, and return the empty vector if it isn't in any.
    1576         [ #  # ]:           0 :     auto cluster = FindCluster(GetRefIndex(arg), level);
    1577         [ #  # ]:           0 :     if (cluster == nullptr) return {};
    1578                 :             :     // Dispatch to the Cluster.
    1579                 :           0 :     std::pair<Cluster*, DepGraphIndex> match = {cluster, m_entries[GetRefIndex(arg)].m_locator[cluster->m_level].index};
    1580                 :           0 :     auto matches = std::span(&match, 1);
    1581                 :           0 :     std::vector<TxGraph::Ref*> ret;
    1582                 :           0 :     cluster->GetDescendantRefs(*this, matches, ret);
    1583                 :           0 :     return ret;
    1584                 :           0 : }
    1585                 :             : 
    1586                 :           0 : std::vector<TxGraph::Ref*> TxGraphImpl::GetAncestorsUnion(std::span<const Ref* const> args, bool main_only) noexcept
    1587                 :             : {
    1588                 :             :     // Apply all dependencies, as the result might be incorrect otherwise.
    1589         [ #  # ]:           0 :     size_t level = GetSpecifiedLevel(main_only);
    1590                 :           0 :     ApplyDependencies(level);
    1591                 :             :     // Ancestry cannot be known if unapplied dependencies remain.
    1592                 :           0 :     Assume(GetClusterSet(level).m_deps_to_add.empty());
    1593                 :             : 
    1594                 :             :     // Translate args to matches.
    1595                 :           0 :     std::vector<std::pair<Cluster*, DepGraphIndex>> matches;
    1596                 :           0 :     matches.reserve(args.size());
    1597         [ #  # ]:           0 :     for (auto arg : args) {
    1598                 :             :         // Skip empty Refs.
    1599         [ #  # ]:           0 :         if (GetRefGraph(*arg) == nullptr) continue;
    1600                 :           0 :         Assume(GetRefGraph(*arg) == this);
    1601                 :             :         // Find the Cluster the argument is in, and skip if none is found.
    1602         [ #  # ]:           0 :         auto cluster = FindCluster(GetRefIndex(*arg), level);
    1603         [ #  # ]:           0 :         if (cluster == nullptr) continue;
    1604                 :             :         // Append to matches.
    1605                 :           0 :         matches.emplace_back(cluster, m_entries[GetRefIndex(*arg)].m_locator[cluster->m_level].index);
    1606                 :             :     }
    1607                 :             :     // Group by Cluster.
    1608   [ #  #  #  #  :           0 :     std::sort(matches.begin(), matches.end(), [](auto& a, auto& b) noexcept { return std::less{}(a.first, b.first); });
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1609                 :             :     // Dispatch to the Clusters.
    1610                 :           0 :     std::span match_span(matches);
    1611                 :           0 :     std::vector<TxGraph::Ref*> ret;
    1612         [ #  # ]:           0 :     while (!match_span.empty()) {
    1613                 :           0 :         match_span.front().first->GetAncestorRefs(*this, match_span, ret);
    1614                 :             :     }
    1615                 :           0 :     return ret;
    1616                 :           0 : }
    1617                 :             : 
    1618                 :           0 : std::vector<TxGraph::Ref*> TxGraphImpl::GetDescendantsUnion(std::span<const Ref* const> args, bool main_only) noexcept
    1619                 :             : {
    1620                 :             :     // Apply all dependencies, as the result might be incorrect otherwise.
    1621         [ #  # ]:           0 :     size_t level = GetSpecifiedLevel(main_only);
    1622                 :           0 :     ApplyDependencies(level);
    1623                 :             :     // Ancestry cannot be known if unapplied dependencies remain.
    1624                 :           0 :     Assume(GetClusterSet(level).m_deps_to_add.empty());
    1625                 :             : 
    1626                 :             :     // Translate args to matches.
    1627                 :           0 :     std::vector<std::pair<Cluster*, DepGraphIndex>> matches;
    1628                 :           0 :     matches.reserve(args.size());
    1629         [ #  # ]:           0 :     for (auto arg : args) {
    1630                 :             :         // Skip empty Refs.
    1631         [ #  # ]:           0 :         if (GetRefGraph(*arg) == nullptr) continue;
    1632                 :           0 :         Assume(GetRefGraph(*arg) == this);
    1633                 :             :         // Find the Cluster the argument is in, and skip if none is found.
    1634         [ #  # ]:           0 :         auto cluster = FindCluster(GetRefIndex(*arg), level);
    1635         [ #  # ]:           0 :         if (cluster == nullptr) continue;
    1636                 :             :         // Append to matches.
    1637                 :           0 :         matches.emplace_back(cluster, m_entries[GetRefIndex(*arg)].m_locator[cluster->m_level].index);
    1638                 :             :     }
    1639                 :             :     // Group by Cluster.
    1640   [ #  #  #  #  :           0 :     std::sort(matches.begin(), matches.end(), [](auto& a, auto& b) noexcept { return std::less{}(a.first, b.first); });
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1641                 :             :     // Dispatch to the Clusters.
    1642                 :           0 :     std::span match_span(matches);
    1643                 :           0 :     std::vector<TxGraph::Ref*> ret;
    1644         [ #  # ]:           0 :     while (!match_span.empty()) {
    1645                 :           0 :         match_span.front().first->GetDescendantRefs(*this, match_span, ret);
    1646                 :             :     }
    1647                 :           0 :     return ret;
    1648                 :           0 : }
    1649                 :             : 
    1650                 :           0 : std::vector<TxGraph::Ref*> TxGraphImpl::GetCluster(const Ref& arg, bool main_only) noexcept
    1651                 :             : {
    1652                 :             :     // Return the empty vector if the Ref is empty (which may be indicative of the transaction
    1653                 :             :     // having been removed already.
    1654         [ #  # ]:           0 :     if (GetRefGraph(arg) == nullptr) return {};
    1655                 :           0 :     Assume(GetRefGraph(arg) == this);
    1656                 :             :     // Apply all removals and dependencies, as the result might be incorrect otherwise.
    1657         [ #  # ]:           0 :     size_t level = GetSpecifiedLevel(main_only);
    1658                 :           0 :     ApplyDependencies(level);
    1659                 :             :     // Cluster linearization cannot be known if unapplied dependencies remain.
    1660                 :           0 :     Assume(GetClusterSet(level).m_deps_to_add.empty());
    1661                 :             :     // Find the Cluster the argument is in, and return the empty vector if it isn't in any.
    1662         [ #  # ]:           0 :     auto cluster = FindCluster(GetRefIndex(arg), level);
    1663         [ #  # ]:           0 :     if (cluster == nullptr) return {};
    1664                 :             :     // Make sure the Cluster has an acceptable quality level, and then dispatch to it.
    1665                 :           0 :     MakeAcceptable(*cluster);
    1666                 :           0 :     return cluster->GetClusterRefs(*this);
    1667                 :             : }
    1668                 :             : 
    1669                 :           0 : TxGraph::GraphIndex TxGraphImpl::GetTransactionCount(bool main_only) noexcept
    1670                 :             : {
    1671         [ #  # ]:           0 :     size_t level = GetSpecifiedLevel(main_only);
    1672                 :           0 :     ApplyRemovals(level);
    1673                 :           0 :     return GetClusterSet(level).m_txcount;
    1674                 :             : }
    1675                 :             : 
    1676                 :           0 : FeePerWeight TxGraphImpl::GetIndividualFeerate(const Ref& arg) noexcept
    1677                 :             : {
    1678                 :             :     // Return the empty FeePerWeight if the passed Ref is empty.
    1679         [ #  # ]:           0 :     if (GetRefGraph(arg) == nullptr) return {};
    1680                 :             :     Assume(GetRefGraph(arg) == this);
    1681                 :             :     // Find the cluster the argument is in (the level does not matter as individual feerates will
    1682                 :             :     // be identical if it occurs in both), and return the empty FeePerWeight if it isn't in any.
    1683                 :           0 :     Cluster* cluster{nullptr};
    1684         [ #  # ]:           0 :     for (int level = 0; level <= GetTopLevel(); ++level) {
    1685                 :             :         // Apply removals, so that we can correctly report FeePerWeight{} for non-existing
    1686                 :             :         // transactions.
    1687                 :           0 :         ApplyRemovals(level);
    1688         [ #  # ]:           0 :         if (m_entries[GetRefIndex(arg)].m_locator[level].IsPresent()) {
    1689                 :             :             cluster = m_entries[GetRefIndex(arg)].m_locator[level].cluster;
    1690                 :             :             break;
    1691                 :             :         }
    1692                 :             :     }
    1693         [ #  # ]:           0 :     if (cluster == nullptr) return {};
    1694                 :             :     // Dispatch to the Cluster.
    1695                 :           0 :     return cluster->GetIndividualFeerate(m_entries[GetRefIndex(arg)].m_locator[cluster->m_level].index);
    1696                 :             : }
    1697                 :             : 
    1698                 :           0 : FeePerWeight TxGraphImpl::GetMainChunkFeerate(const Ref& arg) noexcept
    1699                 :             : {
    1700                 :             :     // Return the empty FeePerWeight if the passed Ref is empty.
    1701         [ #  # ]:           0 :     if (GetRefGraph(arg) == nullptr) return {};
    1702                 :           0 :     Assume(GetRefGraph(arg) == this);
    1703                 :             :     // Apply all removals and dependencies, as the result might be inaccurate otherwise.
    1704                 :           0 :     ApplyDependencies(/*level=*/0);
    1705                 :             :     // Chunk feerates cannot be accurately known if unapplied dependencies remain.
    1706                 :           0 :     Assume(m_main_clusterset.m_deps_to_add.empty());
    1707                 :             :     // Find the cluster the argument is in, and return the empty FeePerWeight if it isn't in any.
    1708         [ #  # ]:           0 :     auto cluster = FindCluster(GetRefIndex(arg), 0);
    1709         [ #  # ]:           0 :     if (cluster == nullptr) return {};
    1710                 :             :     // Make sure the Cluster has an acceptable quality level, and then return the transaction's
    1711                 :             :     // chunk feerate.
    1712                 :           0 :     MakeAcceptable(*cluster);
    1713                 :           0 :     const auto& entry = m_entries[GetRefIndex(arg)];
    1714                 :           0 :     return entry.m_main_chunk_feerate;
    1715                 :             : }
    1716                 :             : 
    1717                 :           0 : bool TxGraphImpl::IsOversized(bool main_only) noexcept
    1718                 :             : {
    1719         [ #  # ]:           0 :     size_t level = GetSpecifiedLevel(main_only);
    1720                 :           0 :     auto& clusterset = GetClusterSet(level);
    1721         [ #  # ]:           0 :     if (clusterset.m_oversized.has_value()) {
    1722                 :             :         // Return cached value if known.
    1723                 :           0 :         return *clusterset.m_oversized;
    1724                 :             :     }
    1725                 :             :     // Find which Clusters will need to be merged together, as that is where the oversize
    1726                 :             :     // property is assessed.
    1727                 :           0 :     GroupClusters(level);
    1728                 :           0 :     Assume(clusterset.m_group_data.has_value());
    1729                 :           0 :     clusterset.m_oversized = clusterset.m_group_data->m_group_oversized;
    1730                 :           0 :     return *clusterset.m_oversized;
    1731                 :             : }
    1732                 :             : 
    1733                 :           0 : void TxGraphImpl::StartStaging() noexcept
    1734                 :             : {
    1735                 :             :     // Staging cannot already exist.
    1736                 :           0 :     Assume(!m_staging_clusterset.has_value());
    1737                 :             :     // Apply all remaining dependencies in main before creating a staging graph. Once staging
    1738                 :             :     // exists, we cannot merge Clusters anymore (because of interference with Clusters being
    1739                 :             :     // pulled into staging), so to make sure all inspectors are available (if not oversized), do
    1740                 :             :     // all merging work now. Call SplitAll() first, so that even if ApplyDependencies does not do
    1741                 :             :     // any thing due to knowing the result is oversized, splitting is still performed.
    1742                 :           0 :     SplitAll(0);
    1743                 :           0 :     ApplyDependencies(0);
    1744                 :             :     // Construct the staging ClusterSet.
    1745                 :           0 :     m_staging_clusterset.emplace();
    1746                 :             :     // Copy statistics, precomputed data, and to-be-applied dependencies (only if oversized) to
    1747                 :             :     // the new graph. To-be-applied removals will always be empty at this point.
    1748                 :           0 :     m_staging_clusterset->m_txcount = m_main_clusterset.m_txcount;
    1749                 :           0 :     m_staging_clusterset->m_deps_to_add = m_main_clusterset.m_deps_to_add;
    1750                 :           0 :     m_staging_clusterset->m_group_data = m_main_clusterset.m_group_data;
    1751                 :           0 :     m_staging_clusterset->m_oversized = m_main_clusterset.m_oversized;
    1752                 :           0 :     Assume(m_staging_clusterset->m_oversized.has_value());
    1753                 :           0 : }
    1754                 :             : 
    1755                 :           0 : void TxGraphImpl::AbortStaging() noexcept
    1756                 :             : {
    1757                 :             :     // Staging must exist.
    1758                 :           0 :     Assume(m_staging_clusterset.has_value());
    1759                 :             :     // Mark all removed transactions as Missing (so the staging locator for these transactions
    1760                 :             :     // can be reused if another staging is created).
    1761         [ #  # ]:           0 :     for (auto idx : m_staging_clusterset->m_removed) {
    1762                 :           0 :         m_entries[idx].m_locator[1].SetMissing();
    1763                 :             :     }
    1764                 :             :     // Do the same with the non-removed transactions in staging Clusters.
    1765         [ #  # ]:           0 :     for (int quality = 0; quality < int(QualityLevel::NONE); ++quality) {
    1766         [ #  # ]:           0 :         for (auto& cluster : m_staging_clusterset->m_clusters[quality]) {
    1767                 :           0 :             cluster->MakeStagingTransactionsMissing(*this);
    1768                 :             :         }
    1769                 :             :     }
    1770                 :             :     // Destroy the staging ClusterSet.
    1771                 :           0 :     m_staging_clusterset.reset();
    1772                 :           0 :     Compact();
    1773         [ #  # ]:           0 :     if (!m_main_clusterset.m_group_data.has_value()) {
    1774                 :             :         // In case m_oversized in main was kept after a Ref destruction while staging exists, we
    1775                 :             :         // need to re-evaluate m_oversized now.
    1776         [ #  # ]:           0 :         m_main_clusterset.m_oversized = std::nullopt;
    1777                 :             :     }
    1778                 :           0 : }
    1779                 :             : 
    1780                 :           0 : void TxGraphImpl::CommitStaging() noexcept
    1781                 :             : {
    1782                 :             :     // Staging must exist.
    1783                 :           0 :     Assume(m_staging_clusterset.has_value());
    1784                 :             :     // Delete all conflicting Clusters in main, to make place for moving the staging ones
    1785                 :             :     // there. All of these have been copied to staging in PullIn().
    1786                 :           0 :     auto conflicts = GetConflicts();
    1787         [ #  # ]:           0 :     for (Cluster* conflict : conflicts) {
    1788                 :           0 :         conflict->Clear(*this);
    1789                 :           0 :         DeleteCluster(*conflict);
    1790                 :             :     }
    1791                 :             :     // Mark the removed transactions as Missing (so the staging locator for these transactions
    1792                 :             :     // can be reused if another staging is created).
    1793         [ #  # ]:           0 :     for (auto idx : m_staging_clusterset->m_removed) {
    1794                 :           0 :         m_entries[idx].m_locator[1].SetMissing();
    1795                 :             :     }
    1796                 :             :     // Then move all Clusters in staging to main.
    1797         [ #  # ]:           0 :     for (int quality = 0; quality < int(QualityLevel::NONE); ++quality) {
    1798                 :           0 :         auto& stage_sets = m_staging_clusterset->m_clusters[quality];
    1799         [ #  # ]:           0 :         while (!stage_sets.empty()) {
    1800                 :           0 :             stage_sets.back()->MoveToMain(*this);
    1801                 :             :         }
    1802                 :             :     }
    1803                 :             :     // Move all statistics, precomputed data, and to-be-applied removals and dependencies.
    1804                 :           0 :     m_main_clusterset.m_deps_to_add = std::move(m_staging_clusterset->m_deps_to_add);
    1805                 :           0 :     m_main_clusterset.m_to_remove = std::move(m_staging_clusterset->m_to_remove);
    1806                 :           0 :     m_main_clusterset.m_group_data = std::move(m_staging_clusterset->m_group_data);
    1807                 :           0 :     m_main_clusterset.m_oversized = std::move(m_staging_clusterset->m_oversized);
    1808                 :           0 :     m_main_clusterset.m_txcount = std::move(m_staging_clusterset->m_txcount);
    1809                 :             :     // Delete the old staging graph, after all its information was moved to main.
    1810                 :           0 :     m_staging_clusterset.reset();
    1811                 :           0 :     Compact();
    1812                 :           0 : }
    1813                 :             : 
    1814                 :           0 : void Cluster::SetFee(TxGraphImpl& graph, DepGraphIndex idx, int64_t fee) noexcept
    1815                 :             : {
    1816                 :             :     // Make sure the specified DepGraphIndex exists in this Cluster.
    1817                 :           0 :     Assume(m_depgraph.Positions()[idx]);
    1818                 :             :     // Bail out if the fee isn't actually being changed.
    1819         [ #  # ]:           0 :     if (m_depgraph.FeeRate(idx).fee == fee) return;
    1820                 :             :     // Update the fee, remember that relinearization will be necessary, and update the Entries
    1821                 :             :     // in the same Cluster.
    1822         [ #  # ]:           0 :     m_depgraph.FeeRate(idx).fee = fee;
    1823         [ #  # ]:           0 :     if (!NeedsSplitting()) {
    1824                 :           0 :         graph.SetClusterQuality(m_level, m_quality, m_setindex, QualityLevel::NEEDS_RELINEARIZE);
    1825                 :             :     } else {
    1826                 :           0 :         graph.SetClusterQuality(m_level, m_quality, m_setindex, QualityLevel::NEEDS_SPLIT);
    1827                 :             :     }
    1828                 :           0 :     Updated(graph);
    1829                 :             : }
    1830                 :             : 
    1831                 :           0 : void TxGraphImpl::SetTransactionFee(const Ref& ref, int64_t fee) noexcept
    1832                 :             : {
    1833                 :             :     // Don't do anything if the passed Ref is empty.
    1834         [ #  # ]:           0 :     if (GetRefGraph(ref) == nullptr) return;
    1835                 :           0 :     Assume(GetRefGraph(ref) == this);
    1836                 :             :     // Find the entry, its locator, and inform its Cluster about the new feerate, if any.
    1837                 :           0 :     auto& entry = m_entries[GetRefIndex(ref)];
    1838         [ #  # ]:           0 :     for (int level = 0; level < MAX_LEVELS; ++level) {
    1839                 :           0 :         auto& locator = entry.m_locator[level];
    1840         [ #  # ]:           0 :         if (locator.IsPresent()) {
    1841                 :           0 :             locator.cluster->SetFee(*this, locator.index, fee);
    1842                 :             :         }
    1843                 :             :     }
    1844                 :             : }
    1845                 :             : 
    1846                 :           0 : std::strong_ordering TxGraphImpl::CompareMainOrder(const Ref& a, const Ref& b) noexcept
    1847                 :             : {
    1848                 :             :     // The references must not be empty.
    1849                 :           0 :     Assume(GetRefGraph(a) == this);
    1850                 :           0 :     Assume(GetRefGraph(b) == this);
    1851                 :             :     // Apply dependencies in main.
    1852                 :           0 :     ApplyDependencies(0);
    1853                 :           0 :     Assume(m_main_clusterset.m_deps_to_add.empty());
    1854                 :             :     // Make both involved Clusters acceptable, so chunk feerates are relevant.
    1855                 :           0 :     const auto& entry_a = m_entries[GetRefIndex(a)];
    1856                 :           0 :     const auto& entry_b = m_entries[GetRefIndex(b)];
    1857                 :           0 :     const auto& locator_a = entry_a.m_locator[0];
    1858                 :           0 :     const auto& locator_b = entry_b.m_locator[0];
    1859                 :           0 :     Assume(locator_a.IsPresent());
    1860                 :           0 :     Assume(locator_b.IsPresent());
    1861                 :           0 :     MakeAcceptable(*locator_a.cluster);
    1862                 :           0 :     MakeAcceptable(*locator_b.cluster);
    1863                 :             :     // Compare chunk feerates, and return result if it differs.
    1864                 :           0 :     auto feerate_cmp = FeeRateCompare(entry_b.m_main_chunk_feerate, entry_a.m_main_chunk_feerate);
    1865         [ #  # ]:           0 :     if (feerate_cmp < 0) return std::strong_ordering::less;
    1866         [ #  # ]:           0 :     if (feerate_cmp > 0) return std::strong_ordering::greater;
    1867                 :             :     // Compare Cluster* as tie-break for equal chunk feerates.
    1868   [ #  #  #  # ]:           0 :     if (locator_a.cluster != locator_b.cluster) return locator_a.cluster <=> locator_b.cluster;
    1869                 :             :     // As final tie-break, compare position within cluster linearization.
    1870   [ #  #  #  # ]:           0 :     return entry_a.m_main_lin_index <=> entry_b.m_main_lin_index;
    1871                 :             : }
    1872                 :             : 
    1873                 :           0 : TxGraph::GraphIndex TxGraphImpl::CountDistinctClusters(std::span<const Ref* const> refs, bool main_only) noexcept
    1874                 :             : {
    1875         [ #  # ]:           0 :     size_t level = GetSpecifiedLevel(main_only);
    1876                 :           0 :     ApplyDependencies(level);
    1877                 :           0 :     auto& clusterset = GetClusterSet(level);
    1878                 :           0 :     Assume(clusterset.m_deps_to_add.empty());
    1879                 :             :     // Build a vector of Clusters that the specified Refs occur in.
    1880                 :           0 :     std::vector<Cluster*> clusters;
    1881                 :           0 :     clusters.reserve(refs.size());
    1882         [ #  # ]:           0 :     for (const Ref* ref : refs) {
    1883         [ #  # ]:           0 :         if (ref == nullptr) continue;
    1884         [ #  # ]:           0 :         if (GetRefGraph(*ref) == nullptr) continue;
    1885                 :           0 :         Assume(GetRefGraph(*ref) == this);
    1886         [ #  # ]:           0 :         auto cluster = FindCluster(GetRefIndex(*ref), level);
    1887         [ #  # ]:           0 :         if (cluster != nullptr) clusters.push_back(cluster);
    1888                 :             :     }
    1889                 :             :     // Count the number of distinct elements in clusters.
    1890                 :           0 :     std::sort(clusters.begin(), clusters.end());
    1891                 :           0 :     Cluster* last{nullptr};
    1892                 :           0 :     GraphIndex ret{0};
    1893         [ #  # ]:           0 :     for (Cluster* cluster : clusters) {
    1894                 :           0 :         ret += (cluster != last);
    1895                 :           0 :         last = cluster;
    1896                 :             :     }
    1897                 :           0 :     return ret;
    1898                 :           0 : }
    1899                 :             : 
    1900                 :           0 : void Cluster::SanityCheck(const TxGraphImpl& graph, int level) const
    1901                 :             : {
    1902                 :             :     // There must be an m_mapping for each m_depgraph position (including holes).
    1903         [ #  # ]:           0 :     assert(m_depgraph.PositionRange() == m_mapping.size());
    1904                 :             :     // The linearization for this Cluster must contain every transaction once.
    1905         [ #  # ]:           0 :     assert(m_depgraph.TxCount() == m_linearization.size());
    1906                 :             :     // The number of transactions in a Cluster cannot exceed m_max_cluster_count.
    1907         [ #  # ]:           0 :     assert(m_linearization.size() <= graph.m_max_cluster_count);
    1908                 :             :     // The level must match the level the Cluster occurs in.
    1909         [ #  # ]:           0 :     assert(m_level == level);
    1910                 :             :     // m_quality and m_setindex are checked in TxGraphImpl::SanityCheck.
    1911                 :             : 
    1912                 :             :     // Compute the chunking of m_linearization.
    1913                 :           0 :     LinearizationChunking linchunking(m_depgraph, m_linearization);
    1914                 :             : 
    1915                 :             :     // Verify m_linearization.
    1916                 :           0 :     SetType m_done;
    1917                 :           0 :     LinearizationIndex linindex{0};
    1918         [ #  # ]:           0 :     assert(m_depgraph.IsAcyclic());
    1919         [ #  # ]:           0 :     for (auto lin_pos : m_linearization) {
    1920         [ #  # ]:           0 :         assert(lin_pos < m_mapping.size());
    1921         [ #  # ]:           0 :         const auto& entry = graph.m_entries[m_mapping[lin_pos]];
    1922                 :             :         // Check that the linearization is topological.
    1923         [ #  # ]:           0 :         m_done.Set(lin_pos);
    1924         [ #  # ]:           0 :         assert(m_done.IsSupersetOf(m_depgraph.Ancestors(lin_pos)));
    1925                 :             :         // Check that the Entry has a locator pointing back to this Cluster & position within it.
    1926         [ #  # ]:           0 :         assert(entry.m_locator[level].cluster == this);
    1927         [ #  # ]:           0 :         assert(entry.m_locator[level].index == lin_pos);
    1928                 :             :         // For main-level entries, check linearization position and chunk feerate.
    1929         [ #  # ]:           0 :         if (level == 0 && IsAcceptable()) {
    1930         [ #  # ]:           0 :             assert(entry.m_main_lin_index == linindex);
    1931                 :           0 :             ++linindex;
    1932         [ #  # ]:           0 :             if (!linchunking.GetChunk(0).transactions[lin_pos]) {
    1933                 :           0 :                 linchunking.MarkDone(linchunking.GetChunk(0).transactions);
    1934                 :             :             }
    1935         [ #  # ]:           0 :             assert(entry.m_main_chunk_feerate == linchunking.GetChunk(0).feerate);
    1936                 :             :             // If this Cluster has an acceptable quality level, its chunks must be connected.
    1937         [ #  # ]:           0 :             assert(m_depgraph.IsConnected(linchunking.GetChunk(0).transactions));
    1938                 :             :         }
    1939                 :             :     }
    1940                 :             :     // Verify that each element of m_depgraph occurred in m_linearization.
    1941         [ #  # ]:           0 :     assert(m_done == m_depgraph.Positions());
    1942                 :           0 : }
    1943                 :             : 
    1944                 :           0 : void TxGraphImpl::SanityCheck() const
    1945                 :             : {
    1946                 :             :     /** Which GraphIndexes ought to occur in m_unlinked, based on m_entries. */
    1947                 :           0 :     std::set<GraphIndex> expected_unlinked;
    1948                 :             :     /** Which Clusters ought to occur in ClusterSet::m_clusters, based on m_entries. */
    1949         [ #  # ]:           0 :     std::set<const Cluster*> expected_clusters[MAX_LEVELS];
    1950                 :             :     /** Which GraphIndexes ought to occur in ClusterSet::m_removed, based on m_entries. */
    1951         [ #  # ]:           0 :     std::set<GraphIndex> expected_removed[MAX_LEVELS];
    1952                 :             :     /** Whether compaction is possible in the current state. */
    1953                 :           0 :     bool compact_possible{true};
    1954                 :             : 
    1955                 :             :     // Go over all Entry objects in m_entries.
    1956         [ #  # ]:           0 :     for (GraphIndex idx = 0; idx < m_entries.size(); ++idx) {
    1957         [ #  # ]:           0 :         const auto& entry = m_entries[idx];
    1958         [ #  # ]:           0 :         if (entry.m_ref == nullptr) {
    1959                 :             :             // Unlinked Entry must have indexes appear in m_unlinked.
    1960         [ #  # ]:           0 :             expected_unlinked.insert(idx);
    1961                 :             :         } else {
    1962                 :             :             // Every non-unlinked Entry must have a Ref that points back to it.
    1963         [ #  # ]:           0 :             assert(GetRefGraph(*entry.m_ref) == this);
    1964         [ #  # ]:           0 :             assert(GetRefIndex(*entry.m_ref) == idx);
    1965                 :             :         }
    1966                 :             :         // Verify the Entry m_locators.
    1967                 :             :         bool was_present{false}, was_removed{false};
    1968         [ #  # ]:           0 :         for (int level = 0; level < MAX_LEVELS; ++level) {
    1969                 :           0 :             const auto& locator = entry.m_locator[level];
    1970                 :             :             // Every Locator must be in exactly one of these 3 states.
    1971   [ #  #  #  #  :           0 :             assert(locator.IsMissing() + locator.IsRemoved() + locator.IsPresent() == 1);
                   #  # ]
    1972         [ #  # ]:           0 :             if (locator.IsPresent()) {
    1973                 :             :                 // Once removed, a transaction cannot be revived.
    1974         [ #  # ]:           0 :                 assert(!was_removed);
    1975                 :             :                 // Verify that the Cluster agrees with where the Locator claims the transaction is.
    1976         [ #  # ]:           0 :                 assert(locator.cluster->GetClusterEntry(locator.index) == idx);
    1977                 :             :                 // Remember that we expect said Cluster to appear in the ClusterSet::m_clusters.
    1978         [ #  # ]:           0 :                 expected_clusters[level].insert(locator.cluster);
    1979                 :             :                 was_present = true;
    1980         [ #  # ]:           0 :             } else if (locator.IsRemoved()) {
    1981                 :             :                 // Level 0 (main) cannot have IsRemoved locators (IsMissing there means non-existing).
    1982         [ #  # ]:           0 :                 assert(level > 0);
    1983                 :             :                 // A Locator can only be IsRemoved if it was IsPresent before, and only once.
    1984         [ #  # ]:           0 :                 assert(was_present && !was_removed);
    1985                 :             :                 // Remember that we expect this GraphIndex to occur in the ClusterSet::m_removed.
    1986         [ #  # ]:           0 :                 expected_removed[level].insert(idx);
    1987                 :             :                 was_removed = true;
    1988                 :             :             }
    1989                 :             :         }
    1990                 :             :     }
    1991                 :             : 
    1992                 :             :     // For all levels (0 = main, 1 = staged)...
    1993         [ #  # ]:           0 :     for (int level = 0; level <= GetTopLevel(); ++level) {
    1994                 :           0 :         assert(level < MAX_LEVELS);
    1995                 :           0 :         auto& clusterset = GetClusterSet(level);
    1996                 :           0 :         std::set<const Cluster*> actual_clusters;
    1997                 :             : 
    1998                 :             :         // For all quality levels...
    1999         [ #  # ]:           0 :         for (int qual = 0; qual < int(QualityLevel::NONE); ++qual) {
    2000                 :           0 :             QualityLevel quality{qual};
    2001                 :           0 :             const auto& quality_clusters = clusterset.m_clusters[qual];
    2002                 :             :             // ... for all clusters in them ...
    2003         [ #  # ]:           0 :             for (ClusterSetIndex setindex = 0; setindex < quality_clusters.size(); ++setindex) {
    2004         [ #  # ]:           0 :                 const auto& cluster = *quality_clusters[setindex];
    2005                 :             :                 // Remember we saw this Cluster (only if it is non-empty; empty Clusters aren't
    2006                 :             :                 // expected to be referenced by the Entry vector).
    2007         [ #  # ]:           0 :                 if (cluster.GetTxCount() != 0) {
    2008         [ #  # ]:           0 :                     actual_clusters.insert(&cluster);
    2009                 :             :                 }
    2010                 :             :                 // Sanity check the cluster, according to the Cluster's internal rules.
    2011                 :           0 :                 cluster.SanityCheck(*this, level);
    2012                 :             :                 // Check that the cluster's quality and setindex matches its position in the quality list.
    2013         [ #  # ]:           0 :                 assert(cluster.m_quality == quality);
    2014         [ #  # ]:           0 :                 assert(cluster.m_setindex == setindex);
    2015                 :             :             }
    2016                 :             :         }
    2017                 :             : 
    2018                 :             :         // Verify that all to-be-removed transactions have valid identifiers.
    2019         [ #  # ]:           0 :         for (GraphIndex idx : clusterset.m_to_remove) {
    2020         [ #  # ]:           0 :             assert(idx < m_entries.size());
    2021                 :             :             // We cannot assert that all m_to_remove transactions are still present: ~Ref on a
    2022                 :             :             // (P,M) transaction (present in main, inherited in staging) will cause an m_to_remove
    2023                 :             :             // addition in both main and staging, but a subsequence ApplyRemovals in main will
    2024                 :             :             // cause it to disappear from staging too, leaving the m_to_remove in place.
    2025                 :             :         }
    2026                 :             : 
    2027                 :             :         // Verify that all to-be-added dependencies have valid identifiers.
    2028   [ #  #  #  # ]:           0 :         for (auto [par_idx, chl_idx] : clusterset.m_deps_to_add) {
    2029         [ #  # ]:           0 :             assert(par_idx != chl_idx);
    2030         [ #  # ]:           0 :             assert(par_idx < m_entries.size());
    2031         [ #  # ]:           0 :             assert(chl_idx < m_entries.size());
    2032                 :             :         }
    2033                 :             : 
    2034                 :             :         // Verify that the actually encountered clusters match the ones occurring in Entry vector.
    2035         [ #  # ]:           0 :         assert(actual_clusters == expected_clusters[level]);
    2036                 :             : 
    2037                 :             :         // Verify that the contents of m_removed matches what was expected based on the Entry vector.
    2038         [ #  # ]:           0 :         std::set<GraphIndex> actual_removed(clusterset.m_removed.begin(), clusterset.m_removed.end());
    2039         [ #  # ]:           0 :         for (auto i : expected_unlinked) {
    2040                 :             :             // If a transaction exists in both main and staging, and is removed from staging (adding
    2041                 :             :             // it to m_removed there), and consequently destroyed (wiping the locator completely),
    2042                 :             :             // it can remain in m_removed despite not having an IsRemoved() locator. Exclude those
    2043                 :             :             // transactions from the comparison here.
    2044                 :           0 :             actual_removed.erase(i);
    2045                 :           0 :             expected_removed[level].erase(i);
    2046                 :             :         }
    2047                 :             : 
    2048         [ #  # ]:           0 :         assert(actual_removed == expected_removed[level]);
    2049                 :             : 
    2050                 :             :         // If any GraphIndex entries remain in this ClusterSet, compact is not possible.
    2051         [ #  # ]:           0 :         if (!clusterset.m_deps_to_add.empty()) compact_possible = false;
    2052         [ #  # ]:           0 :         if (!clusterset.m_to_remove.empty()) compact_possible = false;
    2053         [ #  # ]:           0 :         if (!clusterset.m_removed.empty()) compact_possible = false;
    2054                 :             : 
    2055                 :             :         // If m_group_data exists, its m_group_oversized must match m_oversized.
    2056         [ #  # ]:           0 :         if (clusterset.m_group_data.has_value()) {
    2057         [ #  # ]:           0 :             assert(clusterset.m_oversized == clusterset.m_group_data->m_group_oversized);
    2058                 :             :         }
    2059                 :             : 
    2060                 :             :         // For non-top levels, m_oversized must be known (as it cannot change until the level
    2061                 :             :         // on top is gone).
    2062   [ #  #  #  # ]:           0 :         if (level < GetTopLevel()) assert(clusterset.m_oversized.has_value());
    2063                 :           0 :     }
    2064                 :             : 
    2065                 :             :     // Verify that the contents of m_unlinked matches what was expected based on the Entry vector.
    2066         [ #  # ]:           0 :     std::set<GraphIndex> actual_unlinked(m_unlinked.begin(), m_unlinked.end());
    2067         [ #  # ]:           0 :     assert(actual_unlinked == expected_unlinked);
    2068                 :             : 
    2069                 :             :     // If compaction was possible, it should have been performed already, and m_unlinked must be
    2070                 :             :     // empty (to prevent memory leaks due to an ever-growing m_entries vector).
    2071         [ #  # ]:           0 :     if (compact_possible) {
    2072         [ #  # ]:           0 :         assert(actual_unlinked.empty());
    2073                 :             :     }
    2074   [ #  #  #  #  :           0 : }
             #  #  #  # ]
    2075                 :             : 
    2076                 :           0 : void TxGraphImpl::DoWork() noexcept
    2077                 :             : {
    2078         [ #  # ]:           0 :     for (int level = 0; level <= GetTopLevel(); ++level) {
    2079                 :           0 :         MakeAllAcceptable(level);
    2080                 :             :     }
    2081                 :           0 : }
    2082                 :             : 
    2083                 :             : } // namespace
    2084                 :             : 
    2085                 :           0 : TxGraph::Ref::~Ref()
    2086                 :             : {
    2087         [ #  # ]:           0 :     if (m_graph) {
    2088                 :             :         // Inform the TxGraph about the Ref being destroyed.
    2089                 :           0 :         m_graph->UnlinkRef(m_index);
    2090                 :           0 :         m_graph = nullptr;
    2091                 :             :     }
    2092                 :           0 : }
    2093                 :             : 
    2094                 :           0 : TxGraph::Ref& TxGraph::Ref::operator=(Ref&& other) noexcept
    2095                 :             : {
    2096                 :             :     // Unlink the current graph, if any.
    2097         [ #  # ]:           0 :     if (m_graph) m_graph->UnlinkRef(m_index);
    2098                 :             :     // Inform the other's graph about the move, if any.
    2099         [ #  # ]:           0 :     if (other.m_graph) other.m_graph->UpdateRef(other.m_index, *this);
    2100                 :             :     // Actually update the contents.
    2101                 :           0 :     m_graph = other.m_graph;
    2102                 :           0 :     m_index = other.m_index;
    2103                 :           0 :     other.m_graph = nullptr;
    2104                 :           0 :     other.m_index = GraphIndex(-1);
    2105                 :           0 :     return *this;
    2106                 :             : }
    2107                 :             : 
    2108                 :           0 : TxGraph::Ref::Ref(Ref&& other) noexcept
    2109                 :             : {
    2110                 :             :     // Inform the TxGraph of other that its Ref is being moved.
    2111         [ #  # ]:           0 :     if (other.m_graph) other.m_graph->UpdateRef(other.m_index, *this);
    2112                 :             :     // Actually move the contents.
    2113                 :           0 :     std::swap(m_graph, other.m_graph);
    2114                 :           0 :     std::swap(m_index, other.m_index);
    2115                 :           0 : }
    2116                 :             : 
    2117                 :           0 : std::unique_ptr<TxGraph> MakeTxGraph(unsigned max_cluster_count) noexcept
    2118                 :             : {
    2119         [ #  # ]:           0 :     return std::make_unique<TxGraphImpl>(max_cluster_count);
    2120                 :             : }
        

Generated by: LCOV version 2.0-1