From 8bbcbcfb0db635fce36858cf6dfdebf6c65b681a Mon Sep 17 00:00:00 2001 From: stoffu Date: Sun, 19 Mar 2017 20:31:38 +0900 Subject: wallet rpc: enable solo mining --- src/wallet/wallet_rpc_server.cpp | 51 ++++++++++++++++++++++++++++ src/wallet/wallet_rpc_server.h | 5 +++ src/wallet/wallet_rpc_server_commands_defs.h | 37 ++++++++++++++++++++ 3 files changed, 93 insertions(+) diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp index f77ddf431..f96b1f51e 100644 --- a/src/wallet/wallet_rpc_server.cpp +++ b/src/wallet/wallet_rpc_server.cpp @@ -46,6 +46,7 @@ using namespace epee; #include "string_tools.h" #include "crypto/hash.h" #include "rpc/rpc_args.h" +#include "rpc/core_rpc_server_commands_defs.h" #undef MONERO_DEFAULT_LOG_CATEGORY #define MONERO_DEFAULT_LOG_CATEGORY "wallet.rpc" @@ -172,6 +173,8 @@ namespace tools LOG_PRINT_L0(tr("RPC username/password is stored in file ") << temp); } // end auth enabled + m_http_client.set_server(m_wallet.get_daemon_address(), m_wallet.get_daemon_login()); + m_net_server.set_threads_prefix("RPC"); return epee::http_server_impl_base::init( std::move(bind_port), std::move(rpc_config->bind_ip), std::move(http_login) @@ -1424,6 +1427,54 @@ namespace tools return true; } //------------------------------------------------------------------------------------------------------------------------------ + bool wallet_rpc_server::on_start_mining(const wallet_rpc::COMMAND_RPC_START_MINING::request& req, wallet_rpc::COMMAND_RPC_START_MINING::response& res, epee::json_rpc::error& er) + { + if (!m_trusted_daemon) + { + er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.message = "This command requires a trusted daemon."; + return false; + } + + size_t max_mining_threads_count = (std::max)(tools::get_max_concurrency(), static_cast(2)); + if (req.threads_count < 1 || max_mining_threads_count < req.threads_count) + { + er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.message = "The specified number of threads is inappropriate."; + return false; + } + + cryptonote::COMMAND_RPC_START_MINING::request daemon_req = AUTO_VAL_INIT(daemon_req); + daemon_req.miner_address = m_wallet.get_account().get_public_address_str(m_wallet.testnet()); + daemon_req.threads_count = req.threads_count; + daemon_req.do_background_mining = req.do_background_mining; + daemon_req.ignore_battery = req.ignore_battery; + + cryptonote::COMMAND_RPC_START_MINING::response daemon_res; + bool r = net_utils::invoke_http_json("/start_mining", daemon_req, daemon_res, m_http_client); + if (!r || daemon_res.status != CORE_RPC_STATUS_OK) + { + er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.message = "Couldn't start mining due to unknown error."; + return false; + } + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ + bool wallet_rpc_server::on_stop_mining(const wallet_rpc::COMMAND_RPC_STOP_MINING::request& req, wallet_rpc::COMMAND_RPC_STOP_MINING::response& res, epee::json_rpc::error& er) + { + cryptonote::COMMAND_RPC_STOP_MINING::request daemon_req; + cryptonote::COMMAND_RPC_STOP_MINING::response daemon_res; + bool r = net_utils::invoke_http_json("/stop_mining", daemon_req, daemon_res, m_http_client); + if (!r || daemon_res.status != CORE_RPC_STATUS_OK) + { + er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.message = "Couldn't stop mining due to unknown error."; + return false; + } + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ } int main(int argc, char** argv) { diff --git a/src/wallet/wallet_rpc_server.h b/src/wallet/wallet_rpc_server.h index 7dd07d12f..eafe3fc87 100644 --- a/src/wallet/wallet_rpc_server.h +++ b/src/wallet/wallet_rpc_server.h @@ -93,6 +93,8 @@ namespace tools MAP_JON_RPC_WE("add_address_book", on_add_address_book, wallet_rpc::COMMAND_RPC_ADD_ADDRESS_BOOK_ENTRY) MAP_JON_RPC_WE("delete_address_book",on_delete_address_book,wallet_rpc::COMMAND_RPC_DELETE_ADDRESS_BOOK_ENTRY) MAP_JON_RPC_WE("rescan_spent", on_rescan_spent, wallet_rpc::COMMAND_RPC_RESCAN_SPENT) + MAP_JON_RPC_WE("start_mining", on_start_mining, wallet_rpc::COMMAND_RPC_START_MINING) + MAP_JON_RPC_WE("stop_mining", on_stop_mining, wallet_rpc::COMMAND_RPC_STOP_MINING) END_JSON_RPC_MAP() END_URI_MAP2() @@ -127,6 +129,8 @@ namespace tools bool on_add_address_book(const wallet_rpc::COMMAND_RPC_ADD_ADDRESS_BOOK_ENTRY::request& req, wallet_rpc::COMMAND_RPC_ADD_ADDRESS_BOOK_ENTRY::response& res, epee::json_rpc::error& er); bool on_delete_address_book(const wallet_rpc::COMMAND_RPC_DELETE_ADDRESS_BOOK_ENTRY::request& req, wallet_rpc::COMMAND_RPC_DELETE_ADDRESS_BOOK_ENTRY::response& res, epee::json_rpc::error& er); bool on_rescan_spent(const wallet_rpc::COMMAND_RPC_RESCAN_SPENT::request& req, wallet_rpc::COMMAND_RPC_RESCAN_SPENT::response& res, epee::json_rpc::error& er); + bool on_start_mining(const wallet_rpc::COMMAND_RPC_START_MINING::request& req, wallet_rpc::COMMAND_RPC_START_MINING::response& res, epee::json_rpc::error& er); + bool on_stop_mining(const wallet_rpc::COMMAND_RPC_STOP_MINING::request& req, wallet_rpc::COMMAND_RPC_STOP_MINING::response& res, epee::json_rpc::error& er); //json rpc v2 bool on_query_key(const wallet_rpc::COMMAND_RPC_QUERY_KEY::request& req, wallet_rpc::COMMAND_RPC_QUERY_KEY::response& res, epee::json_rpc::error& er); @@ -141,5 +145,6 @@ namespace tools std::string rpc_login_filename; std::atomic m_stop; bool m_trusted_daemon; + epee::net_utils::http::http_simple_client m_http_client; }; } diff --git a/src/wallet/wallet_rpc_server_commands_defs.h b/src/wallet/wallet_rpc_server_commands_defs.h index 2ec965b4d..49d1c59fb 100644 --- a/src/wallet/wallet_rpc_server_commands_defs.h +++ b/src/wallet/wallet_rpc_server_commands_defs.h @@ -872,5 +872,42 @@ namespace wallet_rpc }; }; + struct COMMAND_RPC_START_MINING + { + struct request + { + uint64_t threads_count; + bool do_background_mining; + bool ignore_battery; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(threads_count) + KV_SERIALIZE(do_background_mining) + KV_SERIALIZE(ignore_battery) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + BEGIN_KV_SERIALIZE_MAP() + END_KV_SERIALIZE_MAP() + }; + }; + + struct COMMAND_RPC_STOP_MINING + { + struct request + { + BEGIN_KV_SERIALIZE_MAP() + END_KV_SERIALIZE_MAP() + }; + + struct response + { + BEGIN_KV_SERIALIZE_MAP() + END_KV_SERIALIZE_MAP() + }; + }; + } } -- cgit v1.2.3