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

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