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.5 % 607 598
Test Date: 2026-08-25 06:16:57 Functions: 100.0 % 11 11
Branches: 50.5 % 1910 964

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

Generated by: LCOV version 2.0-1