From 19718932c3bc9154b9279de727370e5e971d6cef Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Thu, 11 May 2017 12:17:10 +0800 Subject: [PATCH] json_networkinfo added --- README.md | 37 +++++++++++++++++++++++++++ main.cpp | 8 ++++++ src/page.h | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ src/rpccalls.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++ src/rpccalls.h | 4 ++- 5 files changed, 172 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b525fc9..7084ed7 100644 --- a/README.md +++ b/README.md @@ -549,6 +549,43 @@ curl -w "\n" -X GET "http://139.162.32.245:8082/api/outputs?txhash=94782a8c0aa8 } ``` + +Result analogical to the one above. + +#### api/networkinfo + +```bash +curl -w "\n" -X GET "http://139.162.32.245:8081/api/networkinfo" +``` + +```json +{ + "data": { + "alt_blocks_count": 0, + "block_size_limit": 600000, + "cumulative_difficulty": 2067724366624367, + "difficulty": 7530486740, + "grey_peerlist_size": 4987, + "hash_rate": 62754056, + "height": 1307537, + "incoming_connections_count": 0, + "outgoing_connections_count": 8, + "start_time": 1494473774, + "status": "OK", + "target": 120, + "target_height": 1307518, + "testnet": false, + "top_block_hash": "0726de5b86f431547fc64fc2c8e1c11d76843ada0561993ee540e4eee29d83c3", + "tx_count": 1210222, + "tx_pool_size": 5, + "white_peerlist_size": 1000 + }, + "status": "success" +} +``` + + + ## Other monero examples Other examples can be found on [github](https://github.com/moneroexamples?tab=repositories). diff --git a/main.cpp b/main.cpp index 683da41..8c1827d 100644 --- a/main.cpp +++ b/main.cpp @@ -426,6 +426,14 @@ int main(int ac, const char* av[]) { return r; }); + CROW_ROUTE(app, "/api/networkinfo") + ([&](const crow::request &req) { + + myxmr::jsonresponse r{xmrblocks.json_networkinfo()}; + + return r; + }); + CROW_ROUTE(app, "/api/outputs").methods("GET"_method) ([&](const crow::request &req) { diff --git a/src/page.h b/src/page.h index aaea01b..128b541 100644 --- a/src/page.h +++ b/src/page.h @@ -4660,6 +4660,38 @@ namespace xmreg return j_response; } + + /* + * Lets use this json api convention for success and error + * https://labs.omniti.com/labs/jsend + */ + json + json_networkinfo() + { + json j_response { + {"status", "fail"}, + {"data", json {}} + }; + + json& j_data = j_response["data"]; + + json j_info; + + if (!get_monero_network_info(j_info)) + { + j_response["status"] = "error"; + j_response["message"] = "Cant get monero network info"; + return j_response; + } + + j_data = j_info; + + j_response["status"] = "success"; + + return j_response; + } + + private: json @@ -5432,6 +5464,40 @@ namespace xmreg + template_file["footer"]; } + bool + get_monero_network_info(json& j_info) + { + COMMAND_RPC_GET_INFO::response network_info; + + if (!rpc.get_network_info(network_info)) + { + return false; + } + + j_info = json { + {"status" , network_info.status}, + {"height" , network_info.height}, + {"target_height" , network_info.target_height}, + {"difficulty" , network_info.difficulty}, + {"target" , network_info.target}, + {"hash_rate" , (network_info.difficulty/network_info.target)}, + {"tx_count" , network_info.tx_count}, + {"tx_pool_size" , network_info.tx_pool_size}, + {"alt_blocks_count" , network_info.alt_blocks_count}, + {"outgoing_connections_count", network_info.outgoing_connections_count}, + {"incoming_connections_count", network_info.incoming_connections_count}, + {"white_peerlist_size" , network_info.white_peerlist_size}, + {"grey_peerlist_size" , network_info.grey_peerlist_size}, + {"testnet" , network_info.testnet}, + {"top_block_hash" , network_info.top_block_hash}, + {"cumulative_difficulty" , network_info.cumulative_difficulty}, + {"block_size_limit" , network_info.block_size_limit}, + {"start_time" , network_info.start_time} + }; + + return true; + } + string get_footer() { diff --git a/src/rpccalls.cpp b/src/rpccalls.cpp index 61bcf93..7fcce07 100644 --- a/src/rpccalls.cpp +++ b/src/rpccalls.cpp @@ -147,6 +147,64 @@ rpccalls::commit_tx(tools::wallet2::pending_tx& ptx, string& error_msg) return true; } +bool +rpccalls::get_network_info(COMMAND_RPC_GET_INFO::response& response) +{ + + epee::json_rpc::request req_t = AUTO_VAL_INIT(req_t); + epee::json_rpc::response resp_t = AUTO_VAL_INIT(resp_t); + + bool r {false}; + + req_t.jsonrpc = "2.0"; + req_t.id = epee::serialization::storage_entry(0); + req_t.method = "get_info"; + + { + std::lock_guard guard(m_daemon_rpc_mutex); + + if (!connect_to_monero_deamon()) + { + cerr << "get_mempool: not connected to deamon" << endl; + return false; + } + + r = epee::net_utils::invoke_http_json("/json_rpc", + req_t, resp_t, + m_http_client); + } + + string err; + + if (r) + { + if (resp_t.result.status == CORE_RPC_STATUS_BUSY) + { + err = "daemon is busy. Please try again later."; + } + else if (resp_t.result.status != CORE_RPC_STATUS_OK) + { + err = resp_t.result.status; + } + + if (!err.empty()) + { + cerr << "Error connecting to Monero deamon due to " + << err << endl; + return false; + } + } + else + { + cerr << "Error connecting to Monero deamon at " + << deamon_url << endl; + return false; + } + + response = resp_t.result; + + return true; +} } diff --git a/src/rpccalls.h b/src/rpccalls.h index 95ac59f..ed21ea4 100644 --- a/src/rpccalls.h +++ b/src/rpccalls.h @@ -46,10 +46,12 @@ public: bool get_mempool(vector& mempool_txs); - bool commit_tx(tools::wallet2::pending_tx& ptx, string& error_msg); + bool + get_network_info(COMMAND_RPC_GET_INFO::response& info); + };