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 candidate #1935

Merged
merged 1 commit into from
Sep 15, 2020
Merged
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
26 changes: 15 additions & 11 deletions src/neo/SmartContract/Native/Tokens/NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ protected override void OnBalanceChanging(ApplicationEngine engine, UInt160 acco
{
DistributeGas(engine, account, state);
if (amount.IsZero) return;
if (state.VoteTo != null)
{
engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_Candidate).Add(state.VoteTo)).GetInteroperable<CandidateState>().Votes += amount;
engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_VotersCount)).Add(amount);
}
if (state.VoteTo is null) return;
engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_VotersCount)).Add(amount);
StorageKey key = CreateStorageKey(Prefix_Candidate).Add(state.VoteTo);
CandidateState candidate = engine.Snapshot.Storages.GetAndChange(key).GetInteroperable<CandidateState>();
candidate.Votes += amount;
CheckCandidate(engine.Snapshot, key, candidate);
}

private void DistributeGas(ApplicationEngine engine, UInt160 account, NeoAccountState state)
Expand Down Expand Up @@ -89,6 +90,12 @@ private BigInteger CalculateBonus(StoreView snapshot, BigInteger value, uint sta
return value * sum * NeoHolderRewardRatio / 100 / TotalAmount;
}

private static void CheckCandidate(StoreView snapshot, StorageKey key, CandidateState candidate)
{
if (!candidate.Registered && candidate.Votes.IsZero)
snapshot.Storages.Delete(key);
shargon marked this conversation as resolved.
Show resolved Hide resolved
}

private bool ShouldRefreshCommittee(uint height) => height % (ProtocolSettings.Default.CommitteeMembersCount + ProtocolSettings.Default.ValidatorsCount) == 0;

internal override void Initialize(ApplicationEngine engine)
Expand Down Expand Up @@ -189,10 +196,8 @@ private bool UnregisterCandidate(ApplicationEngine engine, ECPoint pubkey)
if (engine.Snapshot.Storages.TryGet(key) is null) return true;
StorageItem item = engine.Snapshot.Storages.GetAndChange(key);
CandidateState state = item.GetInteroperable<CandidateState>();
if (state.Votes.IsZero)
engine.Snapshot.Storages.Delete(key);
else
state.Registered = false;
state.Registered = false;
CheckCandidate(engine.Snapshot, key, state);
return true;
}

Expand Down Expand Up @@ -223,8 +228,7 @@ private bool Vote(ApplicationEngine engine, UInt160 account, ECPoint voteTo)
StorageItem storage_validator = engine.Snapshot.Storages.GetAndChange(key);
CandidateState state_validator = storage_validator.GetInteroperable<CandidateState>();
state_validator.Votes -= state_account.Balance;
if (!state_validator.Registered && state_validator.Votes.IsZero)
engine.Snapshot.Storages.Delete(key);
CheckCandidate(engine.Snapshot, key, state_validator);
}
state_account.VoteTo = voteTo;
if (validator_new != null)
Expand Down