From 6df7e7ddb8d0f122c4cff90331e53210c0b9dec4 Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 7 Apr 2022 10:33:51 +0200 Subject: [PATCH 01/12] Fix GetCandidates --- src/neo/SmartContract/Native/NeoToken.cs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/neo/SmartContract/Native/NeoToken.cs b/src/neo/SmartContract/Native/NeoToken.cs index 31a44a117d..4be3c53ab8 100644 --- a/src/neo/SmartContract/Native/NeoToken.cs +++ b/src/neo/SmartContract/Native/NeoToken.cs @@ -332,7 +332,7 @@ private async ContractTask Vote(ApplicationEngine engine, UInt160 account, } /// - /// Gets all registered candidates. + /// Gets the first 100 registered candidates. /// /// The snapshot used to read data. /// All the registered candidates. @@ -344,7 +344,26 @@ private async ContractTask Vote(ApplicationEngine engine, UInt160 account, ( p.Key.Key.AsSerializable(1), p.Value.GetInteroperable() - )).Where(p => p.Item2.Registered).Select(p => (p.Item1, p.Item2.Votes)).ToArray(); + )).Where(p => p.Item2.Registered).Select(p => (p.Item1, p.Item2.Votes)).Take(100).ToArray(); + } + + /// + /// Gets the first 100 registered candidates. + /// + /// The snapshot used to read data. + /// Specific public key + /// All the registered candidates. + [ContractMethod(CpuFee = 1 << 22, RequiredCallFlags = CallFlags.ReadStates)] + public BigInteger GetCandidateVote(DataCache snapshot, ECPoint pubKey) + { + byte[] prefix_key = CreateStorageKey(Prefix_Candidate).ToArray(); + var entry = snapshot.Find(prefix_key).Select(p => + ( + p.Key.Key.AsSerializable(1), + p.Value.GetInteroperable() + )).Where(p => p.Item2.Registered && p.Item1.Equals(pubKey)).FirstOrDefault(); + + return entry.Item2 == null ? -1 : entry.Item2.Votes; } /// From 9f4a0345f9b9d6dd624daadc27515bdfd8514ef9 Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 7 Apr 2022 10:36:15 +0200 Subject: [PATCH 02/12] Fix summary --- src/neo/SmartContract/Native/NeoToken.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/neo/SmartContract/Native/NeoToken.cs b/src/neo/SmartContract/Native/NeoToken.cs index 4be3c53ab8..6505270427 100644 --- a/src/neo/SmartContract/Native/NeoToken.cs +++ b/src/neo/SmartContract/Native/NeoToken.cs @@ -348,11 +348,11 @@ private async ContractTask Vote(ApplicationEngine engine, UInt160 account, } /// - /// Gets the first 100 registered candidates. + /// Gets votes from specific candidate. /// /// The snapshot used to read data. /// Specific public key - /// All the registered candidates. + /// Votes or -1 if it was not found. [ContractMethod(CpuFee = 1 << 22, RequiredCallFlags = CallFlags.ReadStates)] public BigInteger GetCandidateVote(DataCache snapshot, ECPoint pubKey) { From 3077365a6337d297a36d5876c90b95d96e1cd2fc Mon Sep 17 00:00:00 2001 From: Shargon Date: Wed, 20 Apr 2022 10:58:14 +0200 Subject: [PATCH 03/12] Add overload --- src/neo/SmartContract/Native/NeoToken.cs | 27 +++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/neo/SmartContract/Native/NeoToken.cs b/src/neo/SmartContract/Native/NeoToken.cs index 6505270427..e2aa6a8a22 100644 --- a/src/neo/SmartContract/Native/NeoToken.cs +++ b/src/neo/SmartContract/Native/NeoToken.cs @@ -331,6 +331,18 @@ private async ContractTask Vote(ApplicationEngine engine, UInt160 account, return true; } + /// + /// Gets the registered candidates count. + /// + /// The snapshot used to read data. + /// Count the registered candidates. + [ContractMethod(CpuFee = 1 << 22, RequiredCallFlags = CallFlags.ReadStates)] + public int GetCandidatesCount(DataCache snapshot) + { + byte[] prefix_key = CreateStorageKey(Prefix_Candidate).ToArray(); + return snapshot.Find(prefix_key).Select(p => p.Value.GetInteroperable()).Count(p => p.Registered); + } + /// /// Gets the first 100 registered candidates. /// @@ -338,13 +350,26 @@ private async ContractTask Vote(ApplicationEngine engine, UInt160 account, /// All the registered candidates. [ContractMethod(CpuFee = 1 << 22, RequiredCallFlags = CallFlags.ReadStates)] public (ECPoint PublicKey, BigInteger Votes)[] GetCandidates(DataCache snapshot) + { + return GetCandidates(snapshot, 0, 100); + } + + /// + /// Gets the first 100 registered candidates. + /// + /// The snapshot used to read data. + /// Number of candidates to skip + /// Number of candidates to return + /// All the registered candidates. + [ContractMethod(CpuFee = 1 << 22, RequiredCallFlags = CallFlags.ReadStates)] + public (ECPoint PublicKey, BigInteger Votes)[] GetCandidates(DataCache snapshot, int skip, int count) { byte[] prefix_key = CreateStorageKey(Prefix_Candidate).ToArray(); return snapshot.Find(prefix_key).Select(p => ( p.Key.Key.AsSerializable(1), p.Value.GetInteroperable() - )).Where(p => p.Item2.Registered).Select(p => (p.Item1, p.Item2.Votes)).Take(100).ToArray(); + )).Where(p => p.Item2.Registered).Select(p => (p.Item1, p.Item2.Votes)).Skip(skip).Take(count).ToArray(); } /// From ca71506590f00a59820e44fd30e59f5d969f8922 Mon Sep 17 00:00:00 2001 From: Shargon Date: Wed, 27 Apr 2022 14:09:12 +0200 Subject: [PATCH 04/12] Iterator version --- .../Iterators/KeyValueIterator.cs | 43 +++++++++++++++++++ src/neo/SmartContract/Native/NeoToken.cs | 34 ++++++--------- 2 files changed, 56 insertions(+), 21 deletions(-) create mode 100644 src/neo/SmartContract/Iterators/KeyValueIterator.cs diff --git a/src/neo/SmartContract/Iterators/KeyValueIterator.cs b/src/neo/SmartContract/Iterators/KeyValueIterator.cs new file mode 100644 index 0000000000..f182022e7a --- /dev/null +++ b/src/neo/SmartContract/Iterators/KeyValueIterator.cs @@ -0,0 +1,43 @@ +// Copyright (C) 2015-2021 The Neo Project. +// +// The neo is free software distributed under the MIT software license, +// see the accompanying file LICENSE in the main directory of the +// project or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using Neo.VM; +using Neo.VM.Types; +using System.Collections.Generic; + +namespace Neo.SmartContract.Iterators +{ + internal class KeyValueIterator : IIterator + { + private readonly IEnumerator<(StackItem Key, StackItem Value)> enumerator; + private readonly ReferenceCounter referenceCounter; + + public KeyValueIterator(IEnumerator<(StackItem, StackItem)> enumerator, ReferenceCounter referenceCounter) + { + this.enumerator = enumerator; + this.referenceCounter = referenceCounter; + } + + public void Dispose() + { + enumerator.Dispose(); + } + + public bool Next() + { + return enumerator.MoveNext(); + } + + public StackItem Value() + { + return new Struct(referenceCounter) { enumerator.Current.Key, enumerator.Current.Value }; + } + } +} diff --git a/src/neo/SmartContract/Native/NeoToken.cs b/src/neo/SmartContract/Native/NeoToken.cs index e2aa6a8a22..7c8c89768b 100644 --- a/src/neo/SmartContract/Native/NeoToken.cs +++ b/src/neo/SmartContract/Native/NeoToken.cs @@ -13,6 +13,7 @@ using Neo.Cryptography.ECC; using Neo.IO; using Neo.Persistence; +using Neo.SmartContract.Iterators; using Neo.VM; using Neo.VM.Types; using System; @@ -332,44 +333,35 @@ private async ContractTask Vote(ApplicationEngine engine, UInt160 account, } /// - /// Gets the registered candidates count. - /// - /// The snapshot used to read data. - /// Count the registered candidates. - [ContractMethod(CpuFee = 1 << 22, RequiredCallFlags = CallFlags.ReadStates)] - public int GetCandidatesCount(DataCache snapshot) - { - byte[] prefix_key = CreateStorageKey(Prefix_Candidate).ToArray(); - return snapshot.Find(prefix_key).Select(p => p.Value.GetInteroperable()).Count(p => p.Registered); - } - - /// - /// Gets the first 100 registered candidates. + /// Gets the first 256 registered candidates. /// /// The snapshot used to read data. /// All the registered candidates. [ContractMethod(CpuFee = 1 << 22, RequiredCallFlags = CallFlags.ReadStates)] public (ECPoint PublicKey, BigInteger Votes)[] GetCandidates(DataCache snapshot) { - return GetCandidates(snapshot, 0, 100); + byte[] prefix_key = CreateStorageKey(Prefix_Candidate).ToArray(); + return snapshot.Find(prefix_key).Select(p => + ( + p.Key.Key.AsSerializable(1), + p.Value.GetInteroperable() + )).Where(p => p.Item2.Registered).Select(p => (p.Item1, p.Item2.Votes)).Take(256).ToArray(); } /// - /// Gets the first 100 registered candidates. + /// Gets the registered candidates iterator. /// - /// The snapshot used to read data. - /// Number of candidates to skip - /// Number of candidates to return + /// The that is executing the contract. /// All the registered candidates. [ContractMethod(CpuFee = 1 << 22, RequiredCallFlags = CallFlags.ReadStates)] - public (ECPoint PublicKey, BigInteger Votes)[] GetCandidates(DataCache snapshot, int skip, int count) + public IIterator GetCandidatesIterator(ApplicationEngine engine) { byte[] prefix_key = CreateStorageKey(Prefix_Candidate).ToArray(); - return snapshot.Find(prefix_key).Select(p => + return new KeyValueIterator(engine.Snapshot.Find(prefix_key).Select(p => ( p.Key.Key.AsSerializable(1), p.Value.GetInteroperable() - )).Where(p => p.Item2.Registered).Select(p => (p.Item1, p.Item2.Votes)).Skip(skip).Take(count).ToArray(); + )).Where(p => p.Item2.Registered).Select(p => ((StackItem)new ByteString(p.Item1.ToArray()), (StackItem)new Integer(p.Item2.Votes))).GetEnumerator(), engine.ReferenceCounter); } /// From 0b6c93e1f473839f7c9cfcb4534b71c2651205b4 Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 28 Apr 2022 10:02:37 +0200 Subject: [PATCH 05/12] Update KeyValueIterator.cs --- src/neo/SmartContract/Iterators/KeyValueIterator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/SmartContract/Iterators/KeyValueIterator.cs b/src/neo/SmartContract/Iterators/KeyValueIterator.cs index f182022e7a..a41bb291f3 100644 --- a/src/neo/SmartContract/Iterators/KeyValueIterator.cs +++ b/src/neo/SmartContract/Iterators/KeyValueIterator.cs @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2021 The Neo Project. +// Copyright (C) 2015-2022 The Neo Project. // // The neo is free software distributed under the MIT software license, // see the accompanying file LICENSE in the main directory of the From ab4be82632dd95a91f4e2bf4ddae57c21c5216c4 Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 28 Apr 2022 10:03:01 +0200 Subject: [PATCH 06/12] Update NeoToken.cs --- src/neo/SmartContract/Native/NeoToken.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/SmartContract/Native/NeoToken.cs b/src/neo/SmartContract/Native/NeoToken.cs index 7c8c89768b..0f45a8aaaa 100644 --- a/src/neo/SmartContract/Native/NeoToken.cs +++ b/src/neo/SmartContract/Native/NeoToken.cs @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2021 The Neo Project. +// Copyright (C) 2015-2022 The Neo Project. // // The neo is free software distributed under the MIT software license, // see the accompanying file LICENSE in the main directory of the From 5327286ea04aae2fdfdf50133617cf743016a08c Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Thu, 28 Apr 2022 19:01:52 +0800 Subject: [PATCH 07/12] Delete KeyValueIterator.cs --- .../Iterators/KeyValueIterator.cs | 43 ------------------- src/neo/SmartContract/Native/NeoToken.cs | 13 +++--- 2 files changed, 8 insertions(+), 48 deletions(-) delete mode 100644 src/neo/SmartContract/Iterators/KeyValueIterator.cs diff --git a/src/neo/SmartContract/Iterators/KeyValueIterator.cs b/src/neo/SmartContract/Iterators/KeyValueIterator.cs deleted file mode 100644 index a41bb291f3..0000000000 --- a/src/neo/SmartContract/Iterators/KeyValueIterator.cs +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright (C) 2015-2022 The Neo Project. -// -// The neo is free software distributed under the MIT software license, -// see the accompanying file LICENSE in the main directory of the -// project or http://www.opensource.org/licenses/mit-license.php -// for more details. -// -// Redistribution and use in source and binary forms with or without -// modifications are permitted. - -using Neo.VM; -using Neo.VM.Types; -using System.Collections.Generic; - -namespace Neo.SmartContract.Iterators -{ - internal class KeyValueIterator : IIterator - { - private readonly IEnumerator<(StackItem Key, StackItem Value)> enumerator; - private readonly ReferenceCounter referenceCounter; - - public KeyValueIterator(IEnumerator<(StackItem, StackItem)> enumerator, ReferenceCounter referenceCounter) - { - this.enumerator = enumerator; - this.referenceCounter = referenceCounter; - } - - public void Dispose() - { - enumerator.Dispose(); - } - - public bool Next() - { - return enumerator.MoveNext(); - } - - public StackItem Value() - { - return new Struct(referenceCounter) { enumerator.Current.Key, enumerator.Current.Value }; - } - } -} diff --git a/src/neo/SmartContract/Native/NeoToken.cs b/src/neo/SmartContract/Native/NeoToken.cs index 7fdafde0d4..c7c67380be 100644 --- a/src/neo/SmartContract/Native/NeoToken.cs +++ b/src/neo/SmartContract/Native/NeoToken.cs @@ -358,12 +358,15 @@ private async ContractTask Vote(ApplicationEngine engine, UInt160 account, [ContractMethod(CpuFee = 1 << 22, RequiredCallFlags = CallFlags.ReadStates)] public IIterator GetCandidatesIterator(ApplicationEngine engine) { + const FindOptions options = FindOptions.RemovePrefix | FindOptions.DeserializeValues | FindOptions.PickField1; byte[] prefix_key = CreateStorageKey(Prefix_Candidate).ToArray(); - return new KeyValueIterator(engine.Snapshot.Find(prefix_key).Select(p => - ( - p.Key.Key.AsSerializable(1), - p.Value.GetInteroperable() - )).Where(p => p.Item2.Registered).Select(p => ((StackItem)new ByteString(p.Item1.ToArray()), (StackItem)new Integer(p.Item2.Votes))).GetEnumerator(), engine.ReferenceCounter); + var enumerator = engine.Snapshot.Find(prefix_key) + .Select(p => (p.Key, PublicKey: p.Key.Key.AsSerializable(1), Item: p.Value, State: p.Value.GetInteroperable())) + .Where(p => p.State.Registered) + .Where(p => !Policy.IsBlocked(engine.Snapshot, Contract.CreateSignatureRedeemScript(p.PublicKey).ToScriptHash())) + .Select(p => (p.Key, p.Item)) + .GetEnumerator(); + return new StorageIterator(enumerator, 1, options, engine.ReferenceCounter); } /// From d3be91330f3871cbe630ebfeb30f0af6400e2f14 Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Thu, 28 Apr 2022 19:10:40 +0800 Subject: [PATCH 08/12] Add GetCandidatesInternal --- src/neo/SmartContract/Native/NeoToken.cs | 27 ++++++++++++------------ 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/neo/SmartContract/Native/NeoToken.cs b/src/neo/SmartContract/Native/NeoToken.cs index c7c67380be..1e294d2eb7 100644 --- a/src/neo/SmartContract/Native/NeoToken.cs +++ b/src/neo/SmartContract/Native/NeoToken.cs @@ -340,12 +340,8 @@ private async ContractTask Vote(ApplicationEngine engine, UInt160 account, [ContractMethod(CpuFee = 1 << 22, RequiredCallFlags = CallFlags.ReadStates)] public (ECPoint PublicKey, BigInteger Votes)[] GetCandidates(DataCache snapshot) { - byte[] prefix_key = CreateStorageKey(Prefix_Candidate).ToArray(); - return snapshot.Find(prefix_key) - .Select(p => (p.Key.Key.AsSerializable(1), p.Value.GetInteroperable())) - .Where(p => p.Item2.Registered) - .Where(p => !Policy.IsBlocked(snapshot, Contract.CreateSignatureRedeemScript(p.Item1).ToScriptHash())) - .Select(p => (p.Item1, p.Item2.Votes)) + return GetCandidatesInternal(snapshot) + .Select(p => (p.PublicKey, p.State.Votes)) .Take(256) .ToArray(); } @@ -356,19 +352,24 @@ private async ContractTask Vote(ApplicationEngine engine, UInt160 account, /// The that is executing the contract. /// All the registered candidates. [ContractMethod(CpuFee = 1 << 22, RequiredCallFlags = CallFlags.ReadStates)] - public IIterator GetCandidatesIterator(ApplicationEngine engine) + public IIterator GetAllCandidates(ApplicationEngine engine) { const FindOptions options = FindOptions.RemovePrefix | FindOptions.DeserializeValues | FindOptions.PickField1; - byte[] prefix_key = CreateStorageKey(Prefix_Candidate).ToArray(); - var enumerator = engine.Snapshot.Find(prefix_key) - .Select(p => (p.Key, PublicKey: p.Key.Key.AsSerializable(1), Item: p.Value, State: p.Value.GetInteroperable())) - .Where(p => p.State.Registered) - .Where(p => !Policy.IsBlocked(engine.Snapshot, Contract.CreateSignatureRedeemScript(p.PublicKey).ToScriptHash())) - .Select(p => (p.Key, p.Item)) + var enumerator = GetCandidatesInternal(engine.Snapshot) + .Select(p => (p.Key, p.Value)) .GetEnumerator(); return new StorageIterator(enumerator, 1, options, engine.ReferenceCounter); } + private IEnumerable<(StorageKey Key, StorageItem Value, ECPoint PublicKey, CandidateState State)> GetCandidatesInternal(DataCache snapshot) + { + byte[] prefix_key = CreateStorageKey(Prefix_Candidate).ToArray(); + return snapshot.Find(prefix_key) + .Select(p => (p.Key, p.Value, PublicKey: p.Key.Key.AsSerializable(1), State: p.Value.GetInteroperable())) + .Where(p => p.State.Registered) + .Where(p => !Policy.IsBlocked(snapshot, Contract.CreateSignatureRedeemScript(p.PublicKey).ToScriptHash())); + } + /// /// Gets votes from specific candidate. /// From d234dfac2caca2704ffaff8740cf7227780285be Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Thu, 28 Apr 2022 19:17:57 +0800 Subject: [PATCH 09/12] Optimize GetCandidateVote --- src/neo/SmartContract/Native/NeoToken.cs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/neo/SmartContract/Native/NeoToken.cs b/src/neo/SmartContract/Native/NeoToken.cs index 1e294d2eb7..15c66eddfc 100644 --- a/src/neo/SmartContract/Native/NeoToken.cs +++ b/src/neo/SmartContract/Native/NeoToken.cs @@ -379,14 +379,9 @@ public IIterator GetAllCandidates(ApplicationEngine engine) [ContractMethod(CpuFee = 1 << 22, RequiredCallFlags = CallFlags.ReadStates)] public BigInteger GetCandidateVote(DataCache snapshot, ECPoint pubKey) { - byte[] prefix_key = CreateStorageKey(Prefix_Candidate).ToArray(); - var entry = snapshot.Find(prefix_key).Select(p => - ( - p.Key.Key.AsSerializable(1), - p.Value.GetInteroperable() - )).Where(p => p.Item2.Registered && p.Item1.Equals(pubKey)).FirstOrDefault(); - - return entry.Item2 == null ? -1 : entry.Item2.Votes; + StorageItem storage = snapshot.TryGet(CreateStorageKey(Prefix_Candidate).Add(pubKey)); + CandidateState state = storage?.GetInteroperable(); + return state?.Registered == true ? state.Votes : -1; } /// From e481121cecbbe390e68ba6dd741a0d074126dc9e Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Fri, 29 Apr 2022 06:42:52 +0800 Subject: [PATCH 10/12] Fix fee --- src/neo/SmartContract/Native/NeoToken.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/SmartContract/Native/NeoToken.cs b/src/neo/SmartContract/Native/NeoToken.cs index 15c66eddfc..965995bfc6 100644 --- a/src/neo/SmartContract/Native/NeoToken.cs +++ b/src/neo/SmartContract/Native/NeoToken.cs @@ -376,7 +376,7 @@ public IIterator GetAllCandidates(ApplicationEngine engine) /// The snapshot used to read data. /// Specific public key /// Votes or -1 if it was not found. - [ContractMethod(CpuFee = 1 << 22, RequiredCallFlags = CallFlags.ReadStates)] + [ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)] public BigInteger GetCandidateVote(DataCache snapshot, ECPoint pubKey) { StorageItem storage = snapshot.TryGet(CreateStorageKey(Prefix_Candidate).Add(pubKey)); From 984771aa928a8d5a19e0da06d82ead40ccedef68 Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Fri, 29 Apr 2022 06:49:10 +0800 Subject: [PATCH 11/12] Fix ComputeCommitteeMembers --- src/neo/SmartContract/Native/NeoToken.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/neo/SmartContract/Native/NeoToken.cs b/src/neo/SmartContract/Native/NeoToken.cs index 965995bfc6..0a4a4f2bce 100644 --- a/src/neo/SmartContract/Native/NeoToken.cs +++ b/src/neo/SmartContract/Native/NeoToken.cs @@ -438,7 +438,9 @@ public ECPoint[] ComputeNextBlockValidators(DataCache snapshot, ProtocolSettings { decimal votersCount = (decimal)(BigInteger)snapshot[CreateStorageKey(Prefix_VotersCount)]; decimal voterTurnout = votersCount / (decimal)TotalAmount; - var candidates = GetCandidates(snapshot); + var candidates = GetCandidatesInternal(snapshot) + .Select(p => (p.PublicKey, p.State.Votes)) + .ToArray(); if (voterTurnout < EffectiveVoterTurnout || candidates.Length < settings.CommitteeMembersCount) return settings.StandbyCommittee.Select(p => (p, candidates.FirstOrDefault(k => k.PublicKey.Equals(p)).Votes)); return candidates From 3d0ea0b0446feeb38e3906a5121eff803d41a697 Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Fri, 29 Apr 2022 06:54:32 +0800 Subject: [PATCH 12/12] Fix UT --- src/neo/SmartContract/Native/NeoToken.cs | 6 +++--- .../SmartContract/Native/UT_NeoToken.cs | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/neo/SmartContract/Native/NeoToken.cs b/src/neo/SmartContract/Native/NeoToken.cs index 0a4a4f2bce..6aebca8b26 100644 --- a/src/neo/SmartContract/Native/NeoToken.cs +++ b/src/neo/SmartContract/Native/NeoToken.cs @@ -338,7 +338,7 @@ private async ContractTask Vote(ApplicationEngine engine, UInt160 account, /// The snapshot used to read data. /// All the registered candidates. [ContractMethod(CpuFee = 1 << 22, RequiredCallFlags = CallFlags.ReadStates)] - public (ECPoint PublicKey, BigInteger Votes)[] GetCandidates(DataCache snapshot) + private (ECPoint PublicKey, BigInteger Votes)[] GetCandidates(DataCache snapshot) { return GetCandidatesInternal(snapshot) .Select(p => (p.PublicKey, p.State.Votes)) @@ -352,7 +352,7 @@ private async ContractTask Vote(ApplicationEngine engine, UInt160 account, /// The that is executing the contract. /// All the registered candidates. [ContractMethod(CpuFee = 1 << 22, RequiredCallFlags = CallFlags.ReadStates)] - public IIterator GetAllCandidates(ApplicationEngine engine) + private IIterator GetAllCandidates(ApplicationEngine engine) { const FindOptions options = FindOptions.RemovePrefix | FindOptions.DeserializeValues | FindOptions.PickField1; var enumerator = GetCandidatesInternal(engine.Snapshot) @@ -361,7 +361,7 @@ public IIterator GetAllCandidates(ApplicationEngine engine) return new StorageIterator(enumerator, 1, options, engine.ReferenceCounter); } - private IEnumerable<(StorageKey Key, StorageItem Value, ECPoint PublicKey, CandidateState State)> GetCandidatesInternal(DataCache snapshot) + internal IEnumerable<(StorageKey Key, StorageItem Value, ECPoint PublicKey, CandidateState State)> GetCandidatesInternal(DataCache snapshot) { byte[] prefix_key = CreateStorageKey(Prefix_Candidate).ToArray(); return snapshot.Find(prefix_key) diff --git a/tests/neo.UnitTests/SmartContract/Native/UT_NeoToken.cs b/tests/neo.UnitTests/SmartContract/Native/UT_NeoToken.cs index a627288978..0dadd0a71f 100644 --- a/tests/neo.UnitTests/SmartContract/Native/UT_NeoToken.cs +++ b/tests/neo.UnitTests/SmartContract/Native/UT_NeoToken.cs @@ -227,8 +227,8 @@ public void Check_RegisterValidator() // Check GetRegisteredValidators - var members = NativeContract.NEO.GetCandidates(snapshot); - Assert.AreEqual(2, members.Length); + var members = NativeContract.NEO.GetCandidatesInternal(snapshot); + Assert.AreEqual(2, members.Count()); } [TestMethod] @@ -252,8 +252,8 @@ public void Check_UnregisterCandidate() ret.State.Should().BeTrue(); ret.Result.Should().BeTrue(); - var members = NativeContract.NEO.GetCandidates(snapshot); - Assert.AreEqual(1, members.Length); + var members = NativeContract.NEO.GetCandidatesInternal(snapshot); + Assert.AreEqual(1, members.Count()); snapshot.GetChangeSet().Count().Should().Be(keyCount + 1); StorageKey key = CreateStorageKey(33, point); snapshot.TryGet(key).Should().NotBeNull(); @@ -263,8 +263,8 @@ public void Check_UnregisterCandidate() ret.Result.Should().BeTrue(); snapshot.GetChangeSet().Count().Should().Be(keyCount); - members = NativeContract.NEO.GetCandidates(snapshot); - Assert.AreEqual(0, members.Length); + members = NativeContract.NEO.GetCandidatesInternal(snapshot); + Assert.AreEqual(0, members.Count()); snapshot.TryGet(key).Should().BeNull(); //register with votes, then unregister @@ -567,12 +567,12 @@ public void TestGetCandidates1() public void TestGetCandidates2() { var snapshot = _snapshot.CreateSnapshot(); - var result = NativeContract.NEO.GetCandidates(snapshot); - result.Length.Should().Be(0); + var result = NativeContract.NEO.GetCandidatesInternal(snapshot); + result.Count().Should().Be(0); StorageKey key = NativeContract.NEO.CreateStorageKey(33, ECCurve.Secp256r1.G); snapshot.Add(key, new StorageItem(new CandidateState())); - NativeContract.NEO.GetCandidates(snapshot).Length.Should().Be(1); + NativeContract.NEO.GetCandidatesInternal(snapshot).Count().Should().Be(1); } [TestMethod]