LCOV - code coverage report
Current view: top level - src/test - miner_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 98.8 % 425 420
Test Date: 2024-11-04 05:10:19 Functions: 100.0 % 9 9
Branches: 50.5 % 1300 657

             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 <addresstype.h>
       6                 :             : #include <coins.h>
       7                 :             : #include <common/system.h>
       8                 :             : #include <consensus/consensus.h>
       9                 :             : #include <consensus/merkle.h>
      10                 :             : #include <consensus/tx_verify.h>
      11                 :             : #include <node/miner.h>
      12                 :             : #include <policy/policy.h>
      13                 :             : #include <test/util/random.h>
      14                 :             : #include <test/util/txmempool.h>
      15                 :             : #include <txmempool.h>
      16                 :             : #include <uint256.h>
      17                 :             : #include <util/check.h>
      18                 :             : #include <util/strencodings.h>
      19                 :             : #include <util/time.h>
      20                 :             : #include <util/translation.h>
      21                 :             : #include <validation.h>
      22                 :             : #include <versionbits.h>
      23                 :             : 
      24                 :             : #include <test/util/setup_common.h>
      25                 :             : 
      26                 :             : #include <memory>
      27                 :             : 
      28                 :             : #include <boost/test/unit_test.hpp>
      29                 :             : 
      30                 :             : using namespace util::hex_literals;
      31                 :             : using node::BlockAssembler;
      32                 :             : using node::CBlockTemplate;
      33                 :             : 
      34                 :             : namespace miner_tests {
      35                 :           2 : struct MinerTestingSetup : public TestingSetup {
      36                 :             :     void TestPackageSelection(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
      37                 :             :     void TestBasicMining(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst, int baseheight) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
      38                 :             :     void TestPrioritisedMining(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
      39                 :           8 :     bool TestSequenceLocks(const CTransaction& tx, CTxMemPool& tx_mempool) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
      40                 :             :     {
      41                 :           8 :         CCoinsViewMemPool view_mempool{&m_node.chainman->ActiveChainstate().CoinsTip(), tx_mempool};
      42   [ +  -  +  - ]:           8 :         CBlockIndex* tip{m_node.chainman->ActiveChain().Tip()};
      43         [ +  - ]:           8 :         const std::optional<LockPoints> lock_points{CalculateLockPointsAtTip(tip, view_mempool, tx)};
      44   [ +  -  +  -  :          16 :         return lock_points.has_value() && CheckSequenceLocksAtTip(tip, *lock_points);
                   +  + ]
      45                 :           8 :     }
      46                 :          11 :     CTxMemPool& MakeMempool()
      47                 :             :     {
      48                 :             :         // Delete the previous mempool to ensure with valgrind that the old
      49                 :             :         // pointer is not accessed, when the new one should be accessed
      50                 :             :         // instead.
      51         [ +  - ]:          11 :         m_node.mempool.reset();
      52         [ +  - ]:          11 :         bilingual_str error;
      53   [ +  -  +  - ]:          11 :         m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(m_node), error);
      54         [ +  - ]:          11 :         Assert(error.empty());
      55                 :          11 :         return *m_node.mempool;
      56                 :          11 :     }
      57                 :             :     BlockAssembler AssemblerForTest(CTxMemPool& tx_mempool);
      58                 :             : };
      59                 :             : } // namespace miner_tests
      60                 :             : 
      61                 :             : BOOST_FIXTURE_TEST_SUITE(miner_tests, MinerTestingSetup)
      62                 :             : 
      63                 :             : static CFeeRate blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
      64                 :             : 
      65                 :          20 : BlockAssembler MinerTestingSetup::AssemblerForTest(CTxMemPool& tx_mempool)
      66                 :             : {
      67                 :          20 :     BlockAssembler::Options options;
      68                 :             : 
      69                 :          20 :     options.nBlockMaxWeight = MAX_BLOCK_WEIGHT;
      70                 :          20 :     options.blockMinFeeRate = blockMinFeeRate;
      71                 :          20 :     return BlockAssembler{m_node.chainman->ActiveChainstate(), &tx_mempool, options};
      72                 :             : }
      73                 :             : 
      74                 :             : constexpr static struct {
      75                 :             :     unsigned char extranonce;
      76                 :             :     unsigned int nonce;
      77                 :             : } BLOCKINFO[]{{8, 582909131},  {0, 971462344},  {2, 1169481553}, {6, 66147495},  {7, 427785981},  {8, 80538907},
      78                 :             :               {8, 207348013},  {2, 1951240923}, {4, 215054351},  {1, 491520534}, {8, 1282281282}, {4, 639565734},
      79                 :             :               {3, 248274685},  {8, 1160085976}, {6, 396349768},  {5, 393780549}, {5, 1096899528}, {4, 965381630},
      80                 :             :               {0, 728758712},  {5, 318638310},  {3, 164591898},  {2, 274234550}, {2, 254411237},  {7, 561761812},
      81                 :             :               {2, 268342573},  {0, 402816691},  {1, 221006382},  {6, 538872455}, {7, 393315655},  {4, 814555937},
      82                 :             :               {7, 504879194},  {6, 467769648},  {3, 925972193},  {2, 200581872}, {3, 168915404},  {8, 430446262},
      83                 :             :               {5, 773507406},  {3, 1195366164}, {0, 433361157},  {3, 297051771}, {0, 558856551},  {2, 501614039},
      84                 :             :               {3, 528488272},  {2, 473587734},  {8, 230125274},  {2, 494084400}, {4, 357314010},  {8, 60361686},
      85                 :             :               {7, 640624687},  {3, 480441695},  {8, 1424447925}, {4, 752745419}, {1, 288532283},  {6, 669170574},
      86                 :             :               {5, 1900907591}, {3, 555326037},  {3, 1121014051}, {0, 545835650}, {8, 189196651},  {5, 252371575},
      87                 :             :               {0, 199163095},  {6, 558895874},  {6, 1656839784}, {6, 815175452}, {6, 718677851},  {5, 544000334},
      88                 :             :               {0, 340113484},  {6, 850744437},  {4, 496721063},  {8, 524715182}, {6, 574361898},  {6, 1642305743},
      89                 :             :               {6, 355110149},  {5, 1647379658}, {8, 1103005356}, {7, 556460625}, {3, 1139533992}, {5, 304736030},
      90                 :             :               {2, 361539446},  {2, 143720360},  {6, 201939025},  {7, 423141476}, {4, 574633709},  {3, 1412254823},
      91                 :             :               {4, 873254135},  {0, 341817335},  {6, 53501687},   {3, 179755410}, {5, 172209688},  {8, 516810279},
      92                 :             :               {4, 1228391489}, {8, 325372589},  {6, 550367589},  {0, 876291812}, {7, 412454120},  {7, 717202854},
      93                 :             :               {2, 222677843},  {6, 251778867},  {7, 842004420},  {7, 194762829}, {4, 96668841},   {1, 925485796},
      94                 :             :               {0, 792342903},  {6, 678455063},  {6, 773251385},  {5, 186617471}, {6, 883189502},  {7, 396077336},
      95                 :             :               {8, 254702874},  {0, 455592851}};
      96                 :             : 
      97                 :           2 : static std::unique_ptr<CBlockIndex> CreateBlockIndex(int nHeight, CBlockIndex* active_chain_tip) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
      98                 :             : {
      99                 :           2 :     auto index{std::make_unique<CBlockIndex>()};
     100                 :           2 :     index->nHeight = nHeight;
     101                 :           2 :     index->pprev = active_chain_tip;
     102                 :           2 :     return index;
     103                 :             : }
     104                 :             : 
     105                 :             : // Test suite for ancestor feerate transaction selection.
     106                 :             : // Implemented as an additional function, rather than a separate test case,
     107                 :             : // to allow reusing the blockchain created in CreateNewBlock_validity.
     108                 :           1 : void MinerTestingSetup::TestPackageSelection(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst)
     109                 :             : {
     110                 :           1 :     CTxMemPool& tx_mempool{MakeMempool()};
     111                 :           1 :     LOCK(tx_mempool.cs);
     112                 :             :     // Test the ancestor feerate transaction selection.
     113                 :           1 :     TestMemPoolEntryHelper entry;
     114                 :             : 
     115                 :             :     // Test that a medium fee transaction will be selected after a higher fee
     116                 :             :     // rate package with a low fee rate parent.
     117         [ +  - ]:           1 :     CMutableTransaction tx;
     118         [ +  - ]:           1 :     tx.vin.resize(1);
     119         [ +  - ]:           1 :     tx.vin[0].scriptSig = CScript() << OP_1;
     120         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[0]->GetHash();
     121                 :           1 :     tx.vin[0].prevout.n = 0;
     122         [ +  - ]:           1 :     tx.vout.resize(1);
     123         [ +  - ]:           1 :     tx.vout[0].nValue = 5000000000LL - 1000;
     124                 :             :     // This tx has a low fee: 1000 satoshis
     125         [ +  - ]:           1 :     Txid hashParentTx = tx.GetHash(); // save this txid for later use
     126   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Fee(1000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     127                 :             : 
     128                 :             :     // This tx has a medium fee: 10000 satoshis
     129         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[1]->GetHash();
     130                 :           1 :     tx.vout[0].nValue = 5000000000LL - 10000;
     131         [ +  - ]:           1 :     Txid hashMediumFeeTx = tx.GetHash();
     132   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Fee(10000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     133                 :             : 
     134                 :             :     // This tx has a high fee, but depends on the first transaction
     135         [ +  - ]:           1 :     tx.vin[0].prevout.hash = hashParentTx;
     136                 :           1 :     tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 50k satoshi fee
     137         [ +  - ]:           1 :     Txid hashHighFeeTx = tx.GetHash();
     138   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Fee(50000).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx));
     139                 :             : 
     140   [ +  -  +  - ]:           1 :     std::unique_ptr<CBlockTemplate> pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
     141   [ +  -  +  - ]:           1 :     BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 4U);
     142   [ +  -  +  -  :           2 :     BOOST_CHECK(pblocktemplate->block.vtx[1]->GetHash() == hashParentTx);
                   +  - ]
     143   [ +  -  +  -  :           2 :     BOOST_CHECK(pblocktemplate->block.vtx[2]->GetHash() == hashHighFeeTx);
                   +  - ]
     144   [ +  -  +  -  :           2 :     BOOST_CHECK(pblocktemplate->block.vtx[3]->GetHash() == hashMediumFeeTx);
                   +  - ]
     145                 :             : 
     146                 :             :     // Test that a package below the block min tx fee doesn't get included
     147         [ +  - ]:           1 :     tx.vin[0].prevout.hash = hashHighFeeTx;
     148                 :           1 :     tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 0 fee
     149         [ +  - ]:           1 :     Txid hashFreeTx = tx.GetHash();
     150   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Fee(0).FromTx(tx));
     151                 :           1 :     size_t freeTxSize = ::GetSerializeSize(TX_WITH_WITNESS(tx));
     152                 :             : 
     153                 :             :     // Calculate a fee on child transaction that will put the package just
     154                 :             :     // below the block min tx fee (assuming 1 child tx of the same size).
     155         [ +  - ]:           1 :     CAmount feeToUse = blockMinFeeRate.GetFee(2*freeTxSize) - 1;
     156                 :             : 
     157         [ +  - ]:           1 :     tx.vin[0].prevout.hash = hashFreeTx;
     158                 :           1 :     tx.vout[0].nValue = 5000000000LL - 1000 - 50000 - feeToUse;
     159         [ +  - ]:           1 :     Txid hashLowFeeTx = tx.GetHash();
     160   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Fee(feeToUse).FromTx(tx));
     161   [ +  -  +  - ]:           2 :     pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
     162                 :             :     // Verify that the free tx and the low fee tx didn't get selected
     163         [ +  + ]:           5 :     for (size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
     164   [ +  -  +  -  :           8 :         BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashFreeTx);
                   +  - ]
     165   [ +  -  +  - ]:           8 :         BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashLowFeeTx);
     166                 :             :     }
     167                 :             : 
     168                 :             :     // Test that packages above the min relay fee do get included, even if one
     169                 :             :     // of the transactions is below the min relay fee
     170                 :             :     // Remove the low fee transaction and replace with a higher fee transaction
     171   [ +  -  +  - ]:           1 :     tx_mempool.removeRecursive(CTransaction(tx), MemPoolRemovalReason::REPLACED);
     172         [ +  - ]:           1 :     tx.vout[0].nValue -= 2; // Now we should be just over the min relay fee
     173         [ +  - ]:           1 :     hashLowFeeTx = tx.GetHash();
     174   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Fee(feeToUse + 2).FromTx(tx));
     175   [ +  -  +  - ]:           2 :     pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
     176   [ +  -  +  - ]:           1 :     BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 6U);
     177   [ +  -  +  -  :           2 :     BOOST_CHECK(pblocktemplate->block.vtx[4]->GetHash() == hashFreeTx);
                   +  - ]
     178   [ +  -  +  -  :           2 :     BOOST_CHECK(pblocktemplate->block.vtx[5]->GetHash() == hashLowFeeTx);
                   +  - ]
     179                 :             : 
     180                 :             :     // Test that transaction selection properly updates ancestor fee
     181                 :             :     // calculations as ancestor transactions get included in a block.
     182                 :             :     // Add a 0-fee transaction that has 2 outputs.
     183         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[2]->GetHash();
     184         [ +  - ]:           1 :     tx.vout.resize(2);
     185         [ +  - ]:           1 :     tx.vout[0].nValue = 5000000000LL - 100000000;
     186                 :           1 :     tx.vout[1].nValue = 100000000; // 1BTC output
     187         [ +  - ]:           1 :     Txid hashFreeTx2 = tx.GetHash();
     188   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Fee(0).SpendsCoinbase(true).FromTx(tx));
     189                 :             : 
     190                 :             :     // This tx can't be mined by itself
     191         [ +  - ]:           1 :     tx.vin[0].prevout.hash = hashFreeTx2;
     192         [ +  - ]:           1 :     tx.vout.resize(1);
     193         [ +  - ]:           1 :     feeToUse = blockMinFeeRate.GetFee(freeTxSize);
     194         [ +  - ]:           1 :     tx.vout[0].nValue = 5000000000LL - 100000000 - feeToUse;
     195         [ +  - ]:           1 :     Txid hashLowFeeTx2 = tx.GetHash();
     196   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Fee(feeToUse).SpendsCoinbase(false).FromTx(tx));
     197   [ +  -  +  - ]:           2 :     pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
     198                 :             : 
     199                 :             :     // Verify that this tx isn't selected.
     200         [ +  + ]:           7 :     for (size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
     201   [ +  -  +  -  :          12 :         BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashFreeTx2);
                   +  - ]
     202   [ +  -  +  - ]:          12 :         BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashLowFeeTx2);
     203                 :             :     }
     204                 :             : 
     205                 :             :     // This tx will be mineable, and should cause hashLowFeeTx2 to be selected
     206                 :             :     // as well.
     207         [ +  - ]:           1 :     tx.vin[0].prevout.n = 1;
     208                 :           1 :     tx.vout[0].nValue = 100000000 - 10000; // 10k satoshi fee
     209   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Fee(10000).FromTx(tx));
     210   [ +  -  +  - ]:           2 :     pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
     211   [ +  -  +  - ]:           1 :     BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 9U);
     212   [ +  -  +  - ]:           2 :     BOOST_CHECK(pblocktemplate->block.vtx[8]->GetHash() == hashLowFeeTx2);
     213         [ +  - ]:           3 : }
     214                 :             : 
     215                 :           1 : void MinerTestingSetup::TestBasicMining(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst, int baseheight)
     216                 :             : {
     217                 :           1 :     Txid hash;
     218                 :           1 :     CMutableTransaction tx;
     219                 :           1 :     TestMemPoolEntryHelper entry;
     220                 :           1 :     entry.nFee = 11;
     221                 :           1 :     entry.nHeight = 11;
     222                 :             : 
     223                 :           1 :     const CAmount BLOCKSUBSIDY = 50 * COIN;
     224                 :           1 :     const CAmount LOWFEE = CENT;
     225                 :           1 :     const CAmount HIGHFEE = COIN;
     226                 :           1 :     const CAmount HIGHERFEE = 4 * COIN;
     227                 :             : 
     228                 :           1 :     {
     229         [ +  - ]:           1 :         CTxMemPool& tx_mempool{MakeMempool()};
     230         [ +  - ]:           1 :         LOCK(tx_mempool.cs);
     231                 :             : 
     232                 :             :         // Just to make sure we can still make simple blocks
     233   [ +  -  +  - ]:           1 :         auto pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
     234   [ +  -  +  -  :           2 :         BOOST_CHECK(pblocktemplate);
                   +  - ]
     235                 :             : 
     236                 :             :         // block sigops > limit: 1000 CHECKMULTISIG + 1
     237         [ +  - ]:           1 :         tx.vin.resize(1);
     238                 :             :         // NOTE: OP_NOP is used to force 20 SigOps for the CHECKMULTISIG
     239   [ +  -  +  -  :           1 :         tx.vin[0].scriptSig = CScript() << OP_0 << OP_0 << OP_0 << OP_NOP << OP_CHECKMULTISIG << OP_1;
          +  -  +  -  +  
                -  +  - ]
     240         [ +  - ]:           1 :         tx.vin[0].prevout.hash = txFirst[0]->GetHash();
     241                 :           1 :         tx.vin[0].prevout.n = 0;
     242         [ +  - ]:           1 :         tx.vout.resize(1);
     243                 :           1 :         tx.vout[0].nValue = BLOCKSUBSIDY;
     244         [ +  + ]:        1002 :         for (unsigned int i = 0; i < 1001; ++i) {
     245         [ +  - ]:        1001 :             tx.vout[0].nValue -= LOWFEE;
     246         [ +  - ]:        1001 :             hash = tx.GetHash();
     247                 :        1001 :             bool spendsCoinbase = i == 0; // only first tx spends coinbase
     248                 :             :             // If we don't set the # of sig ops in the CTxMemPoolEntry, template creation fails
     249   [ +  -  +  - ]:        1001 :             tx_mempool.addUnchecked(entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(spendsCoinbase).FromTx(tx));
     250                 :        1001 :             tx.vin[0].prevout.hash = hash;
     251                 :             :         }
     252                 :             : 
     253   [ +  -  +  -  :           3 :         BOOST_CHECK_EXCEPTION(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-blk-sigops"));
          -  +  -  -  -  
          -  -  +  +  -  
             +  -  +  - ]
     254         [ +  - ]:           1 :     }
     255                 :             : 
     256                 :           1 :     {
     257         [ +  - ]:           1 :         CTxMemPool& tx_mempool{MakeMempool()};
     258         [ +  - ]:           1 :         LOCK(tx_mempool.cs);
     259                 :             : 
     260                 :           1 :         tx.vin[0].prevout.hash = txFirst[0]->GetHash();
     261                 :           1 :         tx.vout[0].nValue = BLOCKSUBSIDY;
     262         [ +  + ]:        1002 :         for (unsigned int i = 0; i < 1001; ++i) {
     263         [ +  - ]:        1001 :             tx.vout[0].nValue -= LOWFEE;
     264         [ +  - ]:        1001 :             hash = tx.GetHash();
     265                 :        1001 :             bool spendsCoinbase = i == 0; // only first tx spends coinbase
     266                 :             :             // If we do set the # of sig ops in the CTxMemPoolEntry, template creation passes
     267   [ +  -  +  - ]:        1001 :             tx_mempool.addUnchecked(entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(spendsCoinbase).SigOpsCost(80).FromTx(tx));
     268                 :        1001 :             tx.vin[0].prevout.hash = hash;
     269                 :             :         }
     270   [ +  -  +  -  :           3 :         BOOST_CHECK(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
          +  -  +  -  +  
                      - ]
     271                 :           0 :     }
     272                 :             : 
     273                 :           1 :     {
     274         [ +  - ]:           1 :         CTxMemPool& tx_mempool{MakeMempool()};
     275         [ +  - ]:           1 :         LOCK(tx_mempool.cs);
     276                 :             : 
     277                 :             :         // block size > limit
     278                 :           1 :         tx.vin[0].scriptSig = CScript();
     279                 :             :         // 18 * (520char + DROP) + OP_1 = 9433 bytes
     280         [ +  - ]:           1 :         std::vector<unsigned char> vchData(520);
     281         [ +  + ]:          19 :         for (unsigned int i = 0; i < 18; ++i) {
     282         [ +  - ]:          18 :             tx.vin[0].scriptSig << vchData << OP_DROP;
     283                 :             :         }
     284         [ +  - ]:           1 :         tx.vin[0].scriptSig << OP_1;
     285                 :           1 :         tx.vin[0].prevout.hash = txFirst[0]->GetHash();
     286                 :           1 :         tx.vout[0].nValue = BLOCKSUBSIDY;
     287         [ +  + ]:         129 :         for (unsigned int i = 0; i < 128; ++i) {
     288         [ +  - ]:         128 :             tx.vout[0].nValue -= LOWFEE;
     289         [ +  - ]:         128 :             hash = tx.GetHash();
     290                 :         128 :             bool spendsCoinbase = i == 0; // only first tx spends coinbase
     291   [ +  -  +  - ]:         128 :             tx_mempool.addUnchecked(entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(spendsCoinbase).FromTx(tx));
     292                 :         128 :             tx.vin[0].prevout.hash = hash;
     293                 :             :         }
     294   [ +  -  +  -  :           3 :         BOOST_CHECK(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
             +  -  +  - ]
     295         [ +  - ]:           1 :     }
     296                 :             : 
     297                 :           1 :     {
     298         [ +  - ]:           1 :         CTxMemPool& tx_mempool{MakeMempool()};
     299         [ +  - ]:           1 :         LOCK(tx_mempool.cs);
     300                 :             : 
     301                 :             :         // orphan in tx_mempool, template creation fails
     302         [ +  - ]:           1 :         hash = tx.GetHash();
     303   [ +  -  +  - ]:           1 :         tx_mempool.addUnchecked(entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).FromTx(tx));
     304   [ +  -  +  -  :           3 :         BOOST_CHECK_EXCEPTION(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
          -  +  -  -  -  
          -  +  -  -  +  
          +  -  +  -  +  
                      - ]
     305                 :           0 :     }
     306                 :             : 
     307                 :           1 :     {
     308         [ +  - ]:           1 :         CTxMemPool& tx_mempool{MakeMempool()};
     309         [ +  - ]:           1 :         LOCK(tx_mempool.cs);
     310                 :             : 
     311                 :             :         // child with higher feerate than parent
     312         [ +  - ]:           1 :         tx.vin[0].scriptSig = CScript() << OP_1;
     313         [ +  - ]:           1 :         tx.vin[0].prevout.hash = txFirst[1]->GetHash();
     314                 :           1 :         tx.vout[0].nValue = BLOCKSUBSIDY - HIGHFEE;
     315         [ +  - ]:           1 :         hash = tx.GetHash();
     316   [ +  -  +  - ]:           1 :         tx_mempool.addUnchecked(entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     317         [ +  - ]:           1 :         tx.vin[0].prevout.hash = hash;
     318         [ +  - ]:           1 :         tx.vin.resize(2);
     319         [ +  - ]:           1 :         tx.vin[1].scriptSig = CScript() << OP_1;
     320         [ +  - ]:           1 :         tx.vin[1].prevout.hash = txFirst[0]->GetHash();
     321                 :           1 :         tx.vin[1].prevout.n = 0;
     322                 :           1 :         tx.vout[0].nValue = tx.vout[0].nValue + BLOCKSUBSIDY - HIGHERFEE; // First txn output + fresh coinbase - new txn fee
     323         [ +  - ]:           1 :         hash = tx.GetHash();
     324   [ +  -  +  - ]:           1 :         tx_mempool.addUnchecked(entry.Fee(HIGHERFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     325   [ +  -  +  -  :           3 :         BOOST_CHECK(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
          +  -  +  -  +  
                      - ]
     326                 :           0 :     }
     327                 :             : 
     328                 :           1 :     {
     329         [ +  - ]:           1 :         CTxMemPool& tx_mempool{MakeMempool()};
     330         [ +  - ]:           1 :         LOCK(tx_mempool.cs);
     331                 :             : 
     332                 :             :         // coinbase in tx_mempool, template creation fails
     333         [ +  - ]:           1 :         tx.vin.resize(1);
     334                 :           1 :         tx.vin[0].prevout.SetNull();
     335   [ +  -  +  - ]:           1 :         tx.vin[0].scriptSig = CScript() << OP_0 << OP_1;
     336         [ +  - ]:           1 :         tx.vout[0].nValue = 0;
     337         [ +  - ]:           1 :         hash = tx.GetHash();
     338                 :             :         // give it a fee so it'll get mined
     339   [ +  -  +  - ]:           1 :         tx_mempool.addUnchecked(entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx));
     340                 :             :         // Should throw bad-cb-multiple
     341   [ +  -  +  -  :           3 :         BOOST_CHECK_EXCEPTION(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-cb-multiple"));
          -  +  -  -  -  
          -  +  -  -  +  
          +  -  +  -  +  
                      - ]
     342                 :           0 :     }
     343                 :             : 
     344                 :           1 :     {
     345         [ +  - ]:           1 :         CTxMemPool& tx_mempool{MakeMempool()};
     346         [ +  - ]:           1 :         LOCK(tx_mempool.cs);
     347                 :             : 
     348                 :             :         // double spend txn pair in tx_mempool, template creation fails
     349         [ +  - ]:           1 :         tx.vin[0].prevout.hash = txFirst[0]->GetHash();
     350         [ +  - ]:           1 :         tx.vin[0].scriptSig = CScript() << OP_1;
     351         [ +  - ]:           1 :         tx.vout[0].nValue = BLOCKSUBSIDY - HIGHFEE;
     352         [ +  - ]:           1 :         tx.vout[0].scriptPubKey = CScript() << OP_1;
     353         [ +  - ]:           1 :         hash = tx.GetHash();
     354   [ +  -  +  - ]:           1 :         tx_mempool.addUnchecked(entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     355         [ +  - ]:           1 :         tx.vout[0].scriptPubKey = CScript() << OP_2;
     356         [ +  - ]:           1 :         hash = tx.GetHash();
     357   [ +  -  +  - ]:           1 :         tx_mempool.addUnchecked(entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     358   [ +  -  +  -  :           3 :         BOOST_CHECK_EXCEPTION(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
          -  +  -  -  -  
          -  +  -  -  +  
          +  -  +  -  +  
                      - ]
     359                 :           0 :     }
     360                 :             : 
     361                 :           1 :     {
     362         [ +  - ]:           1 :         CTxMemPool& tx_mempool{MakeMempool()};
     363         [ +  - ]:           1 :         LOCK(tx_mempool.cs);
     364                 :             : 
     365                 :             :         // subsidy changing
     366         [ +  - ]:           1 :         int nHeight = m_node.chainman->ActiveChain().Height();
     367                 :             :         // Create an actual 209999-long block chain (without valid blocks).
     368   [ +  -  +  -  :      419780 :         while (m_node.chainman->ActiveChain().Tip()->nHeight < 209999) {
                   +  + ]
     369   [ +  -  +  - ]:      209889 :             CBlockIndex* prev = m_node.chainman->ActiveChain().Tip();
     370         [ +  - ]:      209889 :             CBlockIndex* next = new CBlockIndex();
     371         [ +  - ]:      209889 :             next->phashBlock = new uint256(m_rng.rand256());
     372   [ +  -  +  -  :      209889 :             m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(next->GetBlockHash());
                   +  - ]
     373                 :      209889 :             next->pprev = prev;
     374                 :      209889 :             next->nHeight = prev->nHeight + 1;
     375         [ +  - ]:      209889 :             next->BuildSkip();
     376   [ +  -  +  - ]:      209889 :             m_node.chainman->ActiveChain().SetTip(*next);
     377                 :             :         }
     378   [ +  -  +  -  :           3 :         BOOST_CHECK(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
             +  -  +  - ]
     379                 :             :         // Extend to a 210000-long block chain.
     380   [ +  -  +  -  :           4 :         while (m_node.chainman->ActiveChain().Tip()->nHeight < 210000) {
                   +  + ]
     381   [ +  -  +  - ]:           1 :             CBlockIndex* prev = m_node.chainman->ActiveChain().Tip();
     382         [ +  - ]:           1 :             CBlockIndex* next = new CBlockIndex();
     383         [ +  - ]:           1 :             next->phashBlock = new uint256(m_rng.rand256());
     384   [ +  -  +  -  :           1 :             m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(next->GetBlockHash());
                   +  - ]
     385                 :           1 :             next->pprev = prev;
     386                 :           1 :             next->nHeight = prev->nHeight + 1;
     387         [ +  - ]:           1 :             next->BuildSkip();
     388   [ +  -  +  - ]:           1 :             m_node.chainman->ActiveChain().SetTip(*next);
     389                 :             :         }
     390   [ +  -  +  -  :           3 :         BOOST_CHECK(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
          +  -  +  -  +  
                      - ]
     391                 :             : 
     392                 :             :         // invalid p2sh txn in tx_mempool, template creation fails
     393         [ +  - ]:           1 :         tx.vin[0].prevout.hash = txFirst[0]->GetHash();
     394                 :           1 :         tx.vin[0].prevout.n = 0;
     395         [ +  - ]:           1 :         tx.vin[0].scriptSig = CScript() << OP_1;
     396         [ +  - ]:           1 :         tx.vout[0].nValue = BLOCKSUBSIDY - LOWFEE;
     397         [ +  - ]:           1 :         CScript script = CScript() << OP_0;
     398   [ +  -  +  - ]:           1 :         tx.vout[0].scriptPubKey = GetScriptForDestination(ScriptHash(script));
     399         [ +  - ]:           1 :         hash = tx.GetHash();
     400   [ +  -  +  - ]:           1 :         tx_mempool.addUnchecked(entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     401         [ -  + ]:           1 :         tx.vin[0].prevout.hash = hash;
     402   [ -  +  +  - ]:           1 :         tx.vin[0].scriptSig = CScript() << std::vector<unsigned char>(script.begin(), script.end());
     403         [ +  - ]:           1 :         tx.vout[0].nValue -= LOWFEE;
     404         [ +  - ]:           1 :         hash = tx.GetHash();
     405   [ +  -  +  - ]:           1 :         tx_mempool.addUnchecked(entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx));
     406                 :             :         // Should throw block-validation-failed
     407   [ +  -  +  -  :           3 :         BOOST_CHECK_EXCEPTION(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("block-validation-failed"));
          -  +  -  -  -  
          -  -  +  +  -  
             +  -  +  - ]
     408                 :             : 
     409                 :             :         // Delete the dummy blocks again.
     410   [ +  -  +  -  :      419782 :         while (m_node.chainman->ActiveChain().Tip()->nHeight > nHeight) {
                   +  + ]
     411   [ +  -  +  - ]:      209890 :             CBlockIndex* del = m_node.chainman->ActiveChain().Tip();
     412   [ +  -  +  -  :      209890 :             m_node.chainman->ActiveChain().SetTip(*Assert(del->pprev));
                   +  - ]
     413   [ +  -  +  -  :      209890 :             m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(del->pprev->GetBlockHash());
                   +  - ]
     414         [ +  - ]:      209890 :             delete del->phashBlock;
     415         [ +  - ]:      209890 :             delete del;
     416                 :             :         }
     417         [ +  - ]:           1 :     }
     418                 :             : 
     419         [ +  - ]:           1 :     CTxMemPool& tx_mempool{MakeMempool()};
     420         [ +  - ]:           1 :     LOCK(tx_mempool.cs);
     421                 :             : 
     422                 :             :     // non-final txs in mempool
     423   [ +  -  +  -  :           2 :     SetMockTime(m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1);
                   +  - ]
     424                 :           1 :     const int flags{LOCKTIME_VERIFY_SEQUENCE};
     425                 :             :     // height map
     426                 :           1 :     std::vector<int> prevheights;
     427                 :             : 
     428                 :             :     // relative height locked
     429                 :           1 :     tx.version = 2;
     430         [ +  - ]:           1 :     tx.vin.resize(1);
     431         [ +  - ]:           1 :     prevheights.resize(1);
     432         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[0]->GetHash(); // only 1 transaction
     433                 :           1 :     tx.vin[0].prevout.n = 0;
     434         [ +  - ]:           1 :     tx.vin[0].scriptSig = CScript() << OP_1;
     435   [ +  -  +  -  :           2 :     tx.vin[0].nSequence = m_node.chainman->ActiveChain().Tip()->nHeight + 1; // txFirst[0] is the 2nd block
                   +  - ]
     436                 :           1 :     prevheights[0] = baseheight + 1;
     437         [ +  - ]:           1 :     tx.vout.resize(1);
     438         [ +  - ]:           1 :     tx.vout[0].nValue = BLOCKSUBSIDY-HIGHFEE;
     439         [ +  - ]:           1 :     tx.vout[0].scriptPubKey = CScript() << OP_1;
     440                 :           1 :     tx.nLockTime = 0;
     441         [ +  - ]:           1 :     hash = tx.GetHash();
     442   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     443   [ +  -  +  -  :           4 :     BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     444   [ +  -  +  -  :           3 :     BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
          +  -  +  -  +  
                      - ]
     445                 :             : 
     446                 :           1 :     {
     447   [ +  -  +  - ]:           1 :         CBlockIndex* active_chain_tip = m_node.chainman->ActiveChain().Tip();
     448   [ +  -  +  -  :           4 :         BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, prevheights, *CreateBlockIndex(active_chain_tip->nHeight + 2, active_chain_tip))); // Sequence locks pass on 2nd block
          +  -  +  -  +  
                      - ]
     449                 :             :     }
     450                 :             : 
     451                 :             :     // relative time locked
     452         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[1]->GetHash();
     453   [ +  -  +  -  :           3 :     tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | (((m_node.chainman->ActiveChain().Tip()->GetMedianTimePast()+1-m_node.chainman->ActiveChain()[1]->GetMedianTimePast()) >> CTxIn::SEQUENCE_LOCKTIME_GRANULARITY) + 1); // txFirst[1] is the 3rd block
          +  -  +  -  +  
                      - ]
     454                 :           1 :     prevheights[0] = baseheight + 2;
     455         [ +  - ]:           1 :     hash = tx.GetHash();
     456   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Time(Now<NodeSeconds>()).FromTx(tx));
     457   [ +  -  +  -  :           4 :     BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     458   [ +  -  +  -  :           3 :     BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
             +  -  +  - ]
     459                 :             : 
     460                 :           1 :     const int SEQUENCE_LOCK_TIME = 512; // Sequence locks pass 512 seconds later
     461         [ +  + ]:          12 :     for (int i = 0; i < CBlockIndex::nMedianTimeSpan; ++i)
     462   [ +  -  +  -  :          33 :         m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i)->nTime += SEQUENCE_LOCK_TIME; // Trick the MedianTimePast
          +  -  +  -  +  
                      - ]
     463                 :           1 :     {
     464   [ +  -  +  - ]:           1 :         CBlockIndex* active_chain_tip = m_node.chainman->ActiveChain().Tip();
     465   [ +  -  +  -  :           3 :         BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, prevheights, *CreateBlockIndex(active_chain_tip->nHeight + 1, active_chain_tip)));
          +  -  +  -  +  
                      - ]
     466                 :             :     }
     467                 :             : 
     468         [ +  + ]:          12 :     for (int i = 0; i < CBlockIndex::nMedianTimeSpan; ++i) {
     469   [ +  -  +  -  :          33 :         CBlockIndex* ancestor{Assert(m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i))};
          +  -  +  -  +  
                -  +  - ]
     470                 :          11 :         ancestor->nTime -= SEQUENCE_LOCK_TIME; // undo tricked MTP
     471                 :             :     }
     472                 :             : 
     473                 :             :     // absolute height locked
     474         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[2]->GetHash();
     475                 :           1 :     tx.vin[0].nSequence = CTxIn::MAX_SEQUENCE_NONFINAL;
     476                 :           1 :     prevheights[0] = baseheight + 3;
     477   [ +  -  +  - ]:           1 :     tx.nLockTime = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
     478         [ +  - ]:           1 :     hash = tx.GetHash();
     479   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Time(Now<NodeSeconds>()).FromTx(tx));
     480   [ +  -  +  -  :           4 :     BOOST_CHECK(!CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime fails
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     481   [ +  -  +  -  :           3 :     BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
          +  -  +  -  +  
                      - ]
     482   [ +  -  +  -  :           5 :     BOOST_CHECK(IsFinalTx(CTransaction(tx), m_node.chainman->ActiveChain().Tip()->nHeight + 2, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast())); // Locktime passes on 2nd block
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     483                 :             : 
     484                 :             :     // absolute time locked
     485         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[3]->GetHash();
     486   [ +  -  +  - ]:           2 :     tx.nLockTime = m_node.chainman->ActiveChain().Tip()->GetMedianTimePast();
     487         [ +  - ]:           1 :     prevheights.resize(1);
     488         [ +  - ]:           1 :     prevheights[0] = baseheight + 4;
     489         [ +  - ]:           1 :     hash = tx.GetHash();
     490   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Time(Now<NodeSeconds>()).FromTx(tx));
     491   [ +  -  +  -  :           4 :     BOOST_CHECK(!CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime fails
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     492   [ +  -  +  -  :           3 :     BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
          +  -  +  -  +  
                      - ]
     493   [ +  -  +  -  :           5 :     BOOST_CHECK(IsFinalTx(CTransaction(tx), m_node.chainman->ActiveChain().Tip()->nHeight + 2, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1)); // Locktime passes 1 second later
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     494                 :             : 
     495                 :             :     // mempool-dependent transactions (not added)
     496         [ +  - ]:           1 :     tx.vin[0].prevout.hash = hash;
     497   [ +  -  +  -  :           2 :     prevheights[0] = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
                   +  - ]
     498                 :           1 :     tx.nLockTime = 0;
     499                 :           1 :     tx.vin[0].nSequence = 0;
     500   [ +  -  +  -  :           4 :     BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     501   [ +  -  +  -  :           3 :     BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
          +  -  +  -  +  
                      - ]
     502         [ +  - ]:           1 :     tx.vin[0].nSequence = 1;
     503   [ +  -  +  -  :           3 :     BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
          +  -  +  -  +  
                      - ]
     504         [ +  - ]:           1 :     tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG;
     505   [ +  -  +  -  :           3 :     BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
          +  -  +  -  +  
                      - ]
     506         [ +  - ]:           1 :     tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | 1;
     507   [ +  -  +  -  :           3 :     BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
          +  -  +  -  +  
                      - ]
     508                 :             : 
     509   [ +  -  +  - ]:           1 :     auto pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
     510   [ +  -  +  -  :           2 :     BOOST_CHECK(pblocktemplate);
                   +  - ]
     511                 :             : 
     512                 :             :     // None of the of the absolute height/time locked tx should have made
     513                 :             :     // it into the template because we still check IsFinalTx in CreateNewBlock,
     514                 :             :     // but relative locked txs will if inconsistently added to mempool.
     515                 :             :     // For now these will still generate a valid template until BIP68 soft fork
     516   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 3U);
     517                 :             :     // However if we advance height by 1 and time by SEQUENCE_LOCK_TIME, all of them should be mined
     518         [ +  + ]:          12 :     for (int i = 0; i < CBlockIndex::nMedianTimeSpan; ++i) {
     519   [ +  -  +  -  :          33 :         CBlockIndex* ancestor{Assert(m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i))};
          +  -  +  -  +  
                -  +  - ]
     520                 :          11 :         ancestor->nTime += SEQUENCE_LOCK_TIME; // Trick the MedianTimePast
     521                 :             :     }
     522   [ +  -  +  - ]:           1 :     m_node.chainman->ActiveChain().Tip()->nHeight++;
     523   [ +  -  +  -  :           2 :     SetMockTime(m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1);
                   +  - ]
     524                 :             : 
     525   [ +  -  +  -  :           4 :     BOOST_CHECK(pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
          +  -  +  -  +  
                      - ]
     526   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 5U);
     527         [ +  - ]:           3 : }
     528                 :             : 
     529                 :           1 : void MinerTestingSetup::TestPrioritisedMining(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst)
     530                 :             : {
     531                 :           1 :     CTxMemPool& tx_mempool{MakeMempool()};
     532                 :           1 :     LOCK(tx_mempool.cs);
     533                 :             : 
     534                 :           1 :     TestMemPoolEntryHelper entry;
     535                 :             : 
     536                 :             :     // Test that a tx below min fee but prioritised is included
     537         [ +  - ]:           1 :     CMutableTransaction tx;
     538         [ +  - ]:           1 :     tx.vin.resize(1);
     539         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[0]->GetHash();
     540                 :           1 :     tx.vin[0].prevout.n = 0;
     541         [ +  - ]:           1 :     tx.vin[0].scriptSig = CScript() << OP_1;
     542         [ +  - ]:           1 :     tx.vout.resize(1);
     543         [ +  - ]:           1 :     tx.vout[0].nValue = 5000000000LL; // 0 fee
     544         [ +  - ]:           1 :     uint256 hashFreePrioritisedTx = tx.GetHash();
     545   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Fee(0).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     546         [ +  - ]:           1 :     tx_mempool.PrioritiseTransaction(hashFreePrioritisedTx, 5 * COIN);
     547                 :             : 
     548         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[1]->GetHash();
     549                 :           1 :     tx.vin[0].prevout.n = 0;
     550                 :           1 :     tx.vout[0].nValue = 5000000000LL - 1000;
     551                 :             :     // This tx has a low fee: 1000 satoshis
     552         [ +  - ]:           1 :     Txid hashParentTx = tx.GetHash(); // save this txid for later use
     553   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Fee(1000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     554                 :             : 
     555                 :             :     // This tx has a medium fee: 10000 satoshis
     556         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[2]->GetHash();
     557                 :           1 :     tx.vout[0].nValue = 5000000000LL - 10000;
     558         [ +  - ]:           1 :     Txid hashMediumFeeTx = tx.GetHash();
     559   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Fee(10000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     560         [ +  - ]:           1 :     tx_mempool.PrioritiseTransaction(hashMediumFeeTx, -5 * COIN);
     561                 :             : 
     562                 :             :     // This tx also has a low fee, but is prioritised
     563         [ +  - ]:           1 :     tx.vin[0].prevout.hash = hashParentTx;
     564                 :           1 :     tx.vout[0].nValue = 5000000000LL - 1000 - 1000; // 1000 satoshi fee
     565         [ +  - ]:           1 :     Txid hashPrioritsedChild = tx.GetHash();
     566   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Fee(1000).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx));
     567         [ +  - ]:           1 :     tx_mempool.PrioritiseTransaction(hashPrioritsedChild, 2 * COIN);
     568                 :             : 
     569                 :             :     // Test that transaction selection properly updates ancestor fee calculations as prioritised
     570                 :             :     // parents get included in a block. Create a transaction with two prioritised ancestors, each
     571                 :             :     // included by itself: FreeParent <- FreeChild <- FreeGrandchild.
     572                 :             :     // When FreeParent is added, a modified entry will be created for FreeChild + FreeGrandchild
     573                 :             :     // FreeParent's prioritisation should not be included in that entry.
     574                 :             :     // When FreeChild is included, FreeChild's prioritisation should also not be included.
     575         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[3]->GetHash();
     576                 :           1 :     tx.vout[0].nValue = 5000000000LL; // 0 fee
     577         [ +  - ]:           1 :     Txid hashFreeParent = tx.GetHash();
     578   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Fee(0).SpendsCoinbase(true).FromTx(tx));
     579         [ +  - ]:           1 :     tx_mempool.PrioritiseTransaction(hashFreeParent, 10 * COIN);
     580                 :             : 
     581         [ +  - ]:           1 :     tx.vin[0].prevout.hash = hashFreeParent;
     582                 :           1 :     tx.vout[0].nValue = 5000000000LL; // 0 fee
     583         [ +  - ]:           1 :     Txid hashFreeChild = tx.GetHash();
     584   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Fee(0).SpendsCoinbase(false).FromTx(tx));
     585         [ +  - ]:           1 :     tx_mempool.PrioritiseTransaction(hashFreeChild, 1 * COIN);
     586                 :             : 
     587         [ +  - ]:           1 :     tx.vin[0].prevout.hash = hashFreeChild;
     588                 :           1 :     tx.vout[0].nValue = 5000000000LL; // 0 fee
     589         [ +  - ]:           1 :     Txid hashFreeGrandchild = tx.GetHash();
     590   [ +  -  +  - ]:           1 :     tx_mempool.addUnchecked(entry.Fee(0).SpendsCoinbase(false).FromTx(tx));
     591                 :             : 
     592   [ +  -  +  - ]:           1 :     auto pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
     593   [ +  -  +  - ]:           1 :     BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 6U);
     594   [ +  -  +  -  :           2 :     BOOST_CHECK(pblocktemplate->block.vtx[1]->GetHash() == hashFreeParent);
                   +  - ]
     595   [ +  -  +  -  :           2 :     BOOST_CHECK(pblocktemplate->block.vtx[2]->GetHash() == hashFreePrioritisedTx);
                   +  - ]
     596   [ +  -  +  -  :           2 :     BOOST_CHECK(pblocktemplate->block.vtx[3]->GetHash() == hashParentTx);
                   +  - ]
     597   [ +  -  +  -  :           2 :     BOOST_CHECK(pblocktemplate->block.vtx[4]->GetHash() == hashPrioritsedChild);
                   +  - ]
     598   [ +  -  +  - ]:           2 :     BOOST_CHECK(pblocktemplate->block.vtx[5]->GetHash() == hashFreeChild);
     599         [ +  + ]:           7 :     for (size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
     600                 :             :         // The FreeParent and FreeChild's prioritisations should not impact the child.
     601   [ +  -  +  -  :          12 :         BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashFreeGrandchild);
                   +  - ]
     602                 :             :         // De-prioritised transaction should not be included.
     603   [ +  -  +  - ]:          12 :         BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashMediumFeeTx);
     604                 :             :     }
     605         [ +  - ]:           3 : }
     606                 :             : 
     607                 :             : // NOTE: These tests rely on CreateNewBlock doing its own self-validation!
     608   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     609                 :             : {
     610                 :             :     // Note that by default, these tests run with size accounting enabled.
     611         [ +  - ]:           1 :     CScript scriptPubKey = CScript() << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"_hex << OP_CHECKSIG;
     612                 :           1 :     std::unique_ptr<CBlockTemplate> pblocktemplate;
     613                 :             : 
     614         [ +  - ]:           1 :     CTxMemPool& tx_mempool{*m_node.mempool};
     615                 :             :     // Simple block creation, nothing special yet:
     616   [ +  -  +  -  :           4 :     BOOST_CHECK(pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
             +  -  +  - ]
     617                 :             : 
     618                 :             :     // We can't make transactions until we have inputs
     619                 :             :     // Therefore, load 110 blocks :)
     620                 :           1 :     static_assert(std::size(BLOCKINFO) == 110, "Should have 110 blocks to import");
     621                 :           1 :     int baseheight = 0;
     622                 :           1 :     std::vector<CTransactionRef> txFirst;
     623         [ +  + ]:         111 :     for (const auto& bi : BLOCKINFO) {
     624         [ +  - ]:         110 :         CBlock *pblock = &pblocktemplate->block; // pointer for convenience
     625                 :         110 :         {
     626         [ +  - ]:         110 :             LOCK(cs_main);
     627                 :         110 :             pblock->nVersion = VERSIONBITS_TOP_BITS;
     628   [ +  -  +  - ]:         220 :             pblock->nTime = m_node.chainman->ActiveChain().Tip()->GetMedianTimePast()+1;
     629         [ +  - ]:         110 :             CMutableTransaction txCoinbase(*pblock->vtx[0]);
     630                 :         110 :             txCoinbase.version = 1;
     631   [ +  -  +  -  :         110 :             txCoinbase.vin[0].scriptSig = CScript{} << (m_node.chainman->ActiveChain().Height() + 1) << bi.extranonce;
                   +  - ]
     632         [ +  - ]:         110 :             txCoinbase.vout.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this)
     633                 :         110 :             txCoinbase.vout[0].scriptPubKey = CScript();
     634   [ +  -  -  + ]:         220 :             pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
     635         [ +  + ]:         110 :             if (txFirst.size() == 0)
     636         [ +  - ]:           1 :                 baseheight = m_node.chainman->ActiveChain().Height();
     637         [ +  + ]:         110 :             if (txFirst.size() < 4)
     638         [ +  - ]:           4 :                 txFirst.push_back(pblock->vtx[0]);
     639         [ +  - ]:         110 :             pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
     640                 :         110 :             pblock->nNonce = bi.nonce;
     641         [ +  - ]:         110 :         }
     642         [ +  - ]:         110 :         std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(*pblock);
     643   [ +  -  +  -  :         220 :         BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(shared_pblock, true, true, nullptr));
          +  -  +  -  +  
                      - ]
     644         [ +  - ]:         110 :         pblock->hashPrevBlock = pblock->GetHash();
     645                 :         110 :     }
     646                 :             : 
     647         [ +  - ]:           1 :     LOCK(cs_main);
     648                 :             : 
     649         [ +  - ]:           1 :     TestBasicMining(scriptPubKey, txFirst, baseheight);
     650                 :             : 
     651   [ +  -  +  - ]:           1 :     m_node.chainman->ActiveChain().Tip()->nHeight--;
     652         [ +  - ]:           1 :     SetMockTime(0);
     653                 :             : 
     654         [ +  - ]:           1 :     TestPackageSelection(scriptPubKey, txFirst);
     655                 :             : 
     656   [ +  -  +  - ]:           1 :     m_node.chainman->ActiveChain().Tip()->nHeight--;
     657         [ +  - ]:           1 :     SetMockTime(0);
     658                 :             : 
     659         [ +  - ]:           1 :     TestPrioritisedMining(scriptPubKey, txFirst);
     660                 :           1 : }
     661                 :             : 
     662                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1