Skip to content

Commit

Permalink
Migrate voting listings
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Jun 24, 2022
1 parent 94abbc8 commit f8e7e23
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions Tzkt.Sync/Protocols/Handlers/Proto13/Activation/ProtoActivator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Threading.Tasks;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using Tzkt.Data.Models;

Expand All @@ -24,7 +26,34 @@ protected override long GetVotingPower(Delegate baker, Protocol protocol)
return baker.StakingBalance;
}

protected override Task MigrateContext(AppState state) => Task.CompletedTask;
protected override async Task MigrateContext(AppState state)
{
var nextProto = await Cache.Protocols.GetAsync(state.NextProtocol);

#region voting snapshots
await Db.Database.ExecuteSqlRawAsync($@"
DELETE FROM ""VotingSnapshots"" WHERE ""Period"" = {state.VotingPeriod}");

var snapshots = Cache.Accounts.GetDelegates()
.Where(x => x.Staked && x.StakingBalance >= nextProto.TokensPerRoll)
.Select(x => new VotingSnapshot
{
Level = state.Level,
Period = state.VotingPeriod,
BakerId = x.Id,
VotingPower = x.StakingBalance,
Status = VoterStatus.None
});

var period = await Cache.Periods.GetAsync(state.VotingPeriod);
Db.TryAttach(period);

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

Db.VotingSnapshots.AddRange(snapshots);
#endregion
}
protected override Task RevertContext(AppState state) => Task.CompletedTask;
}
}

0 comments on commit f8e7e23

Please sign in to comment.