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 <node/miner.h>
12 : : #include <policy/policy.h>
13 : : #include <test/util/random.h>
14 : : #include <test/util/txmempool.h>
15 : : #include <timedata.h>
16 : : #include <txmempool.h>
17 : 0 : #include <uint256.h>
18 : 0 : #include <util/strencodings.h>
19 : : #include <util/time.h>
20 : : #include <validation.h>
21 : : #include <versionbits.h>
22 : :
23 : : #include <test/util/setup_common.h>
24 : :
25 : : #include <memory>
26 : :
27 : : #include <boost/test/unit_test.hpp>
28 : :
29 : : using node::BlockAssembler;
30 : : using node::CBlockTemplate;
31 : :
32 : : namespace miner_tests {
33 : 0 : struct MinerTestingSetup : public TestingSetup {
34 : : void TestPackageSelection(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
35 : : void TestBasicMining(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst, int baseheight) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
36 : : void TestPrioritisedMining(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
37 : 0 : bool TestSequenceLocks(const CTransaction& tx, CTxMemPool& tx_mempool) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
38 : : {
39 : 0 : CCoinsViewMemPool view_mempool{&m_node.chainman->ActiveChainstate().CoinsTip(), tx_mempool};
40 : 0 : CBlockIndex* tip{m_node.chainman->ActiveChain().Tip()};
41 : 0 : const std::optional<LockPoints> lock_points{CalculateLockPointsAtTip(tip, view_mempool, tx)};
42 : 0 : return lock_points.has_value() && CheckSequenceLocksAtTip(tip, *lock_points);
43 : 0 : }
44 : 0 : CTxMemPool& MakeMempool()
45 : : {
46 : : // Delete the previous mempool to ensure with valgrind that the old
47 : : // pointer is not accessed, when the new one should be accessed
48 : : // instead.
49 : 0 : m_node.mempool.reset();
50 : 0 : m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(m_node));
51 : 0 : return *m_node.mempool;
52 : : }
53 : : BlockAssembler AssemblerForTest(CTxMemPool& tx_mempool);
54 : : };
55 : : } // namespace miner_tests
56 : :
57 : 0 : BOOST_FIXTURE_TEST_SUITE(miner_tests, MinerTestingSetup)
58 : :
59 : 0 : static CFeeRate blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
60 : :
61 : 0 : BlockAssembler MinerTestingSetup::AssemblerForTest(CTxMemPool& tx_mempool)
62 : : {
63 : 0 : BlockAssembler::Options options;
64 : :
65 : 0 : options.nBlockMaxWeight = MAX_BLOCK_WEIGHT;
66 : 0 : options.blockMinFeeRate = blockMinFeeRate;
67 : 0 : return BlockAssembler{m_node.chainman->ActiveChainstate(), &tx_mempool, options};
68 : : }
69 : :
70 : : constexpr static struct {
71 : : unsigned char extranonce;
72 : : unsigned int nonce;
73 : : } BLOCKINFO[]{{8, 582909131}, {0, 971462344}, {2, 1169481553}, {6, 66147495}, {7, 427785981}, {8, 80538907},
74 : 0 : {8, 207348013}, {2, 1951240923}, {4, 215054351}, {1, 491520534}, {8, 1282281282}, {4, 639565734},
75 : : {3, 248274685}, {8, 1160085976}, {6, 396349768}, {5, 393780549}, {5, 1096899528}, {4, 965381630},
76 : : {0, 728758712}, {5, 318638310}, {3, 164591898}, {2, 274234550}, {2, 254411237}, {7, 561761812},
77 : : {2, 268342573}, {0, 402816691}, {1, 221006382}, {6, 538872455}, {7, 393315655}, {4, 814555937},
78 : : {7, 504879194}, {6, 467769648}, {3, 925972193}, {2, 200581872}, {3, 168915404}, {8, 430446262},
79 : : {5, 773507406}, {3, 1195366164}, {0, 433361157}, {3, 297051771}, {0, 558856551}, {2, 501614039},
80 : : {3, 528488272}, {2, 473587734}, {8, 230125274}, {2, 494084400}, {4, 357314010}, {8, 60361686},
81 : : {7, 640624687}, {3, 480441695}, {8, 1424447925}, {4, 752745419}, {1, 288532283}, {6, 669170574},
82 : : {5, 1900907591}, {3, 555326037}, {3, 1121014051}, {0, 545835650}, {8, 189196651}, {5, 252371575},
83 : : {0, 199163095}, {6, 558895874}, {6, 1656839784}, {6, 815175452}, {6, 718677851}, {5, 544000334},
84 : : {0, 340113484}, {6, 850744437}, {4, 496721063}, {8, 524715182}, {6, 574361898}, {6, 1642305743},
85 : : {6, 355110149}, {5, 1647379658}, {8, 1103005356}, {7, 556460625}, {3, 1139533992}, {5, 304736030},
86 : : {2, 361539446}, {2, 143720360}, {6, 201939025}, {7, 423141476}, {4, 574633709}, {3, 1412254823},
87 : : {4, 873254135}, {0, 341817335}, {6, 53501687}, {3, 179755410}, {5, 172209688}, {8, 516810279},
88 : : {4, 1228391489}, {8, 325372589}, {6, 550367589}, {0, 876291812}, {7, 412454120}, {7, 717202854},
89 : : {2, 222677843}, {6, 251778867}, {7, 842004420}, {7, 194762829}, {4, 96668841}, {1, 925485796},
90 : : {0, 792342903}, {6, 678455063}, {6, 773251385}, {5, 186617471}, {6, 883189502}, {7, 396077336},
91 : : {8, 254702874}, {0, 455592851}};
92 : :
93 : 0 : static std::unique_ptr<CBlockIndex> CreateBlockIndex(int nHeight, CBlockIndex* active_chain_tip) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
94 : : {
95 : 0 : auto index{std::make_unique<CBlockIndex>()};
96 : 0 : index->nHeight = nHeight;
97 : 0 : index->pprev = active_chain_tip;
98 : 0 : return index;
99 : 0 : }
100 : :
101 : : // Test suite for ancestor feerate transaction selection.
102 : : // Implemented as an additional function, rather than a separate test case,
103 : : // to allow reusing the blockchain created in CreateNewBlock_validity.
104 : 0 : void MinerTestingSetup::TestPackageSelection(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst)
105 : : {
106 : 0 : CTxMemPool& tx_mempool{MakeMempool()};
107 : 0 : LOCK(tx_mempool.cs);
108 : : // Test the ancestor feerate transaction selection.
109 : 0 : TestMemPoolEntryHelper entry;
110 : :
111 : : // Test that a medium fee transaction will be selected after a higher fee
112 : : // rate package with a low fee rate parent.
113 : 0 : CMutableTransaction tx;
114 : 0 : tx.vin.resize(1);
115 : 0 : tx.vin[0].scriptSig = CScript() << OP_1;
116 : 0 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
117 : 0 : tx.vin[0].prevout.n = 0;
118 : 0 : tx.vout.resize(1);
119 : 0 : tx.vout[0].nValue = 5000000000LL - 1000;
120 : : // This tx has a low fee: 1000 satoshis
121 : 0 : uint256 hashParentTx = tx.GetHash(); // save this txid for later use
122 : 0 : tx_mempool.addUnchecked(entry.Fee(1000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
123 : :
124 : : // This tx has a medium fee: 10000 satoshis
125 : 0 : tx.vin[0].prevout.hash = txFirst[1]->GetHash();
126 : 0 : tx.vout[0].nValue = 5000000000LL - 10000;
127 : 0 : uint256 hashMediumFeeTx = tx.GetHash();
128 : 0 : tx_mempool.addUnchecked(entry.Fee(10000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
129 : :
130 : : // This tx has a high fee, but depends on the first transaction
131 : 0 : tx.vin[0].prevout.hash = hashParentTx;
132 : 0 : tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 50k satoshi fee
133 : 0 : uint256 hashHighFeeTx = tx.GetHash();
134 : 0 : tx_mempool.addUnchecked(entry.Fee(50000).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx));
135 : :
136 : 0 : std::unique_ptr<CBlockTemplate> pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
137 : 0 : BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 4U);
138 : 0 : BOOST_CHECK(pblocktemplate->block.vtx[1]->GetHash() == hashParentTx);
139 : 0 : BOOST_CHECK(pblocktemplate->block.vtx[2]->GetHash() == hashHighFeeTx);
140 : 0 : BOOST_CHECK(pblocktemplate->block.vtx[3]->GetHash() == hashMediumFeeTx);
141 : :
142 : : // Test that a package below the block min tx fee doesn't get included
143 : 0 : tx.vin[0].prevout.hash = hashHighFeeTx;
144 : 0 : tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 0 fee
145 : 0 : uint256 hashFreeTx = tx.GetHash();
146 : 0 : tx_mempool.addUnchecked(entry.Fee(0).FromTx(tx));
147 : 0 : size_t freeTxSize = ::GetSerializeSize(tx, PROTOCOL_VERSION);
148 : :
149 : : // Calculate a fee on child transaction that will put the package just
150 : : // below the block min tx fee (assuming 1 child tx of the same size).
151 : 0 : CAmount feeToUse = blockMinFeeRate.GetFee(2*freeTxSize) - 1;
152 : :
153 : 0 : tx.vin[0].prevout.hash = hashFreeTx;
154 : 0 : tx.vout[0].nValue = 5000000000LL - 1000 - 50000 - feeToUse;
155 : 0 : uint256 hashLowFeeTx = tx.GetHash();
156 : 0 : tx_mempool.addUnchecked(entry.Fee(feeToUse).FromTx(tx));
157 : 0 : pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
158 : : // Verify that the free tx and the low fee tx didn't get selected
159 : 0 : for (size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
160 : 0 : BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashFreeTx);
161 : 0 : BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashLowFeeTx);
162 : 0 : }
163 : :
164 : : // Test that packages above the min relay fee do get included, even if one
165 : : // of the transactions is below the min relay fee
166 : : // Remove the low fee transaction and replace with a higher fee transaction
167 : 0 : tx_mempool.removeRecursive(CTransaction(tx), MemPoolRemovalReason::REPLACED);
168 : 0 : tx.vout[0].nValue -= 2; // Now we should be just over the min relay fee
169 : 0 : hashLowFeeTx = tx.GetHash();
170 : 0 : tx_mempool.addUnchecked(entry.Fee(feeToUse + 2).FromTx(tx));
171 : 0 : pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
172 : 0 : BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 6U);
173 : 0 : BOOST_CHECK(pblocktemplate->block.vtx[4]->GetHash() == hashFreeTx);
174 : 0 : BOOST_CHECK(pblocktemplate->block.vtx[5]->GetHash() == hashLowFeeTx);
175 : :
176 : : // Test that transaction selection properly updates ancestor fee
177 : : // calculations as ancestor transactions get included in a block.
178 : : // Add a 0-fee transaction that has 2 outputs.
179 : 0 : tx.vin[0].prevout.hash = txFirst[2]->GetHash();
180 : 0 : tx.vout.resize(2);
181 : 0 : tx.vout[0].nValue = 5000000000LL - 100000000;
182 : 0 : tx.vout[1].nValue = 100000000; // 1BTC output
183 : 0 : uint256 hashFreeTx2 = tx.GetHash();
184 : 0 : tx_mempool.addUnchecked(entry.Fee(0).SpendsCoinbase(true).FromTx(tx));
185 : :
186 : : // This tx can't be mined by itself
187 : 0 : tx.vin[0].prevout.hash = hashFreeTx2;
188 : 0 : tx.vout.resize(1);
189 : 0 : feeToUse = blockMinFeeRate.GetFee(freeTxSize);
190 : 0 : tx.vout[0].nValue = 5000000000LL - 100000000 - feeToUse;
191 : 0 : uint256 hashLowFeeTx2 = tx.GetHash();
192 : 0 : tx_mempool.addUnchecked(entry.Fee(feeToUse).SpendsCoinbase(false).FromTx(tx));
193 : 0 : pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
194 : :
195 : : // Verify that this tx isn't selected.
196 : 0 : for (size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
197 : 0 : BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashFreeTx2);
198 : 0 : BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashLowFeeTx2);
199 : 0 : }
200 : :
201 : : // This tx will be mineable, and should cause hashLowFeeTx2 to be selected
202 : : // as well.
203 : 0 : tx.vin[0].prevout.n = 1;
204 : 0 : tx.vout[0].nValue = 100000000 - 10000; // 10k satoshi fee
205 : 0 : tx_mempool.addUnchecked(entry.Fee(10000).FromTx(tx));
206 : 0 : pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
207 : 0 : BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 9U);
208 : 0 : BOOST_CHECK(pblocktemplate->block.vtx[8]->GetHash() == hashLowFeeTx2);
209 : 0 : }
210 : :
211 : 0 : void MinerTestingSetup::TestBasicMining(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst, int baseheight)
212 : : {
213 : 0 : uint256 hash;
214 : 0 : CMutableTransaction tx;
215 : 0 : TestMemPoolEntryHelper entry;
216 : 0 : entry.nFee = 11;
217 : 0 : entry.nHeight = 11;
218 : :
219 : 0 : const CAmount BLOCKSUBSIDY = 50 * COIN;
220 : 0 : const CAmount LOWFEE = CENT;
221 : 0 : const CAmount HIGHFEE = COIN;
222 : 0 : const CAmount HIGHERFEE = 4 * COIN;
223 : :
224 : : {
225 : 0 : CTxMemPool& tx_mempool{MakeMempool()};
226 : 0 : LOCK(tx_mempool.cs);
227 : 0 :
228 : : // Just to make sure we can still make simple blocks
229 : 0 : auto pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
230 : 0 : BOOST_CHECK(pblocktemplate);
231 : :
232 : : // block sigops > limit: 1000 CHECKMULTISIG + 1
233 : 0 : tx.vin.resize(1);
234 : : // NOTE: OP_NOP is used to force 20 SigOps for the CHECKMULTISIG
235 : 0 : tx.vin[0].scriptSig = CScript() << OP_0 << OP_0 << OP_0 << OP_NOP << OP_CHECKMULTISIG << OP_1;
236 : 0 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
237 : 0 : tx.vin[0].prevout.n = 0;
238 : 0 : tx.vout.resize(1);
239 : 0 : tx.vout[0].nValue = BLOCKSUBSIDY;
240 : 0 : for (unsigned int i = 0; i < 1001; ++i) {
241 : 0 : tx.vout[0].nValue -= LOWFEE;
242 : 0 : hash = tx.GetHash();
243 : 0 : bool spendsCoinbase = i == 0; // only first tx spends coinbase
244 : : // If we don't set the # of sig ops in the CTxMemPoolEntry, template creation fails
245 : 0 : tx_mempool.addUnchecked(entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(spendsCoinbase).FromTx(tx));
246 : 0 : tx.vin[0].prevout.hash = hash;
247 : 0 : }
248 : :
249 : 0 : BOOST_CHECK_EXCEPTION(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-blk-sigops"));
250 : 0 : }
251 : :
252 : : {
253 : 0 : CTxMemPool& tx_mempool{MakeMempool()};
254 : 0 : LOCK(tx_mempool.cs);
255 : :
256 : 0 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
257 : 0 : tx.vout[0].nValue = BLOCKSUBSIDY;
258 : 0 : for (unsigned int i = 0; i < 1001; ++i) {
259 : 0 : tx.vout[0].nValue -= LOWFEE;
260 : 0 : hash = tx.GetHash();
261 : 0 : bool spendsCoinbase = i == 0; // only first tx spends coinbase
262 : : // If we do set the # of sig ops in the CTxMemPoolEntry, template creation passes
263 : 0 : tx_mempool.addUnchecked(entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(spendsCoinbase).SigOpsCost(80).FromTx(tx));
264 : 0 : tx.vin[0].prevout.hash = hash;
265 : 0 : }
266 : 0 : BOOST_CHECK(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
267 : 0 : }
268 : :
269 : : {
270 : 0 : CTxMemPool& tx_mempool{MakeMempool()};
271 : 0 : LOCK(tx_mempool.cs);
272 : :
273 : : // block size > limit
274 : 0 : tx.vin[0].scriptSig = CScript();
275 : : // 18 * (520char + DROP) + OP_1 = 9433 bytes
276 : 0 : std::vector<unsigned char> vchData(520);
277 : 0 : for (unsigned int i = 0; i < 18; ++i) {
278 : 0 : tx.vin[0].scriptSig << vchData << OP_DROP;
279 : 0 : }
280 : 0 : tx.vin[0].scriptSig << OP_1;
281 : 0 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
282 : 0 : tx.vout[0].nValue = BLOCKSUBSIDY;
283 : 0 : for (unsigned int i = 0; i < 128; ++i) {
284 : 0 : tx.vout[0].nValue -= LOWFEE;
285 : 0 : hash = tx.GetHash();
286 : 0 : bool spendsCoinbase = i == 0; // only first tx spends coinbase
287 : 0 : tx_mempool.addUnchecked(entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(spendsCoinbase).FromTx(tx));
288 : 0 : tx.vin[0].prevout.hash = hash;
289 : 0 : }
290 : 0 : BOOST_CHECK(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
291 : 0 : }
292 : :
293 : : {
294 : 0 : CTxMemPool& tx_mempool{MakeMempool()};
295 : 0 : LOCK(tx_mempool.cs);
296 : :
297 : : // orphan in tx_mempool, template creation fails
298 : 0 : hash = tx.GetHash();
299 : 0 : tx_mempool.addUnchecked(entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).FromTx(tx));
300 : 0 : BOOST_CHECK_EXCEPTION(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
301 : 0 : }
302 : :
303 : : {
304 : 0 : CTxMemPool& tx_mempool{MakeMempool()};
305 : 0 : LOCK(tx_mempool.cs);
306 : :
307 : : // child with higher feerate than parent
308 : 0 : tx.vin[0].scriptSig = CScript() << OP_1;
309 : 0 : tx.vin[0].prevout.hash = txFirst[1]->GetHash();
310 : 0 : tx.vout[0].nValue = BLOCKSUBSIDY - HIGHFEE;
311 : 0 : hash = tx.GetHash();
312 : 0 : tx_mempool.addUnchecked(entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
313 : 0 : tx.vin[0].prevout.hash = hash;
314 : 0 : tx.vin.resize(2);
315 : 0 : tx.vin[1].scriptSig = CScript() << OP_1;
316 : 0 : tx.vin[1].prevout.hash = txFirst[0]->GetHash();
317 : 0 : tx.vin[1].prevout.n = 0;
318 : 0 : tx.vout[0].nValue = tx.vout[0].nValue + BLOCKSUBSIDY - HIGHERFEE; // First txn output + fresh coinbase - new txn fee
319 : 0 : hash = tx.GetHash();
320 : 0 : tx_mempool.addUnchecked(entry.Fee(HIGHERFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
321 : 0 : BOOST_CHECK(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
322 : 0 : }
323 : :
324 : : {
325 : 0 : CTxMemPool& tx_mempool{MakeMempool()};
326 : 0 : LOCK(tx_mempool.cs);
327 : :
328 : : // coinbase in tx_mempool, template creation fails
329 : 0 : tx.vin.resize(1);
330 : 0 : tx.vin[0].prevout.SetNull();
331 : 0 : tx.vin[0].scriptSig = CScript() << OP_0 << OP_1;
332 : 0 : tx.vout[0].nValue = 0;
333 : 0 : hash = tx.GetHash();
334 : : // give it a fee so it'll get mined
335 : 0 : tx_mempool.addUnchecked(entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx));
336 : : // Should throw bad-cb-multiple
337 : 0 : BOOST_CHECK_EXCEPTION(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-cb-multiple"));
338 : 0 : }
339 : :
340 : : {
341 : 0 : CTxMemPool& tx_mempool{MakeMempool()};
342 : 0 : LOCK(tx_mempool.cs);
343 : :
344 : : // double spend txn pair in tx_mempool, template creation fails
345 : 0 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
346 : 0 : tx.vin[0].scriptSig = CScript() << OP_1;
347 : 0 : tx.vout[0].nValue = BLOCKSUBSIDY - HIGHFEE;
348 : 0 : tx.vout[0].scriptPubKey = CScript() << OP_1;
349 : 0 : hash = tx.GetHash();
350 : 0 : tx_mempool.addUnchecked(entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
351 : 0 : tx.vout[0].scriptPubKey = CScript() << OP_2;
352 : 0 : hash = tx.GetHash();
353 : 0 : tx_mempool.addUnchecked(entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
354 : 0 : BOOST_CHECK_EXCEPTION(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
355 : 0 : }
356 : :
357 : : {
358 : 0 : CTxMemPool& tx_mempool{MakeMempool()};
359 : 0 : LOCK(tx_mempool.cs);
360 : :
361 : : // subsidy changing
362 : 0 : int nHeight = m_node.chainman->ActiveChain().Height();
363 : : // Create an actual 209999-long block chain (without valid blocks).
364 : 0 : while (m_node.chainman->ActiveChain().Tip()->nHeight < 209999) {
365 : 0 : CBlockIndex* prev = m_node.chainman->ActiveChain().Tip();
366 : 0 : CBlockIndex* next = new CBlockIndex();
367 : 0 : next->phashBlock = new uint256(InsecureRand256());
368 : 0 : m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(next->GetBlockHash());
369 : 0 : next->pprev = prev;
370 : 0 : next->nHeight = prev->nHeight + 1;
371 : 0 : next->BuildSkip();
372 : 0 : m_node.chainman->ActiveChain().SetTip(*next);
373 : : }
374 : 0 : BOOST_CHECK(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
375 : : // Extend to a 210000-long block chain.
376 : 0 : while (m_node.chainman->ActiveChain().Tip()->nHeight < 210000) {
377 : 0 : CBlockIndex* prev = m_node.chainman->ActiveChain().Tip();
378 : 0 : CBlockIndex* next = new CBlockIndex();
379 : 0 : next->phashBlock = new uint256(InsecureRand256());
380 : 0 : m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(next->GetBlockHash());
381 : 0 : next->pprev = prev;
382 : 0 : next->nHeight = prev->nHeight + 1;
383 : 0 : next->BuildSkip();
384 : 0 : m_node.chainman->ActiveChain().SetTip(*next);
385 : : }
386 : 0 : BOOST_CHECK(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
387 : :
388 : : // invalid p2sh txn in tx_mempool, template creation fails
389 : 0 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
390 : 0 : tx.vin[0].prevout.n = 0;
391 : 0 : tx.vin[0].scriptSig = CScript() << OP_1;
392 : 0 : tx.vout[0].nValue = BLOCKSUBSIDY - LOWFEE;
393 : 0 : CScript script = CScript() << OP_0;
394 : 0 : tx.vout[0].scriptPubKey = GetScriptForDestination(ScriptHash(script));
395 : 0 : hash = tx.GetHash();
396 : 0 : tx_mempool.addUnchecked(entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
397 : 0 : tx.vin[0].prevout.hash = hash;
398 : 0 : tx.vin[0].scriptSig = CScript() << std::vector<unsigned char>(script.begin(), script.end());
399 : 0 : tx.vout[0].nValue -= LOWFEE;
400 : 0 : hash = tx.GetHash();
401 : 0 : tx_mempool.addUnchecked(entry.Fee(LOWFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx));
402 : : // Should throw block-validation-failed
403 : 0 : BOOST_CHECK_EXCEPTION(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("block-validation-failed"));
404 : :
405 : : // Delete the dummy blocks again.
406 : 0 : while (m_node.chainman->ActiveChain().Tip()->nHeight > nHeight) {
407 : 0 : CBlockIndex* del = m_node.chainman->ActiveChain().Tip();
408 : 0 : m_node.chainman->ActiveChain().SetTip(*Assert(del->pprev));
409 : 0 : m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(del->pprev->GetBlockHash());
410 : 0 : delete del->phashBlock;
411 : 0 : delete del;
412 : : }
413 : 0 : }
414 : :
415 : 0 : CTxMemPool& tx_mempool{MakeMempool()};
416 : 0 : LOCK(tx_mempool.cs);
417 : :
418 : : // non-final txs in mempool
419 : 0 : SetMockTime(m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1);
420 : 0 : const int flags{LOCKTIME_VERIFY_SEQUENCE};
421 : : // height map
422 : 0 : std::vector<int> prevheights;
423 : :
424 : : // relative height locked
425 : 0 : tx.nVersion = 2;
426 : 0 : tx.vin.resize(1);
427 : 0 : prevheights.resize(1);
428 : 0 : tx.vin[0].prevout.hash = txFirst[0]->GetHash(); // only 1 transaction
429 : 0 : tx.vin[0].prevout.n = 0;
430 : 0 : tx.vin[0].scriptSig = CScript() << OP_1;
431 : 0 : tx.vin[0].nSequence = m_node.chainman->ActiveChain().Tip()->nHeight + 1; // txFirst[0] is the 2nd block
432 : 0 : prevheights[0] = baseheight + 1;
433 : 0 : tx.vout.resize(1);
434 : 0 : tx.vout[0].nValue = BLOCKSUBSIDY-HIGHFEE;
435 : 0 : tx.vout[0].scriptPubKey = CScript() << OP_1;
436 : 0 : tx.nLockTime = 0;
437 : 0 : hash = tx.GetHash();
438 : 0 : tx_mempool.addUnchecked(entry.Fee(HIGHFEE).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
439 : 0 : BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
440 : 0 : BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
441 : :
442 : : {
443 : 0 : CBlockIndex* active_chain_tip = m_node.chainman->ActiveChain().Tip();
444 : 0 : BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, prevheights, *CreateBlockIndex(active_chain_tip->nHeight + 2, active_chain_tip))); // Sequence locks pass on 2nd block
445 : : }
446 : :
447 : : // relative time locked
448 : 0 : tx.vin[0].prevout.hash = txFirst[1]->GetHash();
449 : 0 : 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
450 : 0 : prevheights[0] = baseheight + 2;
451 : 0 : hash = tx.GetHash();
452 : 0 : tx_mempool.addUnchecked(entry.Time(Now<NodeSeconds>()).FromTx(tx));
453 : 0 : BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
454 : 0 : BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
455 : :
456 : 0 : const int SEQUENCE_LOCK_TIME = 512; // Sequence locks pass 512 seconds later
457 : 0 : for (int i = 0; i < CBlockIndex::nMedianTimeSpan; ++i)
458 : 0 : m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i)->nTime += SEQUENCE_LOCK_TIME; // Trick the MedianTimePast
459 : : {
460 : 0 : CBlockIndex* active_chain_tip = m_node.chainman->ActiveChain().Tip();
461 : 0 : BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, prevheights, *CreateBlockIndex(active_chain_tip->nHeight + 1, active_chain_tip)));
462 : : }
463 : :
464 : 0 : for (int i = 0; i < CBlockIndex::nMedianTimeSpan; ++i) {
465 : 0 : CBlockIndex* ancestor{Assert(m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i))};
466 : 0 : ancestor->nTime -= SEQUENCE_LOCK_TIME; // undo tricked MTP
467 : 0 : }
468 : :
469 : : // absolute height locked
470 : 0 : tx.vin[0].prevout.hash = txFirst[2]->GetHash();
471 : 0 : tx.vin[0].nSequence = CTxIn::MAX_SEQUENCE_NONFINAL;
472 : 0 : prevheights[0] = baseheight + 3;
473 : 0 : tx.nLockTime = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
474 : 0 : hash = tx.GetHash();
475 : 0 : tx_mempool.addUnchecked(entry.Time(Now<NodeSeconds>()).FromTx(tx));
476 : 0 : BOOST_CHECK(!CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime fails
477 : 0 : BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
478 : 0 : BOOST_CHECK(IsFinalTx(CTransaction(tx), m_node.chainman->ActiveChain().Tip()->nHeight + 2, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast())); // Locktime passes on 2nd block
479 : :
480 : : // absolute time locked
481 : 0 : tx.vin[0].prevout.hash = txFirst[3]->GetHash();
482 : 0 : tx.nLockTime = m_node.chainman->ActiveChain().Tip()->GetMedianTimePast();
483 : 0 : prevheights.resize(1);
484 : 0 : prevheights[0] = baseheight + 4;
485 : 0 : hash = tx.GetHash();
486 : 0 : tx_mempool.addUnchecked(entry.Time(Now<NodeSeconds>()).FromTx(tx));
487 : 0 : BOOST_CHECK(!CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime fails
488 : 0 : BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
489 : 0 : BOOST_CHECK(IsFinalTx(CTransaction(tx), m_node.chainman->ActiveChain().Tip()->nHeight + 2, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1)); // Locktime passes 1 second later
490 : :
491 : : // mempool-dependent transactions (not added)
492 : 0 : tx.vin[0].prevout.hash = hash;
493 : 0 : prevheights[0] = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
494 : 0 : tx.nLockTime = 0;
495 : 0 : tx.vin[0].nSequence = 0;
496 : 0 : BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
497 : 0 : BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
498 : 0 : tx.vin[0].nSequence = 1;
499 : 0 : BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
500 : 0 : tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG;
501 : 0 : BOOST_CHECK(TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks pass
502 : 0 : tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | 1;
503 : 0 : BOOST_CHECK(!TestSequenceLocks(CTransaction{tx}, tx_mempool)); // Sequence locks fail
504 : :
505 : 0 : auto pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
506 : 0 : BOOST_CHECK(pblocktemplate);
507 : :
508 : : // None of the of the absolute height/time locked tx should have made
509 : : // it into the template because we still check IsFinalTx in CreateNewBlock,
510 : : // but relative locked txs will if inconsistently added to mempool.
511 : : // For now these will still generate a valid template until BIP68 soft fork
512 : 0 : BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 3U);
513 : : // However if we advance height by 1 and time by SEQUENCE_LOCK_TIME, all of them should be mined
514 : 0 : for (int i = 0; i < CBlockIndex::nMedianTimeSpan; ++i) {
515 : 0 : CBlockIndex* ancestor{Assert(m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i))};
516 : 0 : ancestor->nTime += SEQUENCE_LOCK_TIME; // Trick the MedianTimePast
517 : 0 : }
518 : 0 : m_node.chainman->ActiveChain().Tip()->nHeight++;
519 : 0 : SetMockTime(m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1);
520 : :
521 : 0 : BOOST_CHECK(pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
522 : 0 : BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 5U);
523 : 0 : }
524 : :
525 : 0 : void MinerTestingSetup::TestPrioritisedMining(const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst)
526 : : {
527 : 0 : CTxMemPool& tx_mempool{MakeMempool()};
528 : 0 : LOCK(tx_mempool.cs);
529 : :
530 : 0 : TestMemPoolEntryHelper entry;
531 : :
532 : : // Test that a tx below min fee but prioritised is included
533 : 0 : CMutableTransaction tx;
534 : 0 : tx.vin.resize(1);
535 : 0 : tx.vin[0].prevout.hash = txFirst[0]->GetHash();
536 : 0 : tx.vin[0].prevout.n = 0;
537 : 0 : tx.vin[0].scriptSig = CScript() << OP_1;
538 : 0 : tx.vout.resize(1);
539 : 0 : tx.vout[0].nValue = 5000000000LL; // 0 fee
540 : 0 : uint256 hashFreePrioritisedTx = tx.GetHash();
541 : 0 : tx_mempool.addUnchecked(entry.Fee(0).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
542 : 0 : tx_mempool.PrioritiseTransaction(hashFreePrioritisedTx, 5 * COIN);
543 : :
544 : 0 : tx.vin[0].prevout.hash = txFirst[1]->GetHash();
545 : 0 : tx.vin[0].prevout.n = 0;
546 : 0 : tx.vout[0].nValue = 5000000000LL - 1000;
547 : : // This tx has a low fee: 1000 satoshis
548 : 0 : uint256 hashParentTx = tx.GetHash(); // save this txid for later use
549 : 0 : tx_mempool.addUnchecked(entry.Fee(1000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
550 : :
551 : : // This tx has a medium fee: 10000 satoshis
552 : 0 : tx.vin[0].prevout.hash = txFirst[2]->GetHash();
553 : 0 : tx.vout[0].nValue = 5000000000LL - 10000;
554 : 0 : uint256 hashMediumFeeTx = tx.GetHash();
555 : 0 : tx_mempool.addUnchecked(entry.Fee(10000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
556 : 0 : tx_mempool.PrioritiseTransaction(hashMediumFeeTx, -5 * COIN);
557 : :
558 : : // This tx also has a low fee, but is prioritised
559 : 0 : tx.vin[0].prevout.hash = hashParentTx;
560 : 0 : tx.vout[0].nValue = 5000000000LL - 1000 - 1000; // 1000 satoshi fee
561 : 0 : uint256 hashPrioritsedChild = tx.GetHash();
562 : 0 : tx_mempool.addUnchecked(entry.Fee(1000).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx));
563 : 0 : tx_mempool.PrioritiseTransaction(hashPrioritsedChild, 2 * COIN);
564 : :
565 : : // Test that transaction selection properly updates ancestor fee calculations as prioritised
566 : : // parents get included in a block. Create a transaction with two prioritised ancestors, each
567 : : // included by itself: FreeParent <- FreeChild <- FreeGrandchild.
568 : : // When FreeParent is added, a modified entry will be created for FreeChild + FreeGrandchild
569 : : // FreeParent's prioritisation should not be included in that entry.
570 : : // When FreeChild is included, FreeChild's prioritisation should also not be included.
571 : 0 : tx.vin[0].prevout.hash = txFirst[3]->GetHash();
572 : 0 : tx.vout[0].nValue = 5000000000LL; // 0 fee
573 : 0 : uint256 hashFreeParent = tx.GetHash();
574 : 0 : tx_mempool.addUnchecked(entry.Fee(0).SpendsCoinbase(true).FromTx(tx));
575 : 0 : tx_mempool.PrioritiseTransaction(hashFreeParent, 10 * COIN);
576 : :
577 : 0 : tx.vin[0].prevout.hash = hashFreeParent;
578 : 0 : tx.vout[0].nValue = 5000000000LL; // 0 fee
579 : 0 : uint256 hashFreeChild = tx.GetHash();
580 : 0 : tx_mempool.addUnchecked(entry.Fee(0).SpendsCoinbase(false).FromTx(tx));
581 : 0 : tx_mempool.PrioritiseTransaction(hashFreeChild, 1 * COIN);
582 : :
583 : 0 : tx.vin[0].prevout.hash = hashFreeChild;
584 : 0 : tx.vout[0].nValue = 5000000000LL; // 0 fee
585 : 0 : uint256 hashFreeGrandchild = tx.GetHash();
586 : 0 : tx_mempool.addUnchecked(entry.Fee(0).SpendsCoinbase(false).FromTx(tx));
587 : :
588 : 0 : auto pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
589 : 0 : BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 6U);
590 : 0 : BOOST_CHECK(pblocktemplate->block.vtx[1]->GetHash() == hashFreeParent);
591 : 0 : BOOST_CHECK(pblocktemplate->block.vtx[2]->GetHash() == hashFreePrioritisedTx);
592 : 0 : BOOST_CHECK(pblocktemplate->block.vtx[3]->GetHash() == hashParentTx);
593 : 0 : BOOST_CHECK(pblocktemplate->block.vtx[4]->GetHash() == hashPrioritsedChild);
594 : 0 : BOOST_CHECK(pblocktemplate->block.vtx[5]->GetHash() == hashFreeChild);
595 : 0 : for (size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
596 : : // The FreeParent and FreeChild's prioritisations should not impact the child.
597 : 0 : BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashFreeGrandchild);
598 : : // De-prioritised transaction should not be included.
599 : 0 : BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashMediumFeeTx);
600 : 0 : }
601 : 0 : }
602 : :
603 : : // NOTE: These tests rely on CreateNewBlock doing its own self-validation!
604 : 0 : BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
605 : : {
606 : : // Note that by default, these tests run with size accounting enabled.
607 : 0 : CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
608 : 0 : std::unique_ptr<CBlockTemplate> pblocktemplate;
609 : :
610 : 0 : CTxMemPool& tx_mempool{*m_node.mempool};
611 : : // Simple block creation, nothing special yet:
612 : 0 : BOOST_CHECK(pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
613 : :
614 : : // We can't make transactions until we have inputs
615 : : // Therefore, load 110 blocks :)
616 : : static_assert(std::size(BLOCKINFO) == 110, "Should have 110 blocks to import");
617 : 0 : int baseheight = 0;
618 : 0 : std::vector<CTransactionRef> txFirst;
619 : 0 : for (const auto& bi : BLOCKINFO) {
620 : 0 : CBlock *pblock = &pblocktemplate->block; // pointer for convenience
621 : : {
622 : 0 : LOCK(cs_main);
623 : 0 : pblock->nVersion = VERSIONBITS_TOP_BITS;
624 : 0 : pblock->nTime = m_node.chainman->ActiveChain().Tip()->GetMedianTimePast()+1;
625 : 0 : CMutableTransaction txCoinbase(*pblock->vtx[0]);
626 : 0 : txCoinbase.nVersion = 1;
627 : 0 : txCoinbase.vin[0].scriptSig = CScript{} << (m_node.chainman->ActiveChain().Height() + 1) << bi.extranonce;
628 : 0 : txCoinbase.vout.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this)
629 : 0 : txCoinbase.vout[0].scriptPubKey = CScript();
630 : 0 : pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
631 : 0 : if (txFirst.size() == 0)
632 : 0 : baseheight = m_node.chainman->ActiveChain().Height();
633 : 0 : if (txFirst.size() < 4)
634 : 0 : txFirst.push_back(pblock->vtx[0]);
635 : 0 : pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
636 : 0 : pblock->nNonce = bi.nonce;
637 : 0 : }
638 : 0 : std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(*pblock);
639 : 0 : BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(shared_pblock, true, true, nullptr));
640 : 0 : pblock->hashPrevBlock = pblock->GetHash();
641 : 0 : }
642 : :
643 : 0 : LOCK(cs_main);
644 : :
645 : 0 : TestBasicMining(scriptPubKey, txFirst, baseheight);
646 : :
647 : 0 : m_node.chainman->ActiveChain().Tip()->nHeight--;
648 : 0 : SetMockTime(0);
649 : :
650 : 0 : TestPackageSelection(scriptPubKey, txFirst);
651 : :
652 : 0 : m_node.chainman->ActiveChain().Tip()->nHeight--;
653 : 0 : SetMockTime(0);
654 : :
655 : 0 : TestPrioritisedMining(scriptPubKey, txFirst);
656 : 0 : }
657 : :
658 : 0 : BOOST_AUTO_TEST_SUITE_END()
|