Skip to content

Commit

Permalink
Fix protocol constants migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Jun 1, 2024
1 parent ae997e3 commit 9cc2afc
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 26 deletions.
24 changes: 15 additions & 9 deletions Tzkt.Sync/Protocols/Handlers/Proto19/Activation/ProtoActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@ protected override void SetParameters(Protocol protocol, JToken parameters)

protected override void UpgradeParameters(Protocol protocol, Protocol prev)
{
protocol.ConsensusRightsDelay = 2;
protocol.BlocksPerCycle = protocol.BlocksPerCycle * 3 / 2;
protocol.BlocksPerCommitment = protocol.BlocksPerCommitment * 3 / 2;
if (protocol.ConsensusRightsDelay == 5)
protocol.ConsensusRightsDelay = 2;

if (protocol.TimeBetweenBlocks >= 8)
{
protocol.BlocksPerCycle = protocol.BlocksPerCycle * 3 / 2;
protocol.BlocksPerCommitment = protocol.BlocksPerCommitment * 3 / 2;
protocol.BlocksPerVoting = protocol.BlocksPerVoting * 3 / 2;
protocol.TimeBetweenBlocks = protocol.TimeBetweenBlocks * 2 / 3;
protocol.HardBlockGasLimit = prev.HardBlockGasLimit * 2 / 3;
protocol.SmartRollupCommitmentPeriod = 15 * 60 / protocol.TimeBetweenBlocks;
protocol.SmartRollupChallengeWindow = 14 * 24 * 60 * 60 / protocol.TimeBetweenBlocks;
protocol.SmartRollupTimeoutPeriod = 7 * 24 * 60 * 60 / protocol.TimeBetweenBlocks;
}

protocol.BlocksPerSnapshot = protocol.BlocksPerCycle;
protocol.BlocksPerVoting = protocol.BlocksPerVoting * 3 / 2;
protocol.TimeBetweenBlocks = protocol.TimeBetweenBlocks * 2 / 3;
protocol.HardBlockGasLimit = prev.HardBlockGasLimit * 2 / 3;
protocol.SmartRollupCommitmentPeriod = 15 * 60 / protocol.TimeBetweenBlocks;
protocol.SmartRollupChallengeWindow = 14 * 24 * 60 * 60 / protocol.TimeBetweenBlocks;
protocol.SmartRollupTimeoutPeriod = 7 * 24 * 60 * 60 / protocol.TimeBetweenBlocks;
}

protected override async Task MigrateContext(AppState state)
Expand Down
18 changes: 13 additions & 5 deletions Tzkt.Sync/Protocols/Handlers/Proto19/Commits/BakerCycleCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class BakerCycleCommit : Proto18.BakerCycleCommit
{
public BakerCycleCommit(ProtocolHandler protocol) : base(protocol) { }

protected override Task ApplyNewCycle(
protected override async Task ApplyNewCycle(
Block block,
Cycle futureCycle,
IEnumerable<RightsGenerator.BR> futureBakingRights,
Expand All @@ -15,17 +15,25 @@ protected override Task ApplyNewCycle(
Dictionary<int, long> selectedStakes)
{
if (block.Cycle == block.Protocol.FirstCycle)
return Task.CompletedTask;
{
var prevProto = await Cache.Protocols.GetAsync(block.Protocol.Code - 1);
if (prevProto.ConsensusRightsDelay != block.Protocol.ConsensusRightsDelay)
return;
}

return base.ApplyNewCycle(block, futureCycle, futureBakingRights, futureEndorsingRights, snapshots, selectedStakes);
await base.ApplyNewCycle(block, futureCycle, futureBakingRights, futureEndorsingRights, snapshots, selectedStakes);
}

protected override async Task RevertNewCycle(Block block)
{
block.Protocol ??= await Cache.Protocols.GetAsync(block.ProtoCode);

if (block.Cycle == block.Protocol.FirstCycle)
return;
{
var prevProto = await Cache.Protocols.GetAsync(block.Protocol.Code - 1);
if (prevProto.ConsensusRightsDelay != block.Protocol.ConsensusRightsDelay)
return;
}

await base.RevertNewCycle(block);
}
Expand Down
16 changes: 12 additions & 4 deletions Tzkt.Sync/Protocols/Handlers/Proto19/Commits/BakingRightsCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@ class BakingRightsCommit : Proto16.BakingRightsCommit
{
public BakingRightsCommit(ProtocolHandler protocol) : base(protocol) { }

protected override Task ApplyNewCycle(Block block, Cycle futureCycle, Dictionary<int, long> selectedStakes)
protected override async Task ApplyNewCycle(Block block, Cycle futureCycle, Dictionary<int, long> selectedStakes)
{
if (block.Cycle == block.Protocol.FirstCycle)
return Task.CompletedTask;
{
var prevProto = await Cache.Protocols.GetAsync(block.Protocol.Code - 1);
if (prevProto.ConsensusRightsDelay != block.Protocol.ConsensusRightsDelay)
return;
}

return base.ApplyNewCycle(block, futureCycle, selectedStakes);
await base.ApplyNewCycle(block, futureCycle, selectedStakes);
}

public override async Task RevertNewCycle(Block block)
{
block.Protocol ??= await Cache.Protocols.GetAsync(block.ProtoCode);

if (block.Cycle == block.Protocol.FirstCycle)
return;
{
var prevProto = await Cache.Protocols.GetAsync(block.Protocol.Code - 1);
if (prevProto.ConsensusRightsDelay != block.Protocol.ConsensusRightsDelay)
return;
}

await base.RevertNewCycle(block);
}
Expand Down
16 changes: 12 additions & 4 deletions Tzkt.Sync/Protocols/Handlers/Proto19/Commits/CycleCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ public async Task Apply(Block block)

if (block.Cycle == block.Protocol.FirstCycle)
{
Cache.AppState.Get().CyclesCount--;
return;
var prevProto = await Cache.Protocols.GetAsync(block.Protocol.Code - 1);
if (prevProto.ConsensusRightsDelay != block.Protocol.ConsensusRightsDelay)
{
Cache.AppState.Get().CyclesCount--;
return;
}
}

var index = block.Cycle + block.Protocol.ConsensusRightsDelay;
Expand Down Expand Up @@ -75,8 +79,12 @@ public async Task Revert(Block block)

if (block.Cycle == block.Protocol.FirstCycle)
{
Cache.AppState.Get().CyclesCount++;
return;
var prevProto = await Cache.Protocols.GetAsync(block.Protocol.Code - 1);
if (prevProto.ConsensusRightsDelay != block.Protocol.ConsensusRightsDelay)
{
Cache.AppState.Get().CyclesCount++;
return;
}
}

await Db.Database.ExecuteSqlRawAsync($"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ class DelegatorCycleCommit : Proto18.DelegatorCycleCommit
{
public DelegatorCycleCommit(ProtocolHandler protocol) : base(protocol) { }

public override Task Apply(Block block, Cycle futureCycle)
public override async Task Apply(Block block, Cycle futureCycle)
{
if (block.Cycle == block.Protocol.FirstCycle)
return Task.CompletedTask;
{
var prevProto = await Cache.Protocols.GetAsync(block.Protocol.Code - 1);
if (prevProto.ConsensusRightsDelay != block.Protocol.ConsensusRightsDelay)
return;
}

return base.Apply(block, futureCycle);
await base.Apply(block, futureCycle);
}

public override async Task Revert(Block block)
Expand All @@ -22,7 +26,11 @@ public override async Task Revert(Block block)
block.Protocol ??= await Cache.Protocols.GetAsync(block.ProtoCode);

if (block.Cycle == block.Protocol.FirstCycle)
return;
{
var prevProto = await Cache.Protocols.GetAsync(block.Protocol.Code - 1);
if (prevProto.ConsensusRightsDelay != block.Protocol.ConsensusRightsDelay)
return;
}

await base.Revert(block);
}
Expand Down

0 comments on commit 9cc2afc

Please sign in to comment.