From 4500e640c3732c8ec3a25cffbec841ff4e465803 Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Thu, 25 May 2017 13:21:17 +0800 Subject: [PATCH] Mempool data processing moved to async thread --- main.cpp | 18 ++++++++++++++++ src/CmdLineOptions.cpp | 2 ++ src/page.h | 36 +++++++++++++++++++++++--------- src/templates/css/style.css | 2 +- src/templates/mempool_error.html | 10 +++++++++ 5 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 src/templates/mempool_error.html diff --git a/main.cpp b/main.cpp index b3bee81..1c8275e 100644 --- a/main.cpp +++ b/main.cpp @@ -49,6 +49,7 @@ main(int ac, const char* av[]) auto no_blocks_on_index_opt = opts.get_option("no-blocks-on-index"); auto testnet_url = opts.get_option("testnet-url"); auto mainnet_url = opts.get_option("mainnet-url"); + auto network_info_timeout_opt = opts.get_option("network-info-timeout"); auto testnet_opt = opts.get_option("testnet"); auto enable_key_image_checker_opt = opts.get_option("enable-key-image-checker"); auto enable_output_key_checker_opt = opts.get_option("enable-output-key-checker"); @@ -63,6 +64,7 @@ main(int ac, const char* av[]) auto enable_emission_monitor_opt = opts.get_option("enable-emission-monitor"); + bool testnet {*testnet_opt}; bool enable_pusher {*enable_pusher_opt}; bool enable_key_image_checker {*enable_key_image_checker_opt}; @@ -152,6 +154,21 @@ main(int ac, const char* av[]) deamon_url = "http:://127.0.0.1:28081"; } + uint64_t network_info_timeout {3000}; + + try + { + network_info_timeout = boost::lexical_cast(*network_info_timeout_opt); + } + catch (boost::bad_lexical_cast &e) + { + cout << "Cant cast " << (*network_info_timeout_opt) << " into number." + << "Using default value of " << network_info_timeout << " milliseconds." + << endl; + + } + + if (enable_emission_monitor == true) { @@ -200,6 +217,7 @@ main(int ac, const char* av[]) enable_block_cache, show_cache_times, no_blocks_on_index, + network_info_timeout, *testnet_url, *mainnet_url); diff --git a/src/CmdLineOptions.cpp b/src/CmdLineOptions.cpp index 1ae6493..3b423cf 100644 --- a/src/CmdLineOptions.cpp +++ b/src/CmdLineOptions.cpp @@ -55,6 +55,8 @@ namespace xmreg "you can specify mainnet url, if you run it on testnet. link will show on front page to mainnet explorer") ("no-blocks-on-index", value()->default_value("10"), "number of last blocks to be shown on index page") + ("network-info-timeout", value()->default_value("3000"), + "maximum time, in milliseconds, to wait for network and mempool data availability") ("bc-path,b", value(), "path to lmdb folder of the blockchain, e.g., ~/.bitmonero/lmdb") ("ssl-crt-file", value(), diff --git a/src/page.h b/src/page.h index 3493d5d..1ae0387 100644 --- a/src/page.h +++ b/src/page.h @@ -35,6 +35,7 @@ #define TMPL_INDEX TMPL_DIR "/index.html" #define TMPL_INDEX2 TMPL_DIR "/index2.html" #define TMPL_MEMPOOL TMPL_DIR "/mempool.html" +#define TMPL_MEMPOOL_ERROR TMPL_DIR "/mempool_error.html" #define TMPL_HEADER TMPL_DIR "/header.html" #define TMPL_FOOTER TMPL_DIR "/footer.html" #define TMPL_BLOCK TMPL_DIR "/block.html" @@ -261,6 +262,7 @@ namespace xmreg uint64_t no_of_mempool_tx_of_frontpage; uint64_t no_blocks_on_index; + uint64_t network_info_timeout; string testnet_url; string mainnet_url; @@ -335,6 +337,7 @@ namespace xmreg bool _enable_block_cache, bool _show_cache_times, uint64_t _no_blocks_on_index, + uint64_t _network_info_timeout, string _testnet_url, string _mainnet_url) : mcore {_mcore}, @@ -352,6 +355,7 @@ namespace xmreg enable_block_cache {_enable_block_cache}, show_cache_times {_show_cache_times}, no_blocks_on_index {_no_blocks_on_index}, + network_info_timeout {_network_info_timeout}, testnet_url {_testnet_url}, mainnet_url {_mainnet_url}, mempool_tx_json_cache(1000), @@ -369,6 +373,7 @@ namespace xmreg template_file["footer"] = get_footer(); template_file["index2"] = get_full_page(xmreg::read(TMPL_INDEX2)); template_file["mempool"] = xmreg::read(TMPL_MEMPOOL); + template_file["mempool_error"] = xmreg::read(TMPL_MEMPOOL_ERROR); template_file["mempool_full"] = get_full_page(template_file["mempool"]); template_file["block"] = get_full_page(xmreg::read(TMPL_BLOCK)); template_file["tx"] = get_full_page(xmreg::read(TMPL_TX)); @@ -397,6 +402,8 @@ namespace xmreg index2(uint64_t page_no = 0, bool refresh_page = false) { + + // we get network info, such as current hash rate // but since this makes a rpc call to deamon, we make it as an async // call. this way we dont have to wait with execution of the rest of the @@ -420,6 +427,9 @@ namespace xmreg j_info["fee_per_kb"] = fee_estimated; + // get memory pool rendered template + j_info["mempool_html"] = mempool(false, no_of_mempool_tx_of_frontpage); + return j_info; }); @@ -734,11 +744,13 @@ namespace xmreg context["cache_misses"] = cache_misses; // now time to check if we have our networkinfo from network_info future - // wait a bit (200 millisecond max) if not, just in case, but we dont wait more. + // wait a bit (network_info_timeout millisecond max) if not, just in case, but we dont wait more. // if its not ready by now, forget about it. + string mempool_html {"Cant get mempool_pool"}; + std::future_status ftr_status = network_info_ftr.wait_for( - std::chrono::milliseconds(200)); + std::chrono::milliseconds(network_info_timeout)); if (ftr_status == std::future_status::ready) { @@ -758,16 +770,20 @@ namespace xmreg } context["network_info"] = mstch::map { - {"difficulty" , j_network_info["difficulty"].get()}, - {"hash_rate" , difficulty}, - {"fee_per_kb" , print_money(j_network_info["fee_per_kb"])}, - {"alt_blocks_no" , j_network_info["alt_blocks_count"].get()} + {"difficulty" , j_network_info["difficulty"].get()}, + {"hash_rate" , difficulty}, + {"fee_per_kb" , print_money(j_network_info["fee_per_kb"])}, + {"alt_blocks_no" , j_network_info["alt_blocks_count"].get()} }; + + mempool_html = j_network_info["mempool_html"]; } } else { - cerr << "network_info future not ready yet, skipping." << endl; + mempool_html = template_file["mempool_error"]; + + cerr << "network_info and mempool future not ready yet, skipping." << endl; } if (CurrentBlockchainStatus::is_thread_running()) @@ -792,7 +808,7 @@ namespace xmreg // get memory pool rendered template - string mempool_html = mempool(false, no_of_mempool_tx_of_frontpage); + //string mempool_html = mempool(false, no_of_mempool_tx_of_frontpage); // append mempool_html to the index context map context["mempool_info"] = mempool_html; @@ -804,8 +820,8 @@ namespace xmreg } /** - * Render mempool data - */ + * Render mempool data + */ string mempool(bool add_header_and_footer = false, uint64_t no_of_mempool_tx = 25) { diff --git a/src/templates/css/style.css b/src/templates/css/style.css index f11dee2..ac4336b 100644 --- a/src/templates/css/style.css +++ b/src/templates/css/style.css @@ -16,7 +16,7 @@ h1, h2, h3, h4, h5, h6 { padding: 10px;*/ } -tr, li, #pages { +tr, li, #pages, .info { font-family: "Lucida Console", Monaco, monospace; font-size : 12px; height: 22px; diff --git a/src/templates/mempool_error.html b/src/templates/mempool_error.html new file mode 100644 index 0000000..8397c66 --- /dev/null +++ b/src/templates/mempool_error.html @@ -0,0 +1,10 @@ +

+ Memory pool +

+

+
+ + Newtork info and mempool data preparation failed. + Its processing took longer than expected and it timed out. Sorry. + +