Add the pseudo renadom shuffle

This commit is contained in:
mratsim 2018-07-23 14:58:41 +02:00
parent 2438aaada8
commit 4c6665832b
2 changed files with 37 additions and 4 deletions

View File

@ -14,8 +14,8 @@ type
AttestationVote* = object AttestationVote* = object
# Implementation pending https://ethresear.ch/t/implementations-of-proof-of-concept-beacon-chains/2509/5?u=mratsim # 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 Keccak256_Digest* = 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 Blake2_256_Digest* = Hash256 # while Blake2 used hash32, but latest spec changed everything to hash32
BeaconBlock* = object BeaconBlock* = object
parentHash*: Keccak256_Digest # Hash of the parent block 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 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 lastJustifiedEpoch: int64 # The last justified epoch
lastFinalizedEpoch: int64 # The last finalized epoch lastFinalizedEpoch: int64 # The last finalized epoch
current_dynasty: int64 # The current dynasty currentDynasty: int64 # The current dynasty
next_shard: int16 # The next shard that cross-linking assignment will start from nextShard: int16 # The next shard that cross-linking assignment will start from
currentCheckpoint: Keccak256_Digest # The current FFG checkpoint currentCheckpoint: Keccak256_Digest # The current FFG checkpoint
crosslinkRecords: seq[CrosslinkRecord] # Records about the most recent crosslink `for each shard crosslinkRecords: seq[CrosslinkRecord] # Records about the most recent crosslink `for each shard
totalDeposits: Int256 # Total balance of deposits totalDeposits: Int256 # Total balance of deposits

View File

@ -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