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