Skip to content

Commit

Permalink
Fix candidate (neo-project#1935)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored and cloud8little committed Jan 24, 2021
1 parent 75fe3d8 commit 51fc177
Showing 1 changed file with 15 additions and 11 deletions.
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);
}

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

0 comments on commit 51fc177

Please sign in to comment.