LCOV - code coverage report
Current view: top level - src/test - miner_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 98.2 % 504 495
Test Date: 2025-06-25 04:50:20 Functions: 100.0 % 8 8
Branches: 50.7 % 1496 758

             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                 :             : #include <pow.h>
      26                 :             : 
      27                 :             : #include <test/util/setup_common.h>
      28                 :             : 
      29                 :             : #include <memory>
      30                 :             : #include <vector>
      31                 :             : 
      32                 :             : #include <boost/test/unit_test.hpp>
      33                 :             : 
      34                 :             : using namespace util::hex_literals;
      35                 :             : using interfaces::BlockTemplate;
      36                 :             : using interfaces::Mining;
      37                 :             : using node::BlockAssembler;
      38                 :             : 
      39                 :             : namespace miner_tests {
      40                 :           2 : struct MinerTestingSetup : public TestingSetup {
      41                 :             :     void TestPackageSelection(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
      42                 :             :     void TestBasicMining(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst, int baseheight) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
      43                 :             :     void TestPrioritisedMining(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
      44                 :           8 :     bool TestSequenceLocks(const CTransaction& tx, CTxMemPool& tx_mempool) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
      45                 :             :     {
      46                 :           8 :         CCoinsViewMemPool view_mempool{&m_node.chainman->ActiveChainstate().CoinsTip(), tx_mempool};
      47   [ +  -  +  - ]:           8 :         CBlockIndex* tip{m_node.chainman->ActiveChain().Tip()};
      48         [ +  - ]:           8 :         const std::optional<LockPoints> lock_points{CalculateLockPointsAtTip(tip, view_mempool, tx)};
      49   [ +  -  +  -  :          16 :         return lock_points.has_value() && CheckSequenceLocksAtTip(tip, *lock_points);
                   +  + ]
      50                 :           8 :     }
      51                 :          11 :     CTxMemPool& MakeMempool()
      52                 :             :     {
      53                 :             :         // Delete the previous mempool to ensure with valgrind that the old
      54                 :             :         // pointer is not accessed, when the new one should be accessed
      55                 :             :         // instead.
      56         [ +  - ]:          11 :         m_node.mempool.reset();
      57         [ +  - ]:          11 :         bilingual_str error;
      58   [ +  -  +  - ]:          11 :         m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(m_node), error);
      59         [ +  - ]:          11 :         Assert(error.empty());
      60                 :          11 :         return *m_node.mempool;
      61                 :          11 :     }
      62                 :           4 :     std::unique_ptr<Mining> MakeMining()
      63                 :             :     {
      64                 :           4 :         return interfaces::MakeMining(m_node);
      65                 :             :     }
      66                 :             : };
      67                 :             : } // namespace miner_tests
      68                 :             : 
      69                 :             : BOOST_FIXTURE_TEST_SUITE(miner_tests, MinerTestingSetup)
      70                 :             : 
      71                 :             : static CFeeRate blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
      72                 :             : 
      73                 :             : constexpr static struct {
      74                 :             :     unsigned int extranonce;
      75                 :             :     unsigned int nonce;
      76                 :             : } BLOCKINFO[]{{0, 3552706918},   {500, 37506755},   {1000, 948987788}, {400, 524762339},  {800, 258510074},  {300, 102309278},
      77                 :             :               {1300, 54365202},  {600, 1107740426}, {1000, 203094491}, {900, 391178848},  {800, 381177271},  {600, 87188412},
      78                 :             :               {0, 66522866},     {800, 874942736},  {1000, 89200838},  {400, 312638088},  {400, 66263693},   {500, 924648304},
      79                 :             :               {400, 369913599},  {500, 47630099},   {500, 115045364},  {100, 277026602},  {1100, 809621409}, {700, 155345322},
      80                 :             :               {800, 943579953},  {400, 28200730},   {900, 77200495},   {0, 105935488},    {400, 698721821},  {500, 111098863},
      81                 :             :               {1300, 445389594}, {500, 621849894},  {1400, 56010046},  {1100, 370669776}, {1200, 380301940}, {1200, 110654905},
      82                 :             :               {400, 213771024},  {1500, 120014726}, {1200, 835019014}, {1500, 624817237}, {900, 1404297},    {400, 189414558},
      83                 :             :               {400, 293178348},  {1100, 15393789},  {600, 396764180},  {800, 1387046371}, {800, 199368303},  {700, 111496662},
      84                 :             :               {100, 129759616},  {200, 536577982},  {500, 125881300},  {500, 101053391},  {1200, 471590548}, {900, 86957729},
      85                 :             :               {1200, 179604104}, {600, 68658642},   {1000, 203295701}, {500, 139615361},  {900, 233693412},  {300, 153225163},
      86                 :             :               {0, 27616254},     {1200, 9856191},   {100, 220392722},  {200, 66257599},   {1100, 145489641}, {1300, 37859442},
      87                 :             :               {400, 5816075},    {1200, 215752117}, {1400, 32361482},  {1400, 6529223},   {500, 143332977},  {800, 878392},
      88                 :             :               {700, 159290408},  {400, 123197595},  {700, 43988693},   {300, 304224916},  {700, 214771621},  {1100, 274148273},
      89                 :             :               {400, 285632418},  {1100, 923451065}, {600, 12818092},   {1200, 736282054}, {1000, 246683167}, {600, 92950402},
      90                 :             :               {1400, 29223405},  {1000, 841327192}, {700, 174301283},  {1400, 214009854}, {1000, 6989517},   {1200, 278226956},
      91                 :             :               {700, 540219613},  {400, 93663104},   {1100, 152345635}, {1500, 464194499}, {1300, 333850111}, {600, 258311263},
      92                 :             :               {600, 90173162},   {1000, 33590797},  {1500, 332866027}, {100, 204704427},  {1000, 463153545}, {800, 303244785},
      93                 :             :               {600, 88096214},   {0, 137477892},    {1200, 195514506}, {300, 704114595},  {900, 292087369},  {1400, 758684870},
      94                 :             :               {1300, 163493028}, {1200, 53151293}};
      95                 :             : 
      96                 :           2 : static std::unique_ptr<CBlockIndex> CreateBlockIndex(int nHeight, CBlockIndex* active_chain_tip) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
      97                 :             : {
      98                 :           2 :     auto index{std::make_unique<CBlockIndex>()};
      99                 :           2 :     index->nHeight = nHeight;
     100                 :           2 :     index->pprev = active_chain_tip;
     101                 :           2 :     return index;
     102                 :             : }
     103                 :             : 
     104                 :             : // Test suite for ancestor feerate transaction selection.
     105                 :             : // Implemented as an additional function, rather than a separate test case,
     106                 :             : // to allow reusing the blockchain created in CreateNewBlock_validity.
     107                 :           1 : void MinerTestingSetup::TestPackageSelection(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst)
     108                 :             : {
     109                 :           1 :     CTxMemPool& tx_mempool{MakeMempool()};
     110                 :           1 :     auto mining{MakeMining()};
     111         [ +  - ]:           1 :     BlockAssembler::Options options;
     112                 :           1 :     options.coinbase_output_script = scriptPubKey;
     113                 :             : 
     114         [ +  - ]:           1 :     LOCK(tx_mempool.cs);
     115                 :             :     // Test the ancestor feerate transaction selection.
     116                 :           1 :     TestMemPoolEntryHelper entry;
     117                 :             : 
     118                 :             :     // Test that a medium fee transaction will be selected after a higher fee
     119                 :             :     // rate package with a low fee rate parent.
     120         [ +  - ]:           1 :     CMutableTransaction tx;
     121         [ +  - ]:           1 :     tx.vin.resize(1);
     122         [ +  - ]:           1 :     tx.vin[0].scriptSig = CScript() << OP_1;
     123         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[0]->GetHash();
     124                 :           1 :     tx.vin[0].prevout.n = 0;
     125         [ +  - ]:           1 :     tx.vout.resize(1);
     126         [ +  - ]:           1 :     tx.vout[0].nValue = 5000000000LL - 1000;
     127                 :             :     // This tx has a low fee: 1000 satoshis
     128         [ +  - ]:           1 :     Txid hashParentTx = tx.GetHash(); // save this txid for later use
     129         [ +  - ]:           1 :     const auto parent_tx{entry.Fee(1000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx)};
     130         [ +  - ]:           1 :     AddToMempool(tx_mempool, parent_tx);
     131                 :             : 
     132                 :             :     // This tx has a medium fee: 10000 satoshis
     133         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[1]->GetHash();
     134                 :           1 :     tx.vout[0].nValue = 5000000000LL - 10000;
     135         [ +  - ]:           1 :     Txid hashMediumFeeTx = tx.GetHash();
     136         [ +  - ]:           1 :     const auto medium_fee_tx{entry.Fee(10000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx)};
     137         [ +  - ]:           1 :     AddToMempool(tx_mempool, medium_fee_tx);
     138                 :             : 
     139                 :             :     // This tx has a high fee, but depends on the first transaction
     140         [ +  - ]:           1 :     tx.vin[0].prevout.hash = hashParentTx;
     141                 :           1 :     tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 50k satoshi fee
     142         [ +  - ]:           1 :     Txid hashHighFeeTx = tx.GetHash();
     143         [ +  - ]:           1 :     const auto high_fee_tx{entry.Fee(50000).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx)};
     144         [ +  - ]:           1 :     AddToMempool(tx_mempool, high_fee_tx);
     145                 :             : 
     146         [ +  - ]:           1 :     std::unique_ptr<BlockTemplate> block_template = mining->createNewBlock(options);
     147   [ +  -  +  -  :           2 :     BOOST_REQUIRE(block_template);
                   +  - ]
     148         [ +  - ]:           1 :     CBlock block{block_template->getBlock()};
     149   [ +  -  +  - ]:           1 :     BOOST_REQUIRE_EQUAL(block.vtx.size(), 4U);
     150   [ +  -  +  -  :           2 :     BOOST_CHECK(block.vtx[1]->GetHash() == hashParentTx);
                   +  - ]
     151   [ +  -  +  -  :           2 :     BOOST_CHECK(block.vtx[2]->GetHash() == hashHighFeeTx);
                   +  - ]
     152   [ +  -  +  -  :           2 :     BOOST_CHECK(block.vtx[3]->GetHash() == hashMediumFeeTx);
                   +  - ]
     153                 :             : 
     154                 :             :     // Test the inclusion of package feerates in the block template and ensure they are sequential.
     155   [ +  -  +  -  :           1 :     const auto block_package_feerates = BlockAssembler{m_node.chainman->ActiveChainstate(), &tx_mempool, options}.CreateNewBlock()->m_package_feerates;
             +  -  +  - ]
     156   [ +  -  +  -  :           2 :     BOOST_CHECK(block_package_feerates.size() == 2);
                   +  - ]
     157                 :             : 
     158                 :             :     // parent_tx and high_fee_tx are added to the block as a package.
     159                 :           1 :     const auto combined_txs_fee = parent_tx.GetFee() + high_fee_tx.GetFee();
     160   [ +  -  +  - ]:           1 :     const auto combined_txs_size = parent_tx.GetTxSize() + high_fee_tx.GetTxSize();
     161         [ +  - ]:           1 :     FeeFrac package_feefrac{combined_txs_fee, combined_txs_size};
     162                 :             :     // The package should be added first.
     163   [ +  -  +  -  :           3 :     BOOST_CHECK(block_package_feerates[0] == package_feefrac);
             +  -  +  - ]
     164                 :             : 
     165                 :             :     // The medium_fee_tx should be added next.
     166   [ +  -  +  - ]:           1 :     FeeFrac medium_tx_feefrac{medium_fee_tx.GetFee(), medium_fee_tx.GetTxSize()};
     167   [ +  -  +  -  :           3 :     BOOST_CHECK(block_package_feerates[1] == medium_tx_feefrac);
             +  -  +  - ]
     168                 :             : 
     169                 :             :     // Test that a package below the block min tx fee doesn't get included
     170         [ +  - ]:           1 :     tx.vin[0].prevout.hash = hashHighFeeTx;
     171                 :           1 :     tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 0 fee
     172         [ +  - ]:           1 :     Txid hashFreeTx = tx.GetHash();
     173   [ +  -  +  - ]:           1 :     AddToMempool(tx_mempool, entry.Fee(0).FromTx(tx));
     174                 :           1 :     size_t freeTxSize = ::GetSerializeSize(TX_WITH_WITNESS(tx));
     175                 :             : 
     176                 :             :     // Calculate a fee on child transaction that will put the package just
     177                 :             :     // below the block min tx fee (assuming 1 child tx of the same size).
     178         [ +  - ]:           1 :     CAmount feeToUse = blockMinFeeRate.GetFee(2*freeTxSize) - 1;
     179                 :             : 
     180         [ +  - ]:           1 :     tx.vin[0].prevout.hash = hashFreeTx;
     181                 :           1 :     tx.vout[0].nValue = 5000000000LL - 1000 - 50000 - feeToUse;
     182         [ +  - ]:           1 :     Txid hashLowFeeTx = tx.GetHash();
     183   [ +  -  +  - ]:           1 :     AddToMempool(tx_mempool, entry.Fee(feeToUse).FromTx(tx));
     184                 :             : 
     185                 :             :     // waitNext() should return nullptr because there is no better template
     186         [ +  - ]:           1 :     auto should_be_nullptr = block_template->waitNext({.timeout = MillisecondsDouble{0}, .fee_threshold = 1});
     187   [ +  -  +  -  :           2 :     BOOST_REQUIRE(should_be_nullptr == nullptr);
                   +  - ]
     188                 :             : 
     189         [ +  - ]:           1 :     block = block_template->getBlock();
     190                 :             :     // Verify that the free tx and the low fee tx didn't get selected
     191         [ +  + ]:           5 :     for (size_t i=0; i<block.vtx.size(); ++i) {
     192   [ +  -  +  -  :           8 :         BOOST_CHECK(block.vtx[i]->GetHash() != hashFreeTx);
                   +  - ]
     193   [ +  -  +  - ]:           8 :         BOOST_CHECK(block.vtx[i]->GetHash() != hashLowFeeTx);
     194                 :             :     }
     195                 :             : 
     196                 :             :     // Test that packages above the min relay fee do get included, even if one
     197                 :             :     // of the transactions is below the min relay fee
     198                 :             :     // Remove the low fee transaction and replace with a higher fee transaction
     199   [ +  -  +  - ]:           1 :     tx_mempool.removeRecursive(CTransaction(tx), MemPoolRemovalReason::REPLACED);
     200         [ +  - ]:           1 :     tx.vout[0].nValue -= 2; // Now we should be just over the min relay fee
     201         [ +  - ]:           1 :     hashLowFeeTx = tx.GetHash();
     202   [ +  -  +  - ]:           1 :     AddToMempool(tx_mempool, entry.Fee(feeToUse + 2).FromTx(tx));
     203                 :             : 
     204                 :             :     // waitNext() should return if fees for the new template are at least 1 sat up
     205         [ +  - ]:           2 :     block_template = block_template->waitNext({.fee_threshold = 1});
     206   [ +  -  +  -  :           2 :     BOOST_REQUIRE(block_template);
                   +  - ]
     207         [ +  - ]:           1 :     block = block_template->getBlock();
     208   [ +  -  +  - ]:           1 :     BOOST_REQUIRE_EQUAL(block.vtx.size(), 6U);
     209   [ +  -  +  -  :           2 :     BOOST_CHECK(block.vtx[4]->GetHash() == hashFreeTx);
                   +  - ]
     210   [ +  -  +  -  :           2 :     BOOST_CHECK(block.vtx[5]->GetHash() == hashLowFeeTx);
                   +  - ]
     211                 :             : 
     212                 :             :     // Test that transaction selection properly updates ancestor fee
     213                 :             :     // calculations as ancestor transactions get included in a block.
     214                 :             :     // Add a 0-fee transaction that has 2 outputs.
     215         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[2]->GetHash();
     216         [ +  - ]:           1 :     tx.vout.resize(2);
     217         [ +  - ]:           1 :     tx.vout[0].nValue = 5000000000LL - 100000000;
     218                 :           1 :     tx.vout[1].nValue = 100000000; // 1BTC output
     219         [ +  - ]:           1 :     Txid hashFreeTx2 = tx.GetHash();
     220   [ +  -  +  - ]:           1 :     AddToMempool(tx_mempool, entry.Fee(0).SpendsCoinbase(true).FromTx(tx));
     221                 :             : 
     222                 :             :     // This tx can't be mined by itself
     223         [ +  - ]:           1 :     tx.vin[0].prevout.hash = hashFreeTx2;
     224         [ +  - ]:           1 :     tx.vout.resize(1);
     225         [ +  - ]:           1 :     feeToUse = blockMinFeeRate.GetFee(freeTxSize);
     226         [ +  - ]:           1 :     tx.vout[0].nValue = 5000000000LL - 100000000 - feeToUse;
     227         [ +  - ]:           1 :     Txid hashLowFeeTx2 = tx.GetHash();
     228   [ +  -  +  - ]:           1 :     AddToMempool(tx_mempool, entry.Fee(feeToUse).SpendsCoinbase(false).FromTx(tx));
     229         [ +  - ]:           2 :     block_template = mining->createNewBlock(options);
     230   [ +  -  +  -  :           2 :     BOOST_REQUIRE(block_template);
                   +  - ]
     231         [ +  - ]:           1 :     block = block_template->getBlock();
     232                 :             : 
     233                 :             :     // Verify that this tx isn't selected.
     234         [ +  + ]:           7 :     for (size_t i=0; i<block.vtx.size(); ++i) {
     235   [ +  -  +  -  :          12 :         BOOST_CHECK(block.vtx[i]->GetHash() != hashFreeTx2);
                   +  - ]
     236   [ +  -  +  - ]:          12 :         BOOST_CHECK(block.vtx[i]->GetHash() != hashLowFeeTx2);
     237                 :             :     }
     238                 :             : 
     239                 :             :     // This tx will be mineable, and should cause hashLowFeeTx2 to be selected
     240                 :             :     // as well.
     241         [ +  - ]:           1 :     tx.vin[0].prevout.n = 1;
     242                 :           1 :     tx.vout[0].nValue = 100000000 - 10000; // 10k satoshi fee
     243   [ +  -  +  - ]:           1 :     AddToMempool(tx_mempool, entry.Fee(10000).FromTx(tx));
     244         [ +  - ]:           2 :     block_template = mining->createNewBlock(options);
     245   [ +  -  +  -  :           2 :     BOOST_REQUIRE(block_template);
                   +  - ]
     246         [ +  - ]:           1 :     block = block_template->getBlock();
     247   [ +  -  +  - ]:           1 :     BOOST_REQUIRE_EQUAL(block.vtx.size(), 9U);
     248   [ +  -  +  - ]:           2 :     BOOST_CHECK(block.vtx[8]->GetHash() == hashLowFeeTx2);
     249         [ +  - ]:           3 : }
     250                 :             : 
     251                 :           1 : void MinerTestingSetup::TestBasicMining(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst, int baseheight)
     252                 :             : {
     253                 :           1 :     Txid hash;
     254                 :           1 :     CMutableTransaction tx;
     255                 :           1 :     TestMemPoolEntryHelper entry;
     256                 :           1 :     entry.nFee = 11;
     257                 :           1 :     entry.nHeight = 11;
     258                 :             : 
     259                 :           1 :     const CAmount BLOCKSUBSIDY = 50 * COIN;
     260                 :           1 :     const CAmount LOWFEE = CENT;
     261                 :           1 :     const CAmount HIGHFEE = COIN;
     262                 :           1 :     const CAmount HIGHERFEE = 4 * COIN;
     263                 :             : 
     264         [ +  - ]:           1 :     auto mining{MakeMining()};
     265   [ +  -  +  -  :           2 :     BOOST_REQUIRE(mining);
                   +  - ]
     266                 :             : 
     267         [ +  - ]:           1 :     BlockAssembler::Options options;
     268                 :           1 :     options.coinbase_output_script = scriptPubKey;
     269                 :             : 
     270                 :           1 :     {
     271         [ +  - ]:           1 :         CTxMemPool& tx_mempool{MakeMempool()};
     272         [ +  - ]:           1 :         LOCK(tx_mempool.cs);
     273                 :             : 
     274                 :             :         // Just to make sure we can still make simple blocks
     275         [ +  - ]:           1 :         auto block_template{mining->createNewBlock(options)};
     276   [ +  -  +  -  :           2 :         BOOST_REQUIRE(block_template);
                   +  - ]
     277         [ +  - ]:           1 :         CBlock block{block_template->getBlock()};
     278                 :             : 
     279                 :             :         // block sigops > limit: 1000 CHECKMULTISIG + 1
     280         [ +  - ]:           1 :         tx.vin.resize(1);
     281                 :             :         // NOTE: OP_NOP is used to force 20 SigOps for the CHECKMULTISIG
     282   [ +  -  +  -  :           1 :         tx.vin[0].scriptSig = CScript() << OP_0 << OP_0 << OP_0 << OP_NOP << OP_CHECKMULTISIG << OP_1;
          +  -  +  -  +  
                -  +  - ]
     283         [ +  - ]:           1 :         tx.vin[0].prevout.hash = txFirst[0]->GetHash();
     284                 :           1 :         tx.vin[0].prevout.n = 0;
     285         [ +  - ]:           1 :         tx.vout.resize(1);
     286                 :           1 :         tx.vout[0].nValue = BLOCKSUBSIDY;
     287         [ +  + ]:        1002 :         for (unsigned int i = 0; i < 1001; ++i) {
     288         [ +  - ]:        1001 :             tx.vout[0].nValue -= LOWFEE;
     289         [ +  - ]:        1001 :             hash = tx.GetHash();
     290                 :        1001 :             bool spendsCoinbase = i == 0; // only first tx spends coinbase
     291                 :             :             // If we don't set the # of sig ops in the CTxMemPoolEntry, template creation fails
     292   [ +  -  +  - ]:        1001 :             AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(spendsCoinbase).FromTx(tx));
     293                 :        1001 :             tx.vin[0].prevout.hash = hash;
     294                 :             :         }
     295                 :             : 
     296   [ +  -  -  +  :           2 :         BOOST_CHECK_EXCEPTION(mining->createNewBlock(options), std::runtime_error, HasReason("bad-blk-sigops"));
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     297         [ +  - ]:           1 :     }
     298                 :             : 
     299                 :           1 :     {
     300         [ +  - ]:           1 :         CTxMemPool& tx_mempool{MakeMempool()};
     301         [ +  - ]:           1 :         LOCK(tx_mempool.cs);
     302                 :             : 
     303                 :           1 :         tx.vin[0].prevout.hash = txFirst[0]->GetHash();
     304                 :           1 :         tx.vout[0].nValue = BLOCKSUBSIDY;
     305         [ +  + ]:        1002 :         for (unsigned int i = 0; i < 1001; ++i) {
     306         [ +  - ]:        1001 :             tx.vout[0].nValue -= LOWFEE;
     307         [ +  - ]:        1001 :             hash = tx.GetHash();
     308                 :        1001 :             bool spendsCoinbase = i == 0; // only first tx spends coinbase
     309                 :             :             // If we do set the # of sig ops in the CTxMemPoolEntry, template creation passes
     310   [ +  -  +  - ]:        1001 :             AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(spendsCoinbase).SigOpsCost(80).FromTx(tx));
     311                 :        1001 :             tx.vin[0].prevout.hash = hash;
     312                 :             :         }
     313   [ +  -  +  -  :           2 :         BOOST_REQUIRE(mining->createNewBlock(options));
             +  -  +  - ]
     314                 :           0 :     }
     315                 :             : 
     316                 :           1 :     {
     317         [ +  - ]:           1 :         CTxMemPool& tx_mempool{MakeMempool()};
     318         [ +  - ]:           1 :         LOCK(tx_mempool.cs);
     319                 :             : 
     320                 :             :         // block size > limit
     321                 :           1 :         tx.vin[0].scriptSig = CScript();
     322                 :             :         // 18 * (520char + DROP) + OP_1 = 9433 bytes
     323         [ +  - ]:           1 :         std::vector<unsigned char> vchData(520);
     324         [ +  + ]:          19 :         for (unsigned int i = 0; i < 18; ++i) {
     325         [ +  - ]:          18 :             tx.vin[0].scriptSig << vchData << OP_DROP;
     326                 :             :         }
     327         [ +  - ]:           1 :         tx.vin[0].scriptSig << OP_1;
     328                 :           1 :         tx.vin[0].prevout.hash = txFirst[0]->GetHash();
     329                 :           1 :         tx.vout[0].nValue = BLOCKSUBSIDY;
     330         [ +  + ]:         129 :         for (unsigned int i = 0; i < 128; ++i) {
     331         [ +  - ]:         128 :             tx.vout[0].nValue -= LOWFEE;
     332         [ +  - ]:         128 :             hash = tx.GetHash();
     333                 :         128 :             bool spendsCoinbase = i == 0; // only first tx spends coinbase
     334   [ +  -  +  - ]:         128 :             AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(spendsCoinbase).FromTx(tx));
     335                 :         128 :             tx.vin[0].prevout.hash = hash;
     336                 :             :         }
     337   [ +  -  +  -  :           2 :         BOOST_REQUIRE(mining->createNewBlock(options));
                   +  - ]
     338         [ +  - ]:           1 :     }
     339                 :             : 
     340                 :           1 :     {
     341         [ +  - ]:           1 :         CTxMemPool& tx_mempool{MakeMempool()};
     342         [ +  - ]:           1 :         LOCK(tx_mempool.cs);
     343                 :             : 
     344                 :             :         // orphan in tx_mempool, template creation fails
     345         [ +  - ]:           1 :         hash = tx.GetHash();
     346   [ +  -  +  - ]:           1 :         AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).FromTx(tx));
     347   [ +  -  -  +  :           2 :         BOOST_CHECK_EXCEPTION(mining->createNewBlock(options), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
          -  -  -  -  +  
          -  -  +  +  -  
             +  -  +  - ]
     348                 :           0 :     }
     349                 :             : 
     350                 :           1 :     {
     351         [ +  - ]:           1 :         CTxMemPool& tx_mempool{MakeMempool()};
     352         [ +  - ]:           1 :         LOCK(tx_mempool.cs);
     353                 :             : 
     354                 :             :         // child with higher feerate than parent
     355         [ +  - ]:           1 :         tx.vin[0].scriptSig = CScript() << OP_1;
     356         [ +  - ]:           1 :         tx.vin[0].prevout.hash = txFirst[1]->GetHash();
     357                 :           1 :         tx.vout[0].nValue = BLOCKSUBSIDY - HIGHFEE;
     358         [ +  - ]:           1 :         hash = tx.GetHash();
     359   [ +  -  +  - ]:           1 :         AddToMempool(tx_mempool, entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     360         [ +  - ]:           1 :         tx.vin[0].prevout.hash = hash;
     361         [ +  - ]:           1 :         tx.vin.resize(2);
     362         [ +  - ]:           1 :         tx.vin[1].scriptSig = CScript() << OP_1;
     363         [ +  - ]:           1 :         tx.vin[1].prevout.hash = txFirst[0]->GetHash();
     364                 :           1 :         tx.vin[1].prevout.n = 0;
     365                 :           1 :         tx.vout[0].nValue = tx.vout[0].nValue + BLOCKSUBSIDY - HIGHERFEE; // First txn output + fresh coinbase - new txn fee
     366         [ +  - ]:           1 :         hash = tx.GetHash();
     367   [ +  -  +  - ]:           1 :         AddToMempool(tx_mempool, entry.Fee(HIGHERFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     368   [ +  -  +  -  :           2 :         BOOST_REQUIRE(mining->createNewBlock(options));
             +  -  +  - ]
     369                 :           0 :     }
     370                 :             : 
     371                 :           1 :     {
     372         [ +  - ]:           1 :         CTxMemPool& tx_mempool{MakeMempool()};
     373         [ +  - ]:           1 :         LOCK(tx_mempool.cs);
     374                 :             : 
     375                 :             :         // coinbase in tx_mempool, template creation fails
     376         [ +  - ]:           1 :         tx.vin.resize(1);
     377                 :           1 :         tx.vin[0].prevout.SetNull();
     378   [ +  -  +  - ]:           1 :         tx.vin[0].scriptSig = CScript() << OP_0 << OP_1;
     379         [ +  - ]:           1 :         tx.vout[0].nValue = 0;
     380         [ +  - ]:           1 :         hash = tx.GetHash();
     381                 :             :         // give it a fee so it'll get mined
     382   [ +  -  +  - ]:           1 :         AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx));
     383                 :             :         // Should throw bad-cb-multiple
     384   [ +  -  -  +  :           2 :         BOOST_CHECK_EXCEPTION(mining->createNewBlock(options), std::runtime_error, HasReason("bad-cb-multiple"));
          -  -  -  -  +  
          -  -  +  +  -  
             +  -  +  - ]
     385                 :           0 :     }
     386                 :             : 
     387                 :           1 :     {
     388         [ +  - ]:           1 :         CTxMemPool& tx_mempool{MakeMempool()};
     389         [ +  - ]:           1 :         LOCK(tx_mempool.cs);
     390                 :             : 
     391                 :             :         // double spend txn pair in tx_mempool, template creation fails
     392         [ +  - ]:           1 :         tx.vin[0].prevout.hash = txFirst[0]->GetHash();
     393         [ +  - ]:           1 :         tx.vin[0].scriptSig = CScript() << OP_1;
     394         [ +  - ]:           1 :         tx.vout[0].nValue = BLOCKSUBSIDY - HIGHFEE;
     395         [ +  - ]:           1 :         tx.vout[0].scriptPubKey = CScript() << OP_1;
     396         [ +  - ]:           1 :         hash = tx.GetHash();
     397   [ +  -  +  - ]:           1 :         AddToMempool(tx_mempool, entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     398         [ +  - ]:           1 :         tx.vout[0].scriptPubKey = CScript() << OP_2;
     399         [ +  - ]:           1 :         hash = tx.GetHash();
     400   [ +  -  +  - ]:           1 :         AddToMempool(tx_mempool, entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     401   [ +  -  -  +  :           2 :         BOOST_CHECK_EXCEPTION(mining->createNewBlock(options), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
          -  -  -  -  +  
          -  -  +  +  -  
             +  -  +  - ]
     402                 :           0 :     }
     403                 :             : 
     404                 :           1 :     {
     405         [ +  - ]:           1 :         CTxMemPool& tx_mempool{MakeMempool()};
     406         [ +  - ]:           1 :         LOCK(tx_mempool.cs);
     407                 :             : 
     408                 :             :         // subsidy changing
     409         [ +  - ]:           1 :         int nHeight = m_node.chainman->ActiveChain().Height();
     410                 :             :         // Create an actual 209999-long block chain (without valid blocks).
     411   [ +  -  +  -  :      419780 :         while (m_node.chainman->ActiveChain().Tip()->nHeight < 209999) {
                   +  + ]
     412   [ +  -  +  - ]:      209889 :             CBlockIndex* prev = m_node.chainman->ActiveChain().Tip();
     413         [ +  - ]:      209889 :             CBlockIndex* next = new CBlockIndex();
     414         [ +  - ]:      209889 :             next->phashBlock = new uint256(m_rng.rand256());
     415   [ +  -  +  -  :      209889 :             m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(next->GetBlockHash());
                   +  - ]
     416                 :      209889 :             next->pprev = prev;
     417                 :      209889 :             next->nHeight = prev->nHeight + 1;
     418         [ +  - ]:      209889 :             next->BuildSkip();
     419   [ +  -  +  - ]:      209889 :             m_node.chainman->ActiveChain().SetTip(*next);
     420                 :             :         }
     421   [ +  -  +  -  :           2 :         BOOST_REQUIRE(mining->createNewBlock(options));
                   +  - ]
     422                 :             :         // Extend to a 210000-long block chain.
     423   [ +  -  +  -  :           4 :         while (m_node.chainman->ActiveChain().Tip()->nHeight < 210000) {
                   +  + ]
     424   [ +  -  +  - ]:           1 :             CBlockIndex* prev = m_node.chainman->ActiveChain().Tip();
     425         [ +  - ]:           1 :             CBlockIndex* next = new CBlockIndex();
     426         [ +  - ]:           1 :             next->phashBlock = new uint256(m_rng.rand256());
     427   [ +  -  +  -  :           1 :             m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(next->GetBlockHash());
                   +  - ]
     428                 :           1 :             next->pprev = prev;
     429                 :           1 :             next->nHeight = prev->nHeight + 1;
     430         [ +  - ]:           1 :             next->BuildSkip();
     431   [ +  -  +  - ]:           1 :             m_node.chainman->ActiveChain().SetTip(*next);
     432                 :             :         }
     433   [ +  -  +  -  :           2 :         BOOST_REQUIRE(mining->createNewBlock(options));
             +  -  +  - ]
     434                 :             : 
     435                 :             :         // invalid p2sh txn in tx_mempool, template creation fails
     436         [ +  - ]:           1 :         tx.vin[0].prevout.hash = txFirst[0]->GetHash();
     437                 :           1 :         tx.vin[0].prevout.n = 0;
     438         [ +  - ]:           1 :         tx.vin[0].scriptSig = CScript() << OP_1;
     439         [ +  - ]:           1 :         tx.vout[0].nValue = BLOCKSUBSIDY - LOWFEE;
     440         [ +  - ]:           1 :         CScript script = CScript() << OP_0;
     441   [ +  -  +  - ]:           1 :         tx.vout[0].scriptPubKey = GetScriptForDestination(ScriptHash(script));
     442         [ +  - ]:           1 :         hash = tx.GetHash();
     443   [ +  -  +  - ]:           1 :         AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     444         [ -  + ]:           1 :         tx.vin[0].prevout.hash = hash;
     445   [ -  +  +  - ]:           1 :         tx.vin[0].scriptSig = CScript() << std::vector<unsigned char>(script.begin(), script.end());
     446         [ +  - ]:           1 :         tx.vout[0].nValue -= LOWFEE;
     447         [ +  - ]:           1 :         hash = tx.GetHash();
     448   [ +  -  +  - ]:           1 :         AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx));
     449   [ +  -  -  +  :           2 :         BOOST_CHECK_EXCEPTION(mining->createNewBlock(options), std::runtime_error, HasReason("mandatory-script-verify-flag-failed"));
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     450                 :             : 
     451                 :             :         // Delete the dummy blocks again.
     452   [ +  -  +  -  :      419782 :         while (m_node.chainman->ActiveChain().Tip()->nHeight > nHeight) {
                   +  + ]
     453   [ +  -  +  - ]:      209890 :             CBlockIndex* del = m_node.chainman->ActiveChain().Tip();
     454   [ +  -  +  -  :      209890 :             m_node.chainman->ActiveChain().SetTip(*Assert(del->pprev));
                   +  - ]
     455   [ +  -  +  -  :      209890 :             m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(del->pprev->GetBlockHash());
                   +  - ]
     456         [ +  - ]:      209890 :             delete del->phashBlock;
     457         [ +  - ]:      209890 :             delete del;
     458                 :             :         }
     459         [ +  - ]:           1 :     }
     460                 :             : 
     461         [ +  - ]:           1 :     CTxMemPool& tx_mempool{MakeMempool()};
     462         [ +  - ]:           1 :     LOCK(tx_mempool.cs);
     463                 :             : 
     464                 :             :     // non-final txs in mempool
     465   [ +  -  +  -  :           2 :     SetMockTime(m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1);
                   +  - ]
     466                 :           1 :     const int flags{LOCKTIME_VERIFY_SEQUENCE};
     467                 :             :     // height map
     468                 :           1 :     std::vector<int> prevheights;
     469                 :             : 
     470                 :             :     // relative height locked
     471                 :           1 :     tx.version = 2;
     472         [ +  - ]:           1 :     tx.vin.resize(1);
     473         [ +  - ]:           1 :     prevheights.resize(1);
     474         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[0]->GetHash(); // only 1 transaction
     475                 :           1 :     tx.vin[0].prevout.n = 0;
     476         [ +  - ]:           1 :     tx.vin[0].scriptSig = CScript() << OP_1;
     477   [ +  -  +  -  :           2 :     tx.vin[0].nSequence = m_node.chainman->ActiveChain().Tip()->nHeight + 1; // txFirst[0] is the 2nd block
                   +  - ]
     478                 :           1 :     prevheights[0] = baseheight + 1;
     479         [ +  - ]:           1 :     tx.vout.resize(1);
     480         [ +  - ]:           1 :     tx.vout[0].nValue = BLOCKSUBSIDY-HIGHFEE;
     481         [ +  - ]:           1 :     tx.vout[0].scriptPubKey = CScript() << OP_1;
     482                 :           1 :     tx.nLockTime = 0;
     483         [ +  - ]:           1 :     hash = tx.GetHash();
     484   [ +  -  +  - ]:           1 :     AddToMempool(tx_mempool, entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     485   [ +  -  +  -  :           4 :     BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     486   [ +  -  +  -  :           3 :     BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
          +  -  +  -  +  
                      - ]
     487                 :             : 
     488                 :           1 :     {
     489   [ +  -  +  - ]:           1 :         CBlockIndex* active_chain_tip = m_node.chainman->ActiveChain().Tip();
     490   [ +  -  +  -  :           3 :         BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, prevheights, *CreateBlockIndex(active_chain_tip->nHeight + 2, active_chain_tip))); // Sequence locks pass on 2nd block
          +  -  +  -  +  
                -  +  - ]
     491                 :             :     }
     492                 :             : 
     493                 :             :     // relative time locked
     494         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[1]->GetHash();
     495   [ +  -  +  -  :           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
          +  -  +  -  +  
                      - ]
     496                 :           1 :     prevheights[0] = baseheight + 2;
     497         [ +  - ]:           1 :     hash = tx.GetHash();
     498   [ +  -  +  - ]:           1 :     AddToMempool(tx_mempool, entry.Time(Now<NodeSeconds>()).FromTx(tx));
     499   [ +  -  +  -  :           4 :     BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     500   [ +  -  +  -  :           3 :     BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
             +  -  +  - ]
     501                 :             : 
     502                 :           1 :     const int SEQUENCE_LOCK_TIME = 512; // Sequence locks pass 512 seconds later
     503         [ +  + ]:          12 :     for (int i = 0; i < CBlockIndex::nMedianTimeSpan; ++i)
     504   [ +  -  +  -  :          33 :         m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i)->nTime += SEQUENCE_LOCK_TIME; // Trick the MedianTimePast
          +  -  +  -  +  
                      - ]
     505                 :           1 :     {
     506   [ +  -  +  - ]:           1 :         CBlockIndex* active_chain_tip = m_node.chainman->ActiveChain().Tip();
     507   [ +  -  +  -  :           3 :         BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, prevheights, *CreateBlockIndex(active_chain_tip->nHeight + 1, active_chain_tip)));
          +  -  +  -  +  
                      - ]
     508                 :             :     }
     509                 :             : 
     510         [ +  + ]:          12 :     for (int i = 0; i < CBlockIndex::nMedianTimeSpan; ++i) {
     511   [ +  -  +  -  :          33 :         CBlockIndex* ancestor{Assert(m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i))};
          +  -  +  -  +  
                -  +  - ]
     512                 :          11 :         ancestor->nTime -= SEQUENCE_LOCK_TIME; // undo tricked MTP
     513                 :             :     }
     514                 :             : 
     515                 :             :     // absolute height locked
     516         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[2]->GetHash();
     517                 :           1 :     tx.vin[0].nSequence = CTxIn::MAX_SEQUENCE_NONFINAL;
     518                 :           1 :     prevheights[0] = baseheight + 3;
     519   [ +  -  +  - ]:           1 :     tx.nLockTime = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
     520         [ +  - ]:           1 :     hash = tx.GetHash();
     521   [ +  -  +  - ]:           1 :     AddToMempool(tx_mempool, entry.Time(Now<NodeSeconds>()).FromTx(tx));
     522   [ +  -  +  -  :           4 :     BOOST_CHECK(!CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime fails
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     523   [ +  -  +  -  :           3 :     BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
          +  -  +  -  +  
                      - ]
     524   [ +  -  +  -  :           5 :     BOOST_CHECK(IsFinalTx(CTransaction(tx), m_node.chainman->ActiveChain().Tip()->nHeight + 2, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast())); // Locktime passes on 2nd block
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     525                 :             : 
     526                 :             :     // absolute time locked
     527         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[3]->GetHash();
     528   [ +  -  +  - ]:           2 :     tx.nLockTime = m_node.chainman->ActiveChain().Tip()->GetMedianTimePast();
     529         [ +  - ]:           1 :     prevheights.resize(1);
     530         [ +  - ]:           1 :     prevheights[0] = baseheight + 4;
     531         [ +  - ]:           1 :     hash = tx.GetHash();
     532   [ +  -  +  - ]:           1 :     AddToMempool(tx_mempool, entry.Time(Now<NodeSeconds>()).FromTx(tx));
     533   [ +  -  +  -  :           4 :     BOOST_CHECK(!CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime fails
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     534   [ +  -  +  -  :           3 :     BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
          +  -  +  -  +  
                      - ]
     535   [ +  -  +  -  :           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
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     536                 :             : 
     537                 :             :     // mempool-dependent transactions (not added)
     538         [ +  - ]:           1 :     tx.vin[0].prevout.hash = hash;
     539   [ +  -  +  -  :           2 :     prevheights[0] = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
                   +  - ]
     540                 :           1 :     tx.nLockTime = 0;
     541                 :           1 :     tx.vin[0].nSequence = 0;
     542   [ +  -  +  -  :           4 :     BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     543   [ +  -  +  -  :           3 :     BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
          +  -  +  -  +  
                      - ]
     544         [ +  - ]:           1 :     tx.vin[0].nSequence = 1;
     545   [ +  -  +  -  :           3 :     BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
          +  -  +  -  +  
                      - ]
     546         [ +  - ]:           1 :     tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG;
     547   [ +  -  +  -  :           3 :     BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
          +  -  +  -  +  
                      - ]
     548         [ +  - ]:           1 :     tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | 1;
     549   [ +  -  +  -  :           3 :     BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
          +  -  +  -  +  
                      - ]
     550                 :             : 
     551         [ +  - ]:           1 :     auto block_template = mining->createNewBlock(options);
     552   [ +  -  +  -  :           2 :     BOOST_REQUIRE(block_template);
                   +  - ]
     553                 :             : 
     554                 :             :     // None of the of the absolute height/time locked tx should have made
     555                 :             :     // it into the template because we still check IsFinalTx in CreateNewBlock,
     556                 :             :     // but relative locked txs will if inconsistently added to mempool.
     557                 :             :     // For now these will still generate a valid template until BIP68 soft fork
     558         [ +  - ]:           1 :     CBlock block{block_template->getBlock()};
     559   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(block.vtx.size(), 3U);
     560                 :             :     // However if we advance height by 1 and time by SEQUENCE_LOCK_TIME, all of them should be mined
     561         [ +  + ]:          12 :     for (int i = 0; i < CBlockIndex::nMedianTimeSpan; ++i) {
     562   [ +  -  +  -  :          33 :         CBlockIndex* ancestor{Assert(m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i))};
          +  -  +  -  +  
                -  +  - ]
     563                 :          11 :         ancestor->nTime += SEQUENCE_LOCK_TIME; // Trick the MedianTimePast
     564                 :             :     }
     565   [ +  -  +  - ]:           1 :     m_node.chainman->ActiveChain().Tip()->nHeight++;
     566   [ +  -  +  -  :           2 :     SetMockTime(m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1);
                   +  - ]
     567                 :             : 
     568         [ +  - ]:           2 :     block_template = mining->createNewBlock(options);
     569   [ +  -  +  -  :           2 :     BOOST_REQUIRE(block_template);
                   +  - ]
     570         [ +  - ]:           1 :     block = block_template->getBlock();
     571   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(block.vtx.size(), 5U);
     572         [ +  - ]:           3 : }
     573                 :             : 
     574                 :           1 : void MinerTestingSetup::TestPrioritisedMining(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst)
     575                 :             : {
     576                 :           1 :     auto mining{MakeMining()};
     577   [ +  -  +  -  :           2 :     BOOST_REQUIRE(mining);
                   +  - ]
     578                 :             : 
     579         [ +  - ]:           1 :     BlockAssembler::Options options;
     580                 :           1 :     options.coinbase_output_script = scriptPubKey;
     581                 :             : 
     582         [ +  - ]:           1 :     CTxMemPool& tx_mempool{MakeMempool()};
     583         [ +  - ]:           1 :     LOCK(tx_mempool.cs);
     584                 :             : 
     585                 :           1 :     TestMemPoolEntryHelper entry;
     586                 :             : 
     587                 :             :     // Test that a tx below min fee but prioritised is included
     588         [ +  - ]:           1 :     CMutableTransaction tx;
     589         [ +  - ]:           1 :     tx.vin.resize(1);
     590         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[0]->GetHash();
     591                 :           1 :     tx.vin[0].prevout.n = 0;
     592         [ +  - ]:           1 :     tx.vin[0].scriptSig = CScript() << OP_1;
     593         [ +  - ]:           1 :     tx.vout.resize(1);
     594         [ +  - ]:           1 :     tx.vout[0].nValue = 5000000000LL; // 0 fee
     595         [ +  - ]:           1 :     uint256 hashFreePrioritisedTx = tx.GetHash();
     596   [ +  -  +  - ]:           1 :     AddToMempool(tx_mempool, entry.Fee(0).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     597         [ +  - ]:           1 :     tx_mempool.PrioritiseTransaction(hashFreePrioritisedTx, 5 * COIN);
     598                 :             : 
     599         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[1]->GetHash();
     600                 :           1 :     tx.vin[0].prevout.n = 0;
     601                 :           1 :     tx.vout[0].nValue = 5000000000LL - 1000;
     602                 :             :     // This tx has a low fee: 1000 satoshis
     603         [ +  - ]:           1 :     Txid hashParentTx = tx.GetHash(); // save this txid for later use
     604   [ +  -  +  - ]:           1 :     AddToMempool(tx_mempool, entry.Fee(1000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     605                 :             : 
     606                 :             :     // This tx has a medium fee: 10000 satoshis
     607         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[2]->GetHash();
     608                 :           1 :     tx.vout[0].nValue = 5000000000LL - 10000;
     609         [ +  - ]:           1 :     Txid hashMediumFeeTx = tx.GetHash();
     610   [ +  -  +  - ]:           1 :     AddToMempool(tx_mempool, entry.Fee(10000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
     611         [ +  - ]:           1 :     tx_mempool.PrioritiseTransaction(hashMediumFeeTx, -5 * COIN);
     612                 :             : 
     613                 :             :     // This tx also has a low fee, but is prioritised
     614         [ +  - ]:           1 :     tx.vin[0].prevout.hash = hashParentTx;
     615                 :           1 :     tx.vout[0].nValue = 5000000000LL - 1000 - 1000; // 1000 satoshi fee
     616         [ +  - ]:           1 :     Txid hashPrioritsedChild = tx.GetHash();
     617   [ +  -  +  - ]:           1 :     AddToMempool(tx_mempool, entry.Fee(1000).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx));
     618         [ +  - ]:           1 :     tx_mempool.PrioritiseTransaction(hashPrioritsedChild, 2 * COIN);
     619                 :             : 
     620                 :             :     // Test that transaction selection properly updates ancestor fee calculations as prioritised
     621                 :             :     // parents get included in a block. Create a transaction with two prioritised ancestors, each
     622                 :             :     // included by itself: FreeParent <- FreeChild <- FreeGrandchild.
     623                 :             :     // When FreeParent is added, a modified entry will be created for FreeChild + FreeGrandchild
     624                 :             :     // FreeParent's prioritisation should not be included in that entry.
     625                 :             :     // When FreeChild is included, FreeChild's prioritisation should also not be included.
     626         [ +  - ]:           1 :     tx.vin[0].prevout.hash = txFirst[3]->GetHash();
     627                 :           1 :     tx.vout[0].nValue = 5000000000LL; // 0 fee
     628         [ +  - ]:           1 :     Txid hashFreeParent = tx.GetHash();
     629   [ +  -  +  - ]:           1 :     AddToMempool(tx_mempool, entry.Fee(0).SpendsCoinbase(true).FromTx(tx));
     630         [ +  - ]:           1 :     tx_mempool.PrioritiseTransaction(hashFreeParent, 10 * COIN);
     631                 :             : 
     632         [ +  - ]:           1 :     tx.vin[0].prevout.hash = hashFreeParent;
     633                 :           1 :     tx.vout[0].nValue = 5000000000LL; // 0 fee
     634         [ +  - ]:           1 :     Txid hashFreeChild = tx.GetHash();
     635   [ +  -  +  - ]:           1 :     AddToMempool(tx_mempool, entry.Fee(0).SpendsCoinbase(false).FromTx(tx));
     636         [ +  - ]:           1 :     tx_mempool.PrioritiseTransaction(hashFreeChild, 1 * COIN);
     637                 :             : 
     638         [ +  - ]:           1 :     tx.vin[0].prevout.hash = hashFreeChild;
     639                 :           1 :     tx.vout[0].nValue = 5000000000LL; // 0 fee
     640         [ +  - ]:           1 :     Txid hashFreeGrandchild = tx.GetHash();
     641   [ +  -  +  - ]:           1 :     AddToMempool(tx_mempool, entry.Fee(0).SpendsCoinbase(false).FromTx(tx));
     642                 :             : 
     643         [ +  - ]:           1 :     auto block_template = mining->createNewBlock(options);
     644   [ +  -  +  -  :           2 :     BOOST_REQUIRE(block_template);
                   +  - ]
     645         [ +  - ]:           1 :     CBlock block{block_template->getBlock()};
     646   [ +  -  +  - ]:           1 :     BOOST_REQUIRE_EQUAL(block.vtx.size(), 6U);
     647   [ +  -  +  -  :           2 :     BOOST_CHECK(block.vtx[1]->GetHash() == hashFreeParent);
                   +  - ]
     648   [ +  -  +  -  :           2 :     BOOST_CHECK(block.vtx[2]->GetHash() == hashFreePrioritisedTx);
                   +  - ]
     649   [ +  -  +  -  :           2 :     BOOST_CHECK(block.vtx[3]->GetHash() == hashParentTx);
                   +  - ]
     650   [ +  -  +  -  :           2 :     BOOST_CHECK(block.vtx[4]->GetHash() == hashPrioritsedChild);
                   +  - ]
     651   [ +  -  +  - ]:           2 :     BOOST_CHECK(block.vtx[5]->GetHash() == hashFreeChild);
     652         [ +  + ]:           7 :     for (size_t i=0; i<block.vtx.size(); ++i) {
     653                 :             :         // The FreeParent and FreeChild's prioritisations should not impact the child.
     654   [ +  -  +  -  :          12 :         BOOST_CHECK(block.vtx[i]->GetHash() != hashFreeGrandchild);
                   +  - ]
     655                 :             :         // De-prioritised transaction should not be included.
     656   [ +  -  +  - ]:          12 :         BOOST_CHECK(block.vtx[i]->GetHash() != hashMediumFeeTx);
     657                 :             :     }
     658         [ +  - ]:           3 : }
     659                 :             : 
     660                 :             : // NOTE: These tests rely on CreateNewBlock doing its own self-validation!
     661   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     662                 :             : {
     663                 :           1 :     auto mining{MakeMining()};
     664   [ +  -  +  - ]:           2 :     BOOST_REQUIRE(mining);
     665                 :             : 
     666                 :             :     // Note that by default, these tests run with size accounting enabled.
     667         [ +  - ]:           1 :     CScript scriptPubKey = CScript() << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"_hex << OP_CHECKSIG;
     668         [ +  - ]:           1 :     BlockAssembler::Options options;
     669                 :           1 :     options.coinbase_output_script = scriptPubKey;
     670                 :             : 
     671                 :             :     // Create and check a simple template
     672         [ +  - ]:           1 :     std::unique_ptr<BlockTemplate> block_template = mining->createNewBlock(options);
     673   [ +  -  +  -  :           2 :     BOOST_REQUIRE(block_template);
                   +  - ]
     674                 :           1 :     {
     675         [ +  - ]:           1 :         CBlock block{block_template->getBlock()};
     676                 :           1 :         {
     677         [ +  - ]:           1 :             std::string reason;
     678                 :           1 :             std::string debug;
     679   [ +  -  +  -  :           2 :             BOOST_REQUIRE(!mining->checkBlock(block, {.check_pow = false}, reason, debug));
             +  -  +  - ]
     680   [ +  -  +  - ]:           1 :             BOOST_REQUIRE_EQUAL(reason, "bad-txnmrklroot");
     681   [ +  -  +  - ]:           1 :             BOOST_REQUIRE_EQUAL(debug, "hashMerkleRoot mismatch");
     682                 :           1 :         }
     683                 :             : 
     684         [ +  - ]:           1 :         block.hashMerkleRoot = BlockMerkleRoot(block);
     685                 :             : 
     686                 :           1 :         {
     687         [ +  - ]:           1 :             std::string reason;
     688                 :           1 :             std::string debug;
     689   [ +  -  +  -  :           2 :             BOOST_REQUIRE(mining->checkBlock(block, {.check_pow = false}, reason, debug));
             +  -  +  - ]
     690   [ +  -  +  - ]:           1 :             BOOST_REQUIRE_EQUAL(reason, "");
     691   [ +  -  +  - ]:           1 :             BOOST_REQUIRE_EQUAL(debug, "");
     692                 :           1 :         }
     693                 :             : 
     694                 :           1 :         {
     695                 :             :             // A block template does not have proof-of-work, but it might pass
     696                 :             :             // verification by coincidence. Grind the nonce if needed:
     697   [ +  -  +  -  :           1 :             while (CheckProofOfWork(block.GetHash(), block.nBits, Assert(m_node.chainman)->GetParams().GetConsensus())) {
             +  -  -  + ]
     698                 :           0 :                 block.nNonce++;
     699                 :             :             }
     700                 :             : 
     701         [ +  - ]:           1 :             std::string reason;
     702                 :           1 :             std::string debug;
     703   [ +  -  +  -  :           2 :             BOOST_REQUIRE(!mining->checkBlock(block, {.check_pow = true}, reason, debug));
             +  -  +  - ]
     704   [ +  -  +  - ]:           1 :             BOOST_REQUIRE_EQUAL(reason, "high-hash");
     705   [ +  -  +  - ]:           1 :             BOOST_REQUIRE_EQUAL(debug, "proof of work failed");
     706                 :           1 :         }
     707                 :           0 :     }
     708                 :             : 
     709                 :             :     // We can't make transactions until we have inputs
     710                 :             :     // Therefore, load 110 blocks :)
     711                 :           1 :     static_assert(std::size(BLOCKINFO) == 110, "Should have 110 blocks to import");
     712                 :           1 :     int baseheight = 0;
     713                 :           1 :     std::vector<CTransactionRef> txFirst;
     714         [ +  + ]:         111 :     for (const auto& bi : BLOCKINFO) {
     715         [ +  - ]:         110 :         const int current_height{mining->getTip()->height};
     716                 :             : 
     717                 :             :         /**
     718                 :             :          * Simple block creation, nothing special yet.
     719                 :             :          * If current_height is odd, block_template will have already been
     720                 :             :          * set at the end of the previous loop.
     721                 :             :          */
     722         [ +  + ]:         110 :         if (current_height % 2 == 0) {
     723         [ +  - ]:         110 :             block_template = mining->createNewBlock(options);
     724   [ +  -  +  - ]:         110 :             BOOST_REQUIRE(block_template);
     725                 :             :         }
     726                 :             : 
     727         [ +  - ]:         110 :         CBlock block{block_template->getBlock()};
     728         [ +  - ]:         110 :         CMutableTransaction txCoinbase(*block.vtx[0]);
     729                 :         110 :         {
     730         [ +  - ]:         110 :             LOCK(cs_main);
     731                 :         110 :             block.nVersion = VERSIONBITS_TOP_BITS;
     732   [ +  -  +  -  :         220 :             block.nTime = Assert(m_node.chainman)->ActiveChain().Tip()->GetMedianTimePast()+1;
                   +  - ]
     733                 :         110 :             txCoinbase.version = 1;
     734   [ +  -  +  - ]:         110 :             txCoinbase.vin[0].scriptSig = CScript{} << (current_height + 1) << bi.extranonce;
     735         [ +  - ]:         110 :             txCoinbase.vout.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this)
     736                 :         110 :             txCoinbase.vout[0].scriptPubKey = CScript();
     737   [ +  -  -  + ]:         220 :             block.vtx[0] = MakeTransactionRef(txCoinbase);
     738         [ +  + ]:         110 :             if (txFirst.size() == 0)
     739                 :           1 :                 baseheight = current_height;
     740         [ +  + ]:         110 :             if (txFirst.size() < 4)
     741         [ +  - ]:           4 :                 txFirst.push_back(block.vtx[0]);
     742         [ +  - ]:         110 :             block.hashMerkleRoot = BlockMerkleRoot(block);
     743         [ +  - ]:         110 :             block.nNonce = bi.nonce;
     744                 :           0 :         }
     745         [ +  - ]:         110 :         std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
     746                 :             :         // Alternate calls between Chainman's ProcessNewBlock and submitSolution
     747                 :             :         // via the Mining interface. The former is used by net_processing as well
     748                 :             :         // as the submitblock RPC.
     749         [ +  + ]:         110 :         if (current_height % 2 == 0) {
     750   [ +  -  +  -  :         110 :             BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(shared_pblock, /*force_processing=*/true, /*min_pow_checked=*/true, nullptr));
             +  -  +  - ]
     751                 :             :         } else {
     752   [ +  -  +  -  :         165 :             BOOST_REQUIRE(block_template->submitSolution(block.nVersion, block.nTime, block.nNonce, MakeTransactionRef(txCoinbase)));
          +  -  +  -  -  
                      + ]
     753                 :             :         }
     754                 :         110 :         {
     755         [ +  - ]:         110 :             LOCK(cs_main);
     756                 :             :             // The above calls don't guarantee the tip is actually updated, so
     757                 :             :             // we explicitly check this.
     758   [ +  -  +  -  :         110 :             auto maybe_new_tip{Assert(m_node.chainman)->ActiveChain().Tip()};
                   +  - ]
     759   [ +  -  +  -  :         110 :             BOOST_REQUIRE_EQUAL(maybe_new_tip->GetBlockHash(), block.GetHash());
             +  -  +  - ]
     760                 :           0 :         }
     761         [ +  + ]:         110 :         if (current_height % 2 == 0) {
     762         [ +  - ]:         110 :             block_template = block_template->waitNext();
     763   [ +  -  +  - ]:         110 :             BOOST_REQUIRE(block_template);
     764                 :             :         } else {
     765                 :             :             // This just adds coverage
     766         [ +  - ]:          55 :             mining->waitTipChanged(block.hashPrevBlock);
     767                 :             :         }
     768                 :         220 :     }
     769                 :             : 
     770         [ +  - ]:           1 :     LOCK(cs_main);
     771                 :             : 
     772         [ +  - ]:           1 :     TestBasicMining(scriptPubKey, txFirst, baseheight);
     773                 :             : 
     774   [ +  -  +  - ]:           1 :     m_node.chainman->ActiveChain().Tip()->nHeight--;
     775         [ +  - ]:           1 :     SetMockTime(0);
     776                 :             : 
     777         [ +  - ]:           1 :     TestPackageSelection(scriptPubKey, txFirst);
     778                 :             : 
     779   [ +  -  +  - ]:           1 :     m_node.chainman->ActiveChain().Tip()->nHeight--;
     780         [ +  - ]:           1 :     SetMockTime(0);
     781                 :             : 
     782         [ +  - ]:           1 :     TestPrioritisedMining(scriptPubKey, txFirst);
     783                 :           1 : }
     784                 :             : 
     785                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1