blob: 6f8426ee77cdd6beb2573558ca59d3407f37cf2e (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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;
}
}
|