Skip to content

Commit

Permalink
Fixes GetRegisteredValidators() (neo-project#991)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored and Tommo-L committed Jun 22, 2020
1 parent 1a1dbaf commit 343d4da
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
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 });
return snapshot.Storages.Find(prefix_key).Select(p =>
(
p.Key.Key.Skip(1).ToArray().AsSerializable<ECPoint>(),
ValidatorState.FromByteArray(p.Value.Value).Votes
Expand Down

0 comments on commit 343d4da

Please sign in to comment.