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