Add (non-standard) 'daemon_status' endpoint to REST API (#124)

This commit is contained in:
Lee *!* Clagett
2024-08-06 19:04:55 -04:00
committed by Lee *!* Clagett
parent 79564f714f
commit 8fad87a0fe
3 changed files with 122 additions and 2 deletions

View File

@@ -37,6 +37,7 @@
#include "common/error.h" // monero/src
#include "common/expect.h" // monero/src
#include "config.h"
#include "crypto/crypto.h" // monero/src
#include "cryptonote_config.h" // monero/src
#include "db/data.h"
@@ -211,6 +212,60 @@ namespace lws
bool auto_accept_creation;
};
struct daemon_status
{
using request = rpc::daemon_status_request;
using response = rpc::daemon_status_response;
static expect<response> handle(request, const db::storage&, const rpc::client& gclient, const runtime_options&)
{
using info_rpc = cryptonote::rpc::GetInfo;
const expect<rpc::client*> tclient = thread_client(gclient);
if (!tclient)
return tclient.error();
if (*tclient == nullptr)
throw std::logic_error{"Unexpected rpc::client nullptr"};
// default to an unavailable daemon
response resp{
.network = rpc::network_type(lws::config::network),
.state = rpc::daemon_state::unavailable
};
info_rpc::Request daemon_req{};
epee::byte_slice message = rpc::client::make_message("get_info", daemon_req);
const expect<void> sent = send_with_retry(**tclient, std::move(message), std::chrono::seconds{2});
if (!sent)
{
if (sent.matches(std::errc::timed_out))
return resp;
return sent.error();
}
const auto daemon_resp = (*tclient)->receive<info_rpc::Response>(std::chrono::seconds{4}, MLWS_CURRENT_LOCATION);
if (!daemon_resp)
{
if (daemon_resp.matches(std::errc::timed_out))
return resp;
return daemon_resp.error();
}
resp.outgoing_connections_count = daemon_resp->info.outgoing_connections_count;
resp.incoming_connections_count = daemon_resp->info.incoming_connections_count;
resp.height = daemon_resp->info.height;
resp.target_height = daemon_resp->info.target_height;
if (!resp.outgoing_connections_count && !resp.incoming_connections_count)
resp.state = rpc::daemon_state::no_connections;
else if (resp.target_height && (resp.target_height - resp.height) >= 5)
resp.state = rpc::daemon_state::synchronizing;
else
resp.state = rpc::daemon_state::ok;
return resp;
}
};
struct get_address_info
{
using request = rpc::account_credentials;
@@ -956,6 +1011,7 @@ namespace lws
constexpr const endpoint endpoints[] =
{
{"/daemon_status", call<daemon_status>, 1024},
{"/get_address_info", call<get_address_info>, 2 * 1024},
{"/get_address_txs", call<get_address_txs>, 2 * 1024},
{"/get_random_outs", call<get_random_outs>, 2 * 1024},