Skip to content

Commit

Permalink
Feature/feed DNS to discv4 (#6035)
Browse files Browse the repository at this point in the history
* Feed ENR dns to discovery appp

* Missed a file

* Unit tests

* Fixed misconfiguration
  • Loading branch information
asdacap authored Sep 5, 2023
1 parent 12c43de commit 0604348
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,13 @@ private async Task InitPeer()
NodeRecordSigner nodeRecordSigner = new(_api.EthereumEcdsa, new PrivateKeyGenerator().Generate());
EnrRecordParser enrRecordParser = new(nodeRecordSigner);
EnrDiscovery enrDiscovery = new(enrRecordParser, _api.LogManager); // initialize with a proper network

if (!_networkConfig.DisableDiscV4DnsFeeder)
{
// Feed some nodes into discoveryApp in case all bootnodes is faulty.
_api.DisposeStack.Push(new NodeSourceToDiscV4Feeder(enrDiscovery, _api.DiscoveryApp, 50));
}

CompositeNodeSource nodeSources = new(_api.StaticNodesManager, nodesLoader, enrDiscovery, _api.DiscoveryApp);
_api.PeerPool = new PeerPool(nodeSources, _api.NodeStatsManager, peerStorage, _networkConfig, _api.LogManager);
_api.PeerManager = new PeerManager(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Core.Test.Builders;
using Nethermind.Stats.Model;
using NSubstitute;
using NUnit.Framework;

namespace Nethermind.Network.Discovery.Test;

public class NodeSourceToDiscV4FeederTests
{
[Test]
public void Test_ShouldAddNodeToDiscover()
{
INodeSource source = Substitute.For<INodeSource>();
IDiscoveryApp discoveryApp = Substitute.For<IDiscoveryApp>();
using NodeSourceToDiscV4Feeder feeder = new(source, discoveryApp, 10);
source.NodeAdded += Raise.EventWith(new NodeEventArgs(new Node(TestItem.PublicKeyA, TestItem.IPEndPointA)));

discoveryApp.Received().AddNodeToDiscovery(Arg.Any<Node>());
}

[Test]
public void Test_ShouldLimitAddedNode()
{
INodeSource source = Substitute.For<INodeSource>();
IDiscoveryApp discoveryApp = Substitute.For<IDiscoveryApp>();
using NodeSourceToDiscV4Feeder feeder = new(source, discoveryApp, 10);

for (int i = 0; i < 20; i++)
{
source.NodeAdded += Raise.EventWith(new NodeEventArgs(new Node(TestItem.PublicKeyA, TestItem.IPEndPointA)));
}

discoveryApp.Received(10).AddNodeToDiscovery(Arg.Any<Node>());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

namespace Nethermind.Network.Discovery;

public class NodeSourceToDiscV4Feeder : IDisposable
{
private readonly INodeSource _nodeSource;
private readonly IDiscoveryApp _discoveryApp;
private readonly int _maxNodes;
private int _addedNodes = 0;

public NodeSourceToDiscV4Feeder(INodeSource nodeSource, IDiscoveryApp discoveryApp, int maxNodes)
{
nodeSource.NodeAdded += AddToDiscoveryApp;
_nodeSource = nodeSource;
_discoveryApp = discoveryApp;
_maxNodes = maxNodes;
}

private void AddToDiscoveryApp(object? sender, NodeEventArgs e)
{
if (_addedNodes >= _maxNodes) return;
_addedNodes++;
_discoveryApp.AddNodeToDiscovery(e.Node);
}

public void Dispose()
{
_nodeSource.NodeAdded -= AddToDiscoveryApp;
}
}
3 changes: 3 additions & 0 deletions src/Nethermind/Nethermind.Network/Config/INetworkConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,8 @@ public interface INetworkConfig : IConfig

[ConfigItem(DefaultValue = null, HiddenFromDocs = true, Description = "[TECHNICAL] Only allow peer with clientId matching this regex. Useful for testing. eg: 'besu' to only connect to BeSU")]
string? ClientIdMatcher { get; set; }

[ConfigItem(DefaultValue = "false", HiddenFromDocs = true, Description = "[TECHNICAL] Disable feeding ENR DNS records to discv4 table")]
bool DisableDiscV4DnsFeeder { get; set; }
}
}
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Network/Config/NetworkConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ public class NetworkConfig : INetworkConfig
public int ConnectTimeoutMs { get; set; } = 2000;
public int ProcessingThreadCount { get; set; } = 1;
public string? ClientIdMatcher { get; set; } = null;
public bool DisableDiscV4DnsFeeder { get; set; } = false;
}
}

0 comments on commit 0604348

Please sign in to comment.