Skip to content

Commit

Permalink
Merge branch 'master-2.x' into fix-sync-stuck-2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon committed Jul 29, 2019
2 parents eb5ec67 + be17769 commit 0dfca83
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 15 deletions.
96 changes: 96 additions & 0 deletions neo.UnitTests/UT_ProtocolSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using FluentAssertions;
using Microsoft.Extensions.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Neo.UnitTests
{
[TestClass]
public class UT_ProtocolSettings
{
const uint mainNetMagic = 0x746E41u;

// 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 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()
{
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);
}
}
}
28 changes: 24 additions & 4 deletions neo/ProtocolSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace Neo
{
Expand All @@ -16,12 +17,31 @@ public class ProtocolSettings
public Fixed8 LowPriorityThreshold { get; }
public uint SecondsPerBlock { get; }

public static ProtocolSettings Default { get; }
static ProtocolSettings _default;

static ProtocolSettings()
static bool UpdateDefault(IConfiguration configuration)
{
IConfigurationSection section = new ConfigurationBuilder().AddJsonFile("protocol.json", true).Build().GetSection("ProtocolConfiguration");
Default = new ProtocolSettings(section);
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 = new ConfigurationBuilder().AddJsonFile("protocol.json", true).Build();
UpdateDefault(configuration);
}

return _default;
}
}

private ProtocolSettings(IConfigurationSection section)
Expand Down
34 changes: 24 additions & 10 deletions neo/Wallets/NEP6/NEP6Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class NEP6Wallet : Wallet

public override string Name => name;
public override Version Version => version;
public override uint WalletHeight => indexer.IndexHeight;
public override uint WalletHeight => indexer != null ? indexer.IndexHeight : default;

public NEP6Wallet(WalletIndexer indexer, string path, string name = null)
{
Expand All @@ -46,7 +46,8 @@ public NEP6Wallet(WalletIndexer indexer, string path, string name = null)
this.Scrypt = ScryptParameters.FromJson(wallet["scrypt"]);
this.accounts = ((JArray)wallet["accounts"]).Select(p => NEP6Account.FromJson(p, this)).ToDictionary(p => p.ScriptHash);
this.extra = wallet["extra"];
indexer.RegisterAccounts(accounts.Keys);

indexer?.RegisterAccounts(accounts.Keys);
}
else
{
Expand All @@ -56,7 +57,11 @@ public NEP6Wallet(WalletIndexer indexer, string path, string name = null)
this.accounts = new Dictionary<UInt160, NEP6Account>();
this.extra = JObject.Null;
}
indexer.WalletTransaction += WalletIndexer_WalletTransaction;

if (indexer != null)
{
indexer.WalletTransaction += WalletIndexer_WalletTransaction;
}
}

private void AddAccount(NEP6Account account, bool is_import)
Expand Down Expand Up @@ -86,7 +91,7 @@ private void AddAccount(NEP6Account account, bool is_import)
}
else
{
indexer.RegisterAccounts(new[] { account.ScriptHash }, is_import ? 0 : Blockchain.Singleton.Height);
indexer?.RegisterAccounts(new[] { account.ScriptHash }, is_import ? 0 : Blockchain.Singleton.Height);
}
accounts[account.ScriptHash] = account;
}
Expand Down Expand Up @@ -177,14 +182,17 @@ public override bool DeleteAccount(UInt160 scriptHash)
}
if (removed)
{
indexer.UnregisterAccounts(new[] { scriptHash });
indexer?.UnregisterAccounts(new[] { scriptHash });
}
return removed;
}

public override void Dispose()
{
indexer.WalletTransaction -= WalletIndexer_WalletTransaction;
if (indexer != null)
{
indexer.WalletTransaction -= WalletIndexer_WalletTransaction;
}
}

public override Coin[] FindUnspentCoins(UInt256 asset_id, Fixed8 amount, UInt160[] from)
Expand Down Expand Up @@ -212,6 +220,9 @@ public override IEnumerable<WalletAccount> GetAccounts()

public override IEnumerable<Coin> GetCoins(IEnumerable<UInt160> accounts)
{
if (indexer == null)
return Enumerable.Empty<Coin>();

if (unconfirmed.Count == 0)
return indexer.GetCoins(accounts);
else
Expand Down Expand Up @@ -265,12 +276,15 @@ IEnumerable<Coin> GetCoinsInternal()

public override IEnumerable<UInt256> GetTransactions()
{
foreach (UInt256 hash in indexer.GetTransactions(accounts.Keys))
yield return hash;
lock (unconfirmed)
if (indexer != null)
{
foreach (UInt256 hash in unconfirmed.Keys)
foreach (UInt256 hash in indexer.GetTransactions(accounts.Keys))
yield return hash;
lock (unconfirmed)
{
foreach (UInt256 hash in unconfirmed.Keys)
yield return hash;
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion neo/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,9 @@ public Transaction MakeTransaction(List<TransactionAttribute> attributes, IEnume
}
ApplicationEngine engine = ApplicationEngine.Run(script);
if (engine.State.HasFlag(VMState.FAULT)) return null;
balances.Add((account, engine.ResultStack.Pop().GetBigInteger()));
var result = engine.ResultStack.Pop().GetBigInteger();
if (result == 0) continue;
balances.Add((account, result));
}
BigInteger sum = balances.Aggregate(BigInteger.Zero, (x, y) => x + y.Value);
if (sum < output.Value) return null;
Expand Down

0 comments on commit 0dfca83

Please sign in to comment.