Skip to content

Commit

Permalink
Remove ProtocolSettings Initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin Qiao committed Apr 27, 2020
1 parent 1af9369 commit 58dda0b
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 105 deletions.
28 changes: 1 addition & 27 deletions src/neo/ProtocolSettings.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.Extensions.Configuration;
using System;
using System.Linq;
using System.Threading;

namespace Neo
{
Expand All @@ -15,32 +14,7 @@ public class ProtocolSettings
public uint MillisecondsPerBlock { get; }
public int MemoryPoolMaxTransactions { get; }

static ProtocolSettings _default;

static bool UpdateDefault(IConfiguration configuration)
{
var settings = new ProtocolSettings(configuration.GetSection("ProtocolConfiguration"));
return null == Interlocked.CompareExchange(ref _default, settings, null);
}

public static bool Initialize(IConfiguration configuration)
{
return UpdateDefault(configuration);
}

public static ProtocolSettings Default
{
get
{
if (_default == null)
{
var configuration = Utility.LoadConfig("protocol");
UpdateDefault(configuration);
}

return _default;
}
}
public static readonly ProtocolSettings Default = new ProtocolSettings(Utility.LoadConfig("protocol").GetSection("ProtocolConfiguration"));

private ProtocolSettings(IConfigurationSection section)
{
Expand Down
78 changes: 0 additions & 78 deletions tests/neo.UnitTests/UT_ProtocolSettings.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,12 @@
using FluentAssertions;
using Microsoft.Extensions.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Wallets;
using System.Collections.Generic;
using System.Reflection;

namespace Neo.UnitTests
{
[TestClass]
public class UT_ProtocolSettings
{
// since ProtocolSettings.Default is designed to be writable only once, use reflection to
// reset the underlying _default field to null before and after running tests in this class.
static void ResetProtocolSettings()
{
var defaultField = typeof(ProtocolSettings)
.GetField("_default", BindingFlags.Static | BindingFlags.NonPublic);
defaultField.SetValue(null, null);
}

[TestInitialize]
public void Initialize()
{
ResetProtocolSettings();
}

[TestCleanup]
public void Cleanup()
{
ResetProtocolSettings();
}

[TestMethod]
public void CheckFirstLetterOfAddresses()
{
Expand All @@ -47,60 +23,6 @@ public void Default_Magic_should_be_mainnet_Magic_value()
ProtocolSettings.Default.Magic.Should().Be(mainNetMagic);
}

[TestMethod]
public void Can_initialize_ProtocolSettings()
{
var expectedMagic = 12345u;

var dict = new Dictionary<string, string>()
{
{ "ProtocolConfiguration:Magic", $"{expectedMagic}" }
};

var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build();
ProtocolSettings.Initialize(config).Should().BeTrue();
ProtocolSettings.Default.Magic.Should().Be(expectedMagic);
}

[TestMethod]
public void Cant_initialize_ProtocolSettings_after_default_settings_used()
{
var mainNetMagic = 0x4F454Eu;
ProtocolSettings.Default.Magic.Should().Be(mainNetMagic);

var updatedMagic = 54321u;
var dict = new Dictionary<string, string>()
{
{ "ProtocolConfiguration:Magic", $"{updatedMagic}" }
};

var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build();
ProtocolSettings.Initialize(config).Should().BeFalse();
ProtocolSettings.Default.Magic.Should().Be(mainNetMagic);
}

[TestMethod]
public void Cant_initialize_ProtocolSettings_twice()
{
var expectedMagic = 12345u;
var dict = new Dictionary<string, string>()
{
{ "ProtocolConfiguration:Magic", $"{expectedMagic}" }
};

var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build();
ProtocolSettings.Initialize(config).Should().BeTrue();

var updatedMagic = 54321u;
dict = new Dictionary<string, string>()
{
{ "ProtocolConfiguration:Magic", $"{updatedMagic}" }
};
config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build();
ProtocolSettings.Initialize(config).Should().BeFalse();
ProtocolSettings.Default.Magic.Should().Be(expectedMagic);
}

[TestMethod]
public void TestGetMemoryPoolMaxTransactions()
{
Expand Down

0 comments on commit 58dda0b

Please sign in to comment.