Skip to content

Commit

Permalink
Refactor native activation block Index (neo-project#2141)
Browse files Browse the repository at this point in the history
* Refactor activation Index

* Improve parse

* Fix

* Update ManagementContract.cs

* IReadOnlyDictionary

* Erik's feedback

* Optimize

Co-authored-by: Erik Zhang <erik@neo.org>
  • Loading branch information
2 people authored and Shawn committed Jan 8, 2021
1 parent 583a4c3 commit 9684c00
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/neo/ProtocolSettings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

Expand All @@ -16,6 +17,7 @@ public class ProtocolSettings
public uint MillisecondsPerBlock { get; }
public int MemoryPoolMaxTransactions { get; }
public uint MaxTraceableBlocks { get; }
public IReadOnlyDictionary<string, uint> NativeActivations { get; }

static ProtocolSettings _default;

Expand Down Expand Up @@ -96,6 +98,12 @@ private ProtocolSettings(IConfigurationSection section)
this.MillisecondsPerBlock = section.GetValue("MillisecondsPerBlock", 15000u);
this.MemoryPoolMaxTransactions = Math.Max(1, section.GetValue("MemoryPoolMaxTransactions", 50_000));
this.MaxTraceableBlocks = section.GetValue("MaxTraceableBlocks", 2_102_400u);// 365 days
IConfigurationSection section_na = section.GetSection("NativeActivations");
if (section_na.Exists())
this.NativeActivations = section_na.GetChildren().ToDictionary((a) => a.Key, b => uint.Parse(b.Value));
else
this.NativeActivations = new Dictionary<string, uint>();

}
}
}
1 change: 0 additions & 1 deletion src/neo/SmartContract/Native/DesignateContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace Neo.SmartContract.Native
public sealed class DesignationContract : NativeContract
{
public override int Id => -5;
public override uint ActiveBlockIndex => 0;

internal DesignationContract()
{
Expand Down
1 change: 0 additions & 1 deletion src/neo/SmartContract/Native/GasToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace Neo.SmartContract.Native
public sealed class GasToken : Nep17Token<AccountState>
{
public override int Id => -2;
public override uint ActiveBlockIndex => 0;
public override string Symbol => "GAS";
public override byte Decimals => 8;

Expand Down
1 change: 0 additions & 1 deletion src/neo/SmartContract/Native/ManagementContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace Neo.SmartContract.Native
public sealed class ManagementContract : NativeContract
{
public override int Id => 0;
public override uint ActiveBlockIndex => 0;

private const byte Prefix_MinimumDeploymentFee = 20;
private const byte Prefix_NextAvailableId = 15;
Expand Down
4 changes: 3 additions & 1 deletion src/neo/SmartContract/Native/NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public abstract class NativeContract
public UInt160 Hash { get; }
public abstract int Id { get; }
public ContractManifest Manifest { get; }
public abstract uint ActiveBlockIndex { get; }
public uint ActiveBlockIndex { get; }

protected NativeContract()
{
Expand Down Expand Up @@ -71,6 +71,8 @@ protected NativeContract()
Trusts = WildcardContainer<UInt160>.Create(),
Extra = null
};
if (ProtocolSettings.Default.NativeActivations.TryGetValue(Name, out uint activationIndex))
this.ActiveBlockIndex = activationIndex;
contractsList.Add(this);
contractsNameDictionary.Add(Name, this);
contractsHashDictionary.Add(Hash, this);
Expand Down
1 change: 0 additions & 1 deletion src/neo/SmartContract/Native/NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace Neo.SmartContract.Native
public sealed class NeoToken : Nep17Token<NeoToken.NeoAccountState>
{
public override int Id => -1;
public override uint ActiveBlockIndex => 0;
public override string Symbol => "NEO";
public override byte Decimals => 0;
public BigInteger TotalAmount { get; }
Expand Down
1 change: 0 additions & 1 deletion src/neo/SmartContract/Native/OracleContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public sealed class OracleContract : NativeContract
private const long OracleRequestPrice = 0_50000000;

public override int Id => -4;
public override uint ActiveBlockIndex => 0;

internal OracleContract()
{
Expand Down
1 change: 0 additions & 1 deletion src/neo/SmartContract/Native/PolicyContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace Neo.SmartContract.Native
public sealed class PolicyContract : NativeContract
{
public override int Id => -3;
public override uint ActiveBlockIndex => 0;

public const uint DefaultExecFeeFactor = 30;
public const uint DefaultStoragePrice = 100000;
Expand Down
2 changes: 0 additions & 2 deletions tests/neo.UnitTests/SmartContract/Native/UT_NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public void TestInitialize()
private class DummyNative : NativeContract
{
public override int Id => 1;
public override uint ActiveBlockIndex => 0;

[ContractMethod(0, CallFlags.None)]
public void NetTypes(
Expand Down Expand Up @@ -138,7 +137,6 @@ public void TestTestCall()
public class TestNativeContract : NativeContract
{
public override int Id => 0x10000006;
public override uint ActiveBlockIndex => 0;

[ContractMethod(0, CallFlags.None)]
public string HelloWorld => "hello world";
Expand Down
1 change: 0 additions & 1 deletion tests/neo.UnitTests/SmartContract/Native/UT_Nep17Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,5 @@ public class TestNep17Token : Nep17Token<NeoToken.NeoAccountState>
public override int Id => 0x10000005;
public override string Symbol => throw new NotImplementedException();
public override byte Decimals => 8;
public override uint ActiveBlockIndex => 0;
}
}
27 changes: 27 additions & 0 deletions tests/neo.UnitTests/UT_ProtocolSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Wallets;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace Neo.UnitTests
Expand Down Expand Up @@ -62,6 +63,26 @@ public void Can_initialize_ProtocolSettings()
ProtocolSettings.Default.Magic.Should().Be(expectedMagic);
}

[TestMethod]
public void Initialize_ProtocolSettings_NativeBlockIndex()
{
var tempFile = Path.GetTempFileName();
File.WriteAllText(tempFile, @"
{
""ProtocolConfiguration"":
{
""NativeActivations"":{ ""test"":123 }
}
}
");
var config = new ConfigurationBuilder().AddJsonFile(tempFile).Build();
File.Delete(tempFile);

ProtocolSettings.Initialize(config).Should().BeTrue();
ProtocolSettings.Default.NativeActivations.Count.Should().Be(1);
ProtocolSettings.Default.NativeActivations["test"].Should().Be(123);
}

[TestMethod]
public void Cant_initialize_ProtocolSettings_after_default_settings_used()
{
Expand Down Expand Up @@ -118,5 +139,11 @@ public void TestGetSeedList()
{
ProtocolSettings.Default.SeedList.Should().BeEquivalentTo(new string[] { "seed1.neo.org:10333", "seed2.neo.org:10333", "seed3.neo.org:10333", "seed4.neo.org:10333", "seed5.neo.org:10333", });
}

[TestMethod]
public void TestNativeActivations()
{
ProtocolSettings.Default.NativeActivations.Count.Should().Be(0);
}
}
}

0 comments on commit 9684c00

Please sign in to comment.