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 all 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
2 changes: 1 addition & 1 deletion src/neo/Network/P2P/Payloads/OracleResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public override bool Verify(StoreView snapshot, Transaction tx)
OracleRequest request = NativeContract.Oracle.GetRequest(snapshot, Id);
if (request is null) return false;
if (tx.NetworkFee + tx.SystemFee != request.GasForResponse) return false;
UInt160 oracleAccount = Blockchain.GetConsensusAddress(NativeContract.Designate.GetDesignatedByRole(snapshot, Role.Oracle));
UInt160 oracleAccount = Blockchain.GetConsensusAddress(NativeContract.Designate.GetDesignatedByRole(snapshot, Role.Oracle, snapshot.Height + 1));
return tx.Signers.Any(p => p.Account.Equals(oracleAccount));
}
}
Expand Down
34 changes: 20 additions & 14 deletions src/neo/SmartContract/Native/Designate/DesignateContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Neo.Cryptography;
using Neo.Cryptography.ECC;
using Neo.IO;
using Neo.IO.Caching;
using Neo.Ledger;
using Neo.Persistence;
using Neo.SmartContract.Manifest;
Expand All @@ -24,20 +25,18 @@ internal DesignateContract()
Manifest.Features = ContractFeatures.HasStorage;
}

internal override void Initialize(ApplicationEngine engine)
{
foreach (byte role in Enum.GetValues(typeof(Role)))
{
engine.Snapshot.Storages.Add(CreateStorageKey(role), new StorageItem(new NodeList()));
}
}

[ContractMethod(0_01000000, CallFlags.AllowStates)]
public ECPoint[] GetDesignatedByRole(StoreView snapshot, Role role)
public ECPoint[] GetDesignatedByRole(StoreView snapshot, Role role, uint index)
{
if (!Enum.IsDefined(typeof(Role), role))
throw new ArgumentOutOfRangeException(nameof(role));
return snapshot.Storages[CreateStorageKey((byte)role)].GetInteroperable<NodeList>().ToArray();
if (snapshot.Height + 1 < index)
throw new ArgumentOutOfRangeException(nameof(index));
shargon marked this conversation as resolved.
Show resolved Hide resolved
byte[] key = CreateStorageKey((byte)role).AddBigEndian(index).ToArray();
byte[] boundary = CreateStorageKey((byte)role).ToArray();
return snapshot.Storages.FindRange(key, boundary, SeekDirection.Backward)
.Select(u => u.Value.GetInteroperable<NodeList>().ToArray())
.FirstOrDefault() ?? System.Array.Empty<ECPoint>();
}

[ContractMethod(0, CallFlags.AllowModifyStates)]
Expand All @@ -47,19 +46,26 @@ private void DesignateAsRole(ApplicationEngine engine, Role role, ECPoint[] node
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();
if (!CheckCommittee(engine))
throw new InvalidOperationException(nameof(DesignateAsRole));
if (engine.Snapshot.PersistingBlock is null)
throw new InvalidOperationException(nameof(DesignateAsRole));
uint index = engine.Snapshot.PersistingBlock.Index + 1;
var key = CreateStorageKey((byte)role).AddBigEndian(index);
if (engine.Snapshot.Storages.Contains(key))
throw new InvalidOperationException();
NodeList list = new NodeList();
list.AddRange(nodes);
list.Sort();
engine.Snapshot.Storages.Add(key, new StorageItem(list));
}

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
2 changes: 1 addition & 1 deletion src/neo/SmartContract/Native/Oracle/OracleContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected override void PostPersist(ApplicationEngine engine)
if (list.Count == 0) engine.Snapshot.Storages.Delete(key);

//Mint GAS for oracle nodes
nodes ??= NativeContract.Designate.GetDesignatedByRole(engine.Snapshot, Role.Oracle).Select(p => (Contract.CreateSignatureRedeemScript(p).ToScriptHash(), BigInteger.Zero)).ToArray();
nodes ??= NativeContract.Designate.GetDesignatedByRole(engine.Snapshot, Role.Oracle, engine.Snapshot.PersistingBlock.Index).Select(p => (Contract.CreateSignatureRedeemScript(p).ToScriptHash(), BigInteger.Zero)).ToArray();
if (nodes.Length > 0)
{
int index = (int)(response.Id % (ulong)nodes.Length);
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;
}
}
}
70 changes: 70 additions & 0 deletions tests/neo.UnitTests/SmartContract/Native/UT_DesignateContract.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography.ECC;
using Neo.IO;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
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();
snapshot1.PersistingBlock = new Block
{
Index = 0,
};
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) },
new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger(1u) }
);
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,
"getDesignatedByRole",
new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)Role.StateValidator) },
new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger(0) }
);
ret.Should().BeOfType<VM.Types.Array>();
(ret as VM.Types.Array).Count.Should().Be(0);
}
}
}