Branch data Line data Source code
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>
8 : : #include <consensus/consensus.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>
15 : : #include <test/util/transaction_utils.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/setup_common.h>
29 : :
30 : : #include <memory>
31 : : #include <vector>
32 : :
33 : : #include <boost/test/unit_test.hpp>
34 : :
35 : : using namespace util::hex_literals;
36 : : using interfaces::BlockTemplate;
37 : : using interfaces::Mining;
38 : : using node::BlockAssembler;
39 : :
40 : : namespace miner_tests {
41 : 2 : struct MinerTestingSetup : public TestingSetup {
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);
45 : 8 : bool TestSequenceLocks(const CTransaction& tx, CTxMemPool& tx_mempool) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
46 : : {
47 : 8 : CCoinsViewMemPool view_mempool{&m_node.chainman->ActiveChainstate().CoinsTip(), tx_mempool};
48 [ + - - + ]: 8 : CBlockIndex* tip{m_node.chainman->ActiveChain().Tip()};
49 [ + - ]: 8 : const std::optional<LockPoints> lock_points{CalculateLockPointsAtTip(tip, view_mempool, tx)};
50 [ + - + - : 16 : return lock_points.has_value() && CheckSequenceLocksAtTip(tip, *lock_points);
+ + ]
51 : 8 : }
52 : 11 : CTxMemPool& MakeMempool()
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 [ + - ]: 11 : m_node.mempool.reset();
58 [ + - ]: 11 : bilingual_str error;
59 [ + - + - ]: 11 : m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(m_node), error);
60 [ + - ]: 11 : Assert(error.empty());
61 : 11 : return *m_node.mempool;
62 : 11 : }
63 : 4 : std::unique_ptr<Mining> MakeMining()
64 : : {
65 : 4 : return interfaces::MakeMining(m_node);
66 : : }
67 : : };
68 : : } // namespace miner_tests
69 : :
70 : : BOOST_FIXTURE_TEST_SUITE(miner_tests, MinerTestingSetup)
71 : :
72 : : static CFeeRate blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
73 : :
74 : : constexpr 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 : :
97 : 2 : static std::unique_ptr<CBlockIndex> CreateBlockIndex(int nHeight, CBlockIndex* active_chain_tip) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
98 : : {
99 : 2 : auto index{std::make_unique<CBlockIndex>()};
100 : 2 : index->nHeight = nHeight;
101 : 2 : index->pprev = active_chain_tip;
102 : 2 : 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.
108 : 1 : void MinerTestingSetup::TestPackageSelection(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst)
109 : : {
110 : 1 : CTxMemPool& tx_mempool{MakeMempool()};
111 : 1 : auto mining{MakeMining()};
112 [ + - ]: 1 : BlockAssembler::Options options;
113 : 1 : options.coinbase_output_script = scriptPubKey;
114 : :
115 [ + - ]: 1 : LOCK(tx_mempool.cs);
116 : : // Test the ancestor feerate transaction selection.
117 : 1 : TestMemPoolEntryHelper entry;
118 : :
119 : : // Test that a medium fee transaction will be selected after a higher fee
120 : : // rate package with a low fee rate parent.
121 [ + - ]: 1 : CMutableTransaction tx;
122 [ + - ]: 1 : tx.vin.resize(1);
123 [ + - ]: 1 : tx.vin[0].scriptSig = CScript() << OP_1;
124 [ + - ]: 1 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
125 : 1 : tx.vin[0].prevout.n = 0;
126 [ + - ]: 1 : tx.vout.resize(1);
127 [ + - ]: 1 : tx.vout[0].nValue = 5000000000LL - 1000;
128 : : // This tx has a low fee: 1000 satoshis
129 [ + - ]: 1 : Txid hashParentTx = tx.GetHash(); // save this txid for later use
130 [ + - ]: 1 : const auto parent_tx{entry.Fee(1000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx)};
131 [ + - ]: 1 : AddToMempool(tx_mempool, parent_tx);
132 : :
133 : : // This tx has a medium fee: 10000 satoshis
134 [ + - ]: 1 : tx.vin[0].prevout.hash = txFirst[1]->GetHash();
135 : 1 : tx.vout[0].nValue = 5000000000LL - 10000;
136 [ + - ]: 1 : Txid hashMediumFeeTx = tx.GetHash();
137 [ + - ]: 1 : const auto medium_fee_tx{entry.Fee(10000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx)};
138 [ + - ]: 1 : AddToMempool(tx_mempool, medium_fee_tx);
139 : :
140 : : // This tx has a high fee, but depends on the first transaction
141 [ + - ]: 1 : tx.vin[0].prevout.hash = hashParentTx;
142 : 1 : tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 50k satoshi fee
143 [ + - ]: 1 : Txid hashHighFeeTx = tx.GetHash();
144 [ + - ]: 1 : const auto high_fee_tx{entry.Fee(50000).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx)};
145 [ + - ]: 1 : AddToMempool(tx_mempool, high_fee_tx);
146 : :
147 [ + - ]: 1 : std::unique_ptr<BlockTemplate> block_template = mining->createNewBlock(options);
148 [ + - + - : 2 : BOOST_REQUIRE(block_template);
+ - ]
149 [ + - ]: 1 : CBlock block{block_template->getBlock()};
150 [ + - - + : 1 : BOOST_REQUIRE_EQUAL(block.vtx.size(), 4U);
+ - ]
151 [ + - + - : 2 : BOOST_CHECK(block.vtx[1]->GetHash() == hashParentTx);
+ - ]
152 [ + - + - : 2 : BOOST_CHECK(block.vtx[2]->GetHash() == hashHighFeeTx);
+ - ]
153 [ + - + - : 2 : BOOST_CHECK(block.vtx[3]->GetHash() == hashMediumFeeTx);
+ - ]
154 : :
155 : : // Test the inclusion of package feerates in the block template and ensure they are sequential.
156 [ + - + - : 1 : const auto block_package_feerates = BlockAssembler{m_node.chainman->ActiveChainstate(), &tx_mempool, options}.CreateNewBlock()->m_package_feerates;
+ - + - ]
157 [ + - - + : 2 : BOOST_CHECK(block_package_feerates.size() == 2);
+ - + - ]
158 : :
159 : : // parent_tx and high_fee_tx are added to the block as a package.
160 : 1 : const auto combined_txs_fee = parent_tx.GetFee() + high_fee_tx.GetFee();
161 [ + - + - ]: 1 : const auto combined_txs_size = parent_tx.GetTxSize() + high_fee_tx.GetTxSize();
162 [ + - ]: 1 : FeeFrac package_feefrac{combined_txs_fee, combined_txs_size};
163 : : // The package should be added first.
164 [ + - + - : 3 : BOOST_CHECK(block_package_feerates[0] == package_feefrac);
+ - + - ]
165 : :
166 : : // The medium_fee_tx should be added next.
167 [ + - + - ]: 1 : FeeFrac medium_tx_feefrac{medium_fee_tx.GetFee(), medium_fee_tx.GetTxSize()};
168 [ + - + - : 3 : BOOST_CHECK(block_package_feerates[1] == medium_tx_feefrac);
+ - + - ]
169 : :
170 : : // Test that a package below the block min tx fee doesn't get included
171 [ + - ]: 1 : tx.vin[0].prevout.hash = hashHighFeeTx;
172 : 1 : tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 0 fee
173 [ + - ]: 1 : Txid hashFreeTx = tx.GetHash();
174 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(0).FromTx(tx));
175 : 1 : size_t freeTxSize = ::GetSerializeSize(TX_WITH_WITNESS(tx));
176 : :
177 : : // Calculate a fee on child transaction that will put the package just
178 : : // below the block min tx fee (assuming 1 child tx of the same size).
179 [ + - ]: 1 : CAmount feeToUse = blockMinFeeRate.GetFee(2*freeTxSize) - 1;
180 : :
181 [ + - ]: 1 : tx.vin[0].prevout.hash = hashFreeTx;
182 : 1 : tx.vout[0].nValue = 5000000000LL - 1000 - 50000 - feeToUse;
183 [ + - ]: 1 : Txid hashLowFeeTx = tx.GetHash();
184 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(feeToUse).FromTx(tx));
185 : :
186 : : // waitNext() should return nullptr because there is no better template
187 [ + - ]: 1 : auto should_be_nullptr = block_template->waitNext({.timeout = MillisecondsDouble{0}, .fee_threshold = 1});
188 [ + - + - : 2 : BOOST_REQUIRE(should_be_nullptr == nullptr);
+ - ]
189 : :
190 [ + - ]: 1 : block = block_template->getBlock();
191 : : // Verify that the free tx and the low fee tx didn't get selected
192 [ - + + + ]: 5 : for (size_t i=0; i<block.vtx.size(); ++i) {
193 [ + - + - : 8 : BOOST_CHECK(block.vtx[i]->GetHash() != hashFreeTx);
+ - ]
194 [ + - + - ]: 8 : BOOST_CHECK(block.vtx[i]->GetHash() != hashLowFeeTx);
195 : : }
196 : :
197 : : // Test that packages above the min relay fee do get included, even if one
198 : : // of the transactions is below the min relay fee
199 : : // Remove the low fee transaction and replace with a higher fee transaction
200 [ + - + - ]: 1 : tx_mempool.removeRecursive(CTransaction(tx), MemPoolRemovalReason::REPLACED);
201 [ + - ]: 1 : tx.vout[0].nValue -= 2; // Now we should be just over the min relay fee
202 [ + - ]: 1 : hashLowFeeTx = tx.GetHash();
203 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(feeToUse + 2).FromTx(tx));
204 : :
205 : : // waitNext() should return if fees for the new template are at least 1 sat up
206 [ + - ]: 2 : block_template = block_template->waitNext({.fee_threshold = 1});
207 [ + - + - : 2 : BOOST_REQUIRE(block_template);
+ - ]
208 [ + - ]: 1 : block = block_template->getBlock();
209 [ + - - + : 1 : BOOST_REQUIRE_EQUAL(block.vtx.size(), 6U);
+ - ]
210 [ + - + - : 2 : BOOST_CHECK(block.vtx[4]->GetHash() == hashFreeTx);
+ - ]
211 [ + - + - : 2 : BOOST_CHECK(block.vtx[5]->GetHash() == hashLowFeeTx);
+ - ]
212 : :
213 : : // Test that transaction selection properly updates ancestor fee
214 : : // calculations as ancestor transactions get included in a block.
215 : : // Add a 0-fee transaction that has 2 outputs.
216 [ + - ]: 1 : tx.vin[0].prevout.hash = txFirst[2]->GetHash();
217 [ + - ]: 1 : tx.vout.resize(2);
218 [ + - ]: 1 : tx.vout[0].nValue = 5000000000LL - 100000000;
219 : 1 : tx.vout[1].nValue = 100000000; // 1BTC output
220 : : // Increase size to avoid rounding errors: when the feerate is extremely small (i.e. 1sat/kvB), evaluating the fee
221 : : // at a smaller transaction size gives us a rounded value of 0.
222 [ + - ]: 1 : BulkTransaction(tx, 4000);
223 [ + - ]: 1 : Txid hashFreeTx2 = tx.GetHash();
224 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(0).SpendsCoinbase(true).FromTx(tx));
225 : :
226 : : // This tx can't be mined by itself
227 [ + - ]: 1 : tx.vin[0].prevout.hash = hashFreeTx2;
228 [ + - ]: 1 : tx.vout.resize(1);
229 [ + - ]: 1 : feeToUse = blockMinFeeRate.GetFee(freeTxSize);
230 [ + - ]: 1 : tx.vout[0].nValue = 5000000000LL - 100000000 - feeToUse;
231 [ + - ]: 1 : Txid hashLowFeeTx2 = tx.GetHash();
232 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(feeToUse).SpendsCoinbase(false).FromTx(tx));
233 [ + - ]: 2 : block_template = mining->createNewBlock(options);
234 [ + - + - : 2 : BOOST_REQUIRE(block_template);
+ - ]
235 [ + - ]: 1 : block = block_template->getBlock();
236 : :
237 : : // Verify that this tx isn't selected.
238 [ - + + + ]: 7 : for (size_t i=0; i<block.vtx.size(); ++i) {
239 [ + - + - : 12 : BOOST_CHECK(block.vtx[i]->GetHash() != hashFreeTx2);
+ - ]
240 [ + - + - ]: 12 : BOOST_CHECK(block.vtx[i]->GetHash() != hashLowFeeTx2);
241 : : }
242 : :
243 : : // This tx will be mineable, and should cause hashLowFeeTx2 to be selected
244 : : // as well.
245 [ + - ]: 1 : tx.vin[0].prevout.n = 1;
246 : 1 : tx.vout[0].nValue = 100000000 - 10000; // 10k satoshi fee
247 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(10000).FromTx(tx));
248 [ + - ]: 2 : block_template = mining->createNewBlock(options);
249 [ + - + - : 2 : BOOST_REQUIRE(block_template);
+ - ]
250 [ + - ]: 1 : block = block_template->getBlock();
251 [ + - - + : 1 : BOOST_REQUIRE_EQUAL(block.vtx.size(), 9U);
+ - ]
252 [ + - + - ]: 2 : BOOST_CHECK(block.vtx[8]->GetHash() == hashLowFeeTx2);
253 [ + - ]: 3 : }
254 : :
255 : 1 : void MinerTestingSetup::TestBasicMining(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst, int baseheight)
256 : : {
257 : 1 : Txid hash;
258 : 1 : CMutableTransaction tx;
259 : 1 : TestMemPoolEntryHelper entry;
260 : 1 : entry.nFee = 11;
261 : 1 : entry.nHeight = 11;
262 : :
263 : 1 : const CAmount BLOCKSUBSIDY = 50 * COIN;
264 : 1 : const CAmount LOWFEE = CENT;
265 : 1 : const CAmount HIGHFEE = COIN;
266 : 1 : const CAmount HIGHERFEE = 4 * COIN;
267 : :
268 [ + - ]: 1 : auto mining{MakeMining()};
269 [ + - + - : 2 : BOOST_REQUIRE(mining);
+ - ]
270 : :
271 [ + - ]: 1 : BlockAssembler::Options options;
272 : 1 : options.coinbase_output_script = scriptPubKey;
273 : :
274 : 1 : {
275 [ + - ]: 1 : CTxMemPool& tx_mempool{MakeMempool()};
276 [ + - ]: 1 : LOCK(tx_mempool.cs);
277 : :
278 : : // Just to make sure we can still make simple blocks
279 [ + - ]: 1 : auto block_template{mining->createNewBlock(options)};
280 [ + - + - : 2 : BOOST_REQUIRE(block_template);
+ - ]
281 [ + - ]: 1 : CBlock block{block_template->getBlock()};
282 : :
283 : : // block sigops > limit: 1000 CHECKMULTISIG + 1
284 [ + - ]: 1 : tx.vin.resize(1);
285 : : // NOTE: OP_NOP is used to force 20 SigOps for the CHECKMULTISIG
286 [ + - + - : 1 : tx.vin[0].scriptSig = CScript() << OP_0 << OP_0 << OP_0 << OP_NOP << OP_CHECKMULTISIG << OP_1;
+ - + - +
- + - ]
287 [ + - ]: 1 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
288 : 1 : tx.vin[0].prevout.n = 0;
289 [ + - ]: 1 : tx.vout.resize(1);
290 : 1 : tx.vout[0].nValue = BLOCKSUBSIDY;
291 [ + + ]: 1002 : for (unsigned int i = 0; i < 1001; ++i) {
292 [ + - ]: 1001 : tx.vout[0].nValue -= LOWFEE;
293 [ + - ]: 1001 : hash = tx.GetHash();
294 : 1001 : bool spendsCoinbase = i == 0; // only first tx spends coinbase
295 : : // If we don't set the # of sig ops in the CTxMemPoolEntry, template creation fails
296 [ + - + - ]: 1001 : AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(spendsCoinbase).FromTx(tx));
297 : 1001 : tx.vin[0].prevout.hash = hash;
298 : : }
299 : :
300 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(mining->createNewBlock(options), std::runtime_error, HasReason("bad-blk-sigops"));
- - - - -
+ + - + -
+ - ]
301 [ + - ]: 1 : }
302 : :
303 : 1 : {
304 [ + - ]: 1 : CTxMemPool& tx_mempool{MakeMempool()};
305 [ + - ]: 1 : LOCK(tx_mempool.cs);
306 : :
307 : 1 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
308 : 1 : tx.vout[0].nValue = BLOCKSUBSIDY;
309 [ + + ]: 1002 : for (unsigned int i = 0; i < 1001; ++i) {
310 [ + - ]: 1001 : tx.vout[0].nValue -= LOWFEE;
311 [ + - ]: 1001 : hash = tx.GetHash();
312 : 1001 : bool spendsCoinbase = i == 0; // only first tx spends coinbase
313 : : // If we do set the # of sig ops in the CTxMemPoolEntry, template creation passes
314 [ + - + - ]: 1001 : AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(spendsCoinbase).SigOpsCost(80).FromTx(tx));
315 : 1001 : tx.vin[0].prevout.hash = hash;
316 : : }
317 [ + - + - : 2 : BOOST_REQUIRE(mining->createNewBlock(options));
+ - + - ]
318 : 0 : }
319 : :
320 : 1 : {
321 [ + - ]: 1 : CTxMemPool& tx_mempool{MakeMempool()};
322 [ + - ]: 1 : LOCK(tx_mempool.cs);
323 : :
324 : : // block size > limit
325 : 1 : tx.vin[0].scriptSig = CScript();
326 : : // 18 * (520char + DROP) + OP_1 = 9433 bytes
327 [ + - ]: 1 : std::vector<unsigned char> vchData(520);
328 [ + + ]: 19 : for (unsigned int i = 0; i < 18; ++i) {
329 [ - + + - ]: 18 : tx.vin[0].scriptSig << vchData << OP_DROP;
330 : : }
331 [ + - ]: 1 : tx.vin[0].scriptSig << OP_1;
332 : 1 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
333 : 1 : tx.vout[0].nValue = BLOCKSUBSIDY;
334 [ + + ]: 129 : for (unsigned int i = 0; i < 128; ++i) {
335 [ + - ]: 128 : tx.vout[0].nValue -= LOWFEE;
336 [ + - ]: 128 : hash = tx.GetHash();
337 : 128 : bool spendsCoinbase = i == 0; // only first tx spends coinbase
338 [ + - + - ]: 128 : AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(spendsCoinbase).FromTx(tx));
339 : 128 : tx.vin[0].prevout.hash = hash;
340 : : }
341 [ + - + - : 2 : BOOST_REQUIRE(mining->createNewBlock(options));
+ - ]
342 [ + - ]: 1 : }
343 : :
344 : 1 : {
345 [ + - ]: 1 : CTxMemPool& tx_mempool{MakeMempool()};
346 [ + - ]: 1 : LOCK(tx_mempool.cs);
347 : :
348 : : // orphan in tx_mempool, template creation fails
349 [ + - ]: 1 : hash = tx.GetHash();
350 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).FromTx(tx));
351 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(mining->createNewBlock(options), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
- - - - +
- - + + -
+ - + - ]
352 : 0 : }
353 : :
354 : 1 : {
355 [ + - ]: 1 : CTxMemPool& tx_mempool{MakeMempool()};
356 [ + - ]: 1 : LOCK(tx_mempool.cs);
357 : :
358 : : // child with higher feerate than parent
359 [ + - ]: 1 : tx.vin[0].scriptSig = CScript() << OP_1;
360 [ + - ]: 1 : tx.vin[0].prevout.hash = txFirst[1]->GetHash();
361 : 1 : tx.vout[0].nValue = BLOCKSUBSIDY - HIGHFEE;
362 [ + - ]: 1 : hash = tx.GetHash();
363 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
364 [ + - ]: 1 : tx.vin[0].prevout.hash = hash;
365 [ + - ]: 1 : tx.vin.resize(2);
366 [ + - ]: 1 : tx.vin[1].scriptSig = CScript() << OP_1;
367 [ + - ]: 1 : tx.vin[1].prevout.hash = txFirst[0]->GetHash();
368 : 1 : tx.vin[1].prevout.n = 0;
369 : 1 : tx.vout[0].nValue = tx.vout[0].nValue + BLOCKSUBSIDY - HIGHERFEE; // First txn output + fresh coinbase - new txn fee
370 [ + - ]: 1 : hash = tx.GetHash();
371 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(HIGHERFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
372 [ + - + - : 2 : BOOST_REQUIRE(mining->createNewBlock(options));
+ - + - ]
373 : 0 : }
374 : :
375 : 1 : {
376 [ + - ]: 1 : CTxMemPool& tx_mempool{MakeMempool()};
377 [ + - ]: 1 : LOCK(tx_mempool.cs);
378 : :
379 : : // coinbase in tx_mempool, template creation fails
380 [ + - ]: 1 : tx.vin.resize(1);
381 : 1 : tx.vin[0].prevout.SetNull();
382 [ + - + - ]: 1 : tx.vin[0].scriptSig = CScript() << OP_0 << OP_1;
383 [ + - ]: 1 : tx.vout[0].nValue = 0;
384 [ + - ]: 1 : hash = tx.GetHash();
385 : : // give it a fee so it'll get mined
386 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx));
387 : : // Should throw bad-cb-multiple
388 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(mining->createNewBlock(options), std::runtime_error, HasReason("bad-cb-multiple"));
- - - - +
- - + + -
+ - + - ]
389 : 0 : }
390 : :
391 : 1 : {
392 [ + - ]: 1 : CTxMemPool& tx_mempool{MakeMempool()};
393 [ + - ]: 1 : LOCK(tx_mempool.cs);
394 : :
395 : : // double spend txn pair in tx_mempool, template creation fails
396 [ + - ]: 1 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
397 [ + - ]: 1 : tx.vin[0].scriptSig = CScript() << OP_1;
398 [ + - ]: 1 : tx.vout[0].nValue = BLOCKSUBSIDY - HIGHFEE;
399 [ + - ]: 1 : tx.vout[0].scriptPubKey = CScript() << OP_1;
400 [ + - ]: 1 : hash = tx.GetHash();
401 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
402 [ + - ]: 1 : tx.vout[0].scriptPubKey = CScript() << OP_2;
403 [ + - ]: 1 : hash = tx.GetHash();
404 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
405 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(mining->createNewBlock(options), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
- - - - +
- - + + -
+ - + - ]
406 : 0 : }
407 : :
408 : 1 : {
409 [ + - ]: 1 : CTxMemPool& tx_mempool{MakeMempool()};
410 [ + - ]: 1 : LOCK(tx_mempool.cs);
411 : :
412 : : // subsidy changing
413 [ + - - + ]: 1 : int nHeight = m_node.chainman->ActiveChain().Height();
414 : : // Create an actual 209999-long block chain (without valid blocks).
415 [ + - - + : 419780 : while (m_node.chainman->ActiveChain().Tip()->nHeight < 209999) {
+ + ]
416 [ + - - + ]: 209889 : CBlockIndex* prev = m_node.chainman->ActiveChain().Tip();
417 [ + - ]: 209889 : CBlockIndex* next = new CBlockIndex();
418 [ + - ]: 209889 : next->phashBlock = new uint256(m_rng.rand256());
419 [ + - + - : 209889 : m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(next->GetBlockHash());
+ - ]
420 : 209889 : next->pprev = prev;
421 : 209889 : next->nHeight = prev->nHeight + 1;
422 [ + - ]: 209889 : next->BuildSkip();
423 [ + - + - ]: 209889 : m_node.chainman->ActiveChain().SetTip(*next);
424 : : }
425 [ + - + - : 2 : BOOST_REQUIRE(mining->createNewBlock(options));
+ - ]
426 : : // Extend to a 210000-long block chain.
427 [ + - - + : 4 : while (m_node.chainman->ActiveChain().Tip()->nHeight < 210000) {
+ + ]
428 [ + - - + ]: 1 : CBlockIndex* prev = m_node.chainman->ActiveChain().Tip();
429 [ + - ]: 1 : CBlockIndex* next = new CBlockIndex();
430 [ + - ]: 1 : next->phashBlock = new uint256(m_rng.rand256());
431 [ + - + - : 1 : m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(next->GetBlockHash());
+ - ]
432 : 1 : next->pprev = prev;
433 : 1 : next->nHeight = prev->nHeight + 1;
434 [ + - ]: 1 : next->BuildSkip();
435 [ + - + - ]: 1 : m_node.chainman->ActiveChain().SetTip(*next);
436 : : }
437 [ + - + - : 2 : BOOST_REQUIRE(mining->createNewBlock(options));
+ - + - ]
438 : :
439 : : // invalid p2sh txn in tx_mempool, template creation fails
440 [ + - ]: 1 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
441 : 1 : tx.vin[0].prevout.n = 0;
442 [ + - ]: 1 : tx.vin[0].scriptSig = CScript() << OP_1;
443 [ + - ]: 1 : tx.vout[0].nValue = BLOCKSUBSIDY - LOWFEE;
444 [ + - ]: 1 : CScript script = CScript() << OP_0;
445 [ + - + - ]: 1 : tx.vout[0].scriptPubKey = GetScriptForDestination(ScriptHash(script));
446 [ + - ]: 1 : hash = tx.GetHash();
447 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
448 [ - + ]: 1 : tx.vin[0].prevout.hash = hash;
449 [ - + + - ]: 2 : tx.vin[0].scriptSig = CScript() << std::vector<unsigned char>(script.begin(), script.end());
450 [ + - ]: 1 : tx.vout[0].nValue -= LOWFEE;
451 [ + - ]: 1 : hash = tx.GetHash();
452 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx));
453 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(mining->createNewBlock(options), std::runtime_error, HasReason("block-script-verify-flag-failed"));
- - - - -
+ + - + -
+ - ]
454 : :
455 : : // Delete the dummy blocks again.
456 [ + - - + : 419782 : while (m_node.chainman->ActiveChain().Tip()->nHeight > nHeight) {
+ + ]
457 [ + - - + ]: 209890 : CBlockIndex* del = m_node.chainman->ActiveChain().Tip();
458 [ + - + - : 209890 : m_node.chainman->ActiveChain().SetTip(*Assert(del->pprev));
+ - ]
459 [ + - + - : 209890 : m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(del->pprev->GetBlockHash());
+ - ]
460 [ + - ]: 209890 : delete del->phashBlock;
461 [ + - ]: 209890 : delete del;
462 : : }
463 [ + - ]: 1 : }
464 : :
465 [ + - ]: 1 : CTxMemPool& tx_mempool{MakeMempool()};
466 [ + - ]: 1 : LOCK(tx_mempool.cs);
467 : :
468 : : // non-final txs in mempool
469 [ + - - + : 2 : SetMockTime(m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1);
+ - ]
470 : 1 : const int flags{LOCKTIME_VERIFY_SEQUENCE};
471 : : // height map
472 : 1 : std::vector<int> prevheights;
473 : :
474 : : // relative height locked
475 : 1 : tx.version = 2;
476 [ + - ]: 1 : tx.vin.resize(1);
477 [ + - ]: 1 : prevheights.resize(1);
478 [ + - ]: 1 : tx.vin[0].prevout.hash = txFirst[0]->GetHash(); // only 1 transaction
479 : 1 : tx.vin[0].prevout.n = 0;
480 [ + - ]: 1 : tx.vin[0].scriptSig = CScript() << OP_1;
481 [ + - - + : 2 : tx.vin[0].nSequence = m_node.chainman->ActiveChain().Tip()->nHeight + 1; // txFirst[0] is the 2nd block
+ - ]
482 : 1 : prevheights[0] = baseheight + 1;
483 [ + - ]: 1 : tx.vout.resize(1);
484 [ + - ]: 1 : tx.vout[0].nValue = BLOCKSUBSIDY-HIGHFEE;
485 [ + - ]: 1 : tx.vout[0].scriptPubKey = CScript() << OP_1;
486 : 1 : tx.nLockTime = 0;
487 [ + - ]: 1 : hash = tx.GetHash();
488 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
489 [ + - + - : 4 : BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
+ - - + +
- + - + -
+ - ]
490 [ + - + - : 3 : BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
+ - + - +
- ]
491 : :
492 : 1 : {
493 [ + - - + ]: 1 : CBlockIndex* active_chain_tip = m_node.chainman->ActiveChain().Tip();
494 [ + - + - : 3 : BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, prevheights, *CreateBlockIndex(active_chain_tip->nHeight + 2, active_chain_tip))); // Sequence locks pass on 2nd block
+ - + - +
- + - ]
495 : : }
496 : :
497 : : // relative time locked
498 [ + - ]: 1 : tx.vin[0].prevout.hash = txFirst[1]->GetHash();
499 [ + - - + : 3 : 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
+ - - + +
- ]
500 : 1 : prevheights[0] = baseheight + 2;
501 [ + - ]: 1 : hash = tx.GetHash();
502 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Time(Now<NodeSeconds>()).FromTx(tx));
503 [ + - + - : 4 : BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
+ - - + +
- + - + -
+ - ]
504 [ + - + - : 3 : BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
+ - + - ]
505 : :
506 : 1 : const int SEQUENCE_LOCK_TIME = 512; // Sequence locks pass 512 seconds later
507 [ + + ]: 12 : for (int i = 0; i < CBlockIndex::nMedianTimeSpan; ++i)
508 [ + - - + : 33 : m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i)->nTime += SEQUENCE_LOCK_TIME; // Trick the MedianTimePast
+ - - + +
- ]
509 : 1 : {
510 [ + - - + ]: 1 : CBlockIndex* active_chain_tip = m_node.chainman->ActiveChain().Tip();
511 [ + - + - : 3 : BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, prevheights, *CreateBlockIndex(active_chain_tip->nHeight + 1, active_chain_tip)));
+ - + - +
- ]
512 : : }
513 : :
514 [ + + ]: 12 : for (int i = 0; i < CBlockIndex::nMedianTimeSpan; ++i) {
515 [ + - - + : 33 : CBlockIndex* ancestor{Assert(m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i))};
+ - - + +
- + - ]
516 : 11 : ancestor->nTime -= SEQUENCE_LOCK_TIME; // undo tricked MTP
517 : : }
518 : :
519 : : // absolute height locked
520 [ + - ]: 1 : tx.vin[0].prevout.hash = txFirst[2]->GetHash();
521 : 1 : tx.vin[0].nSequence = CTxIn::MAX_SEQUENCE_NONFINAL;
522 : 1 : prevheights[0] = baseheight + 3;
523 [ + - - + ]: 1 : tx.nLockTime = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
524 [ + - ]: 1 : hash = tx.GetHash();
525 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Time(Now<NodeSeconds>()).FromTx(tx));
526 [ + - + - : 4 : BOOST_CHECK(!CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime fails
+ - - + +
- + - + -
+ - ]
527 [ + - + - : 3 : BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
+ - + - +
- ]
528 [ + - + - : 5 : BOOST_CHECK(IsFinalTx(CTransaction(tx), m_node.chainman->ActiveChain().Tip()->nHeight + 2, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast())); // Locktime passes on 2nd block
- + + - -
+ + - + -
+ - + - ]
529 : :
530 : : // ensure tx is final for a specific case where there is no locktime and block height is zero
531 : 1 : tx.nLockTime = 0;
532 [ + - + - : 4 : BOOST_CHECK(IsFinalTx(CTransaction(tx), /*nBlockHeight=*/0, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast()));
- + + - +
- + - +
- ]
533 : :
534 : : // absolute time locked
535 [ + - ]: 1 : tx.vin[0].prevout.hash = txFirst[3]->GetHash();
536 [ + - - + ]: 2 : tx.nLockTime = m_node.chainman->ActiveChain().Tip()->GetMedianTimePast();
537 [ + - ]: 1 : prevheights.resize(1);
538 [ + - ]: 1 : prevheights[0] = baseheight + 4;
539 [ + - ]: 1 : hash = tx.GetHash();
540 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Time(Now<NodeSeconds>()).FromTx(tx));
541 [ + - + - : 4 : BOOST_CHECK(!CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime fails
+ - - + +
- + - + -
+ - ]
542 [ + - + - : 3 : BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
+ - + - +
- ]
543 [ + - + - : 5 : BOOST_CHECK(IsFinalTx(CTransaction(tx), m_node.chainman->ActiveChain().Tip()->nHeight + 2, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1)); // Locktime passes 1 second later
- + + - -
+ + - + -
+ - + - ]
544 : :
545 : : // mempool-dependent transactions (not added)
546 [ + - ]: 1 : tx.vin[0].prevout.hash = hash;
547 [ + - - + : 2 : prevheights[0] = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
+ - ]
548 : 1 : tx.nLockTime = 0;
549 : 1 : tx.vin[0].nSequence = 0;
550 [ + - + - : 4 : BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
+ - - + +
- + - + -
+ - ]
551 [ + - + - : 3 : BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
+ - + - +
- ]
552 [ + - ]: 1 : tx.vin[0].nSequence = 1;
553 [ + - + - : 3 : BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
+ - + - +
- ]
554 [ + - ]: 1 : tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG;
555 [ + - + - : 3 : BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
+ - + - +
- ]
556 [ + - ]: 1 : tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | 1;
557 [ + - + - : 3 : BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
+ - + - +
- ]
558 : :
559 [ + - ]: 1 : auto block_template = mining->createNewBlock(options);
560 [ + - + - : 2 : BOOST_REQUIRE(block_template);
+ - ]
561 : :
562 : : // None of the of the absolute height/time locked tx should have made
563 : : // it into the template because we still check IsFinalTx in CreateNewBlock,
564 : : // but relative locked txs will if inconsistently added to mempool.
565 : : // For now these will still generate a valid template until BIP68 soft fork
566 [ + - ]: 1 : CBlock block{block_template->getBlock()};
567 [ + - - + : 1 : BOOST_CHECK_EQUAL(block.vtx.size(), 3U);
+ - ]
568 : : // However if we advance height by 1 and time by SEQUENCE_LOCK_TIME, all of them should be mined
569 [ + + ]: 12 : for (int i = 0; i < CBlockIndex::nMedianTimeSpan; ++i) {
570 [ + - - + : 33 : CBlockIndex* ancestor{Assert(m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i))};
+ - - + +
- + - ]
571 : 11 : ancestor->nTime += SEQUENCE_LOCK_TIME; // Trick the MedianTimePast
572 : : }
573 [ + - - + ]: 1 : m_node.chainman->ActiveChain().Tip()->nHeight++;
574 [ + - - + : 2 : SetMockTime(m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1);
+ - ]
575 : :
576 [ + - ]: 2 : block_template = mining->createNewBlock(options);
577 [ + - + - : 2 : BOOST_REQUIRE(block_template);
+ - ]
578 [ + - ]: 1 : block = block_template->getBlock();
579 [ + - - + : 1 : BOOST_CHECK_EQUAL(block.vtx.size(), 5U);
+ - ]
580 [ + - ]: 3 : }
581 : :
582 : 1 : void MinerTestingSetup::TestPrioritisedMining(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst)
583 : : {
584 : 1 : auto mining{MakeMining()};
585 [ + - + - : 2 : BOOST_REQUIRE(mining);
+ - ]
586 : :
587 [ + - ]: 1 : BlockAssembler::Options options;
588 : 1 : options.coinbase_output_script = scriptPubKey;
589 : :
590 [ + - ]: 1 : CTxMemPool& tx_mempool{MakeMempool()};
591 [ + - ]: 1 : LOCK(tx_mempool.cs);
592 : :
593 : 1 : TestMemPoolEntryHelper entry;
594 : :
595 : : // Test that a tx below min fee but prioritised is included
596 [ + - ]: 1 : CMutableTransaction tx;
597 [ + - ]: 1 : tx.vin.resize(1);
598 [ + - ]: 1 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
599 : 1 : tx.vin[0].prevout.n = 0;
600 [ + - ]: 1 : tx.vin[0].scriptSig = CScript() << OP_1;
601 [ + - ]: 1 : tx.vout.resize(1);
602 [ + - ]: 1 : tx.vout[0].nValue = 5000000000LL; // 0 fee
603 [ + - ]: 1 : Txid hashFreePrioritisedTx = tx.GetHash();
604 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(0).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
605 [ + - ]: 1 : tx_mempool.PrioritiseTransaction(hashFreePrioritisedTx, 5 * COIN);
606 : :
607 [ + - ]: 1 : tx.vin[0].prevout.hash = txFirst[1]->GetHash();
608 : 1 : tx.vin[0].prevout.n = 0;
609 : 1 : tx.vout[0].nValue = 5000000000LL - 1000;
610 : : // This tx has a low fee: 1000 satoshis
611 [ + - ]: 1 : Txid hashParentTx = tx.GetHash(); // save this txid for later use
612 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(1000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
613 : :
614 : : // This tx has a medium fee: 10000 satoshis
615 [ + - ]: 1 : tx.vin[0].prevout.hash = txFirst[2]->GetHash();
616 : 1 : tx.vout[0].nValue = 5000000000LL - 10000;
617 [ + - ]: 1 : Txid hashMediumFeeTx = tx.GetHash();
618 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(10000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
619 [ + - ]: 1 : tx_mempool.PrioritiseTransaction(hashMediumFeeTx, -5 * COIN);
620 : :
621 : : // This tx also has a low fee, but is prioritised
622 [ + - ]: 1 : tx.vin[0].prevout.hash = hashParentTx;
623 : 1 : tx.vout[0].nValue = 5000000000LL - 1000 - 1000; // 1000 satoshi fee
624 [ + - ]: 1 : Txid hashPrioritsedChild = tx.GetHash();
625 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(1000).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx));
626 [ + - ]: 1 : tx_mempool.PrioritiseTransaction(hashPrioritsedChild, 2 * COIN);
627 : :
628 : : // Test that transaction selection properly updates ancestor fee calculations as prioritised
629 : : // parents get included in a block. Create a transaction with two prioritised ancestors, each
630 : : // included by itself: FreeParent <- FreeChild <- FreeGrandchild.
631 : : // When FreeParent is added, a modified entry will be created for FreeChild + FreeGrandchild
632 : : // FreeParent's prioritisation should not be included in that entry.
633 : : // When FreeChild is included, FreeChild's prioritisation should also not be included.
634 [ + - ]: 1 : tx.vin[0].prevout.hash = txFirst[3]->GetHash();
635 : 1 : tx.vout[0].nValue = 5000000000LL; // 0 fee
636 [ + - ]: 1 : Txid hashFreeParent = tx.GetHash();
637 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(0).SpendsCoinbase(true).FromTx(tx));
638 [ + - ]: 1 : tx_mempool.PrioritiseTransaction(hashFreeParent, 10 * COIN);
639 : :
640 [ + - ]: 1 : tx.vin[0].prevout.hash = hashFreeParent;
641 : 1 : tx.vout[0].nValue = 5000000000LL; // 0 fee
642 [ + - ]: 1 : Txid hashFreeChild = tx.GetHash();
643 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(0).SpendsCoinbase(false).FromTx(tx));
644 [ + - ]: 1 : tx_mempool.PrioritiseTransaction(hashFreeChild, 1 * COIN);
645 : :
646 [ + - ]: 1 : tx.vin[0].prevout.hash = hashFreeChild;
647 : 1 : tx.vout[0].nValue = 5000000000LL; // 0 fee
648 [ + - ]: 1 : Txid hashFreeGrandchild = tx.GetHash();
649 [ + - + - ]: 1 : AddToMempool(tx_mempool, entry.Fee(0).SpendsCoinbase(false).FromTx(tx));
650 : :
651 [ + - ]: 1 : auto block_template = mining->createNewBlock(options);
652 [ + - + - : 2 : BOOST_REQUIRE(block_template);
+ - ]
653 [ + - ]: 1 : CBlock block{block_template->getBlock()};
654 [ + - - + : 1 : BOOST_REQUIRE_EQUAL(block.vtx.size(), 6U);
+ - ]
655 [ + - + - : 2 : BOOST_CHECK(block.vtx[1]->GetHash() == hashFreeParent);
+ - ]
656 [ + - + - : 2 : BOOST_CHECK(block.vtx[2]->GetHash() == hashFreePrioritisedTx);
+ - ]
657 [ + - + - : 2 : BOOST_CHECK(block.vtx[3]->GetHash() == hashParentTx);
+ - ]
658 [ + - + - : 2 : BOOST_CHECK(block.vtx[4]->GetHash() == hashPrioritsedChild);
+ - ]
659 [ + - + - ]: 2 : BOOST_CHECK(block.vtx[5]->GetHash() == hashFreeChild);
660 [ - + + + ]: 7 : for (size_t i=0; i<block.vtx.size(); ++i) {
661 : : // The FreeParent and FreeChild's prioritisations should not impact the child.
662 [ + - + - : 12 : BOOST_CHECK(block.vtx[i]->GetHash() != hashFreeGrandchild);
+ - ]
663 : : // De-prioritised transaction should not be included.
664 [ + - + - ]: 12 : BOOST_CHECK(block.vtx[i]->GetHash() != hashMediumFeeTx);
665 : : }
666 [ + - ]: 3 : }
667 : :
668 : : // NOTE: These tests rely on CreateNewBlock doing its own self-validation!
669 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
670 : : {
671 : 1 : auto mining{MakeMining()};
672 [ + - + - ]: 2 : BOOST_REQUIRE(mining);
673 : :
674 : : // Note that by default, these tests run with size accounting enabled.
675 [ + - ]: 1 : CScript scriptPubKey = CScript() << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"_hex << OP_CHECKSIG;
676 [ + - ]: 1 : BlockAssembler::Options options;
677 : 1 : options.coinbase_output_script = scriptPubKey;
678 : :
679 : : // Create and check a simple template
680 [ + - ]: 1 : std::unique_ptr<BlockTemplate> block_template = mining->createNewBlock(options);
681 [ + - + - : 2 : BOOST_REQUIRE(block_template);
+ - ]
682 : 1 : {
683 [ + - ]: 1 : CBlock block{block_template->getBlock()};
684 : 1 : {
685 [ + - ]: 1 : std::string reason;
686 : 1 : std::string debug;
687 [ + - + - : 2 : BOOST_REQUIRE(!mining->checkBlock(block, {.check_pow = false}, reason, debug));
+ - + - ]
688 [ + - + - ]: 1 : BOOST_REQUIRE_EQUAL(reason, "bad-txnmrklroot");
689 [ + - + - ]: 1 : BOOST_REQUIRE_EQUAL(debug, "hashMerkleRoot mismatch");
690 : 1 : }
691 : :
692 [ + - ]: 1 : block.hashMerkleRoot = BlockMerkleRoot(block);
693 : :
694 : 1 : {
695 [ + - ]: 1 : std::string reason;
696 : 1 : std::string debug;
697 [ + - + - : 2 : BOOST_REQUIRE(mining->checkBlock(block, {.check_pow = false}, reason, debug));
+ - + - ]
698 [ + - + - ]: 1 : BOOST_REQUIRE_EQUAL(reason, "");
699 [ + - + - ]: 1 : BOOST_REQUIRE_EQUAL(debug, "");
700 : 1 : }
701 : :
702 : 1 : {
703 : : // A block template does not have proof-of-work, but it might pass
704 : : // verification by coincidence. Grind the nonce if needed:
705 [ + - + - : 1 : while (CheckProofOfWork(block.GetHash(), block.nBits, Assert(m_node.chainman)->GetParams().GetConsensus())) {
+ - - + ]
706 : 0 : block.nNonce++;
707 : : }
708 : :
709 [ + - ]: 1 : std::string reason;
710 : 1 : std::string debug;
711 [ + - + - : 2 : BOOST_REQUIRE(!mining->checkBlock(block, {.check_pow = true}, reason, debug));
+ - + - ]
712 [ + - + - ]: 1 : BOOST_REQUIRE_EQUAL(reason, "high-hash");
713 [ + - + - ]: 1 : BOOST_REQUIRE_EQUAL(debug, "proof of work failed");
714 : 1 : }
715 : 0 : }
716 : :
717 : : // We can't make transactions until we have inputs
718 : : // Therefore, load 110 blocks :)
719 : 1 : static_assert(std::size(BLOCKINFO) == 110, "Should have 110 blocks to import");
720 : 1 : int baseheight = 0;
721 : 1 : std::vector<CTransactionRef> txFirst;
722 [ + + ]: 111 : for (const auto& bi : BLOCKINFO) {
723 [ + - ]: 110 : const int current_height{mining->getTip()->height};
724 : :
725 : : /**
726 : : * Simple block creation, nothing special yet.
727 : : * If current_height is odd, block_template will have already been
728 : : * set at the end of the previous loop.
729 : : */
730 [ + + ]: 110 : if (current_height % 2 == 0) {
731 [ + - ]: 110 : block_template = mining->createNewBlock(options);
732 [ + - + - ]: 110 : BOOST_REQUIRE(block_template);
733 : : }
734 : :
735 [ + - ]: 110 : CBlock block{block_template->getBlock()};
736 [ + - ]: 110 : CMutableTransaction txCoinbase(*block.vtx[0]);
737 : 110 : {
738 [ + - ]: 110 : LOCK(cs_main);
739 : 110 : block.nVersion = VERSIONBITS_TOP_BITS;
740 [ + - + - : 220 : block.nTime = Assert(m_node.chainman)->ActiveChain().Tip()->GetMedianTimePast()+1;
- + ]
741 : 110 : txCoinbase.version = 1;
742 [ + - + - ]: 110 : txCoinbase.vin[0].scriptSig = CScript{} << (current_height + 1) << bi.extranonce;
743 [ + - ]: 110 : txCoinbase.vout.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this)
744 : 110 : txCoinbase.vout[0].scriptPubKey = CScript();
745 [ + - - + ]: 220 : block.vtx[0] = MakeTransactionRef(txCoinbase);
746 [ - + + + ]: 110 : if (txFirst.size() == 0)
747 : 1 : baseheight = current_height;
748 [ + + ]: 110 : if (txFirst.size() < 4)
749 [ + - ]: 4 : txFirst.push_back(block.vtx[0]);
750 [ + - ]: 110 : block.hashMerkleRoot = BlockMerkleRoot(block);
751 [ + - ]: 110 : block.nNonce = bi.nonce;
752 : 0 : }
753 [ + - ]: 110 : std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
754 : : // Alternate calls between Chainman's ProcessNewBlock and submitSolution
755 : : // via the Mining interface. The former is used by net_processing as well
756 : : // as the submitblock RPC.
757 [ + + ]: 110 : if (current_height % 2 == 0) {
758 [ + - + - : 110 : BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(shared_pblock, /*force_processing=*/true, /*min_pow_checked=*/true, nullptr));
+ - + - ]
759 : : } else {
760 [ + - + - : 165 : BOOST_REQUIRE(block_template->submitSolution(block.nVersion, block.nTime, block.nNonce, MakeTransactionRef(txCoinbase)));
+ - + - -
+ ]
761 : : }
762 : 110 : {
763 [ + - ]: 110 : LOCK(cs_main);
764 : : // The above calls don't guarantee the tip is actually updated, so
765 : : // we explicitly check this.
766 [ + - + - : 110 : auto maybe_new_tip{Assert(m_node.chainman)->ActiveChain().Tip()};
- + ]
767 [ + - + - : 110 : BOOST_REQUIRE_EQUAL(maybe_new_tip->GetBlockHash(), block.GetHash());
+ - + - ]
768 : 0 : }
769 [ + + ]: 110 : if (current_height % 2 == 0) {
770 [ + - ]: 110 : block_template = block_template->waitNext();
771 [ + - + - ]: 110 : BOOST_REQUIRE(block_template);
772 : : } else {
773 : : // This just adds coverage
774 [ + - ]: 55 : mining->waitTipChanged(block.hashPrevBlock);
775 : : }
776 : 220 : }
777 : :
778 [ + - ]: 1 : LOCK(cs_main);
779 : :
780 [ + - ]: 1 : TestBasicMining(scriptPubKey, txFirst, baseheight);
781 : :
782 [ + - - + ]: 1 : m_node.chainman->ActiveChain().Tip()->nHeight--;
783 [ + - ]: 1 : SetMockTime(0);
784 : :
785 [ + - ]: 1 : TestPackageSelection(scriptPubKey, txFirst);
786 : :
787 [ + - - + ]: 1 : m_node.chainman->ActiveChain().Tip()->nHeight--;
788 [ + - ]: 1 : SetMockTime(0);
789 : :
790 [ + - ]: 1 : TestPrioritisedMining(scriptPubKey, txFirst);
791 : 1 : }
792 : :
793 : : BOOST_AUTO_TEST_SUITE_END()
|