Skip to content

Commit

Permalink
Update consensus_rights_delay for ghostnet (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan authored Jan 13, 2025
1 parent 64955cb commit 8b32e7e
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Tzkt.Sync/Protocols/Handlers/Proto20/Commits/CycleCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
52 changes: 49 additions & 3 deletions Tzkt.Sync/Protocols/Handlers/Proto21/Activation/ProtoActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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())
Expand Down Expand Up @@ -77,14 +121,16 @@ async Task MigrateVotingPeriods(AppState state, Protocol nextProto)
newPeriod.LastLevel = newPeriod.FirstLevel + nextProto.BlocksPerVoting - 1;
}

async Task<List<Cycle>> MigrateCycles(AppState state, Protocol nextProto)
async Task<List<Cycle>> 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))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Tzkt.Sync.Protocols.Proto21
{
class BakerCycleCommit : Proto18.BakerCycleCommit
class BakerCycleCommit : Proto19.BakerCycleCommit
{
public BakerCycleCommit(ProtocolHandler protocol) : base(protocol) { }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Tzkt.Sync.Protocols.Proto21
{
class BakingRightsCommit : Proto18.BakingRightsCommit
class BakingRightsCommit : Proto19.BakingRightsCommit
{
public BakingRightsCommit(ProtocolHandler protocol) : base(protocol) { }
}
Expand Down
39 changes: 38 additions & 1 deletion Tzkt.Sync/Protocols/Handlers/Proto21/Commits/CycleCommit.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Tzkt.Sync.Protocols.Proto21
{
class DelegatorCycleCommit : Proto18.DelegatorCycleCommit
class DelegatorCycleCommit : Proto19.DelegatorCycleCommit
{
public DelegatorCycleCommit(ProtocolHandler protocol) : base(protocol) { }
}
Expand Down

0 comments on commit 8b32e7e

Please sign in to comment.