From dca5aae9cb06e19dccc78dac08aac21dc3fdc32d Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Thu, 8 Aug 2019 17:20:47 +0200 Subject: [PATCH] added another command for getting difficulty numbers --- src/currency_core/blockchain_storage.cpp | 40 ++++++++++++++++++++++++ src/currency_core/blockchain_storage.h | 3 +- src/daemon/daemon_commands_handler.h | 19 +++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/currency_core/blockchain_storage.cpp b/src/currency_core/blockchain_storage.cpp index 53b3e8c2..d37e4f46 100644 --- a/src/currency_core/blockchain_storage.cpp +++ b/src/currency_core/blockchain_storage.cpp @@ -2665,6 +2665,46 @@ void blockchain_storage::print_db_cache_perfeormance_data() const ); } //------------------------------------------------------------------ +void blockchain_storage::get_last_n_x_blocks(uint64_t n, bool pos_blocks, std::list>& blocks) const +{ + uint64_t count = 0; + bool looking_for_a_pos = true; + for (uint64_t i = m_db_blocks.size() - 1; i != 0; --i) + { + auto block_ptr = m_db_blocks[i]; + if (is_pos_block(block_ptr->bl) == pos_blocks) + { + blocks.push_back(block_ptr); + ++count; + if (count >= n) + break; + } + } +} +//------------------------------------------------------------------ +void blockchain_storage::print_last_n_difficulty_numbers(uint64_t n) const +{ + + std::stringstream ss; + std::list> pos_blocks; + std::list> pow_blocks; + + get_last_n_x_blocks(n, true, pos_blocks); + get_last_n_x_blocks(n, false, pow_blocks); + ss << "PoS blocks difficulty:" << ENDL; + for (auto& bl_ptr : pos_blocks) + { + ss << bl_ptr->cumulative_diff_precise << ENDL; + } + + ss << "PoW blocks difficulty:" << ENDL; + for (auto& bl_ptr : pow_blocks) + { + ss << bl_ptr->cumulative_diff_precise << ENDL; + } + +} +//------------------------------------------------------------------ void blockchain_storage::print_blockchain_outs_stat() const { LOG_ERROR("NOT IMPLEMENTED YET"); diff --git a/src/currency_core/blockchain_storage.h b/src/currency_core/blockchain_storage.h index 54e9367e..4d6f7d4a 100644 --- a/src/currency_core/blockchain_storage.h +++ b/src/currency_core/blockchain_storage.h @@ -430,6 +430,7 @@ namespace currency void print_blockchain_outs(const std::string& file) const; void print_blockchain_outs_stat() const; void print_db_cache_perfeormance_data() const; + void print_last_n_difficulty_numbers(uint64_t n) const; bool calc_tx_cummulative_blob(const block& bl)const; bool get_outs_index_stat(outs_index_stat& outs_stat)const; bool print_lookup_key_image(const crypto::key_image& ki) const; @@ -561,7 +562,7 @@ namespace currency bool validate_alt_block_txs(const block& b, const crypto::hash& id, std::set& collected_keyimages, alt_block_extended_info& abei, const alt_chain_type& alt_chain, uint64_t split_height, uint64_t& ki_lookup_time_total) const; bool update_alt_out_indexes_for_tx_in_block(const transaction& tx, alt_block_extended_info& abei)const; bool get_transaction_from_pool_or_db(const crypto::hash& tx_id, std::shared_ptr& tx_ptr, uint64_t min_allowed_block_height = 0) const; - + void get_last_n_x_blocks(uint64_t n, bool pos_blocks, std::list>& blocks) const; bool prevalidate_miner_transaction(const block& b, uint64_t height, bool pos)const; bool rollback_blockchain_switching(std::list& original_chain, size_t rollback_height); bool add_transaction_from_block(const transaction& tx, const crypto::hash& tx_id, const crypto::hash& bl_id, uint64_t bl_height, uint64_t timestamp); diff --git a/src/daemon/daemon_commands_handler.h b/src/daemon/daemon_commands_handler.h index ac057703..391f16f4 100644 --- a/src/daemon/daemon_commands_handler.h +++ b/src/daemon/daemon_commands_handler.h @@ -68,6 +68,7 @@ public: m_cmd_binder.set_handler("print_block_from_hex_blob", boost::bind(&daemon_commands_handler::print_block_from_hex_blob, this, _1), "Unserialize block from hex binary data to json-like representation"); m_cmd_binder.set_handler("print_tx_from_hex_blob", boost::bind(&daemon_commands_handler::print_tx_from_hex_blob, this, _1), "Unserialize transaction from hex binary data to json-like representation"); m_cmd_binder.set_handler("print_tx_outputs_usage", boost::bind(&daemon_commands_handler::print_tx_outputs_usage, this, _1), "Analyse if tx outputs for involved in subsequent transactions"); + m_cmd_binder.set_handler("print_difficulties_of_last_n_blocks", boost::bind(&daemon_commands_handler::print_difficulties_of_last_n_blocks, this, _1), "Print difficulties of last n blocks"); } @@ -696,8 +697,26 @@ private: m_srv.get_payload_object().get_core().get_blockchain_storage().print_tx_outputs_lookup(tx_hash); return true; } + //-------------------------------------------------------------------------------- + bool print_difficulties_of_last_n_blocks(const std::vector& args) + { + if (args.empty()) + { + std::cout << "expected: n - number of blocks to read" << std::endl; + return true; + } + const std::string& amount = args.front(); + uint64_t n = 0; + if (!epee::string_tools::get_xtype_from_string(n, amount)) + { + std::cout << "unable to convert to number '" << amount << "'" << std::endl; + return true; + } + m_srv.get_payload_object().get_core().get_blockchain_storage().print_last_n_difficulty_numbers(n); + return true; + } //-------------------------------------------------------------------------------- bool print_pool(const std::vector& args) {