Skip to content

Commit

Permalink
Temporarily change the fetching of endorsing rights for a cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Aug 6, 2021
1 parent 198e387 commit 30709a4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
22 changes: 20 additions & 2 deletions Tzkt.Sync/Protocols/Handlers/Proto1/Commits/BakingRightsCommit.cs
Expand Up @@ -167,8 +167,8 @@ public virtual async Task Apply(Block block)
{
var futureCycle = block.Cycle + block.Protocol.PreservedCycles;

FutureBakingRights = (await Proto.Rpc.GetBakingRightsAsync(block.Level, futureCycle)).EnumerateArray();
FutureEndorsingRights = (await Proto.Rpc.GetEndorsingRightsAsync(block.Level, futureCycle)).EnumerateArray();
FutureBakingRights = await GetBakingRights(block, futureCycle);
FutureEndorsingRights = await GetEndorsingRights(block, futureCycle);

foreach (var er in FutureEndorsingRights)
if (!await Cache.Accounts.ExistsAsync(er.RequiredString("delegate")))
Expand Down Expand Up @@ -241,5 +241,23 @@ public virtual async Task Revert(Block block)
}
#endregion
}

protected virtual async Task<IEnumerable<JsonElement>> GetBakingRights(Block block, int cycle)
{
var rights = (await Proto.Rpc.GetBakingRightsAsync(block.Level, cycle)).RequiredArray().EnumerateArray();
if (!rights.Any() || rights.Count(x => x.RequiredInt32("priority") == 0) != block.Protocol.BlocksPerCycle)
throw new ValidationException("Rpc returned less baking rights (with priority 0) than it should be");

return rights;
}

protected virtual async Task<IEnumerable<JsonElement>> GetEndorsingRights(Block block, int cycle)
{
var rights = (await Proto.Rpc.GetEndorsingRightsAsync(block.Level, cycle)).RequiredArray().EnumerateArray();
if (!rights.Any() || rights.Sum(x => x.RequiredArray("slots").Count()) != block.Protocol.BlocksPerCycle * block.Protocol.EndorsersPerBlock)
throw new ValidationException("Rpc returned less endorsing rights (slots) than it should be");

return rights;
}
}
}
Expand Up @@ -286,7 +286,9 @@ async Task MigrateFutureRights(List<Cycle> cycles, AppState state, Protocol next
async Task FetchBakingRights(Protocol protocol, int block, Cycle cycle, Dictionary<int, BakerCycle> bakerCycles)
{
GC.Collect();
var rights = (await Proto.Rpc.GetBakingRightsAsync(block, cycle.Index)).EnumerateArray();
var rights = (await Proto.Rpc.GetBakingRightsAsync(block, cycle.Index)).RequiredArray().EnumerateArray();
if (!rights.Any() || rights.Count(x => x.RequiredInt32("priority") == 0) != protocol.BlocksPerCycle)
throw new ValidationException("Rpc returned less baking rights (with priority 0) than it should be");

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)");
Expand Down Expand Up @@ -319,7 +321,12 @@ async Task FetchBakingRights(Protocol protocol, int block, Cycle cycle, Dictiona
async Task<List<JsonElement>> FetchEndorsingRights(Protocol protocol, int block, Cycle cycle, Dictionary<int, BakerCycle> bakerCycles, List<JsonElement> shiftedRights)
{
GC.Collect();
var rights = (await Proto.Rpc.GetEndorsingRightsAsync(block, cycle.Index)).EnumerateArray();
//var rights = (await Proto.Rpc.GetEndorsingRightsAsync(block, cycle.Index)).RequiredArray().EnumerateArray();
var rights = new List<JsonElement>(protocol.BlocksPerCycle * protocol.EndorsersPerBlock / 2);
for (int level = cycle.FirstLevel; level <= cycle.LastLevel; level++)
rights.AddRange((await Proto.Rpc.GetLevelEndorsingRightsAsync(block, level)).RequiredArray().EnumerateArray());
if (!rights.Any() || rights.Sum(x => x.RequiredArray("slots").Count()) != protocol.BlocksPerCycle * protocol.EndorsersPerBlock)
throw new ValidationException("Rpc returned less endorsing rights (slots) than it should be");

#region save rights
var conn = Db.Database.GetDbConnection() as NpgsqlConnection;
Expand Down
@@ -1,7 +1,29 @@
namespace Tzkt.Sync.Protocols.Proto10
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Tzkt.Data.Models;

namespace Tzkt.Sync.Protocols.Proto10
{
class BakingRightsCommit : Proto3.BakingRightsCommit
{
public BakingRightsCommit(ProtocolHandler protocol) : base(protocol) { }

// Tezos node is no longer able to return endorsing rights for the whole cycle, so we have to find another way...
protected override async Task<IEnumerable<JsonElement>> GetEndorsingRights(Block block, int cycle)
{
var rights = new List<JsonElement>(block.Protocol.BlocksPerCycle * block.Protocol.EndorsersPerBlock / 2);
var firstLevel = block.Protocol.GetCycleStart(cycle);
var lastLevel = block.Protocol.GetCycleEnd(cycle);

for (int level = firstLevel; level <= lastLevel; level++)
rights.AddRange((await Proto.Rpc.GetLevelEndorsingRightsAsync(block.Level, level)).RequiredArray().EnumerateArray());

if (!rights.Any() || rights.Sum(x => x.RequiredArray("slots").Count()) != block.Protocol.BlocksPerCycle * block.Protocol.EndorsersPerBlock)
throw new ValidationException("Rpc returned less endorsing rights (slots) than it should be");

return rights;
}
}
}
Expand Up @@ -139,8 +139,8 @@ public override async Task Apply(Block block)
{
var futureCycle = block.Cycle + block.Protocol.PreservedCycles;

FutureBakingRights = (await Proto.Rpc.GetBakingRightsAsync(block.Level, futureCycle)).EnumerateArray();
FutureEndorsingRights = (await Proto.Rpc.GetEndorsingRightsAsync(block.Level, futureCycle)).EnumerateArray();
FutureBakingRights = await GetBakingRights(block, futureCycle);
FutureEndorsingRights = await GetEndorsingRights(block, futureCycle);

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)");
Expand Down

0 comments on commit 30709a4

Please sign in to comment.