Debian Codegraph @

tests/core_tests/double_spend.inl

raw
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
33 //======================================================================================================================
34
35 template<class concrete_test>
36 gen_double_spend_base<concrete_test>::gen_double_spend_base()
37 : m_invalid_tx_index(invalid_index_value)
38 , m_invalid_block_index(invalid_index_value)
39 {
40 REGISTER_CALLBACK_METHOD(gen_double_spend_base<concrete_test>, mark_last_valid_block);
41 REGISTER_CALLBACK_METHOD(gen_double_spend_base<concrete_test>, mark_invalid_tx);
42 REGISTER_CALLBACK_METHOD(gen_double_spend_base<concrete_test>, mark_invalid_block);
43 REGISTER_CALLBACK_METHOD(gen_double_spend_base<concrete_test>, check_double_spend);
44 }
45
46 template<class concrete_test>
47 bool gen_double_spend_base<concrete_test>::check_tx_verification_context(const cryptonote::tx_verification_context& tvc, bool tx_added, size_t event_idx, const cryptonote::transaction& /*tx*/)
48 {
49 if (m_invalid_tx_index == event_idx)
50 return tvc.m_verifivation_failed;
51 else
52 return !tvc.m_verifivation_failed && tx_added;
53 }
54
55 template<class concrete_test>
56 bool gen_double_spend_base<concrete_test>::check_block_verification_context(const cryptonote::block_verification_context& bvc, size_t event_idx, const cryptonote::block& /*block*/)
57 {
58 if (m_invalid_block_index == event_idx)
59 return bvc.m_verifivation_failed;
60 else
61 return !bvc.m_verifivation_failed;
62 }
63
64 template<class concrete_test>
65 bool gen_double_spend_base<concrete_test>::mark_last_valid_block(cryptonote::core& c, size_t /*ev_index*/, const std::vector<test_event_entry>& /*events*/)
66 {
67 std::vector<cryptonote::block> block_list;
68 bool r = c.get_blocks(c.get_current_blockchain_height() - 1, 1, block_list);
69 CHECK_AND_ASSERT_MES(r, false, "core::get_blocks failed");
70 m_last_valid_block = block_list.back();
71 return true;
72 }
73
74 template<class concrete_test>
75 bool gen_double_spend_base<concrete_test>::mark_invalid_tx(cryptonote::core& /*c*/, size_t ev_index, const std::vector<test_event_entry>& /*events*/)
76 {
77 m_invalid_tx_index = ev_index + 1;
78 return true;
79 }
80
81 template<class concrete_test>
82 bool gen_double_spend_base<concrete_test>::mark_invalid_block(cryptonote::core& /*c*/, size_t ev_index, const std::vector<test_event_entry>& /*events*/)
83 {
84 m_invalid_block_index = ev_index + 1;
85 return true;
86 }
87
88 template<class concrete_test>
89 bool gen_double_spend_base<concrete_test>::check_double_spend(cryptonote::core& c, size_t /*ev_index*/, const std::vector<test_event_entry>& events)
90 {
91 DEFINE_TESTS_ERROR_CONTEXT("gen_double_spend_base::check_double_spend");
92
93 if (concrete_test::has_invalid_tx)
94 {
95 CHECK_NOT_EQ(invalid_index_value, m_invalid_tx_index);
96 }
97 CHECK_NOT_EQ(invalid_index_value, m_invalid_block_index);
98
99 std::vector<cryptonote::block> block_list;
100 bool r = c.get_blocks(0, 100 + 2 * CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW, block_list);
101 CHECK_TEST_CONDITION(r);
102 CHECK_TEST_CONDITION(m_last_valid_block == block_list.back());
103
104 CHECK_EQ(concrete_test::expected_pool_txs_count, c.get_pool_transactions_count());
105
106 cryptonote::account_base bob_account = boost::get<cryptonote::account_base>(events[1]);
107 cryptonote::account_base alice_account = boost::get<cryptonote::account_base>(events[2]);
108
109 std::vector<cryptonote::block> chain;
110 map_hash2tx_t mtx;
111 std::vector<cryptonote::block> blocks(block_list.begin(), block_list.end());
112 r = find_block_chain(events, chain, mtx, get_block_hash(blocks.back()));
113 CHECK_TEST_CONDITION(r);
114 CHECK_EQ(concrete_test::expected_bob_balance, get_balance(bob_account, blocks, mtx));
115 CHECK_EQ(concrete_test::expected_alice_balance, get_balance(alice_account, blocks, mtx));
116
117 return true;
118 }
119
120 //======================================================================================================================
121
122 template<bool txs_keeped_by_block>
123 bool gen_double_spend_in_tx<txs_keeped_by_block>::generate(std::vector<test_event_entry>& events) const
124 {
125 INIT_DOUBLE_SPEND_TEST();
126 DO_CALLBACK(events, "mark_last_valid_block");
127
128 std::vector<cryptonote::tx_source_entry> sources;
129 cryptonote::tx_source_entry se;
130 se.amount = tx_0.vout[0].amount;
131 se.push_output(0, boost::get<cryptonote::txout_to_key>(tx_0.vout[0].target).key, se.amount);
132 se.real_output = 0;
133 se.rct = false;
134 se.real_out_tx_key = get_tx_pub_key_from_extra(tx_0);
135 se.real_output_in_tx_index = 0;
136 sources.push_back(se);
137 // Double spend!
138 sources.push_back(se);
139
140 cryptonote::tx_destination_entry de;
141 de.addr = alice_account.get_keys().m_account_address;
142 de.amount = 2 * se.amount - TESTS_DEFAULT_FEE;
143 std::vector<cryptonote::tx_destination_entry> destinations;
144 destinations.push_back(de);
145
146 cryptonote::transaction tx_1;
147 if (!construct_tx(bob_account.get_keys(), sources, destinations, boost::none, std::vector<uint8_t>(), tx_1))
148 return false;
149
150 SET_EVENT_VISITOR_SETT(events, txs_keeped_by_block ? event_visitor_settings::set_txs_keeped_by_block : 0);
151 DO_CALLBACK(events, "mark_invalid_tx");
152 events.push_back(tx_1);
153 DO_CALLBACK(events, "mark_invalid_block");
154 MAKE_NEXT_BLOCK_TX1(events, blk_2, blk_1r, miner_account, tx_1);
155 DO_CALLBACK(events, "check_double_spend");
156
157 return true;
158 }
159
160 template<bool txs_keeped_by_block>
161 bool gen_double_spend_in_the_same_block<txs_keeped_by_block>::generate(std::vector<test_event_entry>& events) const
162 {
163 INIT_DOUBLE_SPEND_TEST();
164
165 DO_CALLBACK(events, "mark_last_valid_block");
166 SET_EVENT_VISITOR_SETT(events, txs_keeped_by_block ? event_visitor_settings::set_txs_keeped_by_block : 0);
167
168 MAKE_TX_LIST_START(events, txs_1, bob_account, alice_account, send_amount - TESTS_DEFAULT_FEE, blk_1);
169 cryptonote::transaction tx_1 = txs_1.front();
170 auto tx_1_idx = events.size() - 1;
171 // Remove tx_1, it is being inserted back a little later
172 events.pop_back();
173
174 if (has_invalid_tx)
175 {
176 DO_CALLBACK(events, "mark_invalid_tx");
177 }
178 MAKE_TX_LIST(events, txs_1, bob_account, alice_account, send_amount - TESTS_DEFAULT_FEE, blk_1);
179 events.insert(events.begin() + tx_1_idx, tx_1);
180 DO_CALLBACK(events, "mark_invalid_block");
181 MAKE_NEXT_BLOCK_TX_LIST(events, blk_2, blk_1r, miner_account, txs_1);
182 DO_CALLBACK(events, "check_double_spend");
183
184 return true;
185 }
186
187 template<bool txs_keeped_by_block>
188 bool gen_double_spend_in_different_blocks<txs_keeped_by_block>::generate(std::vector<test_event_entry>& events) const
189 {
190 INIT_DOUBLE_SPEND_TEST();
191
192 DO_CALLBACK(events, "mark_last_valid_block");
193 SET_EVENT_VISITOR_SETT(events, txs_keeped_by_block ? event_visitor_settings::set_txs_keeped_by_block : 0);
194
195 // Create two identical transactions, but don't push it to events list
196 MAKE_TX(events, tx_blk_2, bob_account, alice_account, send_amount - TESTS_DEFAULT_FEE, blk_1);
197 events.pop_back();
198 MAKE_TX(events, tx_blk_3, bob_account, alice_account, send_amount - TESTS_DEFAULT_FEE, blk_1);
199 events.pop_back();
200
201 events.push_back(tx_blk_2);
202 MAKE_NEXT_BLOCK_TX1(events, blk_2, blk_1r, miner_account, tx_blk_2);
203 DO_CALLBACK(events, "mark_last_valid_block");
204
205 if (has_invalid_tx)
206 {
207 DO_CALLBACK(events, "mark_invalid_tx");
208 }
209 events.push_back(tx_blk_3);
210 DO_CALLBACK(events, "mark_invalid_block");
211 MAKE_NEXT_BLOCK_TX1(events, blk_3, blk_2, miner_account, tx_blk_3);
212
213 DO_CALLBACK(events, "check_double_spend");
214
215 return true;
216 }
217
218 template<bool txs_keeped_by_block>
219 bool gen_double_spend_in_alt_chain_in_the_same_block<txs_keeped_by_block>::generate(std::vector<test_event_entry>& events) const
220 {
221 INIT_DOUBLE_SPEND_TEST();
222
223 SET_EVENT_VISITOR_SETT(events, txs_keeped_by_block ? event_visitor_settings::set_txs_keeped_by_block : 0);
224
225 // Main chain
226 MAKE_NEXT_BLOCK(events, blk_2, blk_1r, miner_account);
227 DO_CALLBACK(events, "mark_last_valid_block");
228
229 // Alt chain
230 MAKE_TX_LIST_START(events, txs_1, bob_account, alice_account, send_amount - TESTS_DEFAULT_FEE, blk_1);
231 cryptonote::transaction tx_1 = txs_1.front();
232 auto tx_1_idx = events.size() - 1;
233 // Remove tx_1, it is being inserted back a little later
234 events.pop_back();
235
236 if (has_invalid_tx)
237 {
238 DO_CALLBACK(events, "mark_invalid_tx");
239 }
240 MAKE_TX_LIST(events, txs_1, bob_account, alice_account, send_amount - TESTS_DEFAULT_FEE, blk_1);
241 events.insert(events.begin() + tx_1_idx, tx_1);
242 MAKE_NEXT_BLOCK_TX_LIST(events, blk_3, blk_1r, miner_account, txs_1);
243
244 // Try to switch to alternative chain
245 DO_CALLBACK(events, "mark_invalid_block");
246 MAKE_NEXT_BLOCK(events, blk_4, blk_3, miner_account);
247
248 DO_CALLBACK(events, "check_double_spend");
249
250 return true;
251 }
252
253 template<bool txs_keeped_by_block>
254 bool gen_double_spend_in_alt_chain_in_different_blocks<txs_keeped_by_block>::generate(std::vector<test_event_entry>& events) const
255 {
256 INIT_DOUBLE_SPEND_TEST();
257
258 SET_EVENT_VISITOR_SETT(events, txs_keeped_by_block ? event_visitor_settings::set_txs_keeped_by_block : 0);
259
260 // Main chain
261 MAKE_NEXT_BLOCK(events, blk_2, blk_1r, miner_account);
262 DO_CALLBACK(events, "mark_last_valid_block");
263
264 // Alternative chain
265 MAKE_TX(events, tx_1, bob_account, alice_account, send_amount - TESTS_DEFAULT_FEE, blk_1);
266 events.pop_back();
267 MAKE_TX(events, tx_2, bob_account, alice_account, send_amount - TESTS_DEFAULT_FEE, blk_1);
268 events.pop_back();
269
270 events.push_back(tx_1);
271 MAKE_NEXT_BLOCK_TX1(events, blk_3, blk_1r, miner_account, tx_1);
272
273 // Try to switch to alternative chain
274 if (has_invalid_tx)
275 {
276 DO_CALLBACK(events, "mark_invalid_tx");
277 }
278 events.push_back(tx_2);
279 DO_CALLBACK(events, "mark_invalid_block");
280 MAKE_NEXT_BLOCK_TX1(events, blk_4, blk_3, miner_account, tx_2);
281
282 DO_CALLBACK(events, "check_double_spend");
283
284 return true;
285 }