aboutsummaryrefslogtreecommitdiff
path: root/tests/core_tests/tx_pool.h
blob: eb71dcf7999f50f69d7c5506cc4cba107cbf1f7b (plain) (blame)
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
// 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.

#pragma once

#include <unordered_map>
#include <unordered_set>

#include "chaingen.h"
#include "crypto/crypto.h"

enum class relay_test
{
  no_change = 0, //!< No expected changes to the txpool
  broadcasted,   //!< A new block or fluff/flood tx is expected in txpool
  hidden,        //!< A new stem or local tx is expected in txpool
  no_relay       //!< A new no relay is expected in txpool
};

class txpool_base : public test_chain_unit_base
{
  size_t m_broadcasted_tx_count;
  size_t m_all_tx_count;

public:
  txpool_base();

  bool increase_broadcasted_tx_count(cryptonote::core& c, size_t /*ev_index*/, const std::vector<test_event_entry>& events);
  bool increase_all_tx_count(cryptonote::core& c, size_t /*ev_index*/, const std::vector<test_event_entry>& events);
  bool check_txpool_spent_keys(cryptonote::core& c, size_t /*ev_index*/, const std::vector<test_event_entry>& events);
};

struct txpool_spend_key_public : txpool_base
{
  txpool_spend_key_public() : txpool_base()
  {}

  bool generate(std::vector<test_event_entry>& events) const;
};

struct txpool_spend_key_all : txpool_base
{
  txpool_spend_key_all() : txpool_base()
  {}

  bool generate(std::vector<test_event_entry>& events);
};

class txpool_double_spend_base : public txpool_base
{
  std::unordered_set<crypto::hash> m_broadcasted_hashes;
  std::unordered_set<crypto::hash> m_no_relay_hashes;
  std::unordered_map<crypto::hash, uint64_t> m_all_hashes;
  size_t m_no_new_index;
  size_t m_failed_index;
  size_t m_new_timestamp_index;
  crypto::hash m_last_tx;

  bool check_changed(cryptonote::core& c, size_t ev_index, relay_test condition);

public:
  txpool_double_spend_base();

  bool mark_no_new(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
  bool mark_failed(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
  bool mark_timestamp_change(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);

  //! Pause for 1 second, so that `receive_time` for tx meta changes (tx hidden from public rpc being updated)
  bool timestamp_change_pause(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);

  bool check_unchanged(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
  bool check_new_broadcasted(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
  bool check_new_hidden(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
  bool check_new_no_relay(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);

  bool check_tx_verification_context(const cryptonote::tx_verification_context& tvc, bool tx_added, size_t event_idx, const cryptonote::transaction& /*tx*/);
};

struct txpool_double_spend_norelay : txpool_double_spend_base
{
  txpool_double_spend_norelay()
    : txpool_double_spend_base()
  {}

  bool generate(std::vector<test_event_entry>& events) const;
};

struct txpool_double_spend_local : txpool_double_spend_base
{
  txpool_double_spend_local()
    : txpool_double_spend_base()
  {}

  bool generate(std::vector<test_event_entry>& events) const;
};

struct txpool_double_spend_keyimage : txpool_double_spend_base
{
  txpool_double_spend_keyimage()
    : txpool_double_spend_base()
  {}

  bool generate(std::vector<test_event_entry>& events) const;
};