Beispiel #1
0
        public async Task Init(Block block, RawOperation op, RawBallotContent content)
        {
            var period = await Cache.Periods.CurrentAsync();
            var proposal = await Cache.Proposals.GetAsync((period as ExplorationPeriod)?.ProposalId ?? (period as PromotionPeriod).ProposalId);
            var sender = Cache.Accounts.GetDelegate(content.Source);

            var rolls = 0;
            if (block.Events.HasFlag(BlockEvents.VotingPeriodBegin))
                rolls = (int)(sender.StakingBalance / block.Protocol.TokensPerRoll);
            else
                rolls = (await Db.VotingSnapshots.FirstAsync(x => x.PeriodId == period.Id && x.DelegateId == sender.Id)).Rolls;

            Ballot = new BallotOperation
            {
                Id = Cache.AppState.NextOperationId(),
                Block = block,
                Level = block.Level,
                Timestamp = block.Timestamp,
                OpHash = op.Hash,
                Sender = sender,
                Rolls = rolls,
                Period = period,
                Proposal = proposal,
                Vote = content.Ballot switch
                {
                    "yay" => Vote.Yay,
                    "nay" => Vote.Nay,
                    "pass" => Vote.Pass,
                    _ => throw new Exception("invalid ballot value")
                }
            };
        }
Beispiel #2
0
        protected async Task ValidateBallot(RawBallotContent ballot, RawBlock rawBlock)
        {
            var period = await Cache.Periods.CurrentAsync();

            if (period.EndLevel == rawBlock.Level - 1)
            {
                return;
            }

            var proposal = await Cache.Proposals.GetAsync((period as ExplorationPeriod)?.ProposalId ?? (period as PromotionPeriod).ProposalId);

            if (proposal.Hash != ballot.Proposal)
            {
                throw new ValidationException("invalid ballot proposal");
            }

            if (!Cache.Accounts.DelegateExists(ballot.Source))
            {
                throw new ValidationException("invalid proposal sender");
            }

            if (ballot.Period != rawBlock.Metadata.LevelInfo.VotingPeriod)
            {
                throw new ValidationException("invalid proposal voting period");
            }
        }
Beispiel #3
0
        public static async Task<BallotsCommit> Apply(ProtocolHandler proto, Block block, RawOperation op, RawBallotContent content)
        {
            var commit = new BallotsCommit(proto);
            await commit.Init(block, op, content);
            await commit.Apply();

            return commit;
        }