From 7a4a02b56317eb25c9ac705d3a6d568a3dbe11b1 Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Wed, 17 May 2017 08:20:43 +0800 Subject: [PATCH] emission for testnet network added --- main.cpp | 33 +++++++++++------- src/CurrentBlockchainStatus.cpp | 60 +++++++++++++++++++++++---------- src/CurrentBlockchainStatus.h | 15 ++++++--- 3 files changed, 75 insertions(+), 33 deletions(-) diff --git a/main.cpp b/main.cpp index 6a1d5a7..2b38b74 100644 --- a/main.cpp +++ b/main.cpp @@ -128,18 +128,6 @@ 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 @@ -157,7 +145,28 @@ int main(int ac, const char* av[]) { string deamon_url {*deamon_url_opt}; if (testnet && deamon_url == "http:://127.0.0.1:18081") + { deamon_url = "http:://127.0.0.1:28081"; + } + + xmreg::CurrentBlockchainStatus::blockchain_path + = blockchain_path.string(); + xmreg::CurrentBlockchainStatus::testnet + = testnet; + xmreg::CurrentBlockchainStatus::deamon_url + = deamon_url; + + if (!xmreg::CurrentBlockchainStatus::init_monero_blockchain()) + { + cerr << "Error accessing blockchain from CurrentBlockchainStatus." << 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 page class which // contains logic for the website diff --git a/src/CurrentBlockchainStatus.cpp b/src/CurrentBlockchainStatus.cpp index ce966cd..c25ad99 100644 --- a/src/CurrentBlockchainStatus.cpp +++ b/src/CurrentBlockchainStatus.cpp @@ -39,13 +39,21 @@ void CurrentBlockchainStatus::start_monitor_blockchain_thread() { - string emmision_saved_file = blockchain_path + output_file; + string emmision_saved_file = get_output_file_path(); if (boost::filesystem::exists(emmision_saved_file)) { if (!load_current_emission_amount()) { - cerr << "Cant read or has incorrect format: " << emmision_saved_file << endl; + cerr << "Emission file cant be read, got corrupted or has incorrect format:\n " << emmision_saved_file + << "\nEmission monitoring thread is not started.\nDelete the file and" + << " restart the explorer or disable emission monitoring." + << endl; + + cerr << "Press ENTER to continue without emission monitoring or Ctr+C to exit" << endl; + + cin.get(); + return; } } @@ -62,7 +70,7 @@ CurrentBlockchainStatus::start_monitor_blockchain_thread() save_current_emission_amount(); - if (searched_blk_no < current_height - 1000) + if (searched_blk_no < current_height - blockchain_chunk_size) { std::this_thread::sleep_for(std::chrono::seconds(1)); } @@ -84,11 +92,9 @@ 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 end_block = blk_no + blockchain_chunk_size; uint64_t current_blockchain_height = current_height; @@ -140,7 +146,7 @@ bool CurrentBlockchainStatus::save_current_emission_amount() { - string emmision_saved_file = blockchain_path + output_file; + string emmision_saved_file = get_output_file_path(); ofstream out(emmision_saved_file); @@ -168,33 +174,39 @@ CurrentBlockchainStatus::save_current_emission_amount() bool CurrentBlockchainStatus::load_current_emission_amount() { - string emmision_saved_file = blockchain_path + output_file; + string emmision_saved_file = get_output_file_path(); string last_saved_emmision = xmreg::read(emmision_saved_file); - if (last_saved_emmision.empty()) { + if (last_saved_emmision.empty()) + { cerr << "Couldn't open file." << endl; return false; } + last_saved_emmision.erase(last_saved_emmision.find_last_not_of(" \n\r\t")+1); + vector strs; boost::split(strs, last_saved_emmision, boost::is_any_of(",")); - if (strs.empty()) { + 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)); + 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)); + 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; + catch (boost::bad_lexical_cast &e) + { + cerr << "Cant parse to number date from string: " << last_saved_emmision << endl; return false; } @@ -202,7 +214,8 @@ CurrentBlockchainStatus::load_current_emission_amount() + uint64_t(total_emission_amount) + uint64_t(total_fee_amount); - if (read_check_sum != check_sum) { + if (read_check_sum != check_sum) + { cerr << "read_check_sum != check_sum: " << read_check_sum << " != " << check_sum << endl; @@ -214,6 +227,15 @@ CurrentBlockchainStatus::load_current_emission_amount() } +string +CurrentBlockchainStatus::get_output_file_path() +{ + string emmision_saved_file = blockchain_path + output_file; + + return emmision_saved_file; +} + + vector CurrentBlockchainStatus::get_emission_amount() { @@ -234,10 +256,14 @@ CurrentBlockchainStatus::is_thread_running() string CurrentBlockchainStatus::blockchain_path{"/home/mwo/.bitmonero/lmdb"}; +bool CurrentBlockchainStatus::testnet {false}; + string CurrentBlockchainStatus::output_file {"/emission_amount.txt"}; string CurrentBlockchainStatus::deamon_url{"http:://127.0.0.1:18081"}; +uint64_t CurrentBlockchainStatus::blockchain_chunk_size {10000}; + atomic CurrentBlockchainStatus::current_height {0}; atomic CurrentBlockchainStatus::total_emission_amount {0} ; diff --git a/src/CurrentBlockchainStatus.h b/src/CurrentBlockchainStatus.h index 61762c6..983c0fc 100644 --- a/src/CurrentBlockchainStatus.h +++ b/src/CurrentBlockchainStatus.h @@ -20,17 +20,23 @@ namespace xmreg using namespace std; -class CurrentBlockchainStatus +struct CurrentBlockchainStatus { + static string blockchain_path; + static bool testnet; + static string output_file; static string deamon_url; + static uint64_t blockchain_chunk_size; + static atomic current_height; static atomic total_emission_amount; + static atomic total_fee_amount; static atomic searched_blk_no; @@ -41,10 +47,9 @@ class CurrentBlockchainStatus // make object for accessing the blockchain here static unique_ptr mcore; - static cryptonote::Blockchain *core_storage; + static cryptonote::Blockchain *core_storage; -public: static void start_monitor_blockchain_thread(); @@ -63,9 +68,11 @@ public: static vector get_emission_amount(); + static string + get_output_file_path(); + static bool is_thread_running(); - }; }