From f264b85d1b7778a59dfd9aed1013fd8dea70b6b8 Mon Sep 17 00:00:00 2001 From: Doyle T Date: Fri, 31 Aug 2018 17:41:49 +1000 Subject: [PATCH] Add print_sr for getting staking requirement --- src/daemon/command_parser_executor.cpp | 19 ++++++++++++++++ src/daemon/command_parser_executor.h | 2 ++ src/daemon/command_server.cpp | 6 +++++ src/daemon/rpc_command_executor.cpp | 30 +++++++++++++++++++++++++ src/daemon/rpc_command_executor.h | 2 ++ src/rpc/core_rpc_server.cpp | 8 ++++++- src/rpc/core_rpc_server.h | 2 ++ src/rpc/core_rpc_server_commands_defs.h | 22 ++++++++++++++++++ 8 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/daemon/command_parser_executor.cpp b/src/daemon/command_parser_executor.cpp index 80b396e02f..e55ab3f4ce 100644 --- a/src/daemon/command_parser_executor.cpp +++ b/src/daemon/command_parser_executor.cpp @@ -150,6 +150,25 @@ bool t_command_parser_executor::print_sn_key(const std::vector& arg return result; } +bool t_command_parser_executor::print_sr(const std::vector& args) +{ + if (args.size() != 1) + { + std::cout << "expected 1 argument, , received: " << args.size() << std::endl; + return false; + } + + uint64_t height = 0; + if(!epee::string_tools::get_xtype_from_string(height, args[0])) + { + std::cout << "wrong block height parameter" << std::endl; + return false; + } + + bool result = m_executor.print_sr(height); + return result; +} + bool t_command_parser_executor::prepare_registration() { bool result = m_executor.prepare_registration(); diff --git a/src/daemon/command_parser_executor.h b/src/daemon/command_parser_executor.h index d912ee1654..cadbdcb821 100644 --- a/src/daemon/command_parser_executor.h +++ b/src/daemon/command_parser_executor.h @@ -79,6 +79,8 @@ class t_command_parser_executor final bool print_sn_key(const std::vector& args); + bool print_sr(const std::vector& args); + bool prepare_registration(); bool print_sn(const std::vector& args); diff --git a/src/daemon/command_server.cpp b/src/daemon/command_server.cpp index 34c9a0ba2d..b5850cceb6 100644 --- a/src/daemon/command_server.cpp +++ b/src/daemon/command_server.cpp @@ -107,6 +107,12 @@ t_command_server::t_command_server( , "print_sn_key" , "Print this daemon's service node key, if it is one and launched in service node mode." ); + m_command_lookup.set_handler( + "print_sr" + , std::bind(&t_command_parser_executor::print_sr, &m_parser, p::_1) + , "print_sr " + , "Print the staking requirement for the height." + ); m_command_lookup.set_handler( "prepare_registration" , std::bind(&t_command_parser_executor::prepare_registration, &m_parser) diff --git a/src/daemon/rpc_command_executor.cpp b/src/daemon/rpc_command_executor.cpp index 76cd46e35c..0486bf3dec 100644 --- a/src/daemon/rpc_command_executor.cpp +++ b/src/daemon/rpc_command_executor.cpp @@ -2232,6 +2232,36 @@ bool t_rpc_command_executor::print_sn_status() return result; } +bool t_rpc_command_executor::print_sr(uint64_t height) +{ + cryptonote::COMMAND_RPC_GET_STAKING_REQUIREMENT::request req = {}; + cryptonote::COMMAND_RPC_GET_STAKING_REQUIREMENT::response res = {}; + std::string fail_message = "Unsuccessful"; + epee::json_rpc::error error_resp; + req.height = height; + + if (m_is_rpc) + { + if (!m_rpc_client->json_rpc_request(req, res, "get_staking_requirement", fail_message.c_str())) + { + tools::fail_msg_writer() << make_error(fail_message, res.status); + return true; + } + } + else + { + epee::json_rpc::error error_resp; + if (!m_rpc_server->on_get_staking_requirement(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) + { + tools::fail_msg_writer() << make_error(fail_message, error_resp.message); + return true; + } + } + + tools::success_msg_writer() << "Staking Requirement: " << cryptonote::print_money(res.staking_requirement); + return true; +} + bool t_rpc_command_executor::print_sn_key() { cryptonote::COMMAND_RPC_GET_SERVICE_NODE_KEY::request req = {}; diff --git a/src/daemon/rpc_command_executor.h b/src/daemon/rpc_command_executor.h index 4605182d01..98dba6a7e8 100644 --- a/src/daemon/rpc_command_executor.h +++ b/src/daemon/rpc_command_executor.h @@ -162,6 +162,8 @@ class t_rpc_command_executor final { bool print_sn_status(); + bool print_sr(uint64_t height); + bool prepare_registration(); bool print_sn(const std::vector &args); diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp index d31c8dff2f..28e25746af 100644 --- a/src/rpc/core_rpc_server.cpp +++ b/src/rpc/core_rpc_server.cpp @@ -2317,7 +2317,13 @@ namespace cryptonote return true; } //------------------------------------------------------------------------------------------------------------------------------ - + bool core_rpc_server::on_get_staking_requirement(const COMMAND_RPC_GET_STAKING_REQUIREMENT::request& req, COMMAND_RPC_GET_STAKING_REQUIREMENT::response& res, epee::json_rpc::error& error_resp) + { + PERF_TIMER(on_get_staking_requirement); + res.staking_requirement = service_nodes::get_staking_requirement(nettype(), req.height); + res.status = CORE_RPC_STATUS_OK; + return true; + } const command_line::arg_descriptor core_rpc_server::arg_rpc_bind_port = { "rpc-bind-port" diff --git a/src/rpc/core_rpc_server.h b/src/rpc/core_rpc_server.h index f07687d575..2d01838243 100644 --- a/src/rpc/core_rpc_server.h +++ b/src/rpc/core_rpc_server.h @@ -159,6 +159,7 @@ namespace cryptonote MAP_JON_RPC_WE("get_service_node_registration_cmd", on_get_service_node_registration_cmd, COMMAND_RPC_GET_SERVICE_NODE_REGISTRATION_CMD) MAP_JON_RPC_WE("get_service_node_key", on_get_service_node_key, COMMAND_RPC_GET_SERVICE_NODE_KEY) MAP_JON_RPC_WE("get_service_nodes", on_get_service_nodes, COMMAND_RPC_GET_SERVICE_NODES) + MAP_JON_RPC_WE("get_staking_requirement", on_get_staking_requirement, COMMAND_RPC_GET_STAKING_REQUIREMENT) END_JSON_RPC_MAP() END_URI_MAP2() @@ -226,6 +227,7 @@ namespace cryptonote bool on_get_service_node_registration_cmd(const COMMAND_RPC_GET_SERVICE_NODE_REGISTRATION_CMD::request& req, COMMAND_RPC_GET_SERVICE_NODE_REGISTRATION_CMD::response& res, epee::json_rpc::error& error_resp); bool on_get_service_node_key(const COMMAND_RPC_GET_SERVICE_NODE_KEY::request& req, COMMAND_RPC_GET_SERVICE_NODE_KEY::response& res, epee::json_rpc::error &error_resp); bool on_get_service_nodes(const COMMAND_RPC_GET_SERVICE_NODES::request& req, COMMAND_RPC_GET_SERVICE_NODES::response& res, epee::json_rpc::error& error_resp); + bool on_get_staking_requirement(const COMMAND_RPC_GET_STAKING_REQUIREMENT::request& req, COMMAND_RPC_GET_STAKING_REQUIREMENT::response& res, epee::json_rpc::error& error_resp); //----------------------- private: diff --git a/src/rpc/core_rpc_server_commands_defs.h b/src/rpc/core_rpc_server_commands_defs.h index 0ad2b03a38..64730aaea2 100644 --- a/src/rpc/core_rpc_server_commands_defs.h +++ b/src/rpc/core_rpc_server_commands_defs.h @@ -34,6 +34,7 @@ #include "cryptonote_basic/verification_context.h" #include "cryptonote_basic/difficulty.h" #include "crypto/hash.h" +#include "cryptonote_config.h" #include "cryptonote_core/service_node_deregister.h" namespace cryptonote @@ -2411,4 +2412,25 @@ namespace cryptonote END_KV_SERIALIZE_MAP() }; }; + + struct COMMAND_RPC_GET_STAKING_REQUIREMENT + { + struct request + { + uint64_t height; + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(height) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + uint64_t staking_requirement; + std::string status; + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(staking_requirement) + KV_SERIALIZE(status) + END_KV_SERIALIZE_MAP() + }; + }; }