Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Jun 24, 2022
1 parent dc88700 commit f0ce039
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 22 deletions.
14 changes: 14 additions & 0 deletions Tzkt.Sync/Extensions/BytesExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ public static bool StartWith(this byte[] src, byte[] data)
return true;
}

public static int ToInt32(this byte[] bytes)
{
if (bytes.Length > 4)
throw new Exception("Failed to read Int32 from bytes");

var res = 0;
for (int i = 0; i < bytes.Length; i++)
{
res <<= 8;
res += bytes[i];
}
return res;
}

public static int ReadInt32(this byte[] bytes, int pos)
{
var res = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ partial class ProtoActivator : Proto11.ProtoActivator
(x.Id, Math.Min(x.StakingBalance, x.Balance * 100 / protocol.FrozenDepositsPercentage))));

#region temporary diagnostics
await sampler.Validate(Proto.Node, 1, cycle.Index);
await sampler.Validate(Proto, 1, cycle.Index);
#endregion

var bakingRights = await RightsGenerator.GetBakingRightsAsync(sampler, protocol, cycle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Numerics;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Netezos.Encoding;
using Newtonsoft.Json.Linq;
using Npgsql;
using Tzkt.Data.Models;
Expand Down Expand Up @@ -264,7 +265,7 @@ await Db.Database.ExecuteSqlRawAsync($@"
.Select(x => (x.Id, Math.Min(x.StakingBalance, x.Balance * 100 / nextProto.FrozenDepositsPercentage))));

#region temporary diagnostics
await sampler.Validate(Proto.Node, state.Level, cycle.Index);
await sampler.Validate(Proto, state.Level, cycle.Index);
#endregion

var brs = new List<RightsGenerator.BR>();
Expand Down Expand Up @@ -336,7 +337,7 @@ async Task MigrateFutureRights(AppState state, Protocol nextProto)
.Select(x => (x.Id, Math.Min(x.StakingBalance, x.Balance * 100 / nextProto.FrozenDepositsPercentage))));

#region temporary diagnostics
await sampler.Validate(Proto.Node, state.Level, state.Cycle);
await sampler.Validate(Proto, state.Level, state.Cycle);
#endregion

var cycles = await Db.Cycles.AsNoTracking().Where(x => x.Index >= state.Cycle).OrderBy(x => x.Index).ToListAsync();
Expand Down Expand Up @@ -475,7 +476,10 @@ await Db.Database.ExecuteSqlRawAsync($@"

protected virtual Sampler GetSampler(IEnumerable<(int id, long stake)> selection)
{
var sorted = selection.OrderByDescending(x => x.stake);
var sorted = selection
.OrderByDescending(x => x.stake)
.ThenByDescending(x => Base58.Parse(Cache.Accounts.GetDelegate(x.id).Address), new BytesComparer());

return new Sampler(sorted.Select(x => x.id).ToArray(), sorted.Select(x => x.stake).ToArray());
}
}
Expand Down
20 changes: 14 additions & 6 deletions Tzkt.Sync/Protocols/Handlers/Proto12/Commits/BakingRightsCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Netezos.Encoding;
using Npgsql;
using Tzkt.Data.Models;

Expand Down Expand Up @@ -48,9 +49,11 @@ DELETE FROM ""BakingRights""
{
var cycle = await Db.Cycles.FirstAsync(x => x.Index == block.Cycle);
var bakerCycles = await Cache.BakerCycles.GetAsync(block.Cycle);
var sampler = GetSampler(bakerCycles.Values.Where(x => x.ActiveStake > 0).Select(x => (x.BakerId, x.ActiveStake)));
var sampler = GetSampler(
bakerCycles.Values.Where(x => x.ActiveStake > 0).Select(x => (x.BakerId, x.ActiveStake)),
block.Cycle == block.Protocol.FirstCycle + block.Protocol.PreservedCycles); //TODO: remove this crutch after ithaca is gone
#region temporary diagnostics
await sampler.Validate(Proto.Node, block.Level, block.Cycle);
await sampler.Validate(Proto, block.Level, block.Cycle);
#endregion
var bakingRights = RightsGenerator.GetBakingRights(sampler, cycle, block.Level, block.BlockRound + 1);

Expand Down Expand Up @@ -122,9 +125,11 @@ DELETE FROM ""BakingRights""
#region new cycle
if (futureCycle != null)
{
var sampler = GetSampler(selectedStakes.Where(x => x.Value > 0).Select(x => (x.Key, x.Value)));
var sampler = GetSampler(
selectedStakes.Where(x => x.Value > 0).Select(x => (x.Key, x.Value)),
block.Level == block.Protocol.FirstCycleLevel);
#region temporary diagnostics
await sampler.Validate(Proto.Node, block.Level, futureCycle.Index);
await sampler.Validate(Proto, block.Level, futureCycle.Index);
#endregion
FutureBakingRights = await RightsGenerator.GetBakingRightsAsync(sampler, block.Protocol, futureCycle);
FutureEndorsingRights = await RightsGenerator.GetEndorsingRightsAsync(sampler, block.Protocol, futureCycle);
Expand Down Expand Up @@ -190,9 +195,12 @@ DELETE FROM ""BakingRights""
#endregion
}

protected virtual Sampler GetSampler(IEnumerable<(int id, long stake)> selection)
protected virtual Sampler GetSampler(IEnumerable<(int id, long stake)> selection, bool forceBase)
{
var sorted = selection.OrderByDescending(x => x.stake);
var sorted = selection
.OrderByDescending(x => x.stake)
.ThenByDescending(x => Base58.Parse(Cache.Accounts.GetDelegate(x.id).Address), new BytesComparer());

return new Sampler(sorted.Select(x => x.id).ToArray(), sorted.Select(x => x.stake).ToArray());
}
}
Expand Down
4 changes: 2 additions & 2 deletions Tzkt.Sync/Protocols/Handlers/Proto12/Commits/BlockCommit.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Netezos.Encoding;
using Tzkt.Data.Models;

namespace Tzkt.Sync.Protocols.Proto12
Expand Down Expand Up @@ -40,7 +40,7 @@ public virtual async Task Apply(JsonElement rawBlock)
events |= BlockEvents.BalanceSnapshot;

var payloadRound = header.RequiredInt32("payload_round");
var blockRound = header.RequiredArray("fitness", 5)[4].RequiredInt32();
var blockRound = Hex.Parse(header.RequiredArray("fitness", 5)[4].RequiredString()).ToInt32();
var balanceUpdates = metadata.RequiredArray("balance_updates").EnumerateArray();
var rewardUpdate = balanceUpdates.FirstOrDefault(x => x.RequiredString("kind") == "minted" && x.RequiredString("category") == "baking rewards");
var bonusUpdate = balanceUpdates.FirstOrDefault(x => x.RequiredString("kind") == "minted" && x.RequiredString("category") == "baking bonuses");
Expand Down
13 changes: 7 additions & 6 deletions Tzkt.Sync/Protocols/Handlers/Proto12/Commits/CycleCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public virtual async Task Apply(Block block)
if (!block.Events.HasFlag(BlockEvents.CycleBegin))
return;

var futureCycle = block.Cycle + block.Protocol.PreservedCycles;
var futureCycle = block.Cycle + block.Protocol.PreservedCycles;

var lastSeed = await Db.Cycles
.AsNoTracking()
Expand All @@ -46,15 +46,16 @@ public virtual async Task Apply(Block block)
{
snapshotLevel = block.Protocol.FirstLevel - 1;
}
else if (block.Cycle == block.Protocol.FirstCycle + 1 && block.Protocol.FirstLevel >= block.Protocol.FirstCycleLevel)
{
var snapshotProto = await Cache.Protocols.FindByCycleAsync(block.Cycle - 1);
snapshotIndex = Seed.GetSnapshotIndex(futureSeed, snapshotProto.SnapshotsPerCycle + 1, true) - 1;
snapshotLevel = snapshotProto.GetCycleStart(block.Cycle - 1) - 1 + (snapshotIndex + 1) * snapshotProto.BlocksPerSnapshot;
}
else
{
var snapshotProto = await Cache.Protocols.FindByCycleAsync(block.Cycle - 1);
snapshotIndex = Seed.GetSnapshotIndex(futureSeed, snapshotProto.SnapshotsPerCycle, true);
#region ithaca activation quirk
// on the mainnet the snapshot index after the first Ithaca cycle was calculated differently
if (Cache.AppState.Get().Chain == "mainnet" && block.Cycle == 469)
snapshotIndex = 15;
#endregion
snapshotLevel = snapshotProto.GetCycleStart(block.Cycle - 1) - 1 + (snapshotIndex + 1) * snapshotProto.BlocksPerSnapshot;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ class BakingRightsCommit : Proto12.BakingRightsCommit
{
public BakingRightsCommit(ProtocolHandler protocol) : base(protocol) { }

protected override Sampler GetSampler(IEnumerable<(int id, long stake)> selection)
protected override Sampler GetSampler(IEnumerable<(int id, long stake)> selection, bool forceBase)
{
if (forceBase)
return base.GetSampler(selection, false);

var sorted = selection.OrderByDescending(x =>
Base58.Parse(Cache.Accounts.GetDelegate(x.id).Address), new BytesComparer());

Expand Down
13 changes: 10 additions & 3 deletions Tzkt.Sync/Protocols/Helpers/Sampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
using System.Threading.Tasks;
using Dynamic.Json;
using Tzkt.Sync.Services;
using Netezos.Keys;

namespace Tzkt.Sync.Protocols
{
Expand Down Expand Up @@ -93,14 +93,21 @@ public int GetBaker(byte[] seed)
return el < P[i] ? Bakers[i] : Bakers[Alias[i]];
}

public async Task Validate(TezosNode node, int block, int cycle)
public async Task Validate(ProtocolHandler proto, int block, int cycle)
{
dynamic raw = DJson.Create(await node.GetAsync($"chains/main/blocks/{block}/context/raw/json/cycle/{cycle}"));
dynamic raw = DJson.Create(await proto.Node.GetAsync($"chains/main/blocks/{block}/context/raw/json/cycle/{cycle}"));
var state = raw.delegate_sampler_state;

if (state.total != Total)
throw new Exception("Invalid sampler 'total'");

if (state.support.elements.length != Bakers.Length)
throw new Exception("Invalid sampler 'support'");

for (int i = 0; i < Bakers.Length; i++)
if (PubKey.FromBase58(state.support.elements[i]).Address != proto.Cache.Accounts.GetDelegate(Bakers[i]).Address)
throw new Exception("Invalid sampler 'support' element");

if (state.p.elements.length != P.Length)
throw new Exception("Invalid sampler 'p'");

Expand Down

0 comments on commit f0ce039

Please sign in to comment.