From 68c3d97fd813ceadf41d765cda1bfe970322b633 Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Tue, 20 Jun 2017 09:00:55 +0800 Subject: [PATCH] checking if COMMAND_RPC_GET_ALT_BLOCKS_HASHES exists added --- src/rpccalls.cpp | 54 ---------------------- src/rpccalls.h | 116 +++++++++++++++++++++++++++++++++++++++++++++-- src/tools.h | 2 +- 3 files changed, 114 insertions(+), 58 deletions(-) diff --git a/src/rpccalls.cpp b/src/rpccalls.cpp index 8f522f8..b366bb3 100644 --- a/src/rpccalls.cpp +++ b/src/rpccalls.cpp @@ -208,60 +208,6 @@ rpccalls::get_network_info(COMMAND_RPC_GET_INFO::response& response) return true; } -bool -rpccalls::get_alt_blocks(vector& alt_blocks_hashes) -{ - bool r {false}; - - COMMAND_RPC_GET_ALT_BLOCKS_HASHES::request req; - COMMAND_RPC_GET_ALT_BLOCKS_HASHES::response resp; - - { - std::lock_guard guard(m_daemon_rpc_mutex); - - if (!connect_to_monero_deamon()) - { - cerr << "get_mempool: not connected to deamon" << endl; - return false; - } - - r = epee::net_utils::invoke_http_json("/get_alt_blocks_hashes", - req, resp, - m_http_client); - } - - string err; - - if (r) - { - if (resp.status == CORE_RPC_STATUS_BUSY) - { - err = "daemon is busy. Please try again later."; - } - else if (resp.status != CORE_RPC_STATUS_OK) - { - err = "daemon rpc failed. Please try again later."; - } - - if (!err.empty()) - { - cerr << "Error connecting to Monero deamon due to " - << err << endl; - return false; - } - } - else - { - cerr << "Error connecting to Monero deamon at " - << deamon_url << endl; - return false; - } - - alt_blocks_hashes = resp.blks_hashes; - - return true; -} - bool rpccalls::get_dynamic_per_kb_fee_estimate( diff --git a/src/rpccalls.h b/src/rpccalls.h index 560d9c4..596dc38 100644 --- a/src/rpccalls.h +++ b/src/rpccalls.h @@ -10,6 +10,38 @@ #include + +namespace +{ + + // can be used to check if given class/struct exist + // from: https://stackoverflow.com/a/10722840/248823 + template + struct has_destructor + { + // has destructor + template + static std::true_type test(decltype(declval().~A()) *) + { + return std::true_type(); + } + + // no constructor + template + static std::false_type test(...) + { + return std::false_type(); + } + + /* This will be either `std::true_type` or `std::false_type` */ + typedef decltype(test(0)) type; + + static const bool value = type::value; + }; + +} + + namespace xmreg { @@ -17,6 +49,12 @@ using namespace cryptonote; using namespace crypto; using namespace std; +// declare it. monero should provide definition for this, +// but we need to have it declared as we are going to +// check if its definition exist or not. depending on this +// we decide what gets to be defined as +// get_alt_blocks(vector& alt_blocks_hashes); +struct COMMAND_RPC_GET_ALT_BLOCKS_HASHES; class rpccalls { @@ -52,15 +90,87 @@ public: bool get_network_info(COMMAND_RPC_GET_INFO::response& info); - bool - get_alt_blocks(vector& alt_blocks_hashes); - bool get_dynamic_per_kb_fee_estimate( uint64_t grace_blocks, uint64_t& fee, string& error_msg); + + /** + * This must be in the header for now, as it will be tempalte function + * + * @param alt_blocks_hashes + * @return bool + */ + template + typename enable_if::value, bool>::type + get_alt_blocks(vector& alt_blocks_hashes) + { + // definition of COMMAND_RPC_GET_ALT_BLOCKS_HASHES exist + // so perform rpc call to get this information + + bool r {false}; + + typename T::request req; + typename T::response resp; + + { + std::lock_guard guard(m_daemon_rpc_mutex); + + if (!connect_to_monero_deamon()) + { + cerr << "get_mempool: not connected to deamon" << endl; + return false; + } + + r = epee::net_utils::invoke_http_json("/get_alt_blocks_hashes", + req, resp, + m_http_client); + } + + string err; + + if (r) + { + if (resp.status == CORE_RPC_STATUS_BUSY) + { + err = "daemon is busy. Please try again later."; + } + else if (resp.status != CORE_RPC_STATUS_OK) + { + err = "daemon rpc failed. Please try again later."; + } + + if (!err.empty()) + { + cerr << "Error connecting to Monero deamon due to " + << err << endl; + return false; + } + } + else + { + cerr << "Error connecting to Monero deamon at " + << deamon_url << endl; + return false; + } + + alt_blocks_hashes = resp.blks_hashes; + + return true; + } + + template + typename enable_if::value, bool>::type + get_alt_blocks(vector& alt_blocks_hashes) + { + cerr << "COMMAND_RPC_GET_ALT_BLOCKS_HASHES does not exist!" << endl; + // definition of COMMAND_RPC_GET_ALT_BLOCKS_HASHES does NOT exist + // so dont do anything + return false; + } + }; diff --git a/src/tools.h b/src/tools.h index daed30b..9b0c354 100644 --- a/src/tools.h +++ b/src/tools.h @@ -30,7 +30,7 @@ #include #include #include - +#include /**