LCOV - code coverage report
Current view: top level - src/test - mempool_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 100.0 % 311 311
Test Date: 2025-12-07 05:20:42 Functions: 100.0 % 9 9
Branches: 50.1 % 1490 747

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2011-2022 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <common/system.h>
       6                 :             : #include <policy/policy.h>
       7                 :             : #include <test/util/txmempool.h>
       8                 :             : #include <txmempool.h>
       9                 :             : #include <util/time.h>
      10                 :             : 
      11                 :             : #include <test/util/setup_common.h>
      12                 :             : 
      13                 :             : #include <boost/test/unit_test.hpp>
      14                 :             : #include <vector>
      15                 :             : 
      16                 :             : BOOST_FIXTURE_TEST_SUITE(mempool_tests, TestingSetup)
      17                 :             : 
      18                 :             : static constexpr auto REMOVAL_REASON_DUMMY = MemPoolRemovalReason::REPLACED;
      19                 :             : 
      20                 :             : class MemPoolTest final : public CTxMemPool
      21                 :             : {
      22                 :             : public:
      23                 :             :     using CTxMemPool::GetMinFee;
      24                 :             : };
      25                 :             : 
      26   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(MempoolRemoveTest)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      27                 :             : {
      28                 :             :     // Test CTxMemPool::remove functionality
      29                 :             : 
      30                 :           1 :     TestMemPoolEntryHelper entry;
      31                 :             :     // Parent transaction with three children,
      32                 :             :     // and three grand-children:
      33                 :           1 :     CMutableTransaction txParent;
      34         [ +  - ]:           1 :     txParent.vin.resize(1);
      35         [ +  - ]:           1 :     txParent.vin[0].scriptSig = CScript() << OP_11;
      36         [ +  - ]:           1 :     txParent.vout.resize(3);
      37         [ +  + ]:           4 :     for (int i = 0; i < 3; i++)
      38                 :             :     {
      39   [ +  -  +  - ]:           3 :         txParent.vout[i].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
      40                 :           3 :         txParent.vout[i].nValue = 33000LL;
      41                 :             :     }
      42   [ +  -  +  +  :          10 :     CMutableTransaction txChild[3];
                   -  - ]
      43         [ +  + ]:           4 :     for (int i = 0; i < 3; i++)
      44                 :             :     {
      45         [ +  - ]:           3 :         txChild[i].vin.resize(1);
      46         [ +  - ]:           3 :         txChild[i].vin[0].scriptSig = CScript() << OP_11;
      47   [ +  -  +  - ]:           3 :         txChild[i].vin[0].prevout.hash = txParent.GetHash();
      48                 :           3 :         txChild[i].vin[0].prevout.n = i;
      49         [ +  - ]:           3 :         txChild[i].vout.resize(1);
      50   [ +  -  +  - ]:           3 :         txChild[i].vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
      51                 :           3 :         txChild[i].vout[0].nValue = 11000LL;
      52                 :             :     }
      53   [ +  -  +  +  :          11 :     CMutableTransaction txGrandChild[3];
                   -  - ]
      54         [ +  + ]:           4 :     for (int i = 0; i < 3; i++)
      55                 :             :     {
      56         [ +  - ]:           3 :         txGrandChild[i].vin.resize(1);
      57         [ +  - ]:           3 :         txGrandChild[i].vin[0].scriptSig = CScript() << OP_11;
      58   [ +  -  +  - ]:           3 :         txGrandChild[i].vin[0].prevout.hash = txChild[i].GetHash();
      59                 :           3 :         txGrandChild[i].vin[0].prevout.n = 0;
      60         [ +  - ]:           3 :         txGrandChild[i].vout.resize(1);
      61   [ +  -  +  - ]:           3 :         txGrandChild[i].vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
      62                 :           3 :         txGrandChild[i].vout[0].nValue = 11000LL;
      63                 :             :     }
      64                 :             : 
      65                 :             : 
      66   [ -  +  +  - ]:           1 :     CTxMemPool& testPool = *Assert(m_node.mempool);
      67   [ +  -  +  - ]:           1 :     LOCK2(::cs_main, testPool.cs);
      68                 :             : 
      69                 :             :     // Nothing in pool, remove should do nothing:
      70         [ +  - ]:           1 :     unsigned int poolSize = testPool.size();
      71   [ +  -  +  - ]:           1 :     testPool.removeRecursive(CTransaction(txParent), REMOVAL_REASON_DUMMY);
      72   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(testPool.size(), poolSize);
                   +  - ]
      73                 :             : 
      74                 :             :     // Just the parent:
      75   [ +  -  +  - ]:           1 :     TryAddToMempool(testPool, entry.FromTx(txParent));
      76         [ +  - ]:           1 :     poolSize = testPool.size();
      77   [ +  -  +  - ]:           1 :     testPool.removeRecursive(CTransaction(txParent), REMOVAL_REASON_DUMMY);
      78   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(testPool.size(), poolSize - 1);
                   +  - ]
      79                 :             : 
      80                 :             :     // Parent, children, grandchildren:
      81   [ +  -  +  - ]:           1 :     TryAddToMempool(testPool, entry.FromTx(txParent));
      82         [ +  + ]:           4 :     for (int i = 0; i < 3; i++)
      83                 :             :     {
      84   [ +  -  +  - ]:           3 :         TryAddToMempool(testPool, entry.FromTx(txChild[i]));
      85   [ +  -  +  - ]:           3 :         TryAddToMempool(testPool, entry.FromTx(txGrandChild[i]));
      86                 :             :     }
      87                 :             :     // Remove Child[0], GrandChild[0] should be removed:
      88         [ +  - ]:           1 :     poolSize = testPool.size();
      89   [ +  -  +  - ]:           1 :     testPool.removeRecursive(CTransaction(txChild[0]), REMOVAL_REASON_DUMMY);
      90   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(testPool.size(), poolSize - 2);
                   +  - ]
      91                 :             :     // ... make sure grandchild and child are gone:
      92         [ +  - ]:           1 :     poolSize = testPool.size();
      93   [ +  -  +  - ]:           1 :     testPool.removeRecursive(CTransaction(txGrandChild[0]), REMOVAL_REASON_DUMMY);
      94   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(testPool.size(), poolSize);
                   +  - ]
      95         [ +  - ]:           1 :     poolSize = testPool.size();
      96   [ +  -  +  - ]:           1 :     testPool.removeRecursive(CTransaction(txChild[0]), REMOVAL_REASON_DUMMY);
      97   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(testPool.size(), poolSize);
                   +  - ]
      98                 :             :     // Remove parent, all children/grandchildren should go:
      99         [ +  - ]:           1 :     poolSize = testPool.size();
     100   [ +  -  +  - ]:           1 :     testPool.removeRecursive(CTransaction(txParent), REMOVAL_REASON_DUMMY);
     101   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(testPool.size(), poolSize - 5);
                   +  - ]
     102   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(testPool.size(), 0U);
                   +  - ]
     103                 :             : 
     104                 :             :     // Add children and grandchildren, but NOT the parent (simulate the parent being in a block)
     105         [ +  + ]:           4 :     for (int i = 0; i < 3; i++)
     106                 :             :     {
     107   [ +  -  +  - ]:           3 :         TryAddToMempool(testPool, entry.FromTx(txChild[i]));
     108   [ +  -  +  - ]:           3 :         TryAddToMempool(testPool, entry.FromTx(txGrandChild[i]));
     109                 :             :     }
     110                 :             :     // Now remove the parent, as might happen if a block-re-org occurs but the parent cannot be
     111                 :             :     // put into the mempool (maybe because it is non-standard):
     112         [ +  - ]:           1 :     poolSize = testPool.size();
     113   [ +  -  +  - ]:           1 :     testPool.removeRecursive(CTransaction(txParent), REMOVAL_REASON_DUMMY);
     114   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(testPool.size(), poolSize - 6);
                   +  - ]
     115   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(testPool.size(), 0U);
             +  -  +  - ]
     116   [ +  -  +  +  :          10 : }
          +  +  -  -  -  
                      - ]
     117                 :             : 
     118   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(MempoolSizeLimitTest)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     119                 :             : {
     120         [ -  + ]:           1 :     auto& pool = static_cast<MemPoolTest&>(*Assert(m_node.mempool));
     121         [ +  - ]:           1 :     LOCK2(cs_main, pool.cs);
     122                 :           1 :     TestMemPoolEntryHelper entry;
     123                 :             : 
     124         [ +  - ]:           1 :     CMutableTransaction tx1 = CMutableTransaction();
     125         [ +  - ]:           1 :     tx1.vin.resize(1);
     126         [ +  - ]:           1 :     tx1.vin[0].scriptSig = CScript() << OP_1;
     127         [ +  - ]:           1 :     tx1.vout.resize(1);
     128   [ +  -  +  - ]:           1 :     tx1.vout[0].scriptPubKey = CScript() << OP_1 << OP_EQUAL;
     129         [ +  - ]:           1 :     tx1.vout[0].nValue = 10 * COIN;
     130   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(1000LL).FromTx(tx1));
     131                 :             : 
     132         [ +  - ]:           1 :     CMutableTransaction tx2 = CMutableTransaction();
     133         [ +  - ]:           1 :     tx2.vin.resize(1);
     134         [ +  - ]:           1 :     tx2.vin[0].scriptSig = CScript() << OP_2;
     135         [ +  - ]:           1 :     tx2.vout.resize(1);
     136   [ +  -  +  - ]:           1 :     tx2.vout[0].scriptPubKey = CScript() << OP_2 << OP_EQUAL;
     137         [ +  - ]:           1 :     tx2.vout[0].nValue = 10 * COIN;
     138   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(500LL).FromTx(tx2));
     139                 :             : 
     140   [ +  -  +  - ]:           1 :     pool.TrimToSize(pool.DynamicMemoryUsage()); // should do nothing
     141   [ +  -  +  -  :           2 :     BOOST_CHECK(pool.exists(tx1.GetHash()));
          +  -  +  -  +  
                      - ]
     142   [ +  -  +  -  :           2 :     BOOST_CHECK(pool.exists(tx2.GetHash()));
          +  -  +  -  +  
                      - ]
     143                 :             : 
     144   [ +  -  +  - ]:           1 :     pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4); // should remove the lower-feerate transaction
     145   [ +  -  +  -  :           2 :     BOOST_CHECK(pool.exists(tx1.GetHash()));
          +  -  +  -  +  
                      - ]
     146   [ +  -  +  -  :           2 :     BOOST_CHECK(!pool.exists(tx2.GetHash()));
          +  -  +  -  +  
                      - ]
     147                 :             : 
     148   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.FromTx(tx2));
     149         [ +  - ]:           1 :     CMutableTransaction tx3 = CMutableTransaction();
     150         [ +  - ]:           1 :     tx3.vin.resize(1);
     151   [ +  -  +  - ]:           1 :     tx3.vin[0].prevout = COutPoint(tx2.GetHash(), 0);
     152         [ +  - ]:           1 :     tx3.vin[0].scriptSig = CScript() << OP_2;
     153         [ +  - ]:           1 :     tx3.vout.resize(1);
     154   [ +  -  +  - ]:           1 :     tx3.vout[0].scriptPubKey = CScript() << OP_3 << OP_EQUAL;
     155         [ +  - ]:           1 :     tx3.vout[0].nValue = 10 * COIN;
     156   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(2000LL).FromTx(tx3));
     157                 :             : 
     158   [ +  -  +  - ]:           1 :     pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4); // tx3 should pay for tx2 (CPFP)
     159   [ +  -  +  -  :           2 :     BOOST_CHECK(!pool.exists(tx1.GetHash()));
          +  -  +  -  +  
                      - ]
     160   [ +  -  +  -  :           2 :     BOOST_CHECK(pool.exists(tx2.GetHash()));
          +  -  +  -  +  
                      - ]
     161   [ +  -  +  -  :           2 :     BOOST_CHECK(pool.exists(tx3.GetHash()));
          +  -  +  -  +  
                      - ]
     162                 :             : 
     163   [ +  -  +  - ]:           2 :     pool.TrimToSize(GetVirtualTransactionSize(CTransaction(tx1))); // mempool is limited to tx1's size in memory usage, so nothing fits
     164   [ +  -  +  -  :           2 :     BOOST_CHECK(!pool.exists(tx1.GetHash()));
          +  -  +  -  +  
                      - ]
     165   [ +  -  +  -  :           2 :     BOOST_CHECK(!pool.exists(tx2.GetHash()));
          +  -  +  -  +  
                      - ]
     166   [ +  -  +  -  :           2 :     BOOST_CHECK(!pool.exists(tx3.GetHash()));
          +  -  +  -  +  
                      - ]
     167                 :             : 
     168   [ +  -  +  -  :           4 :     CFeeRate maxFeeRateRemoved(2500, GetVirtualTransactionSize(CTransaction(tx3)) + GetVirtualTransactionSize(CTransaction(tx2)));
                   +  - ]
     169   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), maxFeeRateRemoved.GetFeePerK() + DEFAULT_INCREMENTAL_RELAY_FEE);
                   +  - ]
     170                 :             : 
     171         [ +  - ]:           1 :     CMutableTransaction tx4 = CMutableTransaction();
     172         [ +  - ]:           1 :     tx4.vin.resize(2);
     173                 :           1 :     tx4.vin[0].prevout.SetNull();
     174         [ +  - ]:           1 :     tx4.vin[0].scriptSig = CScript() << OP_4;
     175                 :           1 :     tx4.vin[1].prevout.SetNull();
     176         [ +  - ]:           1 :     tx4.vin[1].scriptSig = CScript() << OP_4;
     177         [ +  - ]:           1 :     tx4.vout.resize(2);
     178   [ +  -  +  - ]:           1 :     tx4.vout[0].scriptPubKey = CScript() << OP_4 << OP_EQUAL;
     179         [ +  - ]:           1 :     tx4.vout[0].nValue = 10 * COIN;
     180   [ +  -  +  - ]:           1 :     tx4.vout[1].scriptPubKey = CScript() << OP_4 << OP_EQUAL;
     181         [ +  - ]:           1 :     tx4.vout[1].nValue = 10 * COIN;
     182                 :             : 
     183         [ +  - ]:           1 :     CMutableTransaction tx5 = CMutableTransaction();
     184         [ +  - ]:           1 :     tx5.vin.resize(2);
     185   [ +  -  +  - ]:           1 :     tx5.vin[0].prevout = COutPoint(tx4.GetHash(), 0);
     186         [ +  - ]:           1 :     tx5.vin[0].scriptSig = CScript() << OP_4;
     187                 :           1 :     tx5.vin[1].prevout.SetNull();
     188         [ +  - ]:           1 :     tx5.vin[1].scriptSig = CScript() << OP_5;
     189         [ +  - ]:           1 :     tx5.vout.resize(2);
     190   [ +  -  +  - ]:           1 :     tx5.vout[0].scriptPubKey = CScript() << OP_5 << OP_EQUAL;
     191         [ +  - ]:           1 :     tx5.vout[0].nValue = 10 * COIN;
     192   [ +  -  +  - ]:           1 :     tx5.vout[1].scriptPubKey = CScript() << OP_5 << OP_EQUAL;
     193         [ +  - ]:           1 :     tx5.vout[1].nValue = 10 * COIN;
     194                 :             : 
     195         [ +  - ]:           1 :     CMutableTransaction tx6 = CMutableTransaction();
     196         [ +  - ]:           1 :     tx6.vin.resize(2);
     197   [ +  -  +  - ]:           1 :     tx6.vin[0].prevout = COutPoint(tx4.GetHash(), 1);
     198         [ +  - ]:           1 :     tx6.vin[0].scriptSig = CScript() << OP_4;
     199                 :           1 :     tx6.vin[1].prevout.SetNull();
     200         [ +  - ]:           1 :     tx6.vin[1].scriptSig = CScript() << OP_6;
     201         [ +  - ]:           1 :     tx6.vout.resize(2);
     202   [ +  -  +  - ]:           1 :     tx6.vout[0].scriptPubKey = CScript() << OP_6 << OP_EQUAL;
     203         [ +  - ]:           1 :     tx6.vout[0].nValue = 10 * COIN;
     204   [ +  -  +  - ]:           1 :     tx6.vout[1].scriptPubKey = CScript() << OP_6 << OP_EQUAL;
     205         [ +  - ]:           1 :     tx6.vout[1].nValue = 10 * COIN;
     206                 :             : 
     207         [ +  - ]:           1 :     CMutableTransaction tx7 = CMutableTransaction();
     208         [ +  - ]:           1 :     tx7.vin.resize(2);
     209   [ +  -  +  - ]:           1 :     tx7.vin[0].prevout = COutPoint(tx5.GetHash(), 0);
     210         [ +  - ]:           1 :     tx7.vin[0].scriptSig = CScript() << OP_5;
     211   [ +  -  +  - ]:           1 :     tx7.vin[1].prevout = COutPoint(tx6.GetHash(), 0);
     212         [ +  - ]:           1 :     tx7.vin[1].scriptSig = CScript() << OP_6;
     213         [ +  - ]:           1 :     tx7.vout.resize(2);
     214   [ +  -  +  - ]:           1 :     tx7.vout[0].scriptPubKey = CScript() << OP_7 << OP_EQUAL;
     215         [ +  - ]:           1 :     tx7.vout[0].nValue = 10 * COIN;
     216   [ +  -  +  - ]:           1 :     tx7.vout[1].scriptPubKey = CScript() << OP_7 << OP_EQUAL;
     217         [ +  - ]:           1 :     tx7.vout[1].nValue = 10 * COIN;
     218                 :             : 
     219   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(700LL).FromTx(tx4));
     220         [ +  - ]:           1 :     auto usage_with_tx4_only = pool.DynamicMemoryUsage();
     221   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(100LL).FromTx(tx5));
     222   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(110LL).FromTx(tx6));
     223   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(900LL).FromTx(tx7));
     224                 :             : 
     225                 :             :     // From the topology above, tx7 must be sorted last, so it should
     226                 :             :     // definitely evicted first if we must trim. tx4 should definitely remain
     227                 :             :     // in the mempool since it has a higher feerate than its descendants and
     228                 :             :     // should be in its own chunk.
     229   [ +  -  +  - ]:           1 :     pool.TrimToSize(pool.DynamicMemoryUsage() - 1);
     230   [ +  -  +  -  :           2 :     BOOST_CHECK(pool.exists(tx4.GetHash()));
          +  -  +  -  +  
                      - ]
     231   [ +  -  +  -  :           2 :     BOOST_CHECK(!pool.exists(tx7.GetHash()));
          +  -  +  -  +  
                      - ]
     232                 :             : 
     233                 :             :     // Tx5 and Tx6 may be removed as well because they're in the same chunk as
     234                 :             :     // tx7, but this behavior need not be guaranteed.
     235                 :             : 
     236   [ +  -  +  -  :           1 :     if (!pool.exists(tx5.GetHash()))
                   +  - ]
     237   [ +  -  +  - ]:           1 :         TryAddToMempool(pool, entry.Fee(100LL).FromTx(tx5));
     238   [ +  -  +  -  :           1 :     if (!pool.exists(tx6.GetHash()))
                   +  - ]
     239   [ +  -  +  - ]:           1 :         TryAddToMempool(pool, entry.Fee(110LL).FromTx(tx6));
     240   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(900LL).FromTx(tx7));
     241                 :             : 
     242                 :             :     // If we trim sufficiently, everything but tx4 should be removed.
     243         [ +  - ]:           1 :     pool.TrimToSize(usage_with_tx4_only + 1);
     244   [ +  -  +  -  :           2 :     BOOST_CHECK(pool.exists(tx4.GetHash()));
          +  -  +  -  +  
                      - ]
     245   [ +  -  +  -  :           2 :     BOOST_CHECK(!pool.exists(tx5.GetHash()));
          +  -  +  -  +  
                      - ]
     246   [ +  -  +  -  :           2 :     BOOST_CHECK(!pool.exists(tx6.GetHash()));
          +  -  +  -  +  
                      - ]
     247   [ +  -  +  -  :           2 :     BOOST_CHECK(!pool.exists(tx7.GetHash()));
          +  -  +  -  +  
                      - ]
     248                 :             : 
     249   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(100LL).FromTx(tx5));
     250   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(110LL).FromTx(tx6));
     251   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(900LL).FromTx(tx7));
     252                 :             : 
     253                 :           1 :     std::vector<CTransactionRef> vtx;
     254         [ +  - ]:           1 :     SetMockTime(42);
     255         [ +  - ]:           1 :     SetMockTime(42 + CTxMemPool::ROLLING_FEE_HALFLIFE);
     256   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), maxFeeRateRemoved.GetFeePerK() + DEFAULT_INCREMENTAL_RELAY_FEE);
                   +  - ]
     257                 :             :     // ... we should keep the same min fee until we get a block
     258         [ +  - ]:           1 :     pool.removeForBlock(vtx, 1);
     259         [ +  - ]:           1 :     SetMockTime(42 + 2*CTxMemPool::ROLLING_FEE_HALFLIFE);
     260   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), llround((maxFeeRateRemoved.GetFeePerK() + DEFAULT_INCREMENTAL_RELAY_FEE)/2.0));
                   +  - ]
     261                 :             :     // ... then feerate should drop 1/2 each halflife
     262                 :             : 
     263         [ +  - ]:           1 :     SetMockTime(42 + 2*CTxMemPool::ROLLING_FEE_HALFLIFE + CTxMemPool::ROLLING_FEE_HALFLIFE/2);
     264   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(pool.GetMinFee(pool.DynamicMemoryUsage() * 5 / 2).GetFeePerK(), llround((maxFeeRateRemoved.GetFeePerK() + DEFAULT_INCREMENTAL_RELAY_FEE)/4.0));
             +  -  +  - ]
     265                 :             :     // ... with a 1/2 halflife when mempool is < 1/2 its target size
     266                 :             : 
     267         [ +  - ]:           1 :     SetMockTime(42 + 2*CTxMemPool::ROLLING_FEE_HALFLIFE + CTxMemPool::ROLLING_FEE_HALFLIFE/2 + CTxMemPool::ROLLING_FEE_HALFLIFE/4);
     268   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(pool.GetMinFee(pool.DynamicMemoryUsage() * 9 / 2).GetFeePerK(), llround((maxFeeRateRemoved.GetFeePerK() + DEFAULT_INCREMENTAL_RELAY_FEE)/8.0));
             +  -  +  - ]
     269                 :             :     // ... with a 1/4 halflife when mempool is < 1/4 its target size
     270                 :             : 
     271         [ +  - ]:           1 :     SetMockTime(42 + 7*CTxMemPool::ROLLING_FEE_HALFLIFE + CTxMemPool::ROLLING_FEE_HALFLIFE/2 + CTxMemPool::ROLLING_FEE_HALFLIFE/4);
     272   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), DEFAULT_INCREMENTAL_RELAY_FEE);
                   +  - ]
     273                 :             :     // ... but feerate should never drop below DEFAULT_INCREMENTAL_RELAY_FEE
     274                 :             : 
     275         [ +  - ]:           1 :     SetMockTime(42 + 8*CTxMemPool::ROLLING_FEE_HALFLIFE + CTxMemPool::ROLLING_FEE_HALFLIFE/2 + CTxMemPool::ROLLING_FEE_HALFLIFE/4);
     276   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), 0);
                   +  - ]
     277                 :             :     // ... unless it has gone all the way to 0 (after getting past DEFAULT_INCREMENTAL_RELAY_FEE/2)
     278   [ +  -  +  - ]:          10 : }
     279                 :             : 
     280                 :          14 : inline CTransactionRef make_tx(std::vector<CAmount>&& output_values, std::vector<CTransactionRef>&& inputs=std::vector<CTransactionRef>(), std::vector<uint32_t>&& input_indices=std::vector<uint32_t>())
     281                 :             : {
     282                 :          14 :     CMutableTransaction tx = CMutableTransaction();
     283   [ -  +  +  - ]:          14 :     tx.vin.resize(inputs.size());
     284   [ -  +  +  - ]:          14 :     tx.vout.resize(output_values.size());
     285   [ -  +  +  + ]:          27 :     for (size_t i = 0; i < inputs.size(); ++i) {
     286         [ -  + ]:          13 :         tx.vin[i].prevout.hash = inputs[i]->GetHash();
     287   [ -  +  +  + ]:          13 :         tx.vin[i].prevout.n = input_indices.size() > i ? input_indices[i] : 0;
     288                 :             :     }
     289   [ -  +  +  + ]:          32 :     for (size_t i = 0; i < output_values.size(); ++i) {
     290   [ +  -  +  - ]:          18 :         tx.vout[i].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
     291                 :          18 :         tx.vout[i].nValue = output_values[i];
     292                 :             :     }
     293         [ +  - ]:          28 :     return MakeTransactionRef(tx);
     294                 :          14 : }
     295                 :             : 
     296                 :             : 
     297   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(MempoolAncestryTests)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     298                 :             : {
     299                 :           1 :     size_t ancestors, clustersize;
     300                 :             : 
     301         [ -  + ]:           1 :     CTxMemPool& pool = *Assert(m_node.mempool);
     302         [ +  - ]:           1 :     LOCK2(cs_main, pool.cs);
     303                 :           1 :     TestMemPoolEntryHelper entry;
     304                 :             : 
     305                 :             :     /* Base transaction */
     306                 :             :     //
     307                 :             :     // [tx1]
     308                 :             :     //
     309   [ +  -  +  - ]:           2 :     CTransactionRef tx1 = make_tx(/*output_values=*/{10 * COIN});
     310   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(10000LL).FromTx(tx1));
     311                 :             : 
     312                 :             :     // Ancestors / clustersize should be 1 / 1 (itself / itself)
     313         [ +  - ]:           1 :     pool.GetTransactionAncestry(tx1->GetHash(), ancestors, clustersize);
     314   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 1ULL);
     315   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(clustersize, 1ULL);
     316                 :             : 
     317                 :             :     /* Child transaction */
     318                 :             :     //
     319                 :             :     // [tx1].0 <- [tx2]
     320                 :             :     //
     321   [ +  -  +  -  :           3 :     CTransactionRef tx2 = make_tx(/*output_values=*/{495 * CENT, 5 * COIN}, /*inputs=*/{tx1});
          +  +  +  -  -  
                -  -  - ]
     322   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(10000LL).FromTx(tx2));
     323                 :             : 
     324                 :             :     // Ancestors / clustersize should be:
     325                 :             :     // transaction  ancestors   clustersize
     326                 :             :     // ============ =========== ===========
     327                 :             :     // tx1          1 (tx1)     2 (tx1,2)
     328                 :             :     // tx2          2 (tx1,2)   2 (tx1,2)
     329         [ +  - ]:           1 :     pool.GetTransactionAncestry(tx1->GetHash(), ancestors, clustersize);
     330   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 1ULL);
     331   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(clustersize, 2ULL);
     332         [ +  - ]:           1 :     pool.GetTransactionAncestry(tx2->GetHash(), ancestors, clustersize);
     333   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 2ULL);
     334   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(clustersize, 2ULL);
     335                 :             : 
     336                 :             :     /* Grand-child 1 */
     337                 :             :     //
     338                 :             :     // [tx1].0 <- [tx2].0 <- [tx3]
     339                 :             :     //
     340   [ +  -  +  -  :           3 :     CTransactionRef tx3 = make_tx(/*output_values=*/{290 * CENT, 200 * CENT}, /*inputs=*/{tx2});
          +  +  +  -  -  
                -  -  - ]
     341   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(10000LL).FromTx(tx3));
     342                 :             : 
     343                 :             :     // Ancestors / clustersize should be:
     344                 :             :     // transaction  ancestors   clustersize
     345                 :             :     // ============ =========== ===========
     346                 :             :     // tx1          1 (tx1)     3 (tx1,2,3)
     347                 :             :     // tx2          2 (tx1,2)   3 (tx1,2,3)
     348                 :             :     // tx3          3 (tx1,2,3) 3 (tx1,2,3)
     349         [ +  - ]:           1 :     pool.GetTransactionAncestry(tx1->GetHash(), ancestors, clustersize);
     350   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 1ULL);
     351   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(clustersize, 3ULL);
     352         [ +  - ]:           1 :     pool.GetTransactionAncestry(tx2->GetHash(), ancestors, clustersize);
     353   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 2ULL);
     354   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(clustersize, 3ULL);
     355         [ +  - ]:           1 :     pool.GetTransactionAncestry(tx3->GetHash(), ancestors, clustersize);
     356   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 3ULL);
     357   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(clustersize, 3ULL);
     358                 :             : 
     359                 :             :     /* Grand-child 2 */
     360                 :             :     //
     361                 :             :     // [tx1].0 <- [tx2].0 <- [tx3]
     362                 :             :     //              |
     363                 :             :     //              \---1 <- [tx4]
     364                 :             :     //
     365   [ +  -  +  -  :           5 :     CTransactionRef tx4 = make_tx(/*output_values=*/{290 * CENT, 250 * CENT}, /*inputs=*/{tx2}, /*input_indices=*/{1});
          +  -  +  +  +  
          -  +  -  -  -  
                   -  - ]
     366   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(10000LL).FromTx(tx4));
     367                 :             : 
     368                 :             :     // Ancestors / clustersize should be:
     369                 :             :     // transaction  ancestors   clustersize
     370                 :             :     // ============ =========== ===========
     371                 :             :     // tx1          1 (tx1)     4 (tx1,2,3,4)
     372                 :             :     // tx2          2 (tx1,2)   4 (tx1,2,3,4)
     373                 :             :     // tx3          3 (tx1,2,3) 4 (tx1,2,3,4)
     374                 :             :     // tx4          3 (tx1,2,4) 4 (tx1,2,3,4)
     375         [ +  - ]:           1 :     pool.GetTransactionAncestry(tx1->GetHash(), ancestors, clustersize);
     376   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 1ULL);
     377   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(clustersize, 4ULL);
     378         [ +  - ]:           1 :     pool.GetTransactionAncestry(tx2->GetHash(), ancestors, clustersize);
     379   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 2ULL);
     380   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(clustersize, 4ULL);
     381         [ +  - ]:           1 :     pool.GetTransactionAncestry(tx3->GetHash(), ancestors, clustersize);
     382   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 3ULL);
     383   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(clustersize, 4ULL);
     384         [ +  - ]:           1 :     pool.GetTransactionAncestry(tx4->GetHash(), ancestors, clustersize);
     385   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 3ULL);
     386   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(clustersize, 4ULL);
     387                 :             : 
     388                 :             :     /* Make an alternate branch that is longer and connect it to tx3 */
     389                 :             :     //
     390                 :             :     // [ty1].0 <- [ty2].0 <- [ty3].0 <- [ty4].0 <- [ty5].0
     391                 :             :     //                                              |
     392                 :             :     // [tx1].0 <- [tx2].0 <- [tx3].0 <- [ty6] --->--/
     393                 :             :     //              |
     394                 :             :     //              \---1 <- [tx4]
     395                 :             :     //
     396                 :           1 :     CTransactionRef ty1, ty2, ty3, ty4, ty5;
     397                 :           1 :     CTransactionRef* ty[5] = {&ty1, &ty2, &ty3, &ty4, &ty5};
     398                 :           1 :     CAmount v = 5 * COIN;
     399         [ +  + ]:           6 :     for (uint64_t i = 0; i < 5; i++) {
     400                 :           5 :         CTransactionRef& tyi = *ty[i];
     401   [ +  +  +  -  :          22 :         tyi = make_tx(/*output_values=*/{v}, /*inputs=*/i > 0 ? std::vector<CTransactionRef>{*ty[i - 1]} : std::vector<CTransactionRef>{});
          +  -  +  -  -  
          +  +  +  +  +  
          +  -  -  -  -  
                -  -  - ]
     402                 :           5 :         v -= 50 * CENT;
     403   [ +  -  +  - ]:           5 :         TryAddToMempool(pool, entry.Fee(10000LL).FromTx(tyi));
     404         [ +  - ]:           5 :         pool.GetTransactionAncestry(tyi->GetHash(), ancestors, clustersize);
     405   [ +  -  +  - ]:           5 :         BOOST_CHECK_EQUAL(ancestors, i+1);
     406   [ +  -  +  - ]:           5 :         BOOST_CHECK_EQUAL(clustersize, i+1);
     407                 :             :     }
     408   [ +  -  +  -  :           4 :     CTransactionRef ty6 = make_tx(/*output_values=*/{5 * COIN}, /*inputs=*/{tx3, ty5});
          +  +  +  -  -  
                -  -  - ]
     409   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(10000LL).FromTx(ty6));
     410                 :             : 
     411                 :             :     // Ancestors / clustersize should be:
     412                 :             :     // transaction  ancestors           clustersize
     413                 :             :     // ============ =================== ===========
     414                 :             :     // tx1          1 (tx1)             10 (tx1-5, ty1-5)
     415                 :             :     // tx2          2 (tx1,2)           10
     416                 :             :     // tx3          3 (tx1,2,3)         10
     417                 :             :     // tx4          3 (tx1,2,4)         10
     418                 :             :     // ty1          1 (ty1)             10
     419                 :             :     // ty2          2 (ty1,2)           10
     420                 :             :     // ty3          3 (ty1,2,3)         10
     421                 :             :     // ty4          4 (y1234)           10
     422                 :             :     // ty5          5 (y12345)          10
     423                 :             :     // ty6          9 (tx123, ty123456) 10
     424         [ +  - ]:           1 :     pool.GetTransactionAncestry(tx1->GetHash(), ancestors, clustersize);
     425   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 1ULL);
     426   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(clustersize, 10ULL);
     427         [ +  - ]:           1 :     pool.GetTransactionAncestry(tx2->GetHash(), ancestors, clustersize);
     428   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 2ULL);
     429   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(clustersize, 10ULL);
     430         [ +  - ]:           1 :     pool.GetTransactionAncestry(tx3->GetHash(), ancestors, clustersize);
     431   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 3ULL);
     432   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(clustersize, 10ULL);
     433         [ +  - ]:           1 :     pool.GetTransactionAncestry(tx4->GetHash(), ancestors, clustersize);
     434   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 3ULL);
     435   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(clustersize, 10ULL);
     436         [ +  - ]:           1 :     pool.GetTransactionAncestry(ty1->GetHash(), ancestors, clustersize);
     437   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 1ULL);
     438   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(clustersize, 10ULL);
     439         [ +  - ]:           1 :     pool.GetTransactionAncestry(ty2->GetHash(), ancestors, clustersize);
     440   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 2ULL);
     441   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(clustersize, 10ULL);
     442         [ +  - ]:           1 :     pool.GetTransactionAncestry(ty3->GetHash(), ancestors, clustersize);
     443   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 3ULL);
     444   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(clustersize, 10ULL);
     445         [ +  - ]:           1 :     pool.GetTransactionAncestry(ty4->GetHash(), ancestors, clustersize);
     446   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 4ULL);
     447   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(clustersize, 10ULL);
     448         [ +  - ]:           1 :     pool.GetTransactionAncestry(ty5->GetHash(), ancestors, clustersize);
     449   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 5ULL);
     450   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(clustersize, 10ULL);
     451         [ +  - ]:           1 :     pool.GetTransactionAncestry(ty6->GetHash(), ancestors, clustersize);
     452   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 9ULL);
     453   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(clustersize, 10ULL);
                   +  - ]
     454   [ +  -  +  -  :          25 : }
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
     455                 :             : 
     456   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(MempoolAncestryTestsDiamond)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     457                 :             : {
     458                 :           1 :     size_t ancestors, descendants;
     459                 :             : 
     460         [ -  + ]:           1 :     CTxMemPool& pool = *Assert(m_node.mempool);
     461         [ +  - ]:           1 :     LOCK2(::cs_main, pool.cs);
     462                 :           1 :     TestMemPoolEntryHelper entry;
     463                 :             : 
     464                 :             :     /* Ancestors represented more than once ("diamond") */
     465                 :             :     //
     466                 :             :     // [ta].0 <- [tb].0 -----<------- [td].0
     467                 :             :     //            |                    |
     468                 :             :     //            \---1 <- [tc].0 --<--/
     469                 :             :     //
     470                 :           1 :     CTransactionRef ta, tb, tc, td;
     471   [ +  -  +  -  :           2 :     ta = make_tx(/*output_values=*/{10 * COIN});
                   -  + ]
     472   [ +  -  +  -  :           3 :     tb = make_tx(/*output_values=*/{5 * COIN, 3 * COIN}, /*inputs=*/ {ta});
          -  +  +  +  +  
             -  -  -  -  
                      - ]
     473   [ +  -  +  -  :           5 :     tc = make_tx(/*output_values=*/{2 * COIN}, /*inputs=*/{tb}, /*input_indices=*/{1});
          +  -  -  +  +  
          +  +  -  +  -  
             -  -  -  - ]
     474   [ +  -  +  -  :           6 :     td = make_tx(/*output_values=*/{6 * COIN}, /*inputs=*/{tb, tc}, /*input_indices=*/{0, 0});
          +  -  -  +  +  
          +  +  -  +  -  
             -  -  -  - ]
     475   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(10000LL).FromTx(ta));
     476   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(10000LL).FromTx(tb));
     477   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(10000LL).FromTx(tc));
     478   [ +  -  +  - ]:           1 :     TryAddToMempool(pool, entry.Fee(10000LL).FromTx(td));
     479                 :             : 
     480                 :             :     // Ancestors / descendants should be:
     481                 :             :     // transaction  ancestors           descendants
     482                 :             :     // ============ =================== ===========
     483                 :             :     // ta           1 (ta               4 (ta,tb,tc,td)
     484                 :             :     // tb           2 (ta,tb)           4 (ta,tb,tc,td)
     485                 :             :     // tc           3 (ta,tb,tc)        4 (ta,tb,tc,td)
     486                 :             :     // td           4 (ta,tb,tc,td)     4 (ta,tb,tc,td)
     487         [ +  - ]:           1 :     pool.GetTransactionAncestry(ta->GetHash(), ancestors, descendants);
     488   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 1ULL);
     489   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(descendants, 4ULL);
     490         [ +  - ]:           1 :     pool.GetTransactionAncestry(tb->GetHash(), ancestors, descendants);
     491   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 2ULL);
     492   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(descendants, 4ULL);
     493         [ +  - ]:           1 :     pool.GetTransactionAncestry(tc->GetHash(), ancestors, descendants);
     494   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 3ULL);
     495   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(descendants, 4ULL);
     496         [ +  - ]:           1 :     pool.GetTransactionAncestry(td->GetHash(), ancestors, descendants);
     497   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ancestors, 4ULL);
     498   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(descendants, 4ULL);
                   +  - ]
     499   [ +  -  +  -  :          13 : }
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     500                 :             : 
     501                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1