json_networkinfo added

This commit is contained in:
moneroexamples 2017-05-11 12:17:10 +08:00
parent 67b48ccc93
commit 19718932c3
5 changed files with 172 additions and 1 deletions

View File

@ -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).

View File

@ -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) {

View File

@ -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()
{

View File

@ -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<cryptonote::COMMAND_RPC_GET_INFO::request> req_t = AUTO_VAL_INIT(req_t);
epee::json_rpc::response<cryptonote::COMMAND_RPC_GET_INFO::response, std::string> 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<std::mutex> 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;
}
}

View File

@ -46,10 +46,12 @@ public:
bool
get_mempool(vector<tx_info>& mempool_txs);
bool
commit_tx(tools::wallet2::pending_tx& ptx, string& error_msg);
bool
get_network_info(COMMAND_RPC_GET_INFO::response& info);
};