LCOV - code coverage report
Current view: top level - src/test - miner_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 98.5 % 619 610
Test Date: 2026-09-29 06:14:05 Functions: 100.0 % 13 13
Branches: 50.4 % 2014 1016

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

Generated by: LCOV version 2.0-1