private void TryToBecomeAValidationDataCenter(VoteMinerInput input, long candidateVotesAmount,
                                                      DataCenterRankingList rankingList)
        {
            var  minimumVotes          = candidateVotesAmount;
            var  minimumVotesCandidate = input.CandidatePubkey;
            bool replaceWillHappen     = false;

            foreach (var pubkeyToVotesAmount in rankingList.DataCenters.OrderBy(x => x.Value))
            {
                if (pubkeyToVotesAmount.Value < minimumVotes)
                {
                    replaceWillHappen     = true;
                    minimumVotesCandidate = pubkeyToVotesAmount.Key;
                    break;
                }
            }

            if (replaceWillHappen)
            {
                State.DataCentersRankingList.Value.DataCenters.Remove(minimumVotesCandidate);
                State.DataCentersRankingList.Value.DataCenters.Add(input.CandidatePubkey,
                                                                   candidateVotesAmount);
                NotifyProfitReplaceCandidateInDataCenter(minimumVotesCandidate, input.CandidatePubkey);
            }
        }
Beispiel #2
0
        private void TryToBecomeAValidationDataCenter(VoteMinerInput input, long candidateVotesAmount,
                                                      DataCenterRankingList rankingList)
        {
            var  minimumVotes          = candidateVotesAmount;
            var  minimumVotesCandidate = input.CandidatePubkey;
            bool replaceWillHappen     = false;

            foreach (var pubkeyToVotesAmount in rankingList.DataCenters.Reverse())
            {
                if (pubkeyToVotesAmount.Value < minimumVotes)
                {
                    replaceWillHappen     = true;
                    minimumVotesCandidate = pubkeyToVotesAmount.Key;
                    break;
                }
            }

            if (replaceWillHappen)
            {
                State.DataCentersRankingList.Value.DataCenters.Remove(minimumVotesCandidate);
                State.DataCentersRankingList.Value.DataCenters.Add(input.CandidatePubkey,
                                                                   candidateVotesAmount);
                State.ProfitContract.RemoveBeneficiary.Send(new RemoveBeneficiaryInput
                {
                    SchemeId    = State.SubsidyHash.Value,
                    Beneficiary = Address.FromPublicKey(ByteArrayHelper.HexStringToByteArray(minimumVotesCandidate))
                });
                State.ProfitContract.AddBeneficiary.Send(new AddBeneficiaryInput
                {
                    SchemeId         = State.SubsidyHash.Value,
                    BeneficiaryShare = new BeneficiaryShare
                    {
                        Beneficiary =
                            Address.FromPublicKey(ByteArrayHelper.HexStringToByteArray(input.CandidatePubkey)),
                        Shares = 1
                    }
                });
            }
        }
        private bool IsUpdateDataCenterAfterMemberVoteAmountChange(DataCenterRankingList rankingList, string member)
        {
            var amountAfterWithdraw = rankingList.DataCenters[member];

            if (rankingList.DataCenters.Any(x => x.Value < amountAfterWithdraw))
            {
                return(false);
            }
            long   maxVoteAmountOutDataCenter = 0;
            string maxVoteOptionOutDataCenter = null;

            foreach (var candidateByteString in State.Candidates.Value.Value)
            {
                var candidatePublicKeyString = candidateByteString.ToHex();
                if (rankingList.DataCenters.ContainsKey(candidatePublicKeyString) ||
                    !State.CandidateInformationMap[candidatePublicKeyString].IsCurrentCandidate)
                {
                    continue;
                }
                var candidateVoteAmount = State.CandidateVotes[candidatePublicKeyString].ObtainedActiveVotedVotesAmount;
                if (maxVoteAmountOutDataCenter > candidateVoteAmount)
                {
                    continue;
                }
                maxVoteOptionOutDataCenter = candidatePublicKeyString;
                maxVoteAmountOutDataCenter = candidateVoteAmount;
            }

            if (maxVoteAmountOutDataCenter <= amountAfterWithdraw)
            {
                return(false);
            }
            rankingList.DataCenters.Remove(member);
            rankingList.DataCenters[maxVoteOptionOutDataCenter] = maxVoteAmountOutDataCenter;
            NotifyProfitReplaceCandidateInDataCenter(member, maxVoteOptionOutDataCenter);
            return(true);
        }
        private bool IsCandidateReplaceMemberInDataCenter(DataCenterRankingList rankingList, string candidate, long voteAmount)
        {
            var dateCenter = rankingList.DataCenters;

            if (dateCenter.Count < GetValidationDataCenterCount())
            {
                return(false);
            }
            if (dateCenter.ContainsKey(candidate))
            {
                return(false);
            }
            var list = dateCenter.ToList();
            var minimumVoteCandidateInDataCenter = list.OrderBy(x => x.Value).First();

            if (voteAmount <= minimumVoteCandidateInDataCenter.Value)
            {
                return(false);
            }
            dateCenter.Remove(minimumVoteCandidateInDataCenter.Key);
            dateCenter[candidate] = voteAmount;
            NotifyProfitReplaceCandidateInDataCenter(minimumVoteCandidateInDataCenter.Key, candidate);
            return(true);
        }