LCOV - code coverage report
Current view: top level - src/test/fuzz - cluster_linearize.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 98.9 % 646 639
Test Date: 2026-02-19 05:12:30 Functions: 100.0 % 38 38
Branches: 67.3 % 966 650

             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 <cluster_linearize.h>
       6                 :             : #include <random.h>
       7                 :             : #include <serialize.h>
       8                 :             : #include <streams.h>
       9                 :             : #include <test/fuzz/FuzzedDataProvider.h>
      10                 :             : #include <test/fuzz/fuzz.h>
      11                 :             : #include <test/util/cluster_linearize.h>
      12                 :             : #include <util/bitset.h>
      13                 :             : #include <util/feefrac.h>
      14                 :             : 
      15                 :             : #include <algorithm>
      16                 :             : #include <cstdint>
      17                 :             : #include <utility>
      18                 :             : #include <vector>
      19                 :             : 
      20                 :             : /*
      21                 :             :  * The tests in this file primarily cover the candidate finder classes and linearization algorithms.
      22                 :             :  *
      23                 :             :  *   <----: An implementation (at the start of the line --) is tested in the test marked with *,
      24                 :             :  *          possibly by comparison with other implementations (at the end of the line ->).
      25                 :             :  *   <<---: The right side is implemented using the left side.
      26                 :             :  *
      27                 :             :  *   +---------------------+                        +-----------+
      28                 :             :  *   | SpanningForestState | <<-------------------- | Linearize |
      29                 :             :  *   +---------------------+                        +-----------+
      30                 :             :  *               |                                       |
      31                 :             :  *               |                                       |        ^^  PRODUCTION CODE
      32                 :             :  *               |                                       |        ||
      33                 :             :  *  ==============================================================================================
      34                 :             :  *               |                                       |        ||
      35                 :             :  *               |-clusterlin_sfl*                       |        vv  TEST CODE
      36                 :             :  *               |                                       |
      37                 :             :  *               \------------------------------------\  |-clusterlin_linearize*
      38                 :             :  *                                                    |  |
      39                 :             :  *                                                    v  v
      40                 :             :  *   +-----------------------+                      +-----------------+
      41                 :             :  *   | SimpleCandidateFinder | <<-------------------| SimpleLinearize |
      42                 :             :  *   +-----------------------+                      +-----------------+
      43                 :             :  *                  |                                    |
      44                 :             :  *                  |-clusterlin_simple_finder*          |-clusterlin_simple_linearize*
      45                 :             :  *                  v                                    v
      46                 :             :  *   +---------------------------+                  +---------------------+
      47                 :             :  *   | ExhaustiveCandidateFinder |                  | ExhaustiveLinearize |
      48                 :             :  *   +---------------------------+                  +---------------------+
      49                 :             :  *
      50                 :             :  * More tests are included for lower-level and related functions and classes:
      51                 :             :  * - DepGraph tests:
      52                 :             :  *   - clusterlin_depgraph_sim
      53                 :             :  *   - clusterlin_depgraph_serialization
      54                 :             :  *   - clusterlin_components
      55                 :             :  * - ChunkLinearization and ChunkLinearizationInfo tests:
      56                 :             :  *   - clusterlin_chunking
      57                 :             :  * - PostLinearize tests:
      58                 :             :  *   - clusterlin_postlinearize
      59                 :             :  *   - clusterlin_postlinearize_tree
      60                 :             :  *   - clusterlin_postlinearize_moved_leaf
      61                 :             :  * - MakeConnected tests (a test-only function):
      62                 :             :  *   - clusterlin_make_connected
      63                 :             :  */
      64                 :             : 
      65                 :             : using namespace cluster_linearize;
      66                 :             : 
      67                 :             : namespace {
      68                 :             : 
      69                 :             : /** A simple finder class for candidate sets (topologically-valid subsets with high feerate), only
      70                 :             :  *  used by SimpleLinearize below. */
      71                 :             : template<typename SetType>
      72                 :             : class SimpleCandidateFinder
      73                 :             : {
      74                 :             :     /** Internal dependency graph. */
      75                 :             :     const DepGraph<SetType>& m_depgraph;
      76                 :             :     /** Which transaction are left to include. */
      77                 :             :     SetType m_todo;
      78                 :             : 
      79                 :             : public:
      80                 :             :     /** Construct an SimpleCandidateFinder for a given graph. */
      81                 :        2074 :     SimpleCandidateFinder(const DepGraph<SetType>& depgraph LIFETIMEBOUND) noexcept :
      82                 :        2074 :         m_depgraph(depgraph), m_todo{depgraph.Positions()} {}
      83                 :             : 
      84                 :             :     /** Remove a set of transactions from the set of to-be-linearized ones. */
      85                 :       22223 :     void MarkDone(SetType select) noexcept { m_todo -= select; }
      86                 :             : 
      87                 :             :     /** Determine whether unlinearized transactions remain. */
      88                 :        2636 :     bool AllDone() const noexcept { return m_todo.None(); }
      89                 :             : 
      90                 :             :     /** Find a candidate set using at most max_iterations iterations, and the number of iterations
      91                 :             :      *  actually performed. If that number is less than max_iterations, then the result is optimal.
      92                 :             :      *
      93                 :             :      * Always returns a connected set of transactions.
      94                 :             :      *
      95                 :             :      * Complexity: O(N * M), where M is the number of connected topological subsets of the cluster.
      96                 :             :      *             That number is bounded by M <= 2^(N-1).
      97                 :             :      */
      98                 :       22223 :     std::pair<SetInfo<SetType>, uint64_t> FindCandidateSet(uint64_t max_iterations) const noexcept
      99                 :             :     {
     100                 :       22223 :         uint64_t iterations_left = max_iterations;
     101                 :             :         // Queue of work units. Each consists of:
     102                 :             :         // - inc: set of transactions definitely included
     103                 :             :         // - und: set of transactions that can be added to inc still
     104                 :       22223 :         std::vector<std::pair<SetType, SetType>> queue;
     105                 :             :         // Initially we have just one queue element, with the entire graph in und.
     106                 :       22223 :         queue.emplace_back(SetType{}, m_todo);
     107                 :             :         // Best solution so far. Initialize with the remaining ancestors of the first remaining
     108                 :             :         // transaction.
     109                 :       22223 :         SetInfo best(m_depgraph, m_depgraph.Ancestors(m_todo.First()) & m_todo);
     110                 :             :         // Process the queue.
     111   [ +  +  +  + ]:    67535994 :         while (!queue.empty() && iterations_left) {
     112                 :             :             // Pop top element of the queue.
     113                 :    67513771 :             auto [inc, und] = queue.back();
     114                 :    67513771 :             queue.pop_back();
     115                 :             :             // Look for a transaction to consider adding/removing.
     116                 :    67513771 :             bool inc_none = inc.None();
     117         [ +  + ]:   146757638 :             for (auto split : und) {
     118                 :             :                 // If inc is empty, consider any split transaction. Otherwise only consider
     119                 :             :                 // transactions that share ancestry with inc so far (which means only connected
     120                 :             :                 // sets will be considered).
     121   [ +  +  +  + ]:   112991518 :                 if (inc_none || inc.Overlaps(m_depgraph.Ancestors(split))) {
     122                 :    33747651 :                     --iterations_left;
     123                 :             :                     // Add a queue entry with split included.
     124                 :    33747651 :                     SetInfo new_inc(m_depgraph, inc | (m_todo & m_depgraph.Ancestors(split)));
     125                 :    33747651 :                     queue.emplace_back(new_inc.transactions, und - new_inc.transactions);
     126                 :             :                     // Add a queue entry with split excluded.
     127                 :    33747651 :                     queue.emplace_back(inc, und - m_depgraph.Descendants(split));
     128                 :             :                     // Update statistics to account for the candidate new_inc.
     129         [ +  + ]:    33747651 :                     if (new_inc.feerate > best.feerate) best = new_inc;
     130                 :             :                     break;
     131                 :             :                 }
     132                 :             :             }
     133                 :             :         }
     134                 :       22223 :         return {std::move(best), max_iterations - iterations_left};
     135                 :       22223 :     }
     136                 :             : };
     137                 :             : 
     138                 :             : /** A very simple finder class for optimal candidate sets, which tries every subset.
     139                 :             :  *
     140                 :             :  * It is even simpler than SimpleCandidateFinder, and exists just to help test the correctness of
     141                 :             :  * SimpleCandidateFinder, so that it can be used in SimpleLinearize, which is then used to test the
     142                 :             :  * correctness of Linearize.
     143                 :             :  */
     144                 :             : template<typename SetType>
     145                 :             : class ExhaustiveCandidateFinder
     146                 :             : {
     147                 :             :     /** Internal dependency graph. */
     148                 :             :     const DepGraph<SetType>& m_depgraph;
     149                 :             :     /** Which transaction are left to include. */
     150                 :             :     SetType m_todo;
     151                 :             : 
     152                 :             : public:
     153                 :             :     /** Construct an ExhaustiveCandidateFinder for a given graph. */
     154                 :         260 :     ExhaustiveCandidateFinder(const DepGraph<SetType>& depgraph LIFETIMEBOUND) noexcept :
     155                 :         260 :         m_depgraph(depgraph), m_todo{depgraph.Positions()} {}
     156                 :             : 
     157                 :             :     /** Remove a set of transactions from the set of to-be-linearized ones. */
     158                 :        2376 :     void MarkDone(SetType select) noexcept { m_todo -= select; }
     159                 :             : 
     160                 :             :     /** Determine whether unlinearized transactions remain. */
     161                 :        2636 :     bool AllDone() const noexcept { return m_todo.None(); }
     162                 :             : 
     163                 :             :     /** Find the optimal remaining candidate set.
     164                 :             :      *
     165                 :             :      * Complexity: O(N * 2^N).
     166                 :             :      */
     167                 :        1472 :     SetInfo<SetType> FindCandidateSet() const noexcept
     168                 :             :     {
     169                 :             :         // Best solution so far.
     170                 :        1472 :         SetInfo<SetType> best{m_todo, m_depgraph.FeeRate(m_todo)};
     171                 :             :         // The number of combinations to try.
     172                 :        1472 :         uint64_t limit = (uint64_t{1} << m_todo.Count()) - 1;
     173                 :             :         // Try the transitive closure of every non-empty subset of m_todo.
     174         [ +  + ]:      667408 :         for (uint64_t x = 1; x < limit; ++x) {
     175                 :             :             // If bit number b is set in x, then the remaining ancestors of the b'th remaining
     176                 :             :             // transaction in m_todo are included.
     177                 :      665936 :             SetType txn;
     178                 :      665936 :             auto x_shifted{x};
     179   [ +  -  +  + ]:     8569688 :             for (auto i : m_todo) {
     180         [ +  + ]:     7237816 :                 if (x_shifted & 1) txn |= m_depgraph.Ancestors(i);
     181                 :     7237816 :                 x_shifted >>= 1;
     182                 :             :             }
     183                 :      665936 :             SetInfo cur(m_depgraph, txn & m_todo);
     184         [ +  + ]:      665936 :             if (cur.feerate > best.feerate) best = cur;
     185                 :             :         }
     186                 :        1472 :         return best;
     187                 :             :     }
     188                 :             : };
     189                 :             : 
     190                 :             : /** A simple linearization algorithm.
     191                 :             :  *
     192                 :             :  * This matches Linearize() in interface and behavior, though with fewer optimizations, lacking
     193                 :             :  * the ability to pass in an existing linearization, and linearizing by simply finding the
     194                 :             :  * consecutive remaining highest-feerate topological subset using SimpleCandidateFinder.
     195                 :             :  */
     196                 :             : template<typename SetType>
     197                 :        1814 : std::pair<std::vector<DepGraphIndex>, bool> SimpleLinearize(const DepGraph<SetType>& depgraph, uint64_t max_iterations)
     198                 :             : {
     199                 :        1814 :     std::vector<DepGraphIndex> linearization;
     200                 :        1814 :     SimpleCandidateFinder finder(depgraph);
     201                 :        1814 :     SetType todo = depgraph.Positions();
     202                 :        1814 :     bool optimal = true;
     203         [ +  + ]:       21661 :     while (todo.Any()) {
     204         [ +  + ]:       19847 :         auto [candidate, iterations_done] = finder.FindCandidateSet(max_iterations);
     205         [ +  + ]:       19847 :         if (iterations_done == max_iterations) optimal = false;
     206                 :       19847 :         depgraph.AppendTopo(linearization, candidate.transactions);
     207                 :       19847 :         todo -= candidate.transactions;
     208                 :       19847 :         finder.MarkDone(candidate.transactions);
     209                 :       19847 :         max_iterations -= iterations_done;
     210                 :             :     }
     211                 :        1814 :     return {std::move(linearization), optimal};
     212                 :        1814 : }
     213                 :             : 
     214                 :             : /** An even simpler linearization algorithm that tries all permutations.
     215                 :             :  *
     216                 :             :  * This roughly matches SimpleLinearize() (and Linearize) in interface and behavior, but always
     217                 :             :  * tries all topologically-valid transaction orderings, has no way to bound how much work it does,
     218                 :             :  * and always finds the optimal. With an O(n!) complexity, it should only be used for small
     219                 :             :  * clusters.
     220                 :             :  */
     221                 :             : template<typename SetType>
     222                 :         168 : std::vector<DepGraphIndex> ExhaustiveLinearize(const DepGraph<SetType>& depgraph)
     223                 :             : {
     224                 :             :     // The best linearization so far, and its chunking.
     225                 :         168 :     std::vector<DepGraphIndex> linearization;
     226                 :         168 :     std::vector<FeeFrac> chunking;
     227                 :             : 
     228         [ +  + ]:         168 :     std::vector<DepGraphIndex> perm_linearization;
     229                 :             :     // Initialize with the lexicographically-first linearization.
     230   [ +  +  +  -  :        1161 :     for (DepGraphIndex i : depgraph.Positions()) perm_linearization.push_back(i);
                   +  + ]
     231                 :             :     // Iterate over all valid permutations.
     232                 :             :     do {
     233                 :             :         /** What prefix of perm_linearization is topological. */
     234                 :      710435 :         DepGraphIndex topo_length{0};
     235                 :      710435 :         TestBitSet perm_done;
     236   [ -  +  +  + ]:     6075260 :         while (topo_length < perm_linearization.size()) {
     237                 :     5454196 :             auto i = perm_linearization[topo_length];
     238         [ +  + ]:     5454196 :             perm_done.Set(i);
     239         [ +  + ]:     5454196 :             if (!depgraph.Ancestors(i).IsSubsetOf(perm_done)) break;
     240                 :     5364825 :             ++topo_length;
     241                 :             :         }
     242   [ -  +  +  + ]:      710435 :         if (topo_length == perm_linearization.size()) {
     243                 :             :             // If all of perm_linearization is topological, check if it is perhaps our best
     244                 :             :             // linearization so far.
     245         [ -  + ]:      621064 :             auto perm_chunking = ChunkLinearization(depgraph, perm_linearization);
     246   [ -  +  +  - ]:      621064 :             auto cmp = CompareChunks(perm_chunking, chunking);
     247                 :             :             // If the diagram is better, or if it is equal but with more chunks (because we
     248                 :             :             // prefer minimal chunks), consider this better.
     249   [ +  +  +  +  :      677486 :             if (linearization.empty() || cmp > 0 || (cmp == 0 && perm_chunking.size() > chunking.size())) {
          +  +  -  +  +  
                      + ]
     250         [ +  - ]:        1966 :                 linearization = perm_linearization;
     251         [ +  - ]:        1966 :                 chunking = perm_chunking;
     252                 :             :             }
     253                 :      621064 :         } else {
     254                 :             :             // Otherwise, fast forward to the last permutation with the same non-topological
     255                 :             :             // prefix.
     256                 :       89371 :             auto first_non_topo = perm_linearization.begin() + topo_length;
     257         [ -  + ]:       89371 :             assert(std::is_sorted(first_non_topo + 1, perm_linearization.end()));
     258                 :       89371 :             std::reverse(first_non_topo + 1, perm_linearization.end());
     259                 :             :         }
     260         [ +  + ]:      710435 :     } while(std::next_permutation(perm_linearization.begin(), perm_linearization.end()));
     261                 :             : 
     262                 :         168 :     return linearization;
     263                 :         168 : }
     264                 :             : 
     265                 :             : 
     266                 :             : /** Stitch connected components together in a DepGraph, guaranteeing its corresponding cluster is connected. */
     267                 :             : template<typename BS>
     268                 :        1449 : void MakeConnected(DepGraph<BS>& depgraph)
     269                 :             : {
     270                 :        1449 :     auto todo = depgraph.Positions();
     271                 :        1449 :     auto comp = depgraph.FindConnectedComponent(todo);
     272         [ -  + ]:        1449 :     Assume(depgraph.IsConnected(comp));
     273                 :        1449 :     todo -= comp;
     274         [ +  + ]:       13676 :     while (todo.Any()) {
     275                 :       12227 :         auto nextcomp = depgraph.FindConnectedComponent(todo);
     276         [ -  + ]:       12227 :         Assume(depgraph.IsConnected(nextcomp));
     277                 :       12227 :         depgraph.AddDependencies(BS::Singleton(comp.Last()), nextcomp.First());
     278                 :       12227 :         todo -= nextcomp;
     279                 :       12227 :         comp = nextcomp;
     280                 :             :     }
     281                 :        1449 : }
     282                 :             : 
     283                 :             : /** Given a dependency graph, and a todo set, read a topological subset of todo from reader. */
     284                 :             : template<typename SetType>
     285                 :        4708 : SetType ReadTopologicalSet(const DepGraph<SetType>& depgraph, const SetType& todo, SpanReader& reader, bool non_empty)
     286                 :             : {
     287                 :             :     // Read a bitmask from the fuzzing input. Add 1 if non_empty, so the mask is definitely not
     288                 :             :     // zero in that case.
     289         [ +  + ]:        4708 :     uint64_t mask{0};
     290                 :             :     try {
     291         [ +  + ]:        4708 :         reader >> VARINT(mask);
     292         [ -  + ]:        3559 :     } catch(const std::ios_base::failure&) {}
     293         [ +  + ]:        4708 :     if (mask != uint64_t(-1)) mask += non_empty;
     294                 :             : 
     295                 :        4708 :     SetType ret;
     296   [ +  -  +  + ]:       62811 :     for (auto i : todo) {
     297         [ +  + ]:       53395 :         if (!ret[i]) {
     298         [ +  + ]:       50608 :             if (mask & 1) ret |= depgraph.Ancestors(i);
     299                 :       50608 :             mask >>= 1;
     300                 :             :         }
     301                 :             :     }
     302                 :        4708 :     ret &= todo;
     303                 :             : 
     304                 :             :     // While mask starts off non-zero if non_empty is true, it is still possible that all its low
     305                 :             :     // bits are 0, and ret ends up being empty. As a last resort, use the in-todo ancestry of the
     306                 :             :     // first todo position.
     307   [ +  -  +  + ]:        4708 :     if (non_empty && ret.None()) {
     308         [ -  + ]:         217 :         Assume(todo.Any());
     309         [ -  + ]:         217 :         ret = depgraph.Ancestors(todo.First()) & todo;
     310         [ -  + ]:         217 :         Assume(ret.Any());
     311                 :             :     }
     312                 :        4708 :     return ret;
     313                 :             : }
     314                 :             : 
     315                 :             : /** Given a dependency graph, construct any valid linearization for it, reading from a SpanReader. */
     316                 :             : template<typename BS>
     317                 :       11866 : std::vector<DepGraphIndex> ReadLinearization(const DepGraph<BS>& depgraph, SpanReader& reader, bool topological=true)
     318                 :             : {
     319                 :       11866 :     std::vector<DepGraphIndex> linearization;
     320                 :       11866 :     TestBitSet todo = depgraph.Positions();
     321                 :             :     // In every iteration one transaction is appended to linearization.
     322         [ +  + ]:      301183 :     while (todo.Any()) {
     323                 :             :         // Compute the set of transactions to select from.
     324                 :      277451 :         TestBitSet potential_next;
     325         [ +  + ]:      277451 :         if (topological) {
     326                 :             :             // Find all transactions with no not-yet-included ancestors.
     327         [ +  + ]:     4218720 :             for (auto j : todo) {
     328         [ +  + ]:     5685642 :                 if ((depgraph.Ancestors(j) & todo) == TestBitSet::Singleton(j)) {
     329                 :     1740126 :                     potential_next.Set(j);
     330                 :             :                 }
     331                 :             :             }
     332                 :             :         } else {
     333                 :             :             // Allow any element to be selected next, regardless of topology.
     334                 :        4247 :             potential_next = todo;
     335                 :             :         }
     336                 :             :         // There must always be one (otherwise there is a cycle in the graph).
     337         [ -  + ]:      277451 :         assert(potential_next.Any());
     338                 :             :         // Read a number from reader, and interpret it as index into potential_next.
     339         [ +  + ]:      277451 :         uint64_t idx{0};
     340                 :             :         try {
     341   [ +  +  +  - ]:      554902 :             reader >> VARINT(idx);
     342         [ -  + ]:      264820 :         } catch (const std::ios_base::failure&) {}
     343                 :      277451 :         idx %= potential_next.Count();
     344                 :             :         // Find out which transaction that corresponds to.
     345   [ +  -  +  - ]:      591120 :         for (auto j : potential_next) {
     346         [ +  + ]:      313669 :             if (idx == 0) {
     347                 :             :                 // When found, add it to linearization and remove it from todo.
     348         [ +  - ]:      277451 :                 linearization.push_back(j);
     349         [ -  + ]:      277451 :                 assert(todo[j]);
     350                 :      277451 :                 todo.Reset(j);
     351                 :      277451 :                 break;
     352                 :             :             }
     353                 :       36218 :             --idx;
     354                 :             :         }
     355                 :             :     }
     356                 :       11866 :     return linearization;
     357                 :           0 : }
     358                 :             : 
     359                 :             : /** Given a dependency graph, construct a tree-structured graph.
     360                 :             :  *
     361                 :             :  * Copies the nodes from the depgraph, but only keeps the first parent (even direction)
     362                 :             :  * or the first child (odd direction) for each transaction.
     363                 :             :  */
     364                 :             : template<typename BS>
     365                 :         647 : DepGraph<BS> BuildTreeGraph(const DepGraph<BS>& depgraph, uint8_t direction)
     366                 :             : {
     367                 :         647 :     DepGraph<BS> depgraph_tree;
     368   [ -  +  +  + ]:       18896 :     for (DepGraphIndex i = 0; i < depgraph.PositionRange(); ++i) {
     369         [ +  + ]:       18249 :         if (depgraph.Positions()[i]) {
     370                 :       14305 :             depgraph_tree.AddTransaction(depgraph.FeeRate(i));
     371                 :             :         } else {
     372                 :             :             // For holes, add a dummy transaction which is deleted below, so that non-hole
     373                 :             :             // transactions retain their position.
     374                 :        3944 :             depgraph_tree.AddTransaction(FeeFrac{});
     375                 :             :         }
     376                 :             :     }
     377                 :         647 :     depgraph_tree.RemoveTransactions(BS::Fill(depgraph.PositionRange()) - depgraph.Positions());
     378                 :             : 
     379         [ +  + ]:         647 :     if (direction & 1) {
     380   [ +  +  +  + ]:        9206 :         for (DepGraphIndex i : depgraph.Positions()) {
     381         [ +  + ]:        8482 :             auto children = depgraph.GetReducedChildren(i);
     382         [ +  + ]:        8482 :             if (children.Any()) {
     383                 :        4790 :                 depgraph_tree.AddDependencies(BS::Singleton(i), children.First());
     384                 :             :             }
     385                 :             :         }
     386                 :             :     } else {
     387   [ +  +  +  + ]:        6379 :         for (DepGraphIndex i : depgraph.Positions()) {
     388         [ +  + ]:        5823 :             auto parents = depgraph.GetReducedParents(i);
     389         [ +  + ]:        5823 :             if (parents.Any()) {
     390                 :        2890 :                 depgraph_tree.AddDependencies(BS::Singleton(parents.First()), i);
     391                 :             :             }
     392                 :             :         }
     393                 :             :     }
     394                 :             : 
     395                 :         647 :     return depgraph_tree;
     396                 :             : }
     397                 :             : 
     398                 :             : } // namespace
     399                 :             : 
     400         [ +  - ]:         960 : FUZZ_TARGET(clusterlin_depgraph_sim)
     401                 :             : {
     402                 :             :     // Simulation test to verify the full behavior of DepGraph.
     403                 :             : 
     404                 :         506 :     FuzzedDataProvider provider(buffer.data(), buffer.size());
     405                 :             : 
     406                 :             :     /** Real DepGraph being tested. */
     407                 :         506 :     DepGraph<TestBitSet> real;
     408                 :             :     /** Simulated DepGraph (sim[i] is std::nullopt if position i does not exist; otherwise,
     409                 :             :      *  sim[i]->first is its individual feerate, and sim[i]->second is its set of ancestors. */
     410                 :         506 :     std::array<std::optional<std::pair<FeeFrac, TestBitSet>>, TestBitSet::Size()> sim;
     411                 :             :     /** The number of non-nullopt position in sim. */
     412                 :         506 :     DepGraphIndex num_tx_sim{0};
     413                 :             : 
     414                 :             :     /** Read a valid index of a transaction from the provider. */
     415                 :       30759 :     auto idx_fn = [&]() {
     416                 :       30253 :         auto offset = provider.ConsumeIntegralInRange<DepGraphIndex>(0, num_tx_sim - 1);
     417         [ +  - ]:      196628 :         for (DepGraphIndex i = 0; i < sim.size(); ++i) {
     418         [ +  + ]:      196628 :             if (!sim[i].has_value()) continue;
     419         [ +  + ]:      172011 :             if (offset == 0) return i;
     420                 :      141758 :             --offset;
     421                 :             :         }
     422                 :           0 :         assert(false);
     423                 :             :         return DepGraphIndex(-1);
     424                 :         506 :     };
     425                 :             : 
     426                 :             :     /** Read a valid subset of the transactions from the provider. */
     427                 :       30759 :     auto subset_fn = [&]() {
     428                 :       30253 :         auto range = (uint64_t{1} << num_tx_sim) - 1;
     429                 :       30253 :         const auto mask = provider.ConsumeIntegralInRange<uint64_t>(0, range);
     430                 :       30253 :         auto mask_shifted = mask;
     431                 :       30253 :         TestBitSet subset;
     432         [ +  + ]:      998349 :         for (DepGraphIndex i = 0; i < sim.size(); ++i) {
     433         [ +  + ]:      968096 :             if (!sim[i].has_value()) continue;
     434         [ +  + ]:      604650 :             if (mask_shifted & 1) {
     435                 :      155365 :                 subset.Set(i);
     436                 :             :             }
     437                 :      604650 :             mask_shifted >>= 1;
     438                 :             :         }
     439         [ -  + ]:       30253 :         assert(mask_shifted == 0);
     440                 :       30253 :         return subset;
     441                 :         506 :     };
     442                 :             : 
     443                 :             :     /** Read any set of transactions from the provider (including unused positions). */
     444                 :       16515 :     auto set_fn = [&]() {
     445                 :       16009 :         auto range = (uint64_t{1} << sim.size()) - 1;
     446                 :       16009 :         const auto mask = provider.ConsumeIntegralInRange<uint64_t>(0, range);
     447                 :       16009 :         TestBitSet set;
     448         [ +  + ]:      528297 :         for (DepGraphIndex i = 0; i < sim.size(); ++i) {
     449         [ +  + ]:      512288 :             if ((mask >> i) & 1) {
     450                 :      187036 :                 set.Set(i);
     451                 :             :             }
     452                 :             :         }
     453                 :       16009 :         return set;
     454                 :         506 :     };
     455                 :             : 
     456                 :             :     /** Propagate ancestor information in sim. */
     457                 :       17021 :     auto anc_update_fn = [&]() {
     458                 :       19294 :         while (true) {
     459                 :       19294 :             bool updates{false};
     460         [ +  + ]:      636702 :             for (DepGraphIndex chl = 0; chl < sim.size(); ++chl) {
     461         [ +  + ]:      617408 :                 if (!sim[chl].has_value()) continue;
     462   [ +  -  +  + ]:     1401379 :                 for (auto par : sim[chl]->second) {
     463         [ +  + ]:      839631 :                     if (!sim[chl]->second.IsSupersetOf(sim[par]->second)) {
     464                 :       11549 :                         sim[chl]->second |= sim[par]->second;
     465                 :       11549 :                         updates = true;
     466                 :             :                     }
     467                 :             :                 }
     468                 :             :             }
     469         [ +  + ]:       19294 :             if (!updates) break;
     470                 :             :         }
     471                 :       17021 :     };
     472                 :             : 
     473                 :             :     /** Compare the state of transaction i in the simulation with the real one. */
     474                 :      203734 :     auto check_fn = [&](DepGraphIndex i) {
     475                 :             :         // Compare used positions.
     476         [ -  + ]:      203228 :         assert(real.Positions()[i] == sim[i].has_value());
     477         [ +  + ]:      203228 :         if (sim[i].has_value()) {
     478                 :             :             // Compare feerate.
     479         [ +  - ]:       44406 :             assert(real.FeeRate(i) == sim[i]->first);
     480                 :             :             // Compare ancestors (note that SanityCheck verifies correspondence between ancestors
     481                 :             :             // and descendants, so we can restrict ourselves to ancestors here).
     482         [ -  + ]:       44406 :             assert(real.Ancestors(i) == sim[i]->second);
     483                 :             :         }
     484                 :      203734 :     };
     485                 :             : 
     486                 :         506 :     auto last_compaction_pos{real.PositionRange()};
     487                 :             : 
     488   [ +  +  +  + ]:      129464 :     LIMITED_WHILE(provider.remaining_bytes() > 0, 1000) {
     489                 :      128958 :         int command = provider.ConsumeIntegral<uint8_t>() % 4;
     490                 :      135027 :         while (true) {
     491                 :             :             // Iterate decreasing command until an applicable branch is found.
     492   [ +  +  +  + ]:      135027 :             if (num_tx_sim < TestBitSet::Size() && command-- == 0) {
     493                 :             :                 // AddTransaction.
     494                 :       44406 :                 auto fee = provider.ConsumeIntegralInRange<int64_t>(-0x8000000000000, 0x7ffffffffffff);
     495                 :       44406 :                 auto size = provider.ConsumeIntegralInRange<int32_t>(1, 0x3fffff);
     496                 :       44406 :                 FeeFrac feerate{fee, size};
     497                 :             :                 // Apply to DepGraph.
     498                 :       44406 :                 auto idx = real.AddTransaction(feerate);
     499                 :             :                 // Verify that the returned index is correct.
     500         [ -  + ]:       44406 :                 assert(!sim[idx].has_value());
     501         [ +  - ]:      584866 :                 for (DepGraphIndex i = 0; i < TestBitSet::Size(); ++i) {
     502         [ +  + ]:      584866 :                     if (!sim[i].has_value()) {
     503         [ -  + ]:       44406 :                         assert(idx == i);
     504                 :             :                         break;
     505                 :             :                     }
     506                 :             :                 }
     507                 :             :                 // Update sim.
     508         [ -  + ]:       44406 :                 sim[idx] = {feerate, TestBitSet::Singleton(idx)};
     509                 :       44406 :                 ++num_tx_sim;
     510                 :       44406 :                 break;
     511   [ +  +  +  + ]:       90621 :             } else if (num_tx_sim > 0 && command-- == 0) {
     512                 :             :                 // AddDependencies.
     513                 :       30253 :                 DepGraphIndex child = idx_fn();
     514                 :       30253 :                 auto parents = subset_fn();
     515                 :             :                 // Apply to DepGraph.
     516                 :       30253 :                 real.AddDependencies(parents, child);
     517                 :             :                 // Apply to sim.
     518                 :       30253 :                 sim[child]->second |= parents;
     519                 :       30253 :                 break;
     520   [ +  +  +  + ]:       60368 :             } else if (num_tx_sim > 0 && command-- == 0) {
     521                 :             :                 // Remove transactions.
     522                 :       16009 :                 auto del = set_fn();
     523                 :             :                 // Propagate all ancestry information before deleting anything in the simulation (as
     524                 :             :                 // intermediary transactions may be deleted which impact connectivity).
     525                 :       16009 :                 anc_update_fn();
     526                 :             :                 // Compare the state of the transactions being deleted.
     527   [ +  +  +  + ]:      217441 :                 for (auto i : del) check_fn(i);
     528                 :             :                 // Apply to DepGraph.
     529                 :       16009 :                 real.RemoveTransactions(del);
     530                 :             :                 // Apply to sim.
     531         [ +  + ]:      528297 :                 for (DepGraphIndex i = 0; i < sim.size(); ++i) {
     532         [ +  + ]:      512288 :                     if (sim[i].has_value()) {
     533         [ +  + ]:      200978 :                         if (del[i]) {
     534                 :       36977 :                             --num_tx_sim;
     535         [ +  - ]:      549265 :                             sim[i] = std::nullopt;
     536                 :             :                         } else {
     537                 :      164001 :                             sim[i]->second -= del;
     538                 :             :                         }
     539                 :             :                     }
     540                 :             :                 }
     541                 :             :                 break;
     542         [ +  + ]:       44359 :             } else if (command-- == 0) {
     543                 :             :                 // Compact.
     544         [ -  + ]:       38290 :                 const size_t mem_before{real.DynamicMemoryUsage()};
     545                 :       38290 :                 real.Compact();
     546         [ -  + ]:       38290 :                 const size_t mem_after{real.DynamicMemoryUsage()};
     547   [ -  +  +  +  :       38290 :                 assert(real.PositionRange() < last_compaction_pos ? mem_after < mem_before : mem_after <= mem_before);
                   -  + ]
     548                 :             :                 last_compaction_pos = real.PositionRange();
     549                 :             :                 break;
     550                 :             :             }
     551                 :             :         }
     552                 :             :     }
     553                 :             : 
     554                 :             :     // Compare the real obtained depgraph against the simulation.
     555                 :         506 :     anc_update_fn();
     556         [ +  + ]:       16698 :     for (DepGraphIndex i = 0; i < sim.size(); ++i) check_fn(i);
     557         [ -  + ]:         506 :     assert(real.TxCount() == num_tx_sim);
     558                 :             :     // Sanity check the result (which includes round-tripping serialization, if applicable).
     559         [ +  - ]:         506 :     SanityCheck(real);
     560                 :         506 : }
     561                 :             : 
     562         [ +  - ]:         762 : FUZZ_TARGET(clusterlin_depgraph_serialization)
     563                 :             : {
     564                 :             :     // Verify that any deserialized depgraph is acyclic and roundtrips to an identical depgraph.
     565                 :             : 
     566                 :             :     // Construct a graph by deserializing.
     567                 :         308 :     SpanReader reader(buffer);
     568                 :         308 :     DepGraph<TestBitSet> depgraph;
     569                 :         308 :     DepGraphIndex par_code{0}, chl_code{0};
     570                 :         308 :     try {
     571   [ +  -  +  +  :         308 :         reader >> Using<DepGraphFormatter>(depgraph) >> VARINT(par_code) >> VARINT(chl_code);
                   +  + ]
     572         [ -  + ]:         264 :     } catch (const std::ios_base::failure&) {}
     573         [ +  - ]:         308 :     SanityCheck(depgraph);
     574                 :             : 
     575                 :             :     // Verify the graph is a DAG.
     576         [ -  + ]:         308 :     assert(depgraph.IsAcyclic());
     577                 :             : 
     578                 :             :     // Introduce a cycle, and then test that IsAcyclic returns false.
     579         [ +  + ]:         308 :     if (depgraph.TxCount() < 2) return;
     580                 :         286 :     DepGraphIndex par(0), chl(0);
     581                 :             :     // Pick any transaction of depgraph as parent.
     582         [ +  - ]:         286 :     par_code %= depgraph.TxCount();
     583   [ +  -  +  - ]:         974 :     for (auto i : depgraph.Positions()) {
     584         [ +  + ]:         688 :         if (par_code == 0) {
     585                 :             :             par = i;
     586                 :             :             break;
     587                 :             :         }
     588                 :         402 :         --par_code;
     589                 :             :     }
     590                 :             :     // Pick any ancestor of par (excluding itself) as child, if any.
     591         [ +  + ]:         286 :     auto ancestors = depgraph.Ancestors(par) - TestBitSet::Singleton(par);
     592         [ +  + ]:         286 :     if (ancestors.None()) return;
     593                 :         152 :     chl_code %= ancestors.Count();
     594         [ +  - ]:         310 :     for (auto i : ancestors) {
     595         [ +  + ]:         310 :         if (chl_code == 0) {
     596                 :             :             chl = i;
     597                 :             :             break;
     598                 :             :         }
     599                 :         158 :         --chl_code;
     600                 :             :     }
     601                 :             :     // Add the cycle-introducing dependency.
     602                 :         152 :     depgraph.AddDependencies(TestBitSet::Singleton(par), chl);
     603                 :             :     // Check that we now detect a cycle.
     604         [ -  + ]:         152 :     assert(!depgraph.IsAcyclic());
     605                 :         308 : }
     606                 :             : 
     607         [ +  - ]:         611 : FUZZ_TARGET(clusterlin_components)
     608                 :             : {
     609                 :             :     // Verify the behavior of DepGraphs's FindConnectedComponent and IsConnected functions.
     610                 :             : 
     611                 :             :     // Construct a depgraph.
     612                 :         157 :     SpanReader reader(buffer);
     613                 :         157 :     DepGraph<TestBitSet> depgraph;
     614                 :         157 :     std::vector<DepGraphIndex> linearization;
     615                 :         157 :     try {
     616         [ +  - ]:         157 :         reader >> Using<DepGraphFormatter>(depgraph);
     617         [ -  - ]:           0 :     } catch (const std::ios_base::failure&) {}
     618                 :             : 
     619                 :         157 :     TestBitSet todo = depgraph.Positions();
     620         [ +  + ]:        1817 :     while (todo.Any()) {
     621                 :             :         // Pick a transaction in todo, or nothing.
     622                 :        1660 :         std::optional<DepGraphIndex> picked;
     623                 :        1660 :         {
     624                 :        1660 :             uint64_t picked_num{0};
     625                 :        1660 :             try {
     626         [ +  + ]:        1660 :                 reader >> VARINT(picked_num);
     627         [ -  + ]:        1123 :             } catch (const std::ios_base::failure&) {}
     628   [ +  +  +  + ]:        1660 :             if (picked_num < todo.Size() && todo[picked_num]) {
     629                 :         316 :                 picked = picked_num;
     630                 :             :             }
     631                 :             :         }
     632                 :             : 
     633                 :             :         // Find a connected component inside todo, including picked if any.
     634         [ +  + ]:        1660 :         auto component = picked ? depgraph.GetConnectedComponent(todo, *picked)
     635                 :        1344 :                                 : depgraph.FindConnectedComponent(todo);
     636                 :             : 
     637                 :             :         // The component must be a subset of todo and non-empty.
     638         [ -  + ]:        1660 :         assert(component.IsSubsetOf(todo));
     639         [ -  + ]:        1660 :         assert(component.Any());
     640                 :             : 
     641                 :             :         // If picked was provided, the component must include it.
     642   [ +  +  -  + ]:        1660 :         if (picked) assert(component[*picked]);
     643                 :             : 
     644                 :             :         // If todo is the entire graph, and the entire graph is connected, then the component must
     645                 :             :         // be the entire graph.
     646         [ +  + ]:        1660 :         if (todo == depgraph.Positions()) {
     647   [ +  +  -  + ]:         244 :             assert((component == todo) == depgraph.IsConnected());
     648                 :             :         }
     649                 :             : 
     650                 :             :         // If subset is connected, then component must match subset.
     651   [ +  +  -  + ]:        2857 :         assert((component == todo) == depgraph.IsConnected(todo));
     652                 :             : 
     653                 :             :         // The component cannot have any ancestors or descendants outside of component but in todo.
     654   [ +  -  +  + ]:       12617 :         for (auto i : component) {
     655         [ -  + ]:        9297 :             assert((depgraph.Ancestors(i) & todo).IsSubsetOf(component));
     656         [ -  + ]:        9297 :             assert((depgraph.Descendants(i) & todo).IsSubsetOf(component));
     657                 :             :         }
     658                 :             : 
     659                 :             :         // Starting from any component element, we must be able to reach every element.
     660   [ +  -  +  + ]:       12617 :         for (auto i : component) {
     661                 :             :             // Start with just i as reachable.
     662                 :        9297 :             TestBitSet reachable = TestBitSet::Singleton(i);
     663                 :             :             // Add in-todo descendants and ancestors to reachable until it does not change anymore.
     664                 :       56831 :             while (true) {
     665                 :       33064 :                 TestBitSet new_reachable = reachable;
     666   [ +  -  +  + ]:      390322 :                 for (auto j : new_reachable) {
     667                 :      324194 :                     new_reachable |= depgraph.Ancestors(j) & todo;
     668                 :      324194 :                     new_reachable |= depgraph.Descendants(j) & todo;
     669                 :             :                 }
     670         [ +  + ]:       33064 :                 if (new_reachable == reachable) break;
     671                 :       23767 :                 reachable = new_reachable;
     672                 :       23767 :             }
     673                 :             :             // Verify that the result is the entire component.
     674         [ -  + ]:        9297 :             assert(component == reachable);
     675                 :             :         }
     676                 :             : 
     677                 :             :         // Construct an arbitrary subset of todo.
     678                 :        1660 :         uint64_t subset_bits{0};
     679                 :        1660 :         try {
     680         [ +  + ]:        1660 :             reader >> VARINT(subset_bits);
     681         [ -  + ]:        1147 :         } catch (const std::ios_base::failure&) {}
     682                 :        1660 :         TestBitSet subset;
     683   [ +  -  +  + ]:       40306 :         for (DepGraphIndex i : depgraph.Positions()) {
     684         [ +  + ]:       36986 :             if (todo[i]) {
     685         [ +  + ]:       19415 :                 if (subset_bits & 1) subset.Set(i);
     686                 :       19415 :                 subset_bits >>= 1;
     687                 :             :             }
     688                 :             :         }
     689                 :             :         // Which must be non-empty.
     690         [ +  + ]:        1660 :         if (subset.None()) subset = TestBitSet::Singleton(todo.First());
     691                 :             :         // Remove it from todo.
     692                 :        1660 :         todo -= subset;
     693                 :             :     }
     694                 :             : 
     695                 :             :     // No components can be found in an empty subset.
     696         [ -  + ]:         157 :     assert(depgraph.FindConnectedComponent(todo).None());
     697                 :         157 : }
     698                 :             : 
     699         [ +  - ]:         739 : FUZZ_TARGET(clusterlin_make_connected)
     700                 :             : {
     701                 :             :     // Verify that MakeConnected makes graphs connected.
     702                 :             : 
     703                 :         285 :     SpanReader reader(buffer);
     704                 :         285 :     DepGraph<TestBitSet> depgraph;
     705                 :         285 :     try {
     706         [ +  - ]:         285 :         reader >> Using<DepGraphFormatter>(depgraph);
     707         [ -  - ]:           0 :     } catch (const std::ios_base::failure&) {}
     708         [ +  - ]:         285 :     MakeConnected(depgraph);
     709         [ +  - ]:         285 :     SanityCheck(depgraph);
     710         [ -  + ]:         285 :     assert(depgraph.IsConnected());
     711                 :         285 : }
     712                 :             : 
     713         [ +  - ]:         617 : FUZZ_TARGET(clusterlin_chunking)
     714                 :             : {
     715                 :             :     // Verify the correctness of the ChunkLinearization function.
     716                 :             : 
     717                 :             :     // Construct a graph by deserializing.
     718                 :         163 :     SpanReader reader(buffer);
     719                 :         163 :     DepGraph<TestBitSet> depgraph;
     720                 :         163 :     try {
     721         [ +  - ]:         163 :         reader >> Using<DepGraphFormatter>(depgraph);
     722         [ -  - ]:           0 :     } catch (const std::ios_base::failure&) {}
     723                 :             : 
     724                 :             :     // Read a valid linearization for depgraph.
     725         [ +  - ]:         163 :     auto linearization = ReadLinearization(depgraph, reader);
     726                 :             : 
     727                 :             :     // Invoke the chunking functions.
     728         [ -  + ]:         163 :     auto chunking = ChunkLinearization(depgraph, linearization);
     729         [ -  + ]:         163 :     auto chunking_info = ChunkLinearizationInfo(depgraph, linearization);
     730                 :             : 
     731                 :             :     // Verify consistency between the two functions.
     732   [ -  +  -  +  :         163 :     assert(chunking.size() == chunking_info.size());
                   -  + ]
     733   [ -  +  +  + ]:         772 :     for (size_t i = 0; i < chunking.size(); ++i) {
     734         [ +  - ]:         609 :         assert(chunking[i] == chunking_info[i].feerate);
     735         [ +  - ]:        1218 :         assert(SetInfo(depgraph, chunking_info[i].transactions) == chunking_info[i]);
     736                 :             :     }
     737                 :             : 
     738                 :             :     // Verify that chunk feerates are monotonically non-increasing.
     739         [ +  + ]:         618 :     for (size_t i = 1; i < chunking.size(); ++i) {
     740         [ -  + ]:         455 :         assert(!(chunking[i] >> chunking[i - 1]));
     741                 :             :     }
     742                 :             : 
     743                 :             :     // Naively recompute the chunks (each is the highest-feerate prefix of what remains).
     744                 :         163 :     auto todo = depgraph.Positions();
     745         [ +  + ]:         772 :     for (const auto& [chunk_set, chunk_feerate] : chunking_info) {
     746         [ -  + ]:         609 :         assert(todo.Any());
     747                 :         609 :         SetInfo<TestBitSet> accumulator, best;
     748         [ +  + ]:       12674 :         for (DepGraphIndex idx : linearization) {
     749         [ +  + ]:       12065 :             if (todo[idx]) {
     750                 :        6737 :                 accumulator.Set(depgraph, idx);
     751   [ +  +  +  + ]:        6737 :                 if (best.feerate.IsEmpty() || accumulator.feerate >> best.feerate) {
     752                 :        1400 :                     best = accumulator;
     753                 :             :                 }
     754                 :             :             }
     755                 :             :         }
     756         [ +  - ]:         609 :         assert(chunk_feerate == best.feerate);
     757         [ -  + ]:         609 :         assert(chunk_set == best.transactions);
     758         [ -  + ]:         609 :         assert(best.transactions.IsSubsetOf(todo));
     759                 :         609 :         todo -= best.transactions;
     760                 :             :     }
     761         [ -  + ]:         163 :     assert(todo.None());
     762                 :         163 : }
     763                 :             : 
     764                 :             : static constexpr auto MAX_SIMPLE_ITERATIONS = 300000;
     765                 :             : 
     766         [ +  - ]:         714 : FUZZ_TARGET(clusterlin_simple_finder)
     767                 :             : {
     768                 :             :     // Verify that SimpleCandidateFinder works as expected by sanity checking the results
     769                 :             :     // and comparing them (if claimed to be optimal) against the sets found by
     770                 :             :     // ExhaustiveCandidateFinder.
     771                 :             :     //
     772                 :             :     // Note that SimpleCandidateFinder is only used in tests; the purpose of this fuzz test is to
     773                 :             :     // establish confidence in SimpleCandidateFinder, so that it can be used in SimpleLinearize,
     774                 :             :     // which is then used to test Linearize below.
     775                 :             : 
     776                 :             :     // Retrieve a depgraph from the fuzz input.
     777                 :         260 :     SpanReader reader(buffer);
     778                 :         260 :     DepGraph<TestBitSet> depgraph;
     779                 :         260 :     try {
     780         [ +  - ]:         260 :         reader >> Using<DepGraphFormatter>(depgraph);
     781         [ -  - ]:           0 :     } catch (const std::ios_base::failure&) {}
     782                 :             : 
     783                 :             :     // Instantiate the SimpleCandidateFinder to be tested, and the ExhaustiveCandidateFinder it is
     784                 :             :     // being tested against.
     785                 :         260 :     SimpleCandidateFinder smp_finder(depgraph);
     786                 :         260 :     ExhaustiveCandidateFinder exh_finder(depgraph);
     787                 :             : 
     788                 :         260 :     auto todo = depgraph.Positions();
     789         [ +  + ]:        2636 :     while (todo.Any()) {
     790         [ -  + ]:        2376 :         assert(!smp_finder.AllDone());
     791         [ -  + ]:        2376 :         assert(!exh_finder.AllDone());
     792                 :             : 
     793                 :             :         // Call SimpleCandidateFinder.
     794         [ -  + ]:        2376 :         auto [found, iterations_done] = smp_finder.FindCandidateSet(MAX_SIMPLE_ITERATIONS);
     795                 :        2376 :         bool optimal = (iterations_done != MAX_SIMPLE_ITERATIONS);
     796                 :             : 
     797                 :             :         // Sanity check the result.
     798         [ -  + ]:        2376 :         assert(iterations_done <= MAX_SIMPLE_ITERATIONS);
     799         [ -  + ]:        2376 :         assert(found.transactions.Any());
     800         [ -  + ]:        2376 :         assert(found.transactions.IsSubsetOf(todo));
     801         [ +  - ]:        2376 :         assert(depgraph.FeeRate(found.transactions) == found.feerate);
     802                 :             :         // Check that it is topologically valid.
     803   [ +  -  +  + ]:        9436 :         for (auto i : found.transactions) {
     804         [ -  + ]:        4684 :             assert(found.transactions.IsSupersetOf(depgraph.Ancestors(i) & todo));
     805                 :             :         }
     806                 :             : 
     807                 :             :         // At most 2^(N-1) iterations can be required: the number of non-empty connected subsets a
     808                 :             :         // graph with N transactions can have. If MAX_SIMPLE_ITERATIONS exceeds this number, the
     809                 :             :         // result is necessarily optimal.
     810         [ -  + ]:        2376 :         assert(iterations_done <= (uint64_t{1} << (todo.Count() - 1)));
     811   [ +  +  -  + ]:        2376 :         if (MAX_SIMPLE_ITERATIONS > (uint64_t{1} << (todo.Count() - 1))) assert(optimal);
     812                 :             : 
     813                 :             :         // SimpleCandidateFinder only finds connected sets.
     814         [ -  + ]:        2376 :         assert(depgraph.IsConnected(found.transactions));
     815                 :             : 
     816                 :             :         // Perform further quality checks only if SimpleCandidateFinder claims an optimal result.
     817         [ +  + ]:        2376 :         if (optimal) {
     818         [ +  + ]:        2332 :             if (todo.Count() <= 12) {
     819                 :             :                 // Compare with ExhaustiveCandidateFinder. This quickly gets computationally
     820                 :             :                 // expensive for large clusters (O(2^n)), so only do it for sufficiently small ones.
     821                 :        1472 :                 auto exhaustive = exh_finder.FindCandidateSet();
     822         [ +  - ]:        1472 :                 assert(exhaustive.feerate == found.feerate);
     823                 :             :             }
     824                 :             : 
     825                 :             :             // Compare with a non-empty topological set read from the fuzz input (comparing with an
     826                 :             :             // empty set is not interesting).
     827         [ +  - ]:        2332 :             auto read_topo = ReadTopologicalSet(depgraph, todo, reader, /*non_empty=*/true);
     828         [ -  + ]:        2332 :             assert(found.feerate >= depgraph.FeeRate(read_topo));
     829                 :             :         }
     830                 :             : 
     831                 :             :         // Find a non-empty topologically valid subset of transactions to remove from the graph.
     832                 :             :         // Using an empty set would mean the next iteration is identical to the current one, and
     833                 :             :         // could cause an infinite loop.
     834         [ +  - ]:        2376 :         auto del_set = ReadTopologicalSet(depgraph, todo, reader, /*non_empty=*/true);
     835                 :        2376 :         todo -= del_set;
     836                 :        2376 :         smp_finder.MarkDone(del_set);
     837                 :        2376 :         exh_finder.MarkDone(del_set);
     838                 :             :     }
     839                 :             : 
     840         [ -  + ]:         260 :     assert(smp_finder.AllDone());
     841         [ -  + ]:         260 :     assert(exh_finder.AllDone());
     842                 :         260 : }
     843                 :             : 
     844         [ +  - ]:         866 : FUZZ_TARGET(clusterlin_simple_linearize)
     845                 :             : {
     846                 :             :     // Verify the behavior of SimpleLinearize(). Note that SimpleLinearize is only used in tests;
     847                 :             :     // the purpose of this fuzz test is to establish confidence in SimpleLinearize, so that it can
     848                 :             :     // be used to test the real Linearize function in the fuzz test below.
     849                 :             : 
     850                 :             :     // Retrieve an iteration count and a depgraph from the fuzz input.
     851                 :         412 :     SpanReader reader(buffer);
     852                 :         412 :     uint64_t iter_count{0};
     853                 :         412 :     DepGraph<TestBitSet> depgraph;
     854                 :         412 :     try {
     855   [ +  +  +  - ]:         412 :         reader >> VARINT(iter_count) >> Using<DepGraphFormatter>(depgraph);
     856         [ -  + ]:           5 :     } catch (const std::ios_base::failure&) {}
     857                 :         412 :     iter_count %= MAX_SIMPLE_ITERATIONS;
     858                 :             : 
     859                 :             :     // Invoke SimpleLinearize().
     860         [ -  + ]:         412 :     auto [linearization, optimal] = SimpleLinearize(depgraph, iter_count);
     861         [ -  + ]:         412 :     SanityCheck(depgraph, linearization);
     862         [ -  + ]:         412 :     auto simple_chunking = ChunkLinearization(depgraph, linearization);
     863                 :             : 
     864                 :             :     // If the iteration count is sufficiently high, an optimal linearization must be found.
     865                 :             :     // SimpleLinearize on k transactions can take up to 2^(k-1) iterations (one per non-empty
     866                 :             :     // connected topologically valid subset), which sums over k=1..n to (2^n)-1.
     867         [ +  - ]:         412 :     const uint64_t n = depgraph.TxCount();
     868   [ +  -  +  + ]:         412 :     if (n <= 63 && (iter_count >> n)) {
     869         [ -  + ]:          96 :         assert(optimal);
     870                 :             :     }
     871                 :             : 
     872                 :             :     // If SimpleLinearize claims optimal result, and the cluster is sufficiently small (there are
     873                 :             :     // n! linearizations), test that the result is as good as every valid linearization.
     874   [ +  +  +  + ]:         412 :     if (optimal && depgraph.TxCount() <= 8) {
     875         [ +  - ]:         168 :         auto exh_linearization = ExhaustiveLinearize(depgraph);
     876         [ -  + ]:         168 :         auto exh_chunking = ChunkLinearization(depgraph, exh_linearization);
     877   [ -  +  -  +  :         168 :         auto cmp = CompareChunks(simple_chunking, exh_chunking);
                   +  - ]
     878         [ -  + ]:         168 :         assert(cmp == 0);
     879   [ -  +  -  +  :         168 :         assert(simple_chunking.size() == exh_chunking.size());
                   -  + ]
     880                 :         168 :     }
     881                 :             : 
     882         [ +  + ]:         412 :     if (optimal) {
     883                 :             :         // Compare with a linearization read from the fuzz input.
     884         [ +  - ]:         320 :         auto read = ReadLinearization(depgraph, reader);
     885         [ -  + ]:         320 :         auto read_chunking = ChunkLinearization(depgraph, read);
     886   [ -  +  -  +  :         320 :         auto cmp = CompareChunks(simple_chunking, read_chunking);
                   +  - ]
     887         [ -  + ]:         320 :         assert(cmp >= 0);
     888                 :         320 :     }
     889                 :         412 : }
     890                 :             : 
     891         [ +  - ]:        1333 : FUZZ_TARGET(clusterlin_sfl)
     892                 :             : {
     893                 :             :     // Verify the individual steps of the SFL algorithm.
     894                 :             : 
     895                 :         879 :     SpanReader reader(buffer);
     896                 :         879 :     DepGraph<TestBitSet> depgraph;
     897                 :         879 :     uint8_t flags{1};
     898                 :         879 :     uint64_t rng_seed{0};
     899                 :         879 :     try {
     900   [ +  +  +  +  :         879 :         reader >> rng_seed >> flags >> Using<DepGraphFormatter>(depgraph);
                   +  - ]
     901         [ -  + ]:           3 :     } catch (const std::ios_base::failure&) {}
     902         [ +  + ]:         879 :     if (depgraph.TxCount() <= 1) return;
     903                 :         864 :     InsecureRandomContext rng(rng_seed);
     904                 :             :     /** Whether to make the depgraph connected. */
     905                 :         864 :     const bool make_connected = flags & 1;
     906                 :             :     /** Whether to load some input linearization into the state. */
     907                 :         864 :     const bool load_linearization = flags & 2;
     908                 :             :     /** Whether that input linearization is topological. */
     909   [ +  +  +  + ]:         864 :     const bool load_topological = load_linearization && (flags & 4);
     910                 :             : 
     911                 :             :     // Initialize SFL state.
     912   [ +  +  +  - ]:         864 :     if (make_connected) MakeConnected(depgraph);
     913                 :         864 :     SpanningForestState sfl(depgraph, rng.rand64());
     914                 :             : 
     915                 :             :     // Function to test the state.
     916                 :         864 :     std::vector<FeeFrac> last_diagram;
     917                 :         864 :     bool was_optimal{false};
     918                 :       33803 :     auto test_fn = [&](bool is_optimal = false, bool is_minimal = false) {
     919         [ +  + ]:       32939 :         if (rng.randbits(4) == 0) {
     920                 :             :             // Perform sanity checks from time to time (too computationally expensive to do after
     921                 :             :             // every step).
     922                 :        3455 :             sfl.SanityCheck();
     923                 :             :         }
     924                 :       32939 :         auto diagram = sfl.GetDiagram();
     925         [ +  + ]:       32939 :         if (rng.randbits(4) == 0) {
     926                 :             :             // Verify that the diagram of GetLinearization() is at least as good as GetDiagram(),
     927                 :             :             // from time to time.
     928                 :        3003 :             auto lin = sfl.GetLinearization(IndexTxOrder{});
     929         [ -  + ]:        3003 :             auto lin_diagram = ChunkLinearization(depgraph, lin);
     930   [ -  +  -  +  :        3003 :             auto cmp_lin = CompareChunks(lin_diagram, diagram);
                   +  - ]
     931         [ -  + ]:        3003 :             assert(cmp_lin >= 0);
     932                 :             :             // If we're in an allegedly optimal state, they must match.
     933   [ +  +  -  + ]:        3003 :             if (is_optimal) assert(cmp_lin == 0);
     934                 :             :             // If we're in an allegedly minimal state, they must also have the same number of
     935                 :             :             // segments.
     936   [ +  +  -  +  :        3003 :             if (is_minimal) assert(diagram.size() == lin_diagram.size());
             -  +  -  + ]
     937                 :        3003 :         }
     938                 :             :         // Verify that subsequent calls to GetDiagram() never get worse/incomparable.
     939         [ +  + ]:       32939 :         if (!last_diagram.empty()) {
     940   [ -  +  -  +  :       32249 :             auto cmp = CompareChunks(diagram, last_diagram);
                   +  - ]
     941         [ -  + ]:       32249 :             assert(cmp >= 0);
     942                 :             :             // If the last diagram was already optimal, the new one cannot be better.
     943   [ +  +  -  + ]:       32249 :             if (was_optimal) assert(cmp == 0);
     944                 :             :             // Also, if the diagram was already optimal, the number of segments can only increase.
     945   [ +  +  -  +  :       32249 :             if (was_optimal) assert(diagram.size() >= last_diagram.size());
             -  +  -  + ]
     946                 :             :         }
     947                 :       32939 :         last_diagram = std::move(diagram);
     948                 :       32939 :         was_optimal = is_optimal;
     949                 :       33803 :     };
     950                 :             : 
     951         [ +  + ]:         864 :     if (load_linearization) {
     952         [ +  - ]:         322 :         auto input_lin = ReadLinearization(depgraph, reader, load_topological);
     953         [ -  + ]:         322 :         sfl.LoadLinearization(input_lin);
     954         [ +  + ]:         322 :         if (load_topological) {
     955                 :             :             // The diagram of the loaded linearization forms an initial lower bound on future
     956                 :             :             // diagrams.
     957         [ -  + ]:         174 :             last_diagram = ChunkLinearization(depgraph, input_lin);
     958                 :             :         } else {
     959                 :             :             // The input linearization may have been non-topological, so invoke MakeTopological to
     960                 :             :             // fix it still.
     961                 :         148 :             sfl.MakeTopological();
     962                 :             :         }
     963                 :         322 :     } else {
     964                 :             :         // Invoke MakeTopological to create an initial from-scratch topological state.
     965                 :         542 :         sfl.MakeTopological();
     966                 :             :     }
     967                 :             : 
     968                 :             :     // Loop until optimal.
     969         [ +  - ]:         864 :     test_fn();
     970                 :         864 :     sfl.StartOptimizing();
     971                 :       14458 :     while (true) {
     972         [ +  - ]:       14458 :         test_fn();
     973         [ +  + ]:       14458 :         if (!sfl.OptimizeStep()) break;
     974                 :             :     }
     975                 :             : 
     976                 :             :     // Loop until minimal.
     977         [ +  - ]:         864 :     test_fn(/*is_optimal=*/true);
     978                 :         864 :     sfl.StartMinimizing();
     979                 :       15889 :     while (true) {
     980         [ +  - ]:       15889 :         test_fn(/*is_optimal=*/true);
     981         [ +  + ]:       15889 :         if (!sfl.MinimizeStep()) break;
     982                 :             :     }
     983         [ +  - ]:         864 :     test_fn(/*is_optimal=*/true, /*is_minimal=*/true);
     984                 :             : 
     985                 :             :     // Verify that optimality is reached within an expected amount of work. This protects against
     986                 :             :     // hypothetical bugs that hugely increase the amount of work needed to reach optimality.
     987         [ -  + ]:         864 :     assert(sfl.GetCost() <= MaxOptimalLinearizationIters(depgraph.TxCount()));
     988                 :             : 
     989                 :             :     // The result must be as good as SimpleLinearize.
     990         [ -  + ]:         864 :     auto [simple_linearization, simple_optimal] = SimpleLinearize(depgraph, MAX_SIMPLE_ITERATIONS / 10);
     991         [ -  + ]:         864 :     auto simple_diagram = ChunkLinearization(depgraph, simple_linearization);
     992   [ -  +  -  +  :         864 :     auto simple_cmp = CompareChunks(last_diagram, simple_diagram);
                   +  - ]
     993         [ -  + ]:         864 :     assert(simple_cmp >= 0);
     994   [ +  +  -  + ]:         864 :     if (simple_optimal) assert(simple_cmp == 0);
     995                 :             :     // If the diagram matches, we must also have at least as many segments (because the SFL state
     996                 :             :     // and its produced diagram are minimal);
     997   [ +  +  -  +  :         864 :     if (simple_cmp == 0) assert(last_diagram.size() >= simple_diagram.size());
             -  +  +  - ]
     998                 :             : 
     999                 :             :     // We can compare with any arbitrary linearization, and the diagram must be at least as good as
    1000                 :             :     // each.
    1001         [ +  + ]:        9504 :     for (int i = 0; i < 10; ++i) {
    1002         [ +  - ]:        8640 :         auto read_lin = ReadLinearization(depgraph, reader);
    1003         [ -  + ]:        8640 :         auto read_diagram = ChunkLinearization(depgraph, read_lin);
    1004   [ -  +  -  +  :        8640 :         auto cmp = CompareChunks(last_diagram, read_diagram);
                   +  - ]
    1005         [ -  + ]:        8640 :         assert(cmp >= 0);
    1006   [ +  +  -  +  :        8640 :         if (cmp == 0) assert(last_diagram.size() >= read_diagram.size());
             -  +  -  + ]
    1007                 :        8640 :     }
    1008                 :         879 : }
    1009                 :             : 
    1010         [ +  - ]:        1312 : FUZZ_TARGET(clusterlin_linearize)
    1011                 :             : {
    1012                 :             :     // Verify the behavior of Linearize().
    1013                 :             : 
    1014                 :             :     // Retrieve an RNG seed, an iteration count, a depgraph, and whether to make it connected from
    1015                 :             :     // the fuzz input.
    1016                 :         858 :     SpanReader reader(buffer);
    1017                 :         858 :     DepGraph<TestBitSet> depgraph;
    1018                 :         858 :     uint64_t rng_seed{0};
    1019                 :         858 :     uint64_t iter_count{0};
    1020                 :         858 :     uint8_t flags{7};
    1021                 :         858 :     try {
    1022   [ +  +  +  -  :         858 :         reader >> VARINT(iter_count) >> Using<DepGraphFormatter>(depgraph) >> rng_seed >> flags;
             +  +  +  + ]
    1023         [ -  + ]:         624 :     } catch (const std::ios_base::failure&) {}
    1024                 :         858 :     bool make_connected = flags & 1;
    1025                 :             :     // The following 3 booleans have 4 combinations:
    1026                 :             :     // - (flags & 6) == 0: do not provide input linearization.
    1027                 :             :     // - (flags & 6) == 2: provide potentially non-topological input.
    1028                 :             :     // - (flags & 6) == 4: provide topological input linearization, but do not claim it is
    1029                 :             :     //                     topological.
    1030                 :             :     // - (flags & 6) == 6: provide topological input linearization, and claim it is topological.
    1031                 :         858 :     bool provide_input = flags & 6;
    1032                 :         858 :     bool provide_topological_input = flags & 4;
    1033                 :         858 :     bool claim_topological_input = (flags & 6) == 6;
    1034                 :             :     // The most complicated graphs are connected ones (other ones just split up). Optionally force
    1035                 :             :     // the graph to be connected.
    1036   [ +  +  +  - ]:         858 :     if (make_connected) MakeConnected(depgraph);
    1037                 :             : 
    1038                 :             :     // Optionally construct an old linearization for it.
    1039                 :         858 :     std::vector<DepGraphIndex> old_linearization;
    1040         [ +  + ]:         858 :     if (provide_input) {
    1041         [ +  - ]:        1500 :         old_linearization = ReadLinearization(depgraph, reader, /*topological=*/provide_topological_input);
    1042   [ +  +  -  + ]:         750 :         if (provide_topological_input) SanityCheck(depgraph, old_linearization);
    1043                 :             :     }
    1044                 :             : 
    1045                 :             :     // Invoke Linearize().
    1046                 :         858 :     iter_count &= 0x7ffff;
    1047   [ -  +  -  + ]:         858 :     auto [linearization, optimal, cost] = Linearize(depgraph, iter_count, rng_seed, IndexTxOrder{}, old_linearization, /*is_topological=*/claim_topological_input);
    1048         [ -  + ]:         858 :     SanityCheck(depgraph, linearization);
    1049         [ -  + ]:         858 :     auto chunking = ChunkLinearization(depgraph, linearization);
    1050                 :             : 
    1051                 :             :     // Linearization must always be as good as the old one, if provided and topological (even when
    1052                 :             :     // not claimed to be topological).
    1053         [ +  + ]:         858 :     if (provide_topological_input) {
    1054         [ -  + ]:         705 :         auto old_chunking = ChunkLinearization(depgraph, old_linearization);
    1055   [ -  +  -  +  :         705 :         auto cmp = CompareChunks(chunking, old_chunking);
                   +  - ]
    1056         [ -  + ]:         705 :         assert(cmp >= 0);
    1057                 :         705 :     }
    1058                 :             : 
    1059                 :             :     // If the iteration count is sufficiently high, an optimal linearization must be found.
    1060         [ +  + ]:         858 :     if (iter_count > MaxOptimalLinearizationIters(depgraph.TxCount())) {
    1061         [ -  + ]:         179 :         assert(optimal);
    1062                 :             :     }
    1063                 :             : 
    1064                 :             :     // If Linearize claims optimal result, run quality tests.
    1065         [ +  + ]:         858 :     if (optimal) {
    1066                 :             :         // It must be as good as SimpleLinearize.
    1067         [ -  + ]:         538 :         auto [simple_linearization, simple_optimal] = SimpleLinearize(depgraph, MAX_SIMPLE_ITERATIONS);
    1068         [ -  + ]:         538 :         SanityCheck(depgraph, simple_linearization);
    1069         [ -  + ]:         538 :         auto simple_chunking = ChunkLinearization(depgraph, simple_linearization);
    1070   [ -  +  -  +  :         538 :         auto cmp = CompareChunks(chunking, simple_chunking);
                   +  - ]
    1071         [ -  + ]:         538 :         assert(cmp >= 0);
    1072                 :             :         // If SimpleLinearize finds the optimal result too, they must be equal (if not,
    1073                 :             :         // SimpleLinearize is broken).
    1074   [ +  +  -  + ]:         538 :         if (simple_optimal) assert(cmp == 0);
    1075                 :             : 
    1076                 :             :         // If simple_chunking is diagram-optimal, it cannot have more chunks than chunking (as
    1077                 :             :         // chunking is claimed to be optimal, which implies minimal chunks).
    1078   [ +  +  -  +  :         538 :         if (cmp == 0) assert(chunking.size() >= simple_chunking.size());
             -  +  -  + ]
    1079                 :             : 
    1080                 :             :         // Compare with a linearization read from the fuzz input.
    1081         [ +  - ]:         538 :         auto read = ReadLinearization(depgraph, reader);
    1082         [ -  + ]:         538 :         auto read_chunking = ChunkLinearization(depgraph, read);
    1083   [ -  +  -  +  :         538 :         auto cmp_read = CompareChunks(chunking, read_chunking);
                   +  - ]
    1084         [ -  + ]:         538 :         assert(cmp_read >= 0);
    1085                 :             : 
    1086                 :             :         // Verify that within every chunk, the transactions are in a valid order. For any pair of
    1087                 :             :         // transactions, it should not be possible to swap them; either due to a missing
    1088                 :             :         // dependency, or because the order would be inconsistent with decreasing feerate,
    1089                 :             :         // increasing size, and fallback order (just DepGraphIndex value here).
    1090         [ -  + ]:         538 :         auto chunking_info = ChunkLinearizationInfo(depgraph, linearization);
    1091                 :             :         /** The set of all transactions (strictly) before tx1 (see below), or (strictly) before
    1092                 :             :          *  chunk1 (see even further below). */
    1093                 :         538 :         TestBitSet done;
    1094                 :         538 :         unsigned pos{0};
    1095         [ +  + ]:        5975 :         for (const auto& chunk : chunking_info) {
    1096                 :        5437 :             auto chunk_start = pos;
    1097                 :        5437 :             auto chunk_end = pos + chunk.transactions.Count() - 1;
    1098                 :             :             // Go over all pairs of transactions. done is the set of transactions seen before pos1.
    1099         [ +  + ]:       17497 :             for (unsigned pos1 = chunk_start; pos1 <= chunk_end; ++pos1) {
    1100                 :       12060 :                 auto tx1 = linearization[pos1];
    1101         [ +  + ]:       65557 :                 for (unsigned pos2 = pos1 + 1; pos2 <= chunk_end; ++pos2) {
    1102         [ +  + ]:       53497 :                     auto tx2 = linearization[pos2];
    1103                 :             :                     // Check whether tx2 only depends on transactions that precede tx1.
    1104         [ +  + ]:       53497 :                     if ((depgraph.Ancestors(tx2) - done).Count() == 1) {
    1105                 :             :                         // tx2 could take position pos1.
    1106                 :             :                         // Verify that individual transaction feerate is decreasing (note that >=
    1107                 :             :                         // tie-breaks by size).
    1108         [ -  + ]:       12423 :                         assert(depgraph.FeeRate(tx1) >= depgraph.FeeRate(tx2));
    1109                 :             :                         // If feerate and size are equal, compare by DepGraphIndex.
    1110         [ +  + ]:       58273 :                         if (depgraph.FeeRate(tx1) == depgraph.FeeRate(tx2)) {
    1111         [ -  + ]:        3816 :                             assert(tx1 < tx2);
    1112                 :             :                         }
    1113                 :             :                     }
    1114                 :             :                 }
    1115                 :       12060 :                 done.Set(tx1);
    1116                 :             :             }
    1117                 :        5437 :             pos += chunk.transactions.Count();
    1118                 :             :         }
    1119                 :             : 
    1120                 :             :         // Verify that chunks themselves are in a valid order. For any pair of chunks, it should
    1121                 :             :         // not be possible to swap them; either due to a missing dependency, or because the order
    1122                 :             :         // would be inconsistent with decreasing chunk feerate, increasing chunk size, and order
    1123                 :             :         // of maximum fallback-ordered element (just maximum DepGraphIndex element here).
    1124                 :         538 :         done = {};
    1125                 :             :         // Go over all pairs of chunks. done is the set of transactions seen before chunk_num1.
    1126   [ -  +  +  + ]:        5975 :         for (unsigned chunk_num1 = 0; chunk_num1 < chunking_info.size(); ++chunk_num1) {
    1127                 :        5437 :             const auto& chunk1 = chunking_info[chunk_num1];
    1128   [ -  +  +  + ]:       54640 :             for (unsigned chunk_num2 = chunk_num1 + 1; chunk_num2 < chunking_info.size(); ++chunk_num2) {
    1129         [ +  - ]:       49203 :                 const auto& chunk2 = chunking_info[chunk_num2];
    1130                 :       49203 :                 TestBitSet chunk2_ancestors;
    1131   [ +  -  +  + ]:      167868 :                 for (auto tx : chunk2.transactions) chunk2_ancestors |= depgraph.Ancestors(tx);
    1132                 :             :                 // Check whether chunk2 only depends on transactions that precede chunk1.
    1133         [ +  + ]:       49203 :                 if ((chunk2_ancestors - done).IsSubsetOf(chunk2.transactions)) {
    1134                 :             :                     // chunk2 could take position chunk_num1.
    1135                 :             :                     // Verify that chunk feerate is decreasing (note that >= tie-breaks by size).
    1136         [ -  + ]:       29959 :                     assert(chunk1.feerate >= chunk2.feerate);
    1137                 :             :                     // If feerate and size are equal, compare by maximum DepGraphIndex element.
    1138         [ +  + ]:       62626 :                     if (chunk1.feerate == chunk2.feerate) {
    1139         [ -  + ]:       10347 :                         assert(chunk1.transactions.Last() < chunk2.transactions.Last());
    1140                 :             :                     }
    1141                 :             :                 }
    1142                 :             :             }
    1143                 :        5437 :             done |= chunk1.transactions;
    1144                 :             :         }
    1145                 :             : 
    1146                 :             :         // Redo from scratch with a different rng_seed. The resulting linearization should be
    1147                 :             :         // deterministic, if both are optimal.
    1148         [ -  + ]:         538 :         auto [linearization2, optimal2, cost2] = Linearize(depgraph, MaxOptimalLinearizationIters(depgraph.TxCount()) + 1, rng_seed ^ 0x1337, IndexTxOrder{});
    1149         [ -  + ]:         538 :         assert(optimal2);
    1150         [ -  + ]:         538 :         assert(linearization2 == linearization);
    1151                 :         538 :     }
    1152                 :         858 : }
    1153                 :             : 
    1154         [ +  - ]:         606 : FUZZ_TARGET(clusterlin_postlinearize)
    1155                 :             : {
    1156                 :             :     // Verify expected properties of PostLinearize() on arbitrary linearizations.
    1157                 :             : 
    1158                 :             :     // Retrieve a depgraph from the fuzz input.
    1159                 :         152 :     SpanReader reader(buffer);
    1160                 :         152 :     DepGraph<TestBitSet> depgraph;
    1161                 :         152 :     try {
    1162         [ +  - ]:         152 :         reader >> Using<DepGraphFormatter>(depgraph);
    1163         [ -  - ]:           0 :     } catch (const std::ios_base::failure&) {}
    1164                 :             : 
    1165                 :             :     // Retrieve a linearization from the fuzz input.
    1166                 :         152 :     std::vector<DepGraphIndex> linearization;
    1167         [ +  - ]:         304 :     linearization = ReadLinearization(depgraph, reader);
    1168         [ -  + ]:         152 :     SanityCheck(depgraph, linearization);
    1169                 :             : 
    1170                 :             :     // Produce a post-processed version.
    1171         [ +  - ]:         152 :     auto post_linearization = linearization;
    1172   [ -  +  +  - ]:         152 :     PostLinearize(depgraph, post_linearization);
    1173         [ -  + ]:         152 :     SanityCheck(depgraph, post_linearization);
    1174                 :             : 
    1175                 :             :     // Compare diagrams: post-linearization cannot worsen anywhere.
    1176         [ -  + ]:         152 :     auto chunking = ChunkLinearization(depgraph, linearization);
    1177         [ -  + ]:         152 :     auto post_chunking = ChunkLinearization(depgraph, post_linearization);
    1178   [ -  +  -  +  :         152 :     auto cmp = CompareChunks(post_chunking, chunking);
                   +  - ]
    1179         [ -  + ]:         152 :     assert(cmp >= 0);
    1180                 :             : 
    1181                 :             :     // Run again, things can keep improving (and never get worse)
    1182         [ +  - ]:         152 :     auto post_post_linearization = post_linearization;
    1183   [ -  +  +  - ]:         152 :     PostLinearize(depgraph, post_post_linearization);
    1184         [ -  + ]:         152 :     SanityCheck(depgraph, post_post_linearization);
    1185         [ -  + ]:         152 :     auto post_post_chunking = ChunkLinearization(depgraph, post_post_linearization);
    1186   [ -  +  -  +  :         152 :     cmp = CompareChunks(post_post_chunking, post_chunking);
                   +  - ]
    1187         [ -  + ]:         152 :     assert(cmp >= 0);
    1188                 :             : 
    1189                 :             :     // The chunks that come out of postlinearizing are always connected.
    1190         [ -  + ]:         152 :     auto linchunking = ChunkLinearizationInfo(depgraph, post_linearization);
    1191         [ +  + ]:        1292 :     for (const auto& [chunk_set, _chunk_feerate] : linchunking) {
    1192         [ -  + ]:        1140 :         assert(depgraph.IsConnected(chunk_set));
    1193                 :             :     }
    1194                 :         152 : }
    1195                 :             : 
    1196         [ +  - ]:        1101 : FUZZ_TARGET(clusterlin_postlinearize_tree)
    1197                 :             : {
    1198                 :             :     // Verify expected properties of PostLinearize() on linearizations of graphs that form either
    1199                 :             :     // an upright or reverse tree structure.
    1200                 :             : 
    1201                 :             :     // Construct a direction, RNG seed, and an arbitrary graph from the fuzz input.
    1202                 :         647 :     SpanReader reader(buffer);
    1203                 :         647 :     uint64_t rng_seed{0};
    1204                 :         647 :     DepGraph<TestBitSet> depgraph_gen;
    1205                 :         647 :     uint8_t direction{0};
    1206                 :         647 :     try {
    1207   [ +  -  +  +  :         647 :         reader >> direction >> rng_seed >> Using<DepGraphFormatter>(depgraph_gen);
                   +  - ]
    1208         [ -  + ]:           3 :     } catch (const std::ios_base::failure&) {}
    1209                 :             : 
    1210                 :         647 :     auto depgraph_tree = BuildTreeGraph(depgraph_gen, direction);
    1211                 :             : 
    1212                 :             :     // Retrieve a linearization from the fuzz input.
    1213                 :         647 :     std::vector<DepGraphIndex> linearization;
    1214         [ +  - ]:        1294 :     linearization = ReadLinearization(depgraph_tree, reader);
    1215         [ -  + ]:         647 :     SanityCheck(depgraph_tree, linearization);
    1216                 :             : 
    1217                 :             :     // Produce a postlinearized version.
    1218         [ +  - ]:         647 :     auto post_linearization = linearization;
    1219   [ -  +  +  - ]:         647 :     PostLinearize(depgraph_tree, post_linearization);
    1220         [ -  + ]:         647 :     SanityCheck(depgraph_tree, post_linearization);
    1221                 :             : 
    1222                 :             :     // Compare diagrams.
    1223         [ -  + ]:         647 :     auto chunking = ChunkLinearization(depgraph_tree, linearization);
    1224         [ -  + ]:         647 :     auto post_chunking = ChunkLinearization(depgraph_tree, post_linearization);
    1225   [ -  +  -  +  :         647 :     auto cmp = CompareChunks(post_chunking, chunking);
                   +  - ]
    1226         [ -  + ]:         647 :     assert(cmp >= 0);
    1227                 :             : 
    1228                 :             :     // Verify that post-linearizing again does not change the diagram. The result must be identical
    1229                 :             :     // as post_linearization ought to be optimal already with a tree-structured graph.
    1230         [ +  - ]:         647 :     auto post_post_linearization = post_linearization;
    1231   [ -  +  +  - ]:         647 :     PostLinearize(depgraph_tree, post_post_linearization);
    1232         [ -  + ]:         647 :     SanityCheck(depgraph_tree, post_post_linearization);
    1233         [ -  + ]:         647 :     auto post_post_chunking = ChunkLinearization(depgraph_tree, post_post_linearization);
    1234   [ -  +  -  +  :         647 :     auto cmp_post = CompareChunks(post_post_chunking, post_chunking);
                   +  - ]
    1235         [ -  + ]:         647 :     assert(cmp_post == 0);
    1236                 :             : 
    1237                 :             :     // Try to find an even better linearization directly. This must not change the diagram for the
    1238                 :             :     // same reason.
    1239   [ -  +  -  + ]:         647 :     auto [opt_linearization, _optimal, _cost] = Linearize(depgraph_tree, 100000, rng_seed, IndexTxOrder{}, post_linearization);
    1240         [ -  + ]:         647 :     auto opt_chunking = ChunkLinearization(depgraph_tree, opt_linearization);
    1241   [ -  +  -  +  :         647 :     auto cmp_opt = CompareChunks(opt_chunking, post_chunking);
                   +  - ]
    1242         [ -  + ]:         647 :     assert(cmp_opt == 0);
    1243                 :         647 : }
    1244                 :             : 
    1245         [ +  - ]:         635 : FUZZ_TARGET(clusterlin_postlinearize_moved_leaf)
    1246                 :             : {
    1247                 :             :     // Verify that taking an existing linearization, and moving a leaf to the back, potentially
    1248                 :             :     // increasing its fee, and then post-linearizing, results in something as good as the
    1249                 :             :     // original. This guarantees that in an RBF that replaces a transaction with one of the same
    1250                 :             :     // size but higher fee, applying the "remove conflicts, append new transaction, postlinearize"
    1251                 :             :     // process will never worsen linearization quality.
    1252                 :             : 
    1253                 :             :     // Construct an arbitrary graph and a fee from the fuzz input.
    1254                 :         181 :     SpanReader reader(buffer);
    1255                 :         181 :     DepGraph<TestBitSet> depgraph;
    1256                 :         181 :     int32_t fee_inc{0};
    1257                 :         181 :     try {
    1258                 :         181 :         uint64_t fee_inc_code;
    1259   [ +  -  +  + ]:         181 :         reader >> Using<DepGraphFormatter>(depgraph) >> VARINT(fee_inc_code);
    1260                 :          66 :         fee_inc = fee_inc_code & 0x3ffff;
    1261         [ -  + ]:         115 :     } catch (const std::ios_base::failure&) {}
    1262         [ +  + ]:         181 :     if (depgraph.TxCount() == 0) return;
    1263                 :             : 
    1264                 :             :     // Retrieve two linearizations from the fuzz input.
    1265         [ +  - ]:         167 :     auto lin = ReadLinearization(depgraph, reader);
    1266         [ +  - ]:         167 :     auto lin_leaf = ReadLinearization(depgraph, reader);
    1267                 :             : 
    1268                 :             :     // Construct a linearization identical to lin, but with the tail end of lin_leaf moved to the
    1269                 :             :     // back.
    1270                 :         167 :     std::vector<DepGraphIndex> lin_moved;
    1271         [ +  + ]:        2180 :     for (auto i : lin) {
    1272   [ +  +  +  - ]:        2013 :         if (i != lin_leaf.back()) lin_moved.push_back(i);
    1273                 :             :     }
    1274         [ +  - ]:         167 :     lin_moved.push_back(lin_leaf.back());
    1275                 :             : 
    1276                 :             :     // Postlinearize lin_moved.
    1277   [ -  +  +  - ]:         167 :     PostLinearize(depgraph, lin_moved);
    1278         [ -  + ]:         167 :     SanityCheck(depgraph, lin_moved);
    1279                 :             : 
    1280                 :             :     // Compare diagrams (applying the fee delta after computing the old one).
    1281         [ -  + ]:         167 :     auto old_chunking = ChunkLinearization(depgraph, lin);
    1282         [ -  + ]:         167 :     depgraph.FeeRate(lin_leaf.back()).fee += fee_inc;
    1283         [ -  + ]:         167 :     auto new_chunking = ChunkLinearization(depgraph, lin_moved);
    1284   [ -  +  -  +  :         167 :     auto cmp = CompareChunks(new_chunking, old_chunking);
                   +  - ]
    1285         [ -  + ]:         167 :     assert(cmp >= 0);
    1286                 :         181 : }
        

Generated by: LCOV version 2.0-1