2019-02-25 14:48:36 +00:00
|
|
|
# Copyright (c) 2018-2019 Status Research & Development GmbH
|
2018-11-23 22:44:43 +00:00
|
|
|
# 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
|
|
|
|
|
2018-11-23 19:42:47 +00:00
|
|
|
import
|
2018-12-04 18:45:30 +00:00
|
|
|
options, nimcrypto, sequtils, math,
|
2019-02-05 19:21:18 +00:00
|
|
|
eth/common,
|
2018-12-03 21:41:24 +00:00
|
|
|
../ssz,
|
2018-11-28 19:49:03 +00:00
|
|
|
./crypto, ./datatypes, ./digest, ./helpers
|
2018-11-23 19:42:47 +00:00
|
|
|
|
2019-03-12 09:59:43 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/0.4.0/specs/core/0_beacon-chain.md#get_shuffling
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/0.4.0/specs/core/0_beacon-chain.md#get_permuted_index
|
2019-03-01 23:50:01 +00:00
|
|
|
func get_shuffled_seq*(seed: Eth2Digest,
|
2019-03-05 19:10:36 +00:00
|
|
|
list_size: uint64,
|
2019-03-01 23:50:01 +00:00
|
|
|
): seq[ValidatorIndex] =
|
2019-02-22 09:56:45 +00:00
|
|
|
## Via https://github.com/protolambda/eth2-shuffle/blob/master/shuffle.go
|
2019-02-13 10:26:32 +00:00
|
|
|
## Shuffles ``validators`` into crosslink committees seeded by ``seed`` and
|
|
|
|
## ``slot``.
|
2019-02-22 22:47:06 +00:00
|
|
|
## Returns a list of ``SLOTS_PER_EPOCH * committees_per_slot`` committees
|
|
|
|
## where each committee is itself a list of validator indices.
|
|
|
|
##
|
|
|
|
## Invert the inner/outer loops from the spec, essentially. Most useful
|
|
|
|
## hash result re-use occurs within a round.
|
2019-02-22 09:56:45 +00:00
|
|
|
var
|
2019-02-22 22:47:06 +00:00
|
|
|
# Share these buffers.
|
2019-02-22 09:56:45 +00:00
|
|
|
pivot_buffer: array[(32+1), byte]
|
2019-02-22 22:47:06 +00:00
|
|
|
source_buffer: array[(32+1+4), byte]
|
2019-02-22 22:59:10 +00:00
|
|
|
shuffled_active_validator_indices = mapIt(
|
|
|
|
0 ..< list_size.int, it.ValidatorIndex)
|
2019-03-01 14:39:17 +00:00
|
|
|
sources = repeat(Eth2Digest(), (list_size div 256) + 1)
|
2018-11-23 22:44:43 +00:00
|
|
|
|
2019-02-22 22:47:06 +00:00
|
|
|
## The pivot's a function of seed and round only.
|
|
|
|
## This doesn't change across rounds.
|
2019-02-22 09:56:45 +00:00
|
|
|
pivot_buffer[0..31] = seed.data
|
2019-02-22 22:47:06 +00:00
|
|
|
source_buffer[0..31] = seed.data
|
2019-02-22 09:56:45 +00:00
|
|
|
|
|
|
|
for round in 0 ..< SHUFFLE_ROUND_COUNT:
|
|
|
|
let round_bytes1 = int_to_bytes1(round)[0]
|
|
|
|
pivot_buffer[32] = round_bytes1
|
2019-02-22 22:47:06 +00:00
|
|
|
source_buffer[32] = round_bytes1
|
2019-02-22 09:56:45 +00:00
|
|
|
|
2019-02-22 22:47:06 +00:00
|
|
|
# Only one pivot per round.
|
|
|
|
let pivot = bytes_to_int(eth2hash(pivot_buffer).data[0..7]) mod list_size
|
2018-11-23 22:44:43 +00:00
|
|
|
|
2019-02-22 22:47:06 +00:00
|
|
|
## Only need to run, per round, position div 256 hashes, so precalculate
|
|
|
|
## them. This consumes memory, but for low-memory devices, it's possible
|
|
|
|
## to mitigate by some light LRU caching and similar.
|
2019-03-01 23:50:01 +00:00
|
|
|
for reduced_position in 0 ..< sources.len:
|
2019-02-22 22:47:06 +00:00
|
|
|
source_buffer[33..36] = int_to_bytes4(reduced_position.uint64)
|
|
|
|
sources[reduced_position] = eth2hash(source_buffer)
|
|
|
|
|
|
|
|
## Iterate over all the indices. This was in get_permuted_index, but large
|
|
|
|
## efficiency gains exist in caching and re-using data.
|
|
|
|
for index in 0 ..< list_size.int:
|
|
|
|
let
|
2019-03-01 23:50:01 +00:00
|
|
|
cur_idx_permuted = shuffled_active_validator_indices[index]
|
|
|
|
flip = ((list_size + pivot) - cur_idx_permuted.uint64) mod list_size
|
|
|
|
position = max(cur_idx_permuted, flip.int)
|
2019-02-22 22:47:06 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
source = sources[position div 256].data
|
|
|
|
byte_value = source[(position mod 256) div 8]
|
|
|
|
bit = (byte_value shr (position mod 8)) mod 2
|
|
|
|
|
|
|
|
if bit != 0:
|
|
|
|
shuffled_active_validator_indices[index] = flip.ValidatorIndex
|
2018-11-23 22:44:43 +00:00
|
|
|
|
2019-03-01 23:50:01 +00:00
|
|
|
result = shuffled_active_validator_indices
|
|
|
|
|
2019-03-13 01:46:44 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.4.0/specs/core/0_beacon-chain.md#get_shuffling
|
2019-03-01 23:50:01 +00:00
|
|
|
func get_shuffling*(seed: Eth2Digest,
|
|
|
|
validators: openArray[Validator],
|
2019-03-05 19:10:36 +00:00
|
|
|
epoch: Epoch,
|
|
|
|
shuffling_cache: ShufflingCache
|
2019-03-01 23:50:01 +00:00
|
|
|
): seq[seq[ValidatorIndex]] =
|
|
|
|
## This function is factored to facilitate testing with
|
|
|
|
## https://github.com/ethereum/eth2.0-test-generators/tree/master/permutated_index
|
|
|
|
## test vectors, which the split of get_shuffling obfuscates.
|
2019-03-13 01:46:44 +00:00
|
|
|
## TODO fix bad list size but keep consistent with cached values,
|
|
|
|
## once epoch processing reordering comes around
|
2019-03-05 19:10:36 +00:00
|
|
|
let list_size = validators.len.uint64
|
2019-03-01 23:50:01 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
active_validator_indices = get_active_validator_indices(validators, epoch)
|
|
|
|
committees_per_epoch = get_epoch_committee_count(
|
|
|
|
len(active_validator_indices)).int
|
2019-03-05 19:10:36 +00:00
|
|
|
# Both mapIt-type-conversions are an SSZ artifact. TODO remove.
|
|
|
|
shuffled_seq =
|
|
|
|
if shuffling_cache.seeds[0] == seed and
|
|
|
|
shuffling_cache.list_sizes[0] == list_size:
|
|
|
|
mapIt(shuffling_cache.shuffling_0, it.ValidatorIndex)
|
|
|
|
elif shuffling_cache.seeds[1] == seed and
|
|
|
|
shuffling_cache.list_sizes[1] == list_size:
|
|
|
|
mapIt(shuffling_cache.shuffling_1, it.ValidatorIndex)
|
|
|
|
else:
|
|
|
|
get_shuffled_seq(seed, list_size)
|
2019-03-01 23:50:01 +00:00
|
|
|
|
2019-01-29 03:22:22 +00:00
|
|
|
# Split the shuffled list into committees_per_epoch pieces
|
2019-03-05 19:10:36 +00:00
|
|
|
result = split(shuffled_seq, committees_per_epoch)
|
2019-03-13 23:04:43 +00:00
|
|
|
doAssert result.len() == committees_per_epoch # what split should do..
|
2018-12-03 21:41:24 +00:00
|
|
|
|
2019-03-12 09:59:43 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/0.4.0/specs/core/0_beacon-chain.md#get_previous_epoch_committee_count
|
2019-01-29 03:22:22 +00:00
|
|
|
func get_previous_epoch_committee_count(state: BeaconState): uint64 =
|
2019-03-12 09:59:43 +00:00
|
|
|
## Return the number of committees in the previous epoch of the given
|
|
|
|
## ``state``.
|
2019-01-23 01:50:51 +00:00
|
|
|
let previous_active_validators = get_active_validator_indices(
|
|
|
|
state.validator_registry,
|
2019-02-19 23:07:56 +00:00
|
|
|
state.previous_shuffling_epoch,
|
2019-01-23 01:50:51 +00:00
|
|
|
)
|
2019-01-29 03:22:22 +00:00
|
|
|
get_epoch_committee_count(len(previous_active_validators))
|
2019-01-23 01:50:51 +00:00
|
|
|
|
2019-03-11 15:33:24 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/0.4.0/specs/core/0_beacon-chain.md#get_next_epoch_committee_count
|
2019-02-07 19:27:13 +00:00
|
|
|
func get_next_epoch_committee_count(state: BeaconState): uint64 =
|
2019-02-20 01:33:58 +00:00
|
|
|
## Return the number of committees in the next epoch of the given ``state``.
|
2019-02-07 19:27:13 +00:00
|
|
|
let next_active_validators = get_active_validator_indices(
|
|
|
|
state.validator_registry,
|
|
|
|
get_current_epoch(state) + 1,
|
|
|
|
)
|
|
|
|
get_epoch_committee_count(len(next_active_validators))
|
|
|
|
|
2019-03-08 18:23:42 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/0.4.0/specs/core/0_beacon-chain.md#get_previous_epoch
|
|
|
|
func get_previous_epoch*(state: BeaconState): Epoch =
|
2019-02-11 15:10:46 +00:00
|
|
|
## Return the previous epoch of the given ``state``.
|
2019-03-08 18:23:42 +00:00
|
|
|
max(get_current_epoch(state) - 1, GENESIS_EPOCH)
|
2019-02-11 15:10:46 +00:00
|
|
|
|
2019-03-12 09:59:43 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/0.4.0/specs/core/0_beacon-chain.md#get_crosslink_committees_at_slot
|
2019-03-12 22:21:32 +00:00
|
|
|
func get_crosslink_committees_at_slot*(state: BeaconState, slot: Slot|uint64,
|
2019-02-07 19:27:13 +00:00
|
|
|
registry_change: bool = false):
|
2019-02-06 19:37:25 +00:00
|
|
|
seq[CrosslinkCommittee] =
|
2019-01-23 01:50:51 +00:00
|
|
|
## Returns the list of ``(committee, shard)`` tuples for the ``slot``.
|
2019-02-07 19:27:13 +00:00
|
|
|
##
|
|
|
|
## Note: There are two possible shufflings for crosslink committees for a
|
|
|
|
## ``slot`` in the next epoch -- with and without a `registry_change`
|
2019-01-23 01:50:51 +00:00
|
|
|
|
2019-01-29 03:22:22 +00:00
|
|
|
let
|
2019-03-09 04:23:14 +00:00
|
|
|
# TODO: the + 1 here works around a bug, remove when upgrading to
|
|
|
|
# some more recent version:
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/pull/732
|
2019-03-12 09:59:43 +00:00
|
|
|
# It's not 100% clear to me regarding 0.4.0; waiting until 0.5.0 to remove
|
|
|
|
# TODO recheck
|
2019-03-09 04:23:14 +00:00
|
|
|
epoch = slot_to_epoch(slot + 1)
|
2019-01-29 03:22:22 +00:00
|
|
|
current_epoch = get_current_epoch(state)
|
2019-02-11 15:10:46 +00:00
|
|
|
previous_epoch = get_previous_epoch(state)
|
2019-01-29 03:22:22 +00:00
|
|
|
next_epoch = current_epoch + 1
|
|
|
|
|
2019-03-09 04:23:14 +00:00
|
|
|
doAssert previous_epoch <= epoch,
|
2019-02-15 16:33:32 +00:00
|
|
|
"Previous epoch: " & $humaneEpochNum(previous_epoch) &
|
|
|
|
", epoch: " & $humaneEpochNum(epoch) &
|
|
|
|
", Next epoch: " & $humaneEpochNum(next_epoch)
|
|
|
|
|
2019-03-09 04:23:14 +00:00
|
|
|
doAssert epoch <= next_epoch,
|
2019-02-15 16:33:32 +00:00
|
|
|
"Previous epoch: " & $humaneEpochNum(previous_epoch) &
|
|
|
|
", epoch: " & $humaneEpochNum(epoch) &
|
|
|
|
", Next epoch: " & $humaneEpochNum(next_epoch)
|
2019-01-29 03:22:22 +00:00
|
|
|
|
2019-02-07 12:03:02 +00:00
|
|
|
template get_epoch_specific_params(): auto =
|
2019-03-12 09:59:43 +00:00
|
|
|
if epoch == current_epoch:
|
2019-01-29 03:22:22 +00:00
|
|
|
let
|
|
|
|
committees_per_epoch = get_current_epoch_committee_count(state)
|
2019-02-19 23:07:56 +00:00
|
|
|
seed = state.current_shuffling_seed
|
|
|
|
shuffling_epoch = state.current_shuffling_epoch
|
|
|
|
shuffling_start_shard = state.current_shuffling_start_shard
|
2019-01-29 03:22:22 +00:00
|
|
|
(committees_per_epoch, seed, shuffling_epoch, shuffling_start_shard)
|
2019-03-12 09:59:43 +00:00
|
|
|
elif epoch == previous_epoch:
|
|
|
|
let
|
|
|
|
committees_per_epoch = get_previous_epoch_committee_count(state)
|
|
|
|
seed = state.previous_shuffling_seed
|
|
|
|
shuffling_epoch = state.previous_shuffling_epoch
|
|
|
|
shuffling_start_shard = state.previous_shuffling_start_shard
|
|
|
|
(committees_per_epoch, seed, shuffling_epoch, shuffling_start_shard)
|
2019-02-07 19:27:13 +00:00
|
|
|
else:
|
2019-03-09 04:23:14 +00:00
|
|
|
doAssert epoch == next_epoch
|
2019-02-07 19:27:13 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
current_committees_per_epoch = get_current_epoch_committee_count(state)
|
|
|
|
committees_per_epoch = get_next_epoch_committee_count(state)
|
|
|
|
shuffling_epoch = next_epoch
|
|
|
|
|
|
|
|
epochs_since_last_registry_update = current_epoch - state.validator_registry_update_epoch
|
|
|
|
condition = epochs_since_last_registry_update > 1'u64 and
|
|
|
|
is_power_of_2(epochs_since_last_registry_update)
|
|
|
|
seed = if registry_change or condition:
|
|
|
|
generate_seed(state, next_epoch)
|
|
|
|
else:
|
2019-02-19 23:07:56 +00:00
|
|
|
state.current_shuffling_seed
|
2019-02-07 19:27:13 +00:00
|
|
|
shuffling_start_shard =
|
|
|
|
if registry_change:
|
2019-02-19 23:07:56 +00:00
|
|
|
(state.current_shuffling_start_shard +
|
2019-02-07 19:27:13 +00:00
|
|
|
current_committees_per_epoch) mod SHARD_COUNT
|
|
|
|
else:
|
2019-02-19 23:07:56 +00:00
|
|
|
state.current_shuffling_start_shard
|
2019-02-07 19:27:13 +00:00
|
|
|
(committees_per_epoch, seed, shuffling_epoch, shuffling_start_shard)
|
2019-01-29 03:22:22 +00:00
|
|
|
|
2019-02-07 12:03:02 +00:00
|
|
|
let (committees_per_epoch, seed, shuffling_epoch, shuffling_start_shard) =
|
|
|
|
get_epoch_specific_params()
|
2019-01-29 03:22:22 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
shuffling = get_shuffling(
|
|
|
|
seed,
|
|
|
|
state.validator_registry,
|
|
|
|
shuffling_epoch,
|
2019-03-12 09:59:43 +00:00
|
|
|
|
|
|
|
# Not in spec
|
2019-03-05 19:10:36 +00:00
|
|
|
state.shuffling_cache
|
2019-01-29 03:22:22 +00:00
|
|
|
)
|
2019-02-20 01:33:58 +00:00
|
|
|
offset = slot mod SLOTS_PER_EPOCH
|
|
|
|
committees_per_slot = committees_per_epoch div SLOTS_PER_EPOCH
|
2019-01-29 03:22:22 +00:00
|
|
|
slot_start_shard = (shuffling_start_shard + committees_per_slot * offset) mod SHARD_COUNT
|
|
|
|
|
|
|
|
for i in 0 ..< committees_per_slot.int:
|
|
|
|
result.add (
|
|
|
|
shuffling[(committees_per_slot * offset + i.uint64).int],
|
|
|
|
(slot_start_shard + i.uint64) mod SHARD_COUNT
|
|
|
|
)
|
2019-01-26 19:32:10 +00:00
|
|
|
|
2019-03-12 09:59:43 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/0.4.0/specs/core/0_beacon-chain.md#get_beacon_proposer_index
|
2019-02-20 20:35:27 +00:00
|
|
|
func get_beacon_proposer_index*(state: BeaconState, slot: Slot): ValidatorIndex =
|
2019-01-26 19:32:10 +00:00
|
|
|
## From Casper RPJ mini-spec:
|
|
|
|
## When slot i begins, validator Vidx is expected
|
|
|
|
## to create ("propose") a block, which contains a pointer to some parent block
|
|
|
|
## that they perceive as the "head of the chain",
|
|
|
|
## and includes all of the **attestations** that they know about
|
|
|
|
## that have not yet been included into that chain.
|
|
|
|
##
|
|
|
|
## idx in Vidx == p(i mod N), pi being a random permutation of validators indices (i.e. a committee)
|
|
|
|
## Returns the beacon proposer index for the ``slot``.
|
|
|
|
# TODO this index is invalid outside of the block state transition function
|
|
|
|
# because presently, `state.slot += 1` happens before this function
|
|
|
|
# is called - see also testutil.getNextBeaconProposerIndex
|
2019-02-28 21:21:29 +00:00
|
|
|
# TODO is the above still true? the shuffling has changed since it was written
|
2019-01-30 08:41:07 +00:00
|
|
|
let (first_committee, _) = get_crosslink_committees_at_slot(state, slot)[0]
|
2019-02-07 21:14:08 +00:00
|
|
|
let idx = int(slot mod uint64(first_committee.len))
|
2019-02-07 15:58:39 +00:00
|
|
|
first_committee[idx]
|