-
Notifications
You must be signed in to change notification settings - Fork 261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
spec updates #20
spec updates #20
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# 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. | ||
|
||
# Helpers and functions pertaining to managing the validator set | ||
|
||
import | ||
options, | ||
eth_common, | ||
./datatypes, ./private/helpers | ||
|
||
func min_empty_validator(validators: seq[ValidatorRecord], current_slot: uint64): Option[int] = | ||
for i, v in validators: | ||
if v.status == WITHDRAWN and v.exit_slot + DELETION_PERIOD.uint64 <= current_slot: | ||
return some(i) | ||
|
||
func add_validator*(validators: var seq[ValidatorRecord], | ||
pubkey: BLSPublicKey, | ||
proof_of_possession: seq[byte], | ||
withdrawal_shard: uint16, | ||
withdrawal_address: EthAddress, | ||
randao_commitment: Blake2_256_Digest, | ||
status: ValidatorStatusCodes, | ||
current_slot: uint64 | ||
): int = | ||
# Check that validator really did register | ||
# let signed_message = as_bytes32(pubkey) + as_bytes2(withdrawal_shard) + withdrawal_address + randao_commitment | ||
# assert BLSVerify(pub=pubkey, | ||
# msg=hash(signed_message), | ||
# sig=proof_of_possession) | ||
|
||
# Pubkey uniqueness | ||
# assert pubkey not in [v.pubkey for v in validators] | ||
let | ||
rec = ValidatorRecord( | ||
pubkey: pubkey, | ||
withdrawal_shard: withdrawal_shard, | ||
withdrawal_address: withdrawal_address, | ||
randao_commitment: randao_commitment, | ||
randao_last_change: current_slot, | ||
balance: DEPOSIT_SIZE * GWEI_PER_ETH, | ||
status: status, | ||
exit_slot: 0, | ||
exit_seq: 0 | ||
) | ||
|
||
let index = min_empty_validator(validators, current_slot) | ||
if index.isNone: | ||
validators.add(rec) | ||
return len(validators) - 1 | ||
else: | ||
validators[index.get()] = rec | ||
return index.get() | ||
|
||
func get_active_validator_indices(validators: openArray[ValidatorRecord]): seq[Uint24] = | ||
## Select the active validators | ||
result = @[] | ||
for idx, val in validators: | ||
if val.status == ACTIVE: | ||
result.add idx.Uint24 | ||
|
||
func get_new_shuffling*(seed: Blake2_256_Digest, | ||
validators: openArray[ValidatorRecord], | ||
crosslinking_start_shard: int | ||
): array[CYCLE_LENGTH, seq[ShardAndCommittee]] = | ||
## 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 | ||
|
||
let | ||
active_validators = get_active_validator_indices(validators) | ||
committees_per_slot = clamp( | ||
len(active_validators) div CYCLE_LENGTH div TARGET_COMMITTEE_SIZE, | ||
1, SHARD_COUNT div CYCLE_LENGTH) | ||
# Shuffle with seed | ||
shuffled_active_validator_indices = shuffle(active_validators, seed) | ||
# Split the shuffled list into cycle_length pieces | ||
validators_per_slot = split(shuffled_active_validator_indices, CYCLE_LENGTH) | ||
|
||
assert validators_per_slot.len() == CYCLE_LENGTH # what split should do.. | ||
|
||
for slot, slot_indices in validators_per_slot: | ||
let | ||
shard_indices = split(slot_indices, committees_per_slot) | ||
shard_id_start = crosslinking_start_shard + slot * committees_per_slot | ||
|
||
var committees = newSeq[ShardAndCommittee](shard_indices.len) | ||
for shard_position, indices in shard_indices: | ||
committees[shard_position].shard_id = (shard_id_start + shard_position).uint16 mod SHARD_COUNT | ||
committees[shard_position].committee = indices | ||
|
||
result[slot] = committees |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# 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. | ||
|
||
import | ||
math, nimcrypto, unittest, sequtils, | ||
../beacon_chain/[datatypes, validator] | ||
|
||
func sumCommittees(v: openArray[seq[ShardAndCommittee]]): int = | ||
for x in v: | ||
for y in x: | ||
inc result, y.committee.len | ||
|
||
suite "Validators": | ||
## For now just test that we can compile and execute block processing with mock data. | ||
|
||
test "Smoke validator shuffling": | ||
let | ||
validators = repeat( | ||
ValidatorRecord( | ||
status: ACTIVE | ||
), 1024) | ||
|
||
# XXX the shuffling looks really odd, probably buggy | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Repurposed #1 (comment) for shuffling test implementations using Sigp tests as reference - https://github.com/sigp/lighthouse/blob/ba548e49a52687a655c61b443b6835d79c6d4236/beacon_chain/validator_shuffling/src/shuffle.rs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks! added links to code, to keep track |
||
let s = get_new_shuffling(Blake2_256_Digest(), validators, 0) | ||
check: | ||
s.len == CYCLE_LENGTH | ||
sumCommittees(s) == validators.len() # all validators accounted for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably need a
mixin init
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cf: https://github.com/cheatfate/nimcrypto/blob/master/nimcrypto/hash.nim#L60-L80 I think Nim tries to resolve the symbol too early (nim-lang/Nim#8677)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mixin init
here doesn't seem to help - added a comment for now, will dig into it later