Merge pull request #6357

42a7a4d daemon: auto public nodes - cache and prioritize most stable nodes (xiphon)
This commit is contained in:
luigi1111
2020-04-04 12:59:23 -05:00
9 changed files with 477 additions and 43 deletions

View File

@@ -178,7 +178,7 @@ namespace cryptonote
return set_bootstrap_daemon(address, credentials);
}
//------------------------------------------------------------------------------------------------------------------------------
boost::optional<std::string> core_rpc_server::get_random_public_node()
std::map<std::string, bool> core_rpc_server::get_public_nodes(uint32_t credits_per_hash_threshold/* = 0*/)
{
COMMAND_RPC_GET_PUBLIC_NODES::request request;
COMMAND_RPC_GET_PUBLIC_NODES::response response;
@@ -187,47 +187,51 @@ namespace cryptonote
request.white = true;
if (!on_get_public_nodes(request, response) || response.status != CORE_RPC_STATUS_OK)
{
return boost::none;
return {};
}
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;
std::map<std::string, bool> result;
const auto append = [&result, &credits_per_hash_threshold](const std::vector<public_node> &nodes, bool white) {
for (const auto &node : nodes)
{
const bool rpc_payment_enabled = credits_per_hash_threshold > 0;
const bool node_rpc_payment_enabled = node.rpc_credits_per_hash > 0;
if (!node_rpc_payment_enabled ||
(rpc_payment_enabled && node.rpc_credits_per_hash >= credits_per_hash_threshold))
{
result.insert(std::make_pair(node.host + ":" + std::to_string(node.rpc_port), white));
}
}
};
if (!response.white.empty())
{
return get_random_node_address(response.white);
}
append(response.white, true);
append(response.gray, false);
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;
return result;
}
//------------------------------------------------------------------------------------------------------------------------------
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);
constexpr const uint32_t credits_per_hash_threshold = 0;
constexpr const bool rpc_payment_enabled = credits_per_hash_threshold != 0;
if (address.empty())
{
m_bootstrap_daemon.reset(nullptr);
}
else if (address == "auto")
{
m_bootstrap_daemon.reset(new bootstrap_daemon([this]{ return get_random_public_node(); }));
auto get_nodes = [this, credits_per_hash_threshold]() {
return get_public_nodes(credits_per_hash_threshold);
};
m_bootstrap_daemon.reset(new bootstrap_daemon(std::move(get_nodes), rpc_payment_enabled));
}
else
{
m_bootstrap_daemon.reset(new bootstrap_daemon(address, credentials));
m_bootstrap_daemon.reset(new bootstrap_daemon(address, credentials, rpc_payment_enabled));
}
m_should_use_bootstrap_daemon = m_bootstrap_daemon.get() != nullptr;
@@ -1941,7 +1945,7 @@ namespace cryptonote
if (*bootstrap_daemon_height < target_height)
{
MINFO("Bootstrap daemon is out of sync");
return m_bootstrap_daemon->handle_result(false);
return m_bootstrap_daemon->handle_result(false, {});
}
uint64_t top_height = m_core.get_current_blockchain_height();