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