From d6bcf94a3d2fdd7134d875f8cb9ad8e44d1ca01e Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Tue, 16 May 2017 13:29:24 +0800 Subject: [PATCH] testing monero emission thread --- main.cpp | 13 ++ src/CMakeLists.txt | 4 +- src/CurrentBlockchainStatus.cpp | 256 ++++++++++++++++++++++++++++++++ src/CurrentBlockchainStatus.h | 73 +++++++++ src/page.h | 29 +++- src/templates/index2.html | 11 +- 6 files changed, 378 insertions(+), 8 deletions(-) create mode 100644 src/CurrentBlockchainStatus.cpp create mode 100644 src/CurrentBlockchainStatus.h diff --git a/main.cpp b/main.cpp index 8c1827d..6a1d5a7 100644 --- a/main.cpp +++ b/main.cpp @@ -128,6 +128,19 @@ int main(int ac, const char* av[]) { cout << blockchain_path << endl; + if (!xmreg::CurrentBlockchainStatus::init_monero_blockchain()) + { + cerr << "Error accessing blockchain." << endl; + return EXIT_FAILURE; + } + + // launch the status monitoring thread so that it keeps track of blockchain + // info, e.g., current height. Information from this thread is used + // by tx searching threads that are launched for each user independently, + // when they log back or create new account. + xmreg::CurrentBlockchainStatus::start_monitor_blockchain_thread(); + + // create instance of our MicroCore // and make pointer to the Blockchain xmreg::MicroCore mcore; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a15dc01..d4f476c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,7 +6,7 @@ set(SOURCE_HEADERS MicroCore.h tools.h monero_headers.h - ) + CurrentBlockchainStatus.h) set(SOURCE_FILES MicroCore.cpp @@ -14,7 +14,7 @@ set(SOURCE_FILES CmdLineOptions.cpp page.h rpccalls.cpp rpccalls.h - version.h.in) + version.h.in CurrentBlockchainStatus.cpp) # make static library called libmyxrm # that we are going to link to diff --git a/src/CurrentBlockchainStatus.cpp b/src/CurrentBlockchainStatus.cpp new file mode 100644 index 0000000..ce966cd --- /dev/null +++ b/src/CurrentBlockchainStatus.cpp @@ -0,0 +1,256 @@ +// +// Created by mwo on 16/05/17. +// + +#include "CurrentBlockchainStatus.h" + +namespace xmreg +{ + +using namespace std; + + + +bool +CurrentBlockchainStatus::init_monero_blockchain() +{ + // set monero log output level + uint32_t log_level = 0; + mlog_configure(mlog_get_default_log_path(""), true); + + mcore = unique_ptr(new xmreg::MicroCore{}); + + // initialize the core using the blockchain path + if (!mcore->init(blockchain_path)) + { + cerr << "Error accessing blockchain." << endl; + return false; + } + + // get the high level Blockchain object to interact + // with the blockchain lmdb database + core_storage = &(mcore->get_core()); + + return true; +} + + +void +CurrentBlockchainStatus::start_monitor_blockchain_thread() +{ + + string emmision_saved_file = blockchain_path + output_file; + + if (boost::filesystem::exists(emmision_saved_file)) + { + if (!load_current_emission_amount()) + { + cerr << "Cant read or has incorrect format: " << emmision_saved_file << endl; + return; + } + } + + if (!is_running) + { + m_thread = std::thread{[]() + { + while (true) + { + current_height = core_storage->get_current_blockchain_height(); + + update_current_emission_amount(); + + save_current_emission_amount(); + + if (searched_blk_no < current_height - 1000) + { + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + else + { + std::this_thread::sleep_for(std::chrono::seconds(60)); + } + } + }}; + + is_running = true; + } +} + + +void +CurrentBlockchainStatus::update_current_emission_amount() +{ + + cout << "updating emission rate: " << current_height << endl; + + uint64_t no_of_blocks_to_search_at_once {1000}; + + uint64_t blk_no = searched_blk_no; + + uint64_t end_block = blk_no + no_of_blocks_to_search_at_once; + + uint64_t current_blockchain_height = current_height; + + end_block = end_block > current_blockchain_height ? current_blockchain_height : end_block; + + uint64_t current_emission_amount {0}; + uint64_t current_fee_amount {0}; + + while (blk_no < end_block) + { + + block blk; + + mcore->get_block_by_height(blk_no, blk); + + uint64_t coinbase_amount = get_outs_money_amount(blk.miner_tx); + + std::list txs; + std::list missed_txs; + + uint64_t tx_fee_amount = 0; + + core_storage->get_transactions(blk.tx_hashes, txs, missed_txs); + + for(const auto& tx: txs) + { + tx_fee_amount += get_tx_fee(tx); + } + + current_emission_amount += coinbase_amount - tx_fee_amount; + current_fee_amount += tx_fee_amount; + + ++blk_no; + } + + searched_blk_no = blk_no; + total_emission_amount += current_emission_amount; + total_fee_amount += current_fee_amount; + + cout << "blk: " << blk_no + << " total_emission_amount: " << xmr_amount_to_str(uint64_t(total_emission_amount)) + << " total_fee_amount: " << xmr_amount_to_str(uint64_t(total_fee_amount)) + << endl; +} + + + +bool +CurrentBlockchainStatus::save_current_emission_amount() +{ + + string emmision_saved_file = blockchain_path + output_file; + + ofstream out(emmision_saved_file); + + if( !out ) + { + cerr << "Couldn't open file." << endl; + return false; + } + + uint64_t check_sum = uint64_t(searched_blk_no) + + uint64_t(total_emission_amount) + + uint64_t(total_fee_amount); + + string out_line = to_string(searched_blk_no) + + "," + to_string(total_emission_amount) + + "," + to_string(total_fee_amount) + + "," + to_string(check_sum); + + out << out_line << flush; + + return true; +} + + +bool +CurrentBlockchainStatus::load_current_emission_amount() +{ + string emmision_saved_file = blockchain_path + output_file; + + string last_saved_emmision = xmreg::read(emmision_saved_file); + + if (last_saved_emmision.empty()) { + cerr << "Couldn't open file." << endl; + return false; + } + + vector strs; + boost::split(strs, last_saved_emmision, boost::is_any_of(",")); + + if (strs.empty()) { + cerr << "Problem spliting string values form emission_amount." << endl; + return false; + } + + uint64_t read_check_sum{0}; + + try { + searched_blk_no = boost::lexical_cast(strs.at(0)); + total_emission_amount = boost::lexical_cast(strs.at(1)); + total_fee_amount = boost::lexical_cast(strs.at(2)); + read_check_sum = boost::lexical_cast(strs.at(3)); + } + catch (boost::bad_lexical_cast &e) { + cerr << "Cant parse to number: " << last_saved_emmision << endl; + return false; + } + + uint64_t check_sum = uint64_t(searched_blk_no) + + uint64_t(total_emission_amount) + + uint64_t(total_fee_amount); + + if (read_check_sum != check_sum) { + cerr << "read_check_sum != check_sum: " + << read_check_sum << " != " << check_sum + << endl; + + return false; + } + + return true; + +} + +vector +CurrentBlockchainStatus::get_emission_amount() +{ + + uint64_t searched_block = searched_blk_no; + uint64_t emission_amount = total_emission_amount; + uint64_t fee_amount = total_fee_amount; + + return {searched_block, emission_amount, fee_amount}; + +} + +bool +CurrentBlockchainStatus::is_thread_running() +{ + return is_running; +} + +string CurrentBlockchainStatus::blockchain_path{"/home/mwo/.bitmonero/lmdb"}; + +string CurrentBlockchainStatus::output_file {"/emission_amount.txt"}; + +string CurrentBlockchainStatus::deamon_url{"http:://127.0.0.1:18081"}; + +atomic CurrentBlockchainStatus::current_height {0}; + +atomic CurrentBlockchainStatus::total_emission_amount {0} ; + +atomic CurrentBlockchainStatus::total_fee_amount {0} ; + +atomic CurrentBlockchainStatus::searched_blk_no {0}; + +std::thread CurrentBlockchainStatus::m_thread; + +atomic CurrentBlockchainStatus::is_running {false}; + +cryptonote::Blockchain* CurrentBlockchainStatus::core_storage; +unique_ptr CurrentBlockchainStatus::mcore; + +} diff --git a/src/CurrentBlockchainStatus.h b/src/CurrentBlockchainStatus.h new file mode 100644 index 0000000..61762c6 --- /dev/null +++ b/src/CurrentBlockchainStatus.h @@ -0,0 +1,73 @@ +// +// Created by mwo on 16/05/17. +// + +#ifndef XMRBLOCKS_CURRENTBLOCKCHAINSTATUS_H +#define XMRBLOCKS_CURRENTBLOCKCHAINSTATUS_H + +#include "MicroCore.h" + +#include + +#include +#include +#include +#include +#include + +namespace xmreg +{ + +using namespace std; + +class CurrentBlockchainStatus +{ + static string blockchain_path; + + static string output_file; + + static string deamon_url; + + static atomic current_height; + + static atomic total_emission_amount; + static atomic total_fee_amount; + + static atomic searched_blk_no; + + static std::thread m_thread; + + static atomic is_running; + + // make object for accessing the blockchain here + static unique_ptr mcore; + static cryptonote::Blockchain *core_storage; + + +public: + static void + start_monitor_blockchain_thread(); + + static bool + init_monero_blockchain(); + + static void + update_current_emission_amount(); + + static bool + save_current_emission_amount(); + + static bool + load_current_emission_amount(); + + static vector + get_emission_amount(); + + static bool + is_thread_running(); + +}; + +} + +#endif //XMRBLOCKS_CURRENTBLOCKCHAINSTATUS_H diff --git a/src/page.h b/src/page.h index fe2b864..fb6a1ea 100644 --- a/src/page.h +++ b/src/page.h @@ -17,6 +17,8 @@ #include "tools.h" #include "rpccalls.h" +#include "CurrentBlockchainStatus.h" + #include "../ext/crow/http_request.h" #include "../ext/vpetrigocaches/cache.hpp" @@ -746,9 +748,9 @@ namespace xmreg } context["network_info"] = mstch::map { - {"difficulty", j_network_info["difficulty"].get()}, - {"hash_rate" , difficulty}, - {"fee_per_kb", xmreg::xmr_amount_to_str(j_network_info["fee_per_kb"], "{:0.12f}")} + {"difficulty" , j_network_info["difficulty"].get()}, + {"hash_rate" , difficulty}, + {"fee_per_kb" , print_money(j_network_info["fee_per_kb"])} }; } } @@ -757,6 +759,27 @@ namespace xmreg cerr << "network_info future not ready yet, skipping." << endl; } + if (CurrentBlockchainStatus::is_thread_running()) + { + vector emission_values + = CurrentBlockchainStatus::get_emission_amount(); + + string emission_blk_no = std::to_string(emission_values.at(0) - 1); + string emission_amount = xmr_amount_to_str(emission_values.at(1), "{:0.3f}"); + string emission_fee_amount = xmr_amount_to_str(emission_values.at(2), "{:0.3f}"); + + context["emission"] = mstch::map { + {"blk_no" , emission_blk_no}, + {"amount" , emission_amount}, + {"fee_amount", emission_fee_amount} + }; + } + else + { + cerr << "emission thread not running, skipping." << endl; + } + + // get memory pool rendered template string mempool_html = mempool(false, no_of_mempool_tx_of_frontpage); diff --git a/src/templates/index2.html b/src/templates/index2.html index 94ac810..b143591 100644 --- a/src/templates/index2.html +++ b/src/templates/index2.html @@ -35,13 +35,18 @@ {{#network_info}} - -

+

Network difficulty: {{difficulty}} | Hash rate: {{hash_rate}} | Fee per kb: {{fee_per_kb}}

- {{/network_info}} + {{/network_info}} + + {{#emission}} +

+ Monero emission (fees) is {{amount}} ({{fee_amount}}) as of {{blk_no}} block +

+ {{/emission}}