From 8b32e7e1126428b7f29e206042c8b6234beb5f9b Mon Sep 17 00:00:00 2001 From: Maksim Strebkov <257byte@gmail.com> Date: Mon, 13 Jan 2025 22:27:06 +0300 Subject: [PATCH] Update `consensus_rights_delay` for ghostnet (#180) --- .../Handlers/Proto20/Commits/CycleCommit.cs | 4 +- .../Proto21/Activation/ProtoActivator.cs | 52 +++++++++++++++++-- .../Proto21/Commits/BakerCycleCommit.cs | 2 +- .../Proto21/Commits/BakingRightsCommit.cs | 2 +- .../Handlers/Proto21/Commits/CycleCommit.cs | 39 +++++++++++++- .../Proto21/Commits/DelegatorCycleCommit.cs | 2 +- 6 files changed, 92 insertions(+), 9 deletions(-) diff --git a/Tzkt.Sync/Protocols/Handlers/Proto20/Commits/CycleCommit.cs b/Tzkt.Sync/Protocols/Handlers/Proto20/Commits/CycleCommit.cs index 8a6d4070..23d9bd46 100644 --- a/Tzkt.Sync/Protocols/Handlers/Proto20/Commits/CycleCommit.cs +++ b/Tzkt.Sync/Protocols/Handlers/Proto20/Commits/CycleCommit.cs @@ -12,7 +12,7 @@ class CycleCommit : ProtocolCommit public CycleCommit(ProtocolHandler protocol) : base(protocol) { } - public async Task Apply(Block block) + public virtual async Task Apply(Block block) { if (!block.Events.HasFlag(BlockEvents.CycleBegin)) return; @@ -60,7 +60,7 @@ public async Task Apply(Block block) Db.Cycles.Add(FutureCycle); } - public async Task Revert(Block block) + public virtual async Task Revert(Block block) { if (!block.Events.HasFlag(BlockEvents.CycleBegin)) return; diff --git a/Tzkt.Sync/Protocols/Handlers/Proto21/Activation/ProtoActivator.cs b/Tzkt.Sync/Protocols/Handlers/Proto21/Activation/ProtoActivator.cs index 221cfdcd..7ce00fae 100644 --- a/Tzkt.Sync/Protocols/Handlers/Proto21/Activation/ProtoActivator.cs +++ b/Tzkt.Sync/Protocols/Handlers/Proto21/Activation/ProtoActivator.cs @@ -11,6 +11,9 @@ public ProtoActivator(ProtocolHandler proto) : base(proto) { } protected override void UpgradeParameters(Protocol protocol, Protocol prev) { + if (protocol.ConsensusRightsDelay == 3) + protocol.ConsensusRightsDelay = 2; + if (protocol.TimeBetweenBlocks >= 5) { protocol.BlocksPerCycle = protocol.BlocksPerCycle * 5 / 4; @@ -33,13 +36,54 @@ protected override async Task MigrateContext(AppState state) var nextProto = await Cache.Protocols.GetAsync(state.NextProtocol); await RemoveDeadRefutationGames(state); + await RemoveFutureCycles(state, prevProto, nextProto); await MigrateSlashing(state, nextProto); MigrateBakers(state, prevProto, nextProto); await MigrateVotingPeriods(state, nextProto); - var cycles = await MigrateCycles(state, nextProto); + var cycles = await MigrateCycles(state, prevProto, nextProto); await MigrateFutureRights(state, nextProto, cycles); } + async Task RemoveFutureCycles(AppState state, Protocol prevProto, Protocol nextProto) + { + if (prevProto.ConsensusRightsDelay == nextProto.ConsensusRightsDelay) + return; + + var lastCycle = state.Cycle + nextProto.ConsensusRightsDelay + 1; + var lastCycleStart = nextProto.GetCycleStart(lastCycle); + + await Db.Database.ExecuteSqlRawAsync($""" + DELETE FROM "BakerCycles" + WHERE "Cycle" > {lastCycle}; + """); + + await Db.Database.ExecuteSqlRawAsync($""" + DELETE FROM "BakingRights" + WHERE "Type" = {(int)BakingRightType.Baking} + AND "Cycle" > {lastCycle}; + + DELETE FROM "BakingRights" + WHERE "Type" = {(int)BakingRightType.Endorsing} + AND "Level" > {lastCycleStart}; + """); + + var removedCycles = await Db.Database.ExecuteSqlRawAsync($""" + DELETE FROM "Cycles" + WHERE "Index" > {lastCycle}; + """); + + await Db.Database.ExecuteSqlRawAsync($""" + DELETE FROM "DelegatorCycles" + WHERE "Cycle" > {lastCycle}; + """); + + Cache.BakerCycles.Reset(); + Cache.BakingRights.Reset(); + + Db.TryAttach(state); + state.CyclesCount -= removedCycles; + } + async Task MigrateSlashing(AppState state, Protocol nextProto) { foreach (var op in await Db.DoubleBakingOps.Where(x => x.SlashedLevel > state.Level).ToListAsync()) @@ -77,14 +121,16 @@ async Task MigrateVotingPeriods(AppState state, Protocol nextProto) newPeriod.LastLevel = newPeriod.FirstLevel + nextProto.BlocksPerVoting - 1; } - async Task> MigrateCycles(AppState state, Protocol nextProto) + async Task> MigrateCycles(AppState state, Protocol prevProto, Protocol nextProto) { var cycles = await Db.Cycles .Where(x => x.Index >= state.Cycle) .OrderBy(x => x.Index) .ToListAsync(); - var res = await Proto.Rpc.GetExpectedIssuance(state.Level); + var res = prevProto.ConsensusRightsDelay != nextProto.ConsensusRightsDelay + ? await Proto.Rpc.GetExpectedIssuance(state.Level + 1) // Crutch for buggy ghostnet + : await Proto.Rpc.GetExpectedIssuance(state.Level); foreach (var cycle in cycles.Where(x => x.Index > state.Cycle)) { diff --git a/Tzkt.Sync/Protocols/Handlers/Proto21/Commits/BakerCycleCommit.cs b/Tzkt.Sync/Protocols/Handlers/Proto21/Commits/BakerCycleCommit.cs index 500b80a9..b134b910 100644 --- a/Tzkt.Sync/Protocols/Handlers/Proto21/Commits/BakerCycleCommit.cs +++ b/Tzkt.Sync/Protocols/Handlers/Proto21/Commits/BakerCycleCommit.cs @@ -1,6 +1,6 @@ namespace Tzkt.Sync.Protocols.Proto21 { - class BakerCycleCommit : Proto18.BakerCycleCommit + class BakerCycleCommit : Proto19.BakerCycleCommit { public BakerCycleCommit(ProtocolHandler protocol) : base(protocol) { } } diff --git a/Tzkt.Sync/Protocols/Handlers/Proto21/Commits/BakingRightsCommit.cs b/Tzkt.Sync/Protocols/Handlers/Proto21/Commits/BakingRightsCommit.cs index d70dd09a..41b6b634 100644 --- a/Tzkt.Sync/Protocols/Handlers/Proto21/Commits/BakingRightsCommit.cs +++ b/Tzkt.Sync/Protocols/Handlers/Proto21/Commits/BakingRightsCommit.cs @@ -1,6 +1,6 @@ namespace Tzkt.Sync.Protocols.Proto21 { - class BakingRightsCommit : Proto18.BakingRightsCommit + class BakingRightsCommit : Proto19.BakingRightsCommit { public BakingRightsCommit(ProtocolHandler protocol) : base(protocol) { } } diff --git a/Tzkt.Sync/Protocols/Handlers/Proto21/Commits/CycleCommit.cs b/Tzkt.Sync/Protocols/Handlers/Proto21/Commits/CycleCommit.cs index 50b19c66..555138ba 100644 --- a/Tzkt.Sync/Protocols/Handlers/Proto21/Commits/CycleCommit.cs +++ b/Tzkt.Sync/Protocols/Handlers/Proto21/Commits/CycleCommit.cs @@ -1,7 +1,44 @@ -namespace Tzkt.Sync.Protocols.Proto21 +using Tzkt.Data.Models; + +namespace Tzkt.Sync.Protocols.Proto21 { class CycleCommit : Proto20.CycleCommit { public CycleCommit(ProtocolHandler protocol) : base(protocol) { } + + public override async Task Apply(Block block) + { + if (block.Cycle == block.Protocol.FirstCycle) + { + var prevProto = await Cache.Protocols.GetAsync(block.Protocol.Code - 1); + if (prevProto.ConsensusRightsDelay != block.Protocol.ConsensusRightsDelay) + { + Cache.AppState.Get().CyclesCount--; + return; + } + } + + await base.Apply(block); + } + + public override async Task Revert(Block block) + { + if (!block.Events.HasFlag(BlockEvents.CycleBegin)) + return; + + block.Protocol ??= await Cache.Protocols.GetAsync(block.ProtoCode); + + if (block.Cycle == block.Protocol.FirstCycle) + { + var prevProto = await Cache.Protocols.GetAsync(block.Protocol.Code - 1); + if (prevProto.ConsensusRightsDelay != block.Protocol.ConsensusRightsDelay) + { + Cache.AppState.Get().CyclesCount++; + return; + } + } + + await base.Revert(block); + } } } diff --git a/Tzkt.Sync/Protocols/Handlers/Proto21/Commits/DelegatorCycleCommit.cs b/Tzkt.Sync/Protocols/Handlers/Proto21/Commits/DelegatorCycleCommit.cs index da6d7108..3ec439f5 100644 --- a/Tzkt.Sync/Protocols/Handlers/Proto21/Commits/DelegatorCycleCommit.cs +++ b/Tzkt.Sync/Protocols/Handlers/Proto21/Commits/DelegatorCycleCommit.cs @@ -1,6 +1,6 @@ namespace Tzkt.Sync.Protocols.Proto21 { - class DelegatorCycleCommit : Proto18.DelegatorCycleCommit + class DelegatorCycleCommit : Proto19.DelegatorCycleCommit { public DelegatorCycleCommit(ProtocolHandler protocol) : base(protocol) { } }