Skip to content

Commit

Permalink
Add Granada migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Aug 2, 2021
1 parent 85b3ef4 commit 37bcee0
Show file tree
Hide file tree
Showing 19 changed files with 424 additions and 68 deletions.
7 changes: 4 additions & 3 deletions Tzkt.Sync/Protocols/Abstract/IRpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ public interface IRpc
#region indexer
Task<JsonElement> GetBlockAsync(int level);
Task<JsonElement> GetCycleAsync(int level, int cycle);
Task<JsonElement> GetBakingRightsAsync(int level, int cycle);
Task<JsonElement> GetEndorsingRightsAsync(int level, int cycle);
Task<JsonElement> GetLevelBakingRightsAsync(int level, int maxPriority);
Task<JsonElement> GetBakingRightsAsync(int block, int cycle);
Task<JsonElement> GetEndorsingRightsAsync(int block, int cycle);
Task<JsonElement> GetLevelBakingRightsAsync(int block, int level, int maxPriority);
Task<JsonElement> GetLevelEndorsingRightsAsync(int block, int level);
Task<JsonElement> GetContractAsync(int level, string address);
Task<JsonElement> GetDelegateAsync(int level, string address);
#endregion
Expand Down
9 changes: 6 additions & 3 deletions Tzkt.Sync/Protocols/Handlers/Genesis/Rpc/Rpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ public Task<JsonElement> GetBlockAsync(int level)
public Task<JsonElement> GetCycleAsync(int level, int cycle)
=> throw new InvalidOperationException();

public Task<JsonElement> GetBakingRightsAsync(int level, int cycle)
public Task<JsonElement> GetBakingRightsAsync(int block, int cycle)
=> throw new InvalidOperationException();

public Task<JsonElement> GetEndorsingRightsAsync(int level, int cycle)
public Task<JsonElement> GetEndorsingRightsAsync(int block, int cycle)
=> throw new InvalidOperationException();

public Task<JsonElement> GetLevelBakingRightsAsync(int level, int maxPriority)
public Task<JsonElement> GetLevelBakingRightsAsync(int block, int level, int maxPriority)
=> throw new InvalidOperationException();

public Task<JsonElement> GetLevelEndorsingRightsAsync(int block, int level)
=> throw new InvalidOperationException();

public Task<JsonElement> GetContractAsync(int level, string address)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,35 @@ public async Task UpgradeProtocol(AppState state)
FirstLevel = state.Level + 1,
LastLevel = -1,
FirstCycle = state.Cycle + 1,
FirstCycleLevel = prev.GetCycleStart(state.Cycle + 1)
FirstCycleLevel = prev.GetCycleStart(state.Cycle + 1),
RampUpCycles = prev.RampUpCycles,
NoRewardCycles = prev.NoRewardCycles,
BlockDeposit = prev.BlockDeposit,
BlockReward0 = prev.BlockReward0,
BlockReward1 = prev.BlockReward1,
BlocksPerCommitment = prev.BlocksPerCommitment,
BlocksPerCycle = prev.BlocksPerCycle,
BlocksPerSnapshot = prev.BlocksPerSnapshot,
BlocksPerVoting = prev.BlocksPerVoting,
ByteCost = prev.ByteCost,
EndorsementDeposit = prev.EndorsementDeposit,
EndorsementReward0 = prev.EndorsementReward0,
EndorsementReward1 = prev.EndorsementReward1,
EndorsersPerBlock = prev.EndorsersPerBlock,
HardBlockGasLimit = prev.HardBlockGasLimit,
HardOperationGasLimit = prev.HardOperationGasLimit,
HardOperationStorageLimit = prev.HardOperationStorageLimit,
OriginationSize = prev.OriginationSize,
PreservedCycles = prev.PreservedCycles,
RevelationReward = prev.RevelationReward,
TimeBetweenBlocks = prev.TimeBetweenBlocks,
TokensPerRoll = prev.TokensPerRoll,
BallotQuorumMin = prev.BallotQuorumMin,
BallotQuorumMax = prev.BallotQuorumMax,
ProposalQuorum = prev.ProposalQuorum,
LBSubsidy = prev.LBSubsidy,
LBSunsetLevel = prev.LBSunsetLevel,
LBEscapeThreshold = prev.LBEscapeThreshold
};
Db.Protocols.Add(protocol);
Cache.Protocols.Add(protocol);
Expand All @@ -101,33 +129,6 @@ public async Task DowngradeProtocol(AppState state)
Cache.Protocols.Reset();
}

protected virtual void UpgradeParameters(Protocol protocol, Protocol prev)
{
protocol.RampUpCycles = prev.RampUpCycles;
protocol.NoRewardCycles = prev.NoRewardCycles;
protocol.BlockDeposit = prev.BlockDeposit;
protocol.BlockReward0 = prev.BlockReward0;
protocol.BlockReward1 = prev.BlockReward1;
protocol.BlocksPerCommitment = prev.BlocksPerCommitment;
protocol.BlocksPerCycle = prev.BlocksPerCycle;
protocol.BlocksPerSnapshot = prev.BlocksPerSnapshot;
protocol.BlocksPerVoting = prev.BlocksPerVoting;
protocol.ByteCost = prev.ByteCost;
protocol.EndorsementDeposit = prev.EndorsementDeposit;
protocol.EndorsementReward0 = prev.EndorsementReward0;
protocol.EndorsementReward1 = prev.EndorsementReward1;
protocol.EndorsersPerBlock = prev.EndorsersPerBlock;
protocol.HardBlockGasLimit = prev.HardBlockGasLimit;
protocol.HardOperationGasLimit = prev.HardOperationGasLimit;
protocol.HardOperationStorageLimit = prev.HardOperationStorageLimit;
protocol.OriginationSize = prev.OriginationSize;
protocol.PreservedCycles = prev.PreservedCycles;
protocol.RevelationReward = prev.RevelationReward;
protocol.TimeBetweenBlocks = prev.TimeBetweenBlocks;
protocol.TokensPerRoll = prev.TokensPerRoll;
protocol.BallotQuorumMin = prev.BallotQuorumMin;
protocol.BallotQuorumMax = prev.BallotQuorumMax;
protocol.ProposalQuorum = prev.ProposalQuorum;
}
protected virtual void UpgradeParameters(Protocol protocol, Protocol prev) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ DELETE FROM ""BakingRights""

if (maxExistedPriority < block.Priority)
{
var bakingRights = await Proto.Rpc.GetLevelBakingRightsAsync(block.Level, block.Priority);
var bakingRights = await Proto.Rpc.GetLevelBakingRightsAsync(block.Level, block.Level, block.Priority);
//bakingRights = bakingRights.OrderBy(x => x.Priority);

var sqlInsert = @"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ protected virtual async Task TestDelegate(int level, Data.Models.Delegate delega

var frozenBalances = remote.RequiredArray("frozen_balance_by_cycle").EnumerateArray();

if ((frozenBalances.Any() ? frozenBalances.Sum(x => x.RequiredInt64("deposit")) : 0) != delegat.FrozenDeposits)
if ((frozenBalances.Any() ? frozenBalances.Sum(x => GetDeposits(x)) : 0) != delegat.FrozenDeposits)
throw new Exception($"Diagnostics failed: wrong frozen deposits {delegat.Address}");

if ((frozenBalances.Any() ? frozenBalances.Sum(x => x.RequiredInt64("rewards")) : 0) != delegat.FrozenRewards)
Expand Down Expand Up @@ -157,5 +157,7 @@ protected virtual void TestAccountCounter(JsonElement remote, Account local)
if (remote.RequiredInt64("balance") > 0 && remote.RequiredInt32("counter") != local.Counter)
throw new Exception($"Diagnostics failed: wrong counter {local.Address}");
}

protected virtual long GetDeposits(JsonElement json) => json.RequiredInt64("deposit");
}
}
15 changes: 9 additions & 6 deletions Tzkt.Sync/Protocols/Handlers/Proto1/Rpc/Rpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ public virtual Task<JsonElement> GetBlockAsync(int level)
public virtual Task<JsonElement> GetCycleAsync(int level, int cycle)
=> Node.GetAsync($"chains/main/blocks/{level}/context/raw/json/cycle/{cycle}");

public virtual Task<JsonElement> GetBakingRightsAsync(int level, int cycle)
=> Node.GetAsync($"chains/main/blocks/{level}/helpers/baking_rights?cycle={cycle}&max_priority=8&all=true");
public virtual Task<JsonElement> GetBakingRightsAsync(int block, int cycle)
=> Node.GetAsync($"chains/main/blocks/{block}/helpers/baking_rights?cycle={cycle}&max_priority=8&all=true");

public virtual Task<JsonElement> GetEndorsingRightsAsync(int level, int cycle)
=> Node.GetAsync($"chains/main/blocks/{level}/helpers/endorsing_rights?cycle={cycle}");
public virtual Task<JsonElement> GetEndorsingRightsAsync(int block, int cycle)
=> Node.GetAsync($"chains/main/blocks/{block}/helpers/endorsing_rights?cycle={cycle}");

public virtual Task<JsonElement> GetLevelBakingRightsAsync(int level, int maxPriority)
=> Node.GetAsync($"chains/main/blocks/{level}/helpers/baking_rights?level={level}&max_priority={maxPriority + 1}&all=true");
public virtual Task<JsonElement> GetLevelBakingRightsAsync(int block, int level, int maxPriority)
=> Node.GetAsync($"chains/main/blocks/{block}/helpers/baking_rights?level={level}&max_priority={maxPriority + 1}&all=true");

public virtual Task<JsonElement> GetLevelEndorsingRightsAsync(int block, int level)
=> Node.GetAsync($"chains/main/blocks/{block}/helpers/endorsing_rights?level={level}");

public virtual Task<JsonElement> GetContractAsync(int level, string address)
=> Node.GetAsync($"chains/main/blocks/{level}/context/contracts/{address}");
Expand Down
9 changes: 6 additions & 3 deletions Tzkt.Sync/Protocols/Handlers/Proto1/Validation/Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ protected virtual async Task ValidateOperations(JsonElement operations)
case "activate_account": await ValidateActivation(content); break;
case "double_baking_evidence": ValidateDoubleBaking(content); break;
case "double_endorsement_evidence": ValidateDoubleEndorsing(content); break;
case "seed_nonce_revelation": ValidateSeedNonceRevelation(content); break;
case "seed_nonce_revelation": await ValidateSeedNonceRevelation(content); break;
case "delegation": await ValidateDelegation(content); break;
case "origination": await ValidateOrigination(content); break;
case "transaction": await ValidateTransaction(content); break;
Expand Down Expand Up @@ -334,9 +334,12 @@ protected virtual void ValidateDoubleEndorsing(JsonElement content)
}
}

protected virtual void ValidateSeedNonceRevelation(JsonElement content)
protected virtual async Task ValidateSeedNonceRevelation(JsonElement content)
{
if (content.RequiredInt32("level") % Protocol.BlocksPerCommitment != 0)
var level = content.RequiredInt32("level");
var proto = await Cache.Protocols.FindByLevelAsync(level);

if (level % proto.BlocksPerCommitment != 0)
throw new ValidationException("invalid seed nonce revelation level");

var balanceUpdate = content.Required("metadata").RequiredArray("balance_updates", 1)[0];
Expand Down
Loading

0 comments on commit 37bcee0

Please sign in to comment.