2018-01-07 18:55:52 -05:00
|
|
|
// Copyright (c) 2017-2018, The Monero Project
|
2017-01-07 14:23:57 -05:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include "node_rpc_proxy.h"
|
|
|
|
#include "rpc/core_rpc_server_commands_defs.h"
|
|
|
|
#include "common/json_util.h"
|
|
|
|
#include "storages/http_abstract_invoke.h"
|
|
|
|
|
|
|
|
using namespace epee;
|
|
|
|
|
|
|
|
namespace tools
|
|
|
|
{
|
|
|
|
|
2017-03-18 19:06:06 -04:00
|
|
|
static const std::chrono::seconds rpc_timeout = std::chrono::minutes(3) + std::chrono::seconds(30);
|
|
|
|
|
2017-01-25 00:16:05 -05:00
|
|
|
NodeRPCProxy::NodeRPCProxy(epee::net_utils::http::http_simple_client &http_client, boost::mutex &mutex)
|
|
|
|
: m_http_client(http_client)
|
|
|
|
, m_daemon_rpc_mutex(mutex)
|
2018-06-30 17:06:09 -04:00
|
|
|
{
|
|
|
|
invalidate();
|
|
|
|
}
|
2017-01-07 14:23:57 -05:00
|
|
|
|
2017-02-27 12:38:05 -05:00
|
|
|
void NodeRPCProxy::invalidate()
|
|
|
|
{
|
|
|
|
m_height = 0;
|
|
|
|
for (size_t n = 0; n < 256; ++n)
|
|
|
|
m_earliest_height[n] = 0;
|
2018-07-18 17:24:53 -04:00
|
|
|
m_dynamic_base_fee_estimate = 0;
|
|
|
|
m_dynamic_base_fee_estimate_cached_height = 0;
|
|
|
|
m_dynamic_base_fee_estimate_grace_blocks = 0;
|
|
|
|
m_fee_quantization_mask = 1;
|
2017-02-27 12:44:06 -05:00
|
|
|
m_rpc_version = 0;
|
2017-08-02 09:43:47 -04:00
|
|
|
m_target_height = 0;
|
2018-07-18 17:24:53 -04:00
|
|
|
m_block_weight_limit = 0;
|
2018-06-30 17:06:09 -04:00
|
|
|
m_get_info_time = 0;
|
2017-02-27 12:44:06 -05:00
|
|
|
}
|
|
|
|
|
2017-08-02 09:43:47 -04:00
|
|
|
boost::optional<std::string> NodeRPCProxy::get_rpc_version(uint32_t &rpc_version) const
|
2017-02-27 12:44:06 -05:00
|
|
|
{
|
|
|
|
if (m_rpc_version == 0)
|
|
|
|
{
|
2018-02-15 19:49:29 -05:00
|
|
|
cryptonote::COMMAND_RPC_GET_VERSION::request req_t = AUTO_VAL_INIT(req_t);
|
|
|
|
cryptonote::COMMAND_RPC_GET_VERSION::response resp_t = AUTO_VAL_INIT(resp_t);
|
2017-02-27 12:44:06 -05:00
|
|
|
m_daemon_rpc_mutex.lock();
|
2018-02-15 19:49:29 -05:00
|
|
|
bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_version", req_t, resp_t, m_http_client, rpc_timeout);
|
2017-02-27 12:44:06 -05:00
|
|
|
m_daemon_rpc_mutex.unlock();
|
2018-09-17 07:02:02 -04:00
|
|
|
CHECK_AND_ASSERT_MES(r, std::string("Failed to connect to daemon"), "Failed to connect to daemon");
|
2018-02-15 19:49:29 -05:00
|
|
|
CHECK_AND_ASSERT_MES(resp_t.status != CORE_RPC_STATUS_BUSY, resp_t.status, "Failed to connect to daemon");
|
|
|
|
CHECK_AND_ASSERT_MES(resp_t.status == CORE_RPC_STATUS_OK, resp_t.status, "Failed to get daemon RPC version");
|
|
|
|
m_rpc_version = resp_t.version;
|
2017-02-27 12:44:06 -05:00
|
|
|
}
|
|
|
|
rpc_version = m_rpc_version;
|
|
|
|
return boost::optional<std::string>();
|
2017-02-27 12:38:05 -05:00
|
|
|
}
|
|
|
|
|
2017-01-07 14:23:57 -05:00
|
|
|
void NodeRPCProxy::set_height(uint64_t h)
|
|
|
|
{
|
|
|
|
m_height = h;
|
|
|
|
}
|
|
|
|
|
2018-06-30 17:06:09 -04:00
|
|
|
boost::optional<std::string> NodeRPCProxy::get_info() const
|
2017-08-02 09:43:47 -04:00
|
|
|
{
|
|
|
|
const time_t now = time(NULL);
|
2018-06-30 17:06:09 -04:00
|
|
|
if (now >= m_get_info_time + 30) // re-cache every 30 seconds
|
2017-08-02 09:43:47 -04:00
|
|
|
{
|
2018-02-15 19:49:29 -05:00
|
|
|
cryptonote::COMMAND_RPC_GET_INFO::request req_t = AUTO_VAL_INIT(req_t);
|
|
|
|
cryptonote::COMMAND_RPC_GET_INFO::response resp_t = AUTO_VAL_INIT(resp_t);
|
2017-08-02 09:43:47 -04:00
|
|
|
|
|
|
|
m_daemon_rpc_mutex.lock();
|
2018-02-15 19:49:29 -05:00
|
|
|
bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_info", req_t, resp_t, m_http_client, rpc_timeout);
|
2017-08-02 09:43:47 -04:00
|
|
|
m_daemon_rpc_mutex.unlock();
|
|
|
|
|
2018-09-17 07:02:02 -04:00
|
|
|
CHECK_AND_ASSERT_MES(r, std::string("Failed to connect to daemon"), "Failed to connect to daemon");
|
2018-02-15 19:49:29 -05:00
|
|
|
CHECK_AND_ASSERT_MES(resp_t.status != CORE_RPC_STATUS_BUSY, resp_t.status, "Failed to connect to daemon");
|
|
|
|
CHECK_AND_ASSERT_MES(resp_t.status == CORE_RPC_STATUS_OK, resp_t.status, "Failed to get target blockchain height");
|
2018-06-30 17:06:09 -04:00
|
|
|
m_height = resp_t.height;
|
2018-02-15 19:49:29 -05:00
|
|
|
m_target_height = resp_t.target_height;
|
2018-07-18 17:24:53 -04:00
|
|
|
m_block_weight_limit = resp_t.block_weight_limit ? resp_t.block_weight_limit : resp_t.block_size_limit;
|
2018-06-30 17:06:09 -04:00
|
|
|
m_get_info_time = now;
|
2017-08-02 09:43:47 -04:00
|
|
|
}
|
2018-06-30 17:06:09 -04:00
|
|
|
return boost::optional<std::string>();
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<std::string> NodeRPCProxy::get_height(uint64_t &height) const
|
|
|
|
{
|
|
|
|
auto res = get_info();
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
height = m_height;
|
|
|
|
return boost::optional<std::string>();
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<std::string> NodeRPCProxy::get_target_height(uint64_t &height) const
|
|
|
|
{
|
|
|
|
auto res = get_info();
|
|
|
|
if (res)
|
|
|
|
return res;
|
2017-08-02 09:43:47 -04:00
|
|
|
height = m_target_height;
|
|
|
|
return boost::optional<std::string>();
|
|
|
|
}
|
|
|
|
|
2018-07-18 17:24:53 -04:00
|
|
|
boost::optional<std::string> NodeRPCProxy::get_block_weight_limit(uint64_t &block_weight_limit) const
|
2018-06-30 17:06:09 -04:00
|
|
|
{
|
|
|
|
auto res = get_info();
|
|
|
|
if (res)
|
|
|
|
return res;
|
2018-07-18 17:24:53 -04:00
|
|
|
block_weight_limit = m_block_weight_limit;
|
2018-06-30 17:06:09 -04:00
|
|
|
return boost::optional<std::string>();
|
|
|
|
}
|
|
|
|
|
2017-08-02 09:43:47 -04:00
|
|
|
boost::optional<std::string> NodeRPCProxy::get_earliest_height(uint8_t version, uint64_t &earliest_height) const
|
2017-01-07 14:23:57 -05:00
|
|
|
{
|
|
|
|
if (m_earliest_height[version] == 0)
|
|
|
|
{
|
2018-02-15 19:49:29 -05:00
|
|
|
cryptonote::COMMAND_RPC_HARD_FORK_INFO::request req_t = AUTO_VAL_INIT(req_t);
|
|
|
|
cryptonote::COMMAND_RPC_HARD_FORK_INFO::response resp_t = AUTO_VAL_INIT(resp_t);
|
2017-01-07 14:23:57 -05:00
|
|
|
|
|
|
|
m_daemon_rpc_mutex.lock();
|
2018-02-15 19:49:29 -05:00
|
|
|
req_t.version = version;
|
|
|
|
bool r = net_utils::invoke_http_json_rpc("/json_rpc", "hard_fork_info", req_t, resp_t, m_http_client, rpc_timeout);
|
2017-01-07 14:23:57 -05:00
|
|
|
m_daemon_rpc_mutex.unlock();
|
2018-09-17 07:02:02 -04:00
|
|
|
CHECK_AND_ASSERT_MES(r, std::string("Failed to connect to daemon"), "Failed to connect to daemon");
|
2018-02-15 19:49:29 -05:00
|
|
|
CHECK_AND_ASSERT_MES(resp_t.status != CORE_RPC_STATUS_BUSY, resp_t.status, "Failed to connect to daemon");
|
|
|
|
CHECK_AND_ASSERT_MES(resp_t.status == CORE_RPC_STATUS_OK, resp_t.status, "Failed to get hard fork status");
|
2018-08-10 10:49:18 -04:00
|
|
|
m_earliest_height[version] = resp_t.earliest_height;
|
2017-01-07 14:23:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
earliest_height = m_earliest_height[version];
|
|
|
|
return boost::optional<std::string>();
|
|
|
|
}
|
|
|
|
|
2018-07-18 17:24:53 -04:00
|
|
|
boost::optional<std::string> NodeRPCProxy::get_dynamic_base_fee_estimate(uint64_t grace_blocks, uint64_t &fee) const
|
2017-01-07 14:23:57 -05:00
|
|
|
{
|
|
|
|
uint64_t height;
|
|
|
|
|
|
|
|
boost::optional<std::string> result = get_height(height);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
|
2018-07-18 17:24:53 -04:00
|
|
|
if (m_dynamic_base_fee_estimate_cached_height != height || m_dynamic_base_fee_estimate_grace_blocks != grace_blocks)
|
2017-01-07 14:23:57 -05:00
|
|
|
{
|
2018-07-18 17:24:53 -04:00
|
|
|
cryptonote::COMMAND_RPC_GET_BASE_FEE_ESTIMATE::request req_t = AUTO_VAL_INIT(req_t);
|
|
|
|
cryptonote::COMMAND_RPC_GET_BASE_FEE_ESTIMATE::response resp_t = AUTO_VAL_INIT(resp_t);
|
2017-01-07 14:23:57 -05:00
|
|
|
|
|
|
|
m_daemon_rpc_mutex.lock();
|
2018-02-15 19:49:29 -05:00
|
|
|
req_t.grace_blocks = grace_blocks;
|
|
|
|
bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_fee_estimate", req_t, resp_t, m_http_client, rpc_timeout);
|
2017-01-07 14:23:57 -05:00
|
|
|
m_daemon_rpc_mutex.unlock();
|
2018-09-17 07:02:02 -04:00
|
|
|
CHECK_AND_ASSERT_MES(r, std::string("Failed to connect to daemon"), "Failed to connect to daemon");
|
2018-02-15 19:49:29 -05:00
|
|
|
CHECK_AND_ASSERT_MES(resp_t.status != CORE_RPC_STATUS_BUSY, resp_t.status, "Failed to connect to daemon");
|
|
|
|
CHECK_AND_ASSERT_MES(resp_t.status == CORE_RPC_STATUS_OK, resp_t.status, "Failed to get fee estimate");
|
2018-07-18 17:24:53 -04:00
|
|
|
m_dynamic_base_fee_estimate = resp_t.fee;
|
|
|
|
m_dynamic_base_fee_estimate_cached_height = height;
|
|
|
|
m_dynamic_base_fee_estimate_grace_blocks = grace_blocks;
|
|
|
|
m_fee_quantization_mask = resp_t.quantization_mask;
|
2017-01-07 14:23:57 -05:00
|
|
|
}
|
|
|
|
|
2018-07-18 17:24:53 -04:00
|
|
|
fee = m_dynamic_base_fee_estimate;
|
|
|
|
return boost::optional<std::string>();
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<std::string> NodeRPCProxy::get_fee_quantization_mask(uint64_t &fee_quantization_mask) const
|
|
|
|
{
|
|
|
|
uint64_t height;
|
|
|
|
|
|
|
|
boost::optional<std::string> result = get_height(height);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
if (m_dynamic_base_fee_estimate_cached_height != height)
|
|
|
|
{
|
|
|
|
cryptonote::COMMAND_RPC_GET_BASE_FEE_ESTIMATE::request req_t = AUTO_VAL_INIT(req_t);
|
|
|
|
cryptonote::COMMAND_RPC_GET_BASE_FEE_ESTIMATE::response resp_t = AUTO_VAL_INIT(resp_t);
|
|
|
|
|
|
|
|
m_daemon_rpc_mutex.lock();
|
|
|
|
req_t.grace_blocks = m_dynamic_base_fee_estimate_grace_blocks;
|
|
|
|
bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_fee_estimate", req_t, resp_t, m_http_client, rpc_timeout);
|
|
|
|
m_daemon_rpc_mutex.unlock();
|
2018-09-17 07:02:02 -04:00
|
|
|
CHECK_AND_ASSERT_MES(r, std::string("Failed to connect to daemon"), "Failed to connect to daemon");
|
2018-07-18 17:24:53 -04:00
|
|
|
CHECK_AND_ASSERT_MES(resp_t.status != CORE_RPC_STATUS_BUSY, resp_t.status, "Failed to connect to daemon");
|
|
|
|
CHECK_AND_ASSERT_MES(resp_t.status == CORE_RPC_STATUS_OK, resp_t.status, "Failed to get fee estimate");
|
|
|
|
m_dynamic_base_fee_estimate = resp_t.fee;
|
|
|
|
m_dynamic_base_fee_estimate_cached_height = height;
|
|
|
|
m_fee_quantization_mask = resp_t.quantization_mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
fee_quantization_mask = m_fee_quantization_mask;
|
|
|
|
if (fee_quantization_mask == 0)
|
|
|
|
{
|
|
|
|
MERROR("Fee quantization mask is 0, forcing to 1");
|
|
|
|
fee_quantization_mask = 1;
|
|
|
|
}
|
2017-01-07 14:23:57 -05:00
|
|
|
return boost::optional<std::string>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|