Branch data Line data Source code
# 1 : : // Copyright (c) 2011-2020 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 <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 : :
# 20 : : #include <test/util/setup_common.h>
# 21 : :
# 22 : : #include <memory>
# 23 : :
# 24 : : #include <boost/test/unit_test.hpp>
# 25 : :
# 26 : : namespace miner_tests {
# 27 : : struct MinerTestingSetup : public TestingSetup {
# 28 : : void TestPackageSelection(const CChainParams& chainparams, const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_node.mempool->cs);
# 29 : : bool TestSequenceLocks(const CTransaction& tx, int flags) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_node.mempool->cs)
# 30 : 16 : {
# 31 : 16 : CCoinsViewMemPool viewMempool(&m_node.chainman->ActiveChainstate().CoinsTip(), *m_node.mempool);
# 32 : 16 : return CheckSequenceLocks(m_node.chainman->ActiveChain().Tip(), viewMempool, tx, flags);
# 33 : 16 : }
# 34 : : BlockAssembler AssemblerForTest(const CChainParams& params);
# 35 : : };
# 36 : : } // namespace miner_tests
# 37 : :
# 38 : : BOOST_FIXTURE_TEST_SUITE(miner_tests, MinerTestingSetup)
# 39 : :
# 40 : : static CFeeRate blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
# 41 : :
# 42 : : BlockAssembler MinerTestingSetup::AssemblerForTest(const CChainParams& params)
# 43 : 38 : {
# 44 : 38 : BlockAssembler::Options options;
# 45 : :
# 46 : 38 : options.nBlockMaxWeight = MAX_BLOCK_WEIGHT;
# 47 : 38 : options.blockMinFeeRate = blockMinFeeRate;
# 48 : 38 : return BlockAssembler(::ChainstateActive(), *m_node.mempool, params, options);
# 49 : 38 : }
# 50 : :
# 51 : : constexpr static struct {
# 52 : : unsigned char extranonce;
# 53 : : unsigned int nonce;
# 54 : : } blockinfo[] = {
# 55 : : {4, 0xa4a3e223}, {2, 0x15c32f9e}, {1, 0x0375b547}, {1, 0x7004a8a5},
# 56 : : {2, 0xce440296}, {2, 0x52cfe198}, {1, 0x77a72cd0}, {2, 0xbb5d6f84},
# 57 : : {2, 0x83f30c2c}, {1, 0x48a73d5b}, {1, 0xef7dcd01}, {2, 0x6809c6c4},
# 58 : : {2, 0x0883ab3c}, {1, 0x087bbbe2}, {2, 0x2104a814}, {2, 0xdffb6daa},
# 59 : : {1, 0xee8a0a08}, {2, 0xba4237c1}, {1, 0xa70349dc}, {1, 0x344722bb},
# 60 : : {3, 0xd6294733}, {2, 0xec9f5c94}, {2, 0xca2fbc28}, {1, 0x6ba4f406},
# 61 : : {2, 0x015d4532}, {1, 0x6e119b7c}, {2, 0x43e8f314}, {2, 0x27962f38},
# 62 : : {2, 0xb571b51b}, {2, 0xb36bee23}, {2, 0xd17924a8}, {2, 0x6bc212d9},
# 63 : : {1, 0x630d4948}, {2, 0x9a4c4ebb}, {2, 0x554be537}, {1, 0xd63ddfc7},
# 64 : : {2, 0xa10acc11}, {1, 0x759a8363}, {2, 0xfb73090d}, {1, 0xe82c6a34},
# 65 : : {1, 0xe33e92d7}, {3, 0x658ef5cb}, {2, 0xba32ff22}, {5, 0x0227a10c},
# 66 : : {1, 0xa9a70155}, {5, 0xd096d809}, {1, 0x37176174}, {1, 0x830b8d0f},
# 67 : : {1, 0xc6e3910e}, {2, 0x823f3ca8}, {1, 0x99850849}, {1, 0x7521fb81},
# 68 : : {1, 0xaacaabab}, {1, 0xd645a2eb}, {5, 0x7aea1781}, {5, 0x9d6e4b78},
# 69 : : {1, 0x4ce90fd8}, {1, 0xabdc832d}, {6, 0x4a34f32a}, {2, 0xf2524c1c},
# 70 : : {2, 0x1bbeb08a}, {1, 0xad47f480}, {1, 0x9f026aeb}, {1, 0x15a95049},
# 71 : : {2, 0xd1cb95b2}, {2, 0xf84bbda5}, {1, 0x0fa62cd1}, {1, 0xe05f9169},
# 72 : : {1, 0x78d194a9}, {5, 0x3e38147b}, {5, 0x737ba0d4}, {1, 0x63378e10},
# 73 : : {1, 0x6d5f91cf}, {2, 0x88612eb8}, {2, 0xe9639484}, {1, 0xb7fabc9d},
# 74 : : {2, 0x19b01592}, {1, 0x5a90dd31}, {2, 0x5bd7e028}, {2, 0x94d00323},
# 75 : : {1, 0xa9b9c01a}, {1, 0x3a40de61}, {1, 0x56e7eec7}, {5, 0x859f7ef6},
# 76 : : {1, 0xfd8e5630}, {1, 0x2b0c9f7f}, {1, 0xba700e26}, {1, 0x7170a408},
# 77 : : {1, 0x70de86a8}, {1, 0x74d64cd5}, {1, 0x49e738a1}, {2, 0x6910b602},
# 78 : : {0, 0x643c565f}, {1, 0x54264b3f}, {2, 0x97ea6396}, {2, 0x55174459},
# 79 : : {2, 0x03e8779a}, {1, 0x98f34d8f}, {1, 0xc07b2b07}, {1, 0xdfe29668},
# 80 : : {1, 0x3141c7c1}, {1, 0xb3b595f4}, {1, 0x735abf08}, {5, 0x623bfbce},
# 81 : : {2, 0xd351e722}, {1, 0xf4ca48c9}, {1, 0x5b19c670}, {1, 0xa164bf0e},
# 82 : : {2, 0xbbbeb305}, {2, 0xfe1c810a},
# 83 : : };
# 84 : :
# 85 : : static CBlockIndex CreateBlockIndex(int nHeight) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
# 86 : 4 : {
# 87 : 4 : CBlockIndex index;
# 88 : 4 : index.nHeight = nHeight;
# 89 : 4 : index.pprev = ::ChainActive().Tip();
# 90 : 4 : return index;
# 91 : 4 : }
# 92 : :
# 93 : : // Test suite for ancestor feerate transaction selection.
# 94 : : // Implemented as an additional function, rather than a separate test case,
# 95 : : // to allow reusing the blockchain created in CreateNewBlock_validity.
# 96 : : void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst)
# 97 : 2 : {
# 98 : : // Test the ancestor feerate transaction selection.
# 99 : 2 : TestMemPoolEntryHelper entry;
# 100 : :
# 101 : : // Test that a medium fee transaction will be selected after a higher fee
# 102 : : // rate package with a low fee rate parent.
# 103 : 2 : CMutableTransaction tx;
# 104 : 2 : tx.vin.resize(1);
# 105 : 2 : tx.vin[0].scriptSig = CScript() << OP_1;
# 106 : 2 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
# 107 : 2 : tx.vin[0].prevout.n = 0;
# 108 : 2 : tx.vout.resize(1);
# 109 : 2 : tx.vout[0].nValue = 5000000000LL - 1000;
# 110 : : // This tx has a low fee: 1000 satoshis
# 111 : 2 : uint256 hashParentTx = tx.GetHash(); // save this txid for later use
# 112 : 2 : m_node.mempool->addUnchecked(entry.Fee(1000).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
# 113 : :
# 114 : : // This tx has a medium fee: 10000 satoshis
# 115 : 2 : tx.vin[0].prevout.hash = txFirst[1]->GetHash();
# 116 : 2 : tx.vout[0].nValue = 5000000000LL - 10000;
# 117 : 2 : uint256 hashMediumFeeTx = tx.GetHash();
# 118 : 2 : m_node.mempool->addUnchecked(entry.Fee(10000).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
# 119 : :
# 120 : : // This tx has a high fee, but depends on the first transaction
# 121 : 2 : tx.vin[0].prevout.hash = hashParentTx;
# 122 : 2 : tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 50k satoshi fee
# 123 : 2 : uint256 hashHighFeeTx = tx.GetHash();
# 124 : 2 : m_node.mempool->addUnchecked(entry.Fee(50000).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
# 125 : :
# 126 : 2 : std::unique_ptr<CBlockTemplate> pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
# 127 : 2 : BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 4U);
# 128 : 2 : BOOST_CHECK(pblocktemplate->block.vtx[1]->GetHash() == hashParentTx);
# 129 : 2 : BOOST_CHECK(pblocktemplate->block.vtx[2]->GetHash() == hashHighFeeTx);
# 130 : 2 : BOOST_CHECK(pblocktemplate->block.vtx[3]->GetHash() == hashMediumFeeTx);
# 131 : :
# 132 : : // Test that a package below the block min tx fee doesn't get included
# 133 : 2 : tx.vin[0].prevout.hash = hashHighFeeTx;
# 134 : 2 : tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 0 fee
# 135 : 2 : uint256 hashFreeTx = tx.GetHash();
# 136 : 2 : m_node.mempool->addUnchecked(entry.Fee(0).FromTx(tx));
# 137 : 2 : size_t freeTxSize = ::GetSerializeSize(tx, PROTOCOL_VERSION);
# 138 : :
# 139 : : // Calculate a fee on child transaction that will put the package just
# 140 : : // below the block min tx fee (assuming 1 child tx of the same size).
# 141 : 2 : CAmount feeToUse = blockMinFeeRate.GetFee(2*freeTxSize) - 1;
# 142 : :
# 143 : 2 : tx.vin[0].prevout.hash = hashFreeTx;
# 144 : 2 : tx.vout[0].nValue = 5000000000LL - 1000 - 50000 - feeToUse;
# 145 : 2 : uint256 hashLowFeeTx = tx.GetHash();
# 146 : 2 : m_node.mempool->addUnchecked(entry.Fee(feeToUse).FromTx(tx));
# 147 : 2 : pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
# 148 : : // Verify that the free tx and the low fee tx didn't get selected
# 149 [ + + ]: 10 : for (size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
# 150 : 8 : BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashFreeTx);
# 151 : 8 : BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashLowFeeTx);
# 152 : 8 : }
# 153 : :
# 154 : : // Test that packages above the min relay fee do get included, even if one
# 155 : : // of the transactions is below the min relay fee
# 156 : : // Remove the low fee transaction and replace with a higher fee transaction
# 157 : 2 : m_node.mempool->removeRecursive(CTransaction(tx), MemPoolRemovalReason::REPLACED);
# 158 : 2 : tx.vout[0].nValue -= 2; // Now we should be just over the min relay fee
# 159 : 2 : hashLowFeeTx = tx.GetHash();
# 160 : 2 : m_node.mempool->addUnchecked(entry.Fee(feeToUse+2).FromTx(tx));
# 161 : 2 : pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
# 162 : 2 : BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 6U);
# 163 : 2 : BOOST_CHECK(pblocktemplate->block.vtx[4]->GetHash() == hashFreeTx);
# 164 : 2 : BOOST_CHECK(pblocktemplate->block.vtx[5]->GetHash() == hashLowFeeTx);
# 165 : :
# 166 : : // Test that transaction selection properly updates ancestor fee
# 167 : : // calculations as ancestor transactions get included in a block.
# 168 : : // Add a 0-fee transaction that has 2 outputs.
# 169 : 2 : tx.vin[0].prevout.hash = txFirst[2]->GetHash();
# 170 : 2 : tx.vout.resize(2);
# 171 : 2 : tx.vout[0].nValue = 5000000000LL - 100000000;
# 172 : 2 : tx.vout[1].nValue = 100000000; // 1BTC output
# 173 : 2 : uint256 hashFreeTx2 = tx.GetHash();
# 174 : 2 : m_node.mempool->addUnchecked(entry.Fee(0).SpendsCoinbase(true).FromTx(tx));
# 175 : :
# 176 : : // This tx can't be mined by itself
# 177 : 2 : tx.vin[0].prevout.hash = hashFreeTx2;
# 178 : 2 : tx.vout.resize(1);
# 179 : 2 : feeToUse = blockMinFeeRate.GetFee(freeTxSize);
# 180 : 2 : tx.vout[0].nValue = 5000000000LL - 100000000 - feeToUse;
# 181 : 2 : uint256 hashLowFeeTx2 = tx.GetHash();
# 182 : 2 : m_node.mempool->addUnchecked(entry.Fee(feeToUse).SpendsCoinbase(false).FromTx(tx));
# 183 : 2 : pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
# 184 : :
# 185 : : // Verify that this tx isn't selected.
# 186 [ + + ]: 14 : for (size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
# 187 : 12 : BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashFreeTx2);
# 188 : 12 : BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashLowFeeTx2);
# 189 : 12 : }
# 190 : :
# 191 : : // This tx will be mineable, and should cause hashLowFeeTx2 to be selected
# 192 : : // as well.
# 193 : 2 : tx.vin[0].prevout.n = 1;
# 194 : 2 : tx.vout[0].nValue = 100000000 - 10000; // 10k satoshi fee
# 195 : 2 : m_node.mempool->addUnchecked(entry.Fee(10000).FromTx(tx));
# 196 : 2 : pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
# 197 : 2 : BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 9U);
# 198 : 2 : BOOST_CHECK(pblocktemplate->block.vtx[8]->GetHash() == hashLowFeeTx2);
# 199 : 2 : }
# 200 : :
# 201 : : // NOTE: These tests rely on CreateNewBlock doing its own self-validation!
# 202 : : BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
# 203 : 2 : {
# 204 : : // Note that by default, these tests run with size accounting enabled.
# 205 : 2 : const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
# 206 : 2 : const CChainParams& chainparams = *chainParams;
# 207 : 2 : CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
# 208 : 2 : std::unique_ptr<CBlockTemplate> pblocktemplate;
# 209 : 2 : CMutableTransaction tx;
# 210 : 2 : CScript script;
# 211 : 2 : uint256 hash;
# 212 : 2 : TestMemPoolEntryHelper entry;
# 213 : 2 : entry.nFee = 11;
# 214 : 2 : entry.nHeight = 11;
# 215 : :
# 216 : 2 : fCheckpointsEnabled = false;
# 217 : :
# 218 : : // Simple block creation, nothing special yet:
# 219 : 2 : BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
# 220 : :
# 221 : : // We can't make transactions until we have inputs
# 222 : : // Therefore, load 110 blocks :)
# 223 : 2 : static_assert(std::size(blockinfo) == 110, "Should have 110 blocks to import");
# 224 : 2 : int baseheight = 0;
# 225 : 2 : std::vector<CTransactionRef> txFirst;
# 226 [ + + ]: 220 : for (const auto& bi : blockinfo) {
# 227 : 220 : CBlock *pblock = &pblocktemplate->block; // pointer for convenience
# 228 : 220 : {
# 229 : 220 : LOCK(cs_main);
# 230 : 220 : pblock->nVersion = 1;
# 231 : 220 : pblock->nTime = ::ChainActive().Tip()->GetMedianTimePast()+1;
# 232 : 220 : CMutableTransaction txCoinbase(*pblock->vtx[0]);
# 233 : 220 : txCoinbase.nVersion = 1;
# 234 : 220 : txCoinbase.vin[0].scriptSig = CScript();
# 235 : 220 : txCoinbase.vin[0].scriptSig.push_back(bi.extranonce);
# 236 : 220 : txCoinbase.vin[0].scriptSig.push_back(::ChainActive().Height());
# 237 : 220 : txCoinbase.vout.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this)
# 238 : 220 : txCoinbase.vout[0].scriptPubKey = CScript();
# 239 : 220 : pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
# 240 [ + + ]: 220 : if (txFirst.size() == 0)
# 241 : 2 : baseheight = ::ChainActive().Height();
# 242 [ + + ]: 220 : if (txFirst.size() < 4)
# 243 : 8 : txFirst.push_back(pblock->vtx[0]);
# 244 : 220 : pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
# 245 : 220 : pblock->nNonce = bi.nonce;
# 246 : 220 : }
# 247 : 220 : std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(*pblock);
# 248 : 220 : BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(chainparams, shared_pblock, true, nullptr));
# 249 : 220 : pblock->hashPrevBlock = pblock->GetHash();
# 250 : 220 : }
# 251 : :
# 252 : 2 : LOCK(cs_main);
# 253 : 2 : LOCK(m_node.mempool->cs);
# 254 : :
# 255 : : // Just to make sure we can still make simple blocks
# 256 : 2 : BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
# 257 : :
# 258 : 2 : const CAmount BLOCKSUBSIDY = 50*COIN;
# 259 : 2 : const CAmount LOWFEE = CENT;
# 260 : 2 : const CAmount HIGHFEE = COIN;
# 261 : 2 : const CAmount HIGHERFEE = 4*COIN;
# 262 : :
# 263 : : // block sigops > limit: 1000 CHECKMULTISIG + 1
# 264 : 2 : tx.vin.resize(1);
# 265 : : // NOTE: OP_NOP is used to force 20 SigOps for the CHECKMULTISIG
# 266 : 2 : tx.vin[0].scriptSig = CScript() << OP_0 << OP_0 << OP_0 << OP_NOP << OP_CHECKMULTISIG << OP_1;
# 267 : 2 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
# 268 : 2 : tx.vin[0].prevout.n = 0;
# 269 : 2 : tx.vout.resize(1);
# 270 : 2 : tx.vout[0].nValue = BLOCKSUBSIDY;
# 271 [ + + ]: 2004 : for (unsigned int i = 0; i < 1001; ++i)
# 272 : 2002 : {
# 273 : 2002 : tx.vout[0].nValue -= LOWFEE;
# 274 : 2002 : hash = tx.GetHash();
# 275 : 2002 : bool spendsCoinbase = i == 0; // only first tx spends coinbase
# 276 : : // If we don't set the # of sig ops in the CTxMemPoolEntry, template creation fails
# 277 : 2002 : m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(spendsCoinbase).FromTx(tx));
# 278 : 2002 : tx.vin[0].prevout.hash = hash;
# 279 : 2002 : }
# 280 : :
# 281 : 2 : BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-blk-sigops"));
# 282 : 2 : m_node.mempool->clear();
# 283 : :
# 284 : 2 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
# 285 : 2 : tx.vout[0].nValue = BLOCKSUBSIDY;
# 286 [ + + ]: 2004 : for (unsigned int i = 0; i < 1001; ++i)
# 287 : 2002 : {
# 288 : 2002 : tx.vout[0].nValue -= LOWFEE;
# 289 : 2002 : hash = tx.GetHash();
# 290 : 2002 : bool spendsCoinbase = i == 0; // only first tx spends coinbase
# 291 : : // If we do set the # of sig ops in the CTxMemPoolEntry, template creation passes
# 292 : 2002 : m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(spendsCoinbase).SigOpsCost(80).FromTx(tx));
# 293 : 2002 : tx.vin[0].prevout.hash = hash;
# 294 : 2002 : }
# 295 : 2 : BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
# 296 : 2 : m_node.mempool->clear();
# 297 : :
# 298 : : // block size > limit
# 299 : 2 : tx.vin[0].scriptSig = CScript();
# 300 : : // 18 * (520char + DROP) + OP_1 = 9433 bytes
# 301 : 2 : std::vector<unsigned char> vchData(520);
# 302 [ + + ]: 38 : for (unsigned int i = 0; i < 18; ++i)
# 303 : 36 : tx.vin[0].scriptSig << vchData << OP_DROP;
# 304 : 2 : tx.vin[0].scriptSig << OP_1;
# 305 : 2 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
# 306 : 2 : tx.vout[0].nValue = BLOCKSUBSIDY;
# 307 [ + + ]: 258 : for (unsigned int i = 0; i < 128; ++i)
# 308 : 256 : {
# 309 : 256 : tx.vout[0].nValue -= LOWFEE;
# 310 : 256 : hash = tx.GetHash();
# 311 : 256 : bool spendsCoinbase = i == 0; // only first tx spends coinbase
# 312 : 256 : m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(spendsCoinbase).FromTx(tx));
# 313 : 256 : tx.vin[0].prevout.hash = hash;
# 314 : 256 : }
# 315 : 2 : BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
# 316 : 2 : m_node.mempool->clear();
# 317 : :
# 318 : : // orphan in *m_node.mempool, template creation fails
# 319 : 2 : hash = tx.GetHash();
# 320 : 2 : m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).FromTx(tx));
# 321 : 2 : BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
# 322 : 2 : m_node.mempool->clear();
# 323 : :
# 324 : : // child with higher feerate than parent
# 325 : 2 : tx.vin[0].scriptSig = CScript() << OP_1;
# 326 : 2 : tx.vin[0].prevout.hash = txFirst[1]->GetHash();
# 327 : 2 : tx.vout[0].nValue = BLOCKSUBSIDY-HIGHFEE;
# 328 : 2 : hash = tx.GetHash();
# 329 : 2 : m_node.mempool->addUnchecked(entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
# 330 : 2 : tx.vin[0].prevout.hash = hash;
# 331 : 2 : tx.vin.resize(2);
# 332 : 2 : tx.vin[1].scriptSig = CScript() << OP_1;
# 333 : 2 : tx.vin[1].prevout.hash = txFirst[0]->GetHash();
# 334 : 2 : tx.vin[1].prevout.n = 0;
# 335 : 2 : tx.vout[0].nValue = tx.vout[0].nValue+BLOCKSUBSIDY-HIGHERFEE; //First txn output + fresh coinbase - new txn fee
# 336 : 2 : hash = tx.GetHash();
# 337 : 2 : m_node.mempool->addUnchecked(entry.Fee(HIGHERFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
# 338 : 2 : BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
# 339 : 2 : m_node.mempool->clear();
# 340 : :
# 341 : : // coinbase in *m_node.mempool, template creation fails
# 342 : 2 : tx.vin.resize(1);
# 343 : 2 : tx.vin[0].prevout.SetNull();
# 344 : 2 : tx.vin[0].scriptSig = CScript() << OP_0 << OP_1;
# 345 : 2 : tx.vout[0].nValue = 0;
# 346 : 2 : hash = tx.GetHash();
# 347 : : // give it a fee so it'll get mined
# 348 : 2 : m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
# 349 : : // Should throw bad-cb-multiple
# 350 : 2 : BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-cb-multiple"));
# 351 : 2 : m_node.mempool->clear();
# 352 : :
# 353 : : // double spend txn pair in *m_node.mempool, template creation fails
# 354 : 2 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
# 355 : 2 : tx.vin[0].scriptSig = CScript() << OP_1;
# 356 : 2 : tx.vout[0].nValue = BLOCKSUBSIDY-HIGHFEE;
# 357 : 2 : tx.vout[0].scriptPubKey = CScript() << OP_1;
# 358 : 2 : hash = tx.GetHash();
# 359 : 2 : m_node.mempool->addUnchecked(entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
# 360 : 2 : tx.vout[0].scriptPubKey = CScript() << OP_2;
# 361 : 2 : hash = tx.GetHash();
# 362 : 2 : m_node.mempool->addUnchecked(entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
# 363 : 2 : BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
# 364 : 2 : m_node.mempool->clear();
# 365 : :
# 366 : : // subsidy changing
# 367 : 2 : int nHeight = ::ChainActive().Height();
# 368 : : // Create an actual 209999-long block chain (without valid blocks).
# 369 [ + + ]: 419780 : while (::ChainActive().Tip()->nHeight < 209999) {
# 370 : 419778 : CBlockIndex* prev = ::ChainActive().Tip();
# 371 : 419778 : CBlockIndex* next = new CBlockIndex();
# 372 : 419778 : next->phashBlock = new uint256(InsecureRand256());
# 373 : 419778 : ::ChainstateActive().CoinsTip().SetBestBlock(next->GetBlockHash());
# 374 : 419778 : next->pprev = prev;
# 375 : 419778 : next->nHeight = prev->nHeight + 1;
# 376 : 419778 : next->BuildSkip();
# 377 : 419778 : ::ChainActive().SetTip(next);
# 378 : 419778 : }
# 379 : 2 : BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
# 380 : : // Extend to a 210000-long block chain.
# 381 [ + + ]: 4 : while (::ChainActive().Tip()->nHeight < 210000) {
# 382 : 2 : CBlockIndex* prev = ::ChainActive().Tip();
# 383 : 2 : CBlockIndex* next = new CBlockIndex();
# 384 : 2 : next->phashBlock = new uint256(InsecureRand256());
# 385 : 2 : ::ChainstateActive().CoinsTip().SetBestBlock(next->GetBlockHash());
# 386 : 2 : next->pprev = prev;
# 387 : 2 : next->nHeight = prev->nHeight + 1;
# 388 : 2 : next->BuildSkip();
# 389 : 2 : ::ChainActive().SetTip(next);
# 390 : 2 : }
# 391 : 2 : BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
# 392 : :
# 393 : : // invalid p2sh txn in *m_node.mempool, template creation fails
# 394 : 2 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
# 395 : 2 : tx.vin[0].prevout.n = 0;
# 396 : 2 : tx.vin[0].scriptSig = CScript() << OP_1;
# 397 : 2 : tx.vout[0].nValue = BLOCKSUBSIDY-LOWFEE;
# 398 : 2 : script = CScript() << OP_0;
# 399 : 2 : tx.vout[0].scriptPubKey = GetScriptForDestination(ScriptHash(script));
# 400 : 2 : hash = tx.GetHash();
# 401 : 2 : m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
# 402 : 2 : tx.vin[0].prevout.hash = hash;
# 403 : 2 : tx.vin[0].scriptSig = CScript() << std::vector<unsigned char>(script.begin(), script.end());
# 404 : 2 : tx.vout[0].nValue -= LOWFEE;
# 405 : 2 : hash = tx.GetHash();
# 406 : 2 : m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
# 407 : : // Should throw block-validation-failed
# 408 : 2 : BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("block-validation-failed"));
# 409 : 2 : m_node.mempool->clear();
# 410 : :
# 411 : : // Delete the dummy blocks again.
# 412 [ + + ]: 419782 : while (::ChainActive().Tip()->nHeight > nHeight) {
# 413 : 419780 : CBlockIndex* del = ::ChainActive().Tip();
# 414 : 419780 : ::ChainActive().SetTip(del->pprev);
# 415 : 419780 : ::ChainstateActive().CoinsTip().SetBestBlock(del->pprev->GetBlockHash());
# 416 : 419780 : delete del->phashBlock;
# 417 : 419780 : delete del;
# 418 : 419780 : }
# 419 : :
# 420 : : // non-final txs in mempool
# 421 : 2 : SetMockTime(::ChainActive().Tip()->GetMedianTimePast()+1);
# 422 : 2 : int flags = LOCKTIME_VERIFY_SEQUENCE|LOCKTIME_MEDIAN_TIME_PAST;
# 423 : : // height map
# 424 : 2 : std::vector<int> prevheights;
# 425 : :
# 426 : : // relative height locked
# 427 : 2 : tx.nVersion = 2;
# 428 : 2 : tx.vin.resize(1);
# 429 : 2 : prevheights.resize(1);
# 430 : 2 : tx.vin[0].prevout.hash = txFirst[0]->GetHash(); // only 1 transaction
# 431 : 2 : tx.vin[0].prevout.n = 0;
# 432 : 2 : tx.vin[0].scriptSig = CScript() << OP_1;
# 433 : 2 : tx.vin[0].nSequence = ::ChainActive().Tip()->nHeight + 1; // txFirst[0] is the 2nd block
# 434 : 2 : prevheights[0] = baseheight + 1;
# 435 : 2 : tx.vout.resize(1);
# 436 : 2 : tx.vout[0].nValue = BLOCKSUBSIDY-HIGHFEE;
# 437 : 2 : tx.vout[0].scriptPubKey = CScript() << OP_1;
# 438 : 2 : tx.nLockTime = 0;
# 439 : 2 : hash = tx.GetHash();
# 440 : 2 : m_node.mempool->addUnchecked(entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
# 441 : 2 : BOOST_CHECK(CheckFinalTx(::ChainActive().Tip(), CTransaction(tx), flags)); // Locktime passes
# 442 : 2 : BOOST_CHECK(!TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks fail
# 443 : 2 : BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, prevheights, CreateBlockIndex(::ChainActive().Tip()->nHeight + 2))); // Sequence locks pass on 2nd block
# 444 : :
# 445 : : // relative time locked
# 446 : 2 : tx.vin[0].prevout.hash = txFirst[1]->GetHash();
# 447 : 2 : tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | (((::ChainActive().Tip()->GetMedianTimePast()+1-::ChainActive()[1]->GetMedianTimePast()) >> CTxIn::SEQUENCE_LOCKTIME_GRANULARITY) + 1); // txFirst[1] is the 3rd block
# 448 : 2 : prevheights[0] = baseheight + 2;
# 449 : 2 : hash = tx.GetHash();
# 450 : 2 : m_node.mempool->addUnchecked(entry.Time(GetTime()).FromTx(tx));
# 451 : 2 : BOOST_CHECK(CheckFinalTx(::ChainActive().Tip(), CTransaction(tx), flags)); // Locktime passes
# 452 : 2 : BOOST_CHECK(!TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks fail
# 453 : :
# 454 [ + + ]: 24 : for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
# 455 : 22 : ::ChainActive().Tip()->GetAncestor(::ChainActive().Tip()->nHeight - i)->nTime += 512; //Trick the MedianTimePast
# 456 : 2 : BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, prevheights, CreateBlockIndex(::ChainActive().Tip()->nHeight + 1))); // Sequence locks pass 512 seconds later
# 457 [ + + ]: 24 : for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
# 458 : 22 : ::ChainActive().Tip()->GetAncestor(::ChainActive().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::SEQUENCE_FINAL - 1;
# 463 : 2 : prevheights[0] = baseheight + 3;
# 464 : 2 : tx.nLockTime = ::ChainActive().Tip()->nHeight + 1;
# 465 : 2 : hash = tx.GetHash();
# 466 : 2 : m_node.mempool->addUnchecked(entry.Time(GetTime()).FromTx(tx));
# 467 : 2 : BOOST_CHECK(!CheckFinalTx(::ChainActive().Tip(), CTransaction(tx), flags)); // Locktime fails
# 468 : 2 : BOOST_CHECK(TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks pass
# 469 : 2 : BOOST_CHECK(IsFinalTx(CTransaction(tx), ::ChainActive().Tip()->nHeight + 2, ::ChainActive().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 = ::ChainActive().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(!CheckFinalTx(::ChainActive().Tip(), CTransaction(tx), flags)); // Locktime fails
# 479 : 2 : BOOST_CHECK(TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks pass
# 480 : 2 : BOOST_CHECK(IsFinalTx(CTransaction(tx), ::ChainActive().Tip()->nHeight + 2, ::ChainActive().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] = ::ChainActive().Tip()->nHeight + 1;
# 485 : 2 : tx.nLockTime = 0;
# 486 : 2 : tx.vin[0].nSequence = 0;
# 487 : 2 : BOOST_CHECK(CheckFinalTx(::ChainActive().Tip(), CTransaction(tx), flags)); // Locktime passes
# 488 : 2 : BOOST_CHECK(TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks pass
# 489 : 2 : tx.vin[0].nSequence = 1;
# 490 : 2 : BOOST_CHECK(!TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks fail
# 491 : 2 : tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG;
# 492 : 2 : BOOST_CHECK(TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks pass
# 493 : 2 : tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | 1;
# 494 : 2 : BOOST_CHECK(!TestSequenceLocks(CTransaction(tx), flags)); // 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 : ::ChainActive().Tip()->GetAncestor(::ChainActive().Tip()->nHeight - i)->nTime += 512; //Trick the MedianTimePast
# 506 : 2 : ::ChainActive().Tip()->nHeight++;
# 507 : 2 : SetMockTime(::ChainActive().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 : ::ChainActive().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()
|