Skip to content
Permalink
Browse files

tests/trezor: HF9 and HF10 tests

- tests fixes for HF10, builder change, rct_config; fix_chain
- get_tx_key test
- proper testing after live refresh added
- live refresh synthetic test
- log available funds for easier test construction
- wallet::API tests with mocked daemon
  • Loading branch information...
ph4r05 committed Feb 27, 2019
1 parent 6f36a39 commit 4f0e0eead9e423e834e5160304ae8d724f5958ee
@@ -87,7 +87,7 @@ namespace trezor {
m_live_refresh_thread_running = false;
if (m_live_refresh_thread)
{
m_live_refresh_thread->detach();
m_live_refresh_thread->join();
m_live_refresh_thread.release();
}
return r;
@@ -17,7 +17,6 @@ void wallet_accessor_test::set_account(tools::wallet2 * wallet, cryptonote::acco
{
wallet->clear();
wallet->m_account = account;
wallet->m_nettype = MAINNET;

wallet->m_key_device_type = account.get_device().get_type();
wallet->m_account_public_address = account.get_keys().m_account_address;
@@ -27,11 +27,15 @@
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

set(trezor_tests_sources
tools.cpp
daemon.cpp
trezor_tests.cpp
../core_tests/chaingen.cpp
../core_tests/wallet_tools.cpp)

set(trezor_tests_headers
tools.h
daemon.h
trezor_tests.h
../core_tests/chaingen.h
../core_tests/wallet_tools.h)
@@ -50,6 +54,15 @@ target_link_libraries(trezor_tests
device
device_trezor
wallet
wallet_api
rpc
cryptonote_protocol
daemon_rpc_server
${Boost_CHRONO_LIBRARY}
${Boost_FILESYSTEM_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${ZMQ_LIB}
${CMAKE_THREAD_LIBS_INIT}
${EXTRA_LIBRARIES})

@@ -0,0 +1,301 @@
// Copyright (c) 2014-2018, 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.
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers

#include "daemon.h"
#include <common/command_line.h>

using namespace std;
using namespace daemonize;
namespace po = boost::program_options;

bool mock_rpc_daemon::on_send_raw_tx_2(const cryptonote::COMMAND_RPC_SEND_RAW_TX::request& req, cryptonote::COMMAND_RPC_SEND_RAW_TX::response& res, const cryptonote::core_rpc_server::connection_context *ctx)
{
cryptonote::COMMAND_RPC_SEND_RAW_TX::request req2(req);
req2.do_not_relay = true; // Do not relay in test setup, only one daemon running.
return cryptonote::core_rpc_server::on_send_raw_tx(req2, res, ctx);
}

void mock_daemon::init_options(boost::program_options::options_description & option_spec)
{
cryptonote::core::init_options(option_spec);
t_node_server::init_options(option_spec);
cryptonote::core_rpc_server::init_options(option_spec);

command_line::add_arg(option_spec, daemon_args::arg_zmq_rpc_bind_ip);
command_line::add_arg(option_spec, daemon_args::arg_zmq_rpc_bind_port);
}

void mock_daemon::default_options(boost::program_options::variables_map & vm)
{
std::vector<std::string> exclusive_nodes{"127.0.0.1:65525"};
tools::options::set_option(vm, nodetool::arg_p2p_add_exclusive_node, po::variable_value(exclusive_nodes, false));

tools::options::set_option(vm, nodetool::arg_p2p_bind_ip, po::variable_value(std::string("127.0.0.1"), false));
tools::options::set_option(vm, nodetool::arg_no_igd, po::variable_value(true, false));
tools::options::set_option(vm, cryptonote::arg_offline, po::variable_value(true, false));
tools::options::set_option(vm, "disable-dns-checkpoints", po::variable_value(true, false));

const char *test_mainnet = getenv("TEST_MAINNET");
if (!test_mainnet || atoi(test_mainnet) == 0)
{
tools::options::set_option(vm, cryptonote::arg_testnet_on, po::variable_value(true, false));
}

// By default pick non-standard ports to avoid confusion with possibly locally running daemons (mainnet/testnet)
const char *test_p2p_port = getenv("TEST_P2P_PORT");
auto p2p_port = std::string(test_p2p_port && strlen(test_p2p_port) > 0 ? test_p2p_port : "61340");
tools::options::set_option(vm, nodetool::arg_p2p_bind_port, po::variable_value(p2p_port, false));

const char *test_rpc_port = getenv("TEST_RPC_PORT");
auto rpc_port = std::string(test_rpc_port && strlen(test_rpc_port) > 0 ? test_rpc_port : "61341");
tools::options::set_option(vm, cryptonote::core_rpc_server::arg_rpc_bind_port, po::variable_value(rpc_port, false));

const char *test_zmq_port = getenv("TEST_ZMQ_PORT");
auto zmq_port = std::string(test_zmq_port && strlen(test_zmq_port) > 0 ? test_zmq_port : "61342");
tools::options::set_option(vm, daemon_args::arg_zmq_rpc_bind_port, po::variable_value(zmq_port, false));

po::notify(vm);
}

mock_daemon::~mock_daemon()
{
if (!m_terminated)
{
try
{
stop();
}
catch (...)
{
MERROR("Failed to stop");
}
}

try
{
m_rpc_server.deinit();
}
catch (...)
{
MERROR("Failed to deinitialize RPC server...");
}

if (m_start_p2p)
{
try
{
m_server.deinit();
}
catch (...)
{
MERROR("Failed to deinitialize p2p...");
}
}

try
{
m_protocol.deinit();
m_protocol.set_p2p_endpoint(nullptr);
}
catch (...)
{
LOG_ERROR("Failed to stop cryptonote protocol!");
}
}

void mock_daemon::init()
{
const auto main_rpc_port = command_line::get_arg(m_vm, cryptonote::core_rpc_server::arg_rpc_bind_port);
m_rpc_server.nettype(m_network_type);

CHECK_AND_ASSERT_THROW_MES(m_protocol.init(m_vm), "Failed to initialize cryptonote protocol.");
CHECK_AND_ASSERT_THROW_MES(m_rpc_server.init(m_vm, false, main_rpc_port), "Failed to initialize RPC server.");

if (m_start_p2p)
CHECK_AND_ASSERT_THROW_MES(m_server.init(m_vm), "Failed to initialize p2p server.");

if(m_http_client.is_connected())
m_http_client.disconnect();

CHECK_AND_ASSERT_THROW_MES(m_http_client.set_server(rpc_addr(), boost::none), "RPC client init fail");
}

void mock_daemon::run()
{
m_run_thread = boost::thread(boost::bind(&mock_daemon::run_main, this));
}

bool mock_daemon::run_main()
{
CHECK_AND_ASSERT_THROW_MES(!m_terminated, "Can't run stopped daemon");
boost::thread stop_thread = boost::thread([this] {
while (!this->m_stopped)
epee::misc_utils::sleep_no_w(100);
this->stop_p2p();
});

epee::misc_utils::auto_scope_leave_caller scope_exit_handler = epee::misc_utils::create_scope_leave_handler([&](){
m_stopped = true;
stop_thread.join();
});

try
{
CHECK_AND_ASSERT_THROW_MES(m_rpc_server.run(2, false), "Failed to start RPC");
cryptonote::rpc::DaemonHandler rpc_daemon_handler(*m_core, m_server);
cryptonote::rpc::ZmqServer zmq_server(rpc_daemon_handler);

if (m_start_zmq)
{
if (!zmq_server.addTCPSocket("127.0.0.1", m_zmq_bind_port))
{
MERROR("Failed to add TCP Socket (127.0.0.1:" << m_zmq_bind_port << ") to ZMQ RPC Server");

stop_rpc();
return false;
}

MINFO("Starting ZMQ server...");
zmq_server.run();

MINFO("ZMQ server started at 127.0.0.1: " << m_zmq_bind_port);
}

if (m_start_p2p)
{
m_server.run(); // blocks until p2p goes down
}
else
{
while (!this->m_stopped)
epee::misc_utils::sleep_no_w(100);
}

if (m_start_zmq)
zmq_server.stop();

stop_rpc();
return true;
}
catch (std::exception const & ex)
{
MFATAL("Uncaught exception! " << ex.what());
return false;
}
catch (...)
{
MFATAL("Uncaught exception!");
return false;
}
}

void mock_daemon::stop()
{
CHECK_AND_ASSERT_THROW_MES(!m_terminated, "Can't stop stopped daemon");
m_stopped = true;
m_terminated = true;
m_run_thread.join();
}

void mock_daemon::stop_rpc()
{
m_rpc_server.send_stop_signal();
m_rpc_server.timed_wait_server_stop(5000);
}

void mock_daemon::stop_p2p()
{
if (m_start_p2p)
m_server.send_stop_signal();
}

void mock_daemon::mine_blocks(size_t num_blocks, const std::string &miner_address)
{
bool blocks_mined = false;
const uint64_t start_height = get_height();
const auto mining_timeout = std::chrono::seconds(30);
MDEBUG("Current height before mining: " << start_height);

start_mining(miner_address);
auto mining_started = std::chrono::system_clock::now();

while(true) {
epee::misc_utils::sleep_no_w(100);
const uint64_t cur_height = get_height();

if (cur_height - start_height >= num_blocks)
{
MDEBUG("Cur blocks: " << cur_height << " start: " << start_height);
blocks_mined = true;
break;
}

auto current_time = std::chrono::system_clock::now();
if (mining_timeout < current_time - mining_started)
{
break;
}
}

stop_mining();
CHECK_AND_ASSERT_THROW_MES(blocks_mined, "Mining failed in the time limit");
}

constexpr const std::chrono::seconds mock_daemon::rpc_timeout;

void mock_daemon::start_mining(const std::string &miner_address, uint64_t threads_count, bool do_background_mining, bool ignore_battery)
{
cryptonote::COMMAND_RPC_START_MINING::request req;
req.miner_address = miner_address;
req.threads_count = threads_count;
req.do_background_mining = do_background_mining;
req.ignore_battery = ignore_battery;

cryptonote::COMMAND_RPC_START_MINING::response resp;
bool r = epee::net_utils::invoke_http_json("/start_mining", req, resp, m_http_client, rpc_timeout);
CHECK_AND_ASSERT_THROW_MES(r, "RPC error - start mining");
CHECK_AND_ASSERT_THROW_MES(resp.status != CORE_RPC_STATUS_BUSY, "Daemon busy");
CHECK_AND_ASSERT_THROW_MES(resp.status == CORE_RPC_STATUS_OK, "Daemon response invalid: " << resp.status);
}

void mock_daemon::stop_mining()
{
cryptonote::COMMAND_RPC_STOP_MINING::request req;
cryptonote::COMMAND_RPC_STOP_MINING::response resp;
bool r = epee::net_utils::invoke_http_json("/stop_mining", req, resp, m_http_client, rpc_timeout);
CHECK_AND_ASSERT_THROW_MES(r, "RPC error - stop mining");
CHECK_AND_ASSERT_THROW_MES(resp.status != CORE_RPC_STATUS_BUSY, "Daemon busy");
CHECK_AND_ASSERT_THROW_MES(resp.status == CORE_RPC_STATUS_OK, "Daemon response invalid: " << resp.status);
}

uint64_t mock_daemon::get_height()
{
return m_core->get_blockchain_storage().get_current_blockchain_height();
}
Oops, something went wrong.

0 comments on commit 4f0e0ee

Please sign in to comment.
You can’t perform that action at this time.