From a0f85e68ebfc218626c252994a5a33e6b62f879e Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Tue, 7 Feb 2017 00:56:17 +0000 Subject: [PATCH] rpccalls.h updated to match current monero rpc changes --- src/page.h | 196 ------------------------------------------------- src/rpccalls.h | 55 ++++++++++---- 2 files changed, 39 insertions(+), 212 deletions(-) diff --git a/src/page.h b/src/page.h index 5bc0f2d..a9ab792 100644 --- a/src/page.h +++ b/src/page.h @@ -278,202 +278,6 @@ public: no_of_mempool_tx_of_frontpage = 25; } - - /** - * @brief Show recent blocks and mempool - * - * Not used currently. index2 method is used instead - * - * @param page_no block page to show - * @param refresh_page enable autorefresh - * @return rendered index page - */ - string - index(uint64_t page_no = 0, bool refresh_page = false) - { - // connect to the deamon if not yet connected - bool is_connected = rpc.connect_to_monero_deamon(); - - if (!is_connected) - { - cerr << "Connection to the Monero demon does not exist or was lost!" << endl; - return "Connection to the Monero demon does not exist or was lost!"; - } - - //get current server timestamp - server_timestamp = std::time(nullptr); - - // number of last blocks to show - uint64_t no_of_last_blocks {100 + 1}; - - uint64_t height = rpc.get_current_height() - 1; - - // initalise page tempate map with basic info about blockchain - mstch::map context { - {"refresh" , refresh_page}, - {"height" , std::to_string(height)}, - {"server_timestamp", xmreg::timestamp_to_str(server_timestamp)}, - {"age_format" , string("[h:m:d]")}, - {"page_no" , std::to_string(page_no)}, - {"total_page_no" , std::to_string(height / (no_of_last_blocks))}, - {"is_page_zero" , !bool(page_no)}, - {"next_page" , std::to_string(page_no + 1)}, - {"prev_page" , std::to_string((page_no > 0 ? page_no - 1 : 0))} - }; - context.emplace("blocks", mstch::array()); - - - // get reference to blocks template map to be field below - mstch::array& blocks = boost::get(context["blocks"]); - - // calculate starting and ending block numbers to show - uint64_t start_height = height - no_of_last_blocks * (page_no + 1); - uint64_t end_height = height - no_of_last_blocks * (page_no); - - // check few conditions to make sure we are whithin the avaliable range - //@TODO its too messed up. needs to find cleaner way. - start_height = start_height > 0 ? start_height : 0; - end_height = end_height < height ? end_height : height; - start_height = start_height > end_height ? 0 : start_height; - end_height = end_height - start_height > no_of_last_blocks - ? no_of_last_blocks : end_height; - - // previous blk timestamp, initalised to lowest possible value - double prev_blk_timestamp {std::numeric_limits::lowest()}; - - // iterate over last no_of_last_blocks of blocks - for (uint64_t i = start_height; i <= end_height; ++i) - { - // get block at the given height i - block blk; - - if (!mcore->get_block_by_height(i, blk)) - { - cerr << "Cant get block: " << i << endl; - continue; - } - - // get block's hash - crypto::hash blk_hash = core_storage->get_block_id_by_height(i); - - // remove "<" and ">" from the hash string - string blk_hash_str = REMOVE_HASH_BRAKETS(fmt::format("{:s}", blk_hash)); - - // get block timestamp in user friendly format - string timestamp_str = xmreg::timestamp_to_str(blk.timestamp); - - pair age = get_age(server_timestamp, blk.timestamp); - - context["age_format"] = age.second; - - // get time difference [m] between previous and current blocks - string time_delta_str {}; - - if (prev_blk_timestamp > std::numeric_limits::lowest()) - { - time_delta_str = fmt::format("{:0.2f}", - (double(blk.timestamp) - double(prev_blk_timestamp))/60.0); - } - - // get xmr in the block reward - array coinbase_tx = sum_money_in_tx(blk.miner_tx); - - // get transactions in the block - const vector& txs_in_blk = - core_storage->get_db().get_tx_list(blk.tx_hashes); - - // sum xmr in the inputs and ouputs of all transactions - array sum_xmr_in_out = sum_money_in_txs(txs_in_blk); - - // get sum of all transactions in the block - uint64_t sum_fees = sum_fees_in_txs(txs_in_blk); - - // get mixin number in each transaction - vector mixin_numbers = xmreg::get_mixin_no_in_txs(txs_in_blk); - - // find minimum and maxium mixin numbers - int mixin_min {-1}; - int mixin_max {-1}; - - if (!mixin_numbers.empty()) - { - mixin_min = static_cast( - *std::min_element(mixin_numbers.begin(), mixin_numbers.end())); - mixin_max = static_cast( - *max_element(mixin_numbers.begin(), mixin_numbers.end())); - } - - // mixing format for the templates - auto mixin_format = [=]() -> mstch::node - { - if (mixin_min < 0) - { - return string("N/A"); - } - return fmt::format("{:d}-{:d}", mixin_min - 1, mixin_max - 1); - }; - - // get block size in bytes - uint64_t blk_size = get_object_blobsize(blk); - - // set output page template map - blocks.push_back(mstch::map { - {"height" , to_string(i)}, - {"timestamp" , timestamp_str}, - {"time_delta" , time_delta_str}, - {"age" , age.first}, - {"hash" , blk_hash_str}, - {"block_reward", fmt::format("{:0.4f}/{:0.4f}", - XMR_AMOUNT(coinbase_tx[1] - sum_fees), - XMR_AMOUNT(sum_fees))}, - {"fees" , fmt::format("{:0.3f}", XMR_AMOUNT(sum_fees))}, - {"notx" , fmt::format("{:d}", blk.tx_hashes.size())}, - {"xmr_inputs" , fmt::format("{:0.2f}", - XMR_AMOUNT(sum_xmr_in_out[0]))}, - {"xmr_outputs" , fmt::format("{:0.2f}", - XMR_AMOUNT(sum_xmr_in_out[1]))}, - {"mixin_range" , mstch::lambda {mixin_format}}, - {"blksize" , fmt::format("{:0.2f}", - static_cast(blk_size) / 1024.0)} - }); - - // save current's block timestamp as reference for the next one - prev_blk_timestamp = static_cast(blk.timestamp); - - } // for (uint64_t i = start_height; i <= end_height; ++i) - - // reverse blocks and remove last (i.e., oldest) - // block. This is done so that time delats - // are easier to calcualte in the above for loop - std::reverse(blocks.begin(), blocks.end()); - - // if we look at the genesis time, we should not remove - // the last block, i.e. genesis one. - if (!(start_height < 2)) - { - blocks.pop_back(); - } - - // get memory pool rendered template - string mempool_html = mempool(); - - // append mempool_html to the index context map - context["mempool_info"] = mempool_html; - - // read index.html - string index_html = xmreg::read(TMPL_INDEX); - - // add header and footer - string full_page = get_full_page(index_html); - - context["css_styles"] = this->css_styles; - - // render the page - return mstch::render(full_page, context); - } - - - /** * @brief show recent transactions and mempool * @param page_no block page to show diff --git a/src/rpccalls.h b/src/rpccalls.h index 82f6436..4980063 100644 --- a/src/rpccalls.h +++ b/src/rpccalls.h @@ -23,6 +23,8 @@ namespace xmreg string deamon_url ; uint64_t timeout_time; + std::chrono::milliseconds timeout_time_ms; + epee::net_utils::http::url_content url; epee::net_utils::http::http_simple_client m_http_client; @@ -34,26 +36,29 @@ namespace xmreg rpccalls(string _deamon_url = "http:://127.0.0.1:18081", uint64_t _timeout = 200000) - : deamon_url {_deamon_url}, timeout_time {_timeout} + : deamon_url {_deamon_url}, + timeout_time {_timeout} { epee::net_utils::parse_url(deamon_url, url); port = std::to_string(url.port); + + timeout_time_ms = std::chrono::milliseconds {timeout_time}; + + m_http_client.set_server(deamon_url); } bool connect_to_monero_deamon() { - std::lock_guard guard(m_daemon_rpc_mutex); + //std::lock_guard guard(m_daemon_rpc_mutex); if(m_http_client.is_connected()) { return true; } - return m_http_client.connect(url.host, - port, - timeout_time); + return m_http_client.connect(timeout_time_ms); } uint64_t @@ -64,9 +69,15 @@ namespace xmreg std::lock_guard guard(m_daemon_rpc_mutex); - bool r = epee::net_utils::invoke_http_json_remote_command2( - deamon_url + "/getheight", - req, res, m_http_client, timeout_time); + if (!connect_to_monero_deamon()) + { + cerr << "get_current_height: not connected to deamon" << endl; + return false; + } + + bool r = epee::net_utils::invoke_http_json( + "/getheight", + req, res, m_http_client, timeout_time_ms); if (!r) { @@ -83,16 +94,23 @@ namespace xmreg } bool - get_mempool(vector& mempool_txs) { + get_mempool(vector& mempool_txs) + { COMMAND_RPC_GET_TRANSACTION_POOL::request req; COMMAND_RPC_GET_TRANSACTION_POOL::response res; std::lock_guard guard(m_daemon_rpc_mutex); - bool r = epee::net_utils::invoke_http_json_remote_command2( - deamon_url + "/get_transaction_pool", - req, res, m_http_client, timeout_time); + if (!connect_to_monero_deamon()) + { + cerr << "get_mempool: not connected to deamon" << endl; + return false; + } + + bool r = epee::net_utils::invoke_http_json( + "/get_transaction_pool", + req, res, m_http_client, timeout_time_ms); if (!r) { @@ -122,10 +140,15 @@ namespace xmreg std::lock_guard guard(m_daemon_rpc_mutex); - bool r = epee::net_utils::invoke_http_json_remote_command2(deamon_url - + "/sendrawtransaction", - req, res, - m_http_client, 200000);; + if (!connect_to_monero_deamon()) + { + cerr << "commit_tx: not connected to deamon" << endl; + return false; + } + + bool r = epee::net_utils::invoke_http_json( + "/sendrawtransaction", + req, res, m_http_client, timeout_time_ms); if (!r || res.status == "Failed") {