Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

enhancement/get-shuffling-to-shuffling #61

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions Sources/BeaconChain/BeaconChain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extension BeaconChain {

extension BeaconChain {

// @todo check this shit
// @todo consider moving to Array+Validator & check this shit
static func getPermutedIndex(index i: Int, listSize: Int, seed: Bytes32) -> Int {
assert(i < listSize)
assert(listSize < 2**40)
Expand All @@ -61,16 +61,6 @@ extension BeaconChain {

return index
}

static func getShuffling(seed: Bytes32, validators: [Validator], epoch: Epoch) -> [[ValidatorIndex]] {
let activeValidatorIndices = validators.activeIndices(epoch: epoch)

let shuffledActiveValidatorIndices = activeValidatorIndices.map {
activeValidatorIndices[getPermutedIndex(index: Int($0), listSize: activeValidatorIndices.count, seed: seed)]
}

return shuffledActiveValidatorIndices.split(count: getEpochCommitteeCount(activeValidatorCount: activeValidatorIndices.count))
}
}

extension BeaconChain {
Expand Down Expand Up @@ -146,7 +136,7 @@ extension BeaconChain {
}
}

let shuffling = getShuffling(seed: seed, validators: state.validatorRegistry, epoch: shufflingEpoch)
let shuffling = state.validatorRegistry.shuffling(seed: seed, epoch: shufflingEpoch)

let offset = slot % SLOTS_PER_EPOCH
let committeesPerSlot = UInt64(committeesPerEpoch) / SLOTS_PER_EPOCH
Expand Down
11 changes: 11 additions & 0 deletions Sources/BeaconChain/Extensions/Array+Validator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,15 @@ extension Array where Element == Validator {
return nil
}
}

func shuffling(seed: Bytes32, epoch: Epoch) -> [[ValidatorIndex]] {
let indices = activeIndices(epoch: epoch)
let size = indices.count

let shuffled = indices.map {
indices[BeaconChain.getPermutedIndex(index: Int($0), listSize: size, seed: seed)]
}

return shuffled.split(count: BeaconChain.getEpochCommitteeCount(activeValidatorCount: size))
}
}