Skip to content

Commit

Permalink
Add initial_seed parameter handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Jun 24, 2022
1 parent f8e7e23 commit 38f2f58
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Netezos.Encoding;
using Newtonsoft.Json.Linq;
using Tzkt.Data.Models;

namespace Tzkt.Sync.Protocols.Proto1
{
partial class ProtoActivator : ProtocolCommit
{
public virtual List<Cycle> BootstrapCycles(Protocol protocol, List<Account> accounts)
public virtual List<Cycle> BootstrapCycles(Protocol protocol, List<Account> accounts, JToken parameters)
{
var cycles = new List<Cycle>(protocol.PreservedCycles + 1);
var delegates = accounts
.Where(x => x.Type == AccountType.Delegate)
.Select(x => x as Delegate);
.Select(x => x as Data.Models.Delegate);

var totalStake = delegates.Sum(x => x.StakingBalance);
var totalDelegated = delegates.Sum(x => x.DelegatedBalance);
Expand All @@ -22,7 +25,11 @@ public virtual List<Cycle> BootstrapCycles(Protocol protocol, List<Account> acco
var selectedStake = delegates.Sum(x => x.StakingBalance - x.StakingBalance % protocol.TokensPerRoll);
var selectedBakers = delegates.Count(x => x.StakingBalance >= protocol.TokensPerRoll);

var seeds = Seed.GetInitialSeeds(protocol.PreservedCycles + 1);
var base58Seed = parameters["initial_seed"]?.Value<string>();
if (!Base58.TryParse(base58Seed, new byte[3], out var initialSeed) || initialSeed.Length != 32)
initialSeed = Array.Empty<byte>();

var seeds = Seed.GetInitialSeeds(protocol.PreservedCycles + 1, initialSeed);
for (int index = 0; index <= protocol.PreservedCycles; index++)
{
var cycle = new Cycle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public async Task Activate(AppState state, JsonElement rawBlock)
var (protocol, parameters) = BootstrapProtocol(rawBlock);

var accounts = await BootstrapAccounts(protocol, parameters);
var cycles = BootstrapCycles(protocol, accounts);
var cycles = BootstrapCycles(protocol, accounts, parameters);
var (bakingRights, endorsingRights) = await BootstrapBakingRights(protocol, cycles);
BootstrapDelegatorCycles(protocol, accounts);
BootstrapBakerCycles(protocol, accounts, cycles, bakingRights, endorsingRights);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
using Tzkt.Data.Models;

namespace Tzkt.Sync.Protocols.Proto12
{
partial class ProtoActivator : Proto11.ProtoActivator
{
public override List<Cycle> BootstrapCycles(Protocol protocol, List<Account> accounts)
public override List<Cycle> BootstrapCycles(Protocol protocol, List<Account> accounts, JToken parameters)
{
var cycles = base.BootstrapCycles(protocol, accounts);
var cycles = base.BootstrapCycles(protocol, accounts, parameters);

var delegates = accounts
.Where(x => x is Data.Models.Delegate d && d.StakingBalance >= protocol.TokensPerRoll)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ protected override void SetParameters(Protocol protocol, JToken parameters)
{
base.SetParameters(protocol, parameters);
protocol.LBToggleThreshold = parameters["liquidity_baking_toggle_ema_threshold"]?.Value<int>() ?? 1_000_000_000;
protocol.BlocksPerVoting = (parameters["cycles_per_voting_period"]?.Value<int>() ?? 5) * protocol.BlocksPerCycle;
}

protected override void UpgradeParameters(Protocol protocol, Protocol prev)
Expand Down
4 changes: 2 additions & 2 deletions Tzkt.Sync/Protocols/Helpers/Seed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace Tzkt.Sync.Protocols
{
static class Seed
{
public static List<byte[]> GetInitialSeeds(int count)
public static List<byte[]> GetInitialSeeds(int count, byte[] initialSeed)
{
var list = new List<byte[]>(count)
{
Blake2b.ComputeHash(32, Array.Empty<byte>())
Blake2b.ComputeHash(32, initialSeed)
};
for (int i = 1; i < count; i++)
{
Expand Down

0 comments on commit 38f2f58

Please sign in to comment.