public async Task AddPeer_Test() { var endpoint = "127.0.0.1:5000"; var result = await _networkService.AddPeerAsync(endpoint); result.ShouldBeTrue(); }
public async Task AddInvalidPeer_Test() { var endpoint = "ipv6:192.168.197.54"; var result = await _networkService.AddPeerAsync(endpoint); result.ShouldBeFalse(); }
internal async Task ProcessPeerDiscoveryJob() { try { var newNodes = await _peerDiscoveryService.DiscoverNodesAsync(); if (newNodes == null || newNodes.Nodes.Count <= 0) { Logger.LogDebug("Discovery: no new nodes discovered"); return; } Logger.LogDebug($"Discovery: new nodes discovered : {newNodes}."); foreach (var node in newNodes.Nodes) { if (_networkService.IsPeerPoolFull()) { Logger.LogDebug("Discovery: Peer pool is full, aborting add."); break; } await _networkService.AddPeerAsync(node.Endpoint); } } catch (Exception e) { Logger.LogError(e, "Exception in discovery worker."); } }
protected override async void DoWork() { try { var newNodes = await _peerDiscoveryService.DiscoverNodesAsync(); if (newNodes == null || newNodes.Nodes.Count <= 0) { Logger.LogDebug("Discovery: no new nodes discovered"); return; } Logger.LogDebug($"Discovery: new nodes discovered : {newNodes}."); foreach (var node in newNodes.Nodes) { if (_peerPool.PeerCount >= NetworkOptions.MaxPeers) { Logger.LogDebug($"Discovery: Max peers reached {_peerPool.PeerCount}, aborting add."); break; } await _networkService.AddPeerAsync(node.Endpoint); } } catch (Exception e) { Logger.LogError(e, "Exception in discovery worker."); } }
public async Task AddPeerAsync_CannotAddBlacklistedPeer() { var endpoint = IpEndPointHelper.Parse("127.0.0.1:5000"); var address = endpoint.Address; _blackListProvider.AddIpToBlackList(address); (await _networkService.AddPeerAsync(endpoint.ToString())).ShouldBeFalse(); }
public async Task AddPeerAsync_CannotAddBlacklistedPeer() { AElfPeerEndpointHelper.TryParse("127.0.0.1:5000", out var endpoint); var host = endpoint.Host; _blackListProvider.AddHostToBlackList(host); (await _networkService.AddPeerAsync(endpoint.ToString())).ShouldBeFalse(); }
internal async Task ProcessPeerDiscoveryJobAsync() { await _peerDiscoveryService.RefreshNodeAsync(); await _peerDiscoveryService.DiscoverNodesAsync(); if (_networkService.IsPeerPoolFull()) { return; } var nodes = await _peerDiscoveryService.GetNodesAsync(10); foreach (var node in nodes.Nodes) { try { var reconnectingPeer = _reconnectionService.GetReconnectingPeer(node.Endpoint); if (reconnectingPeer != null) { Logger.LogDebug($"Peer {node.Endpoint} is already in the reconnection queue."); continue; } if (_networkService.IsPeerPoolFull()) { Logger.LogTrace("Peer pool is full, aborting add."); break; } await _networkService.AddPeerAsync(node.Endpoint); } catch (Exception e) { Logger.LogError(e, $"Exception connecting to {node.Endpoint}."); } } }
internal async Task ProcessPeerDiscoveryJob() { var newNodes = await _peerDiscoveryService.DiscoverNodesAsync(); if (newNodes == null || newNodes.Nodes.Count <= 0) { Logger.LogDebug("No new nodes discovered"); return; } Logger.LogDebug($"New nodes discovered : {newNodes}."); foreach (var node in newNodes.Nodes) { try { var reconnectingPeer = _reconnectionService.GetReconnectingPeer(node.Endpoint); if (reconnectingPeer != null) { Logger.LogDebug($"Peer {node.Endpoint} is already in the reconnection queue."); continue; } if (_networkService.IsPeerPoolFull()) { Logger.LogDebug("Peer pool is full, aborting add."); break; } await _networkService.AddPeerAsync(node.Endpoint); } catch (Exception e) { Logger.LogError(e, $"Exception connecting to {node.Endpoint}."); } } }
/// <summary> /// Attempts to add a node to the connected network nodes /// </summary> /// <returns>true/false</returns> public async Task <bool> AddPeerAsync(AddPeerInput input) { return(await _networkService.AddPeerAsync(input.Address)); }
internal async Task DoReconnectionJobAsync() { CheckNtpClockDrift(); await _networkService.CheckPeersHealthAsync(); var peersToConnect = _reconnectionService.GetPeersReadyForReconnection(TimestampHelper.GetUtcNow()); if (peersToConnect.Count <= 0) { return; } foreach (var peerToConnect in peersToConnect) { string peerEndpoint = peerToConnect.Endpoint; if (!AElfPeerEndpointHelper.TryParse(peerEndpoint, out var parsed)) { if (!_reconnectionService.CancelReconnection(peerEndpoint)) { Logger.LogWarning($"Invalid {peerEndpoint}."); } continue; } // check that we haven't already reconnected to this node if (_peerPool.FindPeerByEndpoint(parsed) != null) { Logger.LogDebug($"Peer {peerEndpoint} already in the pool, no need to reconnect."); if (!_reconnectionService.CancelReconnection(peerEndpoint)) { Logger.LogDebug($"Could not find to {peerEndpoint}."); } continue; } Logger.LogDebug($"Starting reconnection to {peerToConnect.Endpoint}."); var connected = false; try { connected = await _networkService.AddPeerAsync(peerEndpoint); } catch (Exception ex) { // down the stack the AddPeerAsync rethrows any exception, // in order to continue this job, Exception has to be catched for now. Logger.LogInformation(ex, $"Could not re-connect to {peerEndpoint}."); } if (connected) { Logger.LogDebug($"Reconnection to {peerEndpoint} succeeded."); if (!_reconnectionService.CancelReconnection(peerEndpoint)) { Logger.LogDebug($"Could not find {peerEndpoint}."); } } else { var timeExtension = _networkOptions.PeerReconnectionPeriod * (int)Math.Pow(2, ++peerToConnect.RetryCount); peerToConnect.NextAttempt = TimestampHelper.GetUtcNow().AddMilliseconds(timeExtension); // if the option is set, verify that the next attempt does not exceed // the maximum reconnection time. if (_networkOptions.MaximumReconnectionTime != 0) { var maxReconnectionDate = peerToConnect.DisconnectionTime + TimestampHelper.DurationFromMilliseconds(_networkOptions.MaximumReconnectionTime); if (peerToConnect.NextAttempt > maxReconnectionDate) { _reconnectionService.CancelReconnection(peerEndpoint); Logger.LogDebug($"Maximum reconnection time reached {peerEndpoint}, " + $"next was {peerToConnect.NextAttempt}."); continue; } } Logger.LogDebug($"Could not connect to {peerEndpoint}, next attempt {peerToConnect.NextAttempt}, " + $"current retries {peerToConnect.RetryCount}."); } } void CheckNtpClockDrift() { try { _networkService.CheckNtpDrift(); } catch (Exception) { // swallow any exception, we are not interested in anything else than valid checks. } } }
/// <summary> /// Attempts to add a node to the connected network nodes /// </summary> /// <param name="address">ip address</param> /// <returns>true/false</returns> public async Task <bool> AddPeer(string address) { return(await _networkService.AddPeerAsync(address)); }
internal async Task DoReconnectionJobAsync() { CheckNtpClockDrift(); await _networkService.SendHealthChecksAsync(); var peersToConnect = _reconnectionService.GetPeersReadyForReconnection(TimestampHelper.GetUtcNow()); if (peersToConnect.Count <= 0) { Logger.LogDebug("No peers to reconnect."); return; } foreach (var peerToConnect in peersToConnect) { string peerEndpoint = peerToConnect.Endpoint; if (!AElfPeerEndpointHelper.TryParse(peerEndpoint, out var parsed)) { if (!_reconnectionService.CancelReconnection(peerEndpoint)) { Logger.LogWarning($"Invalid {peerEndpoint}."); } continue; } // check that we haven't already reconnected to this node if (_peerPool.FindPeerByEndpoint(parsed) != null) { Logger.LogDebug($"Peer {peerEndpoint} already in the pool, no need to reconnect."); if (!_reconnectionService.CancelReconnection(peerEndpoint)) { Logger.LogDebug($"Could not find to {peerEndpoint}."); } continue; } Logger.LogDebug($"Starting reconnection to {peerToConnect.Endpoint}."); var connected = false; try { connected = await _networkService.AddPeerAsync(peerEndpoint); } catch (Exception ex) { // down the stack the AddPeerAsync rethrows any exception, // in order to continue this job, Exception has to be catched for now. Logger.LogError(ex, $"Could not re-connect to {peerEndpoint}."); } if (connected) { Logger.LogDebug($"Reconnection to {peerEndpoint} succeeded."); if (!_reconnectionService.CancelReconnection(peerEndpoint)) { Logger.LogDebug($"Could not find {peerEndpoint}."); } } else { peerToConnect.NextAttempt = TimestampHelper.GetUtcNow().AddMilliseconds(_networkOptions.PeerReconnectionPeriod); Logger.LogDebug($"Could not connect to {peerEndpoint}, next attempt {peerToConnect.NextAttempt}."); } } void CheckNtpClockDrift() { try { _networkService.CheckNtpDrift(); } catch (Exception) { // swallow any exception, we are not interested in anything else than valid checks. } } }
internal async Task DoReconnectionJobAsync() { await _networkService.SendHealthChecksAsync(); var peersToConnect = _reconnectionService.GetPeersReadyForReconnection(TimestampHelper.GetUtcNow()); if (peersToConnect.Count <= 0) { Logger.LogDebug("No peers to reconnect."); return; } foreach (var peerToConnect in peersToConnect) { string peerEndpoint = peerToConnect.Endpoint; // check that we haven't already reconnected to this node if (_peerPool.FindPeerByEndpoint(IpEndPointHelper.Parse(peerEndpoint)) != null) { Logger.LogDebug($"Peer {peerEndpoint} already in the pool, no need to reconnect."); if (!_reconnectionService.CancelReconnection(peerEndpoint)) { Logger.LogDebug($"Could not find to {peerEndpoint}."); } continue; } Logger.LogDebug($"Starting reconnection to {peerToConnect.Endpoint}."); var connected = false; try { connected = await _networkService.AddPeerAsync(peerEndpoint); } catch (Exception ex) { // todo consider different handling of the exception in dialer // down the stack the AddPeerAsync rethrows any exception, // in order to continue this job, Exception has to be catched for now. Logger.LogError(ex, $"Could not re-connect to {peerEndpoint}."); } if (connected) { Logger.LogDebug($"Reconnection to {peerEndpoint} succeeded."); if (!_reconnectionService.CancelReconnection(peerEndpoint)) { Logger.LogDebug($"Could not find {peerEndpoint}."); } } else { peerToConnect.NextAttempt = TimestampHelper.GetUtcNow().AddMilliseconds(_networkOptions.PeerReconnectionPeriod); Logger.LogDebug($"Could not connect to {peerEndpoint}, next attempt {peerToConnect.NextAttempt}."); } } }