Monero
double_spend.h
Go to the documentation of this file.
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 #pragma once
32 #include "chaingen.h"
33 
34 const size_t invalid_index_value = std::numeric_limits<size_t>::max();
35 const uint64_t FIRST_BLOCK_REWARD = 17592186044415;
36 
37 
38 template<class concrete_test>
40 {
41 public:
43 
45 
46  bool check_tx_verification_context(const cryptonote::tx_verification_context& tvc, bool tx_added, size_t event_idx, const cryptonote::transaction& tx);
48 
49  bool mark_last_valid_block(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
50  bool mark_invalid_tx(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
51  bool mark_invalid_block(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
52  bool check_double_spend(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
53 
54 private:
58 };
59 
60 
61 template<bool txs_keeped_by_block>
62 struct gen_double_spend_in_tx : public gen_double_spend_base< gen_double_spend_in_tx<txs_keeped_by_block> >
63 {
65  static const bool has_invalid_tx = true;
66  static const size_t expected_pool_txs_count = 0;
68  static const uint64_t expected_alice_balance = 0;
69 
70  bool generate(std::vector<test_event_entry>& events) const;
71 };
72 
73 
74 template<bool txs_keeped_by_block>
75 struct gen_double_spend_in_the_same_block : public gen_double_spend_base< gen_double_spend_in_the_same_block<txs_keeped_by_block> >
76 {
78  static const bool has_invalid_tx = !txs_keeped_by_block;
79  static const size_t expected_pool_txs_count = has_invalid_tx ? 1 : 2;
81  static const uint64_t expected_alice_balance = 0;
82 
83  bool generate(std::vector<test_event_entry>& events) const;
84 };
85 
86 
87 template<bool txs_keeped_by_block>
88 struct gen_double_spend_in_different_blocks : public gen_double_spend_base< gen_double_spend_in_different_blocks<txs_keeped_by_block> >
89 {
91  static const bool has_invalid_tx = !txs_keeped_by_block;
92  static const size_t expected_pool_txs_count = has_invalid_tx ? 0 : 1;
93  static const uint64_t expected_bob_balance = 0;
95 
96  bool generate(std::vector<test_event_entry>& events) const;
97 };
98 
99 
100 template<bool txs_keeped_by_block>
101 struct gen_double_spend_in_alt_chain_in_the_same_block : public gen_double_spend_base< gen_double_spend_in_alt_chain_in_the_same_block<txs_keeped_by_block> >
102 {
104  static const bool has_invalid_tx = !txs_keeped_by_block;
105  static const size_t expected_pool_txs_count = has_invalid_tx ? 1 : 2;
108 
109  bool generate(std::vector<test_event_entry>& events) const;
110 };
111 
112 
113 template<bool txs_keeped_by_block>
114 struct gen_double_spend_in_alt_chain_in_different_blocks : public gen_double_spend_base< gen_double_spend_in_alt_chain_in_different_blocks<txs_keeped_by_block> >
115 {
117  static const bool has_invalid_tx = !txs_keeped_by_block;
118  static const size_t expected_pool_txs_count = has_invalid_tx ? 1 : 2;
121 
122  bool generate(std::vector<test_event_entry>& events) const;
123 };
124 
125 
127 {
128 public:
131 
133 
134  bool generate(std::vector<test_event_entry>& events) const;
135 
136  bool check_double_spend(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
137 };
138 
139 
140 #define INIT_DOUBLE_SPEND_TEST() \
141  uint64_t ts_start = 1338224400; \
142  GENERATE_ACCOUNT(miner_account); \
143  MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start); \
144  MAKE_ACCOUNT(events, bob_account); \
145  MAKE_ACCOUNT(events, alice_account); \
146  REWIND_BLOCKS(events, blk_0r, blk_0, miner_account); \
147  MAKE_TX(events, tx_0, miner_account, bob_account, send_amount, blk_0); \
148  MAKE_NEXT_BLOCK_TX1(events, blk_1, blk_0r, miner_account, tx_0); \
149  REWIND_BLOCKS(events, blk_1r, blk_1, miner_account);
150 
151 
152 #include "double_spend.inl"
static const bool has_invalid_tx
Definition: double_spend.h:117
static const uint64_t expected_alice_balance
Definition: double_spend.h:120
static const bool has_invalid_tx
Definition: double_spend.h:91
Definition: double_spend.h:62
static const uint64_t send_amount
Definition: double_spend.h:129
static const size_t expected_pool_txs_count
Definition: double_spend.h:105
static const uint64_t expected_bob_balance
Definition: double_spend.h:80
bool mark_invalid_tx(cryptonote::core &c, size_t ev_index, const std::vector< test_event_entry > &events)
Definition: double_spend.inl:75
Definition: cryptonote_basic.h:474
size_t m_invalid_tx_index
Definition: double_spend.h:56
static const uint64_t expected_bob_balance
Definition: double_spend.h:67
static const uint64_t expected_alice_balance
Definition: double_spend.h:68
static const uint64_t send_amount
Definition: double_spend.h:64
bool generate(std::vector< test_event_entry > &events) const
Definition: double_spend.inl:219
static const uint64_t send_amount
Definition: double_spend.h:42
#define TESTS_DEFAULT_FEE
Definition: chaingen.h:1092
Definition: verification_context.h:65
bool check_double_spend(cryptonote::core &c, size_t ev_index, const std::vector< test_event_entry > &events)
Definition: double_spend.inl:89
static const size_t expected_pool_txs_count
Definition: double_spend.h:66
const uint64_t FIRST_BLOCK_REWARD
Definition: double_spend.h:35
Definition: double_spend.h:88
#define CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW
Definition: cryptonote_config.h:44
bool generate(std::vector< test_event_entry > &events) const
Definition: double_spend.inl:123
Definition: verification_context.h:40
static const uint64_t expected_alice_balance
Definition: double_spend.h:107
static const size_t expected_pool_txs_count
Definition: double_spend.h:79
static const uint64_t send_amount
Definition: double_spend.h:77
gen_double_spend_in_different_chains()
Definition: double_spend.cpp:40
Definition: argon2_core.h:74
static const size_t expected_pool_txs_count
Definition: double_spend.h:92
static const size_t expected_blockchain_height
Definition: double_spend.h:130
static const uint64_t expected_bob_balance
Definition: double_spend.h:106
static const bool has_invalid_tx
Definition: double_spend.h:78
bool mark_invalid_block(cryptonote::core &c, size_t ev_index, const std::vector< test_event_entry > &events)
Definition: double_spend.inl:82
static const uint64_t expected_bob_balance
Definition: double_spend.h:93
handles core cryptonote functionality
Definition: cryptonote_core.h:86
unsigned __int64 uint64_t
Definition: stdint.h:136
bool generate(std::vector< test_event_entry > &events) const
Definition: double_spend.cpp:45
Definition: double_spend.h:126
static const uint64_t send_amount
Definition: double_spend.h:116
bool generate(std::vector< test_event_entry > &events) const
Definition: double_spend.inl:161
static const uint64_t expected_alice_balance
Definition: double_spend.h:94
static const uint64_t send_amount
Definition: double_spend.h:90
static const uint64_t expected_bob_balance
Definition: double_spend.h:119
const size_t invalid_index_value
Definition: double_spend.h:34
Definition: double_spend.h:39
bool check_block_verification_context(const cryptonote::block_verification_context &bvc, size_t event_idx, const cryptonote::block &block)
Definition: double_spend.inl:56
bool mark_last_valid_block(cryptonote::core &c, size_t ev_index, const std::vector< test_event_entry > &events)
Definition: double_spend.inl:65
static const bool has_invalid_tx
Definition: double_spend.h:104
cryptonote::transaction tx
Definition: transaction.cpp:40
static const uint64_t expected_alice_balance
Definition: double_spend.h:81
cryptonote::block m_last_valid_block
Definition: double_spend.h:55
Definition: double_spend.h:75
bool generate(std::vector< test_event_entry > &events) const
Definition: double_spend.inl:188
Definition: chaingen.h:165
bool generate(std::vector< test_event_entry > &events) const
Definition: double_spend.inl:254
static const bool has_invalid_tx
Definition: double_spend.h:65
static const uint64_t send_amount
Definition: double_spend.h:103
bool check_tx_verification_context(const cryptonote::tx_verification_context &tvc, bool tx_added, size_t event_idx, const cryptonote::transaction &tx)
Definition: double_spend.inl:47
Definition: cryptonote_basic.h:204
size_t m_invalid_block_index
Definition: double_spend.h:57
bool check_double_spend(cryptonote::core &c, size_t ev_index, const std::vector< test_event_entry > &events)
Definition: double_spend.cpp:72
c
Definition: pymoduletest.py:79
gen_double_spend_base()
Definition: double_spend.inl:36
static const size_t expected_pool_txs_count
Definition: double_spend.h:118