Debian Codegraph @

tests/core_tests/tx_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 "tx_validation.h"
33 #include "device/device.hpp"
34
35 using namespace epee;
36 using namespace crypto;
37 using namespace cryptonote;
38
39 namespace
40 {
41 struct tx_builder
42 {
43 void step1_init(size_t version = 1, uint64_t unlock_time = 0)
44 {
45 m_tx.vin.clear();
46 m_tx.vout.clear();
47 m_tx.signatures.clear();
48
49 m_tx.version = version;
50 m_tx.unlock_time = unlock_time;
51
52 m_tx_key = keypair::generate(hw::get_device("default"));
53 add_tx_pub_key_to_extra(m_tx, m_tx_key.pub);
54 }
55
56 void step2_fill_inputs(const account_keys& sender_account_keys, const std::vector<tx_source_entry>& sources)
57 {
58 BOOST_FOREACH(const tx_source_entry& src_entr, sources)
59 {
60 m_in_contexts.push_back(keypair());
61 keypair& in_ephemeral = m_in_contexts.back();
62 crypto::key_image img;
63 std::unordered_map<crypto::public_key, cryptonote::subaddress_index> subaddresses;
64 subaddresses[sender_account_keys.m_account_address.m_spend_public_key] = {0,0};
65 auto& out_key = reinterpret_cast<const crypto::public_key&>(src_entr.outputs[src_entr.real_output].second.dest);
66 generate_key_image_helper(sender_account_keys, subaddresses, out_key, src_entr.real_out_tx_key, src_entr.real_out_additional_tx_keys, src_entr.real_output_in_tx_index, in_ephemeral, img, hw::get_device(("default")));
67
68 // put key image into tx input
69 txin_to_key input_to_key;
70 input_to_key.amount = src_entr.amount;
71 input_to_key.k_image = img;
72
73 // fill outputs array and use relative offsets
74 BOOST_FOREACH(const tx_source_entry::output_entry& out_entry, src_entr.outputs)
75 input_to_key.key_offsets.push_back(out_entry.first);
76
77 input_to_key.key_offsets = absolute_output_offsets_to_relative(input_to_key.key_offsets);
78 m_tx.vin.push_back(input_to_key);
79 }
80 }
81
82 void step3_fill_outputs(const std::vector<tx_destination_entry>& destinations)
83 {
84 size_t output_index = 0;
85 BOOST_FOREACH(const tx_destination_entry& dst_entr, destinations)
86 {
87 crypto::key_derivation derivation;
88 crypto::public_key out_eph_public_key;
89 crypto::generate_key_derivation(dst_entr.addr.m_view_public_key, m_tx_key.sec, derivation);
90 crypto::derive_public_key(derivation, output_index, dst_entr.addr.m_spend_public_key, out_eph_public_key);
91
92 tx_out out;
93 out.amount = dst_entr.amount;
94 txout_to_key tk;
95 tk.key = out_eph_public_key;
96 out.target = tk;
97 m_tx.vout.push_back(out);
98 output_index++;
99 }
100 }
101
102 void step4_calc_hash()
103 {
104 get_transaction_prefix_hash(m_tx, m_tx_prefix_hash);
105 }
106
107 void step5_sign(const std::vector<tx_source_entry>& sources)
108 {
109 m_tx.signatures.clear();
110
111 size_t i = 0;
112 BOOST_FOREACH(const tx_source_entry& src_entr, sources)
113 {
114 std::vector<const crypto::public_key*> keys_ptrs;
115 std::vector<crypto::public_key> keys(src_entr.outputs.size());
116 size_t j = 0;
117 BOOST_FOREACH(const tx_source_entry::output_entry& o, src_entr.outputs)
118 {
119 keys[j] = rct::rct2pk(o.second.dest);
120 keys_ptrs.push_back(&keys[j]);
121 ++j;
122 }
123
124 m_tx.signatures.push_back(std::vector<crypto::signature>());
125 std::vector<crypto::signature>& sigs = m_tx.signatures.back();
126 sigs.resize(src_entr.outputs.size());
127 generate_ring_signature(m_tx_prefix_hash, boost::get<txin_to_key>(m_tx.vin[i]).k_image, keys_ptrs, m_in_contexts[i].sec, src_entr.real_output, sigs.data());
128 i++;
129 }
130 }
131
132 transaction m_tx;
133 keypair m_tx_key;
134 std::vector<keypair> m_in_contexts;
135 crypto::hash m_tx_prefix_hash;
136 };
137
138 transaction make_simple_tx_with_unlock_time(const std::vector<test_event_entry>& events,
139 const cryptonote::block& blk_head, const cryptonote::account_base& from, const cryptonote::account_base& to,
140 uint64_t amount, uint64_t unlock_time)
141 {
142 std::vector<tx_source_entry> sources;
143 std::vector<tx_destination_entry> destinations;
144 fill_tx_sources_and_destinations(events, blk_head, from, to, amount, TESTS_DEFAULT_FEE, 0, sources, destinations);
145
146 tx_builder builder;
147 builder.step1_init(1, unlock_time);
148 builder.step2_fill_inputs(from.get_keys(), sources);
149 builder.step3_fill_outputs(destinations);
150 builder.step4_calc_hash();
151 builder.step5_sign(sources);
152 return builder.m_tx;
153 };
154
155 crypto::public_key generate_invalid_pub_key()
156 {
157 for (int i = 0; i <= 0xFF; ++i)
158 {
159 crypto::public_key key;
160 memset(&key, i, sizeof(crypto::public_key));
161 if (!crypto::check_key(key))
162 {
163 return key;
164 }
165 }
166
167 throw std::runtime_error("invalid public key wasn't found");
168 return crypto::public_key();
169 }
170
171 crypto::key_image generate_invalid_key_image()
172 {
173 crypto::key_image key_image;
174 // a random key image plucked from the blockchain
175 if (!epee::string_tools::hex_to_pod("6b9f5d1be7c950dc6e4e258c6ef75509412ba9ecaaf90e6886140151d1365b5e", key_image))
176 throw std::runtime_error("invalid key image wasn't found");
177 return key_image;
178 }
179 }
180
181 //----------------------------------------------------------------------------------------------------------------------
182 // Tests
183
184 bool gen_tx_big_version::generate(std::vector<test_event_entry>& events) const
185 {
186 uint64_t ts_start = 1338224400;
187
188 GENERATE_ACCOUNT(miner_account);
189 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
190 REWIND_BLOCKS(events, blk_0r, blk_0, miner_account);
191
192 std::vector<tx_source_entry> sources;
193 std::vector<tx_destination_entry> destinations;
194 fill_tx_sources_and_destinations(events, blk_0, miner_account, miner_account, MK_COINS(1), TESTS_DEFAULT_FEE, 0, sources, destinations);
195
196 tx_builder builder;
197 builder.step1_init(1 + 1, 0);
198 builder.step2_fill_inputs(miner_account.get_keys(), sources);
199 builder.step3_fill_outputs(destinations);
200 builder.step4_calc_hash();
201 builder.step5_sign(sources);
202
203 DO_CALLBACK(events, "mark_invalid_tx");
204 events.push_back(builder.m_tx);
205
206 return true;
207 }
208
209 bool gen_tx_unlock_time::generate(std::vector<test_event_entry>& events) const
210 {
211 uint64_t ts_start = 1338224400;
212
213 GENERATE_ACCOUNT(miner_account);
214 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
215 REWIND_BLOCKS_N(events, blk_1, blk_0, miner_account, 20);
216 REWIND_BLOCKS(events, blk_1r, blk_1, miner_account);
217
218 auto make_tx_with_unlock_time = [&](uint64_t unlock_time) -> transaction
219 {
220 return make_simple_tx_with_unlock_time(events, blk_1, miner_account, miner_account, MK_COINS(1), unlock_time);
221 };
222
223 std::list<transaction> txs_0;
224
225 // Let's make sure that non-zero unlock times fail with a normal relay method
226 SET_EVENT_VISITOR_SETT(events, 0); // set relay_method::fluff
227
228 txs_0.push_back(make_tx_with_unlock_time(0));
229 events.push_back(txs_0.back());
230
231 DO_CALLBACK(events, "mark_invalid_tx");
232 events.push_back(make_tx_with_unlock_time(get_block_height(blk_1r) - 1));
233
234 DO_CALLBACK(events, "mark_invalid_tx");
235 events.push_back(make_tx_with_unlock_time(get_block_height(blk_1r)));
236
237 DO_CALLBACK(events, "mark_invalid_tx");
238 events.push_back(make_tx_with_unlock_time(get_block_height(blk_1r) + 1));
239
240 DO_CALLBACK(events, "mark_invalid_tx");
241 events.push_back(make_tx_with_unlock_time(get_block_height(blk_1r) + 2));
242
243 DO_CALLBACK(events, "mark_invalid_tx");
244 events.push_back(make_tx_with_unlock_time(ts_start - 1));
245
246 DO_CALLBACK(events, "mark_invalid_tx");
247 events.push_back(make_tx_with_unlock_time(time(0) + 60 * 60));
248
249 // We want to try adding these transactions with non-zero unlock times to the pool, but relay
250 // rules enforce otherwise, so we set the relay method to block
251 SET_EVENT_VISITOR_SETT(events, event_visitor_settings::set_txs_keeped_by_block); // set relay_method::block
252
253 txs_0.push_back(make_tx_with_unlock_time(get_block_height(blk_1r) - 1));
254 events.push_back(txs_0.back());
255
256 txs_0.push_back(make_tx_with_unlock_time(get_block_height(blk_1r)));
257 events.push_back(txs_0.back());
258
259 txs_0.push_back(make_tx_with_unlock_time(get_block_height(blk_1r) + 1));
260 events.push_back(txs_0.back());
261
262 txs_0.push_back(make_tx_with_unlock_time(get_block_height(blk_1r) + 2));
263 events.push_back(txs_0.back());
264
265 txs_0.push_back(make_tx_with_unlock_time(ts_start - 1));
266 events.push_back(txs_0.back());
267
268 txs_0.push_back(make_tx_with_unlock_time(time(0) + 60 * 60));
269 events.push_back(txs_0.back());
270
271 MAKE_NEXT_BLOCK_TX_LIST(events, blk_2, blk_1r, miner_account, txs_0);
272
273 return true;
274 }
275
276 bool gen_tx_input_is_not_txin_to_key::generate(std::vector<test_event_entry>& events) const
277 {
278 uint64_t ts_start = 1338224400;
279
280 GENERATE_ACCOUNT(miner_account);
281 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
282 REWIND_BLOCKS(events, blk_0r, blk_0, miner_account);
283
284 MAKE_NEXT_BLOCK(events, blk_tmp, blk_0r, miner_account);
285 events.pop_back();
286
287 DO_CALLBACK(events, "mark_invalid_tx");
288 events.push_back(blk_tmp.miner_tx);
289
290 auto make_tx_with_input = [&](const txin_v& tx_input) -> transaction
291 {
292 std::vector<tx_source_entry> sources;
293 std::vector<tx_destination_entry> destinations;
294 fill_tx_sources_and_destinations(events, blk_0, miner_account, miner_account, MK_COINS(1), TESTS_DEFAULT_FEE, 0, sources, destinations);
295
296 tx_builder builder;
297 builder.step1_init();
298 builder.m_tx.vin.push_back(tx_input);
299 builder.step3_fill_outputs(destinations);
300 return builder.m_tx;
301 };
302
303 DO_CALLBACK(events, "mark_invalid_tx");
304 events.push_back(make_tx_with_input(txin_to_script()));
305
306 DO_CALLBACK(events, "mark_invalid_tx");
307 events.push_back(make_tx_with_input(txin_to_scripthash()));
308
309 return true;
310 }
311
312 bool gen_tx_no_inputs_no_outputs::generate(std::vector<test_event_entry>& events) const
313 {
314 uint64_t ts_start = 1338224400;
315
316 GENERATE_ACCOUNT(miner_account);
317 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
318
319 tx_builder builder;
320 builder.step1_init();
321
322 DO_CALLBACK(events, "mark_invalid_tx");
323 events.push_back(builder.m_tx);
324
325 return true;
326 }
327
328 bool gen_tx_no_inputs_has_outputs::generate(std::vector<test_event_entry>& events) const
329 {
330 uint64_t ts_start = 1338224400;
331
332 GENERATE_ACCOUNT(miner_account);
333 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
334
335 std::vector<tx_source_entry> sources;
336 std::vector<tx_destination_entry> destinations;
337 fill_tx_sources_and_destinations(events, blk_0, miner_account, miner_account, MK_COINS(1), TESTS_DEFAULT_FEE, 0, sources, destinations);
338
339 tx_builder builder;
340 builder.step1_init();
341 builder.step3_fill_outputs(destinations);
342
343 DO_CALLBACK(events, "mark_invalid_tx");
344 events.push_back(builder.m_tx);
345
346 return true;
347 }
348
349 bool gen_tx_has_inputs_no_outputs::generate(std::vector<test_event_entry>& events) const
350 {
351 uint64_t ts_start = 1338224400;
352
353 GENERATE_ACCOUNT(miner_account);
354 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
355 REWIND_BLOCKS(events, blk_0r, blk_0, miner_account);
356
357 std::vector<tx_source_entry> sources;
358 std::vector<tx_destination_entry> destinations;
359 fill_tx_sources_and_destinations(events, blk_0, miner_account, miner_account, MK_COINS(1), TESTS_DEFAULT_FEE, 0, sources, destinations);
360 destinations.clear();
361
362 tx_builder builder;
363 builder.step1_init();
364 builder.step2_fill_inputs(miner_account.get_keys(), sources);
365 builder.step3_fill_outputs(destinations);
366 builder.step4_calc_hash();
367 builder.step5_sign(sources);
368
369 events.push_back(builder.m_tx);
370 MAKE_NEXT_BLOCK_TX1(events, blk_1, blk_0r, miner_account, builder.m_tx);
371
372 return true;
373 }
374
375 bool gen_tx_invalid_input_amount::generate(std::vector<test_event_entry>& events) const
376 {
377 uint64_t ts_start = 1338224400;
378
379 GENERATE_ACCOUNT(miner_account);
380 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
381 REWIND_BLOCKS(events, blk_0r, blk_0, miner_account);
382
383 std::vector<tx_source_entry> sources;
384 std::vector<tx_destination_entry> destinations;
385 fill_tx_sources_and_destinations(events, blk_0, miner_account, miner_account, MK_COINS(1), TESTS_DEFAULT_FEE, 0, sources, destinations);
386 sources.front().amount++;
387
388 tx_builder builder;
389 builder.step1_init();
390 builder.step2_fill_inputs(miner_account.get_keys(), sources);
391 builder.step3_fill_outputs(destinations);
392 builder.step4_calc_hash();
393 builder.step5_sign(sources);
394
395 DO_CALLBACK(events, "mark_invalid_tx");
396 events.push_back(builder.m_tx);
397
398 return true;
399 }
400
401 bool gen_tx_input_wo_key_offsets::generate(std::vector<test_event_entry>& events) const
402 {
403 uint64_t ts_start = 1338224400;
404
405 GENERATE_ACCOUNT(miner_account);
406 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
407 REWIND_BLOCKS(events, blk_0r, blk_0, miner_account);
408
409 std::vector<tx_source_entry> sources;
410 std::vector<tx_destination_entry> destinations;
411 fill_tx_sources_and_destinations(events, blk_0, miner_account, miner_account, MK_COINS(1), TESTS_DEFAULT_FEE, 0, sources, destinations);
412
413 tx_builder builder;
414 builder.step1_init();
415 builder.step2_fill_inputs(miner_account.get_keys(), sources);
416 builder.step3_fill_outputs(destinations);
417 txin_to_key& in_to_key = boost::get<txin_to_key>(builder.m_tx.vin.front());
418 uint64_t key_offset = in_to_key.key_offsets.front();
419 in_to_key.key_offsets.pop_back();
420 CHECK_AND_ASSERT_MES(in_to_key.key_offsets.empty(), false, "txin contained more than one key_offset");
421 builder.step4_calc_hash();
422 in_to_key.key_offsets.push_back(key_offset);
423 builder.step5_sign(sources);
424 in_to_key.key_offsets.pop_back();
425
426 DO_CALLBACK(events, "mark_invalid_tx");
427 events.push_back(builder.m_tx);
428
429 return true;
430 }
431
432 bool gen_tx_key_offest_points_to_foreign_key::generate(std::vector<test_event_entry>& events) const
433 {
434 uint64_t ts_start = 1338224400;
435
436 GENERATE_ACCOUNT(miner_account);
437 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
438 MAKE_NEXT_BLOCK(events, blk_1, blk_0, miner_account);
439 REWIND_BLOCKS(events, blk_1r, blk_1, miner_account);
440 MAKE_ACCOUNT(events, alice_account);
441 MAKE_ACCOUNT(events, bob_account);
442 MAKE_TX_LIST_START(events, txs_0, miner_account, bob_account, MK_COINS(15) + 1, blk_1);
443 MAKE_TX_LIST(events, txs_0, miner_account, alice_account, MK_COINS(15) + 1, blk_1);
444 MAKE_NEXT_BLOCK_TX_LIST(events, blk_2, blk_1r, miner_account, txs_0);
445
446 std::vector<tx_source_entry> sources_bob;
447 std::vector<tx_destination_entry> destinations_bob;
448 fill_tx_sources_and_destinations(events, blk_2, bob_account, miner_account, MK_COINS(15) + 1 - TESTS_DEFAULT_FEE, TESTS_DEFAULT_FEE, 0, sources_bob, destinations_bob);
449
450 std::vector<tx_source_entry> sources_alice;
451 std::vector<tx_destination_entry> destinations_alice;
452 fill_tx_sources_and_destinations(events, blk_2, alice_account, miner_account, MK_COINS(15) + 1 - TESTS_DEFAULT_FEE, TESTS_DEFAULT_FEE, 0, sources_alice, destinations_alice);
453
454 tx_builder builder;
455 builder.step1_init();
456 builder.step2_fill_inputs(bob_account.get_keys(), sources_bob);
457 txin_to_key& in_to_key = boost::get<txin_to_key>(builder.m_tx.vin.front());
458 in_to_key.key_offsets.front() = sources_alice.front().outputs.front().first;
459 builder.step3_fill_outputs(destinations_bob);
460 builder.step4_calc_hash();
461 builder.step5_sign(sources_bob);
462
463 DO_CALLBACK(events, "mark_invalid_tx");
464 events.push_back(builder.m_tx);
465
466 return true;
467 }
468
469 bool gen_tx_sender_key_offest_not_exist::generate(std::vector<test_event_entry>& events) const
470 {
471 uint64_t ts_start = 1338224400;
472
473 GENERATE_ACCOUNT(miner_account);
474 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
475 REWIND_BLOCKS(events, blk_0r, blk_0, miner_account);
476
477 std::vector<tx_source_entry> sources;
478 std::vector<tx_destination_entry> destinations;
479 fill_tx_sources_and_destinations(events, blk_0, miner_account, miner_account, MK_COINS(1), TESTS_DEFAULT_FEE, 0, sources, destinations);
480
481 tx_builder builder;
482 builder.step1_init();
483 builder.step2_fill_inputs(miner_account.get_keys(), sources);
484 txin_to_key& in_to_key = boost::get<txin_to_key>(builder.m_tx.vin.front());
485 in_to_key.key_offsets.front() = std::numeric_limits<uint64_t>::max();
486 builder.step3_fill_outputs(destinations);
487 builder.step4_calc_hash();
488 builder.step5_sign(sources);
489
490 DO_CALLBACK(events, "mark_invalid_tx");
491 events.push_back(builder.m_tx);
492
493 return true;
494 }
495
496 bool gen_tx_mixed_key_offest_not_exist::generate(std::vector<test_event_entry>& events) const
497 {
498 uint64_t ts_start = 1338224400;
499
500 GENERATE_ACCOUNT(miner_account);
501 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
502 MAKE_NEXT_BLOCK(events, blk_1, blk_0, miner_account);
503 REWIND_BLOCKS(events, blk_1r, blk_1, miner_account);
504 MAKE_ACCOUNT(events, alice_account);
505 MAKE_ACCOUNT(events, bob_account);
506 MAKE_TX_LIST_START(events, txs_0, miner_account, bob_account, MK_COINS(1) + TESTS_DEFAULT_FEE, blk_1);
507 MAKE_TX_LIST(events, txs_0, miner_account, alice_account, MK_COINS(1) + TESTS_DEFAULT_FEE, blk_1);
508 MAKE_NEXT_BLOCK_TX_LIST(events, blk_2, blk_1r, miner_account, txs_0);
509
510 std::vector<tx_source_entry> sources;
511 std::vector<tx_destination_entry> destinations;
512 fill_tx_sources_and_destinations(events, blk_2, bob_account, miner_account, MK_COINS(1), TESTS_DEFAULT_FEE, 1, sources, destinations);
513
514 sources.front().outputs[(sources.front().real_output + 1) % 2].first = std::numeric_limits<uint64_t>::max();
515
516 tx_builder builder;
517 builder.step1_init();
518 builder.step2_fill_inputs(bob_account.get_keys(), sources);
519 builder.step3_fill_outputs(destinations);
520 builder.step4_calc_hash();
521 builder.step5_sign(sources);
522
523 DO_CALLBACK(events, "mark_invalid_tx");
524 events.push_back(builder.m_tx);
525
526 return true;
527 }
528
529 bool gen_tx_key_image_not_derive_from_tx_key::generate(std::vector<test_event_entry>& events) const
530 {
531 uint64_t ts_start = 1338224400;
532
533 GENERATE_ACCOUNT(miner_account);
534 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
535 REWIND_BLOCKS(events, blk_0r, blk_0, miner_account);
536
537 std::vector<tx_source_entry> sources;
538 std::vector<tx_destination_entry> destinations;
539 fill_tx_sources_and_destinations(events, blk_0, miner_account, miner_account, MK_COINS(1), TESTS_DEFAULT_FEE, 0, sources, destinations);
540
541 tx_builder builder;
542 builder.step1_init();
543 builder.step2_fill_inputs(miner_account.get_keys(), sources);
544
545 txin_to_key& in_to_key = boost::get<txin_to_key>(builder.m_tx.vin.front());
546 keypair kp = keypair::generate(hw::get_device("default"));
547 key_image another_ki;
548 crypto::generate_key_image(kp.pub, kp.sec, another_ki);
549 in_to_key.k_image = another_ki;
550
551 builder.step3_fill_outputs(destinations);
552 builder.step4_calc_hash();
553
554 // Tx with invalid key image can't be subscribed, so create empty signature
555 builder.m_tx.signatures.resize(1);
556 builder.m_tx.signatures[0].resize(1);
557 builder.m_tx.signatures[0][0] = crypto::signature{};
558
559 DO_CALLBACK(events, "mark_invalid_tx");
560 events.push_back(builder.m_tx);
561
562 return true;
563 }
564
565 bool gen_tx_key_image_is_invalid::generate(std::vector<test_event_entry>& events) const
566 {
567 uint64_t ts_start = 1338224400;
568
569 GENERATE_ACCOUNT(miner_account);
570 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
571 REWIND_BLOCKS(events, blk_0r, blk_0, miner_account);
572
573 std::vector<tx_source_entry> sources;
574 std::vector<tx_destination_entry> destinations;
575 fill_tx_sources_and_destinations(events, blk_0, miner_account, miner_account, MK_COINS(1), TESTS_DEFAULT_FEE, 0, sources, destinations);
576
577 tx_builder builder;
578 builder.step1_init();
579 builder.step2_fill_inputs(miner_account.get_keys(), sources);
580
581 txin_to_key& in_to_key = boost::get<txin_to_key>(builder.m_tx.vin.front());
582 in_to_key.k_image = generate_invalid_key_image();
583
584 builder.step3_fill_outputs(destinations);
585 builder.step4_calc_hash();
586
587 // Tx with invalid key image can't be subscribed, so create empty signature
588 builder.m_tx.signatures.resize(1);
589 builder.m_tx.signatures[0].resize(1);
590 builder.m_tx.signatures[0][0] = crypto::signature{};
591
592 DO_CALLBACK(events, "mark_invalid_tx");
593 events.push_back(builder.m_tx);
594
595 return true;
596 }
597
598 bool gen_tx_check_input_unlock_time::generate(std::vector<test_event_entry>& events) const
599 {
600 static const size_t tests_count = 6;
601
602 uint64_t ts_start = 1338224400;
603
604 GENERATE_ACCOUNT(miner_account);
605 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
606 REWIND_BLOCKS_N(events, blk_1, blk_0, miner_account, tests_count - 1);
607 REWIND_BLOCKS(events, blk_1r, blk_1, miner_account);
608
609 std::array<account_base, tests_count> accounts;
610 for (size_t i = 0; i < tests_count; ++i)
611 {
612 MAKE_ACCOUNT(events, acc);
613 accounts[i] = acc;
614 }
615
616 std::list<transaction> txs_0;
617 auto make_tx_to_acc = [&](size_t acc_idx, uint64_t unlock_time)
618 {
619 txs_0.push_back(make_simple_tx_with_unlock_time(events, blk_1, miner_account, accounts[acc_idx],
620 MK_COINS(1) + TESTS_DEFAULT_FEE, unlock_time));
621 events.push_back(txs_0.back());
622 };
623
624 SET_EVENT_VISITOR_SETT(events, event_visitor_settings::set_txs_keeped_by_block); // set relay_method::block
625 uint64_t blk_3_height = get_block_height(blk_1r) + 2;
626 make_tx_to_acc(0, 0);
627 make_tx_to_acc(1, blk_3_height - 1);
628 make_tx_to_acc(2, blk_3_height);
629 make_tx_to_acc(3, blk_3_height + 1);
630 make_tx_to_acc(4, time(0) - 1);
631 make_tx_to_acc(5, time(0) + 60 * 60);
632 MAKE_NEXT_BLOCK_TX_LIST(events, blk_2, blk_1r, miner_account, txs_0);
633 SET_EVENT_VISITOR_SETT(events, 0); // set relay_method::fluff
634
635 std::list<transaction> txs_1;
636 auto make_tx_from_acc = [&](size_t acc_idx, bool invalid)
637 {
638 transaction tx = make_simple_tx_with_unlock_time(events, blk_2, accounts[acc_idx], miner_account, MK_COINS(1), 0);
639 if (invalid)
640 {
641 DO_CALLBACK(events, "mark_invalid_tx");
642 }
643 else
644 {
645 txs_1.push_back(tx);
646 }
647 events.push_back(tx);
648 };
649
650 make_tx_from_acc(0, false);
651 make_tx_from_acc(1, false);
652 make_tx_from_acc(2, false);
653 make_tx_from_acc(3, true);
654 make_tx_from_acc(4, false);
655 make_tx_from_acc(5, true);
656 MAKE_NEXT_BLOCK_TX_LIST(events, blk_3, blk_2, miner_account, txs_1);
657
658 return true;
659 }
660
661 bool gen_tx_txout_to_key_has_invalid_key::generate(std::vector<test_event_entry>& events) const
662 {
663 uint64_t ts_start = 1338224400;
664
665 GENERATE_ACCOUNT(miner_account);
666 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
667 REWIND_BLOCKS(events, blk_0r, blk_0, miner_account);
668
669 std::vector<tx_source_entry> sources;
670 std::vector<tx_destination_entry> destinations;
671 fill_tx_sources_and_destinations(events, blk_0, miner_account, miner_account, MK_COINS(1), TESTS_DEFAULT_FEE, 0, sources, destinations);
672
673 tx_builder builder;
674 builder.step1_init();
675 builder.step2_fill_inputs(miner_account.get_keys(), sources);
676 builder.step3_fill_outputs(destinations);
677
678 txout_to_key& out_to_key = boost::get<txout_to_key>(builder.m_tx.vout.front().target);
679 out_to_key.key = generate_invalid_pub_key();
680
681 builder.step4_calc_hash();
682 builder.step5_sign(sources);
683
684 DO_CALLBACK(events, "mark_invalid_tx");
685 events.push_back(builder.m_tx);
686
687 return true;
688 }
689
690 bool gen_tx_output_with_zero_amount::generate(std::vector<test_event_entry>& events) const
691 {
692 uint64_t ts_start = 1338224400;
693
694 GENERATE_ACCOUNT(miner_account);
695 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
696 REWIND_BLOCKS(events, blk_0r, blk_0, miner_account);
697
698 std::vector<tx_source_entry> sources;
699 std::vector<tx_destination_entry> destinations;
700 fill_tx_sources_and_destinations(events, blk_0, miner_account, miner_account, MK_COINS(1), TESTS_DEFAULT_FEE, 0, sources, destinations);
701
702 tx_builder builder;
703 builder.step1_init();
704 builder.step2_fill_inputs(miner_account.get_keys(), sources);
705 builder.step3_fill_outputs(destinations);
706
707 builder.m_tx.vout.front().amount = 0;
708
709 builder.step4_calc_hash();
710 builder.step5_sign(sources);
711
712 DO_CALLBACK(events, "mark_invalid_tx");
713 events.push_back(builder.m_tx);
714
715 return true;
716 }
717
718 bool gen_tx_output_is_not_txout_to_key::generate(std::vector<test_event_entry>& events) const
719 {
720 uint64_t ts_start = 1338224400;
721
722 GENERATE_ACCOUNT(miner_account);
723 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
724 REWIND_BLOCKS(events, blk_0r, blk_0, miner_account);
725
726 std::vector<tx_source_entry> sources;
727 std::vector<tx_destination_entry> destinations;
728 fill_tx_sources_and_destinations(events, blk_0, miner_account, miner_account, MK_COINS(1), TESTS_DEFAULT_FEE, 0, sources, destinations);
729
730 tx_builder builder;
731 builder.step1_init();
732 builder.step2_fill_inputs(miner_account.get_keys(), sources);
733
734 builder.m_tx.vout.push_back(tx_out());
735 builder.m_tx.vout.back().amount = 1;
736 builder.m_tx.vout.back().target = txout_to_script();
737
738 builder.step4_calc_hash();
739 builder.step5_sign(sources);
740
741 DO_CALLBACK(events, "mark_invalid_tx");
742 events.push_back(builder.m_tx);
743
744 builder.step1_init();
745 builder.step2_fill_inputs(miner_account.get_keys(), sources);
746
747 builder.m_tx.vout.push_back(tx_out());
748 builder.m_tx.vout.back().amount = 1;
749 builder.m_tx.vout.back().target = txout_to_scripthash();
750
751 builder.step4_calc_hash();
752 builder.step5_sign(sources);
753
754 DO_CALLBACK(events, "mark_invalid_tx");
755 events.push_back(builder.m_tx);
756
757 return true;
758 }
759
760 bool gen_tx_signatures_are_invalid::generate(std::vector<test_event_entry>& events) const
761 {
762 uint64_t ts_start = 1338224400;
763
764 GENERATE_ACCOUNT(miner_account);
765 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
766 MAKE_NEXT_BLOCK(events, blk_1, blk_0, miner_account);
767 REWIND_BLOCKS(events, blk_1r, blk_1, miner_account);
768 MAKE_ACCOUNT(events, alice_account);
769 MAKE_ACCOUNT(events, bob_account);
770 MAKE_TX_LIST_START(events, txs_0, miner_account, bob_account, MK_COINS(1) + TESTS_DEFAULT_FEE, blk_1);
771 MAKE_TX_LIST(events, txs_0, miner_account, alice_account, MK_COINS(1) + TESTS_DEFAULT_FEE, blk_1);
772 MAKE_NEXT_BLOCK_TX_LIST(events, blk_2, blk_1r, miner_account, txs_0);
773
774 MAKE_TX(events, tx_0, miner_account, miner_account, MK_COINS(60), blk_2);
775 events.pop_back();
776
777 MAKE_TX_MIX(events, tx_1, bob_account, miner_account, MK_COINS(1), 1, blk_2);
778 events.pop_back();
779
780 // Tx with nmix = 0 without signatures
781 DO_CALLBACK(events, "mark_invalid_tx");
782 blobdata sr_tx = t_serializable_object_to_blob(static_cast<transaction_prefix>(tx_0));
783 events.push_back(serialized_transaction(sr_tx));
784
785 // Tx with nmix = 0 have a few inputs, and not enough signatures
786 DO_CALLBACK(events, "mark_invalid_tx");
787 sr_tx = t_serializable_object_to_blob(tx_0);
788 sr_tx.resize(sr_tx.size() - sizeof(crypto::signature));
789 events.push_back(serialized_transaction(sr_tx));
790
791 // Tx with nmix = 0 have a few inputs, and too many signatures
792 DO_CALLBACK(events, "mark_invalid_tx");
793 sr_tx = t_serializable_object_to_blob(tx_0);
794 sr_tx.insert(sr_tx.end(), sr_tx.end() - sizeof(crypto::signature), sr_tx.end());
795 events.push_back(serialized_transaction(sr_tx));
796
797 // Tx with nmix = 1 without signatures
798 DO_CALLBACK(events, "mark_invalid_tx");
799 sr_tx = t_serializable_object_to_blob(static_cast<transaction_prefix>(tx_1));
800 events.push_back(serialized_transaction(sr_tx));
801
802 // Tx with nmix = 1 have not enough signatures
803 DO_CALLBACK(events, "mark_invalid_tx");
804 sr_tx = t_serializable_object_to_blob(tx_1);
805 sr_tx.resize(sr_tx.size() - sizeof(crypto::signature));
806 events.push_back(serialized_transaction(sr_tx));
807
808 // Tx with nmix = 1 have too many signatures
809 DO_CALLBACK(events, "mark_invalid_tx");
810 sr_tx = t_serializable_object_to_blob(tx_1);
811 sr_tx.insert(sr_tx.end(), sr_tx.end() - sizeof(crypto::signature), sr_tx.end());
812 events.push_back(serialized_transaction(sr_tx));
813
814 return true;
815 }