diff --git a/beacon_chain/datatypes.nim b/beacon_chain/datatypes.nim index 1f029d4c5..9775ad4da 100644 --- a/beacon_chain/datatypes.nim +++ b/beacon_chain/datatypes.nim @@ -14,8 +14,8 @@ type AttestationVote* = object # Implementation pending https://ethresear.ch/t/implementations-of-proof-of-concept-beacon-chains/2509/5?u=mratsim - Keccak256_Digest* = distinct Hash256 # TODO, previously Keccak256 fields used the "bytes" type - Blake2_256_Digest* = distinct Hash256 # while Blake2 used hash32, but latest spec changed everything to hash32 + Keccak256_Digest* = Hash256 # TODO, previously Keccak256 fields used the "bytes" type + Blake2_256_Digest* = Hash256 # while Blake2 used hash32, but latest spec changed everything to hash32 BeaconBlock* = object parentHash*: Keccak256_Digest # Hash of the parent block @@ -53,8 +53,8 @@ type currentEpochShuffling: seq[int32] #int24 # The permutation of validators used to determine who cross-links what shard in this epoch lastJustifiedEpoch: int64 # The last justified epoch lastFinalizedEpoch: int64 # The last finalized epoch - current_dynasty: int64 # The current dynasty - next_shard: int16 # The next shard that cross-linking assignment will start from + currentDynasty: int64 # The current dynasty + nextShard: int16 # The next shard that cross-linking assignment will start from currentCheckpoint: Keccak256_Digest # The current FFG checkpoint crosslinkRecords: seq[CrosslinkRecord] # Records about the most recent crosslink `for each shard totalDeposits: Int256 # Total balance of deposits diff --git a/beacon_chain/private/helpers.nim b/beacon_chain/private/helpers.nim new file mode 100644 index 000000000..5143118c3 --- /dev/null +++ b/beacon_chain/private/helpers.nim @@ -0,0 +1,33 @@ +# beacon_chain +# Copyright (c) 2018 Status Research & Development GmbH +# Licensed and distributed under either of +# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT). +# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). +# at your option. This file may not be copied, modified, or distributed except according to those terms. + +# 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 + + assert validatorCount <= MaxValidatorCount + + let randMax = MaxValidatorCount - MaxValidatorCount mod validatorCount + result = toSeq(0 ..< validatorCount) + var source = seed + + var i = 0 + while i < validatorCount: + source = blake2_256.digest source.data + for pos in countup(0, 29, 3): + let remaining = validatorCount - i + if remaining == 0: + break + + let m = source.data[pos].int shl 16 or source.data[pos+1].int shl 8 or source.data[pos+2].int + + if validatorCount < randMax: + let replacementPos = m mod remaining + i + swap result[i], result[replacementPos] + inc i