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.3 % 600 590
Test Date: 2026-11-27 06:02:19 Functions: 100.0 % 14 14
Branches: 50.3 % 2016 1014

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

Generated by: LCOV version 2.0-1