Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update consensus_rights_delay for ghostnet #180

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading