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

Fix GetCandidates #2686

Merged
merged 13 commits into from
May 4, 2022
50 changes: 43 additions & 7 deletions src/neo/SmartContract/Native/NeoToken.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -332,20 +333,55 @@ private async ContractTask<bool> Vote(ApplicationEngine engine, UInt160 account,
}

/// <summary>
/// Gets all registered candidates.
/// Gets the first 256 registered candidates.
/// </summary>
/// <param name="snapshot">The snapshot used to read data.</param>
/// <returns>All the registered candidates.</returns>
[ContractMethod(CpuFee = 1 << 22, RequiredCallFlags = CallFlags.ReadStates)]
public (ECPoint PublicKey, BigInteger Votes)[] GetCandidates(DataCache snapshot)
{
return GetCandidatesInternal(snapshot)
.Select(p => (p.PublicKey, p.State.Votes))
.Take(256)
.ToArray();
}

/// <summary>
/// Gets the registered candidates iterator.
/// </summary>
/// <param name="engine">The <see cref="ApplicationEngine"/> that is executing the contract.</param>
/// <returns>All the registered candidates.</returns>
[ContractMethod(CpuFee = 1 << 22, RequiredCallFlags = CallFlags.ReadStates)]
public IIterator GetAllCandidates(ApplicationEngine engine)
{
const FindOptions options = FindOptions.RemovePrefix | FindOptions.DeserializeValues | FindOptions.PickField1;
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.Key.AsSerializable<ECPoint>(1), p.Value.GetInteroperable<CandidateState>()))
.Where(p => p.Item2.Registered)
.Where(p => !Policy.IsBlocked(snapshot, Contract.CreateSignatureRedeemScript(p.Item1).ToScriptHash()))
.Select(p => (p.Item1, p.Item2.Votes))
.ToArray();
.Select(p => (p.Key, p.Value, PublicKey: p.Key.Key.AsSerializable<ECPoint>(1), State: p.Value.GetInteroperable<CandidateState>()))
.Where(p => p.State.Registered)
.Where(p => !Policy.IsBlocked(snapshot, Contract.CreateSignatureRedeemScript(p.PublicKey).ToScriptHash()));
}

/// <summary>
/// Gets votes from specific candidate.
/// </summary>
/// <param name="snapshot">The snapshot used to read data.</param>
/// <param name="pubKey">Specific public key</param>
/// <returns>Votes or -1 if it was not found.</returns>
[ContractMethod(CpuFee = 1 << 22, RequiredCallFlags = CallFlags.ReadStates)]
public BigInteger GetCandidateVote(DataCache snapshot, ECPoint pubKey)
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
{
StorageItem storage = snapshot.TryGet(CreateStorageKey(Prefix_Candidate).Add(pubKey));
CandidateState state = storage?.GetInteroperable<CandidateState>();
return state?.Registered == true ? state.Votes : -1;
}

/// <summary>
Expand Down