Skip to content

Commit

Permalink
Index voting snapshots for waiting periods as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed May 31, 2024
1 parent d89b2b5 commit 13b2e99
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 57 deletions.
4 changes: 2 additions & 2 deletions Tzkt.Data/Migrations/20240424125408_Initial.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Tzkt.Data/Migrations/20240424125408_Initial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -890,8 +890,8 @@ protected override void Up(MigrationBuilder migrationBuilder)
Kind = table.Column<int>(type: "integer", nullable: false),
Status = table.Column<int>(type: "integer", nullable: false),
Dictator = table.Column<int>(type: "integer", nullable: false),
TotalBakers = table.Column<int>(type: "integer", nullable: true),
TotalVotingPower = table.Column<long>(type: "bigint", nullable: true),
TotalBakers = table.Column<int>(type: "integer", nullable: false),
TotalVotingPower = table.Column<long>(type: "bigint", nullable: false),
UpvotesQuorum = table.Column<int>(type: "integer", nullable: true),
ProposalsCount = table.Column<int>(type: "integer", nullable: true),
TopUpvotes = table.Column<int>(type: "integer", nullable: true),
Expand Down
4 changes: 2 additions & 2 deletions Tzkt.Data/Migrations/20240501115741_Triggers.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Tzkt.Data/Migrations/20240501115809_TokensValue.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Tzkt.Data/Migrations/TzktContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5530,10 +5530,10 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<long?>("TopVotingPower")
.HasColumnType("bigint");

b.Property<int?>("TotalBakers")
b.Property<int>("TotalBakers")
.HasColumnType("integer");

b.Property<long?>("TotalVotingPower")
b.Property<long>("TotalVotingPower")
.HasColumnType("bigint");

b.Property<int?>("UpvotesQuorum")
Expand Down
4 changes: 2 additions & 2 deletions Tzkt.Data/Models/Voting/VotingPeriod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class VotingPeriod
public PeriodStatus Status { get; set; }
public DictatorStatus Dictator { get; set; }

public int? TotalBakers { get; set; }
public long? TotalVotingPower { get; set; }
public int TotalBakers { get; set; }
public long TotalVotingPower { get; set; }

#region proposal
public int? UpvotesQuorum { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Tzkt.Data.Models;

namespace Tzkt.Sync.Protocols.Proto1
Expand Down
68 changes: 29 additions & 39 deletions Tzkt.Sync/Protocols/Handlers/Proto1/Commits/VotingCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,46 +281,36 @@ protected VotingPeriod StartWaitingPeriod(Block block, VotingPeriod current, Per
Status = PeriodStatus.Active
};

if (proto.HasDictator)
{
#region snapshot
Db.VotingSnapshots.AddRange(Cache.Accounts.GetDelegates()
.Where(x => BakerIsListed(x, block, block.Protocol))
.Select(x => new VotingSnapshot
{
Level = block.Level,
Period = period.Index,
BakerId = x.Id,
VotingPower = GetVotingPower(x, block, proto),
Status = VoterStatus.None
}));
#endregion

//#region temporary diagnostics
//var snapshots = Cache.Accounts.GetDelegates()
// .Where(x => BakerIsListed(x, block, block.Protocol))
// .Select(x => new VotingSnapshot
// {
// Level = block.Level,
// Period = period.Index,
// BakerId = x.Id,
// VotingPower = GetVotingPower(x, block, proto),
// Status = VoterStatus.None
// });

//var listing = Proto.Node.GetAsync($"chains/main/blocks/{block.Level}/context/raw/json/votes/listings?depth=1").Result
// .EnumerateArray()
// .ToDictionary(x => x[0].RequiredString(), x => x[1].RequiredInt64());

//if (snapshots.Count() != listing.Count)
// throw new Exception("Wrong voting snapshots");

//foreach (var snapshot in snapshots)
// if (!listing.TryGetValue(Cache.Accounts.GetDelegate(snapshot.BakerId).Address, out var votingPower) || snapshot.VotingPower != votingPower)
// throw new Exception("Wrong voting snapshot");
//#endregion
}
#region snapshot
var snapshots = Cache.Accounts.GetDelegates()
.Where(x => BakerIsListed(x, block, block.Protocol))
.Select(x => new VotingSnapshot
{
Level = block.Level,
Period = period.Index,
BakerId = x.Id,
VotingPower = GetVotingPower(x, block, proto),
Status = VoterStatus.None
});

period.TotalBakers = snapshots.Count();
period.TotalVotingPower = snapshots.Sum(x => x.VotingPower);
#endregion

//#region temporary diagnostics
//var listing = Proto.Node.GetAsync($"chains/main/blocks/{block.Level}/context/raw/json/votes/listings?depth=1").Result
// .EnumerateArray()
// .ToDictionary(x => x[0].RequiredString(), x => x[1].RequiredInt64());

//if (snapshots.Count() != listing.Count)
// throw new Exception("Wrong voting snapshots");

//foreach (var snapshot in snapshots)
// if (!listing.TryGetValue(Cache.Accounts.GetDelegate(snapshot.BakerId).Address, out var votingPower) || snapshot.VotingPower != votingPower)
// throw new Exception("Wrong voting snapshot");
//#endregion

Db.VotingSnapshots.AddRange(snapshots);
Db.VotingPeriods.Add(period);
Cache.Periods.Add(period);
return period;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ public override async Task Apply(Block block, JsonElement op, JsonElement conten
period.Kind = PeriodKind.Adoption;
period.Dictator = DictatorStatus.Submit;

period.TotalBakers = null;
period.TotalVotingPower = null;
period.TotalBakers = snapshots.Count;
period.TotalVotingPower = snapshots.Sum(x => x.VotingPower);

period.UpvotesQuorum = null;
period.ProposalsCount = null;
Expand Down

0 comments on commit 13b2e99

Please sign in to comment.