Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/rest_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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},
Expand Down
24 changes: 24 additions & 0 deletions src/rpc/light_wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "ringct/rctOps.h" // monero/src
#include "span.h" // monero/contrib/epee/include
#include "util/random_outputs.h"
#include "wire.h"
#include "wire/adapted/crypto.h"
#include "wire/error.h"
#include "wire/json.h"
Expand Down Expand Up @@ -195,6 +196,29 @@ namespace lws
convert_address(address, self.address);
}

namespace rpc
{
namespace
{
constexpr const char* map_daemon_state[] = {"ok", "no_connections", "synchronizing", "unavailable"};
constexpr const char* map_network_type[] = {"main", "test", "stage", "fake"};
}
WIRE_DEFINE_ENUM(daemon_state, map_daemon_state);
WIRE_DEFINE_ENUM(network_type, map_network_type);
}

void rpc::write_bytes(wire::json_writer& dest, const daemon_status_response& self)
{
wire::object(dest,
WIRE_FIELD(outgoing_connections_count),
WIRE_FIELD(incoming_connections_count),
WIRE_FIELD(height),
WIRE_FIELD(target_height),
WIRE_FIELD(network),
WIRE_FIELD(state)
);
}

void rpc::write_bytes(wire::json_writer& dest, const new_subaddrs_response& self)
{
wire::object(dest, WIRE_FIELD(new_subaddrs), WIRE_FIELD(all_subaddrs));
Expand Down
44 changes: 42 additions & 2 deletions src/rpc/light_wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
#include <utility>
#include <vector>

#include "common/expect.h" // monero/src
#include "crypto/crypto.h" // monero/src
#include "common/expect.h" // monero/src
#include "cryptonote_config.h" // monero/src
#include "cryptonote_basic/difficulty.h" // monero/src
#include "crypto/crypto.h" // monero/src
#include "db/data.h"
#include "rpc/rates.h"
#include "util/fwd.h"
Expand Down Expand Up @@ -65,6 +67,44 @@ namespace rpc
void read_bytes(wire::json_reader&, account_credentials&);


enum class daemon_state : std::uint8_t
{
ok = 0,
no_connections,
synchronizing,
unavailable
};
WIRE_DECLARE_ENUM(daemon_state);

enum class network_type : std::uint8_t
{
main = 0,
test,
stage,
fake
};
WIRE_DECLARE_ENUM(network_type);

struct daemon_status_request
{
daemon_status_request() = delete;
};
inline void read_bytes(const wire::reader&, const daemon_status_request&)
{}

struct daemon_status_response
{
daemon_status_response() = delete;
std::uint64_t outgoing_connections_count;
std::uint64_t incoming_connections_count;
std::uint64_t height;
std::uint64_t target_height;
network_type network;
daemon_state state;
};
void write_bytes(wire::json_writer&, const daemon_status_response&);


struct new_subaddrs_response
{
new_subaddrs_response() = delete;
Expand Down
Loading