Bitcoin Core 30.99.0
P2P Digital Currency
miner_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2022 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 <addresstype.h>
6#include <coins.h>
7#include <common/system.h>
9#include <consensus/merkle.h>
10#include <consensus/tx_verify.h>
11#include <interfaces/mining.h>
12#include <node/miner.h>
13#include <policy/policy.h>
14#include <test/util/random.h>
16#include <test/util/txmempool.h>
17#include <txmempool.h>
18#include <uint256.h>
19#include <util/check.h>
20#include <util/feefrac.h>
21#include <util/strencodings.h>
22#include <util/time.h>
23#include <util/translation.h>
24#include <validation.h>
25#include <versionbits.h>
26#include <pow.h>
27
29
30#include <memory>
31#include <vector>
32
33#include <boost/test/unit_test.hpp>
34
35using namespace util::hex_literals;
39
40namespace miner_tests {
42 void TestPackageSelection(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
43 void TestBasicMining(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst, int baseheight) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
44 void TestPrioritisedMining(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
46 {
47 CCoinsViewMemPool view_mempool{&m_node.chainman->ActiveChainstate().CoinsTip(), tx_mempool};
48 CBlockIndex* tip{m_node.chainman->ActiveChain().Tip()};
49 const std::optional<LockPoints> lock_points{CalculateLockPointsAtTip(tip, view_mempool, tx)};
50 return lock_points.has_value() && CheckSequenceLocksAtTip(tip, *lock_points);
51 }
53 {
54 // Delete the previous mempool to ensure with valgrind that the old
55 // pointer is not accessed, when the new one should be accessed
56 // instead.
57 m_node.mempool.reset();
58 bilingual_str error;
59 m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(m_node), error);
60 Assert(error.empty());
61 return *m_node.mempool;
62 }
63 std::unique_ptr<Mining> MakeMining()
64 {
66 }
67};
68} // namespace miner_tests
69
70BOOST_FIXTURE_TEST_SUITE(miner_tests, MinerTestingSetup)
71
73
74constexpr static struct {
75 unsigned int extranonce;
76 unsigned int nonce;
77} BLOCKINFO[]{{0, 3552706918}, {500, 37506755}, {1000, 948987788}, {400, 524762339}, {800, 258510074}, {300, 102309278},
78 {1300, 54365202}, {600, 1107740426}, {1000, 203094491}, {900, 391178848}, {800, 381177271}, {600, 87188412},
79 {0, 66522866}, {800, 874942736}, {1000, 89200838}, {400, 312638088}, {400, 66263693}, {500, 924648304},
80 {400, 369913599}, {500, 47630099}, {500, 115045364}, {100, 277026602}, {1100, 809621409}, {700, 155345322},
81 {800, 943579953}, {400, 28200730}, {900, 77200495}, {0, 105935488}, {400, 698721821}, {500, 111098863},
82 {1300, 445389594}, {500, 621849894}, {1400, 56010046}, {1100, 370669776}, {1200, 380301940}, {1200, 110654905},
83 {400, 213771024}, {1500, 120014726}, {1200, 835019014}, {1500, 624817237}, {900, 1404297}, {400, 189414558},
84 {400, 293178348}, {1100, 15393789}, {600, 396764180}, {800, 1387046371}, {800, 199368303}, {700, 111496662},
85 {100, 129759616}, {200, 536577982}, {500, 125881300}, {500, 101053391}, {1200, 471590548}, {900, 86957729},
86 {1200, 179604104}, {600, 68658642}, {1000, 203295701}, {500, 139615361}, {900, 233693412}, {300, 153225163},
87 {0, 27616254}, {1200, 9856191}, {100, 220392722}, {200, 66257599}, {1100, 145489641}, {1300, 37859442},
88 {400, 5816075}, {1200, 215752117}, {1400, 32361482}, {1400, 6529223}, {500, 143332977}, {800, 878392},
89 {700, 159290408}, {400, 123197595}, {700, 43988693}, {300, 304224916}, {700, 214771621}, {1100, 274148273},
90 {400, 285632418}, {1100, 923451065}, {600, 12818092}, {1200, 736282054}, {1000, 246683167}, {600, 92950402},
91 {1400, 29223405}, {1000, 841327192}, {700, 174301283}, {1400, 214009854}, {1000, 6989517}, {1200, 278226956},
92 {700, 540219613}, {400, 93663104}, {1100, 152345635}, {1500, 464194499}, {1300, 333850111}, {600, 258311263},
93 {600, 90173162}, {1000, 33590797}, {1500, 332866027}, {100, 204704427}, {1000, 463153545}, {800, 303244785},
94 {600, 88096214}, {0, 137477892}, {1200, 195514506}, {300, 704114595}, {900, 292087369}, {1400, 758684870},
95 {1300, 163493028}, {1200, 53151293}};
96
97static std::unique_ptr<CBlockIndex> CreateBlockIndex(int nHeight, CBlockIndex* active_chain_tip) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
98{
99 auto index{std::make_unique<CBlockIndex>()};
100 index->nHeight = nHeight;
101 index->pprev = active_chain_tip;
102 return index;
103}
104
105// Test suite for ancestor feerate transaction selection.
106// Implemented as an additional function, rather than a separate test case,
107// to allow reusing the blockchain created in CreateNewBlock_validity.
108void MinerTestingSetup::TestPackageSelection(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst)
109{
110 CTxMemPool& tx_mempool{MakeMempool()};
111 auto mining{MakeMining()};
112 BlockAssembler::Options options;
113 options.coinbase_output_script = scriptPubKey;
114
115 LOCK(tx_mempool.cs);
116 BOOST_CHECK(tx_mempool.size() == 0);
117
118 // Block template should only have a coinbase when there's nothing in the mempool
119 std::unique_ptr<BlockTemplate> block_template = mining->createNewBlock(options);
120 BOOST_REQUIRE(block_template);
121 CBlock block{block_template->getBlock()};
122 BOOST_REQUIRE_EQUAL(block.vtx.size(), 1U);
123
124 // waitNext() on an empty mempool should return nullptr because there is no better template
125 auto should_be_nullptr = block_template->waitNext({.timeout = MillisecondsDouble{0}, .fee_threshold = 1});
126 BOOST_REQUIRE(should_be_nullptr == nullptr);
127
128 // Unless fee_threshold is 0
129 block_template = block_template->waitNext({.timeout = MillisecondsDouble{0}, .fee_threshold = 0});
130 BOOST_REQUIRE(block_template);
131
132 // Test the ancestor feerate transaction selection.
134
135 // Test that a medium fee transaction will be selected after a higher fee
136 // rate package with a low fee rate parent.
138 tx.vin.resize(1);
139 tx.vin[0].scriptSig = CScript() << OP_1;
140 tx.vin[0].prevout.hash = txFirst[0]->GetHash();
141 tx.vin[0].prevout.n = 0;
142 tx.vout.resize(1);
143 tx.vout[0].nValue = 5000000000LL - 1000;
144 // This tx has a low fee: 1000 satoshis
145 Txid hashParentTx = tx.GetHash(); // save this txid for later use
146 const auto parent_tx{entry.Fee(1000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx)};
147 AddToMempool(tx_mempool, parent_tx);
148
149 // This tx has a medium fee: 10000 satoshis
150 tx.vin[0].prevout.hash = txFirst[1]->GetHash();
151 tx.vout[0].nValue = 5000000000LL - 10000;
152 Txid hashMediumFeeTx = tx.GetHash();
153 const auto medium_fee_tx{entry.Fee(10000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx)};
154 AddToMempool(tx_mempool, medium_fee_tx);
155
156 // This tx has a high fee, but depends on the first transaction
157 tx.vin[0].prevout.hash = hashParentTx;
158 tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 50k satoshi fee
159 Txid hashHighFeeTx = tx.GetHash();
160 const auto high_fee_tx{entry.Fee(50000).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx)};
161 AddToMempool(tx_mempool, high_fee_tx);
162
163 block_template = mining->createNewBlock(options);
164 BOOST_REQUIRE(block_template);
165 block = block_template->getBlock();
166 BOOST_REQUIRE_EQUAL(block.vtx.size(), 4U);
167 BOOST_CHECK(block.vtx[1]->GetHash() == hashParentTx);
168 BOOST_CHECK(block.vtx[2]->GetHash() == hashHighFeeTx);
169 BOOST_CHECK(block.vtx[3]->GetHash() == hashMediumFeeTx);
170
171 // Test the inclusion of package feerates in the block template and ensure they are sequential.
172 const auto block_package_feerates = BlockAssembler{m_node.chainman->ActiveChainstate(), &tx_mempool, options}.CreateNewBlock()->m_package_feerates;
173 BOOST_CHECK(block_package_feerates.size() == 2);
174
175 // parent_tx and high_fee_tx are added to the block as a package.
176 const auto combined_txs_fee = parent_tx.GetFee() + high_fee_tx.GetFee();
177 const auto combined_txs_size = parent_tx.GetTxSize() + high_fee_tx.GetTxSize();
178 FeeFrac package_feefrac{combined_txs_fee, combined_txs_size};
179 // The package should be added first.
180 BOOST_CHECK(block_package_feerates[0] == package_feefrac);
181
182 // The medium_fee_tx should be added next.
183 FeeFrac medium_tx_feefrac{medium_fee_tx.GetFee(), medium_fee_tx.GetTxSize()};
184 BOOST_CHECK(block_package_feerates[1] == medium_tx_feefrac);
185
186 // Test that a package below the block min tx fee doesn't get included
187 tx.vin[0].prevout.hash = hashHighFeeTx;
188 tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 0 fee
189 Txid hashFreeTx = tx.GetHash();
190 AddToMempool(tx_mempool, entry.Fee(0).FromTx(tx));
191 size_t freeTxSize = ::GetSerializeSize(TX_WITH_WITNESS(tx));
192
193 // Calculate a fee on child transaction that will put the package just
194 // below the block min tx fee (assuming 1 child tx of the same size).
195 CAmount feeToUse = blockMinFeeRate.GetFee(2*freeTxSize) - 1;
196
197 tx.vin[0].prevout.hash = hashFreeTx;
198 tx.vout[0].nValue = 5000000000LL - 1000 - 50000 - feeToUse;
199 Txid hashLowFeeTx = tx.GetHash();
200 AddToMempool(tx_mempool, entry.Fee(feeToUse).FromTx(tx));
201
202 // waitNext() should return nullptr because there is no better template
203 should_be_nullptr = block_template->waitNext({.timeout = MillisecondsDouble{0}, .fee_threshold = 1});
204 BOOST_REQUIRE(should_be_nullptr == nullptr);
205
206 block = block_template->getBlock();
207 // Verify that the free tx and the low fee tx didn't get selected
208 for (size_t i=0; i<block.vtx.size(); ++i) {
209 BOOST_CHECK(block.vtx[i]->GetHash() != hashFreeTx);
210 BOOST_CHECK(block.vtx[i]->GetHash() != hashLowFeeTx);
211 }
212
213 // Test that packages above the min relay fee do get included, even if one
214 // of the transactions is below the min relay fee
215 // Remove the low fee transaction and replace with a higher fee transaction
216 tx_mempool.removeRecursive(CTransaction(tx), MemPoolRemovalReason::REPLACED);
217 tx.vout[0].nValue -= 2; // Now we should be just over the min relay fee
218 hashLowFeeTx = tx.GetHash();
219 AddToMempool(tx_mempool, entry.Fee(feeToUse + 2).FromTx(tx));
220
221 // waitNext() should return if fees for the new template are at least 1 sat up
222 block_template = block_template->waitNext({.fee_threshold = 1});
223 BOOST_REQUIRE(block_template);
224 block = block_template->getBlock();
225 BOOST_REQUIRE_EQUAL(block.vtx.size(), 6U);
226 BOOST_CHECK(block.vtx[4]->GetHash() == hashFreeTx);
227 BOOST_CHECK(block.vtx[5]->GetHash() == hashLowFeeTx);
228
229 // Test that transaction selection properly updates ancestor fee
230 // calculations as ancestor transactions get included in a block.
231 // Add a 0-fee transaction that has 2 outputs.
232 tx.vin[0].prevout.hash = txFirst[2]->GetHash();
233 tx.vout.resize(2);
234 tx.vout[0].nValue = 5000000000LL - 100000000;
235 tx.vout[1].nValue = 100000000; // 1BTC output
236 // Increase size to avoid rounding errors: when the feerate is extremely small (i.e. 1sat/kvB), evaluating the fee
237 // at smaller sizes gives us rounded values that are equal to each other, which means we incorrectly include
238 // hashFreeTx2 + hashLowFeeTx2.
239 BulkTransaction(tx, 4000);
240 Txid hashFreeTx2 = tx.GetHash();
241 AddToMempool(tx_mempool, entry.Fee(0).SpendsCoinbase(true).FromTx(tx));
242
243 // This tx can't be mined by itself
244 tx.vin[0].prevout.hash = hashFreeTx2;
245 tx.vout.resize(1);
246 feeToUse = blockMinFeeRate.GetFee(freeTxSize);
247 tx.vout[0].nValue = 5000000000LL - 100000000 - feeToUse;
248 Txid hashLowFeeTx2 = tx.GetHash();
249 AddToMempool(tx_mempool, entry.Fee(feeToUse).SpendsCoinbase(false).FromTx(tx));
250 block_template = mining->createNewBlock(options);
251 BOOST_REQUIRE(block_template);
252 block = block_template->getBlock();
253
254 // Verify that this tx isn't selected.
255 for (size_t i=0; i<block.vtx.size(); ++i) {
256 BOOST_CHECK(block.vtx[i]->GetHash() != hashFreeTx2);
257 BOOST_CHECK(block.vtx[i]->GetHash() != hashLowFeeTx2);
258 }
259
260 // This tx will be mineable, and should cause hashLowFeeTx2 to be selected
261 // as well.
262 tx.vin[0].prevout.n = 1;
263 tx.vout[0].nValue = 100000000 - 10000; // 10k satoshi fee
264 AddToMempool(tx_mempool, entry.Fee(10000).FromTx(tx));
265 block_template = mining->createNewBlock(options);
266 BOOST_REQUIRE(block_template);
267 block = block_template->getBlock();
268 BOOST_REQUIRE_EQUAL(block.vtx.size(), 9U);
269 BOOST_CHECK(block.vtx[8]->GetHash() == hashLowFeeTx2);
270}
271
272void MinerTestingSetup::TestBasicMining(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst, int baseheight)
273{
274 Txid hash;
277 entry.nFee = 11;
278 entry.nHeight = 11;
279
280 const CAmount BLOCKSUBSIDY = 50 * COIN;
281 const CAmount LOWFEE = CENT;
282 const CAmount HIGHFEE = COIN;
283 const CAmount HIGHERFEE = 4 * COIN;
284
285 auto mining{MakeMining()};
286 BOOST_REQUIRE(mining);
287
288 BlockAssembler::Options options;
289 options.coinbase_output_script = scriptPubKey;
290
291 {
292 CTxMemPool& tx_mempool{MakeMempool()};
293 LOCK(tx_mempool.cs);
294
295 // Just to make sure we can still make simple blocks
296 auto block_template{mining->createNewBlock(options)};
297 BOOST_REQUIRE(block_template);
298 CBlock block{block_template->getBlock()};
299
300 // block sigops > limit: 1000 CHECKMULTISIG + 1
301 tx.vin.resize(1);
302 // NOTE: OP_NOP is used to force 20 SigOps for the CHECKMULTISIG
303 tx.vin[0].scriptSig = CScript() << OP_0 << OP_0 << OP_0 << OP_NOP << OP_CHECKMULTISIG << OP_1;
304 tx.vin[0].prevout.hash = txFirst[0]->GetHash();
305 tx.vin[0].prevout.n = 0;
306 tx.vout.resize(1);
307 tx.vout[0].nValue = BLOCKSUBSIDY;
308 for (unsigned int i = 0; i < 1001; ++i) {
309 tx.vout[0].nValue -= LOWFEE;
310 hash = tx.GetHash();
311 bool spendsCoinbase = i == 0; // only first tx spends coinbase
312 // If we don't set the # of sig ops in the CTxMemPoolEntry, template creation fails
313 AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(spendsCoinbase).FromTx(tx));
314 tx.vin[0].prevout.hash = hash;
315 }
316
317 BOOST_CHECK_EXCEPTION(mining->createNewBlock(options), std::runtime_error, HasReason("bad-blk-sigops"));
318 }
319
320 {
321 CTxMemPool& tx_mempool{MakeMempool()};
322 LOCK(tx_mempool.cs);
323
324 tx.vin[0].prevout.hash = txFirst[0]->GetHash();
325 tx.vout[0].nValue = BLOCKSUBSIDY;
326 for (unsigned int i = 0; i < 1001; ++i) {
327 tx.vout[0].nValue -= LOWFEE;
328 hash = tx.GetHash();
329 bool spendsCoinbase = i == 0; // only first tx spends coinbase
330 // If we do set the # of sig ops in the CTxMemPoolEntry, template creation passes
331 AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(spendsCoinbase).SigOpsCost(80).FromTx(tx));
332 tx.vin[0].prevout.hash = hash;
333 }
334 BOOST_REQUIRE(mining->createNewBlock(options));
335 }
336
337 {
338 CTxMemPool& tx_mempool{MakeMempool()};
339 LOCK(tx_mempool.cs);
340
341 // block size > limit
342 tx.vin[0].scriptSig = CScript();
343 // 18 * (520char + DROP) + OP_1 = 9433 bytes
344 std::vector<unsigned char> vchData(520);
345 for (unsigned int i = 0; i < 18; ++i) {
346 tx.vin[0].scriptSig << vchData << OP_DROP;
347 }
348 tx.vin[0].scriptSig << OP_1;
349 tx.vin[0].prevout.hash = txFirst[0]->GetHash();
350 tx.vout[0].nValue = BLOCKSUBSIDY;
351 for (unsigned int i = 0; i < 128; ++i) {
352 tx.vout[0].nValue -= LOWFEE;
353 hash = tx.GetHash();
354 bool spendsCoinbase = i == 0; // only first tx spends coinbase
355 AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(spendsCoinbase).FromTx(tx));
356 tx.vin[0].prevout.hash = hash;
357 }
358 BOOST_REQUIRE(mining->createNewBlock(options));
359 }
360
361 {
362 CTxMemPool& tx_mempool{MakeMempool()};
363 LOCK(tx_mempool.cs);
364
365 // orphan in tx_mempool, template creation fails
366 hash = tx.GetHash();
367 AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).FromTx(tx));
368 BOOST_CHECK_EXCEPTION(mining->createNewBlock(options), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
369 }
370
371 {
372 CTxMemPool& tx_mempool{MakeMempool()};
373 LOCK(tx_mempool.cs);
374
375 // child with higher feerate than parent
376 tx.vin[0].scriptSig = CScript() << OP_1;
377 tx.vin[0].prevout.hash = txFirst[1]->GetHash();
378 tx.vout[0].nValue = BLOCKSUBSIDY - HIGHFEE;
379 hash = tx.GetHash();
380 AddToMempool(tx_mempool, entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
381 tx.vin[0].prevout.hash = hash;
382 tx.vin.resize(2);
383 tx.vin[1].scriptSig = CScript() << OP_1;
384 tx.vin[1].prevout.hash = txFirst[0]->GetHash();
385 tx.vin[1].prevout.n = 0;
386 tx.vout[0].nValue = tx.vout[0].nValue + BLOCKSUBSIDY - HIGHERFEE; // First txn output + fresh coinbase - new txn fee
387 hash = tx.GetHash();
388 AddToMempool(tx_mempool, entry.Fee(HIGHERFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
389 BOOST_REQUIRE(mining->createNewBlock(options));
390 }
391
392 {
393 CTxMemPool& tx_mempool{MakeMempool()};
394 LOCK(tx_mempool.cs);
395
396 // coinbase in tx_mempool, template creation fails
397 tx.vin.resize(1);
398 tx.vin[0].prevout.SetNull();
399 tx.vin[0].scriptSig = CScript() << OP_0 << OP_1;
400 tx.vout[0].nValue = 0;
401 hash = tx.GetHash();
402 // give it a fee so it'll get mined
403 AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx));
404 // Should throw bad-cb-multiple
405 BOOST_CHECK_EXCEPTION(mining->createNewBlock(options), std::runtime_error, HasReason("bad-cb-multiple"));
406 }
407
408 {
409 CTxMemPool& tx_mempool{MakeMempool()};
410 LOCK(tx_mempool.cs);
411
412 // double spend txn pair in tx_mempool, template creation fails
413 tx.vin[0].prevout.hash = txFirst[0]->GetHash();
414 tx.vin[0].scriptSig = CScript() << OP_1;
415 tx.vout[0].nValue = BLOCKSUBSIDY - HIGHFEE;
416 tx.vout[0].scriptPubKey = CScript() << OP_1;
417 hash = tx.GetHash();
418 AddToMempool(tx_mempool, entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
419 tx.vout[0].scriptPubKey = CScript() << OP_2;
420 hash = tx.GetHash();
421 AddToMempool(tx_mempool, entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
422 BOOST_CHECK_EXCEPTION(mining->createNewBlock(options), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
423 }
424
425 {
426 CTxMemPool& tx_mempool{MakeMempool()};
427 LOCK(tx_mempool.cs);
428
429 // subsidy changing
430 int nHeight = m_node.chainman->ActiveChain().Height();
431 // Create an actual 209999-long block chain (without valid blocks).
432 while (m_node.chainman->ActiveChain().Tip()->nHeight < 209999) {
433 CBlockIndex* prev = m_node.chainman->ActiveChain().Tip();
434 CBlockIndex* next = new CBlockIndex();
435 next->phashBlock = new uint256(m_rng.rand256());
436 m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(next->GetBlockHash());
437 next->pprev = prev;
438 next->nHeight = prev->nHeight + 1;
439 next->BuildSkip();
440 m_node.chainman->ActiveChain().SetTip(*next);
441 }
442 BOOST_REQUIRE(mining->createNewBlock(options));
443 // Extend to a 210000-long block chain.
444 while (m_node.chainman->ActiveChain().Tip()->nHeight < 210000) {
445 CBlockIndex* prev = m_node.chainman->ActiveChain().Tip();
446 CBlockIndex* next = new CBlockIndex();
447 next->phashBlock = new uint256(m_rng.rand256());
448 m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(next->GetBlockHash());
449 next->pprev = prev;
450 next->nHeight = prev->nHeight + 1;
451 next->BuildSkip();
452 m_node.chainman->ActiveChain().SetTip(*next);
453 }
454 BOOST_REQUIRE(mining->createNewBlock(options));
455
456 // invalid p2sh txn in tx_mempool, template creation fails
457 tx.vin[0].prevout.hash = txFirst[0]->GetHash();
458 tx.vin[0].prevout.n = 0;
459 tx.vin[0].scriptSig = CScript() << OP_1;
460 tx.vout[0].nValue = BLOCKSUBSIDY - LOWFEE;
461 CScript script = CScript() << OP_0;
462 tx.vout[0].scriptPubKey = GetScriptForDestination(ScriptHash(script));
463 hash = tx.GetHash();
464 AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
465 tx.vin[0].prevout.hash = hash;
466 tx.vin[0].scriptSig = CScript() << std::vector<unsigned char>(script.begin(), script.end());
467 tx.vout[0].nValue -= LOWFEE;
468 hash = tx.GetHash();
469 AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx));
470 BOOST_CHECK_EXCEPTION(mining->createNewBlock(options), std::runtime_error, HasReason("block-script-verify-flag-failed"));
471
472 // Delete the dummy blocks again.
473 while (m_node.chainman->ActiveChain().Tip()->nHeight > nHeight) {
474 CBlockIndex* del = m_node.chainman->ActiveChain().Tip();
475 m_node.chainman->ActiveChain().SetTip(*Assert(del->pprev));
476 m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(del->pprev->GetBlockHash());
477 delete del->phashBlock;
478 delete del;
479 }
480 }
481
482 CTxMemPool& tx_mempool{MakeMempool()};
483 LOCK(tx_mempool.cs);
484
485 // non-final txs in mempool
486 SetMockTime(m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1);
488 // height map
489 std::vector<int> prevheights;
490
491 // relative height locked
492 tx.version = 2;
493 tx.vin.resize(1);
494 prevheights.resize(1);
495 tx.vin[0].prevout.hash = txFirst[0]->GetHash(); // only 1 transaction
496 tx.vin[0].prevout.n = 0;
497 tx.vin[0].scriptSig = CScript() << OP_1;
498 tx.vin[0].nSequence = m_node.chainman->ActiveChain().Tip()->nHeight + 1; // txFirst[0] is the 2nd block
499 prevheights[0] = baseheight + 1;
500 tx.vout.resize(1);
501 tx.vout[0].nValue = BLOCKSUBSIDY-HIGHFEE;
502 tx.vout[0].scriptPubKey = CScript() << OP_1;
503 tx.nLockTime = 0;
504 hash = tx.GetHash();
505 AddToMempool(tx_mempool, entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
506 BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
507 BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
508
509 {
510 CBlockIndex* active_chain_tip = m_node.chainman->ActiveChain().Tip();
511 BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, prevheights, *CreateBlockIndex(active_chain_tip->nHeight + 2, active_chain_tip))); // Sequence locks pass on 2nd block
512 }
513
514 // relative time locked
515 tx.vin[0].prevout.hash = txFirst[1]->GetHash();
516 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
517 prevheights[0] = baseheight + 2;
518 hash = tx.GetHash();
519 AddToMempool(tx_mempool, entry.Time(Now<NodeSeconds>()).FromTx(tx));
520 BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
521 BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
522
523 const int SEQUENCE_LOCK_TIME = 512; // Sequence locks pass 512 seconds later
524 for (int i = 0; i < CBlockIndex::nMedianTimeSpan; ++i)
525 m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i)->nTime += SEQUENCE_LOCK_TIME; // Trick the MedianTimePast
526 {
527 CBlockIndex* active_chain_tip = m_node.chainman->ActiveChain().Tip();
528 BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, prevheights, *CreateBlockIndex(active_chain_tip->nHeight + 1, active_chain_tip)));
529 }
530
531 for (int i = 0; i < CBlockIndex::nMedianTimeSpan; ++i) {
532 CBlockIndex* ancestor{Assert(m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i))};
533 ancestor->nTime -= SEQUENCE_LOCK_TIME; // undo tricked MTP
534 }
535
536 // absolute height locked
537 tx.vin[0].prevout.hash = txFirst[2]->GetHash();
538 tx.vin[0].nSequence = CTxIn::MAX_SEQUENCE_NONFINAL;
539 prevheights[0] = baseheight + 3;
540 tx.nLockTime = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
541 hash = tx.GetHash();
542 AddToMempool(tx_mempool, entry.Time(Now<NodeSeconds>()).FromTx(tx));
543 BOOST_CHECK(!CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime fails
544 BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
545 BOOST_CHECK(IsFinalTx(CTransaction(tx), m_node.chainman->ActiveChain().Tip()->nHeight + 2, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast())); // Locktime passes on 2nd block
546
547 // ensure tx is final for a specific case where there is no locktime and block height is zero
548 tx.nLockTime = 0;
549 BOOST_CHECK(IsFinalTx(CTransaction(tx), /*nBlockHeight=*/0, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast()));
550
551 // absolute time locked
552 tx.vin[0].prevout.hash = txFirst[3]->GetHash();
553 tx.nLockTime = m_node.chainman->ActiveChain().Tip()->GetMedianTimePast();
554 prevheights.resize(1);
555 prevheights[0] = baseheight + 4;
556 hash = tx.GetHash();
557 AddToMempool(tx_mempool, entry.Time(Now<NodeSeconds>()).FromTx(tx));
558 BOOST_CHECK(!CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime fails
559 BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
560 BOOST_CHECK(IsFinalTx(CTransaction(tx), m_node.chainman->ActiveChain().Tip()->nHeight + 2, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1)); // Locktime passes 1 second later
561
562 // mempool-dependent transactions (not added)
563 tx.vin[0].prevout.hash = hash;
564 prevheights[0] = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
565 tx.nLockTime = 0;
566 tx.vin[0].nSequence = 0;
567 BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
568 BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
569 tx.vin[0].nSequence = 1;
570 BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
571 tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG;
572 BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
573 tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | 1;
574 BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
575
576 auto block_template = mining->createNewBlock(options);
577 BOOST_REQUIRE(block_template);
578
579 // None of the of the absolute height/time locked tx should have made
580 // it into the template because we still check IsFinalTx in CreateNewBlock,
581 // but relative locked txs will if inconsistently added to mempool.
582 // For now these will still generate a valid template until BIP68 soft fork
583 CBlock block{block_template->getBlock()};
584 BOOST_CHECK_EQUAL(block.vtx.size(), 3U);
585 // However if we advance height by 1 and time by SEQUENCE_LOCK_TIME, all of them should be mined
586 for (int i = 0; i < CBlockIndex::nMedianTimeSpan; ++i) {
587 CBlockIndex* ancestor{Assert(m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i))};
588 ancestor->nTime += SEQUENCE_LOCK_TIME; // Trick the MedianTimePast
589 }
590 m_node.chainman->ActiveChain().Tip()->nHeight++;
591 SetMockTime(m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1);
592
593 block_template = mining->createNewBlock(options);
594 BOOST_REQUIRE(block_template);
595 block = block_template->getBlock();
596 BOOST_CHECK_EQUAL(block.vtx.size(), 5U);
597}
598
599void MinerTestingSetup::TestPrioritisedMining(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst)
600{
601 auto mining{MakeMining()};
602 BOOST_REQUIRE(mining);
603
604 BlockAssembler::Options options;
605 options.coinbase_output_script = scriptPubKey;
606
607 CTxMemPool& tx_mempool{MakeMempool()};
608 LOCK(tx_mempool.cs);
609
611
612 // Test that a tx below min fee but prioritised is included
614 tx.vin.resize(1);
615 tx.vin[0].prevout.hash = txFirst[0]->GetHash();
616 tx.vin[0].prevout.n = 0;
617 tx.vin[0].scriptSig = CScript() << OP_1;
618 tx.vout.resize(1);
619 tx.vout[0].nValue = 5000000000LL; // 0 fee
620 Txid hashFreePrioritisedTx = tx.GetHash();
621 AddToMempool(tx_mempool, entry.Fee(0).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
622 tx_mempool.PrioritiseTransaction(hashFreePrioritisedTx, 5 * COIN);
623
624 tx.vin[0].prevout.hash = txFirst[1]->GetHash();
625 tx.vin[0].prevout.n = 0;
626 tx.vout[0].nValue = 5000000000LL - 1000;
627 // This tx has a low fee: 1000 satoshis
628 Txid hashParentTx = tx.GetHash(); // save this txid for later use
629 AddToMempool(tx_mempool, entry.Fee(1000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
630
631 // This tx has a medium fee: 10000 satoshis
632 tx.vin[0].prevout.hash = txFirst[2]->GetHash();
633 tx.vout[0].nValue = 5000000000LL - 10000;
634 Txid hashMediumFeeTx = tx.GetHash();
635 AddToMempool(tx_mempool, entry.Fee(10000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
636 tx_mempool.PrioritiseTransaction(hashMediumFeeTx, -5 * COIN);
637
638 // This tx also has a low fee, but is prioritised
639 tx.vin[0].prevout.hash = hashParentTx;
640 tx.vout[0].nValue = 5000000000LL - 1000 - 1000; // 1000 satoshi fee
641 Txid hashPrioritsedChild = tx.GetHash();
642 AddToMempool(tx_mempool, entry.Fee(1000).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx));
643 tx_mempool.PrioritiseTransaction(hashPrioritsedChild, 2 * COIN);
644
645 // Test that transaction selection properly updates ancestor fee calculations as prioritised
646 // parents get included in a block. Create a transaction with two prioritised ancestors, each
647 // included by itself: FreeParent <- FreeChild <- FreeGrandchild.
648 // When FreeParent is added, a modified entry will be created for FreeChild + FreeGrandchild
649 // FreeParent's prioritisation should not be included in that entry.
650 // When FreeChild is included, FreeChild's prioritisation should also not be included.
651 tx.vin[0].prevout.hash = txFirst[3]->GetHash();
652 tx.vout[0].nValue = 5000000000LL; // 0 fee
653 Txid hashFreeParent = tx.GetHash();
654 AddToMempool(tx_mempool, entry.Fee(0).SpendsCoinbase(true).FromTx(tx));
655 tx_mempool.PrioritiseTransaction(hashFreeParent, 10 * COIN);
656
657 tx.vin[0].prevout.hash = hashFreeParent;
658 tx.vout[0].nValue = 5000000000LL; // 0 fee
659 Txid hashFreeChild = tx.GetHash();
660 AddToMempool(tx_mempool, entry.Fee(0).SpendsCoinbase(false).FromTx(tx));
661 tx_mempool.PrioritiseTransaction(hashFreeChild, 1 * COIN);
662
663 tx.vin[0].prevout.hash = hashFreeChild;
664 tx.vout[0].nValue = 5000000000LL; // 0 fee
665 Txid hashFreeGrandchild = tx.GetHash();
666 AddToMempool(tx_mempool, entry.Fee(0).SpendsCoinbase(false).FromTx(tx));
667
668 auto block_template = mining->createNewBlock(options);
669 BOOST_REQUIRE(block_template);
670 CBlock block{block_template->getBlock()};
671 BOOST_REQUIRE_EQUAL(block.vtx.size(), 6U);
672 BOOST_CHECK(block.vtx[1]->GetHash() == hashFreeParent);
673 BOOST_CHECK(block.vtx[2]->GetHash() == hashFreePrioritisedTx);
674 BOOST_CHECK(block.vtx[3]->GetHash() == hashParentTx);
675 BOOST_CHECK(block.vtx[4]->GetHash() == hashPrioritsedChild);
676 BOOST_CHECK(block.vtx[5]->GetHash() == hashFreeChild);
677 for (size_t i=0; i<block.vtx.size(); ++i) {
678 // The FreeParent and FreeChild's prioritisations should not impact the child.
679 BOOST_CHECK(block.vtx[i]->GetHash() != hashFreeGrandchild);
680 // De-prioritised transaction should not be included.
681 BOOST_CHECK(block.vtx[i]->GetHash() != hashMediumFeeTx);
682 }
683}
684
685// NOTE: These tests rely on CreateNewBlock doing its own self-validation!
686BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
687{
688 auto mining{MakeMining()};
689 BOOST_REQUIRE(mining);
690
691 // Note that by default, these tests run with size accounting enabled.
692 CScript scriptPubKey = CScript() << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"_hex << OP_CHECKSIG;
693 BlockAssembler::Options options;
694 options.coinbase_output_script = scriptPubKey;
695
696 // Create and check a simple template
697 std::unique_ptr<BlockTemplate> block_template = mining->createNewBlock(options);
698 BOOST_REQUIRE(block_template);
699 {
700 CBlock block{block_template->getBlock()};
701 {
702 std::string reason;
703 std::string debug;
704 BOOST_REQUIRE(!mining->checkBlock(block, {.check_pow = false}, reason, debug));
705 BOOST_REQUIRE_EQUAL(reason, "bad-txnmrklroot");
706 BOOST_REQUIRE_EQUAL(debug, "hashMerkleRoot mismatch");
707 }
708
709 block.hashMerkleRoot = BlockMerkleRoot(block);
710
711 {
712 std::string reason;
713 std::string debug;
714 BOOST_REQUIRE(mining->checkBlock(block, {.check_pow = false}, reason, debug));
715 BOOST_REQUIRE_EQUAL(reason, "");
716 BOOST_REQUIRE_EQUAL(debug, "");
717 }
718
719 {
720 // A block template does not have proof-of-work, but it might pass
721 // verification by coincidence. Grind the nonce if needed:
722 while (CheckProofOfWork(block.GetHash(), block.nBits, Assert(m_node.chainman)->GetParams().GetConsensus())) {
723 block.nNonce++;
724 }
725
726 std::string reason;
727 std::string debug;
728 BOOST_REQUIRE(!mining->checkBlock(block, {.check_pow = true}, reason, debug));
729 BOOST_REQUIRE_EQUAL(reason, "high-hash");
730 BOOST_REQUIRE_EQUAL(debug, "proof of work failed");
731 }
732 }
733
734 // We can't make transactions until we have inputs
735 // Therefore, load 110 blocks :)
736 static_assert(std::size(BLOCKINFO) == 110, "Should have 110 blocks to import");
737 int baseheight = 0;
738 std::vector<CTransactionRef> txFirst;
739 for (const auto& bi : BLOCKINFO) {
740 const int current_height{mining->getTip()->height};
741
747 if (current_height % 2 == 0) {
748 block_template = mining->createNewBlock(options);
749 BOOST_REQUIRE(block_template);
750 }
751
752 CBlock block{block_template->getBlock()};
753 CMutableTransaction txCoinbase(*block.vtx[0]);
754 {
755 LOCK(cs_main);
756 block.nVersion = VERSIONBITS_TOP_BITS;
757 block.nTime = Assert(m_node.chainman)->ActiveChain().Tip()->GetMedianTimePast()+1;
758 txCoinbase.version = 1;
759 txCoinbase.vin[0].scriptSig = CScript{} << (current_height + 1) << bi.extranonce;
760 txCoinbase.vout.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this)
761 txCoinbase.vout[0].scriptPubKey = CScript();
762 block.vtx[0] = MakeTransactionRef(txCoinbase);
763 if (txFirst.size() == 0)
764 baseheight = current_height;
765 if (txFirst.size() < 4)
766 txFirst.push_back(block.vtx[0]);
767 block.hashMerkleRoot = BlockMerkleRoot(block);
768 block.nNonce = bi.nonce;
769 }
770 std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
771 // Alternate calls between Chainman's ProcessNewBlock and submitSolution
772 // via the Mining interface. The former is used by net_processing as well
773 // as the submitblock RPC.
774 if (current_height % 2 == 0) {
775 BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(shared_pblock, /*force_processing=*/true, /*min_pow_checked=*/true, nullptr));
776 } else {
777 BOOST_REQUIRE(block_template->submitSolution(block.nVersion, block.nTime, block.nNonce, MakeTransactionRef(txCoinbase)));
778 }
779 {
780 LOCK(cs_main);
781 // The above calls don't guarantee the tip is actually updated, so
782 // we explicitly check this.
783 auto maybe_new_tip{Assert(m_node.chainman)->ActiveChain().Tip()};
784 BOOST_REQUIRE_EQUAL(maybe_new_tip->GetBlockHash(), block.GetHash());
785 }
786 if (current_height % 2 == 0) {
787 block_template = block_template->waitNext();
788 BOOST_REQUIRE(block_template);
789 } else {
790 // This just adds coverage
791 mining->waitTipChanged(block.hashPrevBlock);
792 }
793 }
794
795 LOCK(cs_main);
796
797 TestBasicMining(scriptPubKey, txFirst, baseheight);
798
799 m_node.chainman->ActiveChain().Tip()->nHeight--;
800 SetMockTime(0);
801
802 TestPackageSelection(scriptPubKey, txFirst);
803
804 m_node.chainman->ActiveChain().Tip()->nHeight--;
805 SetMockTime(0);
806
807 TestPrioritisedMining(scriptPubKey, txFirst);
808}
809
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
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
AddToMempool(pool, CTxMemPoolEntry(tx, fee, 0, 1, 0, false, 4, lp))
node::NodeContext m_node
Definition: bitcoin-gui.cpp:43
int flags
Definition: bitcoin-tx.cpp:529
#define Assert(val)
Identity function.
Definition: check.h:115
Definition: block.h:69
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:144
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:150
void BuildSkip()
Build the skiplist pointer for this entry.
Definition: chain.cpp:121
uint256 GetBlockHash() const
Definition: chain.h:248
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:156
static constexpr int nMedianTimeSpan
Definition: chain.h:281
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
Definition: chain.h:147
CCoinsView that brings transactions from a mempool into view.
Definition: txmempool.h:919
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
Definition: feerate.h:35
CAmount GetFee(int32_t virtual_bytes) const
Return the fee in satoshis for the given vsize in vbytes.
Definition: feerate.cpp:20
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:413
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:296
static const uint32_t MAX_SEQUENCE_NONFINAL
This is the maximum sequence number that enables both nLockTime and OP_CHECKLOCKTIMEVERIFY (BIP 65).
Definition: transaction.h:87
static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG
If CTxIn::nSequence encodes a relative lock-time and this flag is set, the relative lock-time has uni...
Definition: transaction.h:104
static const int SEQUENCE_LOCKTIME_GRANULARITY
In order to use the same number of bits to encode roughly the same wall-clock duration,...
Definition: transaction.h:119
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:281
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: setup_common.h:293
Block template interface.
Definition: mining.h:32
Interface giving clients (RPC, Stratum v2 Template Provider in the future) ability to create block te...
Definition: mining.h:80
Generate a new block, without valid proof-of-work.
Definition: miner.h:151
256-bit opaque blob.
Definition: uint256.h:196
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:66
static constexpr unsigned int LOCKTIME_VERIFY_SEQUENCE
Flags for nSequence and nLockTime locks.
Definition: consensus.h:28
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
unsigned int nHeight
bool spendsCoinbase
@ REPLACED
Removed for replacement.
BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
static constexpr struct @13 BLOCKINFO[]
unsigned int extranonce
Definition: miner_tests.cpp:75
static std::unique_ptr< CBlockIndex > CreateBlockIndex(int nHeight, CBlockIndex *active_chain_tip) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: miner_tests.cpp:97
static CFeeRate blockMinFeeRate
Definition: miner_tests.cpp:72
unsigned int nonce
Definition: miner_tests.cpp:76
std::unique_ptr< Mining > MakeMining(node::NodeContext &node)
Return implementation of Mining interface.
Definition: interfaces.cpp:995
""_hex is a compile-time user-defined literal returning a std::array<std::byte>, equivalent to ParseH...
Definition: strencodings.h:384
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
static constexpr 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:32
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params &params)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
Definition: pow.cpp:140
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:195
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
@ OP_2
Definition: script.h:85
@ OP_CHECKMULTISIG
Definition: script.h:192
@ OP_CHECKSIG
Definition: script.h:190
@ OP_NOP
Definition: script.h:102
@ OP_1
Definition: script.h:83
@ OP_DROP
Definition: script.h:124
@ OP_0
Definition: script.h:76
size_t GetSerializeSize(const T &t)
Definition: serialize.h:1094
static constexpr CAmount CENT
Definition: setup_common.h:47
node::NodeContext m_node
Definition: setup_common.h:66
A mutable version of CTransaction.
Definition: transaction.h:378
std::vector< CTxOut > vout
Definition: transaction.h:380
Txid GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:69
std::vector< CTxIn > vin
Definition: transaction.h:379
Data structure storing a fee and size, ordered by increasing fee/size.
Definition: feefrac.h:40
Definition: txmempool.h:19
TestMemPoolEntryHelper & SigOpsCost(unsigned int _sigopsCost)
Definition: txmempool.h:38
TestMemPoolEntryHelper & Time(NodeSeconds tp)
Definition: txmempool.h:34
CAmount nFee
Definition: txmempool.h:21
CTxMemPoolEntry FromTx(const CMutableTransaction &tx) const
Definition: txmempool.cpp:34
TestMemPoolEntryHelper & SpendsCoinbase(bool _flag)
Definition: txmempool.h:37
unsigned int nHeight
Definition: txmempool.h:23
TestMemPoolEntryHelper & Fee(CAmount _fee)
Definition: txmempool.h:33
Testing setup that configures a complete environment.
Definition: setup_common.h:121
Bilingual messages:
Definition: translation.h:24
bool empty() const
Definition: translation.h:35
std::unique_ptr< Mining > MakeMining()
Definition: miner_tests.cpp:63
void TestPackageSelection(const CScript &scriptPubKey, const std::vector< CTransactionRef > &txFirst) EXCLUSIVE_LOCKS_REQUIRED(void TestBasicMining(const CScript &scriptPubKey, const std::vector< CTransactionRef > &txFirst, int baseheight) EXCLUSIVE_LOCKS_REQUIRED(void TestPrioritisedMining(const CScript &scriptPubKey, const std::vector< CTransactionRef > &txFirst) EXCLUSIVE_LOCKS_REQUIRED(bool TestSequenceLocks(const CTransaction &tx, CTxMemPool &tx_mempool) EXCLUSIVE_LOCKS_REQUIRED(
Definition: miner_tests.cpp:45
std::unique_ptr< CTxMemPool > mempool
Definition: context.h:68
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:72
#define LOCK(cs)
Definition: sync.h:259
CTxMemPool::Options MemPoolOptionsForTest(const NodeContext &node)
Definition: txmempool.cpp:21
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
void BulkTransaction(CMutableTransaction &tx, int32_t target_weight)
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:107
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:17
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:40
std::chrono::duration< double, std::chrono::milliseconds::period > MillisecondsDouble
Definition: time.h:88
bool CheckFinalTxAtTip(const CBlockIndex &active_chain_tip, const CTransaction &tx)
Definition: validation.cpp:148
bool CheckSequenceLocksAtTip(CBlockIndex *tip, const LockPoints &lock_points)
Check if transaction will be BIP68 final in the next block to be created on top of tip.
Definition: validation.cpp:247
bool CheckFinalTxAtTip(const CBlockIndex &active_chain_tip, const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(std::optional< LockPoints > CalculateLockPointsAtTip(CBlockIndex *tip, const CCoinsView &coins_view, const CTransaction &tx)
Check if transaction will be final in the next block to be created.
Definition: validation.h:313
static const int32_t VERSIONBITS_TOP_BITS
What bits to set in version for versionbits blocks.
Definition: versionbits.h:21