PIVX Core  5.6.99
P2P Digital Currency
miner_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2014 The Bitcoin Core developers
2 // Copyright (c) 2016-2021 The PIVX Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or https://www.opensource.org/licenses/mit-license.php.
5 
7 
8 #include "blockassembler.h"
9 #include "checkpoints.h"
10 #include "consensus/merkle.h"
11 #include "miner.h"
12 #include "pubkey.h"
13 #include "uint256.h"
14 #include "util/blockstatecatcher.h"
15 #include "util/system.h"
16 #include "validation.h"
17 #include "wallet/wallet.h"
18 
19 #include <boost/test/unit_test.hpp>
20 
21 
22 // future: this should be MAINNET.
24 
25 static
26 struct {
27  unsigned char extranonce;
28  unsigned int nonce;
29 } blockinfo[] = {
30  {4, 0xa4a3e223}, {2, 0x15c32f9e}, {1, 0x0375b547}, {1, 0x7004a8a5},
31  {2, 0xce440296}, {2, 0x52cfe198}, {1, 0x77a72cd0}, {2, 0xbb5d6f84},
32  {2, 0x83f30c2c}, {1, 0x48a73d5b}, {1, 0xef7dcd01}, {2, 0x6809c6c4},
33  {2, 0x0883ab3c}, {1, 0x087bbbe2}, {2, 0x2104a814}, {2, 0xdffb6daa},
34  {1, 0xee8a0a08}, {2, 0xba4237c1}, {1, 0xa70349dc}, {1, 0x344722bb},
35  {3, 0xd6294733}, {2, 0xec9f5c94}, {2, 0xca2fbc28}, {1, 0x6ba4f406},
36  {2, 0x015d4532}, {1, 0x6e119b7c}, {2, 0x43e8f314}, {2, 0x27962f38},
37  {2, 0xb571b51b}, {2, 0xb36bee23}, {2, 0xd17924a8}, {2, 0x6bc212d9},
38  {1, 0x630d4948}, {2, 0x9a4c4ebb}, {2, 0x554be537}, {1, 0xd63ddfc7},
39  {2, 0xa10acc11}, {1, 0x759a8363}, {2, 0xfb73090d}, {1, 0xe82c6a34},
40  {1, 0xe33e92d7}, {3, 0x658ef5cb}, {2, 0xba32ff22}, {5, 0x0227a10c},
41  {1, 0xa9a70155}, {5, 0xd096d809}, {1, 0x37176174}, {1, 0x830b8d0f},
42  {1, 0xc6e3910e}, {2, 0x823f3ca8}, {1, 0x99850849}, {1, 0x7521fb81},
43  {1, 0xaacaabab}, {1, 0xd645a2eb}, {5, 0x7aea1781}, {5, 0x9d6e4b78},
44  {1, 0x4ce90fd8}, {1, 0xabdc832d}, {6, 0x4a34f32a}, {2, 0xf2524c1c},
45  {2, 0x1bbeb08a}, {1, 0xad47f480}, {1, 0x9f026aeb}, {1, 0x15a95049},
46  {2, 0xd1cb95b2}, {2, 0xf84bbda5}, {1, 0x0fa62cd1}, {1, 0xe05f9169},
47  {1, 0x78d194a9}, {5, 0x3e38147b}, {5, 0x737ba0d4}, {1, 0x63378e10},
48  {1, 0x6d5f91cf}, {2, 0x88612eb8}, {2, 0xe9639484}, {1, 0xb7fabc9d},
49  {2, 0x19b01592}, {1, 0x5a90dd31}, {2, 0x5bd7e028}, {2, 0x94d00323},
50  {1, 0xa9b9c01a}, {1, 0x3a40de61}, {1, 0x56e7eec7}, {5, 0x859f7ef6},
51  {1, 0xfd8e5630}, {1, 0x2b0c9f7f}, {1, 0xba700e26}, {1, 0x7170a408},
52  {1, 0x70de86a8}, {1, 0x74d64cd5}, {1, 0x49e738a1}, {2, 0x6910b602},
53  {0, 0x643c565f}, {1, 0x54264b3f}, {2, 0x97ea6396}, {2, 0x55174459},
54  {2, 0x03e8779a}, {1, 0x98f34d8f}, {1, 0xc07b2b07}, {1, 0xdfe29668},
55  {1, 0x3141c7c1}, {1, 0xb3b595f4}, {1, 0x735abf08}, {5, 0x623bfbce},
56  {2, 0xd351e722}, {1, 0xf4ca48c9}, {1, 0x5b19c670}, {1, 0xa164bf0e},
57  {2, 0xbbbeb305}, {2, 0xfe1c810a},
58 };
59 
60 // Test suite for ancestor feerate transaction selection.
61 // Implemented as an additional function, rather than a separate test case,
62 // to allow reusing the blockchain created in CreateNewBlock_validity.
63 void TestPackageSelection(const CChainParams& chainparams, CScript scriptPubKey, std::vector<CTransactionRef>& txFirst)
64 {
65  // Test the ancestor feerate transaction selection.
67 
68  // Test that a medium fee transaction will be selected after a higher fee
69  // rate package with a low fee rate parent.
71  tx.vin.resize(1);
72  tx.vin[0].scriptSig = CScript() << OP_1;
73  tx.vin[0].prevout.hash = txFirst[0]->GetHash();
74  tx.vin[0].prevout.n = 0;
75  tx.vout.resize(1);
76  tx.vout[0].nValue = 5000000000LL - 1000;
77  // This tx has a low fee: 1000 satoshis
78  uint256 hashParentTx = tx.GetHash(); // save this txid for later use
79  mempool.addUnchecked(hashParentTx, entry.Fee(1000).Time(GetTime()).SpendsCoinbaseOrCoinstake(true).FromTx(tx));
80 
81  // This tx has a medium fee: 10000 satoshis
82  tx.vin[0].prevout.hash = txFirst[1]->GetHash();
83  tx.vout[0].nValue = 5000000000LL - 10000;
84  uint256 hashMediumFeeTx = tx.GetHash();
85  mempool.addUnchecked(hashMediumFeeTx, entry.Fee(10000).Time(GetTime()).SpendsCoinbaseOrCoinstake(true).FromTx(tx));
86 
87  // This tx has a high fee, but depends on the first transaction
88  tx.vin[0].prevout.hash = hashParentTx;
89  tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 50k satoshi fee
90  uint256 hashHighFeeTx = tx.GetHash();
91  mempool.addUnchecked(hashHighFeeTx, entry.Fee(50000).Time(GetTime()).SpendsCoinbaseOrCoinstake(false).FromTx(tx));
92 
93  std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(chainparams, DEFAULT_PRINTPRIORITY).CreateNewBlock(scriptPubKey);
94 
95  BOOST_CHECK(pblocktemplate->block.vtx[1]->GetHash() == hashParentTx);
96  BOOST_CHECK(pblocktemplate->block.vtx[2]->GetHash() == hashHighFeeTx);
97  BOOST_CHECK(pblocktemplate->block.vtx[3]->GetHash() == hashMediumFeeTx);
98 
99  // Test that a package below the min relay fee doesn't get included
100  tx.vin[0].prevout.hash = hashHighFeeTx;
101  tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 0 fee
102  uint256 hashFreeTx = tx.GetHash();
103  mempool.addUnchecked(hashFreeTx, entry.Fee(0).FromTx(tx));
104  size_t freeTxSize = ::GetSerializeSize(tx, PROTOCOL_VERSION);
105 
106  // Calculate a fee on child transaction that will put the package just
107  // below the min relay fee (assuming 1 child tx of the same size).
108  CAmount feeToUse = minRelayTxFee.GetFee(2*freeTxSize) - 1;
109 
110  tx.vin[0].prevout.hash = hashFreeTx;
111  tx.vout[0].nValue = 5000000000LL - 1000 - 50000 - feeToUse;
112  uint256 hashLowFeeTx = tx.GetHash();
113  mempool.addUnchecked(hashLowFeeTx, entry.Fee(feeToUse).FromTx(tx));
114  pblocktemplate = BlockAssembler(chainparams, DEFAULT_PRINTPRIORITY).CreateNewBlock(scriptPubKey);
115  // Verify that the free tx and the low fee tx didn't get selected
116  for (size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
117  BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashFreeTx);
118  BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashLowFeeTx);
119  }
120 
121  // Test that packages above the min relay fee do get included, even if one
122  // of the transactions is below the min relay fee
123  // Remove the low fee transaction and replace with a higher fee transaction
125  tx.vout[0].nValue -= 2; // Now we should be just over the min relay fee
126  hashLowFeeTx = tx.GetHash();
127  mempool.addUnchecked(hashLowFeeTx, entry.Fee(feeToUse+2).FromTx(tx));
128  pblocktemplate = BlockAssembler(chainparams, DEFAULT_PRINTPRIORITY).CreateNewBlock(scriptPubKey);
129  BOOST_CHECK(pblocktemplate->block.vtx[4]->GetHash() == hashFreeTx);
130  BOOST_CHECK(pblocktemplate->block.vtx[5]->GetHash() == hashLowFeeTx);
131 
132  // Test that transaction selection properly updates ancestor fee
133  // calculations as ancestor transactions get included in a block.
134  // Add a 0-fee transaction that has 2 outputs.
135  tx.vin[0].prevout.hash = txFirst[2]->GetHash();
136  tx.vout.resize(2);
137  tx.vout[0].nValue = 5000000000LL - 100000000;
138  tx.vout[1].nValue = 100000000; // 1BTC output
139  uint256 hashFreeTx2 = tx.GetHash();
140  mempool.addUnchecked(hashFreeTx2, entry.Fee(0).SpendsCoinbaseOrCoinstake(true).FromTx(tx));
141 
142  // This tx can't be mined by itself
143  tx.vin[0].prevout.hash = hashFreeTx2;
144  tx.vout.resize(1);
145  feeToUse = minRelayTxFee.GetFee(freeTxSize);
146  tx.vout[0].nValue = 5000000000LL - 100000000 - feeToUse;
147  uint256 hashLowFeeTx2 = tx.GetHash();
148  mempool.addUnchecked(hashLowFeeTx2, entry.Fee(feeToUse).SpendsCoinbaseOrCoinstake(false).FromTx(tx));
149  pblocktemplate = BlockAssembler(chainparams, DEFAULT_PRINTPRIORITY).CreateNewBlock(scriptPubKey);
150 
151  // Verify that this tx isn't selected.
152  for (size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
153  BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashFreeTx2);
154  BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashLowFeeTx2);
155  }
156 
157  // This tx will be mineable, and should cause hashLowFeeTx2 to be selected
158  // as well.
159  tx.vin[0].prevout.n = 1;
160  tx.vout[0].nValue = 100000000 - 10000; // 10k satoshi fee
161  mempool.addUnchecked(tx.GetHash(), entry.Fee(10000).FromTx(tx));
162  pblocktemplate = BlockAssembler(chainparams, DEFAULT_PRINTPRIORITY).CreateNewBlock(scriptPubKey);
163  BOOST_CHECK(pblocktemplate->block.vtx[8]->GetHash() == hashLowFeeTx2);
164 }
165 
166 // NOTE: These tests rely on CreateNewBlock doing its own self-validation!
167 BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
168 {
169  // Note that by default, these tests run with size accounting enabled.
170  const CChainParams& chainparams = Params();
171  CScript scriptPubKey = CScript() << OP_DUP << OP_HASH160 << ParseHex("8d5b4f83212214d6ef693e02e6d71969fddad976") << OP_EQUALVERIFY << OP_CHECKSIG;
172 
173  std::unique_ptr<CBlockTemplate> pblocktemplate;
174  CMutableTransaction tx,tx2;
175  CScript script;
176  uint256 hash;
178  entry.nFee = 11;
179  entry.nHeight = 11;
180 
181  Checkpoints::fEnabled = false;
182 
183  // Simple block creation, nothing special yet:
184  BOOST_CHECK(pblocktemplate = BlockAssembler(Params(), DEFAULT_PRINTPRIORITY).CreateNewBlock(scriptPubKey, &m_wallet, false));
185  // Set genesis block
186  pblocktemplate->block.hashPrevBlock = chainparams.GetConsensus().hashGenesisBlock;
187 
188  // We can't make transactions until we have inputs
189  // Therefore, load 100 blocks :)
190  std::vector<CTransactionRef>txFirst;
191  std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>(pblocktemplate->block); // pointer for convenience
192  for (unsigned int i = 0; i < sizeof(blockinfo)/sizeof(*blockinfo); ++i) {
193  CBlockIndex* pindexPrev = WITH_LOCK(cs_main, return chainActive.Tip());
194  assert(pindexPrev);
195  pblock->nTime = pindexPrev->GetMedianTimePast() + 60;
196  pblock->vtx.clear(); // Update coinbase input height manually
197  CreateCoinbaseTx(pblock.get(), CScript(), pindexPrev);
198  if (txFirst.size() < 4)
199  txFirst.emplace_back(pblock->vtx[0]);
200  pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
201  pblock->nNonce = blockinfo[i].nonce;
202  BlockStateCatcherWrapper stateCatcher(pblock->GetHash());
203  stateCatcher.registerEvent();
204  BOOST_CHECK(ProcessNewBlock(pblock, nullptr));
206  BOOST_CHECK(stateCatcher.get().found && stateCatcher.get().state.IsValid());
207  pblock->hashPrevBlock = pblock->GetHash();
208  }
209 
210  // Just to make sure we can still make simple blocks
211  BOOST_CHECK(pblocktemplate = BlockAssembler(Params(), DEFAULT_PRINTPRIORITY).CreateNewBlock(scriptPubKey, &m_wallet, false));
212 
213  // block sigops > limit: 2000 CHECKMULTISIG + 1
214  tx.vin.resize(1);
215  // NOTE: OP_NOP is used to force 20 SigOps for the CHECKMULTISIG
216  tx.vin[0].scriptSig = CScript() << OP_0 << OP_0 << OP_0 << OP_NOP << OP_CHECKMULTISIG << OP_1;
217  tx.vin[0].prevout.hash = txFirst[0]->GetHash();
218  tx.vin[0].prevout.n = 0;
219  tx.vout.resize(1);
220  tx.vout[0].nValue = 5000000000LL;
221  for (unsigned int i = 0; i < 2001; ++i) {
222  tx.vout[0].nValue -= 1000000;
223  hash = tx.GetHash();
224  bool spendsCoinbase = (i == 0) ? true : false; // only first tx spends coinbase
225  // If we don't set the # of sig ops in the CTxMemPoolEntry, template creation fails
226  mempool.addUnchecked(hash, entry.Fee(1000000).Time(GetTime()).SpendsCoinbaseOrCoinstake(spendsCoinbase).FromTx(tx));
227  tx.vin[0].prevout.hash = hash;
228  }
229  BOOST_CHECK_EXCEPTION(pblocktemplate = BlockAssembler(Params(), DEFAULT_PRINTPRIORITY).CreateNewBlock(scriptPubKey, &m_wallet, false), std::runtime_error, HasReason("bad-blk-sigops"));
230  mempool.clear();
231 
232  tx.vin[0].prevout.hash = txFirst[0]->GetHash();
233  tx.vout[0].nValue = 5000000000LL;
234  for (unsigned int i = 0; i < 1001; ++i) {
235  tx.vout[0].nValue -= 1000000;
236  hash = tx.GetHash();
237  bool spendsCoinbase = (i == 0) ? true : false; // only first tx spends coinbase
238  // If we do set the # of sig ops in the CTxMemPoolEntry, template creation passes
239  mempool.addUnchecked(hash, entry.Fee(1000000).Time(GetTime()).SpendsCoinbaseOrCoinstake(spendsCoinbase).SigOps(20).FromTx(tx));
240  tx.vin[0].prevout.hash = hash;
241  }
242  BOOST_CHECK(pblocktemplate = BlockAssembler(Params(), DEFAULT_PRINTPRIORITY).CreateNewBlock(scriptPubKey, &m_wallet, false));
243  mempool.clear();
244 
245  // block size > limit
246  tx.vin[0].scriptSig = CScript();
247  // 18 * (520char + DROP) + OP_1 = 9433 bytes
248  std::vector<unsigned char> vchData(520);
249  for (unsigned int i = 0; i < 18; ++i)
250  tx.vin[0].scriptSig << vchData << OP_DROP;
251  tx.vin[0].scriptSig << OP_1;
252  tx.vin[0].prevout.hash = txFirst[0]->GetHash();
253  tx.vin[0].prevout.n = 0;
254  tx.vout[0].nValue = 5000000000LL;
255  for (unsigned int i = 0; i < 215; ++i) {
256  tx.vout[0].nValue -= 10000000;
257  hash = tx.GetHash();
258  bool spendsCoinbase = i == 0; // only first tx spends coinbase
259  mempool.addUnchecked(hash, entry.Fee(1000000).Time(GetTime()).SpendsCoinbaseOrCoinstake(spendsCoinbase).FromTx(tx));
260  tx.vin[0].prevout.hash = hash;
261  }
262  BOOST_CHECK(pblocktemplate = BlockAssembler(Params(), DEFAULT_PRINTPRIORITY).CreateNewBlock(scriptPubKey, &m_wallet, false));
263  mempool.clear();
264 
265  // orphan in mempool, template creation fails
266  hash = tx.GetHash();
267  mempool.addUnchecked(hash, entry.Fee(1000000).Time(GetTime()).FromTx(tx));
268  BOOST_CHECK_EXCEPTION(pblocktemplate = BlockAssembler(Params(), DEFAULT_PRINTPRIORITY).CreateNewBlock(scriptPubKey, &m_wallet, false), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
269  mempool.clear();
270 
271  // child with higher feerate than parent
272  tx.vin[0].scriptSig = CScript() << OP_1;
273  tx.vin[0].prevout.hash = txFirst[1]->GetHash();
274  tx.vout[0].nValue = 4900000000LL;
275  hash = tx.GetHash();
276  mempool.addUnchecked(hash, entry.Fee(100000000LL).Time(GetTime()).SpendsCoinbaseOrCoinstake(true).FromTx(tx));
277  tx.vin[0].prevout.hash = hash;
278  tx.vin.resize(2);
279  tx.vin[1].scriptSig = CScript() << OP_1;
280  tx.vin[1].prevout.hash = txFirst[0]->GetHash();
281  tx.vin[1].prevout.n = 0;
282  tx.vout[0].nValue = 5900000000LL;
283  hash = tx.GetHash();
284  mempool.addUnchecked(hash, entry.Fee(400000000LL).Time(GetTime()).SpendsCoinbaseOrCoinstake(true).FromTx(tx));
285  BOOST_CHECK(pblocktemplate = BlockAssembler(Params(), DEFAULT_PRINTPRIORITY).CreateNewBlock(scriptPubKey, &m_wallet, false));
286  mempool.clear();
287 
288  // coinbase in mempool, template creation fails
289  tx.vin.resize(1);
290  tx.vin[0].prevout.SetNull();
291  tx.vin[0].scriptSig = CScript() << OP_0 << OP_1;
292  tx.vout[0].nValue = 0;
293  hash = tx.GetHash();
294  // give it a fee so it'll get mined
295  mempool.addUnchecked(hash, entry.Fee(100000).Time(GetTime()).SpendsCoinbaseOrCoinstake(false).FromTx(tx));
296  BOOST_CHECK_EXCEPTION(pblocktemplate = BlockAssembler(Params(), DEFAULT_PRINTPRIORITY).CreateNewBlock(scriptPubKey, &m_wallet, false), std::runtime_error, HasReason("bad-cb-multiple"));
297  mempool.clear();
298 
299  // invalid (pre-p2sh) txn in mempool, template creation fails
300  tx.vin[0].prevout.hash = txFirst[0]->GetHash();
301  tx.vin[0].prevout.n = 0;
302  tx.vin[0].scriptSig = CScript() << OP_1;
303  tx.vout[0].nValue = 4900000000LL;
304  script = CScript() << OP_0;
305  tx.vout[0].scriptPubKey = GetScriptForDestination(CScriptID(script));
306  hash = tx.GetHash();
307  mempool.addUnchecked(hash, entry.Fee(10000000L).Time(GetTime()).SpendsCoinbaseOrCoinstake(true).FromTx(tx));
308  tx.vin[0].prevout.hash = hash;
309  tx.vin[0].scriptSig = CScript() << std::vector<unsigned char>(script.begin(), script.end());
310  tx.vout[0].nValue -= 1000000;
311  hash = tx.GetHash();
312  mempool.addUnchecked(hash, entry.Fee(1000000).Time(GetTime()).SpendsCoinbaseOrCoinstake(false).FromTx(tx));
313  // Should throw block-validation-failed
314  BOOST_CHECK_EXCEPTION(pblocktemplate = BlockAssembler(Params(), DEFAULT_PRINTPRIORITY).CreateNewBlock(scriptPubKey, &m_wallet, false), std::runtime_error, HasReason("block-validation-failed"));
315  mempool.clear();
316 
317  // double spend txn pair in mempool, template creation fails
318  tx.vin[0].prevout.hash = txFirst[0]->GetHash();
319  tx.vin[0].scriptSig = CScript() << OP_1;
320  tx.vout[0].nValue = 4900000000LL;
321  tx.vout[0].scriptPubKey = CScript() << OP_1;
322  hash = tx.GetHash();
323  mempool.addUnchecked(hash, entry.Fee(100000000L).Time(GetTime()).SpendsCoinbaseOrCoinstake(true).FromTx(tx));
324  tx.vout[0].scriptPubKey = CScript() << OP_2;
325  hash = tx.GetHash();
326  mempool.addUnchecked(hash, entry.Fee(100000000L).Time(GetTime()).SpendsCoinbaseOrCoinstake(true).FromTx(tx));
327  BOOST_CHECK_EXCEPTION(pblocktemplate = BlockAssembler(Params(), DEFAULT_PRINTPRIORITY).CreateNewBlock(scriptPubKey, &m_wallet, false), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
328  mempool.clear();
329 
330  // non-final txs in mempool
332 
333  // height locked
334  tx.vin[0].prevout.hash = txFirst[0]->GetHash();
335  tx.vin[0].scriptSig = CScript() << OP_1;
336  tx.vin[0].nSequence = 0;
337  tx.vout[0].nValue = 4900000000LL;
338  tx.vout[0].scriptPubKey = CScript() << OP_1;
339  tx.nLockTime = WITH_LOCK(cs_main, return chainActive.Tip()->nHeight+1);
340  hash = tx.GetHash();
341  mempool.addUnchecked(hash, entry.Fee(100000000L).Time(GetTime()).SpendsCoinbaseOrCoinstake(true).FromTx(tx));
342  { LOCK(cs_main); BOOST_CHECK(!CheckFinalTx(MakeTransactionRef(tx), LOCKTIME_MEDIAN_TIME_PAST)); }
343 
344  // time locked
345  tx2.vin.resize(1);
346  tx2.vin[0].prevout.hash = txFirst[1]->GetHash();
347  tx2.vin[0].prevout.n = 0;
348  tx2.vin[0].scriptSig = CScript() << OP_1;
349  tx2.vin[0].nSequence = 0;
350  tx2.vout.resize(1);
351  tx2.vout[0].nValue = 4900000000LL;
352  tx2.vout[0].scriptPubKey = CScript() << OP_1;
354  hash = tx2.GetHash();
355  mempool.addUnchecked(hash, entry.Fee(100000000L).Time(GetTime()).SpendsCoinbaseOrCoinstake(true).FromTx(tx2));
356  { LOCK(cs_main); BOOST_CHECK(!CheckFinalTx(MakeTransactionRef(tx2), LOCKTIME_MEDIAN_TIME_PAST)); }
357 
358  BOOST_CHECK(pblocktemplate = BlockAssembler(Params(), DEFAULT_PRINTPRIORITY).CreateNewBlock(scriptPubKey, &m_wallet, false));
359 
360  // Neither tx should have make it into the template.
361  BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 1);
362 
363  {
364  LOCK(cs_main);
365  // However if we advance height and time by one, both will.
366  chainActive.Tip()->nHeight++;
368  }
369 
370  // FIXME: we should *actually* create a new block so the following test
371  // works; CheckFinalTx() isn't fooled by monkey-patching nHeight.
372  //BOOST_CHECK(CheckFinalTx(tx));
373  //BOOST_CHECK(CheckFinalTx(tx2));
374 
375  BOOST_CHECK(pblocktemplate = BlockAssembler(Params(), DEFAULT_PRINTPRIORITY).CreateNewBlock(scriptPubKey, &m_wallet, false));
376  BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 3);
377 
379  SetMockTime(0);
380  mempool.clear();
381 
382  TestPackageSelection(chainparams, scriptPubKey, txFirst);
383 
384  Checkpoints::fEnabled = true;
385 }
386 
int64_t CAmount
Amount in PIV (Can be negative)
Definition: amount.h:13
CMutableTransaction CreateCoinbaseTx(const CScript &scriptPubKeyIn, CBlockIndex *pindexPrev)
const CChainParams & Params()
Return the currently selected parameters.
Generate a new block.
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn, CWallet *pwallet=nullptr, bool fProofOfStake=false, std::vector< CStakeableOutput > *availableCoins=nullptr, bool fNoMempoolTx=false, bool fTestValidity=true, CBlockIndex *prevBlock=nullptr, bool stopPoSOnNewBlock=true, bool fIncludeQfc=true)
Construct a new block template with coinbase to scriptPubKeyIn.
CValidationState state
BlockStateCatcher & get() const
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:139
int64_t GetMedianTimePast() const
Definition: chain.cpp:204
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:151
CBlockIndex * Tip(bool fProofOfStake=false) const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:405
CChainParams defines various tweakable parameters of a given instance of the PIVX system.
Definition: chainparams.h:43
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:72
CAmount GetFee(size_t size) const
Definition: feerate.cpp:21
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:381
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:24
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN)
Definition: txmempool.cpp:601
bool addUnchecked(const uint256 &hash, const CTxMemPoolEntry &entry, bool validFeeEstimate=true)
Definition: txmempool.cpp:1366
void clear()
Definition: txmempool.cpp:887
bool IsValid() const
Definition: validation.h:69
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: test_pivx.h:169
iterator begin()
Definition: prevector.h:285
iterator end()
Definition: prevector.h:287
256-bit opaque blob.
Definition: uint256.h:138
BOOST_AUTO_TEST_SUITE_END()
@ LOCK
Definition: lockunlock.h:16
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:154
BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
void TestPackageSelection(const CChainParams &chainparams, CScript scriptPubKey, std::vector< CTransactionRef > &txFirst)
Definition: miner_tests.cpp:63
unsigned char extranonce
Definition: miner_tests.cpp:27
unsigned int nonce
Definition: miner_tests.cpp:28
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:80
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
@ OP_2
Definition: script.h:61
@ OP_CHECKMULTISIG
Definition: script.h:168
@ OP_CHECKSIG
Definition: script.h:166
@ OP_DUP
Definition: script.h:101
@ OP_NOP
Definition: script.h:78
@ OP_HASH160
Definition: script.h:163
@ OP_1
Definition: script.h:59
@ OP_DROP
Definition: script.h:100
@ OP_0
Definition: script.h:52
@ OP_EQUALVERIFY
Definition: script.h:123
unsigned int GetSerializeSize(const std::array< T, N > &item)
array
Definition: serialize.h:847
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a PIVX scriptPubKey for the given CTxDestination.
Definition: standard.cpp:278
A mutable version of CTransaction.
Definition: transaction.h:409
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:96
std::vector< CTxOut > vout
Definition: transaction.h:411
std::vector< CTxIn > vin
Definition: transaction.h:410
uint256 hashGenesisBlock
Definition: params.h:172
Definition: test_pivx.h:137
TestMemPoolEntryHelper & SigOps(unsigned int _sigops)
Definition: test_pivx.h:157
CAmount nFee
Definition: test_pivx.h:139
TestMemPoolEntryHelper & SpendsCoinbaseOrCoinstake(bool _flag)
Definition: test_pivx.h:156
unsigned int nHeight
Definition: test_pivx.h:141
TestMemPoolEntryHelper & Time(int64_t _time)
Definition: test_pivx.h:154
TestMemPoolEntryHelper & Fee(CAmount _fee)
Definition: test_pivx.h:153
CTxMemPoolEntry FromTx(const CMutableTransaction &tx)
Definition: test_pivx.cpp:250
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:247
std::vector< unsigned char > ParseHex(const char *psz)
void SetMockTime(int64_t nMockTimeIn)
For testing.
Definition: utiltime.cpp:51
int64_t GetTime()
DEPRECATED Use either GetSystemTimeInSeconds (not mockable) or GetTime<T> (mockable)
Definition: utiltime.cpp:27
bool CheckFinalTx(const CTransactionRef &tx, int flags)
Check if transaction will be final in the next block to be created.
Definition: validation.cpp:224
CTxMemPool mempool(::minRelayTxFee)
CFeeRate minRelayTxFee
Fees smaller than this (in upiv) are considered zero fee (for relaying, mining and transaction creati...
Definition: validation.cpp:108
bool ProcessNewBlock(const std::shared_ptr< const CBlock > &pblock, const FlatFilePos *dbp)
Process an incoming block.
CChain chainActive
The currently-connected chain of blocks (protected by cs_main).
Definition: validation.cpp:84
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...