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

Add role designation history #2007

Merged
merged 28 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 49 additions & 11 deletions src/neo/SmartContract/Native/Designate/DesignateContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal override void Initialize(ApplicationEngine engine)
{
foreach (byte role in Enum.GetValues(typeof(Role)))
{
engine.Snapshot.Storages.Add(CreateStorageKey(role), new StorageItem(new NodeList()));
engine.Snapshot.Storages.Add(CreateStorageKey(role), new StorageItem(new NodeMap()));
}
}

Expand All @@ -37,34 +37,72 @@ public ECPoint[] GetDesignatedByRole(StoreView snapshot, Role role)
{
if (!Enum.IsDefined(typeof(Role), role))
throw new ArgumentOutOfRangeException(nameof(role));
return snapshot.Storages[CreateStorageKey((byte)role)].GetInteroperable<NodeList>().ToArray();
NodeMap map = snapshot.Storages[CreateStorageKey((byte)role)].GetInteroperable<NodeMap>();
List<uint> keys = map.Keys.ToList();
if (keys.Count == 0) return System.Array.Empty<ECPoint>();
keys.Sort();
return map[keys.Last()].ToArray();
}

[ContractMethod(0_01000000, CallFlags.AllowStates)]
public ECPoint[] GetDesignatedByRoleAndIndex(StoreView snapshot, Role role, uint index)
{
if (!Enum.IsDefined(typeof(Role), role))
throw new ArgumentOutOfRangeException(nameof(role));
if (snapshot.Height + 2 < index)
throw new ArgumentOutOfRangeException(nameof(index));
NodeMap map = snapshot.Storages[CreateStorageKey((byte)role)].GetInteroperable<NodeMap>();
List<uint> keys = map.Keys.ToList();
if (keys.Count == 0) return System.Array.Empty<ECPoint>();
keys.Sort();

uint height = 0;
foreach (uint h in keys)
{
if (index <= h) break;
height = h;
}

if (0 < height)
return map[height].ToArray();

return System.Array.Empty<ECPoint>();
}

[ContractMethod(0, CallFlags.AllowModifyStates)]
private void DesignateAsRole(ApplicationEngine engine, Role role, ECPoint[] nodes)
private bool DesignateAsRole(ApplicationEngine engine, Role role, ECPoint[] nodes)
{
if (nodes.Length == 0 || nodes.Length > 32)
throw new ArgumentException();
if (!Enum.IsDefined(typeof(Role), role))
throw new ArgumentOutOfRangeException(nameof(role));
if (!CheckCommittee(engine)) throw new InvalidOperationException();
NodeList list = engine.Snapshot.Storages.GetAndChange(CreateStorageKey((byte)role)).GetInteroperable<NodeList>();
list.Clear();
list.AddRange(nodes);
list.Sort();
NodeMap map = engine.Snapshot.Storages.GetAndChange(CreateStorageKey((byte)role)).GetInteroperable<NodeMap>();
map[engine.Snapshot.Height + 1] = nodes.ToList();
return true;
}

private class NodeList : List<ECPoint>, IInteroperable
private class NodeMap : Dictionary<uint, List<ECPoint>>, IInteroperable
{
public void FromStackItem(StackItem stackItem)
{
foreach (StackItem item in (VM.Types.Array)stackItem)
Add(ECPoint.DecodePoint(item.GetSpan(), ECCurve.Secp256r1));
foreach (var element in (Map)stackItem)
{
uint height = (uint)element.Key.GetInteger();
List<ECPoint> list = ((VM.Types.Array)element.Value).Select(p => p.GetSpan().AsSerializable<ECPoint>()).ToList();
Add(height, list);
}
}

public StackItem ToStackItem(ReferenceCounter referenceCounter)
{
return new VM.Types.Array(referenceCounter, this.Select(p => (StackItem)p.ToArray()));
Map map = new Map(referenceCounter);
foreach (var item in this)
{
VM.Types.Array arr = new VM.Types.Array(item.Value.Select(p => (StackItem)p.ToArray()));
map[item.Key] = arr;
}
return map;
}
}
}
Expand Down
73 changes: 73 additions & 0 deletions tests/neo.UnitTests/SmartContract/Native/UT_DesignateContract.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography.ECC;
using Neo.IO;
using Neo.Ledger;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.SmartContract.Native.Designate;
using Neo.UnitTests.Extensions;
using System.Linq;
using System.Numerics;

namespace Neo.UnitTests.SmartContract.Native
{
[TestClass]
public class UT_DesignateContract
{
[TestInitialize]
public void TestSetup()
{
TestBlockchain.InitializeMockNeoSystem();
}

[TestMethod]
public void TestSetAndGet()
{
using var snapshot1 = Blockchain.Singleton.GetSnapshot();
UInt160 committeeMultiSigAddr = NativeContract.NEO.GetCommitteeAddress(snapshot1);
ECPoint[] validators = NativeContract.NEO.ComputeNextBlockValidators(snapshot1);
var ret = NativeContract.Designate.Call(
snapshot1,
new Nep5NativeContractExtensions.ManualWitness(committeeMultiSigAddr),
"designateAsRole",
new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)Role.StateValidator) },
new ContractParameter(ContractParameterType.Array) { Value = validators.Select(p => new ContractParameter(ContractParameterType.ByteArray) { Value = p.ToArray() }).ToList() }
);
snapshot1.Commit();
using var snapshot2 = Blockchain.Singleton.GetSnapshot();
ret = NativeContract.Designate.Call(
snapshot2,
"getDesignatedByRole",
new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)Role.StateValidator) }
);
ret.Should().BeOfType<VM.Types.Array>();
(ret as VM.Types.Array).Count.Should().Be(7);
(ret as VM.Types.Array)[0].GetSpan().ToHexString().Should().Be(validators[0].ToArray().ToHexString());
(ret as VM.Types.Array)[1].GetSpan().ToHexString().Should().Be(validators[1].ToArray().ToHexString());
(ret as VM.Types.Array)[2].GetSpan().ToHexString().Should().Be(validators[2].ToArray().ToHexString());
(ret as VM.Types.Array)[3].GetSpan().ToHexString().Should().Be(validators[3].ToArray().ToHexString());
(ret as VM.Types.Array)[4].GetSpan().ToHexString().Should().Be(validators[4].ToArray().ToHexString());
(ret as VM.Types.Array)[5].GetSpan().ToHexString().Should().Be(validators[5].ToArray().ToHexString());
(ret as VM.Types.Array)[6].GetSpan().ToHexString().Should().Be(validators[6].ToArray().ToHexString());

ret = NativeContract.Designate.Call(
snapshot2,
"getDesignatedByRoleAndIndex",
new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)Role.StateValidator) },
new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger(2) }
);
ret.Should().BeOfType<VM.Types.Array>();
(ret as VM.Types.Array).Count.Should().Be(7);

ret = NativeContract.Designate.Call(
snapshot2,
"getDesignatedByRoleAndIndex",
new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)Role.StateValidator) },
new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger(1) }
);
ret.Should().BeOfType<VM.Types.Array>();
(ret as VM.Types.Array).Count.Should().Be(0);
}
}
}