LCOV - code coverage report
Current view: top level - src/test - miner_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 98.5 % 472 465
Test Date: 2025-01-19 05:08:01 Functions: 100.0 % 8 8
Branches: 50.6 % 1414 715

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

Generated by: LCOV version 2.0-1