monero/tests/functional_tests/transactions_flow_test.cpp

307 lines
11 KiB
C++
Raw Normal View History

// Copyright (c) 2014-2020, The Monero Project
2014-07-23 07:03:52 -06:00
//
// 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.
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
2014-03-03 15:07:58 -07:00
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
2014-03-03 15:07:58 -07:00
#include <boost/uuid/random_generator.hpp>
#include <unordered_map>
#include "include_base_utils.h"
using namespace epee;
#include "wallet/wallet2.h"
using namespace cryptonote;
2014-09-04 20:14:36 -06:00
namespace
{
uint64_t const TEST_FEE = 5000000000; // 5 * 10^9
}
2014-03-03 15:07:58 -07:00
std::string generate_random_wallet_name()
{
std::stringstream ss;
ss << boost::uuids::random_generator()();
return ss.str();
}
inline uint64_t random(const uint64_t max_value) {
return (uint64_t(rand()) ^
(uint64_t(rand())<<16) ^
(uint64_t(rand())<<32) ^
(uint64_t(rand())<<48)) % max_value;
}
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)
{
CHECK_AND_ASSERT_MES(parts > 0, false, "parts must be > 0");
std::vector<cryptonote::tx_destination_entry> dsts;
dsts.reserve(parts);
uint64_t amount_used = 0;
uint64_t max_part = amount_to_transfer / parts;
for (size_t i = 0; i < parts; ++i)
{
cryptonote::tx_destination_entry de;
de.addr = w2.get_account().get_keys().m_account_address;
if (i < parts - 1)
de.amount = random(max_part);
else
de.amount = amount_to_transfer - amount_used;
amount_used += de.amount;
//std::cout << "PARTS (" << amount_to_transfer << ") " << amount_used << " " << de.amount << std::endl;
dsts.push_back(de);
}
2014-04-02 10:00:17 -06:00
try
{
std::vector<tools::wallet2::pending_tx> ptx;
ptx = w1.create_transactions_2(dsts, mix_in_factor, 0, 0, std::vector<uint8_t>(), 0, {});
for (auto &p: ptx)
w1.commit_tx(p);
2014-04-02 10:00:17 -06:00
return true;
}
catch (const std::exception&)
{
return false;
}
2014-03-03 15:07:58 -07:00
}
uint64_t get_money_in_first_transfers(const tools::wallet2::transfer_container& incoming_transfers, size_t n_transfers)
{
uint64_t summ = 0;
size_t count = 0;
BOOST_FOREACH(const tools::wallet2::transfer_details& td, incoming_transfers)
{
summ += td.m_tx.vout[td.m_internal_output_index].amount;
if(++count >= n_transfers)
return summ;
}
return summ;
}
#define FIRST_N_TRANSFERS 10*10
bool transactions_flow_test(std::string& working_folder,
std::string path_source_wallet,
2015-05-17 06:16:26 -06:00
std::string path_target_wallet,
2014-03-03 15:07:58 -07:00
std::string& daemon_addr_a,
std::string& daemon_addr_b,
uint64_t amount_to_transfer, size_t mix_in_factor, size_t transactions_count, size_t transactions_per_second)
{
LOG_PRINT_L0("-----------------------STARTING TRANSACTIONS FLOW TEST-----------------------");
tools::wallet2 w1, w2;
if(path_source_wallet.empty())
path_source_wallet = generate_random_wallet_name();
2015-05-17 06:16:26 -06:00
if(path_target_wallet.empty())
path_target_wallet = generate_random_wallet_name();
2014-03-03 15:07:58 -07:00
2014-04-02 10:00:17 -06:00
try
2014-03-03 15:07:58 -07:00
{
2014-04-02 10:00:17 -06:00
w1.generate(working_folder + "/" + path_source_wallet, "");
2015-05-17 06:16:26 -06:00
w2.generate(working_folder + "/" + path_target_wallet, "");
2014-03-03 15:07:58 -07:00
}
2014-04-02 10:00:17 -06:00
catch (const std::exception& e)
2014-03-03 15:07:58 -07:00
{
2014-04-02 10:00:17 -06:00
LOG_ERROR("failed to generate wallet: " << e.what());
2014-03-03 15:07:58 -07:00
return false;
}
w1.init(daemon_addr_a);
2014-04-02 10:00:17 -06:00
2015-11-17 09:31:37 -07:00
uint64_t blocks_fetched = 0;
2014-04-02 10:00:17 -06:00
bool received_money;
bool ok;
if(!w1.refresh(true, blocks_fetched, received_money, ok))
2014-03-03 15:07:58 -07:00
{
LOG_ERROR( "failed to refresh source wallet from " << daemon_addr_a );
return false;
}
w2.init(daemon_addr_b);
2014-04-02 10:00:17 -06:00
Change logging to easylogging++ This replaces the epee and data_loggers logging systems with a single one, and also adds filename:line and explicit severity levels. Categories may be defined, and logging severity set by category (or set of categories). epee style 0-4 log level maps to a sensible severity configuration. Log files now also rotate when reaching 100 MB. To select which logs to output, use the MONERO_LOGS environment variable, with a comma separated list of categories (globs are supported), with their requested severity level after a colon. If a log matches more than one such setting, the last one in the configuration string applies. A few examples: This one is (mostly) silent, only outputting fatal errors: MONERO_LOGS=*:FATAL This one is very verbose: MONERO_LOGS=*:TRACE This one is totally silent (logwise): MONERO_LOGS="" This one outputs all errors and warnings, except for the "verify" category, which prints just fatal errors (the verify category is used for logs about incoming transactions and blocks, and it is expected that some/many will fail to verify, hence we don't want the spam): MONERO_LOGS=*:WARNING,verify:FATAL Log levels are, in decreasing order of priority: FATAL, ERROR, WARNING, INFO, DEBUG, TRACE Subcategories may be added using prefixes and globs. This example will output net.p2p logs at the TRACE level, but all other net* logs only at INFO: MONERO_LOGS=*:ERROR,net*:INFO,net.p2p:TRACE Logs which are intended for the user (which Monero was using a lot through epee, but really isn't a nice way to go things) should use the "global" category. There are a few helper macros for using this category, eg: MGINFO("this shows up by default") or MGINFO_RED("this is red"), to try to keep a similar look and feel for now. Existing epee log macros still exist, and map to the new log levels, but since they're used as a "user facing" UI element as much as a logging system, they often don't map well to log severities (ie, a log level 0 log may be an error, or may be something we want the user to see, such as an important info). In those cases, I tried to use the new macros. In other cases, I left the existing macros in. When modifying logs, it is probably best to switch to the new macros with explicit levels. The --log-level options and set_log commands now also accept category settings, in addition to the epee style log levels.
2017-01-01 09:34:23 -07:00
MGINFO_GREEN("Using wallets: " << ENDL
2018-02-16 04:04:04 -07:00
<< "Source: " << w1.get_account().get_public_address_str(MAINNET) << ENDL << "Path: " << working_folder + "/" + path_source_wallet << ENDL
<< "Target: " << w2.get_account().get_public_address_str(MAINNET) << ENDL << "Path: " << working_folder + "/" + path_target_wallet);
2014-03-03 15:07:58 -07:00
//lets do some money
epee::net_utils::http::http_simple_client http_client;
COMMAND_RPC_STOP_MINING::request daemon1_req = AUTO_VAL_INIT(daemon1_req);
COMMAND_RPC_STOP_MINING::response daemon1_rsp = AUTO_VAL_INIT(daemon1_rsp);
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));
2014-03-03 15:07:58 -07:00
CHECK_AND_ASSERT_MES(r, false, "failed to stop mining");
COMMAND_RPC_START_MINING::request daemon_req = AUTO_VAL_INIT(daemon_req);
COMMAND_RPC_START_MINING::response daemon_rsp = AUTO_VAL_INIT(daemon_rsp);
2018-02-16 04:04:04 -07:00
daemon_req.miner_address = w1.get_account().get_public_address_str(MAINNET);
2014-03-03 15:07:58 -07:00
daemon_req.threads_count = 9;
r = net_utils::invoke_http_json("/start_mining", daemon_req, daemon_rsp, http_client, std::chrono::seconds(10));
CHECK_AND_ASSERT_MES(r, false, "failed to start mining getrandom_outs");
CHECK_AND_ASSERT_MES(daemon_rsp.status == CORE_RPC_STATUS_OK, false, "failed to start mining");
2014-03-03 15:07:58 -07:00
//wait for money, until balance will have enough money
w1.refresh(true, blocks_fetched, received_money, ok);
while(w1.unlocked_balance(0, true) < amount_to_transfer)
2014-03-03 15:07:58 -07:00
{
misc_utils::sleep_no_w(1000);
w1.refresh(true, blocks_fetched, received_money, ok);
2014-03-03 15:07:58 -07:00
}
//lets make a lot of small outs to ourselves
2014-05-03 10:19:43 -06:00
//since it is not possible to start from transaction that bigger than 20Kb, we gonna make transactions
2014-03-03 15:07:58 -07:00
//with 500 outs (about 18kb), and we have to wait appropriate count blocks, mined for test wallet
while(true)
{
tools::wallet2::transfer_container incoming_transfers;
w1.get_transfers(incoming_transfers);
if(incoming_transfers.size() > FIRST_N_TRANSFERS && get_money_in_first_transfers(incoming_transfers, FIRST_N_TRANSFERS) < w1.unlocked_balance(0, true) )
2014-03-03 15:07:58 -07:00
{
//lets go!
size_t count = 0;
BOOST_FOREACH(tools::wallet2::transfer_details& td, incoming_transfers)
{
cryptonote::transaction tx_s;
2014-09-04 20:14:36 -06:00
bool r = do_send_money(w1, w1, 0, td.m_tx.vout[td.m_internal_output_index].amount - TEST_FEE, tx_s, 50);
2014-03-03 15:07:58 -07:00
CHECK_AND_ASSERT_MES(r, false, "Failed to send starter tx " << get_transaction_hash(tx_s));
Change logging to easylogging++ This replaces the epee and data_loggers logging systems with a single one, and also adds filename:line and explicit severity levels. Categories may be defined, and logging severity set by category (or set of categories). epee style 0-4 log level maps to a sensible severity configuration. Log files now also rotate when reaching 100 MB. To select which logs to output, use the MONERO_LOGS environment variable, with a comma separated list of categories (globs are supported), with their requested severity level after a colon. If a log matches more than one such setting, the last one in the configuration string applies. A few examples: This one is (mostly) silent, only outputting fatal errors: MONERO_LOGS=*:FATAL This one is very verbose: MONERO_LOGS=*:TRACE This one is totally silent (logwise): MONERO_LOGS="" This one outputs all errors and warnings, except for the "verify" category, which prints just fatal errors (the verify category is used for logs about incoming transactions and blocks, and it is expected that some/many will fail to verify, hence we don't want the spam): MONERO_LOGS=*:WARNING,verify:FATAL Log levels are, in decreasing order of priority: FATAL, ERROR, WARNING, INFO, DEBUG, TRACE Subcategories may be added using prefixes and globs. This example will output net.p2p logs at the TRACE level, but all other net* logs only at INFO: MONERO_LOGS=*:ERROR,net*:INFO,net.p2p:TRACE Logs which are intended for the user (which Monero was using a lot through epee, but really isn't a nice way to go things) should use the "global" category. There are a few helper macros for using this category, eg: MGINFO("this shows up by default") or MGINFO_RED("this is red"), to try to keep a similar look and feel for now. Existing epee log macros still exist, and map to the new log levels, but since they're used as a "user facing" UI element as much as a logging system, they often don't map well to log severities (ie, a log level 0 log may be an error, or may be something we want the user to see, such as an important info). In those cases, I tried to use the new macros. In other cases, I left the existing macros in. When modifying logs, it is probably best to switch to the new macros with explicit levels. The --log-level options and set_log commands now also accept category settings, in addition to the epee style log levels.
2017-01-01 09:34:23 -07:00
MGINFO_GREEN("Starter transaction sent " << get_transaction_hash(tx_s));
2014-03-03 15:07:58 -07:00
if(++count >= FIRST_N_TRANSFERS)
break;
}
break;
}else
{
misc_utils::sleep_no_w(1000);
w1.refresh(true, blocks_fetched, received_money, ok);
2014-03-03 15:07:58 -07:00
}
}
//do actual transfer
uint64_t transfered_money = 0;
uint64_t transfer_size = amount_to_transfer/transactions_count;
size_t i = 0;
struct tx_test_entry
{
transaction tx;
size_t m_received_count;
uint64_t amount_transfered;
};
crypto::key_image lst_sent_ki = AUTO_VAL_INIT(lst_sent_ki);
std::unordered_map<crypto::hash, tx_test_entry> txs;
for(i = 0; i != transactions_count; i++)
{
uint64_t amount_to_tx = (amount_to_transfer - transfered_money) > transfer_size ? transfer_size: (amount_to_transfer - transfered_money);
while(w1.unlocked_balance(0, true) < amount_to_tx + TEST_FEE)
2014-03-03 15:07:58 -07:00
{
misc_utils::sleep_no_w(1000);
LOG_PRINT_L0("not enough money, waiting for cashback or mining");
w1.refresh(true, blocks_fetched, received_money, ok);
2014-03-03 15:07:58 -07:00
}
transaction tx;
/*size_t n_attempts = 0;
while (!do_send_money(w1, w2, mix_in_factor, amount_to_tx, tx)) {
n_attempts++;
std::cout << "failed to transfer money, refresh and try again (attempts=" << n_attempts << ")" << std::endl;
w1.refresh();
}*/
if(!do_send_money(w1, w2, mix_in_factor, amount_to_tx, tx))
{
LOG_PRINT_L0("failed to transfer money, tx: " << get_transaction_hash(tx) << ", refresh and try again" );
w1.refresh(true, blocks_fetched, received_money, ok);
2014-03-03 15:07:58 -07:00
if(!do_send_money(w1, w2, mix_in_factor, amount_to_tx, tx))
{
LOG_PRINT_L0( "failed to transfer money, second chance. tx: " << get_transaction_hash(tx) << ", exit" );
LOCAL_ASSERT(false);
return false;
}
}
lst_sent_ki = boost::get<txin_to_key>(tx.vin[0]).k_image;
transfered_money += amount_to_tx;
LOG_PRINT_L0("transferred " << amount_to_tx << ", i=" << i );
tx_test_entry& ent = txs[get_transaction_hash(tx)] = tx_test_entry{};
2014-03-03 15:07:58 -07:00
ent.amount_transfered = amount_to_tx;
ent.tx = tx;
//if(i % transactions_per_second)
// misc_utils::sleep_no_w(1000);
}
LOG_PRINT_L0( "waiting some new blocks...");
misc_utils::sleep_no_w(DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN*20*1000);//wait two blocks before sync on another wallet on another daemon
LOG_PRINT_L0( "refreshing...");
bool recvd_money = false;
while(w2.refresh(true, blocks_fetched, recvd_money, ok) && ( (blocks_fetched && recvd_money) || !blocks_fetched ) )
2014-03-03 15:07:58 -07:00
{
misc_utils::sleep_no_w(DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN*1000);//wait two blocks before sync on another wallet on another daemon
}
uint64_t money_2 = w2.balance(0, true);
2014-03-03 15:07:58 -07:00
if(money_2 == transfered_money)
{
Change logging to easylogging++ This replaces the epee and data_loggers logging systems with a single one, and also adds filename:line and explicit severity levels. Categories may be defined, and logging severity set by category (or set of categories). epee style 0-4 log level maps to a sensible severity configuration. Log files now also rotate when reaching 100 MB. To select which logs to output, use the MONERO_LOGS environment variable, with a comma separated list of categories (globs are supported), with their requested severity level after a colon. If a log matches more than one such setting, the last one in the configuration string applies. A few examples: This one is (mostly) silent, only outputting fatal errors: MONERO_LOGS=*:FATAL This one is very verbose: MONERO_LOGS=*:TRACE This one is totally silent (logwise): MONERO_LOGS="" This one outputs all errors and warnings, except for the "verify" category, which prints just fatal errors (the verify category is used for logs about incoming transactions and blocks, and it is expected that some/many will fail to verify, hence we don't want the spam): MONERO_LOGS=*:WARNING,verify:FATAL Log levels are, in decreasing order of priority: FATAL, ERROR, WARNING, INFO, DEBUG, TRACE Subcategories may be added using prefixes and globs. This example will output net.p2p logs at the TRACE level, but all other net* logs only at INFO: MONERO_LOGS=*:ERROR,net*:INFO,net.p2p:TRACE Logs which are intended for the user (which Monero was using a lot through epee, but really isn't a nice way to go things) should use the "global" category. There are a few helper macros for using this category, eg: MGINFO("this shows up by default") or MGINFO_RED("this is red"), to try to keep a similar look and feel for now. Existing epee log macros still exist, and map to the new log levels, but since they're used as a "user facing" UI element as much as a logging system, they often don't map well to log severities (ie, a log level 0 log may be an error, or may be something we want the user to see, such as an important info). In those cases, I tried to use the new macros. In other cases, I left the existing macros in. When modifying logs, it is probably best to switch to the new macros with explicit levels. The --log-level options and set_log commands now also accept category settings, in addition to the epee style log levels.
2017-01-01 09:34:23 -07:00
MGINFO_GREEN("-----------------------FINISHING TRANSACTIONS FLOW TEST OK-----------------------");
MGINFO_GREEN("transferred " << print_money(transfered_money) << " via " << i << " transactions" );
2014-03-03 15:07:58 -07:00
return true;
}else
{
tools::wallet2::transfer_container tc;
w2.get_transfers(tc);
BOOST_FOREACH(tools::wallet2::transfer_details& td, tc)
{
auto it = txs.find(td.m_txid);
2014-03-03 15:07:58 -07:00
CHECK_AND_ASSERT_MES(it != txs.end(), false, "transaction not found in local cache");
it->second.m_received_count += 1;
}
BOOST_FOREACH(auto& tx_pair, txs)
{
if(tx_pair.second.m_received_count != 1)
{
Change logging to easylogging++ This replaces the epee and data_loggers logging systems with a single one, and also adds filename:line and explicit severity levels. Categories may be defined, and logging severity set by category (or set of categories). epee style 0-4 log level maps to a sensible severity configuration. Log files now also rotate when reaching 100 MB. To select which logs to output, use the MONERO_LOGS environment variable, with a comma separated list of categories (globs are supported), with their requested severity level after a colon. If a log matches more than one such setting, the last one in the configuration string applies. A few examples: This one is (mostly) silent, only outputting fatal errors: MONERO_LOGS=*:FATAL This one is very verbose: MONERO_LOGS=*:TRACE This one is totally silent (logwise): MONERO_LOGS="" This one outputs all errors and warnings, except for the "verify" category, which prints just fatal errors (the verify category is used for logs about incoming transactions and blocks, and it is expected that some/many will fail to verify, hence we don't want the spam): MONERO_LOGS=*:WARNING,verify:FATAL Log levels are, in decreasing order of priority: FATAL, ERROR, WARNING, INFO, DEBUG, TRACE Subcategories may be added using prefixes and globs. This example will output net.p2p logs at the TRACE level, but all other net* logs only at INFO: MONERO_LOGS=*:ERROR,net*:INFO,net.p2p:TRACE Logs which are intended for the user (which Monero was using a lot through epee, but really isn't a nice way to go things) should use the "global" category. There are a few helper macros for using this category, eg: MGINFO("this shows up by default") or MGINFO_RED("this is red"), to try to keep a similar look and feel for now. Existing epee log macros still exist, and map to the new log levels, but since they're used as a "user facing" UI element as much as a logging system, they often don't map well to log severities (ie, a log level 0 log may be an error, or may be something we want the user to see, such as an important info). In those cases, I tried to use the new macros. In other cases, I left the existing macros in. When modifying logs, it is probably best to switch to the new macros with explicit levels. The --log-level options and set_log commands now also accept category settings, in addition to the epee style log levels.
2017-01-01 09:34:23 -07:00
MERROR("Transaction lost: " << get_transaction_hash(tx_pair.second.tx));
2014-03-03 15:07:58 -07:00
}
}
Change logging to easylogging++ This replaces the epee and data_loggers logging systems with a single one, and also adds filename:line and explicit severity levels. Categories may be defined, and logging severity set by category (or set of categories). epee style 0-4 log level maps to a sensible severity configuration. Log files now also rotate when reaching 100 MB. To select which logs to output, use the MONERO_LOGS environment variable, with a comma separated list of categories (globs are supported), with their requested severity level after a colon. If a log matches more than one such setting, the last one in the configuration string applies. A few examples: This one is (mostly) silent, only outputting fatal errors: MONERO_LOGS=*:FATAL This one is very verbose: MONERO_LOGS=*:TRACE This one is totally silent (logwise): MONERO_LOGS="" This one outputs all errors and warnings, except for the "verify" category, which prints just fatal errors (the verify category is used for logs about incoming transactions and blocks, and it is expected that some/many will fail to verify, hence we don't want the spam): MONERO_LOGS=*:WARNING,verify:FATAL Log levels are, in decreasing order of priority: FATAL, ERROR, WARNING, INFO, DEBUG, TRACE Subcategories may be added using prefixes and globs. This example will output net.p2p logs at the TRACE level, but all other net* logs only at INFO: MONERO_LOGS=*:ERROR,net*:INFO,net.p2p:TRACE Logs which are intended for the user (which Monero was using a lot through epee, but really isn't a nice way to go things) should use the "global" category. There are a few helper macros for using this category, eg: MGINFO("this shows up by default") or MGINFO_RED("this is red"), to try to keep a similar look and feel for now. Existing epee log macros still exist, and map to the new log levels, but since they're used as a "user facing" UI element as much as a logging system, they often don't map well to log severities (ie, a log level 0 log may be an error, or may be something we want the user to see, such as an important info). In those cases, I tried to use the new macros. In other cases, I left the existing macros in. When modifying logs, it is probably best to switch to the new macros with explicit levels. The --log-level options and set_log commands now also accept category settings, in addition to the epee style log levels.
2017-01-01 09:34:23 -07:00
MERROR("-----------------------FINISHING TRANSACTIONS FLOW TEST FAILED-----------------------" );
MERROR("income " << print_money(money_2) << " via " << i << " transactions, expected money = " << print_money(transfered_money) );
2014-03-03 15:07:58 -07:00
LOCAL_ASSERT(false);
return false;
}
return true;
}