aboutsummaryrefslogblamecommitdiff
path: root/tests/core_tests/tx_pool.cpp
blob: 537015dca40c63fc08442653dc8bd2bf226038e5 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
















































































































































































































































































































































































































































































































































































                                                                                                                                                                                                             
// Copyright (c) 2019, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
//    conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
//    of conditions and the following disclaimer in the documentation and/or other
//    materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
//    used to endorse or promote products derived from this software without specific
//    prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "tx_pool.h"

#include <boost/chrono/chrono.hpp>
#include <boost/thread/thread_only.hpp>
#include <limits>
#include "string_tools.h"

#define INIT_MEMPOOL_TEST()                                   \
  uint64_t send_amount = 1000;                                \
  uint64_t ts_start = 1338224400;                             \
  GENERATE_ACCOUNT(miner_account);                            \
  GENERATE_ACCOUNT(bob_account);                              \
  MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start); \
  REWIND_BLOCKS(events, blk_0r, blk_0, miner_account);        \


txpool_base::txpool_base()
  : test_chain_unit_base()
  , m_broadcasted_tx_count(0)
  , m_all_tx_count(0)
{
  REGISTER_CALLBACK_METHOD(txpool_spend_key_public, increase_broadcasted_tx_count);
  REGISTER_CALLBACK_METHOD(txpool_spend_key_public, increase_all_tx_count);
  REGISTER_CALLBACK_METHOD(txpool_spend_key_public, check_txpool_spent_keys);
}

bool txpool_base::increase_broadcasted_tx_count(cryptonote::core& /*c*/, size_t /*ev_index*/, const std::vector<test_event_entry>& /*events*/)
{
  ++m_broadcasted_tx_count;
  return true;
}

bool txpool_base::increase_all_tx_count(cryptonote::core& /*c*/, size_t /*ev_index*/, const std::vector<test_event_entry>& /*events*/)
{
  ++m_all_tx_count;
  return true;
}

bool txpool_base::check_txpool_spent_keys(cryptonote::core& c, size_t /*ev_index*/, const std::vector<test_event_entry>& events)
{
  std::vector<cryptonote::tx_info> infos{};
  std::vector<cryptonote::spent_key_image_info> key_images{};
  if (!c.get_pool_transactions_and_spent_keys_info(infos, key_images) || infos.size() != m_broadcasted_tx_count || key_images.size() != m_broadcasted_tx_count)
  {
    MERROR("Failed broadcasted spent keys retrieval - Expected Broadcasted Count: " << m_broadcasted_tx_count << " Actual Info Count: " << infos.size() << " Actual Key Image Count: " << key_images.size());
    return false;
  }

  infos.clear();
  key_images.clear();
  if (!c.get_pool_transactions_and_spent_keys_info(infos, key_images, false) || infos.size() != m_broadcasted_tx_count || key_images.size() != m_broadcasted_tx_count)
  {
    MERROR("Failed broadcasted spent keys retrieval - Expected Broadcasted Count: " << m_broadcasted_tx_count << " Actual Info Count: " << infos.size() << " Actual Key Image Count: " << key_images.size());
    return false;
  }

  infos.clear();
  key_images.clear();
  if (!c.get_pool_transactions_and_spent_keys_info(infos, key_images, true) || infos.size() != m_all_tx_count || key_images.size() != m_all_tx_count)
  {
    MERROR("Failed all spent keys retrieval - Expected All Count: " << m_all_tx_count << " Actual Info Count: " << infos.size() << " Actual Key Image Count: " << key_images.size());
    return false;
  }

  return true;
}

bool txpool_spend_key_public::generate(std::vector<test_event_entry>& events) const
{
  INIT_MEMPOOL_TEST();

  DO_CALLBACK(events, "check_txpool_spent_keys");
  MAKE_TX(events, tx_0, miner_account, bob_account, send_amount, blk_0);
  DO_CALLBACK(events, "increase_broadcasted_tx_count");
  DO_CALLBACK(events, "increase_all_tx_count");
  DO_CALLBACK(events, "check_txpool_spent_keys");

  return true;
}

bool txpool_spend_key_all::generate(std::vector<test_event_entry>& events)
{
  INIT_MEMPOOL_TEST();
  SET_EVENT_VISITOR_SETT(events, event_visitor_settings::set_txs_do_not_relay);

  DO_CALLBACK(events, "check_txpool_spent_keys");
  MAKE_TX(events, tx_0, miner_account, bob_account, send_amount, blk_0);
  DO_CALLBACK(events, "increase_all_tx_count");
  DO_CALLBACK(events, "check_txpool_spent_keys");

  return true;
}

txpool_double_spend_base::txpool_double_spend_base()
  : txpool_base()
  , m_broadcasted_hashes()
  , m_no_relay_hashes()
  , m_all_hashes()
  , m_no_new_index(0)
  , m_new_timestamp_index(0)
  , m_last_tx(crypto::hash{})
{
  REGISTER_CALLBACK_METHOD(txpool_double_spend_base, mark_no_new);
  REGISTER_CALLBACK_METHOD(txpool_double_spend_base, mark_timestamp_change);
  REGISTER_CALLBACK_METHOD(txpool_double_spend_base, timestamp_change_pause);
  REGISTER_CALLBACK_METHOD(txpool_double_spend_base, check_unchanged);
  REGISTER_CALLBACK_METHOD(txpool_double_spend_base, check_new_broadcasted);
  REGISTER_CALLBACK_METHOD(txpool_double_spend_base, check_new_hidden);
  REGISTER_CALLBACK_METHOD(txpool_double_spend_base, check_new_no_relay);
}

bool txpool_double_spend_base::mark_no_new(cryptonote::core& /*c*/, size_t ev_index, const std::vector<test_event_entry>& /*events*/)
{
  m_no_new_index = ev_index + 1;
  return true;
}

bool txpool_double_spend_base::mark_timestamp_change(cryptonote::core& /*c*/, size_t ev_index, const std::vector<test_event_entry>& /*events*/)
{
  m_new_timestamp_index = ev_index + 1;
  return true;
}

bool txpool_double_spend_base::timestamp_change_pause(cryptonote::core& /*c*/, size_t /*ev_index*/, const std::vector<test_event_entry>& /*events*/)
{
  boost::this_thread::sleep_for(boost::chrono::seconds{1} + boost::chrono::milliseconds{100});
  return true;
}

bool txpool_double_spend_base::check_changed(cryptonote::core& c, const size_t ev_index, relay_test condition)
{
  const std::size_t public_hash_count = m_broadcasted_hashes.size();
  const std::size_t all_hash_count = m_all_hashes.size();

  const std::size_t new_broadcasted_hash_count = m_broadcasted_hashes.size() + unsigned(condition == relay_test::broadcasted);
  const std::size_t new_all_hash_count = m_all_hashes.size() + unsigned(condition == relay_test::hidden) + unsigned(condition == relay_test::no_relay);

  std::vector<crypto::hash> hashes{};
  if (!c.get_pool_transaction_hashes(hashes))
  {
    MERROR("Failed to get broadcasted transaction pool hashes");
    return false;
  }

  for (const crypto::hash& hash : hashes)
    m_broadcasted_hashes.insert(hash);

  if (new_broadcasted_hash_count != m_broadcasted_hashes.size())
  {
    MERROR("Expected " << new_broadcasted_hash_count << " broadcasted hashes but got " << m_broadcasted_hashes.size());
    return false;
  }

  if (m_broadcasted_hashes.size() != c.get_pool_transactions_count())
  {
    MERROR("Expected " << m_broadcasted_hashes.size() << " broadcasted hashes but got " << c.get_pool_transactions_count());
    return false;
  }

  hashes.clear();
  if (!c.get_pool_transaction_hashes(hashes, false))
  {
    MERROR("Failed to get broadcasted transaction pool hashes");
    return false;
  }

  for (const crypto::hash& hash : hashes)
    m_all_hashes.insert(std::make_pair(hash, 0));

  if (new_broadcasted_hash_count != m_broadcasted_hashes.size())
  {
    MERROR("Expected " << new_broadcasted_hash_count << " broadcasted hashes but got " << m_broadcasted_hashes.size());
    return false;
  }

  hashes.clear();
  if (!c.get_pool_transaction_hashes(hashes, true))
  {

    MERROR("Failed to get all transaction pool hashes");
    return false;
  }

  for (const crypto::hash& hash : hashes)
    m_all_hashes.insert(std::make_pair(hash, 0));

  if (new_all_hash_count != m_all_hashes.size())
  {
    MERROR("Expected " << new_all_hash_count << " all hashes but got " << m_all_hashes.size());
    return false;
  }

  if (condition == relay_test::no_relay)
  {
    if (!m_no_relay_hashes.insert(m_last_tx).second)
    {
      MERROR("Expected new no_relay tx but got a duplicate legacy tx");
      return false;
    }

    for (const crypto::hash& hash : m_no_relay_hashes)
    {
      if (!c.pool_has_tx(hash))
      {
        MERROR("Expected public tx " << hash << " to be listed in pool");
        return false;
      }
    }
  }

  // check receive time changes
  {
    std::vector<cryptonote::tx_info> infos{};
    std::vector<cryptonote::spent_key_image_info> key_images{};
    if (!c.get_pool_transactions_and_spent_keys_info(infos, key_images, true) || infos.size() != m_all_hashes.size())
    {
      MERROR("Unable to retrieve all txpool metadata");
      return false;
    }

    for (const cryptonote::tx_info& info : infos)
    {
      crypto::hash tx_hash;
      if (!epee::string_tools::hex_to_pod(info.id_hash, tx_hash))
      {
        MERROR("Unable to convert tx_hash hex to binary");
        return false;
      }

      const auto entry = m_all_hashes.find(tx_hash);
      if (entry == m_all_hashes.end())
      {
        MERROR("Unable to find tx_hash in set of tracked hashes");
        return false;
      }

      if (m_new_timestamp_index == ev_index && m_last_tx == tx_hash)
      {
        if (entry->second >= info.receive_time)
        {
          MERROR("Last relay time did not change as expected - last at " << entry->second << " and current at " << info.receive_time);
          return false;
        }
        entry->second = info.receive_time;
      }
      else if (entry->second != info.receive_time)
      {
        MERROR("Last relayed time changed unexpectedly from " << entry->second << " to " << info.receive_time);
        return false;
      }
    }
  }

  {
    std::vector<cryptonote::transaction> txes{};
    if (!c.get_pool_transactions(txes))
    {
      MERROR("Failed to get broadcasted transactions from pool");
      return false;
    }

    hashes.clear();
    for (const cryptonote::transaction& tx : txes)
      hashes.push_back(cryptonote::get_transaction_hash(tx));

    std::unordered_set<crypto::hash> public_hashes = m_broadcasted_hashes;
    for (const crypto::hash& hash : hashes)
    {
      if (!c.pool_has_tx(hash))
      {
        MERROR("Expected broadcasted tx " << hash << " to be listed in pool");
        return false;
      }

      if (!public_hashes.erase(hash))
      {
        MERROR("An unexected transaction was returned from the public pool");
        return false;
      }
    }
    if (!public_hashes.empty())
    {
      MERROR(public_hashes.size() << " transaction(s) were missing from the public pool");
      return false;
    }
  }

  {
    std::vector<cryptonote::transaction> txes{};
    if (!c.get_pool_transactions(txes, false))
    {
      MERROR("Failed to get broadcasted transactions from pool");
      return false;
    }

    hashes.clear();
    for (const cryptonote::transaction& tx : txes)
      hashes.push_back(cryptonote::get_transaction_hash(tx));

    std::unordered_set<crypto::hash> public_hashes = m_broadcasted_hashes;
    for (const crypto::hash& hash : hashes)
    {

      if (!public_hashes.erase(hash))
      {
        MERROR("An unexected transaction was returned from the public pool");
        return false;
      }
    }
    if (!public_hashes.empty())
    {
      MERROR(public_hashes.size() << " transaction(s) were missing from the public pool");
      return false;
    }
  }

  {
    std::vector<cryptonote::transaction> txes{};
    if (!c.get_pool_transactions(txes, true))
    {
      MERROR("Failed to get all transactions from pool");
      return false;
    }

    hashes.clear();
    for (const cryptonote::transaction& tx : txes)
      hashes.push_back(cryptonote::get_transaction_hash(tx));

    std::unordered_map<crypto::hash, uint64_t> all_hashes = m_all_hashes;
    for (const crypto::hash& hash : hashes)
    {
      if (!all_hashes.erase(hash))
      {
        MERROR("An unexected transaction was returned from the all pool");
        return false;
      }
    }
    if (!all_hashes.empty())
    {
      MERROR(m_broadcasted_hashes.size() << " transaction(s) were missing from the all pool");
      return false;
    }
  }

  {
    std::vector<cryptonote::tx_backlog_entry> entries{};
    if (!c.get_txpool_backlog(entries))
    {
      MERROR("Failed to get broadcasted txpool backlog");
      return false;
    }

    if (m_broadcasted_hashes.size() != entries.size())
    {
      MERROR("Expected " << m_broadcasted_hashes.size() << " in the broadcasted txpool backlog but got " << entries.size());
      return false;
    }
  }

  for (const std::pair<crypto::hash, uint64_t>& hash : m_all_hashes)
  {
    cryptonote::blobdata tx_blob{};
    if (!c.get_pool_transaction(hash.first, tx_blob, cryptonote::relay_category::all))
    {
      MERROR("Failed to retrieve tx expected to be in pool: " << hash.first);
      return false;
    }
  }

  {
    std::unordered_map<crypto::hash, uint64_t> difference = m_all_hashes;
    for (const crypto::hash& hash : m_broadcasted_hashes)
      difference.erase(hash);

    for (const crypto::hash& hash : m_no_relay_hashes)
      difference.erase(hash);

    for (const std::pair<crypto::hash, uint64_t>& hash : difference)
    {
      if (c.pool_has_tx(hash.first))
      {
        MERROR("Did not expect private/hidden tx " << hash.first << " to be listed in pool");
        return false;
      }

      cryptonote::blobdata tx_blob{};
      if (c.get_pool_transaction(hash.first, tx_blob, cryptonote::relay_category::broadcasted))
      {
        MERROR("Tx " << hash.first << " is not supposed to be in broadcasted pool");
        return false;
      }

      if (!c.get_pool_transaction(hash.first, tx_blob, cryptonote::relay_category::all))
      {
        MERROR("Tx " << hash.first << " blob could not be retrieved from pool");
        return false;
      }
    }
  }

  {
    cryptonote::txpool_stats stats{};
    if (!c.get_pool_transaction_stats(stats) || stats.txs_total != m_broadcasted_hashes.size())
    {
      MERROR("Expected broadcasted stats to list " << m_broadcasted_hashes.size() << " txes but got " << stats.txs_total);
      return false;
    }

    if (!c.get_pool_transaction_stats(stats, false) || stats.txs_total != m_broadcasted_hashes.size())
    {
      MERROR("Expected broadcasted stats to list " << m_broadcasted_hashes.size() << " txes but got " << stats.txs_total);
      return false;
    }

    if (!c.get_pool_transaction_stats(stats, true) || stats.txs_total != m_all_hashes.size())
    {
      MERROR("Expected all stats to list " << m_all_hashes.size() << " txes but got " << stats.txs_total);
      return false;
    }
  }

  {
    std::vector<cryptonote::rpc::tx_in_pool> infos{};
    cryptonote::rpc::key_images_with_tx_hashes key_images{};
    if (!c.get_pool_for_rpc(infos, key_images) || infos.size() != m_broadcasted_hashes.size() || key_images.size() != m_broadcasted_hashes.size())
    {
      MERROR("Expected broadcasted rpc data to return " << m_broadcasted_hashes.size() << " but got " << infos.size() << " infos and " << key_images.size() << "key images");
      return false;
    }
  }
  return true;
}

bool txpool_double_spend_base::check_unchanged(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& /*events */)
{
  return check_changed(c, ev_index, relay_test::no_change);
}

bool txpool_double_spend_base::check_new_broadcasted(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& /*events */)
{
  return check_changed(c, ev_index, relay_test::broadcasted);
}

bool txpool_double_spend_base::check_new_hidden(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& /*events */)
{
  return check_changed(c, ev_index, relay_test::hidden);
}
bool txpool_double_spend_base::check_new_no_relay(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& /*events */)
{
  return check_changed(c, ev_index, relay_test::no_relay);
}

bool txpool_double_spend_base::check_tx_verification_context(const cryptonote::tx_verification_context& tvc, bool tx_added, size_t event_idx, const cryptonote::transaction& tx) 
{
  m_last_tx = cryptonote::get_transaction_hash(tx);
  if (m_no_new_index == event_idx)
    return !tvc.m_verifivation_failed && !tx_added;
  else
    return !tvc.m_verifivation_failed && tx_added;
}

bool txpool_double_spend_norelay::generate(std::vector<test_event_entry>& events) const
{
  INIT_MEMPOOL_TEST();

  DO_CALLBACK(events, "check_txpool_spent_keys");
  SET_EVENT_VISITOR_SETT(events, event_visitor_settings::set_txs_do_not_relay);
  DO_CALLBACK(events, "mark_no_new");

  MAKE_TX(events, tx_0, miner_account, bob_account, send_amount, blk_0);

  DO_CALLBACK(events, "increase_all_tx_count");
  DO_CALLBACK(events, "check_txpool_spent_keys");
  DO_CALLBACK(events, "mark_timestamp_change");
  DO_CALLBACK(events, "check_new_no_relay");
  DO_CALLBACK(events, "timestamp_change_pause");
  DO_CALLBACK(events, "mark_no_new");
  events.push_back(tx_0);
  DO_CALLBACK(events, "check_txpool_spent_keys");
  DO_CALLBACK(events, "check_unchanged");
  SET_EVENT_VISITOR_SETT(events, 0);
  DO_CALLBACK(events, "timestamp_change_pause");
  DO_CALLBACK(events, "mark_no_new");
  events.push_back(tx_0);
  DO_CALLBACK(events, "check_txpool_spent_keys");
  DO_CALLBACK(events, "check_unchanged");

  // kepped by block currently does not change txpool status
  SET_EVENT_VISITOR_SETT(events, event_visitor_settings::set_txs_keeped_by_block);
  DO_CALLBACK(events, "timestamp_change_pause");
  DO_CALLBACK(events, "mark_no_new");
  events.push_back(tx_0);
  DO_CALLBACK(events, "check_txpool_spent_keys");
  DO_CALLBACK(events, "check_unchanged");

  return true;
}

bool txpool_double_spend_local::generate(std::vector<test_event_entry>& events) const
{
  INIT_MEMPOOL_TEST();

  DO_CALLBACK(events, "check_txpool_spent_keys");
  SET_EVENT_VISITOR_SETT(events, event_visitor_settings::set_local_relay);
  DO_CALLBACK(events, "mark_no_new");

  MAKE_TX(events, tx_0, miner_account, bob_account, send_amount, blk_0);

  DO_CALLBACK(events, "increase_all_tx_count");
  DO_CALLBACK(events, "check_txpool_spent_keys");
  DO_CALLBACK(events, "mark_timestamp_change");
  DO_CALLBACK(events, "check_new_hidden");
  DO_CALLBACK(events, "timestamp_change_pause");
  DO_CALLBACK(events, "mark_no_new");
  events.push_back(tx_0);
  DO_CALLBACK(events, "check_txpool_spent_keys");
  DO_CALLBACK(events, "mark_timestamp_change");
  DO_CALLBACK(events, "check_unchanged");
  SET_EVENT_VISITOR_SETT(events, 0);
  DO_CALLBACK(events, "timestamp_change_pause");
  events.push_back(tx_0);
  DO_CALLBACK(events, "increase_broadcasted_tx_count");
  DO_CALLBACK(events, "check_txpool_spent_keys");
  DO_CALLBACK(events, "mark_timestamp_change");
  DO_CALLBACK(events, "check_new_broadcasted");
  DO_CALLBACK(events, "timestamp_change_pause");
  DO_CALLBACK(events, "mark_no_new");
  events.push_back(tx_0);
  DO_CALLBACK(events, "check_unchanged");

  return true;
}