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") } }; }
public async Task Init(Block block, BallotOperation ballot) { Ballot = ballot; Ballot.Block ??= block; Ballot.Sender ??= Cache.Accounts.GetDelegate(ballot.SenderId); Ballot.Period ??= await Cache.Periods.CurrentAsync(); Ballot.Proposal ??= await Cache.Proposals.GetAsync(ballot.ProposalId); }
public virtual async Task Revert(Block block, BallotOperation ballot) { #region init ballot.Block ??= block; ballot.Sender ??= Cache.Accounts.GetDelegate(ballot.SenderId); ballot.Proposal ??= await Cache.Proposals.GetAsync(ballot.ProposalId); var snapshot = await Db.VotingSnapshots .FirstAsync(x => x.Period == ballot.Period && x.BakerId == ballot.Sender.Id); var period = await Cache.Periods.GetAsync(ballot.Period); #endregion #region entities var sender = ballot.Sender; //Db.TryAttach(block); Db.TryAttach(sender); Db.TryAttach(period); //Db.TryAttach(snapshot); #endregion #region revert operation if (ballot.Vote == Vote.Yay) { period.YayBallots--; period.YayRolls -= ballot.Rolls; snapshot.Status = VoterStatus.None; } else if (ballot.Vote == Vote.Nay) { period.NayBallots--; period.NayRolls -= ballot.Rolls; snapshot.Status = VoterStatus.None; } else { period.PassBallots--; period.PassRolls -= ballot.Rolls; snapshot.Status = VoterStatus.None; } sender.BallotsCount--; #endregion Db.BallotOps.Remove(ballot); }
public static async Task <BallotsCommit> Revert(ProtocolHandler proto, Block block, BallotOperation op) { var commit = new BallotsCommit(proto); await commit.Init(block, op); await commit.Revert(); return(commit); }