diff options
Diffstat (limited to 'src/daemon')
-rw-r--r-- | src/daemon/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/daemon/command_parser_executor.cpp | 41 | ||||
-rw-r--r-- | src/daemon/command_parser_executor.h | 10 | ||||
-rw-r--r-- | src/daemon/command_server.cpp | 55 | ||||
-rw-r--r-- | src/daemon/command_server.h | 10 | ||||
-rw-r--r-- | src/daemon/daemon.cpp | 30 | ||||
-rw-r--r-- | src/daemon/daemon.h | 2 | ||||
-rw-r--r-- | src/daemon/daemon_commands_handler.h | 49 | ||||
-rw-r--r-- | src/daemon/executor.cpp | 2 | ||||
-rw-r--r-- | src/daemon/main.cpp | 14 | ||||
-rw-r--r-- | src/daemon/rpc.h | 5 | ||||
-rw-r--r-- | src/daemon/rpc_command_executor.cpp | 572 | ||||
-rw-r--r-- | src/daemon/rpc_command_executor.h | 18 |
13 files changed, 706 insertions, 105 deletions
diff --git a/src/daemon/CMakeLists.txt b/src/daemon/CMakeLists.txt index afb65b5f6..bf25bbca9 100644 --- a/src/daemon/CMakeLists.txt +++ b/src/daemon/CMakeLists.txt @@ -78,6 +78,9 @@ target_link_libraries(daemon cryptonote_core crypto common + otshell_utils + p2p + cryptonote_protocol daemonizer ${Boost_CHRONO_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} diff --git a/src/daemon/command_parser_executor.cpp b/src/daemon/command_parser_executor.cpp index d3af50b66..fd7b40be2 100644 --- a/src/daemon/command_parser_executor.cpp +++ b/src/daemon/command_parser_executor.cpp @@ -34,8 +34,10 @@ namespace daemonize { t_command_parser_executor::t_command_parser_executor( uint32_t ip , uint16_t port + , bool is_rpc + , cryptonote::core_rpc_server* rpc_server ) - : m_executor(ip, port) + : m_executor(ip, port, is_rpc, rpc_server) {} bool t_command_parser_executor::print_peer_list(const std::vector<std::string>& args) @@ -294,4 +296,41 @@ bool t_command_parser_executor::set_limit_down(const std::vector<std::string>& a return m_executor.set_limit_down(limit); } + +bool t_command_parser_executor::fast_exit(const std::vector<std::string>& args) +{ + if (!args.empty()) return false; + return m_executor.fast_exit(); +} + +bool t_command_parser_executor::out_peers(const std::vector<std::string>& args) +{ + if (args.empty()) return false; + + unsigned int limit; + try { + limit = std::stoi(args[0]); + } + + catch(std::invalid_argument& ex) { + _erro("stoi exception"); + return false; + } + + return m_executor.out_peers(limit); +} + +bool t_command_parser_executor::start_save_graph(const std::vector<std::string>& args) +{ + if (!args.empty()) return false; + return m_executor.start_save_graph(); +} + +bool t_command_parser_executor::stop_save_graph(const std::vector<std::string>& args) +{ + if (!args.empty()) return false; + return m_executor.stop_save_graph(); +} + + } // namespace daemonize diff --git a/src/daemon/command_parser_executor.h b/src/daemon/command_parser_executor.h index 19ed8ada7..27ffabc1c 100644 --- a/src/daemon/command_parser_executor.h +++ b/src/daemon/command_parser_executor.h @@ -37,6 +37,7 @@ #pragma once #include "daemon/rpc_command_executor.h" +#include "rpc/core_rpc_server.h" namespace daemonize { @@ -48,6 +49,8 @@ public: t_command_parser_executor( uint32_t ip , uint16_t port + , bool is_rpc + , cryptonote::core_rpc_server* rpc_server = NULL ); bool print_peer_list(const std::vector<std::string>& args); @@ -90,6 +93,13 @@ public: bool set_limit_down(const std::vector<std::string>& args); + bool fast_exit(const std::vector<std::string>& args); + + bool out_peers(const std::vector<std::string>& args); + + bool start_save_graph(const std::vector<std::string>& args); + + bool stop_save_graph(const std::vector<std::string>& args); }; } // namespace daemonize diff --git a/src/daemon/command_server.cpp b/src/daemon/command_server.cpp index bedceffed..f0f4cd676 100644 --- a/src/daemon/command_server.cpp +++ b/src/daemon/command_server.cpp @@ -37,11 +37,19 @@ namespace p = std::placeholders; t_command_server::t_command_server( uint32_t ip , uint16_t port + , bool is_rpc + , cryptonote::core_rpc_server* rpc_server ) - : m_parser(ip, port) + : m_parser(ip, port, is_rpc, rpc_server) , m_command_lookup() + , m_is_rpc(is_rpc) { m_command_lookup.set_handler( + "q" + , [] (const std::vector<std::string>& args) {return true;} + , "ignored" + ); + m_command_lookup.set_handler( "help" , std::bind(&t_command_server::help, this, p::_1) , "Show this help" @@ -127,6 +135,11 @@ t_command_server::t_command_server( , "Stop the daemon" ); m_command_lookup.set_handler( + "exit" + , std::bind(&t_command_parser_executor::stop_daemon, &m_parser, p::_1) + , "Stop the daemon" + ); + m_command_lookup.set_handler( "print_status" , std::bind(&t_command_parser_executor::print_status, &m_parser, p::_1) , "Prints daemon status" @@ -137,15 +150,35 @@ t_command_server::t_command_server( , "limit <kB/s> - Set download and upload limit" ); m_command_lookup.set_handler( - "limit-up" + "limit_up" , std::bind(&t_command_parser_executor::set_limit_up, &m_parser, p::_1) , "limit <kB/s> - Set upload limit" ); m_command_lookup.set_handler( - "limit-down" + "limit_down" , std::bind(&t_command_parser_executor::set_limit_down, &m_parser, p::_1) , "limit <kB/s> - Set download limit" ); + m_command_lookup.set_handler( + "fast_exit" + , std::bind(&t_command_parser_executor::fast_exit, &m_parser, p::_1) + , "Exit" + ); + m_command_lookup.set_handler( + "out_peers" + , std::bind(&t_command_parser_executor::out_peers, &m_parser, p::_1) + , "Set max limit of out peers" + ); + m_command_lookup.set_handler( + "start_save_graph" + , std::bind(&t_command_parser_executor::start_save_graph, &m_parser, p::_1) + , "Start save data for dr monero" + ); + m_command_lookup.set_handler( + "stop_save_graph" + , std::bind(&t_command_parser_executor::stop_save_graph, &m_parser, p::_1) + , "Stop save data for dr monero" + ); } bool t_command_server::process_command_str(const std::string& cmd) @@ -163,6 +196,22 @@ bool t_command_server::process_command_vec(const std::vector<std::string>& cmd) return result; } +bool t_command_server::start_handling() +{ + if (m_is_rpc) return false; + + m_command_lookup.start_handling("", get_commands_str()); + + return true; +} + +void t_command_server::stop_handling() +{ + if (m_is_rpc) return; + + m_command_lookup.stop_handling(); +} + bool t_command_server::help(const std::vector<std::string>& args) { std::cout << get_commands_str() << std::endl; diff --git a/src/daemon/command_server.h b/src/daemon/command_server.h index ab2d33a3a..494f44d8b 100644 --- a/src/daemon/command_server.h +++ b/src/daemon/command_server.h @@ -47,17 +47,25 @@ namespace daemonize { class t_command_server { private: t_command_parser_executor m_parser; - epee::command_handler m_command_lookup; + epee::console_handlers_binder m_command_lookup; + bool m_is_rpc; + public: t_command_server( uint32_t ip , uint16_t port + , bool is_rpc = true + , cryptonote::core_rpc_server* rpc_server = NULL ); bool process_command_str(const std::string& cmd); bool process_command_vec(const std::vector<std::string>& cmd); + bool start_handling(); + + void stop_handling(); + private: bool help(const std::vector<std::string>& args); diff --git a/src/daemon/daemon.cpp b/src/daemon/daemon.cpp index 728b239a0..7931ba03f 100644 --- a/src/daemon/daemon.cpp +++ b/src/daemon/daemon.cpp @@ -1,5 +1,5 @@ -// Copyright (c) 2014, The Monero Project -// +// Copyright (c) 2014-2015, The Monero Project +// // All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, are @@ -35,12 +35,19 @@ #include "daemon/p2p.h" #include "daemon/protocol.h" #include "daemon/rpc.h" +#include "daemon/command_server.h" #include "misc_log_ex.h" #include "version.h" +#include "../../contrib/epee/include/syncobj.h" + +using namespace epee; + #include <boost/program_options.hpp> #include <functional> #include <memory> +unsigned int epee::g_test_dbg_lock_sleep = 0; + namespace daemonize { struct t_internals { @@ -101,7 +108,7 @@ t_daemon & t_daemon::operator=(t_daemon && other) return *this; } -bool t_daemon::run() +bool t_daemon::run(bool interactive) { if (nullptr == mp_internals) { @@ -113,7 +120,22 @@ bool t_daemon::run() { mp_internals->core.run(); mp_internals->rpc.run(); - mp_internals->p2p.run(); + + daemonize::t_command_server* rpc_commands; + + if (interactive) + { + rpc_commands = new daemonize::t_command_server(0, 0, false, mp_internals->rpc.get_server()); + rpc_commands->start_handling(); + } + + mp_internals->p2p.run(); // blocks until p2p goes down + + if (interactive) + { + rpc_commands->stop_handling(); + } + mp_internals->rpc.stop(); LOG_PRINT("Node stopped.", LOG_LEVEL_0); return true; diff --git a/src/daemon/daemon.h b/src/daemon/daemon.h index 5c5c27b6b..b0869b25b 100644 --- a/src/daemon/daemon.h +++ b/src/daemon/daemon.h @@ -48,7 +48,7 @@ public: t_daemon & operator=(t_daemon && other); ~t_daemon(); - bool run(); + bool run(bool interactive = false); void stop(); }; } diff --git a/src/daemon/daemon_commands_handler.h b/src/daemon/daemon_commands_handler.h index 7416af9c5..215cf26de 100644 --- a/src/daemon/daemon_commands_handler.h +++ b/src/daemon/daemon_commands_handler.h @@ -1,7 +1,35 @@ -// Copyright (c) 2012-2013 The Cryptonote developers -// Distributed under the MIT/X11 software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. - +// Copyright (c) 2014-2015, 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 + + +/* This isn't a header file, may want to refactor this... */ #pragma once #include <boost/lexical_cast.hpp> @@ -12,6 +40,7 @@ #include "common/util.h" #include "crypto/hash.h" #include "version.h" +#include "../../contrib/otshell_utils/utils.hpp" //#include "net/net_helper.h" //#include "../p2p/p2p_protocol_defs.h" @@ -43,10 +72,14 @@ public: m_cmd_binder.set_handler("save", boost::bind(&daemon_cmmands_handler::save, this, _1), "Save blockchain"); m_cmd_binder.set_handler("set_log", boost::bind(&daemon_cmmands_handler::set_log, this, _1), "set_log <level> - Change current log detalization level, <level> is a number 0-4"); m_cmd_binder.set_handler("diff", boost::bind(&daemon_cmmands_handler::diff, this, _1), "Show difficulty"); - m_cmd_binder.set_handler("limit-up", boost::bind(&daemon_cmmands_handler::limit_up, this, _1), "Set upload limit"); - m_cmd_binder.set_handler("limit-down", boost::bind(&daemon_cmmands_handler::limit_down, this, _1), "Set download limit"); - m_cmd_binder.set_handler("limit", boost::bind(&daemon_cmmands_handler::limit, this, _1), "Set download and upload limit"); m_cmd_binder.set_handler("out_peers", boost::bind(&daemon_cmmands_handler::out_peers_limit, this, _1), "Set max limit of out peers"); + m_cmd_binder.set_handler("limit_up", boost::bind(&daemon_cmmands_handler::limit_up, this, _1), "Set upload limit [kB/s]"); + m_cmd_binder.set_handler("limit_down", boost::bind(&daemon_cmmands_handler::limit_down, this, _1), "Set download limit [kB/s]"); + m_cmd_binder.set_handler("limit", boost::bind(&daemon_cmmands_handler::limit, this, _1), "Set download and upload limit [kB/s]"); + m_cmd_binder.set_handler("fast_exit", boost::bind(&daemon_cmmands_handler::fast_exit, this, _1), "Exit"); + m_cmd_binder.set_handler("test_drop_download", boost::bind(&daemon_cmmands_handler::test_drop_download, this, _1), "For network testing, drop downloaded blocks instead checking/adding them to blockchain. Can fake-download blocks very fast."); + m_cmd_binder.set_handler("start_save_graph", boost::bind(&daemon_cmmands_handler::start_save_graph, this, _1), ""); + m_cmd_binder.set_handler("stop_save_graph", boost::bind(&daemon_cmmands_handler::stop_save_graph, this, _1), ""); } bool start_handling() @@ -327,6 +360,8 @@ private: PUSH_WARNINGS DISABLE_GCC_WARNING(maybe-uninitialized) log_space::log_singletone::get_set_log_detalisation_level(true, l); + int otshell_utils_log_level = 100 - (l * 25); + gCurrentLogger.setDebugLevel(otshell_utils_log_level); POP_WARNINGS return true; diff --git a/src/daemon/executor.cpp b/src/daemon/executor.cpp index f4def4d55..bb188c174 100644 --- a/src/daemon/executor.cpp +++ b/src/daemon/executor.cpp @@ -65,7 +65,7 @@ namespace daemonize ) { epee::log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL); - return t_daemon{vm}.run(); + return t_daemon{vm}.run(true); } } diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index 3bad70378..71df34950 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -51,6 +51,9 @@ int main(int argc, char const * argv[]) { try { + _note_c("dbg/main", "Begin of main()"); + // TODO parse the debug options like set log level right here at start + epee::string_tools::set_module_name_and_folder(argv[0]); // Build argument description @@ -72,7 +75,10 @@ int main(int argc, char const * argv[]) command_line::add_arg(visible_options, command_line::arg_testnet_data_dir, default_testnet_data_dir.string()); bf::path default_conf = default_data_dir / std::string(CRYPTONOTE_NAME ".conf"); command_line::add_arg(visible_options, daemon_args::arg_config_file, default_conf.string()); - + command_line::add_arg(visible_options, command_line::arg_test_drop_download); + command_line::add_arg(visible_options, command_line::arg_test_dbg_lock_sleep); + command_line::add_arg(visible_options, command_line::arg_test_drop_download_height); + // Settings bf::path default_log = default_data_dir / std::string(CRYPTONOTE_NAME ".log"); command_line::add_arg(core_settings, daemon_args::arg_log_file, default_log.string()); @@ -129,6 +135,8 @@ int main(int argc, char const * argv[]) std::cout << "OS: " << tools::get_os_version_string() << ENDL; return 0; } + + epee::g_test_dbg_lock_sleep = command_line::get_arg(vm, command_line::arg_test_dbg_lock_sleep); std::string db_type = command_line::get_arg(vm, daemon_args::arg_db_type); @@ -224,6 +232,8 @@ int main(int argc, char const * argv[]) else if (epee::log_space::get_set_log_detalisation_level(false) != new_log_level) { epee::log_space::get_set_log_detalisation_level(true, new_log_level); + int otshell_utils_log_level = 100 - (new_log_level * 25); + gCurrentLogger.setDebugLevel(otshell_utils_log_level); LOG_PRINT_L0("LOG_LEVEL set to " << new_log_level); } } @@ -239,6 +249,8 @@ int main(int argc, char const * argv[]) ); } + _note_c("dbg/main", "Moving from main() into the daemonize now."); + return daemonizer::daemonize(argc, argv, daemonize::t_executor{}, vm); } catch (std::exception const & ex) diff --git a/src/daemon/rpc.h b/src/daemon/rpc.h index cf7da51a7..6477f440e 100644 --- a/src/daemon/rpc.h +++ b/src/daemon/rpc.h @@ -82,6 +82,11 @@ public: m_server.timed_wait_server_stop(5000); } + cryptonote::core_rpc_server* get_server() + { + return &m_server; + } + ~t_rpc() { LOG_PRINT_L0("Deinitializing rpc server..."); diff --git a/src/daemon/rpc_command_executor.cpp b/src/daemon/rpc_command_executor.cpp index 02bef9271..46900b071 100644 --- a/src/daemon/rpc_command_executor.cpp +++ b/src/daemon/rpc_command_executor.cpp @@ -32,6 +32,7 @@ #include "common/scoped_message_writer.h" #include "daemon/rpc_command_executor.h" #include "rpc/core_rpc_server_commands_defs.h" +#include "cryptonote_core/cryptonote_core.h" #include <boost/format.hpp> #include <ctime> @@ -72,17 +73,54 @@ namespace { t_rpc_command_executor::t_rpc_command_executor( uint32_t ip , uint16_t port + , bool is_rpc + , cryptonote::core_rpc_server* rpc_server ) - : m_rpc_client{ip, port} -{} + : m_rpc_client(NULL), m_rpc_server(rpc_server) +{ + if (is_rpc) + { + m_rpc_client = new tools::t_rpc_client(ip, port); + } + else + { + if (rpc_server == NULL) + { + throw std::runtime_error("If not calling commands via RPC, rpc_server pointer must be non-null"); + } + } + + m_is_rpc = is_rpc; +} + +t_rpc_command_executor::~t_rpc_command_executor() +{ + if (m_rpc_client != NULL) + { + delete m_rpc_client; + } +} bool t_rpc_command_executor::print_peer_list() { cryptonote::COMMAND_RPC_GET_PEER_LIST::request req; cryptonote::COMMAND_RPC_GET_PEER_LIST::response res; - bool ok = m_rpc_client.rpc_request(req, res, "/get_peer_list", "Couldn't retrieve peer list"); - - if (!ok) return false; + std::string failure_message = "Couldn't retrieve peer list"; + if (m_is_rpc) + { + if (!m_rpc_client->rpc_request(req, res, "/get_peer_list", failure_message.c_str())) + { + return false; + } + } + else + { + if (!m_rpc_server->on_get_peer_list(req, res)) + { + tools::fail_msg_writer() << failure_message; + return false; + } + } for (auto & peer : res.white_list) { @@ -101,10 +139,24 @@ bool t_rpc_command_executor::save_blockchain() { cryptonote::COMMAND_RPC_SAVE_BC::request req; cryptonote::COMMAND_RPC_SAVE_BC::response res; - if (m_rpc_client.rpc_request(req, res, "/save_bc", "Couldn't save blockchain")) + std::string fail_message = "Couldn't save blockchain"; + + if (m_is_rpc) { - tools::success_msg_writer() << "Blockchain saved"; + if (!m_rpc_client->rpc_request(req, res, "/save_bc", fail_message.c_str())) + { + return true; + } } + else + { + if (!m_rpc_server->on_save_bc(req, res)) + { + tools::fail_msg_writer() << fail_message.c_str(); + } + } + + tools::success_msg_writer() << "Blockchain saved"; return true; } @@ -114,11 +166,25 @@ bool t_rpc_command_executor::show_hash_rate() { cryptonote::COMMAND_RPC_SET_LOG_HASH_RATE::response res; req.visible = true; - if (m_rpc_client.rpc_request(req, res, "/set_log_hash_rate", "Unsuccessful")) + std::string fail_message = "Unsuccessful"; + + if (m_is_rpc) + { + if (!m_rpc_client->rpc_request(req, res, "/set_log_hash_rate", fail_message.c_str())) + { + return true; + } + } + else { - tools::success_msg_writer() << "Hash rate logging is on"; + if (!m_rpc_server->on_set_log_hash_rate(req, res)) + { + tools::fail_msg_writer() << fail_message.c_str(); + } } + tools::success_msg_writer() << "Hash rate logging is on"; + return true; } @@ -127,10 +193,25 @@ bool t_rpc_command_executor::hide_hash_rate() { cryptonote::COMMAND_RPC_SET_LOG_HASH_RATE::response res; req.visible = false; - if (m_rpc_client.rpc_request(req, res, "/set_log_hash_rate", "Unsuccessful")) + std::string fail_message = "Unsuccessful"; + + if (m_is_rpc) { - tools::success_msg_writer() << "Hash rate logging is off"; + if (!m_rpc_client->rpc_request(req, res, "/set_log_hash_rate", fail_message.c_str())) + { + return true; + } } + else + { + if (!m_rpc_server->on_set_log_hash_rate(req, res)) + { + tools::fail_msg_writer() << fail_message.c_str(); + return true; + } + } + + tools::success_msg_writer() << "Hash rate logging is off"; return true; } @@ -139,54 +220,134 @@ bool t_rpc_command_executor::show_difficulty() { cryptonote::COMMAND_RPC_GET_INFO::request req; cryptonote::COMMAND_RPC_GET_INFO::response res; - if (m_rpc_client.rpc_request(req, res, "/getinfo", "Problem fetching info")) + std::string fail_message = "Problem fetching info"; + + if (m_is_rpc) { - tools::success_msg_writer() << "BH: " << res.height - << ", DIFF: " << res.difficulty - << ", HR: " << (int) res.difficulty / 60L << " H/s"; + if (!m_rpc_client->rpc_request(req, res, "/getinfo", fail_message.c_str())) + { + return true; + } + } + else + { + if (!m_rpc_server->on_get_info(req, res)) + { + tools::fail_msg_writer() << fail_message.c_str(); + return true; + } } + tools::success_msg_writer() << "BH: " << res.height + << ", DIFF: " << res.difficulty + << ", HR: " << (int) res.difficulty / 60L << " H/s"; + return true; } bool t_rpc_command_executor::print_connections() { cryptonote::COMMAND_RPC_GET_CONNECTIONS::request req; cryptonote::COMMAND_RPC_GET_CONNECTIONS::response res; + epee::json_rpc::error error_resp; + + std::string fail_message = "Unsuccessful"; - if (m_rpc_client.rpc_request(req, res, "/get_connections", "Unsuccessful")) + if (m_is_rpc) { - for (auto & info : res.connections) + if (!m_rpc_client->json_rpc_request(req, res, "/get_connections", fail_message.c_str())) { - std::string address = info.ip + ":" + info.port; - std::string in_out = info.incoming ? "INC" : "OUT"; - tools::msg_writer() << boost::format("%-25s peer_id: %-25s %s") % address % info.peer_id % in_out; + return true; + } + } + else + { + if (!m_rpc_server->on_get_connections(req, res, error_resp)) + { + tools::fail_msg_writer() << fail_message.c_str(); + return true; } } + tools::msg_writer() << std::setw(30) << std::left << "Remote Host" + << std::setw(20) << "Peer id" + << std::setw(30) << "Recv/Sent (inactive,sec)" + << std::setw(25) << "State" + << std::setw(20) << "Livetime(sec)" + << std::setw(12) << "Down (kB/s)" + << std::setw(14) << "Down(now)" + << std::setw(10) << "Up (kB/s)" + << std::setw(13) << "Up(now)" + << std::endl; + + for (auto & info : res.connections) + { + std::string address = info.incoming ? "INC " : "OUT "; + address += info.ip + ":" + info.port; + //std::string in_out = info.incoming ? "INC " : "OUT "; + tools::msg_writer() + //<< std::setw(30) << std::left << in_out + << std::setw(30) << std::left << address + << std::setw(20) << info.peer_id + << std::setw(30) << std::to_string(info.recv_count) + "(" + std::to_string(info.recv_idle_time) + ")/" + std::to_string(info.send_count) + "(" + std::to_string(info.send_idle_time) + ")" + << std::setw(25) << info.state + << std::setw(20) << info.live_time + << std::setw(12) << info.avg_download + << std::setw(14) << info.current_download + << std::setw(10) << info.avg_upload + << std::setw(13) << info.current_upload + + << std::left << (info.localhost ? "[LOCALHOST]" : "") + << std::left << (info.local_ip ? "[LAN]" : ""); + //tools::msg_writer() << boost::format("%-25s peer_id: %-25s %s") % address % info.peer_id % in_out; + + } + return true; } bool t_rpc_command_executor::print_blockchain_info(uint64_t start_block_index, uint64_t end_block_index) { + +// this function appears to not exist in the json rpc api, and so is commented +// until such a time as it does. + +/* cryptonote::COMMAND_RPC_GET_BLOCK_HEADERS_RANGE::request req; cryptonote::COMMAND_RPC_GET_BLOCK_HEADERS_RANGE::response res; + epee::json_rpc::error error_resp; req.start_height = start_block_index; req.end_height = end_block_index; - if (m_rpc_client.json_rpc_request(req, res, "getblockheadersrange", "Unsuccessful")) + std::string fail_message = "Unsuccessful"; + + if (m_is_rpc) { - for (auto & header : res.headers) + if (!m_rpc_client->json_rpc_request(req, res, "getblockheadersrange", fail_message.c_str())) { - std::cout - << "major version: " << header.major_version << std::endl - << "minor version: " << header.minor_version << std::endl - << "height: " << header.height << ", timestamp: " << header.timestamp << ", difficulty: " << header.difficulty << std::endl - << "block id: " << header.hash << std::endl - << "previous block id: " << header.prev_hash << std::endl - << "difficulty: " << header.difficulty << ", nonce " << header.nonce << std::endl; + return true; + } + } + else + { + if (!m_rpc_server->on_getblockheadersrange(req, res, error_resp)) + { + tools::fail_msg_writer() << fail_message.c_str(); + return true; } } + for (auto & header : res.headers) + { + std::cout + << "major version: " << header.major_version << std::endl + << "minor version: " << header.minor_version << std::endl + << "height: " << header.height << ", timestamp: " << header.timestamp << ", difficulty: " << header.difficulty << std::endl + << "block id: " << header.hash << std::endl + << "previous block id: " << header.prev_hash << std::endl + << "difficulty: " << header.difficulty << ", nonce " << header.nonce << std::endl; + } + +*/ return true; } @@ -195,11 +356,26 @@ bool t_rpc_command_executor::set_log_level(int8_t level) { cryptonote::COMMAND_RPC_SET_LOG_LEVEL::response res; req.level = level; - if (m_rpc_client.rpc_request(req, res, "/set_log_level", "Unsuccessful")) + std::string fail_message = "Unsuccessful"; + + if (m_is_rpc) + { + if (!m_rpc_client->rpc_request(req, res, "/set_log_level", fail_message.c_str())) + { + return true; + } + } + else { - tools::success_msg_writer() << "Log level is now " << boost::lexical_cast<std::string>(level); + if (!m_rpc_server->on_set_log_level(req, res)) + { + tools::fail_msg_writer() << fail_message.c_str(); + return true; + } } + tools::success_msg_writer() << "Log level is now " << boost::lexical_cast<std::string>(level); + return true; } @@ -207,10 +383,25 @@ bool t_rpc_command_executor::print_height() { cryptonote::COMMAND_RPC_GET_HEIGHT::request req; cryptonote::COMMAND_RPC_GET_HEIGHT::response res; - if (m_rpc_client.rpc_request(req, res, "/getheight", "Unsuccessful")) + std::string fail_message = "Unsuccessful"; + + if (m_is_rpc) { - tools::success_msg_writer() << boost::lexical_cast<std::string>(res.height); + if (!m_rpc_client->rpc_request(req, res, "/getheight", fail_message.c_str())) + { + return true; + } } + else + { + if (!m_rpc_server->on_get_height(req, res)) + { + tools::fail_msg_writer() << fail_message.c_str(); + return true; + } + } + + tools::success_msg_writer() << boost::lexical_cast<std::string>(res.height); return true; } @@ -218,28 +409,60 @@ bool t_rpc_command_executor::print_height() { bool t_rpc_command_executor::print_block_by_hash(crypto::hash block_hash) { cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::request req; cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::response res; + epee::json_rpc::error error_resp; req.hash = epee::string_tools::pod_to_hex(block_hash); - if (m_rpc_client.json_rpc_request(req, res, "getblockheaderbyhash", "Unsuccessful")) + std::string fail_message = "Unsuccessful"; + + if (m_is_rpc) { - print_block_header(res.block_header); + if (!m_rpc_client->json_rpc_request(req, res, "getblockheaderbyhash", fail_message.c_str())) + { + return true; + } + } + else + { + if (!m_rpc_server->on_get_block_header_by_hash(req, res, error_resp)) + { + tools::fail_msg_writer() << fail_message.c_str(); + return true; + } } + print_block_header(res.block_header); + return true; } bool t_rpc_command_executor::print_block_by_height(uint64_t height) { cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::request req; cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::response res; + epee::json_rpc::error error_resp; req.height = height; - if (m_rpc_client.json_rpc_request(req, res, "getblockheaderbyheight", "Unsuccessful")) + std::string fail_message = "Unsuccessful"; + + if (m_is_rpc) { - print_block_header(res.block_header); + if (!m_rpc_client->json_rpc_request(req, res, "getblockheaderbyheight", fail_message.c_str())) + { + return true; + } + } + else + { + if (!m_rpc_server->on_get_block_header_by_height(req, res, error_resp)) + { + tools::fail_msg_writer() << fail_message.c_str(); + return true; + } } + print_block_header(res.block_header); + return true; } @@ -247,18 +470,33 @@ bool t_rpc_command_executor::print_transaction(crypto::hash transaction_hash) { cryptonote::COMMAND_RPC_GET_TRANSACTIONS::request req; cryptonote::COMMAND_RPC_GET_TRANSACTIONS::response res; - if (m_rpc_client.rpc_request(req, res, "/gettransactions", "Problem fetching transaction")) + std::string fail_message = "Problem fetching transaction"; + + if (m_is_rpc) { - if (1 == res.txs_as_hex.size()) + if (!m_rpc_client->rpc_request(req, res, "/gettransactions", fail_message.c_str())) { - tools::success_msg_writer() << res.txs_as_hex.front(); + return true; } - else + } + else + { + if (!m_rpc_server->on_get_transactions(req, res)) { - tools::fail_msg_writer() << "transaction wasn't found: <" << transaction_hash << '>' << std::endl; + tools::fail_msg_writer() << fail_message.c_str(); + return true; } } + if (1 == res.txs_as_hex.size()) + { + tools::success_msg_writer() << res.txs_as_hex.front(); + } + else + { + tools::fail_msg_writer() << "transaction wasn't found: <" << transaction_hash << '>' << std::endl; + } + return true; } @@ -266,25 +504,40 @@ bool t_rpc_command_executor::print_transaction_pool_long() { cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::request req; cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::response res; - if (m_rpc_client.rpc_request(req, res, "/get_transaction_pool", "Problem fetching transaction pool")) + std::string fail_message = "Problem fetching transaction pool"; + + if (m_is_rpc) { - if (res.transactions.empty()) + if (!m_rpc_client->rpc_request(req, res, "/get_transaction_pool", fail_message.c_str())) { - tools::msg_writer() << "Pool is empty" << std::endl; + return true; } - for (auto & tx_info : res.transactions) + } + else + { + if (!m_rpc_server->on_get_transaction_pool(req, res)) { - tools::msg_writer() << "id: " << tx_info.id_hash << std::endl - << "blob_size: " << tx_info.blob_size << std::endl - << "fee: " << tx_info.fee << std::endl - << "kept_by_block: " << tx_info.kept_by_block << std::endl - << "max_used_block_height: " << tx_info.max_used_block_height << std::endl - << "max_used_block_id: " << tx_info.max_used_block_id_hash << std::endl - << "last_failed_height: " << tx_info.last_failed_height << std::endl - << "last_failed_id: " << tx_info.last_failed_id_hash << std::endl; + tools::fail_msg_writer() << fail_message.c_str(); + return true; } } + if (res.transactions.empty()) + { + tools::msg_writer() << "Pool is empty" << std::endl; + } + for (auto & tx_info : res.transactions) + { + tools::msg_writer() << "id: " << tx_info.id_hash << std::endl + << "blob_size: " << tx_info.blob_size << std::endl + << "fee: " << tx_info.fee << std::endl + << "kept_by_block: " << tx_info.kept_by_block << std::endl + << "max_used_block_height: " << tx_info.max_used_block_height << std::endl + << "max_used_block_id: " << tx_info.max_used_block_id_hash << std::endl + << "last_failed_height: " << tx_info.last_failed_height << std::endl + << "last_failed_id: " << tx_info.last_failed_id_hash << std::endl; + } + return true; } @@ -292,26 +545,41 @@ bool t_rpc_command_executor::print_transaction_pool_short() { cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::request req; cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::response res; - if (m_rpc_client.rpc_request(req, res, "/get_transaction_pool", "Problem fetching transaction pool")) + std::string fail_message = "Problem fetching transaction pool"; + + if (m_is_rpc) + { + if (!m_rpc_client->rpc_request(req, res, "/get_transaction_pool", fail_message.c_str())) + { + return true; + } + } + else { - for (auto & tx_info : res.transactions) + if (!m_rpc_server->on_get_transaction_pool(req, res)) { - if (res.transactions.empty()) - { - tools::msg_writer() << "Pool is empty" << std::endl; - } - tools::msg_writer() << "id: " << tx_info.id_hash << std::endl - << tx_info.tx_json << std::endl - << "blob_size: " << tx_info.blob_size << std::endl - << "fee: " << tx_info.fee << std::endl - << "kept_by_block: " << tx_info.kept_by_block << std::endl - << "max_used_block_height: " << tx_info.max_used_block_height << std::endl - << "max_used_block_id: " << tx_info.max_used_block_id_hash << std::endl - << "last_failed_height: " << tx_info.last_failed_height << std::endl - << "last_failed_id: " << tx_info.last_failed_id_hash << std::endl; + tools::fail_msg_writer() << fail_message.c_str(); + return true; } } + if (res.transactions.empty()) + { + tools::msg_writer() << "Pool is empty" << std::endl; + } + for (auto & tx_info : res.transactions) + { + tools::msg_writer() << "id: " << tx_info.id_hash << std::endl + << tx_info.tx_json << std::endl + << "blob_size: " << tx_info.blob_size << std::endl + << "fee: " << tx_info.fee << std::endl + << "kept_by_block: " << tx_info.kept_by_block << std::endl + << "max_used_block_height: " << tx_info.max_used_block_height << std::endl + << "max_used_block_id: " << tx_info.max_used_block_id_hash << std::endl + << "last_failed_height: " << tx_info.last_failed_height << std::endl + << "last_failed_id: " << tx_info.last_failed_id_hash << std::endl; + } + return true; } @@ -322,7 +590,7 @@ bool t_rpc_command_executor::start_mining(cryptonote::account_public_address add req.miner_address = cryptonote::get_account_address_as_str(false, address); req.threads_count = num_threads; - if (m_rpc_client.rpc_request(req, res, "/start_mining", "Mining did not start")) + if (m_rpc_client->rpc_request(req, res, "/start_mining", "Mining did not start")) { tools::success_msg_writer() << "Mining started"; } @@ -333,10 +601,25 @@ bool t_rpc_command_executor::stop_mining() { cryptonote::COMMAND_RPC_STOP_MINING::request req; cryptonote::COMMAND_RPC_STOP_MINING::response res; - if (m_rpc_client.rpc_request(req, res, "/stop_mining", "Mining did not stop")) + std::string fail_message = "Mining did not stop"; + + if (m_is_rpc) { - tools::success_msg_writer() << "Mining stopped"; + if (!m_rpc_client->rpc_request(req, res, "/stop_mining", fail_message.c_str())) + { + return true; + } } + else + { + if (!m_rpc_server->on_stop_mining(req, res)) + { + tools::fail_msg_writer() << fail_message.c_str(); + return true; + } + } + + tools::success_msg_writer() << "Mining stopped"; return true; } @@ -359,17 +642,38 @@ bool t_rpc_command_executor::stop_daemon() //# endif // Stop via RPC - if(m_rpc_client.rpc_request(req, res, "/stop_daemon", "Daemon did not stop")) + std::string fail_message = "Daemon did not stop"; + + if (m_is_rpc) + { + if(!m_rpc_client->rpc_request(req, res, "/stop_daemon", fail_message.c_str())) + { + return true; + } + } + else { - tools::success_msg_writer() << "Stop signal sent"; + if (!m_rpc_server->on_stop_daemon(req, res)) + { + tools::fail_msg_writer() << fail_message.c_str(); + return true; + } } + tools::success_msg_writer() << "Stop signal sent"; + return true; } bool t_rpc_command_executor::print_status() { - bool daemon_is_alive = m_rpc_client.check_connection(); + if (!m_is_rpc) + { + tools::success_msg_writer() << "print_status makes no sense in interactive mode"; + return true; + } + + bool daemon_is_alive = m_rpc_client->check_connection(); if(daemon_is_alive) { tools::success_msg_writer() << "bitmonerod is running"; @@ -383,34 +687,134 @@ bool t_rpc_command_executor::print_status() bool t_rpc_command_executor::set_limit(int limit) { -/* epee::net_utils::connection_basic::set_rate_down_limit( limit ); epee::net_utils::connection_basic::set_rate_up_limit( limit ); std::cout << "Set limit-down to " << limit/1024 << " kB/s" << std::endl; std::cout << "Set limit-up to " << limit/1024 << " kB/s" << std::endl; -*/ - return true; } bool t_rpc_command_executor::set_limit_up(int limit) { -/* epee::net_utils::connection_basic::set_rate_up_limit( limit ); std::cout << "Set limit-up to " << limit/1024 << " kB/s" << std::endl; -*/ - return true; } bool t_rpc_command_executor::set_limit_down(int limit) { -/* epee::net_utils::connection_basic::set_rate_down_limit( limit ); std::cout << "Set limit-down to " << limit/1024 << " kB/s" << std::endl; -*/ - return true; } +bool t_rpc_command_executor::fast_exit() +{ + cryptonote::COMMAND_RPC_FAST_EXIT::request req; + cryptonote::COMMAND_RPC_FAST_EXIT::response res; + + std::string fail_message = "Daemon did not stop"; + + if (m_is_rpc) + { + if (!m_rpc_client->rpc_request(req, res, "/fast_exit", fail_message.c_str())) + { + return true; + } + } + + else + { + if (!m_rpc_server->on_fast_exit(req, res)) + { + tools::fail_msg_writer() << fail_message.c_str(); + return true; + } + } + + tools::success_msg_writer() << "Daemon stopped"; + return true; +} + +bool t_rpc_command_executor::out_peers(uint64_t limit) +{ + cryptonote::COMMAND_RPC_OUT_PEERS::request req; + cryptonote::COMMAND_RPC_OUT_PEERS::response res; + + epee::json_rpc::error error_resp; + + req.out_peers = limit; + + std::string fail_message = "Unsuccessful"; + + if (m_is_rpc) + { + if (!m_rpc_client->json_rpc_request(req, res, "/out_peers", fail_message.c_str())) + { + return true; + } + } + else + { + if (!m_rpc_server->on_out_peers(req, res)) + { + tools::fail_msg_writer() << fail_message.c_str(); + return true; + } + } + + return true; +} + +bool t_rpc_command_executor::start_save_graph() +{ + cryptonote::COMMAND_RPC_START_SAVE_GRAPH::request req; + cryptonote::COMMAND_RPC_START_SAVE_GRAPH::response res; + std::string fail_message = "Unsuccessful"; + + if (m_is_rpc) + { + if (!m_rpc_client->rpc_request(req, res, "/start_save_graph", fail_message.c_str())) + { + return true; + } + } + + else + { + if (!m_rpc_server->on_start_save_graph(req, res)) + { + tools::fail_msg_writer() << fail_message.c_str(); + return true; + } + } + + return true; +} + +bool t_rpc_command_executor::stop_save_graph() +{ + cryptonote::COMMAND_RPC_STOP_SAVE_GRAPH::request req; + cryptonote::COMMAND_RPC_STOP_SAVE_GRAPH::response res; + std::string fail_message = "Unsuccessful"; + + if (m_is_rpc) + { + if (!m_rpc_client->rpc_request(req, res, "/stop_save_graph", fail_message.c_str())) + { + return true; + } + } + + else + { + if (!m_rpc_server->on_stop_save_graph(req, res)) + { + tools::fail_msg_writer() << fail_message.c_str(); + return true; + } + } + return true; +} + }// namespace daemonize diff --git a/src/daemon/rpc_command_executor.h b/src/daemon/rpc_command_executor.h index f1dc9cc76..43b8a9fe0 100644 --- a/src/daemon/rpc_command_executor.h +++ b/src/daemon/rpc_command_executor.h @@ -43,18 +43,26 @@ #include "cryptonote_core/cryptonote_core.h" #include "cryptonote_protocol/cryptonote_protocol_handler.h" #include "p2p/net_node.h" +#include "rpc/core_rpc_server.h" namespace daemonize { class t_rpc_command_executor final { private: - tools::t_rpc_client m_rpc_client; + tools::t_rpc_client* m_rpc_client; + cryptonote::core_rpc_server* m_rpc_server; + bool m_is_rpc; + public: t_rpc_command_executor( uint32_t ip , uint16_t port + , bool is_rpc = true + , cryptonote::core_rpc_server* rpc_server = NULL ); + ~t_rpc_command_executor(); + bool print_peer_list(); bool save_blockchain(); @@ -97,7 +105,13 @@ public: bool set_limit_down(int limit); - + bool fast_exit(); + + bool out_peers(uint64_t limit); + + bool start_save_graph(); + + bool stop_save_graph(); }; } // namespace daemonize |