Debian Codegraph @

tests/core_tests/block_validation.cpp

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31 #include "chaingen.h"
32 #include "block_validation.h"
33
34 using namespace epee;
35 using namespace cryptonote;
36
37 namespace
38 {
39 bool lift_up_difficulty(std::vector<test_event_entry>& events, std::vector<uint64_t>& timestamps,
40 std::vector<difficulty_type>& cummulative_difficulties, test_generator& generator,
41 size_t new_block_count, const block &blk_last, const account_base& miner_account)
42 {
43 difficulty_type commulative_diffic = cummulative_difficulties.empty() ? 0 : cummulative_difficulties.back();
44 block blk_prev = blk_last;
45 for (size_t i = 0; i < new_block_count; ++i)
46 {
47 block blk_next;
48 difficulty_type diffic = next_difficulty(timestamps, cummulative_difficulties,DIFFICULTY_TARGET_V1);
49 if (!generator.construct_block_manually(blk_next, blk_prev, miner_account,
50 test_generator::bf_timestamp | test_generator::bf_diffic, 0, 0, blk_prev.timestamp, crypto::hash(), diffic))
51 return false;
52
53 commulative_diffic += diffic;
54 if (timestamps.size() == DIFFICULTY_WINDOW)
55 {
56 timestamps.erase(timestamps.begin());
57 cummulative_difficulties.erase(cummulative_difficulties.begin());
58 }
59 timestamps.push_back(blk_next.timestamp);
60 cummulative_difficulties.push_back(commulative_diffic);
61
62 events.push_back(blk_next);
63 blk_prev = blk_next;
64 }
65
66 return true;
67 }
68 }
69
70 #define BLOCK_VALIDATION_INIT_GENERATE() \
71 GENERATE_ACCOUNT(miner_account); \
72 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, 1338224400);
73
74 //----------------------------------------------------------------------------------------------------------------------
75 // Tests
76
77 bool gen_block_big_major_version::generate(std::vector<test_event_entry>& events) const
78 {
79 BLOCK_VALIDATION_INIT_GENERATE();
80
81 block blk_1;
82 generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_major_ver, 255);
83 events.push_back(blk_1);
84
85 DO_CALLBACK(events, "check_block_purged");
86
87 return true;
88 }
89
90 bool gen_block_big_minor_version::generate(std::vector<test_event_entry>& events) const
91 {
92 BLOCK_VALIDATION_INIT_GENERATE();
93
94 block blk_1;
95 generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_minor_ver, 0, 255);
96 events.push_back(blk_1);
97
98 DO_CALLBACK(events, "check_block_accepted");
99
100 return true;
101 }
102
103 bool gen_block_ts_not_checked::generate(std::vector<test_event_entry>& events) const
104 {
105 BLOCK_VALIDATION_INIT_GENERATE();
106 REWIND_BLOCKS_N(events, blk_0r, blk_0, miner_account, BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW - 2);
107
108 block blk_1;
109 generator.construct_block_manually(blk_1, blk_0r, miner_account, test_generator::bf_timestamp, 0, 0, blk_0.timestamp - 60 * 60);
110 events.push_back(blk_1);
111
112 DO_CALLBACK(events, "check_block_accepted");
113
114 return true;
115 }
116
117 bool gen_block_ts_in_past::generate(std::vector<test_event_entry>& events) const
118 {
119 BLOCK_VALIDATION_INIT_GENERATE();
120 REWIND_BLOCKS_N(events, blk_0r, blk_0, miner_account, BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW - 1);
121
122 uint64_t ts_below_median = boost::get<block>(events[BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW / 2 - 1]).timestamp;
123 block blk_1;
124 generator.construct_block_manually(blk_1, blk_0r, miner_account, test_generator::bf_timestamp, 0, 0, ts_below_median);
125 events.push_back(blk_1);
126
127 DO_CALLBACK(events, "check_block_purged");
128
129 return true;
130 }
131
132 bool gen_block_ts_in_future::generate(std::vector<test_event_entry>& events) const
133 {
134 BLOCK_VALIDATION_INIT_GENERATE();
135
136 block blk_1;
137 generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_timestamp, 0, 0, time(NULL) + 60*60 + CRYPTONOTE_BLOCK_FUTURE_TIME_LIMIT);
138 events.push_back(blk_1);
139
140 DO_CALLBACK(events, "check_block_purged");
141
142 return true;
143 }
144
145 bool gen_block_invalid_prev_id::generate(std::vector<test_event_entry>& events) const
146 {
147 BLOCK_VALIDATION_INIT_GENERATE();
148
149 block blk_1;
150 crypto::hash prev_id = get_block_hash(blk_0);
151 reinterpret_cast<char &>(prev_id) ^= 1;
152 generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_prev_id, 0, 0, 0, prev_id);
153 events.push_back(blk_1);
154
155 DO_CALLBACK(events, "check_block_purged");
156
157 return true;
158 }
159
160 bool gen_block_invalid_prev_id::check_block_verification_context(const cryptonote::block_verification_context& bvc, size_t event_idx, const cryptonote::block& /*blk*/)
161 {
162 if (1 == event_idx)
163 return bvc.m_marked_as_orphaned && !bvc.m_added_to_main_chain && !bvc.m_verifivation_failed;
164 else
165 return !bvc.m_marked_as_orphaned && bvc.m_added_to_main_chain && !bvc.m_verifivation_failed;
166 }
167
168 bool gen_block_invalid_nonce::generate(std::vector<test_event_entry>& events) const
169 {
170 BLOCK_VALIDATION_INIT_GENERATE();
171
172 std::vector<uint64_t> timestamps;
173 std::vector<difficulty_type> commulative_difficulties;
174 if (!lift_up_difficulty(events, timestamps, commulative_difficulties, generator, 2, blk_0, miner_account))
175 return false;
176
177 // Create invalid nonce
178 difficulty_type diffic = next_difficulty(timestamps, commulative_difficulties,DIFFICULTY_TARGET_V1);
179 assert(1 < diffic);
180 const block& blk_last = boost::get<block>(events.back());
181 uint64_t timestamp = blk_last.timestamp;
182 block blk_3;
183 do
184 {
185 ++timestamp;
186 blk_3.miner_tx.set_null();
187 if (!generator.construct_block_manually(blk_3, blk_last, miner_account,
188 test_generator::bf_diffic | test_generator::bf_timestamp, 0, 0, timestamp, crypto::hash(), diffic))
189 return false;
190 }
191 while (0 == blk_3.nonce);
192 --blk_3.nonce;
193 events.push_back(blk_3);
194
195 return true;
196 }
197
198 bool gen_block_no_miner_tx::generate(std::vector<test_event_entry>& events) const
199 {
200 BLOCK_VALIDATION_INIT_GENERATE();
201
202 transaction miner_tx;
203 miner_tx.set_null();
204
205 block blk_1;
206 generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx);
207 events.push_back(blk_1);
208
209 DO_CALLBACK(events, "check_block_purged");
210
211 return true;
212 }
213
214 bool gen_block_unlock_time_is_low::generate(std::vector<test_event_entry>& events) const
215 {
216 BLOCK_VALIDATION_INIT_GENERATE();
217
218 MAKE_MINER_TX_MANUALLY(miner_tx, blk_0);
219 --miner_tx.unlock_time;
220
221 block blk_1;
222 generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx);
223 events.push_back(blk_1);
224
225 DO_CALLBACK(events, "check_block_purged");
226
227 return true;
228 }
229
230 bool gen_block_unlock_time_is_high::generate(std::vector<test_event_entry>& events) const
231 {
232 BLOCK_VALIDATION_INIT_GENERATE();
233
234 MAKE_MINER_TX_MANUALLY(miner_tx, blk_0);
235 ++miner_tx.unlock_time;
236
237 block blk_1;
238 generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx);
239 events.push_back(blk_1);
240
241 DO_CALLBACK(events, "check_block_purged");
242
243 return true;
244 }
245
246 bool gen_block_unlock_time_is_timestamp_in_past::generate(std::vector<test_event_entry>& events) const
247 {
248 BLOCK_VALIDATION_INIT_GENERATE();
249
250 MAKE_MINER_TX_MANUALLY(miner_tx, blk_0);
251 miner_tx.unlock_time = blk_0.timestamp - 10 * 60;
252
253 block blk_1;
254 generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx);
255 events.push_back(blk_1);
256
257 DO_CALLBACK(events, "check_block_purged");
258
259 return true;
260 }
261
262 bool gen_block_unlock_time_is_timestamp_in_future::generate(std::vector<test_event_entry>& events) const
263 {
264 BLOCK_VALIDATION_INIT_GENERATE();
265
266 MAKE_MINER_TX_MANUALLY(miner_tx, blk_0);
267 miner_tx.unlock_time = blk_0.timestamp + 3 * CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW * DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN;
268
269 block blk_1;
270 generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx);
271 events.push_back(blk_1);
272
273 DO_CALLBACK(events, "check_block_purged");
274
275 return true;
276 }
277
278 bool gen_block_height_is_low::generate(std::vector<test_event_entry>& events) const
279 {
280 BLOCK_VALIDATION_INIT_GENERATE();
281
282 MAKE_MINER_TX_MANUALLY(miner_tx, blk_0);
283 boost::get<txin_gen>(miner_tx.vin[0]).height--;
284
285 block blk_1;
286 generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx);
287 events.push_back(blk_1);
288
289 DO_CALLBACK(events, "check_block_purged");
290
291 return true;
292 }
293
294 bool gen_block_height_is_high::generate(std::vector<test_event_entry>& events) const
295 {
296 BLOCK_VALIDATION_INIT_GENERATE();
297
298 MAKE_MINER_TX_MANUALLY(miner_tx, blk_0);
299 boost::get<txin_gen>(miner_tx.vin[0]).height++;
300
301 block blk_1;
302 generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx);
303 events.push_back(blk_1);
304
305 DO_CALLBACK(events, "check_block_purged");
306
307 return true;
308 }
309
310 bool gen_block_miner_tx_has_2_tx_gen_in::generate(std::vector<test_event_entry>& events) const
311 {
312 BLOCK_VALIDATION_INIT_GENERATE();
313
314 MAKE_MINER_TX_MANUALLY(miner_tx, blk_0);
315
316 txin_gen in;
317 in.height = get_block_height(blk_0) + 1;
318 miner_tx.vin.push_back(in);
319
320 block blk_1;
321 generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx);
322 events.push_back(blk_1);
323
324 DO_CALLBACK(events, "check_block_purged");
325
326 return true;
327 }
328
329 bool gen_block_miner_tx_has_2_in::generate(std::vector<test_event_entry>& events) const
330 {
331 BLOCK_VALIDATION_INIT_GENERATE();
332 REWIND_BLOCKS(events, blk_0r, blk_0, miner_account);
333
334 GENERATE_ACCOUNT(alice);
335
336 tx_source_entry se;
337 se.amount = blk_0.miner_tx.vout[0].amount;
338 se.push_output(0, boost::get<txout_to_key>(blk_0.miner_tx.vout[0].target).key, se.amount);
339 se.real_output = 0;
340 se.rct = false;
341 se.real_out_tx_key = get_tx_pub_key_from_extra(blk_0.miner_tx);
342 se.real_output_in_tx_index = 0;
343 std::vector<tx_source_entry> sources;
344 sources.push_back(se);
345
346 tx_destination_entry de;
347 de.addr = miner_account.get_keys().m_account_address;
348 de.amount = se.amount;
349 std::vector<tx_destination_entry> destinations;
350 destinations.push_back(de);
351
352 transaction tmp_tx;
353 if (!construct_tx(miner_account.get_keys(), sources, destinations, boost::none, std::vector<uint8_t>(), tmp_tx))
354 return false;
355
356 MAKE_MINER_TX_MANUALLY(miner_tx, blk_0);
357 miner_tx.vin.push_back(tmp_tx.vin[0]);
358
359 block blk_1;
360 generator.construct_block_manually(blk_1, blk_0r, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx);
361 events.push_back(blk_1);
362
363 DO_CALLBACK(events, "check_block_purged");
364
365 return true;
366 }
367
368 bool gen_block_miner_tx_with_txin_to_key::generate(std::vector<test_event_entry>& events) const
369 {
370 BLOCK_VALIDATION_INIT_GENERATE();
371
372 // This block has only one output
373 block blk_1;
374 generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_none);
375 events.push_back(blk_1);
376
377 REWIND_BLOCKS(events, blk_1r, blk_1, miner_account);
378
379 tx_source_entry se;
380 se.amount = blk_1.miner_tx.vout[0].amount;
381 se.push_output(0, boost::get<txout_to_key>(blk_1.miner_tx.vout[0].target).key, se.amount);
382 se.real_output = 0;
383 se.rct = false;
384 se.real_out_tx_key = get_tx_pub_key_from_extra(blk_1.miner_tx);
385 se.real_output_in_tx_index = 0;
386 std::vector<tx_source_entry> sources;
387 sources.push_back(se);
388
389 tx_destination_entry de;
390 de.addr = miner_account.get_keys().m_account_address;
391 de.amount = se.amount;
392 std::vector<tx_destination_entry> destinations;
393 destinations.push_back(de);
394
395 transaction tmp_tx;
396 if (!construct_tx(miner_account.get_keys(), sources, destinations, boost::none, std::vector<uint8_t>(), tmp_tx))
397 return false;
398
399 MAKE_MINER_TX_MANUALLY(miner_tx, blk_1);
400 miner_tx.vin[0] = tmp_tx.vin[0];
401
402 block blk_2;
403 generator.construct_block_manually(blk_2, blk_1r, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx);
404 events.push_back(blk_2);
405
406 DO_CALLBACK(events, "check_block_purged");
407
408 return true;
409 }
410
411 bool gen_block_miner_tx_out_is_small::generate(std::vector<test_event_entry>& events) const
412 {
413 BLOCK_VALIDATION_INIT_GENERATE();
414
415 MAKE_MINER_TX_MANUALLY(miner_tx, blk_0);
416 miner_tx.vout[0].amount /= 2;
417
418 block blk_1;
419 generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx);
420 events.push_back(blk_1);
421
422 DO_CALLBACK(events, "check_block_purged");
423
424 return true;
425 }
426
427 bool gen_block_miner_tx_out_is_big::generate(std::vector<test_event_entry>& events) const
428 {
429 BLOCK_VALIDATION_INIT_GENERATE();
430
431 MAKE_MINER_TX_MANUALLY(miner_tx, blk_0);
432 miner_tx.vout[0].amount *= 2;
433
434 block blk_1;
435 generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx);
436 events.push_back(blk_1);
437
438 DO_CALLBACK(events, "check_block_purged");
439
440 return true;
441 }
442
443 bool gen_block_miner_tx_has_no_out::generate(std::vector<test_event_entry>& events) const
444 {
445 BLOCK_VALIDATION_INIT_GENERATE();
446
447 MAKE_MINER_TX_MANUALLY(miner_tx, blk_0);
448 miner_tx.vout.clear();
449
450 block blk_1;
451 generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx);
452 events.push_back(blk_1);
453
454 DO_CALLBACK(events, "check_block_purged");
455
456 return true;
457 }
458
459 bool gen_block_miner_tx_has_out_to_alice::generate(std::vector<test_event_entry>& events) const
460 {
461 BLOCK_VALIDATION_INIT_GENERATE();
462
463 GENERATE_ACCOUNT(alice);
464
465 keypair txkey;
466 MAKE_MINER_TX_AND_KEY_MANUALLY(miner_tx, blk_0, &txkey);
467
468 crypto::key_derivation derivation;
469 crypto::public_key out_eph_public_key;
470 crypto::generate_key_derivation(alice.get_keys().m_account_address.m_view_public_key, txkey.sec, derivation);
471 crypto::derive_public_key(derivation, 1, alice.get_keys().m_account_address.m_spend_public_key, out_eph_public_key);
472
473 tx_out out_to_alice;
474 out_to_alice.amount = miner_tx.vout[0].amount / 2;
475 miner_tx.vout[0].amount -= out_to_alice.amount;
476 out_to_alice.target = txout_to_key(out_eph_public_key);
477 miner_tx.vout.push_back(out_to_alice);
478
479 block blk_1;
480 generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx);
481 events.push_back(blk_1);
482
483 DO_CALLBACK(events, "check_block_accepted");
484
485 return true;
486 }
487
488 bool gen_block_has_invalid_tx::generate(std::vector<test_event_entry>& events) const
489 {
490 BLOCK_VALIDATION_INIT_GENERATE();
491
492 std::vector<crypto::hash> tx_hashes;
493 tx_hashes.push_back(crypto::hash());
494
495 block blk_1;
496 generator.construct_block_manually_tx(blk_1, blk_0, miner_account, tx_hashes, 0);
497 events.push_back(blk_1);
498
499 DO_CALLBACK(events, "check_block_purged");
500
501 return true;
502 }
503
504 bool gen_block_is_too_big::generate(std::vector<test_event_entry>& events) const
505 {
506 BLOCK_VALIDATION_INIT_GENERATE();
507
508 // Creating a huge miner_tx, it will have a lot of outs
509 MAKE_MINER_TX_MANUALLY(miner_tx, blk_0);
510 static const size_t tx_out_count = CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1 / 2;
511 uint64_t amount = get_outs_money_amount(miner_tx);
512 uint64_t portion = amount / tx_out_count;
513 uint64_t remainder = amount % tx_out_count;
514 txout_target_v target = miner_tx.vout[0].target;
515 miner_tx.vout.clear();
516 for (size_t i = 0; i < tx_out_count; ++i)
517 {
518 tx_out o;
519 o.amount = portion;
520 o.target = target;
521 miner_tx.vout.push_back(o);
522 }
523 if (0 < remainder)
524 {
525 tx_out o;
526 o.amount = remainder;
527 o.target = target;
528 miner_tx.vout.push_back(o);
529 }
530
531 // Block reward will be incorrect, as it must be reduced if cumulative block size is very big,
532 // but in this test it doesn't matter
533 block blk_1;
534 if (!generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx))
535 return false;
536
537 events.push_back(blk_1);
538
539 DO_CALLBACK(events, "check_block_purged");
540
541 return true;
542 }
543
544 gen_block_invalid_binary_format::gen_block_invalid_binary_format()
545 : m_corrupt_blocks_begin_idx(0)
546 {
547 REGISTER_CALLBACK("check_all_blocks_purged", gen_block_invalid_binary_format::check_all_blocks_purged);
548 REGISTER_CALLBACK("corrupt_blocks_boundary", gen_block_invalid_binary_format::corrupt_blocks_boundary);
549 }
550
551 bool gen_block_invalid_binary_format::generate(std::vector<test_event_entry>& events) const
552 {
553 BLOCK_VALIDATION_INIT_GENERATE();
554
555 std::vector<uint64_t> timestamps;
556 std::vector<difficulty_type> cummulative_difficulties;
557 difficulty_type cummulative_diff = 1;
558
559 // Unlock blk_0 outputs
560 block blk_last = blk_0;
561 assert(CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW < DIFFICULTY_WINDOW);
562 for (size_t i = 0; i < CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW; ++i)
563 {
564 MAKE_NEXT_BLOCK(events, blk_curr, blk_last, miner_account);
565 timestamps.push_back(blk_curr.timestamp);
566 cummulative_difficulties.push_back(++cummulative_diff);
567 blk_last = blk_curr;
568 }
569
570 // Lifting up takes a while
571 difficulty_type diffic;
572 do
573 {
574 blk_last = boost::get<block>(events.back());
575 diffic = next_difficulty(timestamps, cummulative_difficulties,DIFFICULTY_TARGET_V1);
576 if (!lift_up_difficulty(events, timestamps, cummulative_difficulties, generator, 1, blk_last, miner_account))
577 return false;
578 std::cout << "Block #" << events.size() << ", difficulty: " << diffic << std::endl;
579 }
580 while (diffic < 1500);
581
582 blk_last = boost::get<block>(events.back());
583 MAKE_TX(events, tx_0, miner_account, miner_account, MK_COINS(30), boost::get<block>(events[1]));
584 DO_CALLBACK(events, "corrupt_blocks_boundary");
585
586 block blk_test;
587 std::vector<crypto::hash> tx_hashes;
588 tx_hashes.push_back(get_transaction_hash(tx_0));
589 size_t txs_weight = get_transaction_weight(tx_0);
590 diffic = next_difficulty(timestamps, cummulative_difficulties,DIFFICULTY_TARGET_V1);
591 if (!generator.construct_block_manually(blk_test, blk_last, miner_account,
592 test_generator::bf_diffic | test_generator::bf_timestamp | test_generator::bf_tx_hashes, 0, 0, blk_last.timestamp,
593 crypto::hash(), diffic, transaction(), tx_hashes, txs_weight))
594 return false;
595
596 blobdata blob = t_serializable_object_to_blob(blk_test);
597 for (size_t i = 0; i < blob.size(); ++i)
598 {
599 for (size_t bit_idx = 0; bit_idx < sizeof(blobdata::value_type) * 8; ++bit_idx)
600 {
601 serialized_block sr_block(blob);
602 blobdata::value_type& ch = sr_block.data[i];
603 ch ^= 1 << bit_idx;
604
605 events.push_back(sr_block);
606 }
607 }
608
609 DO_CALLBACK(events, "check_all_blocks_purged");
610
611 return true;
612 }
613
614 bool gen_block_invalid_binary_format::check_block_verification_context(const cryptonote::block_verification_context& bvc,
615 size_t event_idx, const cryptonote::block& blk)
616 {
617 if (0 == m_corrupt_blocks_begin_idx || event_idx < m_corrupt_blocks_begin_idx)
618 {
619 return bvc.m_added_to_main_chain;
620 }
621 else
622 {
623 return (!bvc.m_added_to_main_chain && (bvc.m_already_exists || bvc.m_marked_as_orphaned || bvc.m_verifivation_failed))
624 || (bvc.m_added_to_main_chain && bvc.m_partial_block_reward);
625 }
626 }
627
628 bool gen_block_invalid_binary_format::corrupt_blocks_boundary(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events)
629 {
630 m_corrupt_blocks_begin_idx = ev_index + 1;
631 return true;
632 }
633
634 bool gen_block_invalid_binary_format::check_all_blocks_purged(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events)
635 {
636 DEFINE_TESTS_ERROR_CONTEXT("gen_block_invalid_binary_format::check_all_blocks_purged");
637
638 CHECK_EQ(1, c.get_pool_transactions_count());
639 CHECK_EQ(m_corrupt_blocks_begin_idx - 2, c.get_current_blockchain_height());
640
641 return true;
642 }
643
644 bool gen_block_late_v1_coinbase_tx::generate(std::vector<test_event_entry>& events) const
645 {
646 BLOCK_VALIDATION_INIT_GENERATE();
647
648 block blk_1;
649 generator.construct_block_manually(blk_1, blk_0, miner_account,
650 test_generator::bf_major_ver | test_generator::bf_minor_ver,
651 HF_VERSION_MIN_V2_COINBASE_TX, HF_VERSION_MIN_V2_COINBASE_TX);
652 events.push_back(blk_1);
653
654 DO_CALLBACK(events, "check_block_purged");
655
656 return true;
657 }
658
659 bool gen_block_low_coinbase::generate(std::vector<test_event_entry>& events) const
660 {
661 BLOCK_VALIDATION_INIT_GENERATE();
662
663 block blk_1;
664 std::vector<size_t> block_weights;
665 generator.construct_block(blk_1, cryptonote::get_block_height(blk_0) + 1, cryptonote::get_block_hash(blk_0),
666 miner_account, blk_0.timestamp + DIFFICULTY_TARGET_V2, COIN + generator.get_already_generated_coins(cryptonote::get_block_hash(blk_0)),
667 block_weights, {}, HF_VERSION_EXACT_COINBASE);
668 events.push_back(blk_1);
669
670 DO_CALLBACK(events, "check_block_purged");
671
672 return true;
673 }
674
675 bool gen_block_miner_tx_out_has_no_view_tag_before_hf_view_tags::generate(std::vector<test_event_entry>& events) const
676 {
677 bool use_view_tags = false;
678
679 BLOCK_VALIDATION_INIT_GENERATE();
680
681 MAKE_MINER_TX_MANUALLY(miner_tx, blk_0);
682
683 CHECK_AND_ASSERT_MES(!cryptonote::get_output_view_tag(miner_tx.vout[0]), false, "output should not have a view tag");
684
685 crypto::public_key output_public_key;
686 crypto::view_tag view_tag;
687 cryptonote::get_output_public_key(miner_tx.vout[0], output_public_key);
688
689 // explicitly call the setter to ensure it does not set a view tag on the miner tx output
690 cryptonote::set_tx_out(miner_tx.vout[0].amount, output_public_key, use_view_tags, view_tag, miner_tx.vout[0]);
691 CHECK_AND_ASSERT_MES(!cryptonote::get_output_view_tag(miner_tx.vout[0]), false, "output should still not have a view tag");
692
693 block blk_1;
694 generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx);
695 events.push_back(blk_1);
696
697 DO_CALLBACK(events, "check_block_accepted");
698
699 return true;
700 }
701
702 bool gen_block_miner_tx_out_has_no_view_tag_from_hf_view_tags::generate(std::vector<test_event_entry>& events) const
703 {
704 bool use_view_tags = false;
705
706 BLOCK_VALIDATION_INIT_GENERATE();
707
708 keypair txkey;
709 MAKE_MINER_TX_AND_KEY_AT_HF_MANUALLY(miner_tx, blk_0, HF_VERSION_VIEW_TAGS+1, &txkey);
710
711 crypto::public_key output_public_key;
712 crypto::view_tag view_tag;
713 cryptonote::get_output_public_key(miner_tx.vout[0], output_public_key);
714
715 // remove the view tag that is currently set on the miner tx output at this point
716 cryptonote::set_tx_out(miner_tx.vout[0].amount, output_public_key, use_view_tags, view_tag, miner_tx.vout[0]);
717 CHECK_AND_ASSERT_MES(!cryptonote::get_output_view_tag(miner_tx.vout[0]), false, "output should not have a view tag");
718
719 block blk_1;
720 generator.construct_block_manually(blk_1, blk_0, miner_account,
721 test_generator::bf_major_ver | test_generator::bf_minor_ver | test_generator::bf_miner_tx,
722 HF_VERSION_VIEW_TAGS+1, HF_VERSION_VIEW_TAGS+1, 0, crypto::hash(), 0, miner_tx);
723 events.push_back(blk_1);
724
725 DO_CALLBACK(events, "check_block_purged");
726
727 return true;
728 }
729
730 bool gen_block_miner_tx_out_has_view_tag_before_hf_view_tags::generate(std::vector<test_event_entry>& events) const
731 {
732 bool use_view_tags = true;
733
734 BLOCK_VALIDATION_INIT_GENERATE();
735
736 keypair txkey;
737 MAKE_MINER_TX_AND_KEY_MANUALLY(miner_tx, blk_0, &txkey);
738
739 // derive the view tag for the miner tx output
740 crypto::key_derivation derivation;
741 crypto::public_key output_public_key;
742 crypto::view_tag view_tag;
743 crypto::generate_key_derivation(miner_account.get_keys().m_account_address.m_view_public_key, txkey.sec, derivation);
744 crypto::derive_public_key(derivation, 0, miner_account.get_keys().m_account_address.m_spend_public_key, output_public_key);
745 crypto::derive_view_tag(derivation, 0, view_tag);
746
747 // set the view tag on the miner tx output
748 cryptonote::set_tx_out(miner_tx.vout[0].amount, output_public_key, use_view_tags, view_tag, miner_tx.vout[0]);
749 boost::optional<crypto::view_tag> actual_vt = cryptonote::get_output_view_tag(miner_tx.vout[0]);
750 CHECK_AND_ASSERT_MES(actual_vt && *actual_vt == view_tag, false, "unexpected output view tag");
751
752 block blk_1;
753 generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx);
754 events.push_back(blk_1);
755
756 DO_CALLBACK(events, "check_block_purged");
757
758 return true;
759 }
760
761 bool gen_block_miner_tx_out_has_view_tag_from_hf_view_tags::generate(std::vector<test_event_entry>& events) const
762 {
763 bool use_view_tags = true;
764
765 BLOCK_VALIDATION_INIT_GENERATE();
766
767 keypair txkey;
768 MAKE_MINER_TX_AND_KEY_AT_HF_MANUALLY(miner_tx, blk_0, HF_VERSION_VIEW_TAGS, &txkey);
769
770 CHECK_AND_ASSERT_MES(cryptonote::get_output_view_tag(miner_tx.vout[0]), false, "output should have a view tag");
771
772 // derive the view tag for the miner tx output
773 crypto::key_derivation derivation;
774 crypto::public_key output_public_key;
775 crypto::view_tag view_tag;
776 crypto::generate_key_derivation(miner_account.get_keys().m_account_address.m_view_public_key, txkey.sec, derivation);
777 crypto::derive_public_key(derivation, 0, miner_account.get_keys().m_account_address.m_spend_public_key, output_public_key);
778 crypto::derive_view_tag(derivation, 0, view_tag);
779
780 boost::optional<crypto::view_tag> actual_vt = cryptonote::get_output_view_tag(miner_tx.vout[0]);
781 CHECK_AND_ASSERT_MES(actual_vt && *actual_vt == view_tag, false, "unexpected output view tag");
782
783 // set the view tag on the miner tx output
784 cryptonote::set_tx_out(miner_tx.vout[0].amount, output_public_key, use_view_tags, view_tag, miner_tx.vout[0]);
785 boost::optional<crypto::view_tag> actual_vt_after_setting = cryptonote::get_output_view_tag(miner_tx.vout[0]);
786 CHECK_AND_ASSERT_MES(actual_vt_after_setting && *actual_vt_after_setting == view_tag, false, "unexpected output view tag after setting");
787
788 block blk_1;
789 generator.construct_block_manually(blk_1, blk_0, miner_account,
790 test_generator::bf_major_ver | test_generator::bf_minor_ver | test_generator::bf_miner_tx,
791 HF_VERSION_VIEW_TAGS, HF_VERSION_VIEW_TAGS, 0, crypto::hash(), 0, miner_tx);
792 events.push_back(blk_1);
793
794 DO_CALLBACK(events, "check_block_accepted");
795
796 return true;
797 }