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 9 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
44 changes: 40 additions & 4 deletions src/neo/SmartContract/Native/Designate/DesignateContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -28,7 +29,8 @@ 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(BitConverter.GetBytes(0u))); // Same as BigEndian
engine.Snapshot.Storages.Add(CreateStorageKey(role).AddBigEndian(0u), new StorageItem(new NodeList()));
}
}

Expand All @@ -37,7 +39,37 @@ 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();
byte[] index = snapshot.Storages[CreateStorageKey((byte)role)].Value;
KeyBuilder index_key = CreateStorageKey((byte)role).Add(index);
return snapshot.Storages[index_key].GetInteroperable<NodeList>().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));
List<uint> keys = snapshot.Storages.Find(CreateStorageKey((byte)role).ToArray())
.Where(p => p.Key.Key.Length == sizeof(uint) + 1)
.Select(p => BinaryPrimitives.ReadUInt32BigEndian(p.Key.Key.AsSpan(1, sizeof(uint))))
.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 snapshot.Storages[CreateStorageKey((byte)role).AddBigEndian(height)].GetInteroperable<NodeList>().ToArray();

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

[ContractMethod(0, CallFlags.AllowModifyStates)]
Expand All @@ -48,18 +80,22 @@ private void DesignateAsRole(ApplicationEngine engine, Role role, ECPoint[] node
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>();
uint index = engine.Snapshot.Height + 1;
NodeList list = engine.Snapshot.Storages.GetAndChange(CreateStorageKey((byte)role).AddBigEndian(index), () => new StorageItem(new NodeList())).GetInteroperable<NodeList>();
list.Clear();
list.AddRange(nodes);
list.Sort();
StorageItem current = engine.Snapshot.Storages.GetAndChange(CreateStorageKey((byte)role));
current.Value = new byte[sizeof(uint)];
BinaryPrimitives.WriteUInt32BigEndian(current.Value, index);
}

private class NodeList : List<ECPoint>, IInteroperable
{
public void FromStackItem(StackItem stackItem)
{
foreach (StackItem item in (VM.Types.Array)stackItem)
Add(ECPoint.DecodePoint(item.GetSpan(), ECCurve.Secp256r1));
Add(item.GetSpan().AsSerializable<ECPoint>());
}

public StackItem ToStackItem(ReferenceCounter referenceCounter)
Expand Down
4 changes: 3 additions & 1 deletion tests/neo.UnitTests/Extensions/NativeContractExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public static StackItem Call(this NativeContract contract, StoreView snapshot, I
throw exception ?? new InvalidOperationException();
}

return engine.ResultStack.Pop();
if (0 < engine.ResultStack.Count)
return engine.ResultStack.Pop();
return null;
}
}
}
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);
}
}
}