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