wallet: add a node RPC cache layer for simple RPC calls

Mostly getinfo and get_hard_fork_info, which are called
pretty often. This speeds up transfers as a bonus.
This commit is contained in:
moneromooo-monero 2017-01-07 19:23:57 +00:00
parent d86ae2bec6
commit 693c190881
No known key found for this signature in database
GPG Key ID: 686F07454D6CEFC3
6 changed files with 259 additions and 79 deletions

View File

@ -34,6 +34,7 @@ set(wallet_sources
password_container.cpp
wallet2.cpp
wallet_args.cpp
node_rpc_proxy.cpp
api/wallet.cpp
api/wallet_manager.cpp
api/transaction_info.cpp
@ -55,6 +56,7 @@ set(wallet_private_headers
wallet_rpc_server.h
wallet_rpc_server_commands_defs.h
wallet_rpc_server_error_codes.h
node_rpc_proxy.h
api/wallet.h
api/wallet_manager.h
api/transaction_info.h

View File

@ -0,0 +1,134 @@
// Copyright (c) 2017, 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.
#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
{
void NodeRPCProxy::init(const std::string &daemon_address)
{
m_daemon_address = daemon_address;
m_height = 0;
m_height_time = 0;
for (auto &slot: m_earliest_height)
slot = 0;
m_dynamic_per_kb_fee_estimate = 0;
m_dynamic_per_kb_fee_estimate_cached_height = 0;
m_dynamic_per_kb_fee_estimate_grace_blocks = 0;
}
boost::optional<std::string> NodeRPCProxy::get_height(uint64_t &height)
{
const time_t now = time(NULL);
if (m_height == 0 || now >= m_height_time + 30) // re-cache every 30 seconds
{
cryptonote::COMMAND_RPC_GET_HEIGHT::request req = AUTO_VAL_INIT(req);
cryptonote::COMMAND_RPC_GET_HEIGHT::response res = AUTO_VAL_INIT(res);
m_daemon_rpc_mutex.lock();
bool r = net_utils::invoke_http_json_remote_command2(m_daemon_address + "/getheight", req, res, m_http_client);
m_daemon_rpc_mutex.unlock();
CHECK_AND_ASSERT_MES(r, std::string(), "Failed to connect to daemon");
CHECK_AND_ASSERT_MES(res.status != CORE_RPC_STATUS_BUSY, res.status, "Failed to connect to daemon");
CHECK_AND_ASSERT_MES(res.status == CORE_RPC_STATUS_OK, res.status, "Failed to get current blockchain height");
m_height = res.height;
m_height_time = now;
}
height = m_height;
return boost::optional<std::string>();
}
void NodeRPCProxy::set_height(uint64_t h)
{
m_height = h;
}
boost::optional<std::string> NodeRPCProxy::get_earliest_height(uint8_t version, uint64_t &earliest_height)
{
if (m_earliest_height[version] == 0)
{
epee::json_rpc::request<cryptonote::COMMAND_RPC_HARD_FORK_INFO::request> req_t = AUTO_VAL_INIT(req_t);
epee::json_rpc::response<cryptonote::COMMAND_RPC_HARD_FORK_INFO::response, std::string> resp_t = AUTO_VAL_INIT(resp_t);
m_daemon_rpc_mutex.lock();
req_t.jsonrpc = "2.0";
req_t.id = epee::serialization::storage_entry(0);
req_t.method = "hard_fork_info";
req_t.params.version = version;
bool r = net_utils::invoke_http_json_remote_command2(m_daemon_address + "/json_rpc", req_t, resp_t, m_http_client);
m_daemon_rpc_mutex.unlock();
CHECK_AND_ASSERT_MES(r, std::string(), "Failed to connect to daemon");
CHECK_AND_ASSERT_MES(resp_t.result.status != CORE_RPC_STATUS_BUSY, resp_t.result.status, "Failed to connect to daemon");
CHECK_AND_ASSERT_MES(resp_t.result.status == CORE_RPC_STATUS_OK, resp_t.result.status, "Failed to get hard fork status");
m_earliest_height[version] = resp_t.result.earliest_height;
}
earliest_height = m_earliest_height[version];
return boost::optional<std::string>();
}
boost::optional<std::string> NodeRPCProxy::get_dynamic_per_kb_fee_estimate(uint64_t grace_blocks, uint64_t &fee)
{
uint64_t height;
boost::optional<std::string> result = get_height(height);
if (result)
return result;
if (m_dynamic_per_kb_fee_estimate_cached_height != height || m_dynamic_per_kb_fee_estimate_grace_blocks != grace_blocks)
{
epee::json_rpc::request<cryptonote::COMMAND_RPC_GET_PER_KB_FEE_ESTIMATE::request> req_t = AUTO_VAL_INIT(req_t);
epee::json_rpc::response<cryptonote::COMMAND_RPC_GET_PER_KB_FEE_ESTIMATE::response, std::string> resp_t = AUTO_VAL_INIT(resp_t);
m_daemon_rpc_mutex.lock();
req_t.jsonrpc = "2.0";
req_t.id = epee::serialization::storage_entry(0);
req_t.method = "get_fee_estimate";
req_t.params.grace_blocks = grace_blocks;
bool r = net_utils::invoke_http_json_remote_command2(m_daemon_address + "/json_rpc", req_t, resp_t, m_http_client);
m_daemon_rpc_mutex.unlock();
CHECK_AND_ASSERT_MES(r, std::string(), "Failed to connect to daemon");
CHECK_AND_ASSERT_MES(resp_t.result.status != CORE_RPC_STATUS_BUSY, resp_t.result.status, "Failed to connect to daemon");
CHECK_AND_ASSERT_MES(resp_t.result.status == CORE_RPC_STATUS_OK, resp_t.result.status, "Failed to get fee estimate");
m_dynamic_per_kb_fee_estimate = resp_t.result.fee;
m_dynamic_per_kb_fee_estimate_cached_height = height;
m_dynamic_per_kb_fee_estimate_grace_blocks = grace_blocks;
}
fee = m_dynamic_per_kb_fee_estimate;
return boost::optional<std::string>();
}
}

View File

@ -0,0 +1,65 @@
// Copyright (c) 2017, 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.
#pragma once
#include <string>
#include <boost/thread/mutex.hpp>
#include "include_base_utils.h"
#include "net/http_client.h"
namespace tools
{
class NodeRPCProxy
{
public:
NodeRPCProxy(epee::net_utils::http::http_simple_client &http_client, boost::mutex &mutex):
m_http_client(http_client), m_daemon_rpc_mutex(mutex) { init(""); }
void init(const std::string &daemon_address);
boost::optional<std::string> get_height(uint64_t &height);
void set_height(uint64_t h);
boost::optional<std::string> get_earliest_height(uint8_t version, uint64_t &earliest_height);
boost::optional<std::string> get_dynamic_per_kb_fee_estimate(uint64_t grace_blocks, uint64_t &fee);
private:
std::string m_daemon_address;
epee::net_utils::http::http_simple_client &m_http_client;
boost::mutex &m_daemon_rpc_mutex;
uint64_t m_height;
time_t m_height_time;
uint64_t m_earliest_height[256];
uint64_t m_dynamic_per_kb_fee_estimate;
uint64_t m_dynamic_per_kb_fee_estimate_cached_height;
uint64_t m_dynamic_per_kb_fee_estimate_grace_blocks;
};
}

View File

@ -396,6 +396,19 @@ std::unique_ptr<tools::wallet2> generate_from_json(const std::string& json_file,
return nullptr;
}
static void throw_on_rpc_response_error(const boost::optional<std::string> &status, const char *method)
{
// no error
if (!status)
return;
// empty string -> not connection
THROW_WALLET_EXCEPTION_IF(status->empty(), tools::error::no_connection_to_daemon, method);
THROW_WALLET_EXCEPTION_IF(*status == CORE_RPC_STATUS_BUSY, tools::error::daemon_busy, method);
THROW_WALLET_EXCEPTION_IF(*status != CORE_RPC_STATUS_OK, tools::error::wallet_generic_rpc_error, method, *status);
}
} //namespace
namespace tools
@ -473,6 +486,7 @@ void wallet2::init(const std::string& daemon_address, uint64_t upper_transaction
{
m_upper_transaction_size_limit = upper_transaction_size_limit;
m_daemon_address = daemon_address;
m_node_rpc_proxy.init(m_daemon_address);
}
//----------------------------------------------------------------------------------------------------
bool wallet2::is_deterministic() const
@ -1643,7 +1657,10 @@ void wallet2::refresh(uint64_t start_height, uint64_t & blocks_fetched, bool& re
blocks_fetched += added_blocks;
pull_thread.join();
if(blocks_start_height == next_blocks_start_height)
{
m_node_rpc_proxy.set_height(m_blockchain.size());
break;
}
// switch to the new blocks from the daemon
blocks_start_height = next_blocks_start_height;
@ -3259,20 +3276,12 @@ uint64_t wallet2::get_fee_multiplier(uint32_t priority, bool use_new_fee) const
//----------------------------------------------------------------------------------------------------
uint64_t wallet2::get_dynamic_per_kb_fee_estimate()
{
epee::json_rpc::request<cryptonote::COMMAND_RPC_GET_PER_KB_FEE_ESTIMATE::request> req_t = AUTO_VAL_INIT(req_t);
epee::json_rpc::response<cryptonote::COMMAND_RPC_GET_PER_KB_FEE_ESTIMATE::response, std::string> resp_t = AUTO_VAL_INIT(resp_t);
m_daemon_rpc_mutex.lock();
req_t.jsonrpc = "2.0";
req_t.id = epee::serialization::storage_entry(0);
req_t.method = "get_fee_estimate";
req_t.params.grace_blocks = FEE_ESTIMATE_GRACE_BLOCKS;
bool r = net_utils::invoke_http_json_remote_command2(m_daemon_address + "/json_rpc", req_t, resp_t, m_http_client);
m_daemon_rpc_mutex.unlock();
CHECK_AND_ASSERT_THROW_MES(r, "Failed to connect to daemon");
CHECK_AND_ASSERT_THROW_MES(resp_t.result.status != CORE_RPC_STATUS_BUSY, "Failed to connect to daemon");
CHECK_AND_ASSERT_THROW_MES(resp_t.result.status == CORE_RPC_STATUS_OK, "Failed to get fee estimate");
return resp_t.result.fee;
uint64_t fee;
boost::optional<std::string> result = m_node_rpc_proxy.get_dynamic_per_kb_fee_estimate(FEE_ESTIMATE_GRACE_BLOCKS, fee);
if (!result)
return fee;
LOG_PRINT_L1("Failed to query per kB fee, using " << print_money(FEE_PER_KB));
return FEE_PER_KB;
}
//----------------------------------------------------------------------------------------------------
uint64_t wallet2::get_per_kb_fee()
@ -3280,15 +3289,8 @@ uint64_t wallet2::get_per_kb_fee()
bool use_dyn_fee = use_fork_rules(HF_VERSION_DYNAMIC_FEE, -720 * 1);
if (!use_dyn_fee)
return FEE_PER_KB;
try
{
return get_dynamic_per_kb_fee_estimate();
}
catch (...)
{
LOG_PRINT_L1("Failed to query per kB fee, using " << print_money(FEE_PER_KB));
return FEE_PER_KB;
}
}
//----------------------------------------------------------------------------------------------------
// separated the call(s) to wallet2::transfer into their own function
@ -4513,39 +4515,19 @@ uint64_t wallet2::unlocked_dust_balance(const tx_dust_policy &dust_policy) const
//----------------------------------------------------------------------------------------------------
void wallet2::get_hard_fork_info(uint8_t version, uint64_t &earliest_height)
{
epee::json_rpc::request<cryptonote::COMMAND_RPC_HARD_FORK_INFO::request> req_t = AUTO_VAL_INIT(req_t);
epee::json_rpc::response<cryptonote::COMMAND_RPC_HARD_FORK_INFO::response, std::string> resp_t = AUTO_VAL_INIT(resp_t);
m_daemon_rpc_mutex.lock();
req_t.jsonrpc = "2.0";
req_t.id = epee::serialization::storage_entry(0);
req_t.method = "hard_fork_info";
req_t.params.version = version;
bool r = net_utils::invoke_http_json_remote_command2(m_daemon_address + "/json_rpc", req_t, resp_t, m_http_client);
m_daemon_rpc_mutex.unlock();
CHECK_AND_ASSERT_THROW_MES(r, "Failed to connect to daemon");
CHECK_AND_ASSERT_THROW_MES(resp_t.result.status != CORE_RPC_STATUS_BUSY, "Failed to connect to daemon");
CHECK_AND_ASSERT_THROW_MES(resp_t.result.status == CORE_RPC_STATUS_OK, "Failed to get hard fork status");
earliest_height = resp_t.result.earliest_height;
boost::optional<std::string> result = m_node_rpc_proxy.get_earliest_height(version, earliest_height);
throw_on_rpc_response_error(result, "get_hard_fork_info");
}
//----------------------------------------------------------------------------------------------------
bool wallet2::use_fork_rules(uint8_t version, int64_t early_blocks)
{
cryptonote::COMMAND_RPC_GET_HEIGHT::request req = AUTO_VAL_INIT(req);
cryptonote::COMMAND_RPC_GET_HEIGHT::response res = AUTO_VAL_INIT(res);
uint64_t height, earliest_height;
boost::optional<std::string> result = m_node_rpc_proxy.get_height(height);
throw_on_rpc_response_error(result, "get_info");
result = m_node_rpc_proxy.get_earliest_height(version, earliest_height);
throw_on_rpc_response_error(result, "get_hard_fork_info");
m_daemon_rpc_mutex.lock();
bool r = net_utils::invoke_http_json_remote_command2(m_daemon_address + "/getheight", req, res, m_http_client);
m_daemon_rpc_mutex.unlock();
CHECK_AND_ASSERT_MES(r, false, "Failed to connect to daemon");
CHECK_AND_ASSERT_MES(res.status != CORE_RPC_STATUS_BUSY, false, "Failed to connect to daemon");
CHECK_AND_ASSERT_MES(res.status == CORE_RPC_STATUS_OK, false, "Failed to get current blockchain height");
uint64_t earliest_height;
get_hard_fork_info(version, earliest_height); // can throw
bool close_enough = res.height >= earliest_height - early_blocks; // start using the rules that many blocks beforehand
bool close_enough = height >= earliest_height - early_blocks; // start using the rules that many blocks beforehand
if (close_enough)
LOG_PRINT_L2("Using v" << (unsigned)version << " rules");
else
@ -4734,34 +4716,17 @@ std::string wallet2::get_daemon_address() const
uint64_t wallet2::get_daemon_blockchain_height(string &err)
{
// XXX: DRY violation. copy-pasted from simplewallet.cpp:get_daemon_blockchain_height()
// consider to move it from simplewallet to wallet2 ?
COMMAND_RPC_GET_HEIGHT::request req;
COMMAND_RPC_GET_HEIGHT::response res = boost::value_initialized<COMMAND_RPC_GET_HEIGHT::response>();
m_daemon_rpc_mutex.lock();
bool ok = net_utils::invoke_http_json_remote_command2(m_daemon_address + "/getheight", req, res, m_http_client);
m_daemon_rpc_mutex.unlock();
// XXX: DRY violation. copy-pasted from simplewallet.cpp:interpret_rpc_response()
if (ok)
uint64_t height;
boost::optional<std::string> result = m_node_rpc_proxy.get_height(height);
if (result)
{
if (res.status == CORE_RPC_STATUS_BUSY)
{
err = "daemon is busy. Please try again later.";
err = *result;
return 0;
}
else if (res.status != CORE_RPC_STATUS_OK)
{
err = res.status;
}
else // success, cleaning up error message
{
err = "";
}
}
else
{
err = "possibly lost connection to daemon";
}
return res.height;
return height;
}
uint64_t wallet2::get_daemon_blockchain_target_height(string &err)

View File

@ -54,6 +54,7 @@
#include "wallet_errors.h"
#include "password_container.h"
#include "node_rpc_proxy.h"
#include <iostream>
@ -103,7 +104,7 @@ namespace tools
};
private:
wallet2(const wallet2&) : m_run(true), m_callback(0), m_testnet(false), m_always_confirm_transfers(true), m_print_ring_members(false), m_store_tx_info(true), m_default_mixin(0), m_default_priority(0), m_refresh_type(RefreshOptimizeCoinbase), m_auto_refresh(true), m_refresh_from_block_height(0), m_confirm_missing_payment_id(true) {}
wallet2(const wallet2&) : m_run(true), m_callback(0), m_testnet(false), m_always_confirm_transfers(true), m_print_ring_members(false), m_store_tx_info(true), m_default_mixin(0), m_default_priority(0), m_refresh_type(RefreshOptimizeCoinbase), m_auto_refresh(true), m_refresh_from_block_height(0), m_confirm_missing_payment_id(true), m_node_rpc_proxy(m_http_client, m_daemon_rpc_mutex) {}
public:
static const char* tr(const char* str);// { return i18n_translate(str, "cryptonote::simple_wallet"); }
@ -124,7 +125,7 @@ namespace tools
//! Uses stdin and stdout. Returns a wallet2 and password for wallet with no file if no errors.
static std::pair<std::unique_ptr<wallet2>, password_container> make_new(const boost::program_options::variables_map& vm);
wallet2(bool testnet = false, bool restricted = false) : m_run(true), m_callback(0), m_testnet(testnet), m_always_confirm_transfers(true), m_print_ring_members(false), m_store_tx_info(true), m_default_mixin(0), m_default_priority(0), m_refresh_type(RefreshOptimizeCoinbase), m_auto_refresh(true), m_refresh_from_block_height(0), m_confirm_missing_payment_id(true), m_restricted(restricted), is_old_file_format(false) {}
wallet2(bool testnet = false, bool restricted = false) : m_run(true), m_callback(0), m_testnet(testnet), m_always_confirm_transfers(true), m_print_ring_members(false), m_store_tx_info(true), m_default_mixin(0), m_default_priority(0), m_refresh_type(RefreshOptimizeCoinbase), m_auto_refresh(true), m_refresh_from_block_height(0), m_confirm_missing_payment_id(true), m_restricted(restricted), is_old_file_format(false), m_node_rpc_proxy(m_http_client, m_daemon_rpc_mutex) {}
struct transfer_details
{
uint64_t m_block_height;
@ -655,6 +656,7 @@ namespace tools
bool m_auto_refresh;
uint64_t m_refresh_from_block_height;
bool m_confirm_missing_payment_id;
NodeRPCProxy m_node_rpc_proxy;
};
}
BOOST_CLASS_VERSION(tools::wallet2, 16)

View File

@ -626,6 +626,18 @@ namespace tools
std::string m_request;
};
//----------------------------------------------------------------------------------------------------
struct wallet_generic_rpc_error : public wallet_rpc_error
{
explicit wallet_generic_rpc_error(std::string&& loc, const std::string& request, const std::string& status)
: wallet_rpc_error(std::move(loc), std::string("error in ") + request + " RPC: " + status, request),
m_status(status)
{
}
const std::string& status() const { return m_status; }
private:
const std::string m_status;
};
//----------------------------------------------------------------------------------------------------
struct daemon_busy : public wallet_rpc_error
{
explicit daemon_busy(std::string&& loc, const std::string& request)