| 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 | #include "daemon.h" |
| 30 | #include <common/command_line.h> |
| 31 | |
| 32 | using namespace std; |
| 33 | using namespace daemonize; |
| 34 | namespace po = boost::program_options; |
| 35 | |
| 36 | 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) |
| 37 | { |
| 38 | cryptonote::COMMAND_RPC_SEND_RAW_TX::request req2(req); |
| 39 | req2.do_not_relay = true; // Do not relay in test setup, only one daemon running. |
| 40 | return cryptonote::core_rpc_server::on_send_raw_tx(req2, res, ctx); |
| 41 | } |
| 42 | |
| 43 | void mock_daemon::init_options(boost::program_options::options_description & option_spec) |
| 44 | { |
| 45 | cryptonote::core::init_options(option_spec); |
| 46 | t_node_server::init_options(option_spec); |
| 47 | cryptonote::core_rpc_server::init_options(option_spec); |
| 48 | |
| 49 | command_line::add_arg(option_spec, daemon_args::arg_zmq_rpc_bind_ip); |
| 50 | command_line::add_arg(option_spec, daemon_args::arg_zmq_rpc_bind_port); |
| 51 | } |
| 52 | |
| 53 | void mock_daemon::default_options(boost::program_options::variables_map & vm) |
| 54 | { |
| 55 | std::vector<std::string> exclusive_nodes{"127.0.0.1:65525"}; |
| 56 | tools::options::set_option(vm, nodetool::arg_p2p_add_exclusive_node, po::variable_value(exclusive_nodes, false)); |
| 57 | |
| 58 | tools::options::set_option(vm, nodetool::arg_p2p_bind_ip, po::variable_value(std::string("127.0.0.1"), false)); |
| 59 | tools::options::set_option(vm, cryptonote::arg_offline, po::variable_value(true, false)); |
| 60 | tools::options::set_option(vm, "disable-dns-checkpoints", po::variable_value(true, false)); |
| 61 | |
| 62 | const char *test_mainnet = getenv("TEST_MAINNET"); |
| 63 | if (!test_mainnet || atoi(test_mainnet) == 0) |
| 64 | { |
| 65 | tools::options::set_option(vm, cryptonote::arg_testnet_on, po::variable_value(true, false)); |
| 66 | } |
| 67 | |
| 68 | // By default pick non-standard ports to avoid confusion with possibly locally running daemons (mainnet/testnet) |
| 69 | const char *test_p2p_port = getenv("TEST_P2P_PORT"); |
| 70 | auto p2p_port = std::string(test_p2p_port && strlen(test_p2p_port) > 0 ? test_p2p_port : "61340"); |
| 71 | tools::options::set_option(vm, nodetool::arg_p2p_bind_port, po::variable_value(p2p_port, false)); |
| 72 | |
| 73 | const char *test_rpc_port = getenv("TEST_RPC_PORT"); |
| 74 | auto rpc_port = std::string(test_rpc_port && strlen(test_rpc_port) > 0 ? test_rpc_port : "61341"); |
| 75 | tools::options::set_option(vm, cryptonote::core_rpc_server::arg_rpc_bind_port, po::variable_value(rpc_port, false)); |
| 76 | |
| 77 | const char *test_zmq_port = getenv("TEST_ZMQ_PORT"); |
| 78 | auto zmq_port = std::string(test_zmq_port && strlen(test_zmq_port) > 0 ? test_zmq_port : "61342"); |
| 79 | tools::options::set_option(vm, daemon_args::arg_zmq_rpc_bind_port, po::variable_value(zmq_port, false)); |
| 80 | |
| 81 | po::notify(vm); |
| 82 | } |
| 83 | |
| 84 | void mock_daemon::set_ports(boost::program_options::variables_map & vm, unsigned initial_port) |
| 85 | { |
| 86 | CHECK_AND_ASSERT_THROW_MES(initial_port < 65535-2, "Invalid port number"); |
| 87 | tools::options::set_option(vm, nodetool::arg_p2p_bind_port, po::variable_value(std::to_string(initial_port), false)); |
| 88 | tools::options::set_option(vm, cryptonote::core_rpc_server::arg_rpc_bind_port, po::variable_value(std::to_string(initial_port + 1), false)); |
| 89 | tools::options::set_option(vm, daemon_args::arg_zmq_rpc_bind_port, po::variable_value(std::to_string(initial_port + 2), false)); |
| 90 | po::notify(vm); |
| 91 | } |
| 92 | |
| 93 | void mock_daemon::load_params(boost::program_options::variables_map const & vm) |
| 94 | { |
| 95 | m_p2p_bind_port = command_line::get_arg(vm, nodetool::arg_p2p_bind_port); |
| 96 | m_rpc_bind_port = command_line::get_arg(vm, cryptonote::core_rpc_server::arg_rpc_bind_port); |
| 97 | m_zmq_bind_port = command_line::get_arg(vm, daemon_args::arg_zmq_rpc_bind_port); |
| 98 | m_network_type = command_line::get_arg(vm, cryptonote::arg_testnet_on) ? cryptonote::TESTNET : cryptonote::MAINNET; |
| 99 | } |
| 100 | |
| 101 | mock_daemon::~mock_daemon() |
| 102 | { |
| 103 | if(m_http_client.is_connected()) |
| 104 | m_http_client.disconnect(); |
| 105 | |
| 106 | if (!m_terminated) |
| 107 | { |
| 108 | try |
| 109 | { |
| 110 | stop(); |
| 111 | } |
| 112 | catch (...) |
| 113 | { |
| 114 | MERROR("Failed to stop"); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (!m_deinitalized) |
| 119 | { |
| 120 | deinit(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void mock_daemon::init() |
| 125 | { |
| 126 | m_deinitalized = false; |
| 127 | const auto main_rpc_port = command_line::get_arg(m_vm, cryptonote::core_rpc_server::arg_rpc_bind_port); |
| 128 | m_rpc_server.nettype(m_network_type); |
| 129 | |
| 130 | CHECK_AND_ASSERT_THROW_MES(m_protocol.init(m_vm), "Failed to initialize cryptonote protocol."); |
| 131 | CHECK_AND_ASSERT_THROW_MES(m_rpc_server.init(m_vm, false, main_rpc_port, false), "Failed to initialize RPC server."); |
| 132 | |
| 133 | if (m_start_p2p) |
| 134 | CHECK_AND_ASSERT_THROW_MES(m_server.init(m_vm), "Failed to initialize p2p server."); |
| 135 | |
| 136 | if(m_http_client.is_connected()) |
| 137 | m_http_client.disconnect(); |
| 138 | |
| 139 | CHECK_AND_ASSERT_THROW_MES(m_http_client.set_server(rpc_addr(), boost::none, epee::net_utils::ssl_support_t::e_ssl_support_disabled), "RPC client init fail"); |
| 140 | } |
| 141 | |
| 142 | void mock_daemon::deinit() |
| 143 | { |
| 144 | if(m_http_client.is_connected()) |
| 145 | m_http_client.disconnect(); |
| 146 | |
| 147 | try |
| 148 | { |
| 149 | m_rpc_server.deinit(); |
| 150 | } |
| 151 | catch (...) |
| 152 | { |
| 153 | MERROR("Failed to deinitialize RPC server..."); |
| 154 | } |
| 155 | |
| 156 | if (m_start_p2p) |
| 157 | { |
| 158 | try |
| 159 | { |
| 160 | m_server.deinit(); |
| 161 | } |
| 162 | catch (...) |
| 163 | { |
| 164 | MERROR("Failed to deinitialize p2p..."); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | try |
| 169 | { |
| 170 | m_protocol.deinit(); |
| 171 | m_protocol.set_p2p_endpoint(nullptr); |
| 172 | } |
| 173 | catch (...) |
| 174 | { |
| 175 | MERROR("Failed to stop cryptonote protocol!"); |
| 176 | } |
| 177 | |
| 178 | m_deinitalized = true; |
| 179 | } |
| 180 | |
| 181 | void mock_daemon::init_and_run() |
| 182 | { |
| 183 | init(); |
| 184 | run(); |
| 185 | } |
| 186 | |
| 187 | void mock_daemon::stop_and_deinit() |
| 188 | { |
| 189 | stop(); |
| 190 | deinit(); |
| 191 | } |
| 192 | |
| 193 | void mock_daemon::try_init_and_run(boost::optional<unsigned> initial_port) |
| 194 | { |
| 195 | const unsigned max_attempts = 3; |
| 196 | for(unsigned attempts=0; attempts < max_attempts; ++attempts) |
| 197 | { |
| 198 | if (initial_port) |
| 199 | { |
| 200 | set_ports(m_vm, initial_port.get()); |
| 201 | load_params(m_vm); |
| 202 | MDEBUG("Ports changed, RPC: " << rpc_addr()); |
| 203 | } |
| 204 | |
| 205 | try |
| 206 | { |
| 207 | init_and_run(); |
| 208 | return; |
| 209 | } |
| 210 | catch(const std::exception &e) |
| 211 | { |
| 212 | MWARNING("Could not init and start, attempt: " << attempts << ", reason: " << e.what()); |
| 213 | if (attempts + 1 >= max_attempts) |
| 214 | { |
| 215 | throw; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void mock_daemon::run() |
| 222 | { |
| 223 | m_run_thread = boost::thread(boost::bind(&mock_daemon::run_main, this)); |
| 224 | } |
| 225 | |
| 226 | bool mock_daemon::run_main() |
| 227 | { |
| 228 | CHECK_AND_ASSERT_THROW_MES(!m_terminated, "Can't run stopped daemon"); |
| 229 | CHECK_AND_ASSERT_THROW_MES(!m_start_zmq || m_start_p2p, "ZMQ requires P2P"); |
| 230 | boost::thread stop_thread = boost::thread([this] { |
| 231 | while (!this->m_stopped) |
| 232 | epee::misc_utils::sleep_no_w(100); |
| 233 | this->stop_p2p(); |
| 234 | }); |
| 235 | |
| 236 | epee::misc_utils::auto_scope_leave_caller scope_exit_handler = epee::misc_utils::create_scope_leave_handler([&](){ |
| 237 | m_stopped = true; |
| 238 | stop_thread.join(); |
| 239 | }); |
| 240 | |
| 241 | try |
| 242 | { |
| 243 | CHECK_AND_ASSERT_THROW_MES(m_rpc_server.run(2, false), "Failed to start RPC"); |
| 244 | cryptonote::rpc::DaemonHandler rpc_daemon_handler(*m_core, m_server); |
| 245 | cryptonote::rpc::ZmqServer zmq_server(rpc_daemon_handler); |
| 246 | |
| 247 | if (m_start_zmq) |
| 248 | { |
| 249 | if (!zmq_server.init_rpc("127.0.0.1", m_zmq_bind_port)) |
| 250 | { |
| 251 | MERROR("Failed to add TCP Socket (127.0.0.1:" << m_zmq_bind_port << ") to ZMQ RPC Server"); |
| 252 | |
| 253 | stop_rpc(); |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | MINFO("Starting ZMQ server..."); |
| 258 | zmq_server.run(); |
| 259 | |
| 260 | MINFO("ZMQ server started at 127.0.0.1: " << m_zmq_bind_port); |
| 261 | } |
| 262 | |
| 263 | if (m_start_p2p) |
| 264 | { |
| 265 | m_server.run(); // blocks until p2p goes down |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | while (!this->m_stopped) |
| 270 | epee::misc_utils::sleep_no_w(100); |
| 271 | } |
| 272 | |
| 273 | if (m_start_zmq) |
| 274 | zmq_server.stop(); |
| 275 | |
| 276 | stop_rpc(); |
| 277 | return true; |
| 278 | } |
| 279 | catch (std::exception const & ex) |
| 280 | { |
| 281 | MFATAL("Uncaught exception! " << ex.what()); |
| 282 | return false; |
| 283 | } |
| 284 | catch (...) |
| 285 | { |
| 286 | MFATAL("Uncaught exception!"); |
| 287 | return false; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | void mock_daemon::stop() |
| 292 | { |
| 293 | CHECK_AND_ASSERT_THROW_MES(!m_terminated, "Can't stop stopped daemon"); |
| 294 | m_stopped = true; |
| 295 | m_terminated = true; |
| 296 | m_run_thread.join(); |
| 297 | } |
| 298 | |
| 299 | void mock_daemon::stop_rpc() |
| 300 | { |
| 301 | m_rpc_server.send_stop_signal(); |
| 302 | m_rpc_server.timed_wait_server_stop(5000); |
| 303 | } |
| 304 | |
| 305 | void mock_daemon::stop_p2p() |
| 306 | { |
| 307 | if (m_start_p2p) |
| 308 | m_server.send_stop_signal(); |
| 309 | } |
| 310 | |
| 311 | void mock_daemon::mine_blocks(size_t num_blocks, const std::string &miner_address) |
| 312 | { |
| 313 | bool blocks_mined = false; |
| 314 | const uint64_t start_height = get_height(); |
| 315 | const auto mining_timeout = std::chrono::seconds(120); |
| 316 | MDEBUG("Current height before mining: " << start_height); |
| 317 | |
| 318 | start_mining(miner_address); |
| 319 | auto mining_started = std::chrono::system_clock::now(); |
| 320 | |
| 321 | while(true) { |
| 322 | epee::misc_utils::sleep_no_w(100); |
| 323 | const uint64_t cur_height = get_height(); |
| 324 | |
| 325 | if (cur_height - start_height >= num_blocks) |
| 326 | { |
| 327 | MDEBUG("Cur blocks: " << cur_height << " start: " << start_height); |
| 328 | blocks_mined = true; |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | auto current_time = std::chrono::system_clock::now(); |
| 333 | if (mining_timeout < current_time - mining_started) |
| 334 | { |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | stop_mining(); |
| 340 | CHECK_AND_ASSERT_THROW_MES(blocks_mined, "Mining failed in the time limit"); |
| 341 | } |
| 342 | |
| 343 | constexpr const std::chrono::seconds mock_daemon::rpc_timeout; |
| 344 | |
| 345 | void mock_daemon::start_mining(const std::string &miner_address, uint64_t threads_count, bool do_background_mining, bool ignore_battery) |
| 346 | { |
| 347 | cryptonote::COMMAND_RPC_START_MINING::request req; |
| 348 | req.miner_address = miner_address; |
| 349 | req.threads_count = threads_count; |
| 350 | req.do_background_mining = do_background_mining; |
| 351 | req.ignore_battery = ignore_battery; |
| 352 | |
| 353 | cryptonote::COMMAND_RPC_START_MINING::response resp; |
| 354 | bool r = epee::net_utils::invoke_http_json("/start_mining", req, resp, m_http_client, rpc_timeout); |
| 355 | CHECK_AND_ASSERT_THROW_MES(r, "RPC error - start mining"); |
| 356 | CHECK_AND_ASSERT_THROW_MES(resp.status != CORE_RPC_STATUS_BUSY, "Daemon busy"); |
| 357 | CHECK_AND_ASSERT_THROW_MES(resp.status == CORE_RPC_STATUS_OK, "Daemon response invalid: " << resp.status); |
| 358 | } |
| 359 | |
| 360 | void mock_daemon::stop_mining() |
| 361 | { |
| 362 | cryptonote::COMMAND_RPC_STOP_MINING::request req; |
| 363 | cryptonote::COMMAND_RPC_STOP_MINING::response resp; |
| 364 | bool r = epee::net_utils::invoke_http_json("/stop_mining", req, resp, m_http_client, rpc_timeout); |
| 365 | CHECK_AND_ASSERT_THROW_MES(r, "RPC error - stop mining"); |
| 366 | CHECK_AND_ASSERT_THROW_MES(resp.status != CORE_RPC_STATUS_BUSY, "Daemon busy"); |
| 367 | CHECK_AND_ASSERT_THROW_MES(resp.status == CORE_RPC_STATUS_OK, "Daemon response invalid: " << resp.status); |
| 368 | } |
| 369 | |
| 370 | uint64_t mock_daemon::get_height() |
| 371 | { |
| 372 | return m_core->get_blockchain_storage().get_current_blockchain_height(); |
| 373 | } |