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