From a4dc575ccba8348886ed19594a16f5a7242f8fe6 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Tue, 22 Oct 2019 15:28:08 +0000 Subject: [PATCH] rpc: add a flush_cache RPC This allows flushing internal caches (for now, the bad tx cache, which will allow debugging a stuck monerod after it has failed to verify a transaction in a block, since it would otherwise not try again, making subsequent log changes pointless) --- src/cryptonote_core/cryptonote_core.cpp | 8 +++++++ src/cryptonote_core/cryptonote_core.h | 5 +++++ src/daemon/command_parser_executor.cpp | 12 +++++++++++ src/daemon/command_parser_executor.h | 2 ++ src/daemon/command_server.cpp | 6 ++++++ src/daemon/rpc_command_executor.cpp | 28 +++++++++++++++++++++++++ src/daemon/rpc_command_executor.h | 2 ++ src/rpc/core_rpc_server.cpp | 10 ++++++++- src/rpc/core_rpc_server.h | 2 ++ src/rpc/core_rpc_server_commands_defs.h | 23 ++++++++++++++++++++ utils/python-rpc/framework/daemon.py | 12 +++++------ 11 files changed, 103 insertions(+), 7 deletions(-) diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp index 5a303a67e..acb494a49 100644 --- a/src/cryptonote_core/cryptonote_core.cpp +++ b/src/cryptonote_core/cryptonote_core.cpp @@ -1911,6 +1911,14 @@ namespace cryptonote return true; } //----------------------------------------------------------------------------------------------- + void core::flush_bad_txs_cache() + { + bad_semantics_txes_lock.lock(); + for (int idx = 0; idx < 2; ++idx) + bad_semantics_txes[idx].clear(); + bad_semantics_txes_lock.unlock(); + } + //----------------------------------------------------------------------------------------------- bool core::update_blockchain_pruning() { return m_blockchain_storage.update_blockchain_pruning(); diff --git a/src/cryptonote_core/cryptonote_core.h b/src/cryptonote_core/cryptonote_core.h index 0db6350be..f69ac3509 100644 --- a/src/cryptonote_core/cryptonote_core.h +++ b/src/cryptonote_core/cryptonote_core.h @@ -839,6 +839,11 @@ namespace cryptonote */ bool has_block_weights(uint64_t height, uint64_t nblocks) const; + /** + * @brief flushes the bad txs cache + */ + void flush_bad_txs_cache(); + private: /** diff --git a/src/daemon/command_parser_executor.cpp b/src/daemon/command_parser_executor.cpp index 8e78f3bd1..b827221f6 100644 --- a/src/daemon/command_parser_executor.cpp +++ b/src/daemon/command_parser_executor.cpp @@ -844,4 +844,16 @@ bool t_command_parser_executor::set_bootstrap_daemon(const std::vector 2 ? args[2] : std::string()); } +bool t_command_parser_executor::flush_cache(const std::vector& args) +{ + if (args.empty()) + goto show_list; + if (args[0] == "bad-txs") + return m_executor.flush_cache(true); + +show_list: + std::cout << "Cache type needed: bad-txs" << std::endl; + return true; +} + } // namespace daemonize diff --git a/src/daemon/command_parser_executor.h b/src/daemon/command_parser_executor.h index 5c94c540e..8b85dcf69 100644 --- a/src/daemon/command_parser_executor.h +++ b/src/daemon/command_parser_executor.h @@ -154,6 +154,8 @@ public: bool print_net_stats(const std::vector& args); bool set_bootstrap_daemon(const std::vector& args); + + bool flush_cache(const std::vector& args); }; } // namespace daemonize diff --git a/src/daemon/command_server.cpp b/src/daemon/command_server.cpp index b00e253c7..8ec690631 100644 --- a/src/daemon/command_server.cpp +++ b/src/daemon/command_server.cpp @@ -322,6 +322,12 @@ t_command_server::t_command_server( , "URL of a 'bootstrap' remote daemon that the connected wallets can use while this daemon is still not fully synced.\n" "Use 'auto' to enable automatic public nodes discovering and bootstrap daemon switching" ); + m_command_lookup.set_handler( + "flush_cache" + , std::bind(&t_command_parser_executor::flush_cache, &m_parser, p::_1) + , "flush_cache bad-txs" + , "Flush the specified cache(s)." + ); } bool t_command_server::process_command_str(const std::string& cmd) diff --git a/src/daemon/rpc_command_executor.cpp b/src/daemon/rpc_command_executor.cpp index 552862d2f..ed614a89b 100644 --- a/src/daemon/rpc_command_executor.cpp +++ b/src/daemon/rpc_command_executor.cpp @@ -2373,6 +2373,34 @@ bool t_rpc_command_executor::set_bootstrap_daemon( return true; } +bool t_rpc_command_executor::flush_cache(bool bad_txs) +{ + cryptonote::COMMAND_RPC_FLUSH_CACHE::request req; + cryptonote::COMMAND_RPC_FLUSH_CACHE::response res; + std::string fail_message = "Unsuccessful"; + epee::json_rpc::error error_resp; + + req.bad_txs = bad_txs; + + if (m_is_rpc) + { + if (!m_rpc_client->json_rpc_request(req, res, "flush_cache", fail_message.c_str())) + { + return true; + } + } + else + { + if (!m_rpc_server->on_flush_cache(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) + { + tools::fail_msg_writer() << make_error(fail_message, res.status); + return true; + } + } + + return true; +} + bool t_rpc_command_executor::rpc_payments() { cryptonote::COMMAND_RPC_ACCESS_DATA::request req; diff --git a/src/daemon/rpc_command_executor.h b/src/daemon/rpc_command_executor.h index 783b47373..e8b12cb9b 100644 --- a/src/daemon/rpc_command_executor.h +++ b/src/daemon/rpc_command_executor.h @@ -169,6 +169,8 @@ public: const std::string &password); bool rpc_payments(); + + bool flush_cache(bool bad_txs); }; } // namespace daemonize diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp index 8882df5d7..5a6a395bc 100644 --- a/src/rpc/core_rpc_server.cpp +++ b/src/rpc/core_rpc_server.cpp @@ -2995,10 +2995,18 @@ namespace cryptonote return true; } //------------------------------------------------------------------------------------------------------------------------------ + bool core_rpc_server::on_flush_cache(const COMMAND_RPC_FLUSH_CACHE::request& req, COMMAND_RPC_FLUSH_CACHE::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx) + { + RPC_TRACKER(flush_cache); + if (req.bad_txs) + m_core.flush_bad_txs_cache(); + res.status = CORE_RPC_STATUS_OK; + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ bool core_rpc_server::on_rpc_access_submit_nonce(const COMMAND_RPC_ACCESS_SUBMIT_NONCE::request& req, COMMAND_RPC_ACCESS_SUBMIT_NONCE::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx) { RPC_TRACKER(rpc_access_submit_nonce); - bool r; if (use_bootstrap_daemon_if_necessary(invoke_http_mode::JON, "rpc_access_submit_nonce", req, res, r)) return r; diff --git a/src/rpc/core_rpc_server.h b/src/rpc/core_rpc_server.h index 6655d621e..23c611470 100644 --- a/src/rpc/core_rpc_server.h +++ b/src/rpc/core_rpc_server.h @@ -174,6 +174,7 @@ namespace cryptonote MAP_JON_RPC_WE("get_txpool_backlog", on_get_txpool_backlog, COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG) MAP_JON_RPC_WE("get_output_distribution", on_get_output_distribution, COMMAND_RPC_GET_OUTPUT_DISTRIBUTION) MAP_JON_RPC_WE_IF("prune_blockchain", on_prune_blockchain, COMMAND_RPC_PRUNE_BLOCKCHAIN, !m_restricted) + MAP_JON_RPC_WE_IF("flush_cache", on_flush_cache, COMMAND_RPC_FLUSH_CACHE, !m_restricted) MAP_JON_RPC_WE("rpc_access_info", on_rpc_access_info, COMMAND_RPC_ACCESS_INFO) MAP_JON_RPC_WE("rpc_access_submit_nonce",on_rpc_access_submit_nonce, COMMAND_RPC_ACCESS_SUBMIT_NONCE) MAP_JON_RPC_WE("rpc_access_pay", on_rpc_access_pay, COMMAND_RPC_ACCESS_PAY) @@ -247,6 +248,7 @@ namespace cryptonote bool on_get_txpool_backlog(const COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG::request& req, COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL); bool on_get_output_distribution(const COMMAND_RPC_GET_OUTPUT_DISTRIBUTION::request& req, COMMAND_RPC_GET_OUTPUT_DISTRIBUTION::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL); bool on_prune_blockchain(const COMMAND_RPC_PRUNE_BLOCKCHAIN::request& req, COMMAND_RPC_PRUNE_BLOCKCHAIN::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL); + bool on_flush_cache(const COMMAND_RPC_FLUSH_CACHE::request& req, COMMAND_RPC_FLUSH_CACHE::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL); bool on_rpc_access_info(const COMMAND_RPC_ACCESS_INFO::request& req, COMMAND_RPC_ACCESS_INFO::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL); bool on_rpc_access_submit_nonce(const COMMAND_RPC_ACCESS_SUBMIT_NONCE::request& req, COMMAND_RPC_ACCESS_SUBMIT_NONCE::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL); bool on_rpc_access_pay(const COMMAND_RPC_ACCESS_PAY::request& req, COMMAND_RPC_ACCESS_PAY::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL); diff --git a/src/rpc/core_rpc_server_commands_defs.h b/src/rpc/core_rpc_server_commands_defs.h index 89c88a835..855ea854c 100644 --- a/src/rpc/core_rpc_server_commands_defs.h +++ b/src/rpc/core_rpc_server_commands_defs.h @@ -2554,4 +2554,27 @@ namespace cryptonote typedef epee::misc_utils::struct_init response; }; + struct COMMAND_RPC_FLUSH_CACHE + { + struct request_t + { + bool bad_txs; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE_OPT(bad_txs, false) + END_KV_SERIALIZE_MAP() + }; + typedef epee::misc_utils::struct_init request; + + struct response_t + { + std::string status; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(status) + END_KV_SERIALIZE_MAP() + }; + typedef epee::misc_utils::struct_init response; + }; + } diff --git a/utils/python-rpc/framework/daemon.py b/utils/python-rpc/framework/daemon.py index f811535b1..9f7805dc8 100644 --- a/utils/python-rpc/framework/daemon.py +++ b/utils/python-rpc/framework/daemon.py @@ -516,16 +516,16 @@ class Daemon(object): } return self.rpc.send_json_rpc_request(prune_blockchain) - def get_block_rate(self, seconds = [3600]): - get_block_rate = { - 'method': 'get_block_rate', + def flush_cache(self, bad_txs = False): + flush_cache = { + 'method': 'flush_cache', 'params': { - 'seconds': seconds, + 'bad_txs': bad_txs, }, - 'jsonrpc': '2.0', + 'jsonrpc': '2.0', 'id': '0' } - return self.rpc.send_json_rpc_request(get_block_rate) + return self.rpc.send_json_rpc_request(flush_cache) def rpc_access_info(self, client): rpc_access_info = {