You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

miner_tests.cpp 23KB

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