public override async Task Apply() { FutureBakingRights = new List <List <RawBakingRight> >(Block.Protocol.PreservedCycles + 1); FutureEndorsingRights = new List <List <RawEndorsingRight> >(Block.Protocol.PreservedCycles + 1); var delegates = BootstrapedAccounts .Where(x => x.Type == AccountType.Delegate) .ToDictionary(k => k.Address, v => v.Id); for (int cycle = 0; cycle <= Block.Protocol.PreservedCycles; cycle++) { using var bakingRightsStream = await Proto.Node.GetBakingRightsAsync(1, cycle, BakingRight.MaxPriority + 1); var bakingRights = await(Proto.Serializer as Serializer).DeserializeBakingRights(bakingRightsStream); using var endorsingRightsStream = await Proto.Node.GetEndorsingRightsAsync(1, cycle); var endorsingRights = await(Proto.Serializer as Serializer).DeserializeEndorsingRights(endorsingRightsStream); FutureBakingRights.Add(bakingRights); FutureEndorsingRights.Add(endorsingRights); var conn = Db.Database.GetDbConnection() as NpgsqlConnection; using var writer = conn.BeginBinaryImport(@"COPY ""BakingRights"" (""Cycle"", ""Level"", ""BakerId"", ""Type"", ""Status"", ""Priority"", ""Slots"") FROM STDIN (FORMAT BINARY)"); foreach (var er in endorsingRights) { writer.StartRow(); writer.Write(er.Level / Block.Protocol.BlocksPerCycle, NpgsqlTypes.NpgsqlDbType.Integer); // level + 1 (shifted) writer.Write(er.Level + 1, NpgsqlTypes.NpgsqlDbType.Integer); // level + 1 (shifted) writer.Write(delegates[er.Delegate], NpgsqlTypes.NpgsqlDbType.Integer); writer.Write((byte)BakingRightType.Endorsing, NpgsqlTypes.NpgsqlDbType.Smallint); writer.Write((byte)BakingRightStatus.Future, NpgsqlTypes.NpgsqlDbType.Smallint); writer.WriteNull(); writer.Write(er.Slots.Count, NpgsqlTypes.NpgsqlDbType.Integer); } var skipLevel = FutureBakingRights[cycle][0].Level; //skip bootstrap block rights foreach (var br in bakingRights.SkipWhile(x => cycle == 0 && x.Level == skipLevel)) { writer.StartRow(); writer.Write(cycle, NpgsqlTypes.NpgsqlDbType.Integer); writer.Write(br.Level, NpgsqlTypes.NpgsqlDbType.Integer); writer.Write(delegates[br.Delegate], NpgsqlTypes.NpgsqlDbType.Integer); writer.Write((byte)BakingRightType.Baking, NpgsqlTypes.NpgsqlDbType.Smallint); writer.Write((byte)BakingRightStatus.Future, NpgsqlTypes.NpgsqlDbType.Smallint); writer.Write(br.Priority, NpgsqlTypes.NpgsqlDbType.Integer); writer.WriteNull(); } writer.Complete(); } }
public override Task Apply() { var bakers = BootstrapedAccounts .Where(x => x.Type == AccountType.Delegate) .Select(x => x as Data.Models.Delegate); var totalRolls = bakers.Sum(x => (int)(x.StakingBalance / Block.Protocol.TokensPerRoll)); for (int cycle = 0; cycle <= Block.Protocol.PreservedCycles; cycle++) { var bakerCycles = bakers.ToDictionary(x => x.Address, x => { var rolls = (int)(x.StakingBalance / Block.Protocol.TokensPerRoll); var rollsShare = (double)rolls / totalRolls; var bakerCycle = new BakerCycle { Cycle = cycle, BakerId = x.Id, Rolls = rolls, StakingBalance = x.StakingBalance, DelegatedBalance = x.StakingBalance - x.Balance, //nothing is frozen yet DelegatorsCount = x.DelegatorsCount, ExpectedBlocks = Block.Protocol.BlocksPerCycle * rollsShare, ExpectedEndorsements = Block.Protocol.EndorsersPerBlock * Block.Protocol.BlocksPerCycle * rollsShare }; return(bakerCycle); }); #region future baking rights var skipLevel = FutureBakingRights[cycle][0].Level; //skip bootstrap block rights foreach (var br in FutureBakingRights[cycle].SkipWhile(x => cycle == 0 && x.Level == skipLevel)) { if (br.Priority > 0) { continue; } if (!bakerCycles.TryGetValue(br.Delegate, out var bakerCycle)) { throw new Exception("Unknown baking right recipient"); } bakerCycle.FutureBlocks++; bakerCycle.FutureBlockDeposits += GetBlockDeposit(Block.Protocol, cycle); //bakerCycle.FutureBlockRewards += GetBlockReward(Block.Protocol, cycle); } #endregion #region future endorsing rights skipLevel = FutureEndorsingRights[cycle][^ 1].Level; //skip shifted rights
public override async Task Apply() { Snapshots = BootstrapedAccounts .Where(x => x.Type == AccountType.Delegate) .Select(x => x as Data.Models.Delegate) .ToDictionary(x => x.Id, x => new DelegateSnapshot { StakingBalance = x.StakingBalance, DelegatedBalance = x.StakingBalance - x.Balance, //nothing is frozen yet DelegatorsCount = x.DelegatorsCount }); var totalRolls = Snapshots.Values.Sum(x => (int)(x.StakingBalance / Block.Protocol.TokensPerRoll)); var totalStake = Snapshots.Values.Sum(x => x.StakingBalance); var totalDelegated = Snapshots.Values.Sum(x => x.DelegatedBalance); var totalDelegators = Snapshots.Values.Sum(x => x.DelegatorsCount); var totalBakers = Snapshots.Count; for (int cycle = 0; cycle <= Block.Protocol.PreservedCycles; cycle++) { var cycleStream = await Proto.Node.GetCycleAsync(1, cycle); var rawCycle = await(Proto.Serializer as Serializer).DeserializeCycle(cycleStream); Db.Cycles.Add(new Cycle { Index = cycle, SnapshotIndex = 0, SnapshotLevel = 1, TotalRolls = totalRolls, TotalStaking = totalStake, TotalDelegated = totalDelegated, TotalDelegators = totalDelegators, TotalBakers = totalBakers, Seed = rawCycle.RandomSeed }); } }