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

Fixes GetRegisteredValidators() #991

Merged
merged 4 commits into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 15 additions & 1 deletion neo.UnitTests/SmartContract/Native/Tokens/UT_NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ public void Check_RegisterValidator()
ret.Result.Should().BeTrue();

snapshot.Storages.GetChangeSet().Count().Should().Be(keyCount + 1); // New validator

// Check GetRegisteredValidators

var validators = NativeContract.NEO.GetRegisteredValidators(snapshot).OrderBy(u => u.PublicKey).ToArray();
var check = Blockchain.StandbyValidators.Select(u => u.EncodePoint(true)).ToList();
check.Add(point); // Add the new member

for (int x = 0; x < validators.Length; x++)
{
Assert.AreEqual(1, check.RemoveAll(u => u.SequenceEqual(validators[x].PublicKey.EncodePoint(true))));
Assert.AreEqual(0, validators[x].Votes);
}

Assert.AreEqual(0, check.Count);
}

[TestMethod]
Expand Down Expand Up @@ -357,4 +371,4 @@ internal static void CheckBalance(byte[] account, DataCache<StorageKey, StorageI
trackable.Item.IsConstant.Should().Be(false);
}
}
}
}
19 changes: 19 additions & 0 deletions neo/Ledger/StorageKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ public class StorageKey : IEquatable<StorageKey>, ISerializable

int ISerializable.Size => ScriptHash.Size + (Key.Length / 16 + 1) * 17;

internal static byte[] CreateSearchPrefix(UInt160 hash, byte[] prefix)
{
using (MemoryStream ms = new MemoryStream())
{
int index = 0;
int remain = prefix.Length;
while (remain >= 16)
{
ms.Write(prefix, index, 16);
ms.WriteByte(16);
index += 16;
remain -= 16;
}
if (remain > 0)
ms.Write(prefix, index, remain);
return hash.ToArray().Concat(ms.ToArray()).ToArray();
}
}

void ISerializable.Deserialize(BinaryReader reader)
{
ScriptHash = reader.ReadSerializable<UInt160>();
Expand Down
18 changes: 1 addition & 17 deletions neo/SmartContract/InteropService.NEO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Neo.VM;
using Neo.VM.Types;
using System;
using System.IO;
using System.Linq;
using VMArray = Neo.VM.Types.Array;

Expand Down Expand Up @@ -345,22 +344,7 @@ private static bool Storage_Find(ApplicationEngine engine)
StorageContext context = _interface.GetInterface<StorageContext>();
if (!CheckStorageContext(engine, context)) return false;
byte[] prefix = engine.CurrentContext.EvaluationStack.Pop().GetByteArray();
byte[] prefix_key;
using (MemoryStream ms = new MemoryStream())
{
int index = 0;
int remain = prefix.Length;
while (remain >= 16)
{
ms.Write(prefix, index, 16);
ms.WriteByte(16);
index += 16;
remain -= 16;
}
if (remain > 0)
ms.Write(prefix, index, remain);
prefix_key = context.ScriptHash.ToArray().Concat(ms.ToArray()).ToArray();
}
byte[] prefix_key = StorageKey.CreateSearchPrefix(context.ScriptHash, prefix);
StorageIterator iterator = engine.AddDisposable(new StorageIterator(engine.Snapshot.Storages.Find(prefix_key).Where(p => p.Key.Key.Take(prefix.Length).SequenceEqual(prefix)).GetEnumerator()));
engine.CurrentContext.EvaluationStack.Push(StackItem.FromInterface(iterator));
return true;
Expand Down
3 changes: 2 additions & 1 deletion neo/SmartContract/Native/Tokens/NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ private StackItem GetRegisteredValidators(ApplicationEngine engine, VMArray args

public IEnumerable<(ECPoint PublicKey, BigInteger Votes)> GetRegisteredValidators(Snapshot snapshot)
{
return snapshot.Storages.Find(new[] { Prefix_Validator }).Select(p =>
byte[] prefix_key = StorageKey.CreateSearchPrefix(Hash, new[] { Prefix_Validator });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks clear, but maybe add a comment about the Hash of the validators being the the one of NativeContract.
This Hash gives the idea of the local hash of the NeoToken.

return snapshot.Storages.Find(prefix_key).Select(p =>
(
p.Key.Key.Skip(1).ToArray().AsSerializable<ECPoint>(),
ValidatorState.FromByteArray(p.Value.Value).Votes
Expand Down