Skip to content

Commit

Permalink
Periodically re-check addresses of seed nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
abitmore committed May 1, 2020
1 parent 864cf3e commit 30648f2
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
62 changes: 62 additions & 0 deletions libraries/net/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,43 @@ namespace graphene { namespace net { namespace detail {
// _retrigger_connect_loop_promise->set_value();
}

void node_impl::update_seed_nodes_task()
{
VERIFY_CORRECT_THREAD();

try
{
dlog("Starting an iteration of update_seed_nodes loop.");
for( const std::string& endpoint_string : _seed_nodes )
{
resolve_seed_node_and_add( endpoint_string );
}
dlog("Done an iteration of update_seed_nodes loop.");
}
catch (const fc::canceled_exception&)
{
throw;
}
FC_CAPTURE_AND_LOG( (_seed_nodes) )

schedule_next_update_seed_nodes_task();
}

void node_impl::schedule_next_update_seed_nodes_task()
{
VERIFY_CORRECT_THREAD();

if( _node_is_shutting_down )
return;

if( _update_seed_nodes_loop_done.valid() && _update_seed_nodes_loop_done.canceled() )
return;

_update_seed_nodes_loop_done = fc::schedule( [this]() { update_seed_nodes_task(); },
fc::time_point::now() + fc::hours(3),
"update_seed_nodes_loop" );
}

bool node_impl::have_already_received_sync_item( const item_hash_t& item_hash )
{
VERIFY_CORRECT_THREAD();
Expand Down Expand Up @@ -3762,6 +3799,20 @@ namespace graphene { namespace net { namespace detail {
wlog( "Exception thrown while terminating Fetch updated peer lists loop, ignoring" );
}

try
{
_update_seed_nodes_loop_done.cancel_and_wait("node_impl::close()");
dlog("Update seed nodes loop terminated");
}
catch ( const fc::exception& e )
{
wlog( "Exception thrown while terminating Update seed nodes loop, ignoring: ${e}", ("e", e) );
}
catch (...)
{
wlog( "Exception thrown while terminating Update seed nodes loop, ignoring" );
}

try
{
_bandwidth_monitor_loop_done.cancel_and_wait("node_impl::close()");
Expand Down Expand Up @@ -4166,6 +4217,7 @@ namespace graphene { namespace net { namespace detail {

assert(!_accept_loop_complete.valid() &&
!_p2p_network_connect_loop_done.valid() &&
!_update_seed_nodes_loop_done.valid() &&
!_fetch_sync_items_loop_done.valid() &&
!_fetch_item_loop_done.valid() &&
!_advertise_inventory_loop_done.valid() &&
Expand All @@ -4183,6 +4235,7 @@ namespace graphene { namespace net { namespace detail {
_fetch_updated_peer_lists_loop_done = fc::async([=](){ fetch_updated_peer_lists_loop(); }, "fetch_updated_peer_lists_loop");
_bandwidth_monitor_loop_done = fc::async([=](){ bandwidth_monitor_loop(); }, "bandwidth_monitor_loop");
_dump_node_status_task_done = fc::async([=](){ dump_node_status_task(); }, "dump_node_status_task");
schedule_next_update_seed_nodes_task();
}

void node_impl::add_node(const fc::ip::endpoint& ep)
Expand All @@ -4201,9 +4254,17 @@ namespace graphene { namespace net { namespace detail {
}

void node_impl::add_seed_node(const std::string& endpoint_string)
{
VERIFY_CORRECT_THREAD();
_seed_nodes.insert( endpoint_string );
resolve_seed_node_and_add( endpoint_string );
}

void node_impl::resolve_seed_node_and_add(const std::string& endpoint_string)
{
VERIFY_CORRECT_THREAD();
std::vector<fc::ip::endpoint> endpoints;
ilog("Resolving seed node ${endpoint}", ("endpoint", endpoint_string));
try
{
endpoints = graphene::net::node::resolve_string_to_ip_endpoints(endpoint_string);
Expand Down Expand Up @@ -5125,6 +5186,7 @@ namespace graphene { namespace net { namespace detail {

} // end namespace detail

// TODO move this function to impl class
std::vector<fc::ip::endpoint> node::resolve_string_to_ip_endpoints(const std::string& in)
{
try
Expand Down
9 changes: 9 additions & 0 deletions libraries/net/node_impl.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,14 @@ class node_impl : public peer_connection_delegate

std::list<fc::future<void> > _handle_message_calls_in_progress;

/// used by the task that checks whether addresses of seed nodes have been updated
// @{
boost::container::flat_set<std::string> _seed_nodes;
fc::future<void> _update_seed_nodes_loop_done;
void update_seed_nodes_task();
void schedule_next_update_seed_nodes_task();
// @}

node_impl(const std::string& user_agent);
virtual ~node_impl();

Expand Down Expand Up @@ -483,6 +491,7 @@ class node_impl : public peer_connection_delegate
void connect_to_p2p_network();
void add_node( const fc::ip::endpoint& ep );
void add_seed_node( const std::string& seed_string );
void resolve_seed_node_and_add( const std::string& seed_string );
void initiate_connect_to(const peer_connection_ptr& peer);
void connect_to_endpoint(const fc::ip::endpoint& ep);
void listen_on_endpoint(const fc::ip::endpoint& ep , bool wait_if_not_available);
Expand Down

0 comments on commit 30648f2

Please sign in to comment.