forked from wownero/wownero
parent
cdfa2e58df
commit
082730b6e5
@ -0,0 +1,95 @@
|
||||
#include "bootstrap_daemon.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include "crypto/crypto.h"
|
||||
#include "cryptonote_core/cryptonote_core.h"
|
||||
#include "misc_log_ex.h"
|
||||
|
||||
#undef MONERO_DEFAULT_LOG_CATEGORY
|
||||
#define MONERO_DEFAULT_LOG_CATEGORY "daemon.rpc.bootstrap_daemon"
|
||||
|
||||
namespace cryptonote
|
||||
{
|
||||
|
||||
bootstrap_daemon::bootstrap_daemon(std::function<boost::optional<std::string>()> get_next_public_node) noexcept
|
||||
: m_get_next_public_node(get_next_public_node)
|
||||
{
|
||||
}
|
||||
|
||||
bootstrap_daemon::bootstrap_daemon(const std::string &address, const boost::optional<epee::net_utils::http::login> &credentials)
|
||||
: bootstrap_daemon(nullptr)
|
||||
{
|
||||
if (!set_server(address, credentials))
|
||||
{
|
||||
throw std::runtime_error("invalid bootstrap daemon address or credentials");
|
||||
}
|
||||
}
|
||||
|
||||
std::string bootstrap_daemon::address() const noexcept
|
||||
{
|
||||
const auto& host = m_http_client.get_host();
|
||||
if (host.empty())
|
||||
{
|
||||
return std::string();
|
||||
}
|
||||
return host + ":" + m_http_client.get_port();
|
||||
}
|
||||
|
||||
boost::optional<uint64_t> bootstrap_daemon::get_height()
|
||||
{
|
||||
cryptonote::COMMAND_RPC_GET_HEIGHT::request req;
|
||||
cryptonote::COMMAND_RPC_GET_HEIGHT::response res;
|
||||
|
||||
if (!invoke_http_json("/getheight", req, res))
|
||||
{
|
||||
return boost::none;
|
||||
}
|
||||
|
||||
if (res.status != CORE_RPC_STATUS_OK)
|
||||
{
|
||||
return boost::none;
|
||||
}
|
||||
|
||||
return res.height;
|
||||
}
|
||||
|
||||
bool bootstrap_daemon::handle_result(bool success)
|
||||
{
|
||||
if (!success && m_get_next_public_node)
|
||||
{
|
||||
m_http_client.disconnect();
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool bootstrap_daemon::set_server(const std::string &address, const boost::optional<epee::net_utils::http::login> &credentials /* = boost::none */)
|
||||
{
|
||||
if (!m_http_client.set_server(address, credentials))
|
||||
{
|
||||
MERROR("Failed to set bootstrap daemon address " << address);
|
||||
return false;
|
||||
}
|
||||
|
||||
MINFO("Changed bootstrap daemon address to " << address);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool bootstrap_daemon::switch_server_if_needed()
|
||||
{
|
||||
if (!m_get_next_public_node || m_http_client.is_connected())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
const boost::optional<std::string> address = m_get_next_public_node();
|
||||
if (address) {
|
||||
return set_server(*address);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/optional/optional.hpp>
|
||||
#include <boost/utility/string_ref.hpp>
|
||||
|
||||
#include "net/http_client.h"
|
||||
#include "storages/http_abstract_invoke.h"
|
||||
|
||||
namespace cryptonote
|
||||
{
|
||||
|
||||
class bootstrap_daemon
|
||||
{
|
||||
public:
|
||||
bootstrap_daemon(std::function<boost::optional<std::string>()> get_next_public_node) noexcept;
|
||||
bootstrap_daemon(const std::string &address, const boost::optional<epee::net_utils::http::login> &credentials);
|
||||
|
||||
std::string address() const noexcept;
|
||||
boost::optional<uint64_t> get_height();
|
||||
bool handle_result(bool success);
|
||||
|
||||
template <class t_request, class t_response>
|
||||
bool invoke_http_json(const boost::string_ref uri, const t_request &out_struct, t_response &result_struct)
|
||||
{
|
||||
if (!switch_server_if_needed())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return handle_result(epee::net_utils::invoke_http_json(uri, out_struct, result_struct, m_http_client));
|
||||
}
|
||||
|
||||
template <class t_request, class t_response>
|
||||
bool invoke_http_bin(const boost::string_ref uri, const t_request &out_struct, t_response &result_struct)
|
||||
{
|
||||
if (!switch_server_if_needed())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return handle_result(epee::net_utils::invoke_http_bin(uri, out_struct, result_struct, m_http_client));
|
||||
}
|
||||
|
||||
template <class t_request, class t_response>
|
||||
bool invoke_http_json_rpc(const boost::string_ref command_name, const t_request &out_struct, t_response &result_struct)
|
||||
{
|
||||
if (!switch_server_if_needed())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return handle_result(epee::net_utils::invoke_http_json_rpc("/json_rpc", std::string(command_name.begin(), command_name.end()), out_struct, result_struct, m_http_client));
|
||||
}
|
||||
|
||||
private:
|
||||
bool set_server(const std::string &address, const boost::optional<epee::net_utils::http::login> &credentials = boost::none);
|
||||
bool switch_server_if_needed();
|
||||
|
||||
private:
|
||||
epee::net_utils::http::http_simple_client m_http_client;
|
||||
std::function<boost::optional<std::string>()> m_get_next_public_node;
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in new issue