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