1 // Copyright (c) 2011-2014 The Bitcoin Core developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
11 #include <boost/test/unit_test.hpp>
13 BOOST_AUTO_TEST_SUITE(miner_tests)
17 unsigned char extranonce;
20 {4, 0xa4a3e223}, {2, 0x15c32f9e}, {1, 0x0375b547}, {1, 0x7004a8a5},
21 {2, 0xce440296}, {2, 0x52cfe198}, {1, 0x77a72cd0}, {2, 0xbb5d6f84},
22 {2, 0x83f30c2c}, {1, 0x48a73d5b}, {1, 0xef7dcd01}, {2, 0x6809c6c4},
23 {2, 0x0883ab3c}, {1, 0x087bbbe2}, {2, 0x2104a814}, {2, 0xdffb6daa},
24 {1, 0xee8a0a08}, {2, 0xba4237c1}, {1, 0xa70349dc}, {1, 0x344722bb},
25 {3, 0xd6294733}, {2, 0xec9f5c94}, {2, 0xca2fbc28}, {1, 0x6ba4f406},
26 {2, 0x015d4532}, {1, 0x6e119b7c}, {2, 0x43e8f314}, {2, 0x27962f38},
27 {2, 0xb571b51b}, {2, 0xb36bee23}, {2, 0xd17924a8}, {2, 0x6bc212d9},
28 {1, 0x630d4948}, {2, 0x9a4c4ebb}, {2, 0x554be537}, {1, 0xd63ddfc7},
29 {2, 0xa10acc11}, {1, 0x759a8363}, {2, 0xfb73090d}, {1, 0xe82c6a34},
30 {1, 0xe33e92d7}, {3, 0x658ef5cb}, {2, 0xba32ff22}, {5, 0x0227a10c},
31 {1, 0xa9a70155}, {5, 0xd096d809}, {1, 0x37176174}, {1, 0x830b8d0f},
32 {1, 0xc6e3910e}, {2, 0x823f3ca8}, {1, 0x99850849}, {1, 0x7521fb81},
33 {1, 0xaacaabab}, {1, 0xd645a2eb}, {5, 0x7aea1781}, {5, 0x9d6e4b78},
34 {1, 0x4ce90fd8}, {1, 0xabdc832d}, {6, 0x4a34f32a}, {2, 0xf2524c1c},
35 {2, 0x1bbeb08a}, {1, 0xad47f480}, {1, 0x9f026aeb}, {1, 0x15a95049},
36 {2, 0xd1cb95b2}, {2, 0xf84bbda5}, {1, 0x0fa62cd1}, {1, 0xe05f9169},
37 {1, 0x78d194a9}, {5, 0x3e38147b}, {5, 0x737ba0d4}, {1, 0x63378e10},
38 {1, 0x6d5f91cf}, {2, 0x88612eb8}, {2, 0xe9639484}, {1, 0xb7fabc9d},
39 {2, 0x19b01592}, {1, 0x5a90dd31}, {2, 0x5bd7e028}, {2, 0x94d00323},
40 {1, 0xa9b9c01a}, {1, 0x3a40de61}, {1, 0x56e7eec7}, {5, 0x859f7ef6},
41 {1, 0xfd8e5630}, {1, 0x2b0c9f7f}, {1, 0xba700e26}, {1, 0x7170a408},
42 {1, 0x70de86a8}, {1, 0x74d64cd5}, {1, 0x49e738a1}, {2, 0x6910b602},
43 {0, 0x643c565f}, {1, 0x54264b3f}, {2, 0x97ea6396}, {2, 0x55174459},
44 {2, 0x03e8779a}, {1, 0x98f34d8f}, {1, 0xc07b2b07}, {1, 0xdfe29668},
45 {1, 0x3141c7c1}, {1, 0xb3b595f4}, {1, 0x735abf08}, {5, 0x623bfbce},
46 {2, 0xd351e722}, {1, 0xf4ca48c9}, {1, 0x5b19c670}, {1, 0xa164bf0e},
47 {2, 0xbbbeb305}, {2, 0xfe1c810a},
50 // NOTE: These tests rely on CreateNewBlock doing its own self-validation!
51 BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
53 CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
54 CBlockTemplate *pblocktemplate;
55 CMutableTransaction tx,tx2;
60 Checkpoints::fEnabled = false;
62 // Simple block creation, nothing special yet:
63 BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
65 // We can't make transactions until we have inputs
66 // Therefore, load 100 blocks :)
67 std::vector<CTransaction*>txFirst;
68 for (unsigned int i = 0; i < sizeof(blockinfo)/sizeof(*blockinfo); ++i)
70 CBlock *pblock = &pblocktemplate->block; // pointer for convenience
72 pblock->nTime = chainActive.Tip()->GetMedianTimePast()+1;
73 CMutableTransaction txCoinbase(pblock->vtx[0]);
74 txCoinbase.vin[0].scriptSig = CScript();
75 txCoinbase.vin[0].scriptSig.push_back(blockinfo[i].extranonce);
76 txCoinbase.vin[0].scriptSig.push_back(chainActive.Height());
77 txCoinbase.vout[0].scriptPubKey = CScript();
78 pblock->vtx[0] = CTransaction(txCoinbase);
79 if (txFirst.size() < 2)
80 txFirst.push_back(new CTransaction(pblock->vtx[0]));
81 pblock->hashMerkleRoot = pblock->BuildMerkleTree();
82 pblock->nNonce = blockinfo[i].nonce;
83 CValidationState state;
84 BOOST_CHECK(ProcessNewBlock(state, NULL, pblock));
85 BOOST_CHECK(state.IsValid());
86 pblock->hashPrevBlock = pblock->GetHash();
88 delete pblocktemplate;
90 // Just to make sure we can still make simple blocks
91 BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
92 delete pblocktemplate;
94 // block sigops > limit: 1000 CHECKMULTISIG + 1
96 // NOTE: OP_NOP is used to force 20 SigOps for the CHECKMULTISIG
97 tx.vin[0].scriptSig = CScript() << OP_0 << OP_0 << OP_0 << OP_NOP << OP_CHECKMULTISIG << OP_1;
98 tx.vin[0].prevout.hash = txFirst[0]->GetHash();
99 tx.vin[0].prevout.n = 0;
101 tx.vout[0].nValue = 5000000000LL;
102 for (unsigned int i = 0; i < 1001; ++i)
104 tx.vout[0].nValue -= 1000000;
106 mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
107 tx.vin[0].prevout.hash = hash;
109 BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
110 delete pblocktemplate;
113 // block size > limit
114 tx.vin[0].scriptSig = CScript();
115 // 18 * (520char + DROP) + OP_1 = 9433 bytes
116 std::vector<unsigned char> vchData(520);
117 for (unsigned int i = 0; i < 18; ++i)
118 tx.vin[0].scriptSig << vchData << OP_DROP;
119 tx.vin[0].scriptSig << OP_1;
120 tx.vin[0].prevout.hash = txFirst[0]->GetHash();
121 tx.vout[0].nValue = 5000000000LL;
122 for (unsigned int i = 0; i < 128; ++i)
124 tx.vout[0].nValue -= 10000000;
126 mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
127 tx.vin[0].prevout.hash = hash;
129 BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
130 delete pblocktemplate;
135 mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
136 BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
137 delete pblocktemplate;
140 // child with higher priority than parent
141 tx.vin[0].scriptSig = CScript() << OP_1;
142 tx.vin[0].prevout.hash = txFirst[1]->GetHash();
143 tx.vout[0].nValue = 4900000000LL;
145 mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
146 tx.vin[0].prevout.hash = hash;
148 tx.vin[1].scriptSig = CScript() << OP_1;
149 tx.vin[1].prevout.hash = txFirst[0]->GetHash();
150 tx.vin[1].prevout.n = 0;
151 tx.vout[0].nValue = 5900000000LL;
153 mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
154 BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
155 delete pblocktemplate;
158 // coinbase in mempool
160 tx.vin[0].prevout.SetNull();
161 tx.vin[0].scriptSig = CScript() << OP_0 << OP_1;
162 tx.vout[0].nValue = 0;
164 mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
165 BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
166 delete pblocktemplate;
169 // invalid (pre-p2sh) txn in mempool
170 tx.vin[0].prevout.hash = txFirst[0]->GetHash();
171 tx.vin[0].prevout.n = 0;
172 tx.vin[0].scriptSig = CScript() << OP_1;
173 tx.vout[0].nValue = 4900000000LL;
174 script = CScript() << OP_0;
175 tx.vout[0].scriptPubKey = GetScriptForDestination(CScriptID(script));
177 mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
178 tx.vin[0].prevout.hash = hash;
179 tx.vin[0].scriptSig = CScript() << (std::vector<unsigned char>)script;
180 tx.vout[0].nValue -= 1000000;
182 mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
183 BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
184 delete pblocktemplate;
187 // double spend txn pair in mempool
188 tx.vin[0].prevout.hash = txFirst[0]->GetHash();
189 tx.vin[0].scriptSig = CScript() << OP_1;
190 tx.vout[0].nValue = 4900000000LL;
191 tx.vout[0].scriptPubKey = CScript() << OP_1;
193 mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
194 tx.vout[0].scriptPubKey = CScript() << OP_2;
196 mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
197 BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
198 delete pblocktemplate;
202 int nHeight = chainActive.Height();
203 chainActive.Tip()->nHeight = 209999;
204 BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
205 delete pblocktemplate;
206 chainActive.Tip()->nHeight = 210000;
207 BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
208 delete pblocktemplate;
209 chainActive.Tip()->nHeight = nHeight;
211 // non-final txs in mempool
212 SetMockTime(chainActive.Tip()->GetMedianTimePast()+1);
215 tx.vin[0].prevout.hash = txFirst[0]->GetHash();
216 tx.vin[0].scriptSig = CScript() << OP_1;
217 tx.vin[0].nSequence = 0;
218 tx.vout[0].nValue = 4900000000LL;
219 tx.vout[0].scriptPubKey = CScript() << OP_1;
220 tx.nLockTime = chainActive.Tip()->nHeight+1;
222 mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
223 BOOST_CHECK(!IsFinalTx(tx, chainActive.Tip()->nHeight + 1));
227 tx2.vin[0].prevout.hash = txFirst[1]->GetHash();
228 tx2.vin[0].prevout.n = 0;
229 tx2.vin[0].scriptSig = CScript() << OP_1;
230 tx2.vin[0].nSequence = 0;
232 tx2.vout[0].nValue = 4900000000LL;
233 tx2.vout[0].scriptPubKey = CScript() << OP_1;
234 tx2.nLockTime = chainActive.Tip()->GetMedianTimePast()+1;
235 hash = tx2.GetHash();
236 mempool.addUnchecked(hash, CTxMemPoolEntry(tx2, 11, GetTime(), 111.0, 11));
237 BOOST_CHECK(!IsFinalTx(tx2));
239 BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
241 // Neither tx should have make it into the template.
242 BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 1);
243 delete pblocktemplate;
245 // However if we advance height and time by one, both will.
246 chainActive.Tip()->nHeight++;
247 SetMockTime(chainActive.Tip()->GetMedianTimePast()+2);
249 BOOST_CHECK(IsFinalTx(tx, chainActive.Tip()->nHeight + 1));
250 BOOST_CHECK(IsFinalTx(tx2));
252 BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
253 BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 3);
254 delete pblocktemplate;
256 chainActive.Tip()->nHeight--;
260 BOOST_FOREACH(CTransaction *tx, txFirst)
263 Checkpoints::fEnabled = true;
266 BOOST_AUTO_TEST_SUITE_END()