From 585072ae154e3d10276b51397e707628a401c096 Mon Sep 17 00:00:00 2001 From: mratsim Date: Mon, 23 Jul 2018 16:22:37 +0200 Subject: [PATCH] Add getCutoffs helper (buggy) --- beacon_chain/datatypes.nim | 16 +++++----- beacon_chain/private/helpers.nim | 50 ++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/beacon_chain/datatypes.nim b/beacon_chain/datatypes.nim index 9775ad4da..dbecdf354 100644 --- a/beacon_chain/datatypes.nim +++ b/beacon_chain/datatypes.nim @@ -78,11 +78,11 @@ type hash: Keccak256_Digest # The block hash const - ShardCount* = 1024 # a constant referring to the number of shards - EthSupplyCap* = 2^27 # ~= 134 million - DepositSize* = 32 # You need to deposit 32 ETH to be a validator in Casper - MaxValidatorCount* = EthSupplyCap div DepositSize # 4_194_304 - EpochLength* = 64 # blocks - SlotDuration* = 8 # seconds - MinCommitteeSize* = 128 # (rationale: see recommended minimum 111 here https://vitalik.ca/files/Ithaca201807_Sharding.pdf) - EndEpochGracePeriod = 8 # blocks + ShardCount* = 1024 # a constant referring to the number of shards + EthSupplyCap* = 2^27 # ~= 134 million + DepositSize* = 32 # You need to deposit 32 ETH to be a validator in Casper + MaxValidatorCount* = EthSupplyCap div DepositSize # 4_194_304 + EpochLength* = 64 # blocks + SlotDuration* = 8 # seconds + MinCommitteeSize* = 128 # (rationale: see recommended minimum 111 here https://vitalik.ca/files/Ithaca201807_Sharding.pdf) + EndEpochGracePeriod* = 8 # blocks diff --git a/beacon_chain/private/helpers.nim b/beacon_chain/private/helpers.nim index 5143118c3..f0fcf73cb 100644 --- a/beacon_chain/private/helpers.nim +++ b/beacon_chain/private/helpers.nim @@ -8,8 +8,8 @@ # Helper functions import ../datatypes, sequtils, nimcrypto -func getShuffling(seed: Blake2_256_Digest, validatorCount: int): seq[int] {.noInit.}= - # Pseudorandomly shuffles the validator set based on some seed +func getShuffling*(seed: Blake2_256_Digest, validatorCount: int): seq[int] {.noInit.}= + ## Pseudorandomly shuffles the validator set based on some seed assert validatorCount <= MaxValidatorCount @@ -31,3 +31,49 @@ func getShuffling(seed: Blake2_256_Digest, validatorCount: int): seq[int] {.noIn let replacementPos = m mod remaining + i swap result[i], result[replacementPos] inc i + +func getCutoffs*(validatorCount: int): tuple[height, shard: seq[int]] {.noInit.} = + ## Split up validators into groups at the start of every epoch, + ## determining at what height they can make attestations and what shard they are making crosslinks for + ## Implementation should do the following: http://vitalik.ca/files/ShuffleAndAssign.png + ## TODO: It doens't work. + + result.height = @[0] + let cofactor = 39 # EpochLength / phi + const StandardCommitteeSize = MaxValidatorCount div ShardCount + + var heightCount: int + var heights: seq[int] + + if validatorCount < EpochLength * MinCommitteeSize: + # If there are not enough validators to fill a minimally + # sized committee at every height, skip some heights + heightCount = validatorCount div MinCommitteeSize or 1 # TODO div/or precedence ? + for i in 0 ..< heightCount: + heights.add (i * cofactor) mod EpochLength + else: + # If there are enough validators, fill all the heights + heightCount = EpochLength + heights = toSeq(0 ..< EpochLength) + + var filled = 0 + for i in 0 ..< EpochLength - 1: + if i notin heights: # TODO, this will be slow for seq, use intsets instead? + result.height.add result.height[^1] + else: + inc filled + result.height.add filled * validatorCount div heightCount + result.height.add validatorCount + + # For the validators assigned to each height, split them up + # into committees for different shards. Do not assign the + # last END_EPOCH_GRACE_PERIOD heights in an epoch to any shards. + + result.shard = @[0] + for i in 0 ..< EpochLength - EndEpochGracePeriod: + let + size = result.height[i+1] - result.height[i] + shards = (size + StandardCommitteeSize - 1) div StandardCommitteeSize + pre = result.shard[^1] + for j in 1 .. shards: + result.shard.add pre + size * j div shards