Bitcoin Core 22.99.0
P2P Digital Currency
miner_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2021 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>
8#include <consensus/merkle.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#include <versionbits.h>
20
22
23#include <memory>
24
25#include <boost/test/unit_test.hpp>
26
27namespace miner_tests {
29 void TestPackageSelection(const CChainParams& chainparams, const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_node.mempool->cs);
31 {
32 CCoinsViewMemPool view_mempool(&m_node.chainman->ActiveChainstate().CoinsTip(), *m_node.mempool);
33 return CheckSequenceLocks(m_node.chainman->ActiveChain().Tip(), view_mempool, tx, flags);
34 }
36};
37} // namespace miner_tests
38
39BOOST_FIXTURE_TEST_SUITE(miner_tests, MinerTestingSetup)
40
42
43BlockAssembler MinerTestingSetup::AssemblerForTest(const CChainParams& params)
44{
46
49 return BlockAssembler(m_node.chainman->ActiveChainstate(), *m_node.mempool, params, options);
50}
51
52constexpr static struct {
53 unsigned char extranonce;
54 unsigned int nonce;
55} BLOCKINFO[]{{8, 582909131}, {0, 971462344}, {2, 1169481553}, {6, 66147495}, {7, 427785981}, {8, 80538907},
56 {8, 207348013}, {2, 1951240923}, {4, 215054351}, {1, 491520534}, {8, 1282281282}, {4, 639565734},
57 {3, 248274685}, {8, 1160085976}, {6, 396349768}, {5, 393780549}, {5, 1096899528}, {4, 965381630},
58 {0, 728758712}, {5, 318638310}, {3, 164591898}, {2, 274234550}, {2, 254411237}, {7, 561761812},
59 {2, 268342573}, {0, 402816691}, {1, 221006382}, {6, 538872455}, {7, 393315655}, {4, 814555937},
60 {7, 504879194}, {6, 467769648}, {3, 925972193}, {2, 200581872}, {3, 168915404}, {8, 430446262},
61 {5, 773507406}, {3, 1195366164}, {0, 433361157}, {3, 297051771}, {0, 558856551}, {2, 501614039},
62 {3, 528488272}, {2, 473587734}, {8, 230125274}, {2, 494084400}, {4, 357314010}, {8, 60361686},
63 {7, 640624687}, {3, 480441695}, {8, 1424447925}, {4, 752745419}, {1, 288532283}, {6, 669170574},
64 {5, 1900907591}, {3, 555326037}, {3, 1121014051}, {0, 545835650}, {8, 189196651}, {5, 252371575},
65 {0, 199163095}, {6, 558895874}, {6, 1656839784}, {6, 815175452}, {6, 718677851}, {5, 544000334},
66 {0, 340113484}, {6, 850744437}, {4, 496721063}, {8, 524715182}, {6, 574361898}, {6, 1642305743},
67 {6, 355110149}, {5, 1647379658}, {8, 1103005356}, {7, 556460625}, {3, 1139533992}, {5, 304736030},
68 {2, 361539446}, {2, 143720360}, {6, 201939025}, {7, 423141476}, {4, 574633709}, {3, 1412254823},
69 {4, 873254135}, {0, 341817335}, {6, 53501687}, {3, 179755410}, {5, 172209688}, {8, 516810279},
70 {4, 1228391489}, {8, 325372589}, {6, 550367589}, {0, 876291812}, {7, 412454120}, {7, 717202854},
71 {2, 222677843}, {6, 251778867}, {7, 842004420}, {7, 194762829}, {4, 96668841}, {1, 925485796},
72 {0, 792342903}, {6, 678455063}, {6, 773251385}, {5, 186617471}, {6, 883189502}, {7, 396077336},
73 {8, 254702874}, {0, 455592851}};
74
76{
77 CBlockIndex index;
78 index.nHeight = nHeight;
79 index.pprev = active_chain_tip;
80 return index;
81}
82
83// Test suite for ancestor feerate transaction selection.
84// Implemented as an additional function, rather than a separate test case,
85// to allow reusing the blockchain created in CreateNewBlock_validity.
86void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst)
87{
88 // Test the ancestor feerate transaction selection.
90
91 // Test that a medium fee transaction will be selected after a higher fee
92 // rate package with a low fee rate parent.
94 tx.vin.resize(1);
95 tx.vin[0].scriptSig = CScript() << OP_1;
96 tx.vin[0].prevout.hash = txFirst[0]->GetHash();
97 tx.vin[0].prevout.n = 0;
98 tx.vout.resize(1);
99 tx.vout[0].nValue = 5000000000LL - 1000;
100 // This tx has a low fee: 1000 satoshis
101 uint256 hashParentTx = tx.GetHash(); // save this txid for later use
102 m_node.mempool->addUnchecked(entry.Fee(1000).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
103
104 // This tx has a medium fee: 10000 satoshis
105 tx.vin[0].prevout.hash = txFirst[1]->GetHash();
106 tx.vout[0].nValue = 5000000000LL - 10000;
107 uint256 hashMediumFeeTx = tx.GetHash();
108 m_node.mempool->addUnchecked(entry.Fee(10000).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
109
110 // This tx has a high fee, but depends on the first transaction
111 tx.vin[0].prevout.hash = hashParentTx;
112 tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 50k satoshi fee
113 uint256 hashHighFeeTx = tx.GetHash();
114 m_node.mempool->addUnchecked(entry.Fee(50000).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
115
116 std::unique_ptr<CBlockTemplate> pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
117 BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 4U);
118 BOOST_CHECK(pblocktemplate->block.vtx[1]->GetHash() == hashParentTx);
119 BOOST_CHECK(pblocktemplate->block.vtx[2]->GetHash() == hashHighFeeTx);
120 BOOST_CHECK(pblocktemplate->block.vtx[3]->GetHash() == hashMediumFeeTx);
121
122 // Test that a package below the block min tx fee doesn't get included
123 tx.vin[0].prevout.hash = hashHighFeeTx;
124 tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 0 fee
125 uint256 hashFreeTx = tx.GetHash();
126 m_node.mempool->addUnchecked(entry.Fee(0).FromTx(tx));
127 size_t freeTxSize = ::GetSerializeSize(tx, PROTOCOL_VERSION);
128
129 // Calculate a fee on child transaction that will put the package just
130 // below the block min tx fee (assuming 1 child tx of the same size).
131 CAmount feeToUse = blockMinFeeRate.GetFee(2*freeTxSize) - 1;
132
133 tx.vin[0].prevout.hash = hashFreeTx;
134 tx.vout[0].nValue = 5000000000LL - 1000 - 50000 - feeToUse;
135 uint256 hashLowFeeTx = tx.GetHash();
136 m_node.mempool->addUnchecked(entry.Fee(feeToUse).FromTx(tx));
137 pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
138 // Verify that the free tx and the low fee tx didn't get selected
139 for (size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
140 BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashFreeTx);
141 BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashLowFeeTx);
142 }
143
144 // Test that packages above the min relay fee do get included, even if one
145 // of the transactions is below the min relay fee
146 // Remove the low fee transaction and replace with a higher fee transaction
148 tx.vout[0].nValue -= 2; // Now we should be just over the min relay fee
149 hashLowFeeTx = tx.GetHash();
150 m_node.mempool->addUnchecked(entry.Fee(feeToUse+2).FromTx(tx));
151 pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
152 BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 6U);
153 BOOST_CHECK(pblocktemplate->block.vtx[4]->GetHash() == hashFreeTx);
154 BOOST_CHECK(pblocktemplate->block.vtx[5]->GetHash() == hashLowFeeTx);
155
156 // Test that transaction selection properly updates ancestor fee
157 // calculations as ancestor transactions get included in a block.
158 // Add a 0-fee transaction that has 2 outputs.
159 tx.vin[0].prevout.hash = txFirst[2]->GetHash();
160 tx.vout.resize(2);
161 tx.vout[0].nValue = 5000000000LL - 100000000;
162 tx.vout[1].nValue = 100000000; // 1BTC output
163 uint256 hashFreeTx2 = tx.GetHash();
164 m_node.mempool->addUnchecked(entry.Fee(0).SpendsCoinbase(true).FromTx(tx));
165
166 // This tx can't be mined by itself
167 tx.vin[0].prevout.hash = hashFreeTx2;
168 tx.vout.resize(1);
169 feeToUse = blockMinFeeRate.GetFee(freeTxSize);
170 tx.vout[0].nValue = 5000000000LL - 100000000 - feeToUse;
171 uint256 hashLowFeeTx2 = tx.GetHash();
172 m_node.mempool->addUnchecked(entry.Fee(feeToUse).SpendsCoinbase(false).FromTx(tx));
173 pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
174
175 // Verify that this tx isn't selected.
176 for (size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
177 BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashFreeTx2);
178 BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashLowFeeTx2);
179 }
180
181 // This tx will be mineable, and should cause hashLowFeeTx2 to be selected
182 // as well.
183 tx.vin[0].prevout.n = 1;
184 tx.vout[0].nValue = 100000000 - 10000; // 10k satoshi fee
185 m_node.mempool->addUnchecked(entry.Fee(10000).FromTx(tx));
186 pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
187 BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 9U);
188 BOOST_CHECK(pblocktemplate->block.vtx[8]->GetHash() == hashLowFeeTx2);
189}
190
191// NOTE: These tests rely on CreateNewBlock doing its own self-validation!
192BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
193{
194 // Note that by default, these tests run with size accounting enabled.
195 const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
196 const CChainParams& chainparams = *chainParams;
197 CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
198 std::unique_ptr<CBlockTemplate> pblocktemplate;
200 CScript script;
201 uint256 hash;
203 entry.nFee = 11;
204 entry.nHeight = 11;
205
206 fCheckpointsEnabled = false;
207
208 // Simple block creation, nothing special yet:
209 BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
210
211 // We can't make transactions until we have inputs
212 // Therefore, load 110 blocks :)
213 static_assert(std::size(BLOCKINFO) == 110, "Should have 110 blocks to import");
214 int baseheight = 0;
215 std::vector<CTransactionRef> txFirst;
216 for (const auto& bi : BLOCKINFO) {
217 CBlock *pblock = &pblocktemplate->block; // pointer for convenience
218 {
219 LOCK(cs_main);
221 pblock->nTime = m_node.chainman->ActiveChain().Tip()->GetMedianTimePast()+1;
222 CMutableTransaction txCoinbase(*pblock->vtx[0]);
223 txCoinbase.nVersion = 1;
224 txCoinbase.vin[0].scriptSig = CScript{} << (m_node.chainman->ActiveChain().Height() + 1) << bi.extranonce;
225 txCoinbase.vout.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this)
226 txCoinbase.vout[0].scriptPubKey = CScript();
227 pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
228 if (txFirst.size() == 0)
229 baseheight = m_node.chainman->ActiveChain().Height();
230 if (txFirst.size() < 4)
231 txFirst.push_back(pblock->vtx[0]);
232 pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
233 pblock->nNonce = bi.nonce;
234 }
235 std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(*pblock);
236 BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(chainparams, shared_pblock, true, nullptr));
237 pblock->hashPrevBlock = pblock->GetHash();
238 }
239
240 LOCK(cs_main);
241 LOCK(m_node.mempool->cs);
242
243 // Just to make sure we can still make simple blocks
244 BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
245
246 const CAmount BLOCKSUBSIDY = 50*COIN;
247 const CAmount LOWFEE = CENT;
248 const CAmount HIGHFEE = COIN;
249 const CAmount HIGHERFEE = 4*COIN;
250
251 // block sigops > limit: 1000 CHECKMULTISIG + 1
252 tx.vin.resize(1);
253 // NOTE: OP_NOP is used to force 20 SigOps for the CHECKMULTISIG
254 tx.vin[0].scriptSig = CScript() << OP_0 << OP_0 << OP_0 << OP_NOP << OP_CHECKMULTISIG << OP_1;
255 tx.vin[0].prevout.hash = txFirst[0]->GetHash();
256 tx.vin[0].prevout.n = 0;
257 tx.vout.resize(1);
258 tx.vout[0].nValue = BLOCKSUBSIDY;
259 for (unsigned int i = 0; i < 1001; ++i)
260 {
261 tx.vout[0].nValue -= LOWFEE;
262 hash = tx.GetHash();
263 bool spendsCoinbase = i == 0; // only first tx spends coinbase
264 // If we don't set the # of sig ops in the CTxMemPoolEntry, template creation fails
265 m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(spendsCoinbase).FromTx(tx));
266 tx.vin[0].prevout.hash = hash;
267 }
268
269 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-blk-sigops"));
270 m_node.mempool->clear();
271
272 tx.vin[0].prevout.hash = txFirst[0]->GetHash();
273 tx.vout[0].nValue = BLOCKSUBSIDY;
274 for (unsigned int i = 0; i < 1001; ++i)
275 {
276 tx.vout[0].nValue -= LOWFEE;
277 hash = tx.GetHash();
278 bool spendsCoinbase = i == 0; // only first tx spends coinbase
279 // If we do set the # of sig ops in the CTxMemPoolEntry, template creation passes
280 m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(spendsCoinbase).SigOpsCost(80).FromTx(tx));
281 tx.vin[0].prevout.hash = hash;
282 }
283 BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
284 m_node.mempool->clear();
285
286 // block size > limit
287 tx.vin[0].scriptSig = CScript();
288 // 18 * (520char + DROP) + OP_1 = 9433 bytes
289 std::vector<unsigned char> vchData(520);
290 for (unsigned int i = 0; i < 18; ++i)
291 tx.vin[0].scriptSig << vchData << OP_DROP;
292 tx.vin[0].scriptSig << OP_1;
293 tx.vin[0].prevout.hash = txFirst[0]->GetHash();
294 tx.vout[0].nValue = BLOCKSUBSIDY;
295 for (unsigned int i = 0; i < 128; ++i)
296 {
297 tx.vout[0].nValue -= LOWFEE;
298 hash = tx.GetHash();
299 bool spendsCoinbase = i == 0; // only first tx spends coinbase
300 m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(spendsCoinbase).FromTx(tx));
301 tx.vin[0].prevout.hash = hash;
302 }
303 BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
304 m_node.mempool->clear();
305
306 // orphan in *m_node.mempool, template creation fails
307 hash = tx.GetHash();
308 m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).FromTx(tx));
309 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
310 m_node.mempool->clear();
311
312 // child with higher feerate than parent
313 tx.vin[0].scriptSig = CScript() << OP_1;
314 tx.vin[0].prevout.hash = txFirst[1]->GetHash();
315 tx.vout[0].nValue = BLOCKSUBSIDY-HIGHFEE;
316 hash = tx.GetHash();
317 m_node.mempool->addUnchecked(entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
318 tx.vin[0].prevout.hash = hash;
319 tx.vin.resize(2);
320 tx.vin[1].scriptSig = CScript() << OP_1;
321 tx.vin[1].prevout.hash = txFirst[0]->GetHash();
322 tx.vin[1].prevout.n = 0;
323 tx.vout[0].nValue = tx.vout[0].nValue+BLOCKSUBSIDY-HIGHERFEE; //First txn output + fresh coinbase - new txn fee
324 hash = tx.GetHash();
325 m_node.mempool->addUnchecked(entry.Fee(HIGHERFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
326 BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
327 m_node.mempool->clear();
328
329 // coinbase in *m_node.mempool, template creation fails
330 tx.vin.resize(1);
331 tx.vin[0].prevout.SetNull();
332 tx.vin[0].scriptSig = CScript() << OP_0 << OP_1;
333 tx.vout[0].nValue = 0;
334 hash = tx.GetHash();
335 // give it a fee so it'll get mined
336 m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
337 // Should throw bad-cb-multiple
338 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-cb-multiple"));
339 m_node.mempool->clear();
340
341 // double spend txn pair in *m_node.mempool, template creation fails
342 tx.vin[0].prevout.hash = txFirst[0]->GetHash();
343 tx.vin[0].scriptSig = CScript() << OP_1;
344 tx.vout[0].nValue = BLOCKSUBSIDY-HIGHFEE;
345 tx.vout[0].scriptPubKey = CScript() << OP_1;
346 hash = tx.GetHash();
347 m_node.mempool->addUnchecked(entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
348 tx.vout[0].scriptPubKey = CScript() << OP_2;
349 hash = tx.GetHash();
350 m_node.mempool->addUnchecked(entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
351 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
352 m_node.mempool->clear();
353
354 // subsidy changing
355 int nHeight = m_node.chainman->ActiveChain().Height();
356 // Create an actual 209999-long block chain (without valid blocks).
357 while (m_node.chainman->ActiveChain().Tip()->nHeight < 209999) {
358 CBlockIndex* prev = m_node.chainman->ActiveChain().Tip();
359 CBlockIndex* next = new CBlockIndex();
360 next->phashBlock = new uint256(InsecureRand256());
361 m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(next->GetBlockHash());
362 next->pprev = prev;
363 next->nHeight = prev->nHeight + 1;
364 next->BuildSkip();
365 m_node.chainman->ActiveChain().SetTip(next);
366 }
367 BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
368 // Extend to a 210000-long block chain.
369 while (m_node.chainman->ActiveChain().Tip()->nHeight < 210000) {
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
381 // invalid p2sh txn in *m_node.mempool, template creation fails
382 tx.vin[0].prevout.hash = txFirst[0]->GetHash();
383 tx.vin[0].prevout.n = 0;
384 tx.vin[0].scriptSig = CScript() << OP_1;
385 tx.vout[0].nValue = BLOCKSUBSIDY-LOWFEE;
386 script = CScript() << OP_0;
387 tx.vout[0].scriptPubKey = GetScriptForDestination(ScriptHash(script));
388 hash = tx.GetHash();
389 m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
390 tx.vin[0].prevout.hash = hash;
391 tx.vin[0].scriptSig = CScript() << std::vector<unsigned char>(script.begin(), script.end());
392 tx.vout[0].nValue -= LOWFEE;
393 hash = tx.GetHash();
394 m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
395 // Should throw block-validation-failed
396 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("block-validation-failed"));
397 m_node.mempool->clear();
398
399 // Delete the dummy blocks again.
400 while (m_node.chainman->ActiveChain().Tip()->nHeight > nHeight) {
401 CBlockIndex* del = m_node.chainman->ActiveChain().Tip();
402 m_node.chainman->ActiveChain().SetTip(del->pprev);
403 m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(del->pprev->GetBlockHash());
404 delete del->phashBlock;
405 delete del;
406 }
407
408 // non-final txs in mempool
409 SetMockTime(m_node.chainman->ActiveChain().Tip()->GetMedianTimePast()+1);
411 // height map
412 std::vector<int> prevheights;
413
414 // relative height locked
415 tx.nVersion = 2;
416 tx.vin.resize(1);
417 prevheights.resize(1);
418 tx.vin[0].prevout.hash = txFirst[0]->GetHash(); // only 1 transaction
419 tx.vin[0].prevout.n = 0;
420 tx.vin[0].scriptSig = CScript() << OP_1;
421 tx.vin[0].nSequence = m_node.chainman->ActiveChain().Tip()->nHeight + 1; // txFirst[0] is the 2nd block
422 prevheights[0] = baseheight + 1;
423 tx.vout.resize(1);
424 tx.vout[0].nValue = BLOCKSUBSIDY-HIGHFEE;
425 tx.vout[0].scriptPubKey = CScript() << OP_1;
426 tx.nLockTime = 0;
427 hash = tx.GetHash();
428 m_node.mempool->addUnchecked(entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
429 BOOST_CHECK(CheckFinalTx(m_node.chainman->ActiveChain().Tip(), CTransaction(tx), flags)); // Locktime passes
430 BOOST_CHECK(!TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks fail
431
432 {
433 CBlockIndex* active_chain_tip = m_node.chainman->ActiveChain().Tip();
434 BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, prevheights, CreateBlockIndex(active_chain_tip->nHeight + 2, active_chain_tip))); // Sequence locks pass on 2nd block
435 }
436
437 // relative time locked
438 tx.vin[0].prevout.hash = txFirst[1]->GetHash();
439 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
440 prevheights[0] = baseheight + 2;
441 hash = tx.GetHash();
442 m_node.mempool->addUnchecked(entry.Time(GetTime()).FromTx(tx));
443 BOOST_CHECK(CheckFinalTx(m_node.chainman->ActiveChain().Tip(), CTransaction(tx), flags)); // Locktime passes
444 BOOST_CHECK(!TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks fail
445
446 for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
447 m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i)->nTime += 512; //Trick the MedianTimePast
448
449 {
450 CBlockIndex* active_chain_tip = m_node.chainman->ActiveChain().Tip();
451 BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, prevheights, CreateBlockIndex(active_chain_tip->nHeight + 1, active_chain_tip))); // Sequence locks pass 512 seconds later
452 }
453
454 for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
455 m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().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 = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
462 hash = tx.GetHash();
463 m_node.mempool->addUnchecked(entry.Time(GetTime()).FromTx(tx));
464 BOOST_CHECK(!CheckFinalTx(m_node.chainman->ActiveChain().Tip(), CTransaction(tx), flags)); // Locktime fails
465 BOOST_CHECK(TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks pass
466 BOOST_CHECK(IsFinalTx(CTransaction(tx), m_node.chainman->ActiveChain().Tip()->nHeight + 2, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast())); // Locktime passes on 2nd block
467
468 // absolute time locked
469 tx.vin[0].prevout.hash = txFirst[3]->GetHash();
470 tx.nLockTime = m_node.chainman->ActiveChain().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(m_node.chainman->ActiveChain().Tip(), CTransaction(tx), flags)); // Locktime fails
476 BOOST_CHECK(TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks pass
477 BOOST_CHECK(IsFinalTx(CTransaction(tx), m_node.chainman->ActiveChain().Tip()->nHeight + 2, m_node.chainman->ActiveChain().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] = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
482 tx.nLockTime = 0;
483 tx.vin[0].nSequence = 0;
484 BOOST_CHECK(CheckFinalTx(m_node.chainman->ActiveChain().Tip(), 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 m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i)->nTime += 512; //Trick the MedianTimePast
503 m_node.chainman->ActiveChain().Tip()->nHeight++;
504 SetMockTime(m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1);
505
506 BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
507 BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 5U);
508
509 m_node.chainman->ActiveChain().Tip()->nHeight--;
510 SetMockTime(0);
511 m_node.mempool->clear();
512
513 TestPackageSelection(chainparams, scriptPubKey, txFirst);
514
515 fCheckpointsEnabled = true;
516}
517
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
NodeContext m_node
Definition: bitcoin-gui.cpp:36
int flags
Definition: bitcoin-tx.cpp:525
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:118
std::unique_ptr< const CChainParams > CreateChainParams(const ArgsManager &args, const std::string &chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
#define Assert(val)
Identity function.
Definition: check.h:57
Generate a new block, without valid proof-of-work.
Definition: miner.h:127
static const std::string MAIN
Chain name strings.
uint32_t nNonce
Definition: block.h:29
uint32_t nTime
Definition: block.h:27
int32_t nVersion
Definition: block.h:24
uint256 hashPrevBlock
Definition: block.h:25
uint256 hashMerkleRoot
Definition: block.h:26
uint256 GetHash() const
Definition: block.cpp:11
Definition: block.h:63
std::vector< CTransactionRef > vtx
Definition: block.h:66
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:146
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:152
void BuildSkip()
Build the skiplist pointer for this entry.
Definition: chain.cpp:116
uint256 GetBlockHash() const
Definition: chain.h:254
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:158
static constexpr int nMedianTimeSpan
Definition: chain.h:278
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
Definition: chain.h:149
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:70
CCoinsView that brings transactions from a mempool into view.
Definition: txmempool.h:852
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:30
CAmount GetFee(uint32_t num_bytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:23
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:260
static const uint32_t SEQUENCE_FINAL
Definition: transaction.h:75
static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG
Definition: transaction.h:85
static const int SEQUENCE_LOCKTIME_GRANULARITY
Definition: transaction.h:98
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: setup_common.h:218
iterator begin()
Definition: prevector.h:290
iterator end()
Definition: prevector.h:292
256-bit opaque blob.
Definition: uint256.h:124
static constexpr unsigned int LOCKTIME_VERIFY_SEQUENCE
Flags for nSequence and nLockTime locks.
Definition: consensus.h:28
static constexpr unsigned int LOCKTIME_MEDIAN_TIME_PAST
Use GetMedianTimePast() instead of nTime for end point timestamp.
Definition: consensus.h:30
static const unsigned int MAX_BLOCK_WEIGHT
The maximum allowed weight for a block, see BIP 141 (network rule)
Definition: consensus.h:15
BOOST_AUTO_TEST_SUITE_END()
unsigned int nHeight
bool spendsCoinbase
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:65
BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
static CFeeRate blockMinFeeRate
Definition: miner_tests.cpp:41
static CBlockIndex CreateBlockIndex(int nHeight, CBlockIndex *active_chain_tip) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: miner_tests.cpp:75
unsigned char extranonce
Definition: miner_tests.cpp:53
unsigned int nonce
Definition: miner_tests.cpp:54
constexpr static struct @15 BLOCKINFO[]
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
static const unsigned int DEFAULT_BLOCK_MIN_TX_FEE
Default for -blockmintxfee, which sets the minimum feerate for a transaction in blocks created by min...
Definition: policy.h:22
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:387
@ OP_2
Definition: script.h:78
@ OP_CHECKMULTISIG
Definition: script.h:185
@ OP_CHECKSIG
Definition: script.h:183
@ OP_NOP
Definition: script.h:95
@ OP_1
Definition: script.h:76
@ OP_DROP
Definition: script.h:117
@ OP_0
Definition: script.h:69
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1080
static constexpr CAmount CENT
Definition: setup_common.h:71
static uint256 InsecureRand256()
Definition: setup_common.h:66
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:310
std::vector< unsigned char > ParseHex(const char *psz)
NodeContext m_node
Definition: setup_common.h:78
CFeeRate blockMinFeeRate
Definition: miner.h:155
size_t nBlockMaxWeight
Definition: miner.h:154
A mutable version of CTransaction.
Definition: transaction.h:345
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:63
std::vector< CTxOut > vout
Definition: transaction.h:347
std::vector< CTxIn > vin
Definition: transaction.h:346
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:47
ArgsManager * args
Definition: context.h:49
std::unique_ptr< CTxMemPool > mempool
Definition: context.h:44
Definition: setup_common.h:183
CAmount nFee
Definition: setup_common.h:185
TestMemPoolEntryHelper & Time(int64_t _time)
Definition: setup_common.h:201
CTxMemPoolEntry FromTx(const CMutableTransaction &tx) const
TestMemPoolEntryHelper & SpendsCoinbase(bool _flag)
Definition: setup_common.h:203
unsigned int nHeight
Definition: setup_common.h:187
TestMemPoolEntryHelper & Fee(CAmount _fee)
Definition: setup_common.h:200
Testing setup that configures a complete environment.
Definition: setup_common.h:99
void TestPackageSelection(const CChainParams &chainparams, const CScript &scriptPubKey, const std::vector< CTransactionRef > &txFirst) EXCLUSIVE_LOCKS_REQUIRED(bool TestSequenceLocks(const CTransaction &tx, int flags) EXCLUSIVE_LOCKS_REQUIRED(
Definition: miner_tests.cpp:30
BlockAssembler AssemblerForTest(const CChainParams &params)
Definition: miner_tests.cpp:43
#define LOCK(cs)
Definition: sync.h:226
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:101
int64_t GetTime()
DEPRECATED Use either GetTimeSeconds (not mockable) or GetTime<T> (mockable)
Definition: time.cpp:26
bool SequenceLocks(const CTransaction &tx, int flags, std::vector< int > &prevHeights, const CBlockIndex &block)
Check if transaction is final per BIP 68 sequence numbers and can be included in a block.
Definition: tx_verify.cpp:112
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
Check if transaction is final and can be included in a block with the specified height and time.
Definition: tx_verify.cpp:18
@ REPLACED
Removed for replacement.
bool CheckSequenceLocks(CBlockIndex *tip, const CCoinsView &coins_view, const CTransaction &tx, int flags, LockPoints *lp, bool useExistingLockPoints)
Check if transaction will be BIP68 final in the next block to be created on top of tip.
Definition: validation.cpp:233
bool CheckFinalTx(const CBlockIndex *active_chain_tip, const CTransaction &tx, int flags)
Transaction validation functions.
Definition: validation.cpp:182
bool fCheckpointsEnabled
Definition: validation.cpp:127
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
static const int32_t VERSIONBITS_TOP_BITS
What bits to set in version for versionbits blocks.
Definition: versionbits.h:16