Skip to content

Commit

Permalink
Merge branch 'master' into feature/ssz-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
flcl42 authored Jan 7, 2025
2 parents 8f17122 + c87bd25 commit 172a2b0
Show file tree
Hide file tree
Showing 71 changed files with 812 additions and 544 deletions.
2 changes: 1 addition & 1 deletion src/Nethermind/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<PackageVersion Include="Nethermind.Gmp" Version="1.0.1" />
<PackageVersion Include="Nethermind.Libp2p" Version="1.0.0-preview.34" />
<PackageVersion Include="Nethermind.Libp2p.Protocols.PubsubPeerDiscovery" Version="1.0.0-preview.34" />
<PackageVersion Include="Nethermind.Numerics.Int256" Version="1.3.0" />
<PackageVersion Include="Nethermind.Numerics.Int256" Version="1.3.2" />
<PackageVersion Include="Nito.Collections.Deque" Version="1.2.1" />
<PackageVersion Include="NLog" Version="5.3.4" />
<PackageVersion Include="NLog.Targets.Seq" Version="4.0.1" />
Expand Down
33 changes: 17 additions & 16 deletions src/Nethermind/Ethereum.Blockchain.Test/Eip2537Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@

namespace Ethereum.Blockchain.Test
{
[TestFixture]
[Parallelizable(ParallelScope.All)]
public class Eip2537Tests : GeneralStateTestBase
{
[TestCaseSource(nameof(LoadTests))]
public void Test(GeneralStateTest test)
{
Assert.That(RunTest(test).Pass, Is.True);
}

public static IEnumerable<GeneralStateTest> LoadTests()
{
var loader = new TestsSourceLoader(new LoadGeneralStateTestsStrategy(), "../EIPTests/StateTests/stEIP2537");
return (IEnumerable<GeneralStateTest>)loader.LoadTests();
}
}
// Tests need to be updated
// [TestFixture]
// [Parallelizable(ParallelScope.All)]
// public class Eip2537Tests : GeneralStateTestBase
// {
// [TestCaseSource(nameof(LoadTests))]
// public void Test(GeneralStateTest test)
// {
// Assert.That(RunTest(test).Pass, Is.True);
// }
//
// public static IEnumerable<GeneralStateTest> LoadTests()
// {
// var loader = new TestsSourceLoader(new LoadGeneralStateTestsStrategy(), "../EIPTests/StateTests/stEIP2537");
// return (IEnumerable<GeneralStateTest>)loader.LoadTests();
// }
// }
}
1 change: 1 addition & 0 deletions src/Nethermind/Ethereum.Blockchain.Test/MetaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class MetaTests
"ref",
"TestFiles",
"Blockhash",
"stEIP2537", // ToDo Remove this after updating tests
"Data"
};

Expand Down
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using Nethermind.JsonRpc;
using Nethermind.JsonRpc.Modules.Eth.GasPrice;
using Nethermind.State;
using Nethermind.Synchronization.FastSync;
using Nethermind.Trie;
using Nethermind.Trie.Pruning;
using Nethermind.TxPool;
Expand Down Expand Up @@ -63,6 +64,7 @@ public interface IApiWithBlockchain : IApiWithStores, IBlockchainBridgeFactory
/// DO NOT USE OUTSIDE OF PROCESSING BLOCK CONTEXT!
/// </remarks>
IWorldState? WorldState { get; set; }
IBlockingVerifyTrie? BlockingVerifyTrie { get; set; }
IReadOnlyStateProvider? ChainHeadStateProvider { get; set; }
IStateReader? StateReader { get; set; }
IWorldStateManager? WorldStateManager { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Api/NethermindApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
using Nethermind.Trie;
using Nethermind.Consensus.Processing.CensorshipDetector;
using Nethermind.Facade.Find;
using Nethermind.Synchronization.FastSync;

namespace Nethermind.Api
{
Expand Down Expand Up @@ -196,6 +197,7 @@ public ISealEngine SealEngine
public IPeerDifficultyRefreshPool? PeerDifficultyRefreshPool => ApiWithNetworkServiceContainer?.Resolve<IPeerDifficultyRefreshPool>();
public ISynchronizer? Synchronizer => ApiWithNetworkServiceContainer?.Resolve<ISynchronizer>();
public ISyncServer? SyncServer => ApiWithNetworkServiceContainer?.Resolve<ISyncServer>();
public IBlockingVerifyTrie? BlockingVerifyTrie { get; set; }
public IWorldState? WorldState { get; set; }
public IReadOnlyStateProvider? ChainHeadStateProvider { get; set; }
public IWorldStateManager? WorldStateManager { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,42 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections;
using System.Collections.Generic;
using NSubstitute;
using NUnit.Framework;

namespace Nethermind.Config.Test;

[TestFixture]
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
[Parallelizable(ParallelScope.All)]
public class ConfigProvider_FindIncorrectSettings_Tests
{
private IEnvironment _env;

[SetUp]
public void Initialize()
{
_env = Substitute.For<IEnvironment>();
_env.GetEnvironmentVariable(Arg.Any<string>())
.Returns(call =>
{
IDictionary vars = _env.GetEnvironmentVariables();
var key = call.Arg<string>();

return vars.Contains(key) ? vars[key] : null;
});
}

[Test]
public void CorrectSettingNames_CaseInsensitive()
{
JsonConfigSource? jsonSource = new("SampleJson/CorrectSettingNames.json");

IEnvironment? env = Substitute.For<IEnvironment>();
env.GetEnvironmentVariables().Returns(new Dictionary<string, string>() { { "NETHERMIND_NETWORKCONFIG_MAXCANDIDATEPEERCOUNT", "500" } });
EnvConfigSource? envSource = new(env);
Dictionary<string, string> envVars = new() { { "NETHERMIND_NETWORKCONFIG_MAXCANDIDATEPEERCOUNT", "500" } };

_env.GetEnvironmentVariables().Returns(envVars);
EnvConfigSource? envSource = new(_env);

ArgsConfigSource? argsSource = new(new Dictionary<string, string>() {
{ "DiscoveryConfig.BucketSize", "10" },
Expand All @@ -33,20 +51,19 @@ public void CorrectSettingNames_CaseInsensitive()
configProvider.Initialize();
(_, IList<(IConfigSource Source, string Category, string Name)> Errors) = configProvider.FindIncorrectSettings();

Assert.That(Errors.Count, Is.EqualTo(0));
Assert.That(Errors, Is.Empty);
}

[Test]
public void NoCategorySettings()
{
IEnvironment? env = Substitute.For<IEnvironment>();
env.GetEnvironmentVariables().Returns(new Dictionary<string, string>() {
_env.GetEnvironmentVariables().Returns(new Dictionary<string, string>() {
{ "NETHERMIND_CLI_SWITCH_LOCAL", "http://localhost:80" },
{ "NETHERMIND_CONFIG", "test2.json" },
{ "NETHERMIND_XYZ", "xyz" }, // not existing, should get error
{ "QWER", "qwerty" } // not Nethermind setting, no error
});
EnvConfigSource? envSource = new(env);
EnvConfigSource? envSource = new(_env);

ConfigProvider? configProvider = new();
configProvider.AddSource(envSource);
Expand All @@ -55,22 +72,23 @@ public void NoCategorySettings()

(string ErrorMsg, IList<(IConfigSource Source, string Category, string Name)> Errors) = configProvider.FindIncorrectSettings();

Assert.That(Errors.Count, Is.EqualTo(1));
Assert.That(Errors[0].Name, Is.EqualTo("XYZ"));
Assert.That(ErrorMsg, Is.EqualTo($"ConfigType:EnvironmentVariable(NETHERMIND_*)|Category:|Name:XYZ"));

Assert.That(Errors, Has.Count.EqualTo(1));
Assert.Multiple(() =>
{
Assert.That(Errors[0].Name, Is.EqualTo("XYZ"));
Assert.That(ErrorMsg, Is.EqualTo($"ConfigType:EnvironmentVariable(NETHERMIND_*)|Category:|Name:XYZ"));
});
}

[Test]
public void SettingWithTypos()
{
JsonConfigSource? jsonSource = new("SampleJson/ConfigWithTypos.json");

IEnvironment? env = Substitute.For<IEnvironment>();
env.GetEnvironmentVariables().Returns(new Dictionary<string, string>() {
_env.GetEnvironmentVariables().Returns(new Dictionary<string, string>() {
{ "NETHERMIND_NETWORKCONFIG_MAXCANDIDATEPERCOUNT", "500" } // incorrect, should be NETHERMIND_NETWORKCONFIG_MAXCANDIDATEPEERCOUNT
});
EnvConfigSource? envSource = new(env);
EnvConfigSource? envSource = new(_env);

ConfigProvider? configProvider = new();
configProvider.AddSource(jsonSource);
Expand All @@ -80,21 +98,23 @@ public void SettingWithTypos()

(string ErrorMsg, IList<(IConfigSource Source, string Category, string Name)> Errors) = configProvider.FindIncorrectSettings();

Assert.That(Errors.Count, Is.EqualTo(3));
Assert.That(Errors[0].Name, Is.EqualTo("Concurrenc"));
Assert.That(Errors[1].Category, Is.EqualTo("BlomConfig"));
Assert.That(Errors[2].Name, Is.EqualTo("MAXCANDIDATEPERCOUNT"));
Assert.That(ErrorMsg, Is.EqualTo($"ConfigType:JsonConfigFile|Category:DiscoveRyConfig|Name:Concurrenc{Environment.NewLine}ConfigType:JsonConfigFile|Category:BlomConfig|Name:IndexLevelBucketSizes{Environment.NewLine}ConfigType:EnvironmentVariable(NETHERMIND_*)|Category:NETWORKCONFIG|Name:MAXCANDIDATEPERCOUNT"));
Assert.That(Errors, Has.Count.EqualTo(3));
Assert.Multiple(() =>
{
Assert.That(Errors[0].Name, Is.EqualTo("Concurrenc"));
Assert.That(Errors[1].Category, Is.EqualTo("BlomConfig"));
Assert.That(Errors[2].Name, Is.EqualTo("MAXCANDIDATEPERCOUNT"));
Assert.That(ErrorMsg, Is.EqualTo($"ConfigType:JsonConfigFile|Category:DiscoveRyConfig|Name:Concurrenc{Environment.NewLine}ConfigType:JsonConfigFile|Category:BlomConfig|Name:IndexLevelBucketSizes{Environment.NewLine}ConfigType:EnvironmentVariable(NETHERMIND_*)|Category:NETWORKCONFIG|Name:MAXCANDIDATEPERCOUNT"));
});
}

[Test]
public void IncorrectFormat()
{
IEnvironment? env = Substitute.For<IEnvironment>();
env.GetEnvironmentVariables().Returns(new Dictionary<string, string>() {
_env.GetEnvironmentVariables().Returns(new Dictionary<string, string>() {
{ "NETHERMIND_NETWORKCONFIGMAXCANDIDATEPEERCOUNT", "500" } // incorrect, should be NETHERMIND_NETWORKCONFIG_MAXCANDIDATEPEERCOUNT
});
EnvConfigSource? envSource = new(env);
EnvConfigSource? envSource = new(_env);

ConfigProvider? configProvider = new();
configProvider.AddSource(envSource);
Expand All @@ -103,9 +123,68 @@ public void IncorrectFormat()

(string ErrorMsg, IList<(IConfigSource Source, string Category, string Name)> Errors) = configProvider.FindIncorrectSettings();

Assert.That(Errors.Count, Is.EqualTo(1));
Assert.That(Errors[0].Name, Is.EqualTo("NETWORKCONFIGMAXCANDIDATEPEERCOUNT"));
Assert.That(ErrorMsg, Is.EqualTo($"ConfigType:EnvironmentVariable(NETHERMIND_*)|Category:|Name:NETWORKCONFIGMAXCANDIDATEPEERCOUNT"));
Assert.That(Errors, Has.Count.EqualTo(1));
Assert.Multiple(() =>
{
Assert.That(Errors[0].Name, Is.EqualTo("NETWORKCONFIGMAXCANDIDATEPEERCOUNT"));
Assert.That(ErrorMsg, Is.EqualTo($"ConfigType:EnvironmentVariable(NETHERMIND_*)|Category:|Name:NETWORKCONFIGMAXCANDIDATEPEERCOUNT"));
});
}

[Test]
public void Should_keep_blank_string_values()
{
Dictionary<string, string> envVars = new()
{
{ "NETHERMIND_BLOCKSCONFIG_EXTRADATA", "" }
};

_env.GetEnvironmentVariables().Returns(envVars);
EnvConfigSource? envSource = new(_env);

ConfigProvider? configProvider = new();
configProvider.AddSource(envSource);

(bool isSet, object value) = envSource.GetValue(typeof(string), "BlocksConfig", "ExtraData");

Assert.Multiple(() =>
{
Assert.That(isSet, Is.True);
Assert.That(value, Is.Empty);
});
}

[Test]
public void Should_ignore_blank_nonstring_values()
{
Dictionary<string, string> envVars = new()
{
{ "NETHERMIND_BLOOMCONFIG_INDEX", " " },
{ "NETHERMIND_BLOOMCONFIG_MIGRATION", "" }
};

_env.GetEnvironmentVariables().Returns(envVars);
EnvConfigSource? envSource = new(_env);

ConfigProvider? configProvider = new();
configProvider.AddSource(envSource);

Assert.DoesNotThrow(configProvider.Initialize);

(bool isSet, object value) = envSource.GetValue(typeof(bool), "BloomConfig", "Index");

Assert.Multiple(() =>
{
Assert.That(isSet, Is.False);
Assert.That(((ValueTuple<bool, object>)value).Item2, Is.False);
});

(isSet, value) = envSource.GetValue(typeof(bool), "BloomConfig", "Migration");

Assert.Multiple(() =>
{
Assert.That(isSet, Is.False);
Assert.That(((ValueTuple<bool, object>)value).Item2, Is.False);
});
}
}
6 changes: 3 additions & 3 deletions src/Nethermind/Nethermind.Config/ConfigSourceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static object ParseValue(Type valueType, string valueString, string categ
//supports Arrays, e.g int[] and generic IEnumerable<T>, IList<T>
var itemType = valueType.IsGenericType ? valueType.GetGenericArguments()[0] : valueType.GetElementType();

if (itemType == typeof(byte) && !valueString.AsSpan().TrimStart().StartsWith("["))
if (itemType == typeof(byte) && !valueString.AsSpan().TrimStart().StartsWith('['))
{
// hex encoded byte array
value = Bytes.FromHexString(valueString.Trim());
Expand Down Expand Up @@ -99,13 +99,13 @@ public static object ParseValue(Type valueType, string valueString, string categ
}

private static bool IsNullString(string valueString) =>
string.IsNullOrEmpty(valueString) || valueString.Equals("null", StringComparison.OrdinalIgnoreCase);
valueString?.Equals("null", StringComparison.OrdinalIgnoreCase) ?? true;

public static object GetDefault(Type type) => type.IsValueType ? (false, Activator.CreateInstance(type)) : (false, null);

private static bool TryFromHex(Type type, string itemValue, out object value)
{
if (!itemValue.StartsWith("0x"))
if (!itemValue.StartsWith("0x", StringComparison.Ordinal))
{
value = null;
return false;
Expand Down
7 changes: 6 additions & 1 deletion src/Nethermind/Nethermind.Config/EnvConfigSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ public EnvConfigSource(IEnvironment environmentWrapper)
public (bool IsSet, object Value) GetValue(Type type, string category, string name)
{
(bool isSet, string value) = GetRawValue(category, name);

// Unset blank values for non-string types
if (type != typeof(string) && string.IsNullOrWhiteSpace(value))
isSet = false;

return (isSet, isSet ? ConfigSourceHelper.ParseValue(type, value, category, name) : ConfigSourceHelper.GetDefault(type));
}

public (bool IsSet, string Value) GetRawValue(string category, string name)
{
var variableName = string.IsNullOrEmpty(category) ? $"NETHERMIND_{name.ToUpperInvariant()}" : $"NETHERMIND_{category.ToUpperInvariant()}_{name.ToUpperInvariant()}";
var variableValueString = _environmentWrapper.GetEnvironmentVariable(variableName);
return string.IsNullOrWhiteSpace(variableValueString) ? (false, null) : (true, variableValueString);
return (variableValueString is not null, variableValueString);
}

public IEnumerable<(string Category, string Name)> GetConfigKeys()
Expand Down
Loading

0 comments on commit 172a2b0

Please sign in to comment.