LCOV - code coverage report
Current view: top level - src/test - miner_tests.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 343 343 100.0 %
Date: 2022-04-21 14:51:19 Functions: 5 5 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 30 30 100.0 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2011-2021 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 <chainparams.h>
#       6                 :            : #include <coins.h>
#       7                 :            : #include <consensus/consensus.h>
#       8                 :            : #include <consensus/merkle.h>
#       9                 :            : #include <consensus/tx_verify.h>
#      10                 :            : #include <node/miner.h>
#      11                 :            : #include <policy/policy.h>
#      12                 :            : #include <script/standard.h>
#      13                 :            : #include <txmempool.h>
#      14                 :            : #include <uint256.h>
#      15                 :            : #include <util/strencodings.h>
#      16                 :            : #include <util/system.h>
#      17                 :            : #include <util/time.h>
#      18                 :            : #include <validation.h>
#      19                 :            : #include <versionbits.h>
#      20                 :            : 
#      21                 :            : #include <test/util/setup_common.h>
#      22                 :            : 
#      23                 :            : #include <memory>
#      24                 :            : 
#      25                 :            : #include <boost/test/unit_test.hpp>
#      26                 :            : 
#      27                 :            : using node::BlockAssembler;
#      28                 :            : using node::CBlockTemplate;
#      29                 :            : 
#      30                 :            : namespace miner_tests {
#      31                 :            : struct MinerTestingSetup : public TestingSetup {
#      32                 :            :     void TestPackageSelection(const CChainParams& chainparams, const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_node.mempool->cs);
#      33                 :            :     bool TestSequenceLocks(const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_node.mempool->cs)
#      34                 :         16 :     {
#      35                 :         16 :         CCoinsViewMemPool view_mempool(&m_node.chainman->ActiveChainstate().CoinsTip(), *m_node.mempool);
#      36                 :         16 :         return CheckSequenceLocksAtTip(m_node.chainman->ActiveChain().Tip(), view_mempool, tx);
#      37                 :         16 :     }
#      38                 :            :     BlockAssembler AssemblerForTest(const CChainParams& params);
#      39                 :            : };
#      40                 :            : } // namespace miner_tests
#      41                 :            : 
#      42                 :            : BOOST_FIXTURE_TEST_SUITE(miner_tests, MinerTestingSetup)
#      43                 :            : 
#      44                 :            : static CFeeRate blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
#      45                 :            : 
#      46                 :            : BlockAssembler MinerTestingSetup::AssemblerForTest(const CChainParams& params)
#      47                 :         38 : {
#      48                 :         38 :     BlockAssembler::Options options;
#      49                 :            : 
#      50                 :         38 :     options.nBlockMaxWeight = MAX_BLOCK_WEIGHT;
#      51                 :         38 :     options.blockMinFeeRate = blockMinFeeRate;
#      52                 :         38 :     return BlockAssembler(m_node.chainman->ActiveChainstate(), *m_node.mempool, params, options);
#      53                 :         38 : }
#      54                 :            : 
#      55                 :            : constexpr static struct {
#      56                 :            :     unsigned char extranonce;
#      57                 :            :     unsigned int nonce;
#      58                 :            : } BLOCKINFO[]{{8, 582909131},  {0, 971462344},  {2, 1169481553}, {6, 66147495},  {7, 427785981},  {8, 80538907},
#      59                 :            :               {8, 207348013},  {2, 1951240923}, {4, 215054351},  {1, 491520534}, {8, 1282281282}, {4, 639565734},
#      60                 :            :               {3, 248274685},  {8, 1160085976}, {6, 396349768},  {5, 393780549}, {5, 1096899528}, {4, 965381630},
#      61                 :            :               {0, 728758712},  {5, 318638310},  {3, 164591898},  {2, 274234550}, {2, 254411237},  {7, 561761812},
#      62                 :            :               {2, 268342573},  {0, 402816691},  {1, 221006382},  {6, 538872455}, {7, 393315655},  {4, 814555937},
#      63                 :            :               {7, 504879194},  {6, 467769648},  {3, 925972193},  {2, 200581872}, {3, 168915404},  {8, 430446262},
#      64                 :            :               {5, 773507406},  {3, 1195366164}, {0, 433361157},  {3, 297051771}, {0, 558856551},  {2, 501614039},
#      65                 :            :               {3, 528488272},  {2, 473587734},  {8, 230125274},  {2, 494084400}, {4, 357314010},  {8, 60361686},
#      66                 :            :               {7, 640624687},  {3, 480441695},  {8, 1424447925}, {4, 752745419}, {1, 288532283},  {6, 669170574},
#      67                 :            :               {5, 1900907591}, {3, 555326037},  {3, 1121014051}, {0, 545835650}, {8, 189196651},  {5, 252371575},
#      68                 :            :               {0, 199163095},  {6, 558895874},  {6, 1656839784}, {6, 815175452}, {6, 718677851},  {5, 544000334},
#      69                 :            :               {0, 340113484},  {6, 850744437},  {4, 496721063},  {8, 524715182}, {6, 574361898},  {6, 1642305743},
#      70                 :            :               {6, 355110149},  {5, 1647379658}, {8, 1103005356}, {7, 556460625}, {3, 1139533992}, {5, 304736030},
#      71                 :            :               {2, 361539446},  {2, 143720360},  {6, 201939025},  {7, 423141476}, {4, 574633709},  {3, 1412254823},
#      72                 :            :               {4, 873254135},  {0, 341817335},  {6, 53501687},   {3, 179755410}, {5, 172209688},  {8, 516810279},
#      73                 :            :               {4, 1228391489}, {8, 325372589},  {6, 550367589},  {0, 876291812}, {7, 412454120},  {7, 717202854},
#      74                 :            :               {2, 222677843},  {6, 251778867},  {7, 842004420},  {7, 194762829}, {4, 96668841},   {1, 925485796},
#      75                 :            :               {0, 792342903},  {6, 678455063},  {6, 773251385},  {5, 186617471}, {6, 883189502},  {7, 396077336},
#      76                 :            :               {8, 254702874},  {0, 455592851}};
#      77                 :            : 
#      78                 :            : static CBlockIndex CreateBlockIndex(int nHeight, CBlockIndex* active_chain_tip) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
#      79                 :          4 : {
#      80                 :          4 :     CBlockIndex index;
#      81                 :          4 :     index.nHeight = nHeight;
#      82                 :          4 :     index.pprev = active_chain_tip;
#      83                 :          4 :     return index;
#      84                 :          4 : }
#      85                 :            : 
#      86                 :            : // Test suite for ancestor feerate transaction selection.
#      87                 :            : // Implemented as an additional function, rather than a separate test case,
#      88                 :            : // to allow reusing the blockchain created in CreateNewBlock_validity.
#      89                 :            : void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst)
#      90                 :          2 : {
#      91                 :            :     // Test the ancestor feerate transaction selection.
#      92                 :          2 :     TestMemPoolEntryHelper entry;
#      93                 :            : 
#      94                 :            :     // Test that a medium fee transaction will be selected after a higher fee
#      95                 :            :     // rate package with a low fee rate parent.
#      96                 :          2 :     CMutableTransaction tx;
#      97                 :          2 :     tx.vin.resize(1);
#      98                 :          2 :     tx.vin[0].scriptSig = CScript() << OP_1;
#      99                 :          2 :     tx.vin[0].prevout.hash = txFirst[0]->GetHash();
#     100                 :          2 :     tx.vin[0].prevout.n = 0;
#     101                 :          2 :     tx.vout.resize(1);
#     102                 :          2 :     tx.vout[0].nValue = 5000000000LL - 1000;
#     103                 :            :     // This tx has a low fee: 1000 satoshis
#     104                 :          2 :     uint256 hashParentTx = tx.GetHash(); // save this txid for later use
#     105                 :          2 :     m_node.mempool->addUnchecked(entry.Fee(1000).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
#     106                 :            : 
#     107                 :            :     // This tx has a medium fee: 10000 satoshis
#     108                 :          2 :     tx.vin[0].prevout.hash = txFirst[1]->GetHash();
#     109                 :          2 :     tx.vout[0].nValue = 5000000000LL - 10000;
#     110                 :          2 :     uint256 hashMediumFeeTx = tx.GetHash();
#     111                 :          2 :     m_node.mempool->addUnchecked(entry.Fee(10000).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
#     112                 :            : 
#     113                 :            :     // This tx has a high fee, but depends on the first transaction
#     114                 :          2 :     tx.vin[0].prevout.hash = hashParentTx;
#     115                 :          2 :     tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 50k satoshi fee
#     116                 :          2 :     uint256 hashHighFeeTx = tx.GetHash();
#     117                 :          2 :     m_node.mempool->addUnchecked(entry.Fee(50000).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
#     118                 :            : 
#     119                 :          2 :     std::unique_ptr<CBlockTemplate> pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
#     120                 :          2 :     BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 4U);
#     121                 :          2 :     BOOST_CHECK(pblocktemplate->block.vtx[1]->GetHash() == hashParentTx);
#     122                 :          2 :     BOOST_CHECK(pblocktemplate->block.vtx[2]->GetHash() == hashHighFeeTx);
#     123                 :          2 :     BOOST_CHECK(pblocktemplate->block.vtx[3]->GetHash() == hashMediumFeeTx);
#     124                 :            : 
#     125                 :            :     // Test that a package below the block min tx fee doesn't get included
#     126                 :          2 :     tx.vin[0].prevout.hash = hashHighFeeTx;
#     127                 :          2 :     tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 0 fee
#     128                 :          2 :     uint256 hashFreeTx = tx.GetHash();
#     129                 :          2 :     m_node.mempool->addUnchecked(entry.Fee(0).FromTx(tx));
#     130                 :          2 :     size_t freeTxSize = ::GetSerializeSize(tx, PROTOCOL_VERSION);
#     131                 :            : 
#     132                 :            :     // Calculate a fee on child transaction that will put the package just
#     133                 :            :     // below the block min tx fee (assuming 1 child tx of the same size).
#     134                 :          2 :     CAmount feeToUse = blockMinFeeRate.GetFee(2*freeTxSize) - 1;
#     135                 :            : 
#     136                 :          2 :     tx.vin[0].prevout.hash = hashFreeTx;
#     137                 :          2 :     tx.vout[0].nValue = 5000000000LL - 1000 - 50000 - feeToUse;
#     138                 :          2 :     uint256 hashLowFeeTx = tx.GetHash();
#     139                 :          2 :     m_node.mempool->addUnchecked(entry.Fee(feeToUse).FromTx(tx));
#     140                 :          2 :     pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
#     141                 :            :     // Verify that the free tx and the low fee tx didn't get selected
#     142         [ +  + ]:         10 :     for (size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
#     143                 :          8 :         BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashFreeTx);
#     144                 :          8 :         BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashLowFeeTx);
#     145                 :          8 :     }
#     146                 :            : 
#     147                 :            :     // Test that packages above the min relay fee do get included, even if one
#     148                 :            :     // of the transactions is below the min relay fee
#     149                 :            :     // Remove the low fee transaction and replace with a higher fee transaction
#     150                 :          2 :     m_node.mempool->removeRecursive(CTransaction(tx), MemPoolRemovalReason::REPLACED);
#     151                 :          2 :     tx.vout[0].nValue -= 2; // Now we should be just over the min relay fee
#     152                 :          2 :     hashLowFeeTx = tx.GetHash();
#     153                 :          2 :     m_node.mempool->addUnchecked(entry.Fee(feeToUse+2).FromTx(tx));
#     154                 :          2 :     pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
#     155                 :          2 :     BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 6U);
#     156                 :          2 :     BOOST_CHECK(pblocktemplate->block.vtx[4]->GetHash() == hashFreeTx);
#     157                 :          2 :     BOOST_CHECK(pblocktemplate->block.vtx[5]->GetHash() == hashLowFeeTx);
#     158                 :            : 
#     159                 :            :     // Test that transaction selection properly updates ancestor fee
#     160                 :            :     // calculations as ancestor transactions get included in a block.
#     161                 :            :     // Add a 0-fee transaction that has 2 outputs.
#     162                 :          2 :     tx.vin[0].prevout.hash = txFirst[2]->GetHash();
#     163                 :          2 :     tx.vout.resize(2);
#     164                 :          2 :     tx.vout[0].nValue = 5000000000LL - 100000000;
#     165                 :          2 :     tx.vout[1].nValue = 100000000; // 1BTC output
#     166                 :          2 :     uint256 hashFreeTx2 = tx.GetHash();
#     167                 :          2 :     m_node.mempool->addUnchecked(entry.Fee(0).SpendsCoinbase(true).FromTx(tx));
#     168                 :            : 
#     169                 :            :     // This tx can't be mined by itself
#     170                 :          2 :     tx.vin[0].prevout.hash = hashFreeTx2;
#     171                 :          2 :     tx.vout.resize(1);
#     172                 :          2 :     feeToUse = blockMinFeeRate.GetFee(freeTxSize);
#     173                 :          2 :     tx.vout[0].nValue = 5000000000LL - 100000000 - feeToUse;
#     174                 :          2 :     uint256 hashLowFeeTx2 = tx.GetHash();
#     175                 :          2 :     m_node.mempool->addUnchecked(entry.Fee(feeToUse).SpendsCoinbase(false).FromTx(tx));
#     176                 :          2 :     pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
#     177                 :            : 
#     178                 :            :     // Verify that this tx isn't selected.
#     179         [ +  + ]:         14 :     for (size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
#     180                 :         12 :         BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashFreeTx2);
#     181                 :         12 :         BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashLowFeeTx2);
#     182                 :         12 :     }
#     183                 :            : 
#     184                 :            :     // This tx will be mineable, and should cause hashLowFeeTx2 to be selected
#     185                 :            :     // as well.
#     186                 :          2 :     tx.vin[0].prevout.n = 1;
#     187                 :          2 :     tx.vout[0].nValue = 100000000 - 10000; // 10k satoshi fee
#     188                 :          2 :     m_node.mempool->addUnchecked(entry.Fee(10000).FromTx(tx));
#     189                 :          2 :     pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
#     190                 :          2 :     BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 9U);
#     191                 :          2 :     BOOST_CHECK(pblocktemplate->block.vtx[8]->GetHash() == hashLowFeeTx2);
#     192                 :          2 : }
#     193                 :            : 
#     194                 :            : // NOTE: These tests rely on CreateNewBlock doing its own self-validation!
#     195                 :            : BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
#     196                 :          2 : {
#     197                 :            :     // Note that by default, these tests run with size accounting enabled.
#     198                 :          2 :     const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
#     199                 :          2 :     const CChainParams& chainparams = *chainParams;
#     200                 :          2 :     CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
#     201                 :          2 :     std::unique_ptr<CBlockTemplate> pblocktemplate;
#     202                 :          2 :     CMutableTransaction tx;
#     203                 :          2 :     CScript script;
#     204                 :          2 :     uint256 hash;
#     205                 :          2 :     TestMemPoolEntryHelper entry;
#     206                 :          2 :     entry.nFee = 11;
#     207                 :          2 :     entry.nHeight = 11;
#     208                 :            : 
#     209                 :          2 :     fCheckpointsEnabled = false;
#     210                 :            : 
#     211                 :            :     // Simple block creation, nothing special yet:
#     212                 :          2 :     BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
#     213                 :            : 
#     214                 :            :     // We can't make transactions until we have inputs
#     215                 :            :     // Therefore, load 110 blocks :)
#     216                 :          2 :     static_assert(std::size(BLOCKINFO) == 110, "Should have 110 blocks to import");
#     217                 :          2 :     int baseheight = 0;
#     218                 :          2 :     std::vector<CTransactionRef> txFirst;
#     219         [ +  + ]:        220 :     for (const auto& bi : BLOCKINFO) {
#     220                 :        220 :         CBlock *pblock = &pblocktemplate->block; // pointer for convenience
#     221                 :        220 :         {
#     222                 :        220 :             LOCK(cs_main);
#     223                 :        220 :             pblock->nVersion = VERSIONBITS_TOP_BITS;
#     224                 :        220 :             pblock->nTime = m_node.chainman->ActiveChain().Tip()->GetMedianTimePast()+1;
#     225                 :        220 :             CMutableTransaction txCoinbase(*pblock->vtx[0]);
#     226                 :        220 :             txCoinbase.nVersion = 1;
#     227                 :        220 :             txCoinbase.vin[0].scriptSig = CScript{} << (m_node.chainman->ActiveChain().Height() + 1) << bi.extranonce;
#     228                 :        220 :             txCoinbase.vout.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this)
#     229                 :        220 :             txCoinbase.vout[0].scriptPubKey = CScript();
#     230                 :        220 :             pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
#     231         [ +  + ]:        220 :             if (txFirst.size() == 0)
#     232                 :          2 :                 baseheight = m_node.chainman->ActiveChain().Height();
#     233         [ +  + ]:        220 :             if (txFirst.size() < 4)
#     234                 :          8 :                 txFirst.push_back(pblock->vtx[0]);
#     235                 :        220 :             pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
#     236                 :        220 :             pblock->nNonce = bi.nonce;
#     237                 :        220 :         }
#     238                 :        220 :         std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(*pblock);
#     239                 :        220 :         BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(chainparams, shared_pblock, true, nullptr));
#     240                 :        220 :         pblock->hashPrevBlock = pblock->GetHash();
#     241                 :        220 :     }
#     242                 :            : 
#     243                 :          2 :     LOCK(cs_main);
#     244                 :          2 :     LOCK(m_node.mempool->cs);
#     245                 :            : 
#     246                 :            :     // Just to make sure we can still make simple blocks
#     247                 :          2 :     BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
#     248                 :            : 
#     249                 :          2 :     const CAmount BLOCKSUBSIDY = 50*COIN;
#     250                 :          2 :     const CAmount LOWFEE = CENT;
#     251                 :          2 :     const CAmount HIGHFEE = COIN;
#     252                 :          2 :     const CAmount HIGHERFEE = 4*COIN;
#     253                 :            : 
#     254                 :            :     // block sigops > limit: 1000 CHECKMULTISIG + 1
#     255                 :          2 :     tx.vin.resize(1);
#     256                 :            :     // NOTE: OP_NOP is used to force 20 SigOps for the CHECKMULTISIG
#     257                 :          2 :     tx.vin[0].scriptSig = CScript() << OP_0 << OP_0 << OP_0 << OP_NOP << OP_CHECKMULTISIG << OP_1;
#     258                 :          2 :     tx.vin[0].prevout.hash = txFirst[0]->GetHash();
#     259                 :          2 :     tx.vin[0].prevout.n = 0;
#     260                 :          2 :     tx.vout.resize(1);
#     261                 :          2 :     tx.vout[0].nValue = BLOCKSUBSIDY;
#     262         [ +  + ]:       2004 :     for (unsigned int i = 0; i < 1001; ++i)
#     263                 :       2002 :     {
#     264                 :       2002 :         tx.vout[0].nValue -= LOWFEE;
#     265                 :       2002 :         hash = tx.GetHash();
#     266                 :       2002 :         bool spendsCoinbase = i == 0; // only first tx spends coinbase
#     267                 :            :         // If we don't set the # of sig ops in the CTxMemPoolEntry, template creation fails
#     268                 :       2002 :         m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(spendsCoinbase).FromTx(tx));
#     269                 :       2002 :         tx.vin[0].prevout.hash = hash;
#     270                 :       2002 :     }
#     271                 :            : 
#     272                 :          2 :     BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-blk-sigops"));
#     273                 :          2 :     m_node.mempool->clear();
#     274                 :            : 
#     275                 :          2 :     tx.vin[0].prevout.hash = txFirst[0]->GetHash();
#     276                 :          2 :     tx.vout[0].nValue = BLOCKSUBSIDY;
#     277         [ +  + ]:       2004 :     for (unsigned int i = 0; i < 1001; ++i)
#     278                 :       2002 :     {
#     279                 :       2002 :         tx.vout[0].nValue -= LOWFEE;
#     280                 :       2002 :         hash = tx.GetHash();
#     281                 :       2002 :         bool spendsCoinbase = i == 0; // only first tx spends coinbase
#     282                 :            :         // If we do set the # of sig ops in the CTxMemPoolEntry, template creation passes
#     283                 :       2002 :         m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(spendsCoinbase).SigOpsCost(80).FromTx(tx));
#     284                 :       2002 :         tx.vin[0].prevout.hash = hash;
#     285                 :       2002 :     }
#     286                 :          2 :     BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
#     287                 :          2 :     m_node.mempool->clear();
#     288                 :            : 
#     289                 :            :     // block size > limit
#     290                 :          2 :     tx.vin[0].scriptSig = CScript();
#     291                 :            :     // 18 * (520char + DROP) + OP_1 = 9433 bytes
#     292                 :          2 :     std::vector<unsigned char> vchData(520);
#     293         [ +  + ]:         38 :     for (unsigned int i = 0; i < 18; ++i)
#     294                 :         36 :         tx.vin[0].scriptSig << vchData << OP_DROP;
#     295                 :          2 :     tx.vin[0].scriptSig << OP_1;
#     296                 :          2 :     tx.vin[0].prevout.hash = txFirst[0]->GetHash();
#     297                 :          2 :     tx.vout[0].nValue = BLOCKSUBSIDY;
#     298         [ +  + ]:        258 :     for (unsigned int i = 0; i < 128; ++i)
#     299                 :        256 :     {
#     300                 :        256 :         tx.vout[0].nValue -= LOWFEE;
#     301                 :        256 :         hash = tx.GetHash();
#     302                 :        256 :         bool spendsCoinbase = i == 0; // only first tx spends coinbase
#     303                 :        256 :         m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(spendsCoinbase).FromTx(tx));
#     304                 :        256 :         tx.vin[0].prevout.hash = hash;
#     305                 :        256 :     }
#     306                 :          2 :     BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
#     307                 :          2 :     m_node.mempool->clear();
#     308                 :            : 
#     309                 :            :     // orphan in *m_node.mempool, template creation fails
#     310                 :          2 :     hash = tx.GetHash();
#     311                 :          2 :     m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).FromTx(tx));
#     312                 :          2 :     BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
#     313                 :          2 :     m_node.mempool->clear();
#     314                 :            : 
#     315                 :            :     // child with higher feerate than parent
#     316                 :          2 :     tx.vin[0].scriptSig = CScript() << OP_1;
#     317                 :          2 :     tx.vin[0].prevout.hash = txFirst[1]->GetHash();
#     318                 :          2 :     tx.vout[0].nValue = BLOCKSUBSIDY-HIGHFEE;
#     319                 :          2 :     hash = tx.GetHash();
#     320                 :          2 :     m_node.mempool->addUnchecked(entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
#     321                 :          2 :     tx.vin[0].prevout.hash = hash;
#     322                 :          2 :     tx.vin.resize(2);
#     323                 :          2 :     tx.vin[1].scriptSig = CScript() << OP_1;
#     324                 :          2 :     tx.vin[1].prevout.hash = txFirst[0]->GetHash();
#     325                 :          2 :     tx.vin[1].prevout.n = 0;
#     326                 :          2 :     tx.vout[0].nValue = tx.vout[0].nValue+BLOCKSUBSIDY-HIGHERFEE; //First txn output + fresh coinbase - new txn fee
#     327                 :          2 :     hash = tx.GetHash();
#     328                 :          2 :     m_node.mempool->addUnchecked(entry.Fee(HIGHERFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
#     329                 :          2 :     BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
#     330                 :          2 :     m_node.mempool->clear();
#     331                 :            : 
#     332                 :            :     // coinbase in *m_node.mempool, template creation fails
#     333                 :          2 :     tx.vin.resize(1);
#     334                 :          2 :     tx.vin[0].prevout.SetNull();
#     335                 :          2 :     tx.vin[0].scriptSig = CScript() << OP_0 << OP_1;
#     336                 :          2 :     tx.vout[0].nValue = 0;
#     337                 :          2 :     hash = tx.GetHash();
#     338                 :            :     // give it a fee so it'll get mined
#     339                 :          2 :     m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
#     340                 :            :     // Should throw bad-cb-multiple
#     341                 :          2 :     BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-cb-multiple"));
#     342                 :          2 :     m_node.mempool->clear();
#     343                 :            : 
#     344                 :            :     // double spend txn pair in *m_node.mempool, template creation fails
#     345                 :          2 :     tx.vin[0].prevout.hash = txFirst[0]->GetHash();
#     346                 :          2 :     tx.vin[0].scriptSig = CScript() << OP_1;
#     347                 :          2 :     tx.vout[0].nValue = BLOCKSUBSIDY-HIGHFEE;
#     348                 :          2 :     tx.vout[0].scriptPubKey = CScript() << OP_1;
#     349                 :          2 :     hash = tx.GetHash();
#     350                 :          2 :     m_node.mempool->addUnchecked(entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
#     351                 :          2 :     tx.vout[0].scriptPubKey = CScript() << OP_2;
#     352                 :          2 :     hash = tx.GetHash();
#     353                 :          2 :     m_node.mempool->addUnchecked(entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
#     354                 :          2 :     BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
#     355                 :          2 :     m_node.mempool->clear();
#     356                 :            : 
#     357                 :            :     // subsidy changing
#     358                 :          2 :     int nHeight = m_node.chainman->ActiveChain().Height();
#     359                 :            :     // Create an actual 209999-long block chain (without valid blocks).
#     360         [ +  + ]:     419780 :     while (m_node.chainman->ActiveChain().Tip()->nHeight < 209999) {
#     361                 :     419778 :         CBlockIndex* prev = m_node.chainman->ActiveChain().Tip();
#     362                 :     419778 :         CBlockIndex* next = new CBlockIndex();
#     363                 :     419778 :         next->phashBlock = new uint256(InsecureRand256());
#     364                 :     419778 :         m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(next->GetBlockHash());
#     365                 :     419778 :         next->pprev = prev;
#     366                 :     419778 :         next->nHeight = prev->nHeight + 1;
#     367                 :     419778 :         next->BuildSkip();
#     368                 :     419778 :         m_node.chainman->ActiveChain().SetTip(next);
#     369                 :     419778 :     }
#     370                 :          2 :     BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
#     371                 :            :     // Extend to a 210000-long block chain.
#     372         [ +  + ]:          4 :     while (m_node.chainman->ActiveChain().Tip()->nHeight < 210000) {
#     373                 :          2 :         CBlockIndex* prev = m_node.chainman->ActiveChain().Tip();
#     374                 :          2 :         CBlockIndex* next = new CBlockIndex();
#     375                 :          2 :         next->phashBlock = new uint256(InsecureRand256());
#     376                 :          2 :         m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(next->GetBlockHash());
#     377                 :          2 :         next->pprev = prev;
#     378                 :          2 :         next->nHeight = prev->nHeight + 1;
#     379                 :          2 :         next->BuildSkip();
#     380                 :          2 :         m_node.chainman->ActiveChain().SetTip(next);
#     381                 :          2 :     }
#     382                 :          2 :     BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
#     383                 :            : 
#     384                 :            :     // invalid p2sh txn in *m_node.mempool, template creation fails
#     385                 :          2 :     tx.vin[0].prevout.hash = txFirst[0]->GetHash();
#     386                 :          2 :     tx.vin[0].prevout.n = 0;
#     387                 :          2 :     tx.vin[0].scriptSig = CScript() << OP_1;
#     388                 :          2 :     tx.vout[0].nValue = BLOCKSUBSIDY-LOWFEE;
#     389                 :          2 :     script = CScript() << OP_0;
#     390                 :          2 :     tx.vout[0].scriptPubKey = GetScriptForDestination(ScriptHash(script));
#     391                 :          2 :     hash = tx.GetHash();
#     392                 :          2 :     m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
#     393                 :          2 :     tx.vin[0].prevout.hash = hash;
#     394                 :          2 :     tx.vin[0].scriptSig = CScript() << std::vector<unsigned char>(script.begin(), script.end());
#     395                 :          2 :     tx.vout[0].nValue -= LOWFEE;
#     396                 :          2 :     hash = tx.GetHash();
#     397                 :          2 :     m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
#     398                 :            :     // Should throw block-validation-failed
#     399                 :          2 :     BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("block-validation-failed"));
#     400                 :          2 :     m_node.mempool->clear();
#     401                 :            : 
#     402                 :            :     // Delete the dummy blocks again.
#     403         [ +  + ]:     419782 :     while (m_node.chainman->ActiveChain().Tip()->nHeight > nHeight) {
#     404                 :     419780 :         CBlockIndex* del = m_node.chainman->ActiveChain().Tip();
#     405                 :     419780 :         m_node.chainman->ActiveChain().SetTip(del->pprev);
#     406                 :     419780 :         m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(del->pprev->GetBlockHash());
#     407                 :     419780 :         delete del->phashBlock;
#     408                 :     419780 :         delete del;
#     409                 :     419780 :     }
#     410                 :            : 
#     411                 :            :     // non-final txs in mempool
#     412                 :          2 :     SetMockTime(m_node.chainman->ActiveChain().Tip()->GetMedianTimePast()+1);
#     413                 :          2 :     const int flags{LOCKTIME_VERIFY_SEQUENCE | LOCKTIME_MEDIAN_TIME_PAST};
#     414                 :            :     // height map
#     415                 :          2 :     std::vector<int> prevheights;
#     416                 :            : 
#     417                 :            :     // relative height locked
#     418                 :          2 :     tx.nVersion = 2;
#     419                 :          2 :     tx.vin.resize(1);
#     420                 :          2 :     prevheights.resize(1);
#     421                 :          2 :     tx.vin[0].prevout.hash = txFirst[0]->GetHash(); // only 1 transaction
#     422                 :          2 :     tx.vin[0].prevout.n = 0;
#     423                 :          2 :     tx.vin[0].scriptSig = CScript() << OP_1;
#     424                 :          2 :     tx.vin[0].nSequence = m_node.chainman->ActiveChain().Tip()->nHeight + 1; // txFirst[0] is the 2nd block
#     425                 :          2 :     prevheights[0] = baseheight + 1;
#     426                 :          2 :     tx.vout.resize(1);
#     427                 :          2 :     tx.vout[0].nValue = BLOCKSUBSIDY-HIGHFEE;
#     428                 :          2 :     tx.vout[0].scriptPubKey = CScript() << OP_1;
#     429                 :          2 :     tx.nLockTime = 0;
#     430                 :          2 :     hash = tx.GetHash();
#     431                 :          2 :     m_node.mempool->addUnchecked(entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
#     432                 :          2 :     BOOST_CHECK(CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime passes
#     433                 :          2 :     BOOST_CHECK(!TestSequenceLocks(CTransaction{tx})); // Sequence locks fail
#     434                 :            : 
#     435                 :          2 :     {
#     436                 :          2 :         CBlockIndex* active_chain_tip = m_node.chainman->ActiveChain().Tip();
#     437                 :          2 :         BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, prevheights, CreateBlockIndex(active_chain_tip->nHeight + 2, active_chain_tip))); // Sequence locks pass on 2nd block
#     438                 :          2 :     }
#     439                 :            : 
#     440                 :            :     // relative time locked
#     441                 :          2 :     tx.vin[0].prevout.hash = txFirst[1]->GetHash();
#     442                 :          2 :     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
#     443                 :          2 :     prevheights[0] = baseheight + 2;
#     444                 :          2 :     hash = tx.GetHash();
#     445                 :          2 :     m_node.mempool->addUnchecked(entry.Time(GetTime()).FromTx(tx));
#     446                 :          2 :     BOOST_CHECK(CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime passes
#     447                 :          2 :     BOOST_CHECK(!TestSequenceLocks(CTransaction{tx})); // Sequence locks fail
#     448                 :            : 
#     449         [ +  + ]:         24 :     for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
#     450                 :         22 :         m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i)->nTime += 512; //Trick the MedianTimePast
#     451                 :            : 
#     452                 :          2 :     {
#     453                 :          2 :         CBlockIndex* active_chain_tip = m_node.chainman->ActiveChain().Tip();
#     454                 :          2 :         BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, prevheights, CreateBlockIndex(active_chain_tip->nHeight + 1, active_chain_tip))); // Sequence locks pass 512 seconds later
#     455                 :          2 :     }
#     456                 :            : 
#     457         [ +  + ]:         24 :     for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
#     458                 :         22 :         m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i)->nTime -= 512; //undo tricked MTP
#     459                 :            : 
#     460                 :            :     // absolute height locked
#     461                 :          2 :     tx.vin[0].prevout.hash = txFirst[2]->GetHash();
#     462                 :          2 :     tx.vin[0].nSequence = CTxIn::MAX_SEQUENCE_NONFINAL;
#     463                 :          2 :     prevheights[0] = baseheight + 3;
#     464                 :          2 :     tx.nLockTime = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
#     465                 :          2 :     hash = tx.GetHash();
#     466                 :          2 :     m_node.mempool->addUnchecked(entry.Time(GetTime()).FromTx(tx));
#     467                 :          2 :     BOOST_CHECK(!CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime fails
#     468                 :          2 :     BOOST_CHECK(TestSequenceLocks(CTransaction{tx})); // Sequence locks pass
#     469                 :          2 :     BOOST_CHECK(IsFinalTx(CTransaction(tx), m_node.chainman->ActiveChain().Tip()->nHeight + 2, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast())); // Locktime passes on 2nd block
#     470                 :            : 
#     471                 :            :     // absolute time locked
#     472                 :          2 :     tx.vin[0].prevout.hash = txFirst[3]->GetHash();
#     473                 :          2 :     tx.nLockTime = m_node.chainman->ActiveChain().Tip()->GetMedianTimePast();
#     474                 :          2 :     prevheights.resize(1);
#     475                 :          2 :     prevheights[0] = baseheight + 4;
#     476                 :          2 :     hash = tx.GetHash();
#     477                 :          2 :     m_node.mempool->addUnchecked(entry.Time(GetTime()).FromTx(tx));
#     478                 :          2 :     BOOST_CHECK(!CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime fails
#     479                 :          2 :     BOOST_CHECK(TestSequenceLocks(CTransaction{tx})); // Sequence locks pass
#     480                 :          2 :     BOOST_CHECK(IsFinalTx(CTransaction(tx), m_node.chainman->ActiveChain().Tip()->nHeight + 2, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1)); // Locktime passes 1 second later
#     481                 :            : 
#     482                 :            :     // mempool-dependent transactions (not added)
#     483                 :          2 :     tx.vin[0].prevout.hash = hash;
#     484                 :          2 :     prevheights[0] = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
#     485                 :          2 :     tx.nLockTime = 0;
#     486                 :          2 :     tx.vin[0].nSequence = 0;
#     487                 :          2 :     BOOST_CHECK(CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime passes
#     488                 :          2 :     BOOST_CHECK(TestSequenceLocks(CTransaction{tx})); // Sequence locks pass
#     489                 :          2 :     tx.vin[0].nSequence = 1;
#     490                 :          2 :     BOOST_CHECK(!TestSequenceLocks(CTransaction{tx})); // Sequence locks fail
#     491                 :          2 :     tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG;
#     492                 :          2 :     BOOST_CHECK(TestSequenceLocks(CTransaction{tx})); // Sequence locks pass
#     493                 :          2 :     tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | 1;
#     494                 :          2 :     BOOST_CHECK(!TestSequenceLocks(CTransaction{tx})); // Sequence locks fail
#     495                 :            : 
#     496                 :          2 :     BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
#     497                 :            : 
#     498                 :            :     // None of the of the absolute height/time locked tx should have made
#     499                 :            :     // it into the template because we still check IsFinalTx in CreateNewBlock,
#     500                 :            :     // but relative locked txs will if inconsistently added to mempool.
#     501                 :            :     // For now these will still generate a valid template until BIP68 soft fork
#     502                 :          2 :     BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 3U);
#     503                 :            :     // However if we advance height by 1 and time by 512, all of them should be mined
#     504         [ +  + ]:         24 :     for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
#     505                 :         22 :         m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i)->nTime += 512; //Trick the MedianTimePast
#     506                 :          2 :     m_node.chainman->ActiveChain().Tip()->nHeight++;
#     507                 :          2 :     SetMockTime(m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1);
#     508                 :            : 
#     509                 :          2 :     BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
#     510                 :          2 :     BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 5U);
#     511                 :            : 
#     512                 :          2 :     m_node.chainman->ActiveChain().Tip()->nHeight--;
#     513                 :          2 :     SetMockTime(0);
#     514                 :          2 :     m_node.mempool->clear();
#     515                 :            : 
#     516                 :          2 :     TestPackageSelection(chainparams, scriptPubKey, txFirst);
#     517                 :            : 
#     518                 :          2 :     fCheckpointsEnabled = true;
#     519                 :          2 : }
#     520                 :            : 
#     521                 :            : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a