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

Refactor native activation block Index #2141

Merged
merged 14 commits into from
Dec 16, 2020
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
6 changes: 2 additions & 4 deletions 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 All @@ -36,10 +35,9 @@ internal override void Initialize(ApplicationEngine engine)

internal override void OnPersist(ApplicationEngine engine)
{
foreach (NativeContract contract in Contracts)
if (!ActiveBlockIndexes.TryGetValue(engine.Snapshot.PersistingBlock.Index, out var activations)) return;
foreach (NativeContract contract in activations)
{
if (contract.ActiveBlockIndex != engine.Snapshot.PersistingBlock.Index)
continue;
engine.Snapshot.Storages.Add(CreateStorageKey(Prefix_Contract).Add(contract.Hash), new StorageItem(new ContractState
{
Id = contract.Id,
Expand Down
23 changes: 22 additions & 1 deletion src/neo/SmartContract/Native/NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public abstract class NativeContract
private static readonly Dictionary<string, NativeContract> contractsNameDictionary = new Dictionary<string, NativeContract>();
private static readonly Dictionary<UInt160, NativeContract> contractsHashDictionary = new Dictionary<UInt160, NativeContract>();
private readonly Dictionary<string, ContractMethodMetadata> methods = new Dictionary<string, ContractMethodMetadata>();
public static readonly Dictionary<uint, List<NativeContract>> ActiveBlockIndexes = new Dictionary<uint, List<NativeContract>>();
shargon marked this conversation as resolved.
Show resolved Hide resolved

public static IReadOnlyCollection<NativeContract> Contracts { get; } = contractsList;
public static ManagementContract Management { get; } = new ManagementContract();
Expand All @@ -26,12 +27,32 @@ public abstract class NativeContract
public static OracleContract Oracle { get; } = new OracleContract();
public static DesignationContract Designation { get; } = new DesignationContract();

static NativeContract()
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
{
foreach (var contract in new NativeContract[] { Management, NEO, GAS, Policy, Oracle, Designation })
{
if (ProtocolSettings.Default.NativeActivations.TryGetValue(contract.Name, out uint activationIndex))
{
contract.ActiveBlockIndex = activationIndex;
}

if (ActiveBlockIndexes.TryGetValue(contract.ActiveBlockIndex, out var list))
{
list.Add(contract);
}
else
{
ActiveBlockIndexes.Add(contract.ActiveBlockIndex, new List<NativeContract>() { contract });
}
}
}

public string Name => GetType().Name;
public byte[] Script { get; }
public UInt160 Hash { get; }
public abstract int Id { get; }
public ContractManifest Manifest { get; }
public abstract uint ActiveBlockIndex { get; }
public uint ActiveBlockIndex { get; private set; }

protected NativeContract()
{
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);
}
}
}