Debian Codegraph @

tests/functional_tests/transactions_flow_test.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 <boost/uuid/uuid.hpp>
32 #include <boost/uuid/uuid_io.hpp>
33 #include <boost/uuid/random_generator.hpp>
34 #include <unordered_map>
35
36 #include "include_base_utils.h"
37 using namespace epee;
38 #include "wallet/wallet2.h"
39 using namespace cryptonote;
40
41 namespace
42 {
43 uint64_t const TEST_FEE = 5000000000; // 5 * 10^9
44 }
45
46 std::string generate_random_wallet_name()
47 {
48 std::stringstream ss;
49 ss << boost::uuids::random_generator()();
50 return ss.str();
51 }
52
53 inline uint64_t random(const uint64_t max_value) {
54 return (uint64_t(rand()) ^
55 (uint64_t(rand())<<16) ^
56 (uint64_t(rand())<<32) ^
57 (uint64_t(rand())<<48)) % max_value;
58 }
59
60 bool do_send_money(tools::wallet2& w1, tools::wallet2& w2, size_t mix_in_factor, uint64_t amount_to_transfer, transaction& tx, size_t parts=1)
61 {
62 CHECK_AND_ASSERT_MES(parts > 0, false, "parts must be > 0");
63
64 std::vector<cryptonote::tx_destination_entry> dsts;
65 dsts.reserve(parts);
66 uint64_t amount_used = 0;
67 uint64_t max_part = amount_to_transfer / parts;
68
69 for (size_t i = 0; i < parts; ++i)
70 {
71 cryptonote::tx_destination_entry de;
72 de.addr = w2.get_account().get_keys().m_account_address;
73
74 if (i < parts - 1)
75 de.amount = random(max_part);
76 else
77 de.amount = amount_to_transfer - amount_used;
78 amount_used += de.amount;
79
80 //std::cout << "PARTS (" << amount_to_transfer << ") " << amount_used << " " << de.amount << std::endl;
81
82 dsts.push_back(de);
83 }
84
85 try
86 {
87 std::vector<tools::wallet2::pending_tx> ptx;
88 ptx = w1.create_transactions_2(dsts, mix_in_factor, 0, std::vector<uint8_t>(), 0, {});
89 for (auto &p: ptx)
90 w1.commit_tx(p);
91 return true;
92 }
93 catch (const std::exception&)
94 {
95 return false;
96 }
97 }
98
99 uint64_t get_money_in_first_transfers(const tools::wallet2::transfer_container& incoming_transfers, size_t n_transfers)
100 {
101 uint64_t summ = 0;
102 size_t count = 0;
103 BOOST_FOREACH(const tools::wallet2::transfer_details& td, incoming_transfers)
104 {
105 summ += td.m_tx.vout[td.m_internal_output_index].amount;
106 if(++count >= n_transfers)
107 return summ;
108 }
109 return summ;
110 }
111
112 #define FIRST_N_TRANSFERS 10*10
113
114 bool transactions_flow_test(std::string& working_folder,
115 std::string path_source_wallet,
116 std::string path_target_wallet,
117 std::string& daemon_addr_a,
118 std::string& daemon_addr_b,
119 uint64_t amount_to_transfer, size_t mix_in_factor, size_t transactions_count, size_t transactions_per_second)
120 {
121 LOG_PRINT_L0("-----------------------STARTING TRANSACTIONS FLOW TEST-----------------------");
122 tools::wallet2 w1, w2;
123 if(path_source_wallet.empty())
124 path_source_wallet = generate_random_wallet_name();
125
126 if(path_target_wallet.empty())
127 path_target_wallet = generate_random_wallet_name();
128
129
130 try
131 {
132 w1.generate(working_folder + "/" + path_source_wallet, "");
133 w2.generate(working_folder + "/" + path_target_wallet, "");
134 }
135 catch (const std::exception& e)
136 {
137 LOG_ERROR("failed to generate wallet: " << e.what());
138 return false;
139 }
140
141 w1.init(daemon_addr_a);
142
143 uint64_t blocks_fetched = 0;
144 bool received_money;
145 bool ok;
146 if(!w1.refresh(true, blocks_fetched, received_money, ok))
147 {
148 LOG_ERROR( "failed to refresh source wallet from " << daemon_addr_a );
149 return false;
150 }
151
152 w2.init(daemon_addr_b);
153
154 MGINFO_GREEN("Using wallets: " << ENDL
155 << "Source: " << w1.get_account().get_public_address_str(MAINNET) << ENDL << "Path: " << working_folder + "/" + path_source_wallet << ENDL
156 << "Target: " << w2.get_account().get_public_address_str(MAINNET) << ENDL << "Path: " << working_folder + "/" + path_target_wallet);
157
158 //lets do some money
159 epee::net_utils::http::http_simple_client http_client;
160 COMMAND_RPC_STOP_MINING::request daemon1_req = AUTO_VAL_INIT(daemon1_req);
161 COMMAND_RPC_STOP_MINING::response daemon1_rsp = AUTO_VAL_INIT(daemon1_rsp);
162 bool r = http_client.set_server(daemon_addr_a, boost::none) && net_utils::invoke_http_json("/stop_mine", daemon1_req, daemon1_rsp, http_client, std::chrono::seconds(10));
163 CHECK_AND_ASSERT_MES(r, false, "failed to stop mining");
164
165 COMMAND_RPC_START_MINING::request daemon_req = AUTO_VAL_INIT(daemon_req);
166 COMMAND_RPC_START_MINING::response daemon_rsp = AUTO_VAL_INIT(daemon_rsp);
167 daemon_req.miner_address = w1.get_account().get_public_address_str(MAINNET);
168 daemon_req.threads_count = 9;
169 r = net_utils::invoke_http_json("/start_mining", daemon_req, daemon_rsp, http_client, std::chrono::seconds(10));
170 CHECK_AND_ASSERT_MES(r, false, "failed to start mining getrandom_outs");
171 CHECK_AND_ASSERT_MES(daemon_rsp.status == CORE_RPC_STATUS_OK, false, "failed to start mining");
172
173 //wait for money, until balance will have enough money
174 w1.refresh(true, blocks_fetched, received_money, ok);
175 while(w1.unlocked_balance(0, true) < amount_to_transfer)
176 {
177 misc_utils::sleep_no_w(1000);
178 w1.refresh(true, blocks_fetched, received_money, ok);
179 }
180
181 //lets make a lot of small outs to ourselves
182 //since it is not possible to start from transaction that bigger than 20Kb, we gonna make transactions
183 //with 500 outs (about 18kb), and we have to wait appropriate count blocks, mined for test wallet
184 while(true)
185 {
186 tools::wallet2::transfer_container incoming_transfers;
187 w1.get_transfers(incoming_transfers);
188 if(incoming_transfers.size() > FIRST_N_TRANSFERS && get_money_in_first_transfers(incoming_transfers, FIRST_N_TRANSFERS) < w1.unlocked_balance(0, true) )
189 {
190 //lets go!
191 size_t count = 0;
192 BOOST_FOREACH(tools::wallet2::transfer_details& td, incoming_transfers)
193 {
194 cryptonote::transaction tx_s;
195 bool r = do_send_money(w1, w1, 0, td.m_tx.vout[td.m_internal_output_index].amount - TEST_FEE, tx_s, 50);
196 CHECK_AND_ASSERT_MES(r, false, "Failed to send starter tx " << get_transaction_hash(tx_s));
197 MGINFO_GREEN("Starter transaction sent " << get_transaction_hash(tx_s));
198 if(++count >= FIRST_N_TRANSFERS)
199 break;
200 }
201 break;
202 }else
203 {
204 misc_utils::sleep_no_w(1000);
205 w1.refresh(true, blocks_fetched, received_money, ok);
206 }
207 }
208 //do actual transfer
209 uint64_t transfered_money = 0;
210 uint64_t transfer_size = amount_to_transfer/transactions_count;
211 size_t i = 0;
212 struct tx_test_entry
213 {
214 transaction tx;
215 size_t m_received_count;
216 uint64_t amount_transfered;
217 };
218 crypto::key_image lst_sent_ki = AUTO_VAL_INIT(lst_sent_ki);
219 std::unordered_map<crypto::hash, tx_test_entry> txs;
220 for(i = 0; i != transactions_count; i++)
221 {
222 uint64_t amount_to_tx = (amount_to_transfer - transfered_money) > transfer_size ? transfer_size: (amount_to_transfer - transfered_money);
223 while(w1.unlocked_balance(0, true) < amount_to_tx + TEST_FEE)
224 {
225 misc_utils::sleep_no_w(1000);
226 LOG_PRINT_L0("not enough money, waiting for cashback or mining");
227 w1.refresh(true, blocks_fetched, received_money, ok);
228 }
229
230 transaction tx;
231 /*size_t n_attempts = 0;
232 while (!do_send_money(w1, w2, mix_in_factor, amount_to_tx, tx)) {
233 n_attempts++;
234 std::cout << "failed to transfer money, refresh and try again (attempts=" << n_attempts << ")" << std::endl;
235 w1.refresh();
236 }*/
237
238
239 if(!do_send_money(w1, w2, mix_in_factor, amount_to_tx, tx))
240 {
241 LOG_PRINT_L0("failed to transfer money, tx: " << get_transaction_hash(tx) << ", refresh and try again" );
242 w1.refresh(true, blocks_fetched, received_money, ok);
243 if(!do_send_money(w1, w2, mix_in_factor, amount_to_tx, tx))
244 {
245 LOG_PRINT_L0( "failed to transfer money, second chance. tx: " << get_transaction_hash(tx) << ", exit" );
246 LOCAL_ASSERT(false);
247 return false;
248 }
249 }
250 lst_sent_ki = boost::get<txin_to_key>(tx.vin[0]).k_image;
251
252 transfered_money += amount_to_tx;
253
254 LOG_PRINT_L0("transferred " << amount_to_tx << ", i=" << i );
255 tx_test_entry& ent = txs[get_transaction_hash(tx)] = tx_test_entry{};
256 ent.amount_transfered = amount_to_tx;
257 ent.tx = tx;
258 //if(i % transactions_per_second)
259 // misc_utils::sleep_no_w(1000);
260 }
261
262
263 LOG_PRINT_L0( "waiting some new blocks...");
264 misc_utils::sleep_no_w(DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN*20*1000);//wait two blocks before sync on another wallet on another daemon
265 LOG_PRINT_L0( "refreshing...");
266 bool recvd_money = false;
267 while(w2.refresh(true, blocks_fetched, recvd_money, ok) && ( (blocks_fetched && recvd_money) || !blocks_fetched ) )
268 {
269 misc_utils::sleep_no_w(DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN*1000);//wait two blocks before sync on another wallet on another daemon
270 }
271
272 uint64_t money_2 = w2.balance(0, true);
273 if(money_2 == transfered_money)
274 {
275 MGINFO_GREEN("-----------------------FINISHING TRANSACTIONS FLOW TEST OK-----------------------");
276 MGINFO_GREEN("transferred " << print_money(transfered_money) << " via " << i << " transactions" );
277 return true;
278 }else
279 {
280 tools::wallet2::transfer_container tc;
281 w2.get_transfers(tc);
282 BOOST_FOREACH(tools::wallet2::transfer_details& td, tc)
283 {
284 auto it = txs.find(td.m_txid);
285 CHECK_AND_ASSERT_MES(it != txs.end(), false, "transaction not found in local cache");
286 it->second.m_received_count += 1;
287 }
288
289 BOOST_FOREACH(auto& tx_pair, txs)
290 {
291 if(tx_pair.second.m_received_count != 1)
292 {
293 MERROR("Transaction lost: " << get_transaction_hash(tx_pair.second.tx));
294 }
295
296 }
297
298 MERROR("-----------------------FINISHING TRANSACTIONS FLOW TEST FAILED-----------------------" );
299 MERROR("income " << print_money(money_2) << " via " << i << " transactions, expected money = " << print_money(transfered_money) );
300 LOCAL_ASSERT(false);
301 return false;
302 }
303
304 return true;
305 }
306