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.6 % 584 576
Test Date: 2026-07-17 06:52:43 Functions: 100.0 % 9 9
Branches: 50.6 % 1788 905

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

Generated by: LCOV version 2.0-1