From 0605406714b626575d8ea3945504fa5e123399fd Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Thu, 2 May 2019 22:23:00 +0000 Subject: [PATCH 1/3] daemon: sort alt chains by height --- src/cryptonote_core/blockchain.cpp | 4 ++-- src/cryptonote_core/blockchain.h | 4 ++-- src/daemon/rpc_command_executor.cpp | 4 +++- src/rpc/core_rpc_server.cpp | 2 +- src/rpc/core_rpc_server_commands_defs.h | 2 +- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 39c9f8695..d79a8fab6 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -4798,9 +4798,9 @@ std::map> Blockchain:: get_ou return m_db->get_output_histogram(amounts, unlocked, recent_cutoff, min_count); } -std::list>> Blockchain::get_alternative_chains() const +std::vector>> Blockchain::get_alternative_chains() const { - std::list>> chains; + std::vector>> chains; for (const auto &i: m_alternative_chains) { diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h index 6200ec87e..e45023368 100644 --- a/src/cryptonote_core/blockchain.h +++ b/src/cryptonote_core/blockchain.h @@ -961,9 +961,9 @@ namespace cryptonote /** * @brief returns a set of known alternate chains * - * @return a list of chains + * @return a vector of chains */ - std::list>> get_alternative_chains() const; + std::vector>> get_alternative_chains() const; void add_txpool_tx(const crypto::hash &txid, const cryptonote::blobdata &blob, const txpool_tx_meta_t &meta); void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t &meta); diff --git a/src/daemon/rpc_command_executor.cpp b/src/daemon/rpc_command_executor.cpp index 151baa33f..054b956ab 100644 --- a/src/daemon/rpc_command_executor.cpp +++ b/src/daemon/rpc_command_executor.cpp @@ -1861,7 +1861,9 @@ bool t_rpc_command_executor::alt_chain_info(const std::string &tip) if (tip.empty()) { tools::msg_writer() << boost::lexical_cast(res.chains.size()) << " alternate chains found:"; - for (const auto &chain: res.chains) + auto chains = res.chains; + std::sort(chains.begin(), chains.end(), [](const cryptonote::COMMAND_RPC_GET_ALTERNATE_CHAINS::chain_info &info0, cryptonote::COMMAND_RPC_GET_ALTERNATE_CHAINS::chain_info &info1){ return info0.height < info1.height; }); + for (const auto &chain: chains) { uint64_t start_height = (chain.height - chain.length + 1); tools::msg_writer() << chain.length << " blocks long, from height " << start_height << " (" << (ires.height - start_height - 1) diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp index bbcbc2fcd..c6c6553f3 100644 --- a/src/rpc/core_rpc_server.cpp +++ b/src/rpc/core_rpc_server.cpp @@ -1991,7 +1991,7 @@ namespace cryptonote PERF_TIMER(on_get_alternate_chains); try { - std::list>> chains = m_core.get_blockchain_storage().get_alternative_chains(); + std::vector>> chains = m_core.get_blockchain_storage().get_alternative_chains(); for (const auto &i: chains) { difficulty_type wdiff = i.first.cumulative_difficulty; diff --git a/src/rpc/core_rpc_server_commands_defs.h b/src/rpc/core_rpc_server_commands_defs.h index cfe4bbf23..5125d2e0e 100644 --- a/src/rpc/core_rpc_server_commands_defs.h +++ b/src/rpc/core_rpc_server_commands_defs.h @@ -2070,7 +2070,7 @@ namespace cryptonote struct response_t { std::string status; - std::list chains; + std::vector chains; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(status) From 4228ee0b9ee9096cb2a49fd09d88a5f5ae96afc1 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Wed, 8 May 2019 18:56:47 +0000 Subject: [PATCH 2/3] daemon: add optional arguments to alt_chain_info >N limits display to alt chains with more than N blocks -N limits display to alt chains younger than N blocks --- src/daemon/command_parser_executor.cpp | 31 ++++++++++++++++++++++++-- src/daemon/rpc_command_executor.cpp | 20 +++++++++++++---- src/daemon/rpc_command_executor.h | 2 +- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/daemon/command_parser_executor.cpp b/src/daemon/command_parser_executor.cpp index 17b945c9a..a8a3db45a 100644 --- a/src/daemon/command_parser_executor.cpp +++ b/src/daemon/command_parser_executor.cpp @@ -672,11 +672,38 @@ bool t_command_parser_executor::alt_chain_info(const std::vector& a { if(args.size() > 1) { - std::cout << "usage: alt_chain_info [block_hash]" << std::endl; + std::cout << "usage: alt_chain_info [block_hash|>N|-N]" << std::endl; return false; } - return m_executor.alt_chain_info(args.size() == 1 ? args[0] : ""); + std::string tip; + size_t above = 0; + uint64_t last_blocks = 0; + if (args.size() == 1) + { + if (args[0].size() > 0 && args[0][0] == '>') + { + if (!epee::string_tools::get_xtype_from_string(above, args[0].c_str() + 1)) + { + std::cout << "invalid above parameter" << std::endl; + return false; + } + } + else if (args[0].size() > 0 && args[0][0] == '-') + { + if (!epee::string_tools::get_xtype_from_string(last_blocks, args[0].c_str() + 1)) + { + std::cout << "invalid last_blocks parameter" << std::endl; + return false; + } + } + else + { + tip = args[0]; + } + } + + return m_executor.alt_chain_info(tip, above, last_blocks); } bool t_command_parser_executor::print_blockchain_dynamic_stats(const std::vector& args) diff --git a/src/daemon/rpc_command_executor.cpp b/src/daemon/rpc_command_executor.cpp index 054b956ab..51fbd8628 100644 --- a/src/daemon/rpc_command_executor.cpp +++ b/src/daemon/rpc_command_executor.cpp @@ -1823,7 +1823,7 @@ bool t_rpc_command_executor::print_coinbase_tx_sum(uint64_t height, uint64_t cou return true; } -bool t_rpc_command_executor::alt_chain_info(const std::string &tip) +bool t_rpc_command_executor::alt_chain_info(const std::string &tip, size_t above, uint64_t last_blocks) { cryptonote::COMMAND_RPC_GET_INFO::request ireq; cryptonote::COMMAND_RPC_GET_INFO::response ires; @@ -1860,12 +1860,24 @@ bool t_rpc_command_executor::alt_chain_info(const std::string &tip) if (tip.empty()) { - tools::msg_writer() << boost::lexical_cast(res.chains.size()) << " alternate chains found:"; auto chains = res.chains; std::sort(chains.begin(), chains.end(), [](const cryptonote::COMMAND_RPC_GET_ALTERNATE_CHAINS::chain_info &info0, cryptonote::COMMAND_RPC_GET_ALTERNATE_CHAINS::chain_info &info1){ return info0.height < info1.height; }); - for (const auto &chain: chains) + std::vector display; + for (size_t i = 0; i < chains.size(); ++i) { - uint64_t start_height = (chain.height - chain.length + 1); + const auto &chain = chains[i]; + if (chain.length <= above) + continue; + const uint64_t start_height = (chain.height - chain.length + 1); + if (last_blocks > 0 && ires.height - 1 - start_height >= last_blocks) + continue; + display.push_back(i); + } + tools::msg_writer() << boost::lexical_cast(display.size()) << " alternate chains found:"; + for (const size_t idx: display) + { + const auto &chain = chains[idx]; + const uint64_t start_height = (chain.height - chain.length + 1); tools::msg_writer() << chain.length << " blocks long, from height " << start_height << " (" << (ires.height - start_height - 1) << " deep), diff " << chain.difficulty << ": " << chain.block_hash; } diff --git a/src/daemon/rpc_command_executor.h b/src/daemon/rpc_command_executor.h index 3c2686b3f..96e742e1b 100644 --- a/src/daemon/rpc_command_executor.h +++ b/src/daemon/rpc_command_executor.h @@ -145,7 +145,7 @@ public: bool print_coinbase_tx_sum(uint64_t height, uint64_t count); - bool alt_chain_info(const std::string &tip); + bool alt_chain_info(const std::string &tip, size_t above, uint64_t last_blocks); bool print_blockchain_dynamic_stats(uint64_t nblocks); From 880ebfdeeaceab5e1ca40b748516987e9c6cecb7 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Thu, 9 May 2019 10:13:12 +0000 Subject: [PATCH 3/3] daemon: add more chain specific info in alt_chain_info --- src/common/util.cpp | 17 ++++++ src/common/util.h | 2 + src/daemon/rpc_command_executor.cpp | 44 +++++++++++++++ src/rpc/core_rpc_server.cpp | 75 +++++++++++++++---------- src/rpc/core_rpc_server_commands_defs.h | 6 +- utils/python-rpc/framework/daemon.py | 3 +- 6 files changed, 116 insertions(+), 31 deletions(-) diff --git a/src/common/util.cpp b/src/common/util.cpp index db5aa3052..0fa9e8dc1 100644 --- a/src/common/util.cpp +++ b/src/common/util.cpp @@ -1068,6 +1068,23 @@ std::string get_nix_version_display_string() return std::string(buffer); } + std::string get_human_readable_timespan(uint64_t seconds) + { + if (seconds < 60) + return std::to_string(seconds) + " seconds"; + if (seconds < 3600) + return std::to_string((uint64_t)(seconds / 60)) + " minutes"; + if (seconds < 3600 * 24) + return std::to_string((uint64_t)(seconds / 3600)) + " hours"; + if (seconds < 3600 * 24 * 30.5) + return std::to_string((uint64_t)(seconds / (3600 * 24))) + " days"; + if (seconds < 3600 * 24 * 365.25) + return std::to_string((uint64_t)(seconds / (3600 * 24 * 30.5))) + " months"; + if (seconds < 3600 * 24 * 365.25 * 100) + return std::to_string((uint64_t)(seconds / (3600 * 24 * 30.5 * 365.25))) + " years"; + return "a long time"; + } + std::string get_human_readable_bytes(uint64_t bytes) { // Use 1024 for "kilo", 1024*1024 for "mega" and so on instead of the more modern and standard-conforming diff --git a/src/common/util.h b/src/common/util.h index f6d5c9b1f..b0f734eff 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -245,5 +245,7 @@ namespace tools std::string get_human_readable_timestamp(uint64_t ts); + std::string get_human_readable_timespan(uint64_t seconds); + std::string get_human_readable_bytes(uint64_t bytes); } diff --git a/src/daemon/rpc_command_executor.cpp b/src/daemon/rpc_command_executor.cpp index 51fbd8628..cd1c22806 100644 --- a/src/daemon/rpc_command_executor.cpp +++ b/src/daemon/rpc_command_executor.cpp @@ -1884,6 +1884,7 @@ bool t_rpc_command_executor::alt_chain_info(const std::string &tip, size_t above } else { + const uint64_t now = time(NULL); const auto i = std::find_if(res.chains.begin(), res.chains.end(), [&tip](cryptonote::COMMAND_RPC_GET_ALTERNATE_CHAINS::chain_info &info){ return info.block_hash == tip; }); if (i != res.chains.end()) { @@ -1895,6 +1896,49 @@ bool t_rpc_command_executor::alt_chain_info(const std::string &tip, size_t above for (const std::string &block_id: chain.block_hashes) tools::msg_writer() << " " << block_id; tools::msg_writer() << "Chain parent on main chain: " << chain.main_chain_parent_block; + cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::request bhreq; + cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::response bhres; + bhreq.hashes = chain.block_hashes; + bhreq.hashes.push_back(chain.main_chain_parent_block); + bhreq.fill_pow_hash = false; + if (m_is_rpc) + { + if (!m_rpc_client->json_rpc_request(bhreq, bhres, "getblockheaderbyhash", fail_message.c_str())) + { + return true; + } + } + else + { + if (!m_rpc_server->on_get_block_header_by_hash(bhreq, bhres, error_resp)) + { + tools::fail_msg_writer() << make_error(fail_message, res.status); + return true; + } + } + if (bhres.block_headers.size() != chain.length + 1) + { + tools::fail_msg_writer() << "Failed to get block header info for alt chain"; + return true; + } + uint64_t t0 = bhres.block_headers.front().timestamp, t1 = t0; + for (const cryptonote::block_header_response &block_header: bhres.block_headers) + { + t0 = std::min(t0, block_header.timestamp); + t1 = std::max(t1, block_header.timestamp); + } + const uint64_t dt = t1 - t0; + const uint64_t age = std::max(dt, t0 < now ? now - t0 : 0); + tools::msg_writer() << "Age: " << tools::get_human_readable_timespan(age); + if (chain.length > 1) + { + tools::msg_writer() << "Time span: " << tools::get_human_readable_timespan(dt); + cryptonote::difficulty_type start_difficulty = bhres.block_headers.back().difficulty; + if (start_difficulty > 0) + tools::msg_writer() << "Approximated " << 100.f * DIFFICULTY_TARGET_V2 * chain.length / dt << "% of network hash rate"; + else + tools::fail_msg_writer() << "Bad cmumulative difficulty reported by dameon"; + } } else tools::fail_msg_writer() << "Block hash " << tip << " is not the tip of any known alternate chain"; diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp index c6c6553f3..55f2ccdaf 100644 --- a/src/rpc/core_rpc_server.cpp +++ b/src/rpc/core_rpc_server.cpp @@ -1593,38 +1593,55 @@ namespace cryptonote if (use_bootstrap_daemon_if_necessary(invoke_http_mode::JON_RPC, "getblockheaderbyhash", req, res, r)) return r; - crypto::hash block_hash; - bool hash_parsed = parse_hash256(req.hash, block_hash); - if(!hash_parsed) - { - error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PARAM; - error_resp.message = "Failed to parse hex representation of block hash. Hex = " + req.hash + '.'; - return false; - } - block blk; - bool orphan = false; - bool have_block = m_core.get_block_by_hash(block_hash, blk, &orphan); - if (!have_block) - { - error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR; - error_resp.message = "Internal error: can't get block by hash. Hash = " + req.hash + '.'; - return false; - } - if (blk.miner_tx.vin.size() != 1 || blk.miner_tx.vin.front().type() != typeid(txin_gen)) - { - error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR; - error_resp.message = "Internal error: coinbase transaction in the block has the wrong type"; - return false; - } - uint64_t block_height = boost::get(blk.miner_tx.vin.front()).height; + auto get = [this](const std::string &hash, bool fill_pow_hash, block_header_response &block_header, bool restricted, epee::json_rpc::error& error_resp) -> bool { + crypto::hash block_hash; + bool hash_parsed = parse_hash256(hash, block_hash); + if(!hash_parsed) + { + error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PARAM; + error_resp.message = "Failed to parse hex representation of block hash. Hex = " + hash + '.'; + return false; + } + block blk; + bool orphan = false; + bool have_block = m_core.get_block_by_hash(block_hash, blk, &orphan); + if (!have_block) + { + error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR; + error_resp.message = "Internal error: can't get block by hash. Hash = " + hash + '.'; + return false; + } + if (blk.miner_tx.vin.size() != 1 || blk.miner_tx.vin.front().type() != typeid(txin_gen)) + { + error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR; + error_resp.message = "Internal error: coinbase transaction in the block has the wrong type"; + return false; + } + uint64_t block_height = boost::get(blk.miner_tx.vin.front()).height; + bool response_filled = fill_block_header_response(blk, orphan, block_height, block_hash, block_header, fill_pow_hash && !restricted); + if (!response_filled) + { + error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR; + error_resp.message = "Internal error: can't produce valid response."; + return false; + } + return true; + }; + const bool restricted = m_restricted && ctx; - bool response_filled = fill_block_header_response(blk, orphan, block_height, block_hash, res.block_header, req.fill_pow_hash && !restricted); - if (!response_filled) + if (!req.hash.empty()) { - error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR; - error_resp.message = "Internal error: can't produce valid response."; - return false; + if (!get(req.hash, req.fill_pow_hash, res.block_header, restricted, error_resp)) + return false; } + res.block_headers.reserve(req.hashes.size()); + for (const std::string &hash: req.hashes) + { + res.block_headers.push_back({}); + if (!get(hash, req.fill_pow_hash, res.block_headers.back(), restricted, error_resp)) + return false; + } + res.status = CORE_RPC_STATUS_OK; return true; } diff --git a/src/rpc/core_rpc_server_commands_defs.h b/src/rpc/core_rpc_server_commands_defs.h index 5125d2e0e..ffd49903a 100644 --- a/src/rpc/core_rpc_server_commands_defs.h +++ b/src/rpc/core_rpc_server_commands_defs.h @@ -84,7 +84,7 @@ namespace cryptonote // advance which version they will stop working with // Don't go over 32767 for any of these #define CORE_RPC_VERSION_MAJOR 2 -#define CORE_RPC_VERSION_MINOR 6 +#define CORE_RPC_VERSION_MINOR 7 #define MAKE_CORE_RPC_VERSION(major,minor) (((major)<<16)|(minor)) #define CORE_RPC_VERSION MAKE_CORE_RPC_VERSION(CORE_RPC_VERSION_MAJOR, CORE_RPC_VERSION_MINOR) @@ -1091,10 +1091,12 @@ namespace cryptonote struct request_t { std::string hash; + std::vector hashes; bool fill_pow_hash; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(hash) + KV_SERIALIZE(hashes) KV_SERIALIZE_OPT(fill_pow_hash, false); END_KV_SERIALIZE_MAP() }; @@ -1104,10 +1106,12 @@ namespace cryptonote { std::string status; block_header_response block_header; + std::vector block_headers; bool untrusted; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(block_header) + KV_SERIALIZE(block_headers) KV_SERIALIZE(status) KV_SERIALIZE(untrusted) END_KV_SERIALIZE_MAP() diff --git a/utils/python-rpc/framework/daemon.py b/utils/python-rpc/framework/daemon.py index 04fc5b5cf..bbdc5a4fa 100644 --- a/utils/python-rpc/framework/daemon.py +++ b/utils/python-rpc/framework/daemon.py @@ -88,11 +88,12 @@ class Daemon(object): } return self.rpc.send_json_rpc_request(getlastblockheader) - def getblockheaderbyhash(self, hash): + def getblockheaderbyhash(self, hash = "", hashes = []): getblockheaderbyhash = { 'method': 'getblockheaderbyhash', 'params': { 'hash': hash, + 'hashes': hashes, }, 'jsonrpc': '2.0', 'id': '0'