LCOV - code coverage report
Current view: top level - src/test - txgraph_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 206 206
Test Date: 2026-11-27 06:02:19 Functions: 100.0 % 14 14
Branches: 52.8 % 776 410

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2023-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or https://opensource.org/license/mit/.
       4                 :             : 
       5                 :             : #include <txgraph.h>
       6                 :             : 
       7                 :             : #include <random.h>
       8                 :             : #include <test/util/common.h>
       9                 :             : 
      10                 :             : #include <boost/test/unit_test.hpp>
      11                 :             : 
      12                 :             : #include <memory>
      13                 :             : #include <vector>
      14                 :             : 
      15                 :             : BOOST_AUTO_TEST_SUITE(txgraph_tests)
      16                 :             : 
      17                 :             : namespace {
      18                 :             : 
      19                 :             : /** The number used as acceptable_cost argument in these tests. High enough that everything
      20                 :             :  *  should be optimal, always. */
      21                 :             : constexpr uint64_t HIGH_ACCEPTABLE_COST = 100'000'000;
      22                 :             : 
      23                 :          52 : std::strong_ordering PointerComparator(const TxGraph::Ref& a, const TxGraph::Ref& b) noexcept
      24                 :             : {
      25   [ +  -  +  + ]:          52 :     return (&a) <=> (&b);
      26                 :             : }
      27                 :             : 
      28                 :             : } // namespace
      29                 :             : 
      30   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(txgraph_trim_zigzag)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  -  +  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      31                 :             : {
      32                 :             :     // T     T     T     T     T     T     T     T     T     T     T     T     T     T (50 T's)
      33                 :             :     //  \   / \   / \   / \   / \   / \   / \   / \   / \   / \   / \   / \   / \   /
      34                 :             :     //   \ /   \ /   \ /   \ /   \ /   \ /   \ /   \ /   \ /   \ /   \ /   \ /   \ /
      35                 :             :     //    B     B     B     B     B     B     B     B     B     B     B     B     B    (49 B's)
      36                 :             :     //
      37                 :             :     /** The maximum cluster count used in this test. */
      38                 :           1 :     static constexpr int MAX_CLUSTER_COUNT = 50;
      39                 :             :     /** The number of "bottom" transactions, which are in the mempool already. */
      40                 :           1 :     static constexpr int NUM_BOTTOM_TX = 49;
      41                 :             :     /** The number of "top" transactions, which come from disconnected blocks. These are re-added
      42                 :             :      *  to the mempool and, while connecting them to the already-in-mempool transactions, we
      43                 :             :      *   discover the resulting cluster is oversized. */
      44                 :           1 :     static constexpr int NUM_TOP_TX = 50;
      45                 :             :     /** The total number of transactions in the test. */
      46                 :           1 :     static constexpr int NUM_TOTAL_TX = NUM_BOTTOM_TX + NUM_TOP_TX;
      47                 :           1 :     static_assert(NUM_TOTAL_TX > MAX_CLUSTER_COUNT);
      48                 :             :     /** Set a very large cluster size limit so that only the count limit is triggered. */
      49                 :           1 :     static constexpr int32_t MAX_CLUSTER_SIZE = 100'000 * 100;
      50                 :             : 
      51                 :             :     // Create a new graph for the test.
      52                 :           1 :     auto graph = MakeTxGraph(MAX_CLUSTER_COUNT, MAX_CLUSTER_SIZE, HIGH_ACCEPTABLE_COST, PointerComparator);
      53                 :             : 
      54                 :             :     // Add all transactions and store their Refs.
      55                 :           1 :     std::vector<TxGraph::Ref> refs;
      56         [ +  - ]:           1 :     refs.reserve(NUM_TOTAL_TX);
      57                 :             :     // First all bottom transactions: the i'th bottom transaction is at position i.
      58         [ +  + ]:          50 :     for (unsigned int i = 0; i < NUM_BOTTOM_TX; ++i) {
      59         [ +  - ]:          49 :         graph->AddTransaction(refs.emplace_back(), FeePerWeight{200 - i, 100});
      60                 :             :     }
      61                 :             :     // Then all top transactions: the i'th top transaction is at position NUM_BOTTOM_TX + i.
      62         [ +  + ]:          51 :     for (unsigned int i = 0; i < NUM_TOP_TX; ++i) {
      63         [ +  - ]:          50 :         graph->AddTransaction(refs.emplace_back(), FeePerWeight{100 - i, 100});
      64                 :             :     }
      65                 :             : 
      66                 :             :     // Create the zigzag dependency structure.
      67                 :             :     // Each transaction in the bottom row depends on two adjacent transactions from the top row.
      68         [ +  - ]:           1 :     graph->SanityCheck();
      69         [ +  + ]:          50 :     for (unsigned int i = 0; i < NUM_BOTTOM_TX; ++i) {
      70                 :          49 :         graph->AddDependency(/*parent=*/refs[NUM_BOTTOM_TX + i], /*child=*/refs[i]);
      71                 :          49 :         graph->AddDependency(/*parent=*/refs[NUM_BOTTOM_TX + i + 1], /*child=*/refs[i]);
      72                 :             :     }
      73                 :             : 
      74                 :             :     // Check that the graph is now oversized. This also forces the graph to
      75                 :             :     // group clusters and compute the oversized status.
      76         [ +  - ]:           1 :     graph->SanityCheck();
      77   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::TOP), NUM_TOTAL_TX);
      78   [ +  -  +  - ]:           2 :     BOOST_CHECK(graph->IsOversized(TxGraph::Level::TOP));
      79                 :             : 
      80                 :             :     // Call Trim() to remove transactions and bring the cluster back within limits.
      81                 :           1 :     auto removed_refs = graph->Trim();
      82         [ +  - ]:           1 :     graph->SanityCheck();
      83   [ +  -  +  -  :           2 :     BOOST_CHECK(!graph->IsOversized(TxGraph::Level::TOP));
                   +  - ]
      84                 :             : 
      85                 :             :     // We only need to trim the middle bottom transaction to end up with 2 clusters each within cluster limits.
      86   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(removed_refs.size(), 1);
                   +  - ]
      87   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::TOP), MAX_CLUSTER_COUNT * 2 - 2);
      88   [ -  +  +  + ]:         100 :     for (unsigned int i = 0; i < refs.size(); ++i) {
      89   [ +  -  +  - ]:          99 :         BOOST_CHECK_EQUAL(graph->Exists(refs[i], TxGraph::Level::TOP), i != (NUM_BOTTOM_TX / 2));
      90                 :             :     }
      91                 :           1 : }
      92                 :             : 
      93   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(txgraph_trim_flower)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  -  +  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      94                 :             : {
      95                 :             :     // We will build an oversized flower-shaped graph: all transactions are spent by 1 descendant.
      96                 :             :     //
      97                 :             :     //   T   T   T   T   T   T   T   T (100 T's)
      98                 :             :     //   |   |   |   |   |   |   |   |
      99                 :             :     //   |   |   |   |   |   |   |   |
     100                 :             :     //   \---+---+---+-+-+---+---+---/
     101                 :             :     //                 |
     102                 :             :     //                 B (1 B)
     103                 :             :     //
     104                 :             :     /** The maximum cluster count used in this test. */
     105                 :           1 :     static constexpr int MAX_CLUSTER_COUNT = 50;
     106                 :             :     /** The number of "top" transactions, which come from disconnected blocks. These are re-added
     107                 :             :      *  to the mempool and, connecting them to the already-in-mempool transactions, we discover the
     108                 :             :      *  resulting cluster is oversized. */
     109                 :           1 :     static constexpr int NUM_TOP_TX = MAX_CLUSTER_COUNT * 2;
     110                 :             :     /** The total number of transactions in this test. */
     111                 :           1 :     static constexpr int NUM_TOTAL_TX = NUM_TOP_TX + 1;
     112                 :             :     /** Set a very large cluster size limit so that only the count limit is triggered. */
     113                 :           1 :     static constexpr int32_t MAX_CLUSTER_SIZE = 100'000 * 100;
     114                 :             : 
     115                 :           1 :     auto graph = MakeTxGraph(MAX_CLUSTER_COUNT, MAX_CLUSTER_SIZE, HIGH_ACCEPTABLE_COST, PointerComparator);
     116                 :             : 
     117                 :             :     // Add all transactions and store their Refs.
     118                 :           1 :     std::vector<TxGraph::Ref> refs;
     119         [ +  - ]:           1 :     refs.reserve(NUM_TOTAL_TX);
     120                 :             : 
     121                 :             :     // Add all transactions. They are in individual clusters.
     122         [ +  - ]:           1 :     graph->AddTransaction(refs.emplace_back(), {1, 100});
     123         [ +  + ]:         101 :     for (unsigned int i = 0; i < NUM_TOP_TX; ++i) {
     124         [ +  - ]:         100 :         graph->AddTransaction(refs.emplace_back(), FeePerWeight{500 + i, 100});
     125                 :             :     }
     126         [ +  - ]:           1 :     graph->SanityCheck();
     127                 :             : 
     128                 :             :     // The 0th transaction spends all the top transactions.
     129         [ +  + ]:         101 :     for (unsigned int i = 1; i < NUM_TOTAL_TX; ++i) {
     130                 :         100 :         graph->AddDependency(/*parent=*/refs[i], /*child=*/refs[0]);
     131                 :             :     }
     132         [ +  - ]:           1 :     graph->SanityCheck();
     133                 :             : 
     134                 :             :     // Check that the graph is now oversized. This also forces the graph to
     135                 :             :     // group clusters and compute the oversized status.
     136   [ +  -  +  - ]:           2 :     BOOST_CHECK(graph->IsOversized(TxGraph::Level::TOP));
     137                 :             : 
     138                 :             :     // Call Trim() to remove transactions and bring the cluster back within limits.
     139                 :           1 :     auto removed_refs = graph->Trim();
     140         [ +  - ]:           1 :     graph->SanityCheck();
     141   [ +  -  +  -  :           2 :     BOOST_CHECK(!graph->IsOversized(TxGraph::Level::TOP));
                   +  - ]
     142                 :             : 
     143                 :             :     // Since only the bottom transaction connects these clusters, we only need to remove it.
     144   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(removed_refs.size(), 1);
                   +  - ]
     145   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::TOP), MAX_CLUSTER_COUNT * 2);
     146   [ +  -  +  - ]:           2 :     BOOST_CHECK(!graph->Exists(refs[0], TxGraph::Level::TOP));
     147   [ -  +  +  + ]:         101 :     for (unsigned int i = 1; i < refs.size(); ++i) {
     148   [ +  -  +  - ]:         200 :         BOOST_CHECK(graph->Exists(refs[i], TxGraph::Level::TOP));
     149                 :             :     }
     150                 :           1 : }
     151                 :             : 
     152   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(txgraph_trim_huge)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  -  +  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     153                 :             : {
     154                 :             :     // The from-block transactions consist of 1000 fully linear clusters, each with 64
     155                 :             :     // transactions. The mempool contains 11 transactions that together merge all of these into
     156                 :             :     // a single cluster.
     157                 :             :     //
     158                 :             :     // (1000 chains of 64 transactions, 64000 T's total)
     159                 :             :     //
     160                 :             :     //      T          T          T          T          T          T          T          T
     161                 :             :     //      |          |          |          |          |          |          |          |
     162                 :             :     //      T          T          T          T          T          T          T          T
     163                 :             :     //      |          |          |          |          |          |          |          |
     164                 :             :     //      T          T          T          T          T          T          T          T
     165                 :             :     //      |          |          |          |          |          |          |          |
     166                 :             :     //      T          T          T          T          T          T          T          T
     167                 :             :     //  (64 long)  (64 long)  (64 long)  (64 long)  (64 long)  (64 long)  (64 long)  (64 long)
     168                 :             :     //      |          |          |          |          |          |          |          |
     169                 :             :     //      |          |         / \         |         / \         |          |         /
     170                 :             :     //      \----------+--------/   \--------+--------/   \--------+-----+----+--------/
     171                 :             :     //                 |                     |                           |
     172                 :             :     //                 B                     B                           B
     173                 :             :     //
     174                 :             :     //  (11 B's, each attaching to up to 100 chains of 64 T's)
     175                 :             :     //
     176                 :             :     /** The maximum cluster count used in this test. */
     177                 :           1 :     static constexpr int MAX_CLUSTER_COUNT = 64;
     178                 :             :     /** The number of "top" (from-block) chains of transactions. */
     179                 :           1 :     static constexpr int NUM_TOP_CHAINS = 1000;
     180                 :             :     /** The number of transactions per top chain. */
     181                 :           1 :     static constexpr int NUM_TX_PER_TOP_CHAIN = MAX_CLUSTER_COUNT;
     182                 :             :     /** The (maximum) number of dependencies per bottom transaction. */
     183                 :           1 :     static constexpr int NUM_DEPS_PER_BOTTOM_TX = 100;
     184                 :             :     /** The number of bottom transactions that are expected to be created. */
     185                 :           1 :     static constexpr int NUM_BOTTOM_TX = (NUM_TOP_CHAINS - 1 + (NUM_DEPS_PER_BOTTOM_TX - 2)) / (NUM_DEPS_PER_BOTTOM_TX - 1);
     186                 :             :     /** The total number of transactions created in this test. */
     187                 :           1 :     static constexpr int NUM_TOTAL_TX = NUM_TOP_CHAINS * NUM_TX_PER_TOP_CHAIN + NUM_BOTTOM_TX;
     188                 :             :     /** Set a very large cluster size limit so that only the count limit is triggered. */
     189                 :           1 :     static constexpr int32_t MAX_CLUSTER_SIZE = 100'000 * 100;
     190                 :             : 
     191                 :             :     /** Refs to all top transactions. */
     192                 :           1 :     std::vector<TxGraph::Ref> top_refs;
     193                 :             :     /** Refs to all bottom transactions. */
     194                 :           1 :     std::vector<TxGraph::Ref> bottom_refs;
     195                 :             :     /** Indexes into top_refs for some transaction of each component, in arbitrary order.
     196                 :             :      *  Initially these are the last transactions in each chains, but as bottom transactions are
     197                 :             :      *  added, entries will be removed when they get merged, and randomized. */
     198                 :           1 :     std::vector<size_t> top_components;
     199                 :             : 
     200                 :           1 :     FastRandomContext rng;
     201                 :           1 :     auto graph = MakeTxGraph(MAX_CLUSTER_COUNT, MAX_CLUSTER_SIZE, HIGH_ACCEPTABLE_COST, PointerComparator);
     202                 :             : 
     203                 :             :     // Construct the top chains.
     204         [ +  + ]:        1001 :     for (int chain = 0; chain < NUM_TOP_CHAINS; ++chain) {
     205         [ +  + ]:       65000 :         for (int chaintx = 0; chaintx < NUM_TX_PER_TOP_CHAIN; ++chaintx) {
     206                 :             :             // Use random fees, size 1.
     207                 :       64000 :             int64_t fee = rng.randbits<27>() + 100;
     208         [ +  - ]:       64000 :             FeePerWeight feerate{fee, 1};
     209         [ +  - ]:       64000 :             graph->AddTransaction(top_refs.emplace_back(), feerate);
     210                 :             :             // Add internal dependencies linking the chain transactions together.
     211         [ +  + ]:       64000 :             if (chaintx > 0) {
     212                 :       63000 :                  graph->AddDependency(*(top_refs.rbegin()), *(top_refs.rbegin() + 1));
     213                 :             :             }
     214                 :             :         }
     215                 :             :         // Remember the last transaction in each chain, to attach the bottom transactions to.
     216   [ -  +  +  - ]:        1000 :         top_components.push_back(top_refs.size() - 1);
     217                 :             :     }
     218         [ +  - ]:           1 :     graph->SanityCheck();
     219                 :             : 
     220                 :             :     // Not oversized so far (just 1000 clusters of 64).
     221   [ +  -  +  - ]:           2 :     BOOST_CHECK(!graph->IsOversized(TxGraph::Level::TOP));
     222                 :             : 
     223                 :             :     // Construct the bottom transactions, and dependencies to the top chains.
     224   [ -  +  +  + ]:          12 :     while (top_components.size() > 1) {
     225                 :             :         // Construct the transaction.
     226                 :          11 :         int64_t fee = rng.randbits<27>() + 100;
     227                 :          11 :         FeePerWeight feerate{fee, 1};
     228                 :          11 :         TxGraph::Ref bottom_tx;
     229                 :          11 :         graph->AddTransaction(bottom_tx, feerate);
     230                 :             :         // Determine the number of dependencies this transaction will have.
     231   [ -  +  +  + ]:          11 :         int deps = std::min<int>(NUM_DEPS_PER_BOTTOM_TX, top_components.size());
     232         [ +  + ]:        1021 :         for (int dep = 0; dep < deps; ++dep) {
     233                 :             :             // Pick an transaction in top_components to attach to.
     234         [ -  + ]:        1010 :             auto idx = rng.randrange(top_components.size());
     235                 :             :             // Add dependency.
     236                 :        1010 :             graph->AddDependency(/*parent=*/top_refs[top_components[idx]], /*child=*/bottom_tx);
     237                 :             :             // Unless this is the last dependency being added, remove from top_components, as
     238                 :             :             // the component will be merged with that one.
     239         [ +  + ]:        1010 :             if (dep < deps - 1) {
     240                 :             :                 // Move entry top the back.
     241   [ -  +  +  + ]:         999 :                 if (idx != top_components.size() - 1) std::swap(top_components.back(), top_components[idx]);
     242                 :             :                 // And pop it.
     243                 :         999 :                 top_components.pop_back();
     244                 :             :             }
     245                 :             :         }
     246         [ +  - ]:          11 :         bottom_refs.push_back(std::move(bottom_tx));
     247                 :          11 :     }
     248         [ +  - ]:           1 :     graph->SanityCheck();
     249                 :             : 
     250                 :             :     // Now we are oversized (one cluster of 64011).
     251   [ +  -  +  - ]:           2 :     BOOST_CHECK(graph->IsOversized(TxGraph::Level::TOP));
     252                 :           1 :     const auto total_tx_count = graph->GetTransactionCount(TxGraph::Level::TOP);
     253   [ +  -  -  +  :           2 :     BOOST_CHECK(total_tx_count == top_refs.size() + bottom_refs.size());
          -  +  +  -  +  
                      - ]
     254   [ +  -  +  - ]:           2 :     BOOST_CHECK(total_tx_count == NUM_TOTAL_TX);
     255                 :             : 
     256                 :             :     // Call Trim() to remove transactions and bring the cluster back within limits.
     257                 :           1 :     auto removed_refs = graph->Trim();
     258   [ +  -  +  -  :           2 :     BOOST_CHECK(!graph->IsOversized(TxGraph::Level::TOP));
                   +  - ]
     259   [ +  -  -  +  :           2 :     BOOST_CHECK(removed_refs.size() == total_tx_count - graph->GetTransactionCount(TxGraph::Level::TOP));
             +  -  +  - ]
     260         [ +  - ]:           1 :     graph->SanityCheck();
     261                 :             : 
     262                 :             :     // At least 99% of chains must survive.
     263   [ +  -  +  - ]:           2 :     BOOST_CHECK(graph->GetTransactionCount(TxGraph::Level::TOP) >= (NUM_TOP_CHAINS * NUM_TX_PER_TOP_CHAIN * 99) / 100);
     264                 :           1 : }
     265                 :             : 
     266   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(txgraph_trim_big_singletons)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  -  +  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     267                 :             : {
     268                 :             :     // Mempool consists of 100 singleton clusters; there are no dependencies. Some are oversized. Trim() should remove all of the oversized ones.
     269                 :           1 :     static constexpr int MAX_CLUSTER_COUNT = 64;
     270                 :           1 :     static constexpr int32_t MAX_CLUSTER_SIZE = 100'000;
     271                 :           1 :     static constexpr int NUM_TOTAL_TX = 100;
     272                 :             : 
     273                 :             :     // Create a new graph for the test.
     274                 :           1 :     auto graph = MakeTxGraph(MAX_CLUSTER_COUNT, MAX_CLUSTER_SIZE, HIGH_ACCEPTABLE_COST, PointerComparator);
     275                 :             : 
     276                 :             :     // Add all transactions and store their Refs.
     277                 :           1 :     std::vector<TxGraph::Ref> refs;
     278         [ +  - ]:           1 :     refs.reserve(NUM_TOTAL_TX);
     279                 :             : 
     280                 :             :     // Add all transactions. They are in individual clusters.
     281         [ +  + ]:         101 :     for (unsigned int i = 0; i < NUM_TOTAL_TX; ++i) {
     282                 :             :         // The 88th transaction is oversized.
     283                 :             :         // Every 20th transaction is oversized.
     284   [ +  +  +  + ]:         100 :         const FeePerWeight feerate{500 + i, (i == 88 || i % 20 == 0) ? MAX_CLUSTER_SIZE + 1 : 100};
     285         [ +  - ]:         100 :         graph->AddTransaction(refs.emplace_back(), feerate);
     286                 :             :     }
     287         [ +  - ]:           1 :     graph->SanityCheck();
     288                 :             : 
     289                 :             :     // Check that the graph is now oversized. This also forces the graph to
     290                 :             :     // group clusters and compute the oversized status.
     291   [ +  -  +  - ]:           2 :     BOOST_CHECK(graph->IsOversized(TxGraph::Level::TOP));
     292                 :             : 
     293                 :             :     // Call Trim() to remove transactions and bring the cluster back within limits.
     294                 :           1 :     graph->Trim();
     295         [ +  - ]:           1 :     graph->SanityCheck();
     296   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::TOP), NUM_TOTAL_TX - 6);
     297   [ +  -  +  - ]:           2 :     BOOST_CHECK(!graph->IsOversized(TxGraph::Level::TOP));
     298                 :             : 
     299                 :             :     // Check that all the oversized transactions were removed.
     300   [ -  +  +  + ]:         101 :     for (unsigned int i = 0; i < refs.size(); ++i) {
     301   [ +  -  +  +  :         106 :         BOOST_CHECK_EQUAL(graph->Exists(refs[i], TxGraph::Level::TOP), i != 88 && i % 20 != 0);
             +  +  +  - ]
     302                 :             :     }
     303                 :           1 : }
     304                 :             : 
     305   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(txgraph_chunk_chain)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  -  +  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     306                 :             : {
     307                 :             :     // Create a new graph for the test.
     308                 :           1 :     auto graph = MakeTxGraph(50, 1000, HIGH_ACCEPTABLE_COST, PointerComparator);
     309                 :             : 
     310                 :           6 :     auto block_builder_checker = [&graph](std::vector<std::vector<TxGraph::Ref*>> expected_chunks) {
     311                 :           5 :         std::vector<std::vector<TxGraph::Ref*>> chunks;
     312                 :           5 :         auto builder = graph->GetBlockBuilder();
     313                 :           5 :         FeePerWeight last_chunk_feerate;
     314         [ +  + ]:          19 :         while (auto chunk = builder->GetCurrentChunk()) {
     315                 :           7 :             FeePerWeight sum;
     316         [ +  + ]:          18 :             for (TxGraph::Ref* ref : chunk->first) {
     317                 :             :                 // The reported chunk feerate must match the chunk feerate obtained by asking
     318                 :             :                 // it for each of the chunk's transactions individually.
     319   [ +  -  +  - ]:          11 :                 BOOST_CHECK_EQUAL(graph->GetMainChunkFeerate(*ref), chunk->second);
     320                 :             :                 // Verify the chunk feerate matches the sum of the reported individual feerates.
     321                 :          11 :                 sum += graph->GetIndividualFeerate(*ref);
     322                 :             :             }
     323   [ +  -  +  - ]:           7 :             BOOST_CHECK_EQUAL(sum, chunk->second);
     324         [ +  - ]:           7 :             chunks.push_back(std::move(chunk->first));
     325                 :           7 :             last_chunk_feerate = chunk->second;
     326                 :           7 :             builder->Include();
     327                 :           7 :         }
     328                 :             : 
     329   [ +  -  +  - ]:          10 :         BOOST_CHECK(chunks == expected_chunks);
     330                 :           5 :         auto& last_chunk = chunks.back();
     331                 :             :         // The last chunk returned by the BlockBuilder must match GetWorstMainChunk, in reverse.
     332                 :           5 :         std::reverse(last_chunk.begin(), last_chunk.end());
     333         [ +  - ]:           5 :         auto [worst_chunk, worst_chunk_feerate] = graph->GetWorstMainChunk();
     334   [ +  -  +  -  :          10 :         BOOST_CHECK(last_chunk == worst_chunk);
                   +  - ]
     335   [ +  -  +  - ]:           5 :         BOOST_CHECK_EQUAL(last_chunk_feerate, worst_chunk_feerate);
     336                 :           6 :     };
     337                 :             : 
     338                 :           1 :     std::vector<TxGraph::Ref> refs;
     339         [ +  - ]:           1 :     refs.reserve(4);
     340                 :             : 
     341                 :           1 :     FeePerWeight feerateA{2, 10};
     342                 :           1 :     FeePerWeight feerateB{1, 10};
     343                 :           1 :     FeePerWeight feerateC{2, 10};
     344                 :           1 :     FeePerWeight feerateD{4, 10};
     345                 :             : 
     346                 :             :     // everytime adding a transaction, test the chunk status
     347                 :             :     // [A]
     348         [ +  - ]:           1 :     graph->AddTransaction(refs.emplace_back(), feerateA);
     349   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::TOP), 1);
     350   [ +  -  +  -  :           2 :     block_builder_checker({{&refs[0]}});
             +  +  -  - ]
     351                 :             :     // [A, B]
     352         [ +  - ]:           1 :     graph->AddTransaction(refs.emplace_back(), feerateB);
     353                 :           1 :     graph->AddDependency(/*parent=*/refs[0], /*child=*/refs[1]);
     354   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::TOP), 2);
     355   [ +  -  +  -  :           3 :     block_builder_checker({{&refs[0]}, {&refs[1]}});
          +  -  +  +  -  
                      - ]
     356                 :             : 
     357                 :             :     // [A, BC]
     358         [ +  - ]:           1 :     graph->AddTransaction(refs.emplace_back(), feerateC);
     359                 :           1 :     graph->AddDependency(/*parent=*/refs[1], /*child=*/refs[2]);
     360   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::TOP), 3);
     361   [ +  -  +  -  :           3 :     block_builder_checker({{&refs[0]}, {&refs[1], &refs[2]}});
          +  -  +  +  -  
                      - ]
     362                 :             : 
     363                 :             :     // [ABCD]
     364         [ +  - ]:           1 :     graph->AddTransaction(refs.emplace_back(), feerateD);
     365                 :           1 :     graph->AddDependency(/*parent=*/refs[2], /*child=*/refs[3]);
     366   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::TOP), 4);
     367   [ +  -  +  -  :           2 :     block_builder_checker({{&refs[0], &refs[1], &refs[2], &refs[3]}});
             +  +  -  - ]
     368                 :             : 
     369         [ +  - ]:           1 :     graph->SanityCheck();
     370                 :             : 
     371                 :             :     // D->C->A
     372                 :           1 :     graph->RemoveTransaction(refs[1]);
     373                 :             :     // txgraph is not responsible for removing the descendants or ancestors
     374   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::TOP), 3);
     375                 :             :     // only A remains there
     376                 :           1 :     graph->RemoveTransaction(refs[2]);
     377                 :           1 :     graph->RemoveTransaction(refs[3]);
     378   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::TOP), 1);
     379   [ +  -  +  -  :           2 :     block_builder_checker({{&refs[0]}});
             +  +  -  - ]
     380   [ +  -  +  -  :           6 : }
          +  -  +  -  +  
          -  +  -  +  -  
             -  -  -  - ]
     381                 :             : 
     382   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(txgraph_staging)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  -  +  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     383                 :             : {
     384                 :             :     /* Create a new graph for the test.
     385                 :             :      * The parameters are max_cluster_count, max_cluster_size, acceptable_iters
     386                 :             :      */
     387                 :           1 :     auto graph = MakeTxGraph(10, 1000, HIGH_ACCEPTABLE_COST, PointerComparator);
     388                 :             : 
     389                 :           1 :     std::vector<TxGraph::Ref> refs;
     390         [ +  - ]:           1 :     refs.reserve(2);
     391                 :             : 
     392                 :           1 :     FeePerWeight feerateA{2, 10};
     393                 :           1 :     FeePerWeight feerateB{1, 10};
     394                 :             : 
     395                 :             :     // everytime adding a transaction, test the chunk status
     396                 :             :     // [A]
     397         [ +  - ]:           1 :     graph->AddTransaction(refs.emplace_back(), feerateA);
     398   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->HaveStaging(), false);
     399   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::TOP), 1);
     400                 :             : 
     401                 :           1 :     graph->StartStaging();
     402   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->HaveStaging(), true);
     403   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::TOP), 1);
     404                 :             : 
     405                 :             :     // [A, B]
     406         [ +  - ]:           1 :     graph->AddTransaction(refs.emplace_back(), feerateB);
     407   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::MAIN), 1);
     408   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::TOP), 2);
     409   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->Exists(refs[0], TxGraph::Level::TOP), true);
     410   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->Exists(refs[1], TxGraph::Level::TOP), true);
     411                 :             : 
     412                 :           1 :     graph->AddDependency(/*parent=*/refs[0], /*child=*/refs[1]);
     413   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::MAIN), 1);
     414   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::TOP), 2);
     415                 :             : 
     416                 :           1 :     graph->CommitStaging();
     417   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->HaveStaging(), false);
     418                 :             : 
     419   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::MAIN), 2);
     420                 :             : 
     421                 :           1 :     graph->StartStaging();
     422                 :             : 
     423                 :             :     // [A]
     424                 :           1 :     graph->RemoveTransaction(refs[1]);
     425   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::MAIN), 2);
     426   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::TOP), 1);
     427                 :             : 
     428                 :           1 :     graph->CommitStaging();
     429                 :             : 
     430   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(graph->GetTransactionCount(TxGraph::Level::MAIN), 1);
     431                 :             : 
     432         [ +  - ]:           1 :     graph->SanityCheck();
     433                 :           1 : }
     434                 :             : 
     435                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1