Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dns peer feed no queue #3716

Merged
merged 16 commits into from
Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Api/IApiWithNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
using Nethermind.JsonRpc.Modules;
using Nethermind.Monitoring;
using Nethermind.Network;
using Nethermind.Network.Discovery;
using Nethermind.Network.P2P;
using Nethermind.Network.P2P.Analyzers;
using Nethermind.Network.Rlpx;
Expand All @@ -44,10 +43,11 @@ public interface IApiWithNetwork : IApiWithBlockchain
IMonitoringService MonitoringService { get; set; }
INodeStatsManager? NodeStatsManager { get; set; }
IPeerManager? PeerManager { get; set; }
IPeerPool? PeerPool { get; set; }
IProtocolsManager? ProtocolsManager { get; set; }
IProtocolValidator? ProtocolValidator { get; set; }
IList<IPublisher> Publishers { get; }
IRlpxPeer? RlpxPeer { get; set; }
IRlpxHost? RlpxPeer { get; set; }
IRpcModuleProvider? RpcModuleProvider { get; set; }
ISessionMonitor? SessionMonitor { get; set; }
IStaticNodesManager? StaticNodesManager { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Api/NethermindApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
using Nethermind.Logging;
using Nethermind.Monitoring;
using Nethermind.Network;
using Nethermind.Network.Discovery;
using Nethermind.Network.P2P;
using Nethermind.Network.P2P.Analyzers;
using Nethermind.Network.Rlpx;
Expand Down Expand Up @@ -146,14 +145,15 @@ public IBlockchainBridge CreateBlockchainBridge()
public IMonitoringService MonitoringService { get; set; } = NullMonitoringService.Instance;
public INodeStatsManager? NodeStatsManager { get; set; }
public IPeerManager? PeerManager { get; set; }
public IPeerPool? PeerPool { get; set; }
public IProtocolsManager? ProtocolsManager { get; set; }
public IProtocolValidator? ProtocolValidator { get; set; }
public IReceiptStorage? ReceiptStorage { get; set; }
public IWitnessCollector? WitnessCollector { get; set; }
public IWitnessRepository? WitnessRepository { get; set; }
public IReceiptFinder? ReceiptFinder { get; set; }
public IRewardCalculatorSource? RewardCalculatorSource { get; set; } = NoBlockRewards.Instance;
public IRlpxPeer? RlpxPeer { get; set; }
public IRlpxHost? RlpxPeer { get; set; }
public IRpcModuleProvider RpcModuleProvider { get; set; } = NullModuleProvider.Instance;
public ISealer? Sealer { get; set; } = NullSealEngine.Instance;
public string SealEngineType { get; set; } = Nethermind.Core.SealEngineType.None;
Expand Down
11 changes: 11 additions & 0 deletions src/Nethermind/Nethermind.Core.Test/PrivateKeyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.IO;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Crypto;
using NUnit.Framework;
Expand Down Expand Up @@ -94,6 +95,16 @@ public void Address_returns_the_same_value_when_called_twice()
Assert.AreSame(address1, address2);
}

[Test]
public void Can_decompress_public_key()
{
PrivateKey privateKey = new(TestPrivateKeyHex);
PublicKey a = privateKey.PublicKey;
PublicKey b = privateKey.CompressedPublicKey.Decompress();
Assert.AreEqual(a, b);
}


/// <summary>
/// https://en.bitcoin.it/wiki/Private_key
/// </summary>
Expand Down
13 changes: 12 additions & 1 deletion src/Nethermind/Nethermind.Core.Test/SignerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
namespace Nethermind.Core.Test
{
[TestFixture]
public class EcdsaTEsts
public class EcdsaTests
{
[OneTimeSetUp]
public void SetUp()
Expand All @@ -52,5 +52,16 @@ public void Sign_and_recover()
Signature signature = ethereumEcdsa.Sign(privateKey, message);
Assert.AreEqual(privateKey.Address, ethereumEcdsa.RecoverAddress(signature, message));
}

[Test]
public void Decompress()
{
EthereumEcdsa ethereumEcdsa = new(ChainId.Olympic, LimboLogs.Instance);
PrivateKey privateKey = Build.A.PrivateKey.TestObject;
CompressedPublicKey compressedPublicKey = privateKey.CompressedPublicKey;
PublicKey expected = privateKey.PublicKey;
PublicKey actual = ethereumEcdsa.Decompress(compressedPublicKey);
Assert.AreEqual(expected, actual);
}
}
}
6 changes: 6 additions & 0 deletions src/Nethermind/Nethermind.Core/Crypto/CompressedPublicKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System;
using System.Runtime.InteropServices;
using Nethermind.Core.Extensions;
using Nethermind.Secp256k1;

namespace Nethermind.Core.Crypto;

Expand All @@ -41,6 +42,11 @@ public CompressedPublicKey(ReadOnlySpan<byte> bytes)
Bytes = bytes.Slice(bytes.Length - LengthInBytes, LengthInBytes).ToArray();
}

public PublicKey Decompress()
{
return new PublicKey(Proxy.Decompress(Bytes));
}

public byte[] Bytes { get; }

public bool Equals(CompressedPublicKey? other)
Expand Down
6 changes: 6 additions & 0 deletions src/Nethermind/Nethermind.Crypto/Ecdsa.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,11 @@ public Signature Sign(PrivateKey privateKey, Keccak message)

return new CompressedPublicKey(publicKey);
}

public PublicKey Decompress(CompressedPublicKey compressedPublicKey)
{
byte[] deserialized = Proxy.Decompress(compressedPublicKey.Bytes);
return new PublicKey(deserialized);
}
}
}
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Init/Nethermind.Init.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ProjectReference Include="..\Nethermind.Db.Rpc\Nethermind.Db.Rpc.csproj" />
<ProjectReference Include="..\Nethermind.EthStats\Nethermind.EthStats.csproj" />
<ProjectReference Include="..\Nethermind.Network.Discovery\Nethermind.Network.Discovery.csproj" />
<ProjectReference Include="..\Nethermind.Network.Dns\Nethermind.Network.Dns.csproj" />
<ProjectReference Include="..\Nethermind.Network.Enr\Nethermind.Network.Enr.csproj" />
<ProjectReference Include="..\Nethermind.Specs\Nethermind.Specs.csproj" />
</ItemGroup>
Expand Down
32 changes: 22 additions & 10 deletions src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using Nethermind.Network.Discovery.Messages;
using Nethermind.Network.Discovery.RoutingTable;
using Nethermind.Network.Discovery.Serializers;
using Nethermind.Network.Dns;
using Nethermind.Network.Enr;
using Nethermind.Network.P2P;
using Nethermind.Network.P2P.Analyzers;
Expand Down Expand Up @@ -252,6 +253,7 @@ private void StartPeer()
}

if (_logger.IsDebug) _logger.Debug("Initializing peer manager");
_api.PeerPool.Start();
_api.PeerManager.Start();
_api.SessionMonitor.Start();
if (_logger.IsDebug) _logger.Debug("Peer manager initialization completed");
Expand Down Expand Up @@ -429,7 +431,7 @@ private async Task InitPeer()

_api.DisconnectsAnalyzer = new MetricsDisconnectsAnalyzer();
_api.SessionMonitor = new SessionMonitor(_networkConfig, _api.LogManager);
_api.RlpxPeer = new RlpxPeer(
_api.RlpxPeer = new RlpxHost(
_api.MessageSerializationService,
_api.NodeKey.PublicKey,
_networkConfig.P2PPort,
Expand Down Expand Up @@ -478,19 +480,29 @@ private async Task InitPeer()
{
await plugin.InitNetworkProtocol();
}

PeerLoader peerLoader = new(_networkConfig, _api.NodeStatsManager, peerStorage, _api.LogManager);

NodesLoader nodesLoader = new(_networkConfig, _api.NodeStatsManager, peerStorage, _api.RlpxPeer, _api.LogManager);
EnrDiscovery enrDiscovery = new (); // initialize with a proper network
CompositeNodeSource nodeSources = new(_api.StaticNodesManager, nodesLoader, enrDiscovery, _api.DiscoveryApp);
_api.PeerPool = new PeerPool(nodeSources, _api.NodeStatsManager, peerStorage, _networkConfig, _api.LogManager);
_api.PeerManager = new PeerManager(
_api.RlpxPeer,
_api.DiscoveryApp,
_api.PeerPool,
_api.NodeStatsManager,
peerStorage,
peerLoader,
_networkConfig,
_api.LogManager,
_api.StaticNodesManager);

_api.PeerManager.Init();
_api.LogManager);

string chainName = ChainId.GetChainName(_api.ChainSpec!.ChainId).ToLowerInvariant();
#pragma warning disable CS4014
enrDiscovery.SearchTree($"all.{chainName}.ethdisco.net").ContinueWith(t =>
#pragma warning restore CS4014
{
if (t.IsFaulted)
{
_logger.Error($"ENR discovery failed: {t.Exception}");

}
});
LukaszRozmej marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public virtual async Task Execute(CancellationToken cancellationToken)
AdminRpcModule adminRpcModule = new(
_api.BlockTree,
networkConfig,
_api.PeerManager,
_api.PeerPool,
_api.StaticNodesManager,
_api.Enode,
initConfig.BaseDbPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Nethermind.Blockchain;
using Nethermind.Config;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Test.Builders;
using Nethermind.JsonRpc.Modules.Admin;
using Nethermind.Network;
Expand Down Expand Up @@ -49,12 +51,14 @@ public void Setup()
{
_blockTree = Build.A.BlockTree().OfChainLength(5).TestObject;
_networkConfig = new NetworkConfig();
IPeerManager peerManager = Substitute.For<IPeerManager>();
peerManager.ActivePeers.Returns(new List<Peer> {new(new Node("127.0.0.1", 30303, true))});
IPeerPool peerPool = Substitute.For<IPeerPool>();
ConcurrentDictionary<PublicKey, Peer> dict = new();
dict.TryAdd(TestItem.PublicKeyA, new Peer(new Node(TestItem.PublicKeyA, "127.0.0.1", 30303, true)));
peerPool.ActivePeers.Returns(dict);

IStaticNodesManager staticNodesManager = Substitute.For<IStaticNodesManager>();
Enode enode = new(_enodeString);
_adminRpcModule = new AdminRpcModule(_blockTree, _networkConfig, peerManager, staticNodesManager, enode, _exampleDataDir);
_adminRpcModule = new AdminRpcModule(_blockTree, _networkConfig, peerPool, staticNodesManager, enode, _exampleDataDir);
_serializer = new EthereumJsonSerializer();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public void Initialize()

private static Peer SetUpPeerA()
{
Node node = new("127.0.0.1", 30303, true);
Node node = new(TestItem.PublicKeyA, "127.0.0.1", 30303, true);
node.ClientId = "Geth/v1.9.21-stable/linux-amd64/go1.15.2";

Peer peer = new(node);
Expand Down Expand Up @@ -228,7 +228,7 @@ private static Peer SetUpPeerA()

private static Peer SetUpPeerB()
{
Node node = new("95.217.106.25", 22222, true);
Node node = new(TestItem.PublicKeyB, "95.217.106.25", 22222, true);
node.ClientId = "Geth/v1.9.26-unstable/linux-amd64/go1.15.6";

Peer peer = new(node);
Expand Down Expand Up @@ -343,7 +343,7 @@ public void parity_netPeers_empty_ActivePeers()

IPeerManager peerManager = Substitute.For<IPeerManager>();
peerManager.ActivePeers.Returns(new List<Peer>{});
peerManager.ConnectedPeers.Returns(new List<Peer> {new(new Node("111.1.1.1", 11111, true))});
peerManager.ConnectedPeers.Returns(new List<Peer> {new(new Node(TestItem.PublicKeyA, "111.1.1.1", 11111, true))});

IParityRpcModule parityRpcModule = new ParityRpcModule(ethereumEcdsa, txPool, blockTree, receiptStorage, new Enode(TestItem.PublicKeyA, IPAddress.Loopback, 8545),
_signerStore, new MemKeyStore(new[] {TestItem.PrivateKeyA}), MainnetSpecProvider.Instance, peerManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using Nethermind.Blockchain;
Expand All @@ -24,33 +23,33 @@
using Nethermind.Core.Crypto;
using Nethermind.Network;
using Nethermind.Network.Config;
using Nethermind.Stats.Model;

namespace Nethermind.JsonRpc.Modules.Admin
{
public class AdminRpcModule : IAdminRpcModule
{
private readonly IBlockTree _blockTree;
private readonly INetworkConfig _networkConfig;
private readonly IPeerManager _peerManager;
private readonly IPeerPool _peerPool;
private readonly IStaticNodesManager _staticNodesManager;
private readonly IEnode _enode;
private readonly string _dataDir;

[NotNull]
private NodeInfo _nodeInfo;
private NodeInfo _nodeInfo = null!;

public AdminRpcModule(
IBlockTree blockTree,
INetworkConfig networkConfig,
IPeerManager peerManager,
IPeerPool peerPool,
IStaticNodesManager staticNodesManager,
IEnode enode,
string dataDir)
{
_enode = enode ?? throw new ArgumentNullException(nameof(enode));
_dataDir = dataDir ?? throw new ArgumentNullException(nameof(dataDir));
_blockTree = blockTree ?? throw new ArgumentNullException(nameof(blockTree));
_peerManager = peerManager ?? throw new ArgumentNullException(nameof(peerManager));
_peerPool = peerPool ?? throw new ArgumentNullException(nameof(peerPool));
_networkConfig = networkConfig ?? throw new ArgumentNullException(nameof(networkConfig));
_staticNodesManager = staticNodesManager ?? throw new ArgumentNullException(nameof(staticNodesManager));

Expand Down Expand Up @@ -88,7 +87,8 @@ public async Task<ResultWrapper<string>> admin_addPeer(string enode, bool addToS
}
else
{
_peerManager.AddPeer(new NetworkNode(enode));
NetworkNode networkNode = new(enode);
_peerPool.GetOrAdd(new Node(networkNode));
added = true;
}

Expand All @@ -106,7 +106,7 @@ public async Task<ResultWrapper<string>> admin_removePeer(string enode, bool rem
}
else
{
removed = _peerManager.RemovePeer(new NetworkNode(enode));
removed = _peerPool.TryRemove(new NetworkNode(enode).NodeId, out Peer _);
}

return removed
Expand All @@ -116,7 +116,7 @@ public async Task<ResultWrapper<string>> admin_removePeer(string enode, bool rem

public ResultWrapper<PeerInfo[]> admin_peers(bool includeDetails = false)
=> ResultWrapper<PeerInfo[]>.Success(
_peerManager.ActivePeers.Select(p => new PeerInfo(p, includeDetails)).ToArray());
_peerPool.ActivePeers.Select(p => new PeerInfo(p.Value, includeDetails)).ToArray());

public ResultWrapper<NodeInfo> admin_nodeInfo()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class NodeStatsCtorBenchmarks
[GlobalSetup]
public void Setup()
{
_node = new Node(new PublicKey("0x000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"), "127.0.0.1", 1234);
_node = new Node(new PublicKey("0x000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f"), "127.0.0.1", 1234, false);
LukaszRozmej marked this conversation as resolved.
Show resolved Hide resolved
}

[Benchmark]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void Initialize()
NodeLifecycleManagerFactory lifecycleFactory = new(_nodeTable, evictionManager,
new NodeStatsManager(timerFactory, logManager), new NodeRecord(), discoveryConfig, Timestamper.Default, logManager);

_nodes = new[] {new Node("192.168.1.18", 1), new Node("192.168.1.19", 2)};
_nodes = new[] {new Node(TestItem.PublicKeyA, "192.168.1.18", 1, false), new Node(TestItem.PublicKeyB,"192.168.1.19", 2, false)};

IFullDb nodeDb = new SimpleFilePublicKeyDb("Test", "test_db", logManager);
_discoveryManager = new DiscoveryManager(lifecycleFactory, _nodeTable, new NetworkStorage(nodeDb, logManager), discoveryConfig, logManager);
Expand Down Expand Up @@ -148,7 +148,7 @@ public void MemoryTest()
{
for (int b = 0; b < 255; b++)
{
INodeLifecycleManager? manager = _discoveryManager.GetNodeLifecycleManager(new Node($"{a}.{b}.1.1", 8000));
INodeLifecycleManager? manager = _discoveryManager.GetNodeLifecycleManager(new Node(TestItem.PublicKeyA, $"{a}.{b}.1.1", 8000, false));
manager?.SendPingAsync();

PongMsg pongMsg = new(_publicKey, GetExpirationTime(), Array.Empty<byte>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,12 @@ public void NeighborsMessageTest()
{
NeighborsMsg message =
new(_privateKey.PublicKey, 60 + _timestamper.UnixTime.MillisecondsLong,
new[] { new Node("192.168.1.2", 1), new Node("192.168.1.3", 2), new Node("192.168.1.4", 3) })
new[]
{
new Node(TestItem.PublicKeyA, "192.168.1.2", 1, false),
new Node(TestItem.PublicKeyB, "192.168.1.3", 2, false),
new Node(TestItem.PublicKeyC, "192.168.1.4", 3, false)
})
{
FarAddress = _farAddress
};
Expand Down
Loading