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
|
2019-03-26 12:58:34 +00:00
|
|
|
options, nimcrypto, sequtils, math, chronicles,
|
2019-02-05 19:21:18 +00:00
|
|
|
eth/common,
|
2019-04-05 14:18:13 +00:00
|
|
|
../ssz, ../beacon_node_types,
|
2018-11-28 19:49:03 +00:00
|
|
|
./crypto, ./datatypes, ./digest, ./helpers
|
2018-11-23 19:42:47 +00:00
|
|
|
|
2019-05-10 08:14:01 +00:00
|
|
|
# TODO: Proceed to renaming and signature changes
|
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
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.0/specs/core/0_beacon-chain.md#get_shuffled_index
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.0/specs/core/0_beacon-chain.md#compute_committee
|
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-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
|
|
|
|
|
|
|
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.
|
2019-05-10 08:14:01 +00:00
|
|
|
let pivot = bytes_to_int(eth2hash(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:
|
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-05-23 11:13:02 +00:00
|
|
|
func get_shuffled_index(index: ValidatorIndex, index_count: uint64, seed: Eth2Digest): uint64 =
|
|
|
|
## Return the shuffled validator index corresponding to ``seed`` (and
|
|
|
|
## ``index_count``).
|
|
|
|
## https://github.com/status-im/nim-beacon-chain/blob/f77016af6818ad2c853f6c9e2751b17548e0222e/beacon_chain/spec/validator.nim#L15
|
|
|
|
|
|
|
|
doAssert index.uint64 < index_count
|
|
|
|
doAssert index_count <= 2'u64^40
|
|
|
|
|
|
|
|
result = index
|
|
|
|
var pivot_buffer: array[(32+1), byte]
|
|
|
|
var source_buffer: array[(32+1+4), byte]
|
|
|
|
|
|
|
|
# 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
|
|
|
|
for round in 0 ..< SHUFFLE_ROUND_COUNT:
|
|
|
|
pivot_buffer[0..31] = seed.data
|
|
|
|
let round_bytes1 = int_to_bytes1(round)[0]
|
|
|
|
pivot_buffer[32] = round_bytes1
|
|
|
|
|
|
|
|
let
|
|
|
|
pivot = bytes_to_int(eth2hash(pivot_buffer).data[0..7]) mod index_count
|
|
|
|
flip = (pivot - index) mod index_count
|
|
|
|
position = max(index.uint64, flip)
|
|
|
|
|
|
|
|
## Tradeoff between slicing (if reusing one larger buffer) and additional
|
|
|
|
## copies here of seed and `int_to_bytes1(round)`.
|
|
|
|
source_buffer[0..31] = seed.data
|
|
|
|
source_buffer[32] = round_bytes1
|
|
|
|
source_buffer[33..36] = int_to_bytes4(position div 256)
|
|
|
|
|
|
|
|
let
|
|
|
|
source = eth2hash(source_buffer).data
|
|
|
|
byte_value = source[(position mod 256) div 8]
|
|
|
|
bit = (byte_value shr (position mod 8)) mod 2
|
|
|
|
|
|
|
|
if bit != 0:
|
|
|
|
result = flip
|
|
|
|
|
2019-06-14 13:50:47 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.0/specs/core/0_beacon-chain.md#get_previous_epoch
|
2019-03-08 18:23:42 +00:00
|
|
|
func get_previous_epoch*(state: BeaconState): Epoch =
|
2019-02-11 15:10:46 +00:00
|
|
|
## Return the previous epoch of the given ``state``.
|
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
|
|
|
## Return the current epoch if it's genesis epoch.
|
|
|
|
let current_epoch = get_current_epoch(state)
|
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:
|
|
|
|
current_epoch - 1
|
2019-02-11 15:10:46 +00:00
|
|
|
|
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
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.0/specs/core/0_beacon-chain.md#get_shard_delta
|
|
|
|
func get_shard_delta*(state: BeaconState, epoch: Epoch): uint64 =
|
2019-05-23 11:13:02 +00:00
|
|
|
## Return the number of shards to increment ``state.latest_start_shard``
|
|
|
|
## during ``epoch``.
|
|
|
|
min(get_epoch_committee_count(state, epoch),
|
|
|
|
(SHARD_COUNT - SHARD_COUNT div SLOTS_PER_EPOCH).uint64)
|
2019-03-16 19:52:37 +00:00
|
|
|
|
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
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.0/specs/core/0_beacon-chain.md#get_epoch_start_shard
|
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
|
|
|
func get_epoch_start_shard*(state: BeaconState, epoch: Epoch): Shard =
|
2019-05-23 11:13:02 +00:00
|
|
|
doAssert epoch <= get_current_epoch(state) + 1
|
|
|
|
var
|
|
|
|
check_epoch = get_current_epoch(state) + 1
|
|
|
|
shard =
|
|
|
|
(state.latest_start_shard +
|
|
|
|
get_shard_delta(state, get_current_epoch(state))) mod SHARD_COUNT
|
|
|
|
while check_epoch > epoch:
|
|
|
|
check_epoch -= 1
|
|
|
|
shard = (shard + SHARD_COUNT - get_shard_delta(state, check_epoch)) mod
|
|
|
|
SHARD_COUNT
|
|
|
|
return shard
|
2019-04-29 16:48:30 +00:00
|
|
|
|
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
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.0/specs/core/0_beacon-chain.md#compute_committee
|
2019-05-23 11:13:02 +00:00
|
|
|
func compute_committee(indices: seq[ValidatorIndex], seed: Eth2Digest,
|
|
|
|
index: uint64, count: uint64): seq[ValidatorIndex] =
|
|
|
|
let
|
|
|
|
start = (len(indices).uint64 * index) div count
|
|
|
|
endIdx = (len(indices).uint64 * (index + 1)) div count
|
|
|
|
doAssert endIdx.int - start.int > 0
|
|
|
|
mapIt(
|
|
|
|
start.int .. (endIdx.int-1),
|
|
|
|
indices[
|
|
|
|
get_shuffled_index(it.ValidatorIndex, len(indices).uint64, seed).int])
|
|
|
|
|
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
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.0/specs/core/0_beacon-chain.md#get_crosslink_committee
|
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
|
|
|
func get_crosslink_committee*(state: BeaconState, epoch: Epoch, shard: Shard):
|
2019-05-23 11:13:02 +00:00
|
|
|
seq[ValidatorIndex] =
|
|
|
|
compute_committee(
|
|
|
|
get_active_validator_indices(state, epoch),
|
|
|
|
generate_seed(state, epoch),
|
|
|
|
(shard + SHARD_COUNT - get_epoch_start_shard(state, epoch)) mod SHARD_COUNT,
|
|
|
|
get_epoch_committee_count(state, epoch),
|
|
|
|
)
|
|
|
|
|
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
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.5.0/specs/core/0_beacon-chain.md#get_crosslink_committees_at_slot
|
|
|
|
func get_crosslink_committees_at_slot*(state: BeaconState, slot: Slot|uint64):
|
|
|
|
seq[CrosslinkCommittee] =
|
|
|
|
## Returns the list of ``(committee, shard)`` tuples for the ``slot``.
|
|
|
|
|
|
|
|
let
|
|
|
|
epoch = slot_to_epoch(slot) # TODO, enforce slot to be a Slot
|
|
|
|
current_epoch = get_current_epoch(state)
|
|
|
|
previous_epoch = get_previous_epoch(state)
|
|
|
|
next_epoch = current_epoch + 1
|
|
|
|
|
|
|
|
doAssert previous_epoch <= epoch,
|
|
|
|
"Previous epoch: " & $humaneEpochNum(previous_epoch) &
|
|
|
|
", epoch: " & $humaneEpochNum(epoch) &
|
|
|
|
" (slot: " & $humaneSlotNum(slot.Slot) & ")" &
|
|
|
|
", Next epoch: " & $humaneEpochNum(next_epoch)
|
|
|
|
|
|
|
|
doAssert epoch <= next_epoch,
|
|
|
|
"Previous epoch: " & $humaneEpochNum(previous_epoch) &
|
|
|
|
", epoch: " & $humaneEpochNum(epoch) &
|
|
|
|
" (slot: " & $humaneSlotNum(slot.Slot) & ")" &
|
|
|
|
", Next epoch: " & $humaneEpochNum(next_epoch)
|
|
|
|
|
|
|
|
for i in 0'u64 ..< get_epoch_committee_count(state, epoch):
|
|
|
|
let shard = i mod SHARD_COUNT
|
|
|
|
result.add (
|
|
|
|
get_crosslink_committee(state, epoch, shard),
|
|
|
|
shard
|
|
|
|
)
|
|
|
|
|
|
|
|
iterator get_crosslink_committees_at_slot_cached*(
|
|
|
|
state: BeaconState, slot: Slot|uint64, cache: var StateCache):
|
|
|
|
CrosslinkCommittee =
|
|
|
|
let key = (slot_to_epoch(slot).uint64, false)
|
|
|
|
if key in cache.crosslink_committee_cache:
|
|
|
|
for v in cache.crosslink_committee_cache[key]: yield v
|
|
|
|
#debugEcho "get_crosslink_committees_at_slot_cached: MISS"
|
|
|
|
let result = get_crosslink_committees_at_slot(state, slot)
|
|
|
|
cache.crosslink_committee_cache[key] = result
|
|
|
|
for v in result: yield v
|
|
|
|
|
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
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.0/specs/core/0_beacon-chain.md#get_beacon_proposer_index
|
2019-04-29 16:48:30 +00:00
|
|
|
func get_beacon_proposer_index*(state: BeaconState): ValidatorIndex =
|
2019-05-23 11:13:02 +00:00
|
|
|
# Return the current beacon proposer index.
|
|
|
|
const
|
|
|
|
MAX_RANDOM_BYTE = 255
|
|
|
|
|
|
|
|
let
|
|
|
|
epoch = get_current_epoch(state)
|
|
|
|
committees_per_slot =
|
|
|
|
get_epoch_committee_count(state, epoch) div SLOTS_PER_EPOCH
|
|
|
|
offset = committees_per_slot * (state.slot mod SLOTS_PER_EPOCH)
|
|
|
|
shard = (get_epoch_start_shard(state, epoch) + offset) mod SHARD_COUNT
|
|
|
|
first_committee = get_crosslink_committee(state, epoch, shard)
|
|
|
|
seed = generate_seed(state, epoch)
|
|
|
|
|
|
|
|
var
|
|
|
|
i = 0
|
|
|
|
buffer: array[(32+8), byte]
|
|
|
|
buffer[0..31] = seed.data
|
|
|
|
while true:
|
|
|
|
buffer[32..39] = int_to_bytes8(i.uint64 div 32)
|
|
|
|
let
|
|
|
|
candidate_index = first_committee[((epoch + i.uint64) mod
|
|
|
|
len(first_committee).uint64).int]
|
|
|
|
random_byte = (eth2hash(buffer).data)[i mod 32]
|
|
|
|
effective_balance =
|
|
|
|
state.validator_registry[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-05-23 11:13:02 +00:00
|
|
|
return candidate_index
|
|
|
|
i += 1
|