Esempio n. 1
0
        public void BootstrapVoting(Protocol protocol, List <Account> accounts)
        {
            var snapshots = accounts
                            .Where(x => x.Type == AccountType.Delegate)
                            .Select(x => x as Delegate)
                            .Select(x => new VotingSnapshot
            {
                Level   = 1,
                Period  = 0,
                BakerId = x.Id,
                Rolls   = (int)(x.StakingBalance / protocol.TokensPerRoll),
                Status  = VoterStatus.None
            });

            var period = new VotingPeriod
            {
                Index          = 0,
                Epoch          = 0,
                FirstLevel     = 1,
                LastLevel      = protocol.BlocksPerVoting,
                Kind           = PeriodKind.Proposal,
                Status         = PeriodStatus.Active,
                TotalBakers    = snapshots.Count(),
                TotalRolls     = snapshots.Sum(x => x.Rolls),
                UpvotesQuorum  = protocol.ProposalQuorum,
                ProposalsCount = 0,
                TopUpvotes     = 0,
                TopRolls       = 0
            };

            Db.VotingSnapshots.AddRange(snapshots);
            Db.VotingPeriods.Add(period);
            Cache.Periods.Add(period);
        }
Esempio n. 2
0
        protected PeriodStatus GetPeriodStatus(VotingPeriod p)
        {
            if (p.Kind == PeriodKind.Proposal)
            {
                if (p.ProposalsCount == 0)
                {
                    return(PeriodStatus.NoProposals);
                }

                if (p.TopRolls < p.TotalRolls * p.UpvotesQuorum / 10000)
                {
                    return(PeriodStatus.NoQuorum);
                }
            }
            else if (p.Kind == PeriodKind.Exploration || p.Kind == PeriodKind.Promotion)
            {
                if (p.YayRolls + p.NayRolls + p.PassRolls < p.TotalRolls * p.BallotsQuorum / 10000)
                {
                    return(PeriodStatus.NoQuorum);
                }

                if (p.YayRolls < (p.YayRolls + p.NayRolls) * p.Supermajority / 10000)
                {
                    return(PeriodStatus.NoSupermajority);
                }
            }

            return(PeriodStatus.Success);
        }
Esempio n. 3
0
 void FillBallots(StringBuilder post, VotingPeriod vp)
 {
     post.AppendLine("<ul>");
     post.AppendLine($"<li>Yay - {vp.yayRolls} rolls ({(100D * vp.yayRolls / (vp.yayRolls + vp.nayRolls + vp.passRolls)) ?? 0:##0.0}%)</li>");
     post.AppendLine($"<li>Nay - {vp.nayRolls} rolls ({(100D * vp.nayRolls / (vp.yayRolls + vp.nayRolls + vp.passRolls)) ?? 0:##0.0}%)</li>");
     post.AppendLine($"<li>Pass - {vp.passRolls} rolls ({(100D * vp.passRolls / (vp.yayRolls + vp.nayRolls + vp.passRolls)) ?? 0:##0.0}%)</li>");
     post.AppendLine("</ul>");
 }
Esempio n. 4
0
 protected virtual VotingPeriod StartNextPeriod(Block block, VotingPeriod current)
 {
     return(current.Kind switch
     {
         PeriodKind.Proposal => StartBallotPeriod(block, current, PeriodKind.Exploration),
         PeriodKind.Exploration => StartWaitingPeriod(block, current, PeriodKind.Testing),
         PeriodKind.Testing => StartBallotPeriod(block, current, PeriodKind.Promotion),
         PeriodKind.Promotion => StartProposalPeriod(block, current),
         _ => throw new Exception("Invalid voting period kind")
     });
Esempio n. 5
0
 void BallotQuorum(StringBuilder post, VotingPeriod vp)
 {
     if (vp.ballotsQuorum < (100M * (vp.yayRolls + vp.nayRolls + vp.passRolls) / vp.totalRolls))
     {
         post.AppendLine($"<p>Quorum of {vp.ballotsQuorum:#0.0}% reached</p>");
     }
     else
     {
         post.AppendLine($"<p>Quorum of {vp.ballotsQuorum:#0.0}% not reached</p>");
     }
 }
Esempio n. 6
0
        public async Task Init(RawBlock rawBlock)
        {
            var protocol = await Cache.Protocols.GetAsync(rawBlock.Protocol);

            VotingPeriod = new ProposalPeriod
            {
                Code  = 0,
                Epoch = new VotingEpoch {
                    Level = rawBlock.Level
                },
                Kind       = VotingPeriods.Proposal,
                StartLevel = rawBlock.Level,
                EndLevel   = protocol.BlocksPerVoting
            };
        }
Esempio n. 7
0
        protected override int GetParticipationEma(VotingPeriod period, Protocol proto)
        {
            var prev = Db.VotingPeriods
                       .AsNoTracking()
                       .OrderByDescending(x => x.Index)
                       .FirstOrDefault(x => x.Kind == PeriodKind.Exploration || x.Kind == PeriodKind.Promotion);

            if (prev != null)
            {
                var participation = 10000 * (prev.YayRolls + prev.NayRolls + prev.PassRolls) / prev.TotalRolls;
                return(((int)prev.ParticipationEma * 8000 + (int)participation * 2000) / 10000);
            }

            return(proto.BallotQuorumMax);
        }
Esempio n. 8
0
        protected virtual ProposalStatus GetProposalStatus(Proposal proposal, VotingPeriod period)
        {
            if (period.Status == PeriodStatus.Success)
            {
                return(period.Kind == PeriodKind.Promotion
                    ? ProposalStatus.Accepted
                    : ProposalStatus.Active);
            }

            if (period.Status == PeriodStatus.NoSupermajority)
            {
                return(ProposalStatus.Rejected);
            }

            return(ProposalStatus.Skipped);
        }
Esempio n. 9
0
        protected async Task UpdateProposalsStatus(VotingPeriod p)
        {
            if (p.Kind == PeriodKind.Proposal)
            {
                if (p.Status == PeriodStatus.NoProposals)
                {
                    return;
                }

                var proposals = await Db.Proposals
                                .Where(x => x.Status == ProposalStatus.Active)
                                .ToListAsync();

                foreach (var proposal in proposals)
                {
                    proposal.Status = ProposalStatus.Skipped;
                }

                if (p.Status == PeriodStatus.Success)
                {
                    var winner = proposals.First(x => x.Rolls == p.TopRolls);
                    winner.Status = GetProposalStatus(winner, p);
                    if (winner.Status == ProposalStatus.Active)
                    {
                        winner.LastPeriod = p.Index + 1;
                    }
                }
            }
            else
            {
                var proposal = await Db.Proposals.FirstAsync(x => x.Status == ProposalStatus.Active);

                proposal.Status = GetProposalStatus(proposal, p);
                if (proposal.Status == ProposalStatus.Active)
                {
                    proposal.LastPeriod = p.Index + 1;
                }
            }
        }
Esempio n. 10
0
 public void Remove()
 {
     VotingPeriod = null;
 }
Esempio n. 11
0
 public void Add(VotingPeriod period)
 {
     VotingPeriod = period;
 }
Esempio n. 12
0
 public void Reset()
 {
     VotingPeriod = null;
 }
Esempio n. 13
0
 public async Task Init(Block block)
 {
     VotingPeriod = await Db.VotingPeriods.Include(x => x.Epoch).SingleAsync();
 }
Esempio n. 14
0
 protected override int GetBallotQuorum(VotingPeriod period, Protocol proto)
 {
     return(proto.BallotQuorumMin + (int)period.ParticipationEma * (proto.BallotQuorumMax - proto.BallotQuorumMin) / 10000);
 }
Esempio n. 15
0
 public void Remove(VotingPeriod period)
 {
     Cached.Remove(period.Index);
 }
Esempio n. 16
0
 public void Add(VotingPeriod period)
 {
     CheckSpace();
     Cached[period.Index] = period;
 }