2020-01-27 11:36:17 +00:00
|
|
|
# Copyright (c) 2018-2020 Status Research & Development GmbH
|
2018-11-23 22:44:43 +00:00
|
|
|
# Licensed and distributed under either of
|
2019-11-25 15:30:02 +00:00
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
2018-11-23 22:44:43 +00:00
|
|
|
# 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
|
|
|
|
|
2020-04-22 05:53:02 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2018-11-23 19:42:47 +00:00
|
|
|
import
|
2020-07-27 16:04:44 +00:00
|
|
|
algorithm, options, sequtils, math, tables,
|
|
|
|
./datatypes, ./digest, ./helpers
|
2018-11-23 19:42:47 +00:00
|
|
|
|
2020-07-27 16:49:46 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/beacon-chain.md#compute_shuffled_index
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/beacon-chain.md#compute_committee
|
2019-11-27 20:43:02 +00:00
|
|
|
func get_shuffled_seq*(seed: Eth2Digest,
|
2019-11-25 06:31:37 +00:00
|
|
|
list_size: uint64,
|
|
|
|
): seq[ValidatorIndex] =
|
2019-02-22 09:56:45 +00:00
|
|
|
## Via https://github.com/protolambda/eth2-shuffle/blob/master/shuffle.go
|
2019-11-25 06:31:37 +00:00
|
|
|
## Shuffles ``validators`` into beacon committees, seeded by ``seed`` and
|
2019-02-13 10:26:32 +00:00
|
|
|
## ``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-05-10 08:14:01 +00:00
|
|
|
|
|
|
|
# Empty size -> empty list.
|
|
|
|
if list_size == 0:
|
|
|
|
return
|
|
|
|
|
2019-02-22 09:56:45 +00:00
|
|
|
var
|
2019-02-22 22:47:06 +00:00
|
|
|
# Share these buffers.
|
2019-05-10 08:14:01 +00:00
|
|
|
# TODO: Redo to follow spec.
|
|
|
|
# We can have an "Impl" private version that takes buffer as parameters
|
|
|
|
# so that we avoid alloc on repeated calls from compute_committee
|
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
|
|
|
|
2020-07-27 16:04:44 +00:00
|
|
|
static: doAssert SHUFFLE_ROUND_COUNT < uint8.high
|
|
|
|
for round in 0'u8 ..< SHUFFLE_ROUND_COUNT.uint8:
|
|
|
|
pivot_buffer[32] = round
|
|
|
|
source_buffer[32] = round
|
2019-02-22 09:56:45 +00:00
|
|
|
|
2019-02-22 22:47:06 +00:00
|
|
|
# Only one pivot per round.
|
2020-07-27 10:59:57 +00:00
|
|
|
let pivot =
|
|
|
|
bytes_to_uint64(eth2digest(pivot_buffer).data.toOpenArray(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:
|
2020-07-27 10:59:57 +00:00
|
|
|
source_buffer[33..36] = uint_to_bytes4(reduced_position.uint64)
|
2020-06-16 12:16:43 +00:00
|
|
|
sources[reduced_position] = eth2digest(source_buffer)
|
2019-02-22 22:47:06 +00:00
|
|
|
|
|
|
|
## 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
|
2019-09-29 17:42:53 +00:00
|
|
|
position = max(cur_idx_permuted.int, 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
|
|
|
|
2020-07-15 10:44:18 +00:00
|
|
|
shuffled_active_validator_indices
|
2019-03-01 23:50:01 +00:00
|
|
|
|
2020-05-29 06:10:20 +00:00
|
|
|
func get_shuffled_active_validator_indices*(state: BeaconState, epoch: Epoch):
|
|
|
|
seq[ValidatorIndex] =
|
|
|
|
# Non-spec function, to cache a data structure from which one can cheaply
|
|
|
|
# compute both get_active_validator_indexes() and get_beacon_committee().
|
|
|
|
let active_validator_indices = get_active_validator_indices(state, epoch)
|
|
|
|
mapIt(
|
|
|
|
get_shuffled_seq(
|
|
|
|
get_seed(state, epoch, DOMAIN_BEACON_ATTESTER),
|
2020-07-26 18:55:48 +00:00
|
|
|
active_validator_indices.lenu64),
|
2020-05-29 06:10:20 +00:00
|
|
|
active_validator_indices[it])
|
|
|
|
|
2020-07-15 10:44:18 +00:00
|
|
|
func get_shuffled_active_validator_indices*(
|
2020-07-27 16:04:44 +00:00
|
|
|
cache: var StateCache, state: BeaconState, epoch: Epoch):
|
|
|
|
var seq[ValidatorIndex] =
|
|
|
|
# `cache` comes first because of nim's borrowing rules for the `var` return -
|
|
|
|
# the `var` returns avoids copying the validator set.
|
|
|
|
cache.shuffled_active_validator_indices.withValue(epoch, validator_indices) do:
|
|
|
|
return validator_indices[]
|
|
|
|
do:
|
|
|
|
let indices = get_shuffled_active_validator_indices(state, epoch)
|
|
|
|
return cache.shuffled_active_validator_indices.mgetOrPut(epoch, indices)
|
|
|
|
|
2020-07-27 16:49:46 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/beacon-chain.md#get_active_validator_indices
|
2020-07-27 16:04:44 +00:00
|
|
|
func count_active_validators*(state: BeaconState,
|
|
|
|
epoch: Epoch,
|
|
|
|
cache: var StateCache): uint64 =
|
|
|
|
cache.get_shuffled_active_validator_indices(state, epoch).lenu64
|
|
|
|
|
2020-07-27 16:49:46 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/beacon-chain.md#get_committee_count_per_slot
|
2020-07-27 16:04:44 +00:00
|
|
|
func get_committee_count_per_slot*(num_active_validators: uint64): uint64 =
|
|
|
|
clamp(
|
|
|
|
num_active_validators div SLOTS_PER_EPOCH div TARGET_COMMITTEE_SIZE,
|
|
|
|
1'u64, MAX_COMMITTEES_PER_SLOT)
|
|
|
|
|
|
|
|
func get_committee_count_per_slot*(state: BeaconState,
|
|
|
|
epoch: Epoch,
|
|
|
|
cache: var StateCache): uint64 =
|
|
|
|
# Return the number of committees at ``slot``.
|
|
|
|
|
|
|
|
# TODO this is mostly used in for loops which have indexes which then need to
|
|
|
|
# be converted to CommitteeIndex types for get_beacon_committee(...); replace
|
|
|
|
# with better and more type-safe use pattern, probably beginning with using a
|
|
|
|
# CommitteeIndex return type here.
|
|
|
|
let
|
|
|
|
active_validator_count = count_active_validators(state, epoch, cache)
|
|
|
|
result = get_committee_count_per_slot(active_validator_count)
|
|
|
|
|
|
|
|
# Otherwise, get_beacon_committee(...) cannot access some committees.
|
|
|
|
doAssert (SLOTS_PER_EPOCH * MAX_COMMITTEES_PER_SLOT) >= uint64(result)
|
|
|
|
|
|
|
|
func get_committee_count_per_slot*(state: BeaconState,
|
|
|
|
slot: Slot,
|
|
|
|
cache: var StateCache): uint64 =
|
|
|
|
get_committee_count_per_slot(state, slot.compute_epoch_at_slot, cache)
|
2020-07-15 10:44:18 +00:00
|
|
|
|
2020-07-27 16:49:46 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/beacon-chain.md#get_previous_epoch
|
2020-07-22 07:51:45 +00:00
|
|
|
func get_previous_epoch*(current_epoch: Epoch): Epoch =
|
2019-07-01 09:05:22 +00:00
|
|
|
# Return the previous epoch (unless the current epoch is ``GENESIS_EPOCH``).
|
2019-06-14 13:50:47 +00:00
|
|
|
if current_epoch == GENESIS_EPOCH:
|
More 0.6.1 updates (#274)
* implement get_churn_limit from 0.6.1; update initiate_validator_exit and get_previous_epoch to 0.6.1; remove obsoleted/non-spec reduce_balance, replaced by decrease_balance; mark BeaconBlock as 0.6.1
* rename get_block_root to get_block_root_at_slot and introduce epoch-oriented get_block_root; implement get_attestation_slot, get_matching_source_attestations, get_matching_target_attestations, get_matching_head_attestations, 0.6.1 get_attesting_indices, get_unslashed_attesting_indices, get_attestation_deltas, process_rewards_and_penalties; rm get_inactivity_penalty
* update Validator and processVoluntaryExits to 0.6.1; rm obsolete inclusion_slots/inclusion_distances
* rm removed activate_validator; mark DepositData, misc values, Gwei values, Randao processing, state caching as 0.6.1; update GENESIS_SLOT to 64, since it doesn't quite yet work at 0
* mark BeaconBlockHeader as 0.6.1; update BeaconBlockBody to 0.6.1; rename WHISTLEBLOWER_REWARD_QUOTIENT to WHISTLEBLOWING_REWARD_QUOTIENT; update slash_validator to 0.6.1
* implement 0.6.2 is_slashable_validator; update processProposerSlashings to 0.6.2; mark state caching as 0.6.2
* mark get_total_active_balance and process_slashings as 0.6.2; update get_base_reward to 0.6.2
* rm prepare_validator_for_withdrawal, process_exit_queue; mark mainnet misc constants as 0.6.2; update process block header to 0.6.2
* address mratsim's code review comment
2019-05-29 10:08:56 +00:00
|
|
|
current_epoch
|
2019-06-14 13:50:47 +00:00
|
|
|
else:
|
More 0.8.0 updates (#311)
* replace BeaconState.finalized_{epoch,root} with BeaconState.finalized_checkpoint; rename get_delayed_activation_exit_epoch(...) to compute_activation_exit_epoch(...) and mark as 0.8.0; update get_churn_limit(...)/get_validator_churn_limit(...) to 0.8.0; update process_registry_updates(...) to 0.8.0
* update process_crosslinks(...) to 0.8.0; mark compute_start_slot_of_epoch(...) and get_committee_count(...) as 0.8.0
* mark Fork, is_slashable_validator(...), and get_beacon_proposer_index(...) as 0.8.0
* rename LATEST_SLASHED_EXIT_LENGTH to EPOCHS_PER_SLASHINGS_VECTOR; update process_slashings(...) to 0.8.0; remove pointless type conversion warning in get_previous_epoch(...)
* convert remaining references to finalized_epoch to finalized_checkpoint.epoch
* update slash_validator(...) to 0.8.0; mark inital value, Gwei, and time constants as 0.8.0; mark hash(...) and processBlockHeader(...) as 0.8.0
* rename WHISTLEBLOWING_REWARD_QUOTIENT to WHISTLEBLOWER_REWARD_QUOTIENT; rename LATEST_ACTIVE_INDEX_ROOTS_LENGTH to EPOCHS_PER_HISTORICAL_VECTOR (randao will also get merged into this); remove get_active_index_root(...); mark time parameter, signature domain types, and max operations per block constants as 0.8.0; update rewards and penalties constants to 0.8.0
* update is_valid_indexed_attestation(...) to 0.8.0; mark process_slot(...) as 0.8.0
* replace BeaconState.{current,previous}_justified_{epoch,root} with BeaconState.{current,previous}_justified_checkpoint
2019-07-05 08:30:05 +00:00
|
|
|
current_epoch - 1
|
2019-02-11 15:10:46 +00:00
|
|
|
|
2020-07-22 07:51:45 +00:00
|
|
|
func get_previous_epoch*(state: BeaconState): Epoch =
|
|
|
|
# Return the previous epoch (unless the current epoch is ``GENESIS_EPOCH``).
|
|
|
|
get_previous_epoch(get_current_epoch(state))
|
|
|
|
|
2020-07-27 16:49:46 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/beacon-chain.md#compute_committee
|
2020-07-28 13:54:32 +00:00
|
|
|
func compute_committee*(shuffled_indices: seq[ValidatorIndex],
|
2020-06-01 07:44:50 +00:00
|
|
|
index: uint64, count: uint64): seq[ValidatorIndex] =
|
2019-07-01 09:05:22 +00:00
|
|
|
## Return the committee corresponding to ``indices``, ``seed``, ``index``,
|
|
|
|
## and committee ``count``.
|
2020-07-27 16:04:44 +00:00
|
|
|
## In this version, we pass in the shuffled indices meaning we no longer need
|
|
|
|
## the seed.
|
2020-06-01 07:44:50 +00:00
|
|
|
|
2020-06-01 18:27:57 +00:00
|
|
|
let
|
2020-07-27 16:04:44 +00:00
|
|
|
active_validators = shuffled_indices.len.uint64
|
|
|
|
start = (active_validators * index) div count
|
|
|
|
endIdx = (active_validators * (index + 1)) div count
|
2020-04-22 05:53:02 +00:00
|
|
|
|
2020-06-01 18:27:57 +00:00
|
|
|
# These assertions from compute_shuffled_index(...)
|
2020-07-27 16:04:44 +00:00
|
|
|
doAssert endIdx <= active_validators
|
|
|
|
doAssert active_validators <= 2'u64^40
|
2020-04-22 05:53:02 +00:00
|
|
|
|
2020-06-01 18:27:57 +00:00
|
|
|
# In spec, this calls get_shuffled_index() every time, but that's wasteful
|
|
|
|
# Here, get_beacon_committee() gets the shuffled version.
|
|
|
|
try:
|
2020-07-27 16:04:44 +00:00
|
|
|
shuffled_indices[start.int .. (endIdx.int-1)]
|
2020-04-22 05:53:02 +00:00
|
|
|
except KeyError:
|
|
|
|
raiseAssert("Cached entries are added before use")
|
2019-05-23 11:13:02 +00:00
|
|
|
|
2020-07-27 16:04:44 +00:00
|
|
|
func compute_committee_len(active_validators: uint64,
|
|
|
|
index: uint64, count: uint64): uint64 =
|
|
|
|
## Return the committee corresponding to ``indices``, ``seed``, ``index``,
|
|
|
|
## and committee ``count``.
|
|
|
|
|
|
|
|
# indices only used here for its length, or for the shuffled version,
|
|
|
|
# so unlike spec, pass the shuffled version in directly.
|
|
|
|
let
|
|
|
|
start = (active_validators * index) div count
|
|
|
|
endIdx = (active_validators * (index + 1)) div count
|
|
|
|
|
|
|
|
# These assertions from compute_shuffled_index(...)
|
|
|
|
doAssert endIdx <= active_validators
|
|
|
|
doAssert active_validators <= 2'u64^40
|
|
|
|
|
|
|
|
# In spec, this calls get_shuffled_index() every time, but that's wasteful
|
|
|
|
# Here, get_beacon_committee() gets the shuffled version.
|
|
|
|
endIdx - start
|
|
|
|
|
2020-07-27 16:49:46 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/beacon-chain.md#get_beacon_committee
|
2020-04-15 09:01:36 +00:00
|
|
|
func get_beacon_committee*(
|
|
|
|
state: BeaconState, slot: Slot, index: CommitteeIndex,
|
|
|
|
cache: var StateCache): seq[ValidatorIndex] =
|
2019-11-07 21:13:27 +00:00
|
|
|
# Return the beacon committee at ``slot`` for ``index``.
|
|
|
|
let
|
|
|
|
epoch = compute_epoch_at_slot(slot)
|
2020-07-27 16:04:44 +00:00
|
|
|
committees_per_slot = get_committee_count_per_slot(state, epoch, cache)
|
|
|
|
compute_committee(
|
|
|
|
cache.get_shuffled_active_validator_indices(state, epoch),
|
|
|
|
(slot mod SLOTS_PER_EPOCH) * committees_per_slot +
|
|
|
|
index.uint64,
|
|
|
|
committees_per_slot * SLOTS_PER_EPOCH
|
|
|
|
)
|
|
|
|
|
|
|
|
func get_beacon_committee_len*(
|
|
|
|
state: BeaconState, slot: Slot, index: CommitteeIndex,
|
|
|
|
cache: var StateCache): uint64 =
|
|
|
|
# Return the number of members in the beacon committee at ``slot`` for ``index``.
|
|
|
|
let
|
|
|
|
epoch = compute_epoch_at_slot(slot)
|
|
|
|
committees_per_slot = get_committee_count_per_slot(state, epoch, cache)
|
2019-11-07 21:13:27 +00:00
|
|
|
|
2020-07-27 16:04:44 +00:00
|
|
|
compute_committee_len(
|
|
|
|
count_active_validators(state, epoch, cache),
|
|
|
|
(slot mod SLOTS_PER_EPOCH) * committees_per_slot +
|
|
|
|
index.uint64,
|
|
|
|
committees_per_slot * SLOTS_PER_EPOCH
|
|
|
|
)
|
2019-11-07 21:13:27 +00:00
|
|
|
|
2020-07-27 16:49:46 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/beacon-chain.md#compute_shuffled_index
|
2020-07-15 10:44:18 +00:00
|
|
|
func compute_shuffled_index(
|
|
|
|
index: uint64, index_count: uint64, seed: Eth2Digest): uint64 =
|
|
|
|
# Return the shuffled index corresponding to ``seed`` (and ``index_count``).
|
|
|
|
doAssert index < index_count
|
|
|
|
|
|
|
|
var
|
|
|
|
pivot_buffer: array[(32+1), byte]
|
|
|
|
source_buffer: array[(32+1+4), byte]
|
|
|
|
cur_idx_permuted = index
|
|
|
|
|
|
|
|
pivot_buffer[0..31] = seed.data
|
|
|
|
source_buffer[0..31] = seed.data
|
|
|
|
|
|
|
|
# Swap or not (https://link.springer.com/content/pdf/10.1007%2F978-3-642-32009-5_1.pdf)
|
|
|
|
# See the 'generalized domain' algorithm on page 3
|
2020-07-27 16:04:44 +00:00
|
|
|
for current_round in 0'u8 ..< SHUFFLE_ROUND_COUNT.uint8:
|
|
|
|
pivot_buffer[32] = current_round
|
|
|
|
source_buffer[32] = current_round
|
2020-07-15 10:44:18 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
# If using multiple indices, can amortize this
|
|
|
|
pivot =
|
2020-07-27 10:59:57 +00:00
|
|
|
bytes_to_uint64(eth2digest(pivot_buffer).data.toOpenArray(0, 7)) mod
|
2020-07-15 10:44:18 +00:00
|
|
|
index_count
|
|
|
|
|
|
|
|
flip = ((index_count + pivot) - cur_idx_permuted) mod index_count
|
|
|
|
position = max(cur_idx_permuted.int, flip.int)
|
2020-07-27 10:59:57 +00:00
|
|
|
source_buffer[33..36] = uint_to_bytes4((position div 256).uint64)
|
2020-07-15 10:44:18 +00:00
|
|
|
let
|
|
|
|
source = eth2digest(source_buffer).data
|
|
|
|
byte_value = source[(position mod 256) div 8]
|
|
|
|
bit = (byte_value shr (position mod 8)) mod 2
|
|
|
|
|
|
|
|
cur_idx_permuted = if bit != 0: flip else: cur_idx_permuted
|
|
|
|
|
|
|
|
cur_idx_permuted
|
0.6.2 updates (#275)
* update process_justification_and_finalization to 0.6.2; mark AttesterSlashing as 0.6.2
* replace get_effective_balance(...) with state.validator_registry[idx].effective_balance; rm get_effective_balance, process_ejections, should_update_validator_registry, update_validator_registry, and update_registry_and_shuffling_data; update get_total_balance to 0.6.2; implement process_registry_updates
* rm exit_validator; implement is_slashable_attestation_data; partly update processAttesterSlashings
* mark HistoricalBatch and Eth1Data as 0.6.2; implement get_shard_delta(...); replace 0.5 finish_epoch_update with 0.6 process_final_updates
* mark increase_balance, decrease_balance, get_delayed_activation_exit_epoch, bls_aggregate_pubkeys, bls_verify_multiple, Attestation, Transfer, slot_to_epoch, Crosslink, get_current_epoch, int_to_bytes*, various constants, processEth1Data, processTransfers, and verifyStateRoot as 0.6.2; rm is_double_vote and is_surround_vote
* mark get_bitfield_bit, verify_bitfield, ProposerSlashing, DepositData, VoluntaryExit, PendingAttestation, Fork, integer_squareroot, get_epoch_start_slot, is_active_validator, generate_seed, some constants to 0.6.2; rename MIN_PENALTY_QUOTIENT to MIN_SLASHING_PENALTY_QUOTIENT
* rm get_previous_total_balance, get_current_epoch_boundary_attestations, get_previous_epoch_boundary_attestations, and get_previous_epoch_matching_head_attestations
* update BeaconState to 0.6.2; simplify legacy get_crosslink_committees_at_slot infrastructure a bit by noting that registry_change is always false; reimplment 0.5 get_crosslink_committees_at_slot in terms of 0.6 get_crosslink_committee
* mark process_deposit(...), get_block_root_at_slot(...), get_block_root(...), Deposit, BeaconBlockHeader, BeaconBlockBody, hash(...), get_active_index_root(...), various constants, get_shard_delta(...), get_epoch_start_shard(...), get_crosslink_committee(...), processRandao(...), processVoluntaryExits(...), cacheState(...) as 0.6.2
* rm removed-since-0.5 split(...), is_power_of_2(...), get_shuffling(...); rm 0.5 versions of get_active_validator_indices and get_epoch_committee_count; add a few tests for integer_squareroot
* mark bytes_to_int(...) and advanceState(...) as 0.6.2
* rm 0.5 get_attesting_indices; update get_attesting_balance to 0.6.2
* another tiny commit to poke AppVeyor to maybe not timeout at connecting to GitHub partway through CI: mark get_churn_limit(...), initiate_validator_exit(...), and Validator as 0.6.2
* mark get_attestation_slot(...), AttestationDataAndCustodyBit, and BeaconBlock as 0.6.2
2019-06-03 10:31:04 +00:00
|
|
|
|
2020-07-27 16:49:46 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/beacon-chain.md#compute_proposer_index
|
2019-11-25 06:31:37 +00:00
|
|
|
func compute_proposer_index(state: BeaconState, indices: seq[ValidatorIndex],
|
2020-06-03 05:42:08 +00:00
|
|
|
seed: Eth2Digest): Option[ValidatorIndex] =
|
2019-11-10 00:03:41 +00:00
|
|
|
# Return from ``indices`` a random index sampled by effective balance.
|
|
|
|
const MAX_RANDOM_BYTE = 255
|
|
|
|
|
2019-12-04 10:49:59 +00:00
|
|
|
if len(indices) == 0:
|
|
|
|
return none(ValidatorIndex)
|
2019-05-23 11:13:02 +00:00
|
|
|
|
2020-07-26 18:55:48 +00:00
|
|
|
let seq_len = indices.lenu64
|
2019-11-06 15:50:12 +00:00
|
|
|
|
2019-05-23 11:13:02 +00:00
|
|
|
var
|
2020-07-15 10:44:18 +00:00
|
|
|
i = 0'u64
|
2019-11-10 00:03:41 +00:00
|
|
|
buffer: array[32+8, byte]
|
2019-05-23 11:13:02 +00:00
|
|
|
buffer[0..31] = seed.data
|
|
|
|
while true:
|
2020-07-27 10:59:57 +00:00
|
|
|
buffer[32..39] = uint_to_bytes8(i div 32)
|
2019-05-23 11:13:02 +00:00
|
|
|
let
|
2020-07-15 10:44:18 +00:00
|
|
|
candidate_index =
|
|
|
|
indices[compute_shuffled_index(i mod seq_len, seq_len, seed)]
|
2020-06-16 12:16:43 +00:00
|
|
|
random_byte = (eth2digest(buffer).data)[i mod 32]
|
2020-07-15 10:44:18 +00:00
|
|
|
effective_balance = state.validators[candidate_index].effective_balance
|
0.7.0 updates which don't change semantics; mostly comment changes (#281)
* 0.7.0 updates which don't change semantics; mostly comment changes
* mark get_attesting_indices, bls_verify, ProposerSlashing, BeaconBlockBody, deposit contract constants, state list length constants, compute_committee, get_crosslink_committee, is_slashable_attestation_data, processAttesterSlashings as 0.7.0; rename BASE_REWARD_QUOTIENT to BASE_REWARD_FACTOR
* complete marking unchanged-in-0.7.0 datatypes as such; a few more have trivial field renamings, etc
2019-06-13 07:40:58 +00:00
|
|
|
if effective_balance * MAX_RANDOM_BYTE >=
|
|
|
|
MAX_EFFECTIVE_BALANCE * random_byte:
|
2019-12-04 10:49:59 +00:00
|
|
|
return some(candidate_index)
|
2019-05-23 11:13:02 +00:00
|
|
|
i += 1
|
2019-11-10 00:03:41 +00:00
|
|
|
|
2020-07-27 16:49:46 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/beacon-chain.md#get_beacon_proposer_index
|
2020-06-03 05:42:08 +00:00
|
|
|
func get_beacon_proposer_index*(state: BeaconState, cache: var StateCache, slot: Slot):
|
2019-12-04 10:49:59 +00:00
|
|
|
Option[ValidatorIndex] =
|
2020-07-27 16:04:44 +00:00
|
|
|
cache.beacon_proposer_indices.withValue(slot, proposer) do:
|
|
|
|
return proposer[]
|
|
|
|
do:
|
2020-06-04 12:03:16 +00:00
|
|
|
|
2020-07-27 16:04:44 +00:00
|
|
|
# Return the beacon proposer index at the current slot.
|
|
|
|
let epoch = get_current_epoch(state)
|
2019-11-10 00:03:41 +00:00
|
|
|
|
2020-07-27 16:04:44 +00:00
|
|
|
var buffer: array[32 + 8, byte]
|
|
|
|
buffer[0..31] = get_seed(state, epoch, DOMAIN_BEACON_PROPOSER).data
|
|
|
|
buffer[32..39] = uint_to_bytes8(slot.uint64)
|
2019-11-10 00:03:41 +00:00
|
|
|
|
2020-07-27 16:04:44 +00:00
|
|
|
# There's exactly one beacon proposer per slot.
|
2020-06-03 05:42:08 +00:00
|
|
|
|
|
|
|
let
|
2020-06-16 12:16:43 +00:00
|
|
|
seed = eth2digest(buffer)
|
2020-06-03 05:42:08 +00:00
|
|
|
indices =
|
2020-07-27 16:04:44 +00:00
|
|
|
sorted(cache.get_shuffled_active_validator_indices(state, epoch), system.cmp)
|
2019-11-10 00:03:41 +00:00
|
|
|
|
2020-07-27 16:04:44 +00:00
|
|
|
return cache.beacon_proposer_indices.mgetOrPut(
|
|
|
|
slot, compute_proposer_index(state, indices, seed))
|
2020-02-20 22:22:59 +00:00
|
|
|
|
2020-07-27 16:49:46 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/beacon-chain.md#get_beacon_proposer_index
|
2020-06-04 12:03:16 +00:00
|
|
|
func get_beacon_proposer_index*(state: BeaconState, cache: var StateCache):
|
2020-05-22 17:04:52 +00:00
|
|
|
Option[ValidatorIndex] =
|
2020-06-04 12:03:16 +00:00
|
|
|
get_beacon_proposer_index(state, cache, state.slot)
|
2020-05-22 17:04:52 +00:00
|
|
|
|
2020-07-29 12:47:03 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/validator.md#validator-assignments
|
2020-05-27 17:06:28 +00:00
|
|
|
func get_committee_assignment*(
|
2020-07-10 09:24:04 +00:00
|
|
|
state: BeaconState, epoch: Epoch,
|
2020-07-22 08:04:21 +00:00
|
|
|
validator_index: ValidatorIndex):
|
|
|
|
Option[tuple[a: seq[ValidatorIndex], b: CommitteeIndex, c: Slot]] =
|
2020-02-20 22:22:59 +00:00
|
|
|
# Return the committee assignment in the ``epoch`` for ``validator_index``.
|
|
|
|
# ``assignment`` returned is a tuple of the following form:
|
|
|
|
# * ``assignment[0]`` is the list of validators in the committee
|
|
|
|
# * ``assignment[1]`` is the index to which the committee is assigned
|
|
|
|
# * ``assignment[2]`` is the slot at which the committee is assigned
|
|
|
|
# Return None if no assignment.
|
|
|
|
let next_epoch = get_current_epoch(state) + 1
|
|
|
|
doAssert epoch <= next_epoch
|
|
|
|
|
2020-07-15 10:44:18 +00:00
|
|
|
var cache = StateCache()
|
2020-02-20 22:22:59 +00:00
|
|
|
|
2020-07-27 10:59:57 +00:00
|
|
|
let
|
|
|
|
start_slot = compute_start_slot_at_epoch(epoch)
|
|
|
|
committee_count_per_slot =
|
|
|
|
get_committee_count_per_slot(state, epoch, cache)
|
2020-02-20 22:22:59 +00:00
|
|
|
for slot in start_slot ..< start_slot + SLOTS_PER_EPOCH:
|
2020-07-27 10:59:57 +00:00
|
|
|
for index in 0'u64 ..< committee_count_per_slot:
|
2020-04-15 09:01:36 +00:00
|
|
|
let idx = index.CommitteeIndex
|
2020-07-10 09:24:04 +00:00
|
|
|
let committee = get_beacon_committee(state, slot, idx, cache)
|
2020-07-22 08:04:21 +00:00
|
|
|
if validator_index in committee:
|
2020-04-15 09:01:36 +00:00
|
|
|
return some((committee, idx, slot))
|
|
|
|
none(tuple[a: seq[ValidatorIndex], b: CommitteeIndex, c: Slot])
|