diff --git a/src/Nethermind/Nethermind.Network.Discovery/DiscoveryApp.cs b/src/Nethermind/Nethermind.Network.Discovery/DiscoveryApp.cs index 0a447340ad3..192d4a88e16 100644 --- a/src/Nethermind/Nethermind.Network.Discovery/DiscoveryApp.cs +++ b/src/Nethermind/Nethermind.Network.Discovery/DiscoveryApp.cs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LGPL-3.0-only using System.Net; +using System.Net.NetworkInformation; using System.Net.Sockets; using System.Runtime.InteropServices; using DotNetty.Handlers.Logging; @@ -145,6 +146,8 @@ private void InitializeUdpChannel() .Handler(new ActionChannelInitializer(InitializeChannel)); } + NetworkChange.NetworkAvailabilityChanged += ResetUnreachableStatus; + IPAddress ip = IPAddress.Parse(_networkConfig.LocalIp!); _bindingTask = bootstrap.BindAsync(ip, _networkConfig.DiscoveryPort) .ContinueWith( @@ -160,6 +163,19 @@ private void InitializeUdpChannel() }); } + private void ResetUnreachableStatus(object? sender, NetworkAvailabilityEventArgs e) + { + if (!e.IsAvailable) + { + return; + } + + foreach (INodeLifecycleManager unreachable in _discoveryManager.GetNodeLifecycleManagers().Where(x => x.State == NodeLifecycleState.Unreachable)) + { + unreachable.ResetUnreachableStatus(); + } + } + private Task? _bindingTask; private void InitializeChannel(IDatagramChannel channel) @@ -338,6 +354,8 @@ private async Task StopUdpChannelAsync() { delayCancellation.Cancel(); } + + NetworkChange.NetworkAvailabilityChanged -= ResetUnreachableStatus; } catch (Exception e) { diff --git a/src/Nethermind/Nethermind.Network.Discovery/Lifecycle/INodeLifecycleManager.cs b/src/Nethermind/Nethermind.Network.Discovery/Lifecycle/INodeLifecycleManager.cs index c7d062d5d15..235a5c76704 100644 --- a/src/Nethermind/Nethermind.Network.Discovery/Lifecycle/INodeLifecycleManager.cs +++ b/src/Nethermind/Nethermind.Network.Discovery/Lifecycle/INodeLifecycleManager.cs @@ -23,5 +23,7 @@ public interface INodeLifecycleManager void StartEvictionProcess(); void LostEvictionProcess(); + void ResetUnreachableStatus(); + event EventHandler OnStateChanged; } diff --git a/src/Nethermind/Nethermind.Network.Discovery/Lifecycle/NodeLifecycleManager.cs b/src/Nethermind/Nethermind.Network.Discovery/Lifecycle/NodeLifecycleManager.cs index 7c38c1f8edc..7d4d8f86443 100644 --- a/src/Nethermind/Nethermind.Network.Discovery/Lifecycle/NodeLifecycleManager.cs +++ b/src/Nethermind/Nethermind.Network.Discovery/Lifecycle/NodeLifecycleManager.cs @@ -293,6 +293,14 @@ public void StartEvictionProcess() UpdateState(NodeLifecycleState.EvictCandidate); } + public void ResetUnreachableStatus() + { + if (State == NodeLifecycleState.Unreachable) + { + UpdateState(NodeLifecycleState.New); + } + } + public void LostEvictionProcess() { if (State == NodeLifecycleState.Active) @@ -383,8 +391,10 @@ private async Task CreateAndSendPingAsync(int counter = 1) { await CreateAndSendPingAsync(counter - 1); } - - UpdateState(NodeLifecycleState.Unreachable); + else + { + UpdateState(NodeLifecycleState.Unreachable); + } } } catch (Exception e)