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 Dictionary<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 @@ -18,7 +18,6 @@ public sealed class DesignationContract : NativeContract
{
public override string Name => "Designation";
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 @@ -8,7 +8,6 @@ public sealed class GasToken : Nep17Token<AccountState>
{
public override int Id => -2;
public override string Name => "GAS";
public override uint ActiveBlockIndex => 0;
public override string Symbol => "GAS";
public override byte Decimals => 8;

Expand Down
7 changes: 3 additions & 4 deletions src/neo/SmartContract/Native/ManagementContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public sealed class ManagementContract : NativeContract
{
public override string Name => "Neo Contract Management";
public override int Id => 0;
public override uint ActiveBlockIndex => 0;

private const byte Prefix_NextAvailableId = 15;
private const byte Prefix_Contract = 8;
Expand All @@ -29,10 +28,10 @@ private int GetNextAvailableId(StoreView snapshot)

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 contractsList)
{
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 abstract string Name { get; }
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 @@ -19,7 +19,6 @@ public sealed class NeoToken : Nep17Token<NeoToken.NeoAccountState>
{
public override int Id => -1;
public override string Name => "NEO";
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 @@ -30,7 +30,6 @@ public sealed class OracleContract : NativeContract

public override int Id => -4;
public override string Name => "Oracle";
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 @@ -13,7 +13,6 @@ public sealed class PolicyContract : NativeContract
{
public override string Name => "Policy";
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 @@ -34,7 +34,6 @@ private class DummyNative : NativeContract
{
public override string Name => "Dummy";
public override int Id => 1;
public override uint ActiveBlockIndex => 0;

[ContractMethod(0, CallFlags.None)]
public void NetTypes(
Expand Down Expand Up @@ -140,7 +139,6 @@ public class TestNativeContract : NativeContract
{
public override string Name => "test";
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 @@ -83,6 +83,5 @@ public class TestNep17Token : Nep17Token<NeoToken.NeoAccountState>
public override string Name => "testNep17Token";
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);
}
}
}