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