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 all 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
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private IEnumerable<string> Resolve(string configWildcard)
List<IEnumerable<string>> toIntersect = new();
foreach (string singleWildcard in configWildcards)
{
string singleWildcardBase = singleWildcard.Replace("^", "");
string singleWildcardBase = singleWildcard.Replace("^", string.Empty);
IEnumerable<string> result = groups.ContainsKey(singleWildcardBase)
? groups[singleWildcardBase]
: Enumerable.Repeat(singleWildcardBase, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private static void CheckDefault(PropertyInfo property, object? instance)
// there is a case when we have default value as [4, 8, 8] and we need to compare this string to int[] so removing brackets and whitespaces
string[] expectedItems = expectedValue
.Trim('[').Trim(']')
.Replace(" ", "")
.Replace(" ", string.Empty)
.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal static Address[] ExtractSigners(BlockHeader blockHeader)
{
if (blockHeader.ExtraData == null)
{
throw new Exception("");
throw new Exception(string.Empty);
}

Span<byte> signersData = blockHeader.ExtraData.AsSpan()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class KeccakConverterTests
public void Can_read_null()
{
KeccakConverter converter = new();
JsonReader reader = new JsonTextReader(new StringReader(""));
JsonReader reader = new JsonTextReader(new StringReader(string.Empty));
reader.ReadAsString();
Keccak result = converter.ReadJson(reader, typeof(Keccak), null, false, JsonSerializer.CreateDefault());
Assert.AreEqual(null, result);
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);
}
}
}
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Core/ClientVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public static class ClientVersion
static ClientVersion()
{
_date = DateTime.UtcNow.ToString("yyyyMMdd");
_gitTag = File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory!, "git-hash")) ? File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory!, "git-hash")).Trim().Replace("g", "") : string.Empty;
_gitTag = File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory!, "git-hash")) ? File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory!, "git-hash")).Trim().Replace("g", string.Empty) : string.Empty;

Description = $"Nethermind/v{Version}/{RuntimeInformation.OSArchitecture}-{Platform.GetPlatformName()}/{RuntimeInformation.FrameworkDescription.Trim().Replace(".NET ", "").Replace(" ", "")}";
Description = $"Nethermind/v{Version}/{RuntimeInformation.OSArchitecture}-{Platform.GetPlatformName()}/{RuntimeInformation.FrameworkDescription.Trim().Replace(".NET ", string.Empty).Replace(" ", string.Empty)}";
}

public static string Version => $"{_gitTag}-{_date}";
Expand Down
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
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Core/Extensions/Bytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ public static string ByteArrayToHexViaLookup32Safe(byte[] bytes, bool withZeroX)
{
if (bytes.Length == 0)
{
return withZeroX ? "0x" : "";
return withZeroX ? "0x" : string.Empty;
}

int length = bytes.Length * 2 + (withZeroX ? 2 : 0);
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);
}
}
}
12 changes: 6 additions & 6 deletions src/Nethermind/Nethermind.Grpc/Nethermind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public QueryRequest Clone() {

/// <summary>Field number for the "client" field.</summary>
public const int ClientFieldNumber = 1;
private string client_ = "";
private string client_ = string.Empty;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public string Client {
get { return client_; }
Expand Down Expand Up @@ -234,7 +234,7 @@ public QueryResponse Clone() {

/// <summary>Field number for the "client" field.</summary>
public const int ClientFieldNumber = 1;
private string client_ = "";
private string client_ = string.Empty;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public string Client {
get { return client_; }
Expand All @@ -245,7 +245,7 @@ public string Client {

/// <summary>Field number for the "data" field.</summary>
public const int DataFieldNumber = 2;
private string data_ = "";
private string data_ = string.Empty;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public string Data {
get { return data_; }
Expand Down Expand Up @@ -391,7 +391,7 @@ public SubscriptionRequest Clone() {

/// <summary>Field number for the "client" field.</summary>
public const int ClientFieldNumber = 1;
private string client_ = "";
private string client_ = string.Empty;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public string Client {
get { return client_; }
Expand Down Expand Up @@ -540,7 +540,7 @@ public SubscriptionResponse Clone() {

/// <summary>Field number for the "client" field.</summary>
public const int ClientFieldNumber = 1;
private string client_ = "";
private string client_ = string.Empty;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public string Client {
get { return client_; }
Expand All @@ -551,7 +551,7 @@ public string Client {

/// <summary>Field number for the "data" field.</summary>
public const int DataFieldNumber = 2;
private string data_ = "";
private string data_ = string.Empty;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public string Data {
get { return data_; }
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Grpc/NethermindGrpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public abstract partial class NethermindServiceBase
{
public virtual global::System.Threading.Tasks.Task<global::Nethermind.Grpc.QueryResponse> Query(global::Nethermind.Grpc.QueryRequest request, grpc::ServerCallContext context)
{
throw new grpc::RpcException(new grpc::Status(grpc::StatusCode.Unimplemented, ""));
throw new grpc::RpcException(new grpc::Status(grpc::StatusCode.Unimplemented, string.Empty));
}

public virtual global::System.Threading.Tasks.Task Subscribe(global::Nethermind.Grpc.SubscriptionRequest request, grpc::IServerStreamWriter<global::Nethermind.Grpc.SubscriptionResponse> responseStream, grpc::ServerCallContext context)
{
throw new grpc::RpcException(new grpc::Status(grpc::StatusCode.Unimplemented, ""));
throw new grpc::RpcException(new grpc::Status(grpc::StatusCode.Unimplemented, string.Empty));
}

}
Expand Down
6 changes: 3 additions & 3 deletions src/Nethermind/Nethermind.HashLib/Extensions/Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ public static void ConvertFloatsToULongs(float[] a_in, int a_index_in, int a_len

public static byte[] ConvertHexStringToBytes(string a_in)
{
a_in = a_in.Replace("-", "");
a_in = a_in.Replace("-", string.Empty);

Debug.Assert(a_in.Length % 2 == 0);

Expand All @@ -479,7 +479,7 @@ public static string ConvertBytesToHexString(byte[] a_in, bool a_group = true)

string[] ar = BitConverter.ToString(a_in).ToUpper().Split(new char[] {'-'});

hex = "";
hex = string.Empty;

for (int i = 0; i < ar.Length / 4; i++)
{
Expand All @@ -489,7 +489,7 @@ public static string ConvertBytesToHexString(byte[] a_in, bool a_group = true)
}
}
else
hex = hex.Replace("-", "");
hex = hex.Replace("-", string.Empty);

return hex;
}
Expand Down
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
36 changes: 26 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,33 @@ 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);

// I do not use the key here -> API is broken - no sense to use the node signer here
NodeRecordSigner nodeRecordSigner = new(_api.EthereumEcdsa, new PrivateKeyGenerator().Generate());
EnrRecordParser enrRecordParser = new(nodeRecordSigner);
EnrDiscovery enrDiscovery = new(enrRecordParser, _api.LogManager); // 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
Loading