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
163b247
record historical designation
ZhangTao1596 Oct 15, 2020
cb97588
fix ut
ZhangTao1596 Oct 15, 2020
008ac48
Merge branch 'master' into fix-role-designate-history
ZhangTao1596 Oct 15, 2020
13b63ad
Merge branch 'master' into fix-role-designate-history
Tommo-L Oct 19, 2020
14c9d75
sort
ZhangTao1596 Oct 19, 2020
3f04df6
Remove sort
shargon Oct 19, 2020
0611e48
use different key
ZhangTao1596 Oct 20, 2020
265296b
fix some
ZhangTao1596 Oct 20, 2020
a4e587f
Store it as BiEndian
shargon Oct 21, 2020
a39e66a
Merge branch 'master' into fix-role-designate-history
ZhangTao1596 Oct 27, 2020
a4ca86e
avoid in same block
ZhangTao1596 Oct 27, 2020
6ddc6bb
Optimize
shargon Oct 27, 2020
9909067
Merge branch 'master' into fix-role-designate-history
shargon Oct 27, 2020
7c3a24a
Merge branch 'master' into fix-role-designate-history
shargon Oct 28, 2020
c756bb9
use findrange
ZhangTao1596 Oct 29, 2020
1b91cb3
Merge branch 'master' into fix-role-designate-history
ZhangTao1596 Oct 29, 2020
811a6ae
optimize index get
ZhangTao1596 Oct 29, 2020
777b526
Merge branch 'master' into fix-role-designate-history
shargon Oct 30, 2020
63c8c75
Erik's feedback
shargon Nov 1, 2020
785ccbe
Fix UT
shargon Nov 1, 2020
d2a326a
Merge branch 'master' into fix-role-designate-history
ZhangTao1596 Nov 2, 2020
b700b94
fix empty sequence
ZhangTao1596 Nov 2, 2020
62241de
optimize height
ZhangTao1596 Nov 2, 2020
187b140
fix ut
ZhangTao1596 Nov 2, 2020
475ba5d
Merge branch 'master' into fix-role-designate-history
ZhangTao1596 Nov 3, 2020
f32c744
index
ZhangTao1596 Nov 3, 2020
62b9f58
fix ut
ZhangTao1596 Nov 3, 2020
48e0098
Merge branch 'master' into fix-role-designate-history
erikzhang Nov 3, 2020
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
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
engine.Snapshot.Storages.Add(CreateStorageKey(role).AddBigEndian(0u), new StorageItem(new NodeList()));
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
}
}

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)
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
throw new ArgumentOutOfRangeException(nameof(index));
shargon marked this conversation as resolved.
Show resolved Hide resolved
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>();
shargon marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
}