p2p: startup speedup, init seed nodes on first 'connect_to_seed()'

This commit is contained in:
xiphon 2020-04-10 02:24:00 +00:00
parent ecb8cc9df4
commit 145be6dbdb
2 changed files with 139 additions and 111 deletions

View file

@ -384,6 +384,7 @@ namespace nodetool
bool is_addr_recently_failed(const epee::net_utils::network_address& addr); bool is_addr_recently_failed(const epee::net_utils::network_address& addr);
bool is_priority_node(const epee::net_utils::network_address& na); bool is_priority_node(const epee::net_utils::network_address& na);
std::set<std::string> get_seed_nodes(cryptonote::network_type nettype) const; std::set<std::string> get_seed_nodes(cryptonote::network_type nettype) const;
std::set<std::string> get_seed_nodes();
bool connect_to_seed(); bool connect_to_seed();
template <class Container> template <class Container>
@ -467,7 +468,9 @@ namespace nodetool
std::list<epee::net_utils::network_address> m_priority_peers; std::list<epee::net_utils::network_address> m_priority_peers;
std::vector<epee::net_utils::network_address> m_exclusive_peers; std::vector<epee::net_utils::network_address> m_exclusive_peers;
std::vector<epee::net_utils::network_address> m_seed_nodes; std::vector<epee::net_utils::network_address> m_seed_nodes;
bool m_fallback_seed_nodes_added; bool m_seed_nodes_initialized = false;
boost::shared_mutex m_seed_nodes_lock;
std::atomic_flag m_fallback_seed_nodes_added;
std::vector<nodetool::peerlist_entry> m_command_line_peers; std::vector<nodetool::peerlist_entry> m_command_line_peers;
uint64_t m_peer_livetime; uint64_t m_peer_livetime;
//keep connections to initiate some interactions //keep connections to initiate some interactions

View file

@ -435,6 +435,8 @@ namespace nodetool
if (command_line::has_arg(vm, arg_p2p_seed_node)) if (command_line::has_arg(vm, arg_p2p_seed_node))
{ {
boost::unique_lock<boost::shared_mutex> lock(m_seed_nodes_lock);
if (!parse_peers_and_add_to_container(vm, arg_p2p_seed_node, m_seed_nodes)) if (!parse_peers_and_add_to_container(vm, arg_p2p_seed_node, m_seed_nodes))
return false; return false;
} }
@ -633,40 +635,23 @@ namespace nodetool
} }
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
template<class t_payload_net_handler> template<class t_payload_net_handler>
typename node_server<t_payload_net_handler>::network_zone& node_server<t_payload_net_handler>::add_zone(const epee::net_utils::zone zone) std::set<std::string> node_server<t_payload_net_handler>::get_seed_nodes()
{ {
const auto zone_ = m_network_zones.lower_bound(zone); if (!m_exclusive_peers.empty() || m_offline)
if (zone_ != m_network_zones.end() && zone_->first == zone) {
return zone_->second; return {};
network_zone& public_zone = m_network_zones[epee::net_utils::zone::public_];
return m_network_zones.emplace_hint(zone_, std::piecewise_construct, std::make_tuple(zone), std::tie(public_zone.m_net_server.get_io_service()))->second;
} }
//-----------------------------------------------------------------------------------
template<class t_payload_net_handler>
bool node_server<t_payload_net_handler>::init(const boost::program_options::variables_map& vm)
{
std::set<std::string> full_addrs;
bool res = handle_command_line(vm);
CHECK_AND_ASSERT_MES(res, false, "Failed to handle command line");
m_fallback_seed_nodes_added = false;
if (m_nettype == cryptonote::TESTNET) if (m_nettype == cryptonote::TESTNET)
{ {
memcpy(&m_network_id, &::config::testnet::NETWORK_ID, 16); return get_seed_nodes(cryptonote::TESTNET);
full_addrs = get_seed_nodes(cryptonote::TESTNET);
} }
else if (m_nettype == cryptonote::STAGENET) if (m_nettype == cryptonote::STAGENET)
{ {
memcpy(&m_network_id, &::config::stagenet::NETWORK_ID, 16); return get_seed_nodes(cryptonote::STAGENET);
full_addrs = get_seed_nodes(cryptonote::STAGENET);
} }
else
{ std::set<std::string> full_addrs;
memcpy(&m_network_id, &::config::NETWORK_ID, 16);
if (m_exclusive_peers.empty() && !m_offline)
{
// for each hostname in the seed nodes list, attempt to DNS resolve and // for each hostname in the seed nodes list, attempt to DNS resolve and
// add the result addresses as seed nodes // add the result addresses as seed nodes
// TODO: at some point add IPv6 support, but that won't be relevant // TODO: at some point add IPv6 support, but that won't be relevant
@ -752,17 +737,41 @@ namespace nodetool
for (const auto &peer: get_seed_nodes(cryptonote::MAINNET)) for (const auto &peer: get_seed_nodes(cryptonote::MAINNET))
full_addrs.insert(peer); full_addrs.insert(peer);
m_fallback_seed_nodes_added = true; m_fallback_seed_nodes_added.test_and_set();
}
}
} }
for (const auto& full_addr : full_addrs) return full_addrs;
{ }
MDEBUG("Seed node: " << full_addr); //-----------------------------------------------------------------------------------
append_net_address(m_seed_nodes, full_addr, cryptonote::get_config(m_nettype).P2P_DEFAULT_PORT); template<class t_payload_net_handler>
typename node_server<t_payload_net_handler>::network_zone& node_server<t_payload_net_handler>::add_zone(const epee::net_utils::zone zone)
{
const auto zone_ = m_network_zones.lower_bound(zone);
if (zone_ != m_network_zones.end() && zone_->first == zone)
return zone_->second;
network_zone& public_zone = m_network_zones[epee::net_utils::zone::public_];
return m_network_zones.emplace_hint(zone_, std::piecewise_construct, std::make_tuple(zone), std::tie(public_zone.m_net_server.get_io_service()))->second;
}
//-----------------------------------------------------------------------------------
template<class t_payload_net_handler>
bool node_server<t_payload_net_handler>::init(const boost::program_options::variables_map& vm)
{
bool res = handle_command_line(vm);
CHECK_AND_ASSERT_MES(res, false, "Failed to handle command line");
if (m_nettype == cryptonote::TESTNET)
{
memcpy(&m_network_id, &::config::testnet::NETWORK_ID, 16);
}
else if (m_nettype == cryptonote::STAGENET)
{
memcpy(&m_network_id, &::config::stagenet::NETWORK_ID, 16);
}
else
{
memcpy(&m_network_id, &::config::NETWORK_ID, 16);
} }
MDEBUG("Number of seed nodes: " << m_seed_nodes.size());
m_config_folder = command_line::get_arg(vm, cryptonote::arg_data_dir); m_config_folder = command_line::get_arg(vm, cryptonote::arg_data_dir);
network_zone& public_zone = m_network_zones.at(epee::net_utils::zone::public_); network_zone& public_zone = m_network_zones.at(epee::net_utils::zone::public_);
@ -1539,6 +1548,20 @@ namespace nodetool
template<class t_payload_net_handler> template<class t_payload_net_handler>
bool node_server<t_payload_net_handler>::connect_to_seed() bool node_server<t_payload_net_handler>::connect_to_seed()
{ {
boost::upgrade_lock<boost::shared_mutex> seed_nodes_upgrade_lock(m_seed_nodes_lock);
if (!m_seed_nodes_initialized)
{
boost::upgrade_to_unique_lock<boost::shared_mutex> seed_nodes_lock(seed_nodes_upgrade_lock);
m_seed_nodes_initialized = true;
for (const auto& full_addr : get_seed_nodes())
{
MDEBUG("Seed node: " << full_addr);
append_net_address(m_seed_nodes, full_addr, cryptonote::get_config(m_nettype).P2P_DEFAULT_PORT);
}
MDEBUG("Number of seed nodes: " << m_seed_nodes.size());
}
if (m_seed_nodes.empty() || m_offline || !m_exclusive_peers.empty()) if (m_seed_nodes.empty() || m_offline || !m_exclusive_peers.empty())
return true; return true;
@ -1559,16 +1582,19 @@ namespace nodetool
break; break;
if(++try_count > m_seed_nodes.size()) if(++try_count > m_seed_nodes.size())
{ {
if (!m_fallback_seed_nodes_added) if (!m_fallback_seed_nodes_added.test_and_set())
{ {
MWARNING("Failed to connect to any of seed peers, trying fallback seeds"); MWARNING("Failed to connect to any of seed peers, trying fallback seeds");
current_index = m_seed_nodes.size() - 1; current_index = m_seed_nodes.size() - 1;
{
boost::upgrade_to_unique_lock<boost::shared_mutex> seed_nodes_lock(seed_nodes_upgrade_lock);
for (const auto &peer: get_seed_nodes(m_nettype)) for (const auto &peer: get_seed_nodes(m_nettype))
{ {
MDEBUG("Fallback seed node: " << peer); MDEBUG("Fallback seed node: " << peer);
append_net_address(m_seed_nodes, peer, cryptonote::get_config(m_nettype).P2P_DEFAULT_PORT); append_net_address(m_seed_nodes, peer, cryptonote::get_config(m_nettype).P2P_DEFAULT_PORT);
} }
m_fallback_seed_nodes_added = true; }
if (current_index == m_seed_nodes.size() - 1) if (current_index == m_seed_nodes.size() - 1)
{ {
MWARNING("No fallback seeds, continuing without seeds"); MWARNING("No fallback seeds, continuing without seeds");
@ -1602,9 +1628,8 @@ namespace nodetool
// Only have seeds in the public zone right now. // Only have seeds in the public zone right now.
size_t start_conn_count = get_public_outgoing_connections_count(); size_t start_conn_count = get_public_outgoing_connections_count();
if(!get_public_white_peers_count() && m_seed_nodes.size()) if(!get_public_white_peers_count() && !connect_to_seed())
{ {
if (!connect_to_seed())
return false; return false;
} }