aboutsummaryrefslogtreecommitdiff
path: root/src/rpc/core_rpc_server.cpp
diff options
context:
space:
mode:
authorxiphon <xiphon@protonmail.com>2019-08-27 12:01:49 +0000
committerxiphon <xiphon@protonmail.com>2019-08-27 12:01:49 +0000
commit082730b6e569e155988df5c5b376c45fba56b396 (patch)
tree171ade8114b4acd15ca28c91aef6ef80f0706b5c /src/rpc/core_rpc_server.cpp
parentMerge pull request #5827 (diff)
downloadmonero-082730b6e569e155988df5c5b376c45fba56b396.tar.xz
daemon: automatic public nodes discovering and bootstrap daemon switching
Diffstat (limited to 'src/rpc/core_rpc_server.cpp')
-rw-r--r--src/rpc/core_rpc_server.cpp110
1 files changed, 77 insertions, 33 deletions
diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp
index 73138686d..4c07bddcb 100644
--- a/src/rpc/core_rpc_server.cpp
+++ b/src/rpc/core_rpc_server.cpp
@@ -116,20 +116,59 @@ namespace cryptonote
return set_bootstrap_daemon(address, credentials);
}
//------------------------------------------------------------------------------------------------------------------------------
+ boost::optional<std::string> core_rpc_server::get_random_public_node()
+ {
+ COMMAND_RPC_GET_PUBLIC_NODES::request request;
+ COMMAND_RPC_GET_PUBLIC_NODES::response response;
+
+ request.gray = true;
+ request.white = true;
+ if (!on_get_public_nodes(request, response) || response.status != CORE_RPC_STATUS_OK)
+ {
+ return boost::none;
+ }
+
+ const auto get_random_node_address = [](const std::vector<public_node>& public_nodes) -> std::string {
+ const auto& random_node = public_nodes[crypto::rand_idx(public_nodes.size())];
+ const auto address = random_node.host + ":" + std::to_string(random_node.rpc_port);
+ return address;
+ };
+
+ if (!response.white.empty())
+ {
+ return get_random_node_address(response.white);
+ }
+
+ MDEBUG("No white public node found, checking gray peers");
+
+ if (!response.gray.empty())
+ {
+ return get_random_node_address(response.gray);
+ }
+
+ MERROR("Failed to find any suitable public node");
+
+ return boost::none;
+ }
+ //------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server::set_bootstrap_daemon(const std::string &address, const boost::optional<epee::net_utils::http::login> &credentials)
{
boost::unique_lock<boost::shared_mutex> lock(m_bootstrap_daemon_mutex);
- if (!address.empty())
+ if (address.empty())
{
- if (!m_http_client.set_server(address, credentials, epee::net_utils::ssl_support_t::e_ssl_support_autodetect))
- {
- return false;
- }
+ m_bootstrap_daemon.reset(nullptr);
+ }
+ else if (address == "auto")
+ {
+ m_bootstrap_daemon.reset(new bootstrap_daemon([this]{ return get_random_public_node(); }));
+ }
+ else
+ {
+ m_bootstrap_daemon.reset(new bootstrap_daemon(address, credentials));
}
- m_bootstrap_daemon_address = address;
- m_should_use_bootstrap_daemon = !m_bootstrap_daemon_address.empty();
+ m_should_use_bootstrap_daemon = m_bootstrap_daemon.get() != nullptr;
return true;
}
@@ -220,7 +259,10 @@ namespace cryptonote
{
{
boost::shared_lock<boost::shared_mutex> lock(m_bootstrap_daemon_mutex);
- res.bootstrap_daemon_address = m_bootstrap_daemon_address;
+ if (m_bootstrap_daemon.get() != nullptr)
+ {
+ res.bootstrap_daemon_address = m_bootstrap_daemon->address();
+ }
}
crypto::hash top_hash;
m_core.get_blockchain_top(res.height_without_bootstrap, top_hash);
@@ -269,7 +311,10 @@ namespace cryptonote
else
{
boost::shared_lock<boost::shared_mutex> lock(m_bootstrap_daemon_mutex);
- res.bootstrap_daemon_address = m_bootstrap_daemon_address;
+ if (m_bootstrap_daemon.get() != nullptr)
+ {
+ res.bootstrap_daemon_address = m_bootstrap_daemon->address();
+ }
res.was_bootstrap_ever_used = m_was_bootstrap_ever_used;
}
res.database_size = m_core.get_blockchain_storage().get_db().get_database_size();
@@ -1593,8 +1638,10 @@ namespace cryptonote
boost::upgrade_lock<boost::shared_mutex> upgrade_lock(m_bootstrap_daemon_mutex);
- if (m_bootstrap_daemon_address.empty())
+ if (m_bootstrap_daemon.get() == nullptr)
+ {
return false;
+ }
if (!m_should_use_bootstrap_daemon)
{
@@ -1610,42 +1657,38 @@ namespace cryptonote
m_bootstrap_height_check_time = current_time;
}
- uint64_t top_height;
- crypto::hash top_hash;
- m_core.get_blockchain_top(top_height, top_hash);
- ++top_height; // turn top block height into blockchain height
+ boost::optional<uint64_t> bootstrap_daemon_height = m_bootstrap_daemon->get_height();
+ if (!bootstrap_daemon_height)
+ {
+ MERROR("Failed to fetch bootstrap daemon height");
+ return false;
+ }
- // query bootstrap daemon's height
- cryptonote::COMMAND_RPC_GET_HEIGHT::request getheight_req;
- cryptonote::COMMAND_RPC_GET_HEIGHT::response getheight_res;
- bool ok = epee::net_utils::invoke_http_json("/getheight", getheight_req, getheight_res, m_http_client);
- ok = ok && getheight_res.status == CORE_RPC_STATUS_OK;
+ uint64_t target_height = m_core.get_target_blockchain_height();
+ if (*bootstrap_daemon_height < target_height)
+ {
+ MINFO("Bootstrap daemon is out of sync");
+ return m_bootstrap_daemon->handle_result(false);
+ }
- m_should_use_bootstrap_daemon = ok && top_height + 10 < getheight_res.height;
- MINFO((m_should_use_bootstrap_daemon ? "Using" : "Not using") << " the bootstrap daemon (our height: " << top_height << ", bootstrap daemon's height: " << (ok ? getheight_res.height : 0) << ")");
+ uint64_t top_height = m_core.get_current_blockchain_height();
+ m_should_use_bootstrap_daemon = top_height + 10 < *bootstrap_daemon_height;
+ MINFO((m_should_use_bootstrap_daemon ? "Using" : "Not using") << " the bootstrap daemon (our height: " << top_height << ", bootstrap daemon's height: " << *bootstrap_daemon_height << ")");
}
if (!m_should_use_bootstrap_daemon)
return false;
if (mode == invoke_http_mode::JON)
{
- r = epee::net_utils::invoke_http_json(command_name, req, res, m_http_client);
+ r = m_bootstrap_daemon->invoke_http_json(command_name, req, res);
}
else if (mode == invoke_http_mode::BIN)
{
- r = epee::net_utils::invoke_http_bin(command_name, req, res, m_http_client);
+ r = m_bootstrap_daemon->invoke_http_bin(command_name, req, res);
}
else if (mode == invoke_http_mode::JON_RPC)
{
- epee::json_rpc::request<typename COMMAND_TYPE::request> json_req = AUTO_VAL_INIT(json_req);
- epee::json_rpc::response<typename COMMAND_TYPE::response, std::string> json_resp = AUTO_VAL_INIT(json_resp);
- json_req.jsonrpc = "2.0";
- json_req.id = epee::serialization::storage_entry(0);
- json_req.method = command_name;
- json_req.params = req;
- r = net_utils::invoke_http_json("/json_rpc", json_req, json_resp, m_http_client);
- if (r)
- res = json_resp.result;
+ r = m_bootstrap_daemon->invoke_http_json_rpc(command_name, req, res);
}
else
{
@@ -2617,7 +2660,8 @@ namespace cryptonote
const command_line::arg_descriptor<std::string> core_rpc_server::arg_bootstrap_daemon_address = {
"bootstrap-daemon-address"
- , "URL of a 'bootstrap' remote daemon that the connected wallets can use while this daemon is still not fully synced"
+ , "URL of a 'bootstrap' remote daemon that the connected wallets can use while this daemon is still not fully synced.\n"
+ "Use 'auto' to enable automatic public nodes discovering and bootstrap daemon switching"
, ""
};