public override async Task Apply() { var cycle = (Block.Level - 1) / Block.Protocol.BlocksPerCycle; #region current rights CurrentRights = await Cache.BakingRights.GetAsync(cycle, Block.Level); var sql = string.Empty; // TODO: better use protocol of the block where the endorsing rights were generated if (Block.Priority == 0 && Block.Validations == Block.Protocol.EndorsersPerBlock) { CurrentRights.RemoveAll(x => x.Type == BakingRightType.Baking && x.Priority > 0); CurrentRights.ForEach(x => x.Status = BakingRightStatus.Realized); sql = $@" DELETE FROM ""BakingRights"" WHERE ""Level"" = {Block.Level} AND ""Type"" = {(int)BakingRightType.Baking} AND ""Priority"" > 0; UPDATE ""BakingRights"" SET ""Status"" = {(int)BakingRightStatus.Realized} WHERE ""Level"" = {Block.Level};"; } else { #region load missed priority var maxExistedPriority = CurrentRights .Where(x => x.Type == BakingRightType.Baking) .Select(x => x.Priority) .Max(); if (maxExistedPriority < Block.Priority) { using var stream = await Proto.Node.GetLevelBakingRightsAsync(Block.Level, Block.Priority + 1); var bakingRights = await(Proto.Serializer as Serializer).DeserializeBakingRights(stream); //bakingRights = bakingRights.OrderBy(x => x.Priority); var sqlInsert = @" INSERT INTO ""BakingRights"" (""Cycle"", ""Level"", ""BakerId"", ""Type"", ""Status"", ""Priority"", ""Slots"") VALUES "; foreach (var bakingRight in bakingRights.SkipWhile(x => x.Priority <= maxExistedPriority)) { var delegat = Cache.Accounts.GetDelegateOrDefault(bakingRight.Delegate); if (delegat == null) { continue; // WTF: [level:28680] - Baking rights were given to non-baker account } sqlInsert += $@" ({cycle}, {Block.Level}, {delegat.Id}, {(int)BakingRightType.Baking}, {(int)BakingRightStatus.Future}, {bakingRight.Priority}, null),"; } await Db.Database.ExecuteSqlRawAsync(sqlInsert[..^ 1]);
public virtual async Task Apply(Block block) { #region current rights CurrentRights = await Cache.BakingRights.GetAsync(block.Cycle, block.Level); var sql = string.Empty; if (block.Priority == 0 && block.Validations == block.Protocol.EndorsersPerBlock) { CurrentRights.RemoveAll(x => x.Type == BakingRightType.Baking && x.Priority > 0); CurrentRights.ForEach(x => x.Status = BakingRightStatus.Realized); sql = $@" DELETE FROM ""BakingRights"" WHERE ""Level"" = {block.Level} AND ""Type"" = {(int)BakingRightType.Baking} AND ""Priority"" > 0; UPDATE ""BakingRights"" SET ""Status"" = {(int)BakingRightStatus.Realized} WHERE ""Level"" = {block.Level};"; } else { #region load missed priority var maxExistedPriority = CurrentRights .Where(x => x.Type == BakingRightType.Baking) .Select(x => x.Priority) .Max(); if (maxExistedPriority < block.Priority) { var bakingRights = await Proto.Rpc.GetLevelBakingRightsAsync(block.Level, block.Level, block.Priority); //bakingRights = bakingRights.OrderBy(x => x.Priority); var sqlInsert = @" INSERT INTO ""BakingRights"" (""Cycle"", ""Level"", ""BakerId"", ""Type"", ""Status"", ""Priority"", ""Slots"") VALUES "; foreach (var bakingRight in bakingRights.EnumerateArray().SkipWhile(x => x.RequiredInt32("priority") <= maxExistedPriority)) { var delegat = Cache.Accounts.GetDelegateOrDefault(bakingRight.RequiredString("delegate")); if (delegat == null) { continue; // WTF: [level:28680] - Baking rights were given to non-baker account } sqlInsert += $@" ({block.Cycle}, {block.Level}, {delegat.Id}, {(int)BakingRightType.Baking}, {(int)BakingRightStatus.Future}, {bakingRight.RequiredInt32("priority")}, null),"; } await Db.Database.ExecuteSqlRawAsync(sqlInsert[..^ 1]);
public override async Task Apply(Block block) { var cycle = (block.Level - 1) / block.Protocol.BlocksPerCycle; #region current rights CurrentRights = await Cache.BakingRights.GetAsync(cycle, block.Level); var sql = string.Empty; // TODO: better use protocol of the block where the endorsing rights were generated if (block.Priority == 0 && block.Validations == block.Protocol.EndorsersPerBlock) { CurrentRights.RemoveAll(x => x.Type == BakingRightType.Baking && x.Priority > 0); CurrentRights.ForEach(x => x.Status = BakingRightStatus.Realized); sql = $@" DELETE FROM ""BakingRights"" WHERE ""Level"" = {block.Level} AND ""Type"" = {(int)BakingRightType.Baking} AND ""Priority"" > 0; UPDATE ""BakingRights"" SET ""Status"" = {(int)BakingRightStatus.Realized} WHERE ""Level"" = {block.Level};"; } else { #region load missed priority var maxExistedPriority = CurrentRights .Where(x => x.Type == BakingRightType.Baking) .Select(x => x.Priority) .Max(); if (maxExistedPriority < block.Priority) { var bakingRights = await Proto.Rpc.GetLevelBakingRightsAsync(block.Level, block.Priority); //bakingRights = bakingRights.OrderBy(x => x.Priority); var sqlInsert = @" INSERT INTO ""BakingRights"" (""Cycle"", ""Level"", ""BakerId"", ""Type"", ""Status"", ""Priority"", ""Slots"") VALUES "; foreach (var br in bakingRights.EnumerateArray().SkipWhile(x => x.RequiredInt32("priority") <= maxExistedPriority)) { sqlInsert += $@" ({cycle}, {block.Level}, {Cache.Accounts.GetDelegate(br.RequiredString("delegate")).Id}, {(int)BakingRightType.Baking}, {(int)BakingRightStatus.Future}, {br.RequiredInt32("priority")}, null),"; } await Db.Database.ExecuteSqlRawAsync(sqlInsert[..^ 1]);