Skip to content

Commit

Permalink
Discovery when offline on start (NethermindEth#7223)
Browse files Browse the repository at this point in the history
  • Loading branch information
flcl42 authored Jun 27, 2024
1 parent 82b84b2 commit 949589f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/Nethermind/Nethermind.Network.Discovery/DiscoveryApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -145,6 +146,8 @@ private void InitializeUdpChannel()
.Handler(new ActionChannelInitializer<IDatagramChannel>(InitializeChannel));
}

NetworkChange.NetworkAvailabilityChanged += ResetUnreachableStatus;

IPAddress ip = IPAddress.Parse(_networkConfig.LocalIp!);
_bindingTask = bootstrap.BindAsync(ip, _networkConfig.DiscoveryPort)
.ContinueWith(
Expand All @@ -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)
Expand Down Expand Up @@ -338,6 +354,8 @@ private async Task StopUdpChannelAsync()
{
delayCancellation.Cancel();
}

NetworkChange.NetworkAvailabilityChanged -= ResetUnreachableStatus;
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ public interface INodeLifecycleManager

void StartEvictionProcess();
void LostEvictionProcess();
void ResetUnreachableStatus();

event EventHandler<NodeLifecycleState> OnStateChanged;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 949589f

Please sign in to comment.