2018-07-23 12:58:41 +00:00
|
|
|
# beacon_chain
|
|
|
|
# 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.
|
|
|
|
|
2018-11-28 19:49:03 +00:00
|
|
|
# Uncategorized helper functions from the spec
|
|
|
|
|
|
|
|
import ./datatypes, ./digest, sequtils, math
|
2018-07-23 12:58:41 +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#integer_squareroot
|
2018-12-19 04:36:10 +00:00
|
|
|
func integer_squareroot*(n: SomeInteger): SomeInteger =
|
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
|
|
|
## The largest integer ``x`` such that ``x**2`` is less than or equal to
|
|
|
|
## ``n``.
|
2019-03-16 19:52:37 +00:00
|
|
|
doAssert n >= 0'u64
|
|
|
|
|
2018-12-03 21:41:24 +00:00
|
|
|
var
|
|
|
|
x = n
|
|
|
|
y = (x + 1) div 2
|
|
|
|
while y < x:
|
|
|
|
x = y
|
|
|
|
y = (x + n div x) div 2
|
|
|
|
x
|
|
|
|
|
2019-04-05 14:18:13 +00:00
|
|
|
# TODO reuse as necessary/useful for merkle proof building
|
2018-12-13 16:00:55 +00:00
|
|
|
func merkle_root*(values: openArray[Eth2Digest]): Eth2Digest =
|
2019-02-07 19:27:13 +00:00
|
|
|
## Merkleize ``values`` (where ``len(values)`` is a power of two) and return
|
|
|
|
## the Merkle root.
|
2019-02-22 09:56:45 +00:00
|
|
|
## https://crypto.stackexchange.com/questions/43430/what-is-the-reason-to-separate-domains-in-the-internal-hash-algorithm-of-a-merkl
|
2019-02-20 20:35:27 +00:00
|
|
|
let num_values = len(values)
|
|
|
|
|
|
|
|
# Simplifies boundary conditions
|
2019-02-22 09:56:45 +00:00
|
|
|
doAssert is_power_of_two(num_values)
|
|
|
|
doAssert num_values >= 2
|
|
|
|
doAssert num_values mod 2 == 0
|
2019-02-20 20:35:27 +00:00
|
|
|
|
|
|
|
# TODO reverse ``o`` order and use newSeqWith to avoid pointless zero-filling.
|
|
|
|
var o = repeat(ZERO_HASH, len(values))
|
|
|
|
var hash_buffer: array[2*32, byte]
|
|
|
|
|
|
|
|
# These ``o`` indices get filled from ``values``.
|
|
|
|
let highest_internally_filled_index = (num_values div 2) - 1
|
2019-03-13 23:04:43 +00:00
|
|
|
doAssert (highest_internally_filled_index + 1) * 2 >= num_values
|
2019-02-20 20:35:27 +00:00
|
|
|
|
|
|
|
for i in countdown(num_values-1, highest_internally_filled_index + 1):
|
|
|
|
hash_buffer[0..31] = values[i*2 - num_values].data
|
|
|
|
hash_buffer[32..63] = values[i*2+1 - num_values].data
|
|
|
|
o[i] = eth2hash(hash_buffer)
|
|
|
|
|
|
|
|
## These ``o`` indices get filled from other ``o`` indices.
|
2019-03-13 23:04:43 +00:00
|
|
|
doAssert highest_internally_filled_index * 2 + 1 < num_values
|
2019-02-20 20:35:27 +00:00
|
|
|
|
|
|
|
for i in countdown(highest_internally_filled_index, 1):
|
|
|
|
hash_buffer[0..31] = o[i*2].data
|
|
|
|
hash_buffer[32..63] = o[i*2+1].data
|
|
|
|
o[i] = eth2hash(hash_buffer)
|
|
|
|
|
|
|
|
o[1]
|
2018-12-19 04:36:10 +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#slot_to_epoch
|
2019-03-12 22:21:32 +00:00
|
|
|
func slot_to_epoch*(slot: Slot|uint64): 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
|
|
|
# Return the epoch number of the given ``slot``.
|
2019-03-12 22:21:32 +00:00
|
|
|
(slot div SLOTS_PER_EPOCH).Epoch
|
2019-01-29 03:22:22 +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_slot
|
2019-02-20 01:33:58 +00:00
|
|
|
func get_epoch_start_slot*(epoch: Epoch): Slot =
|
2019-02-15 16:33:32 +00:00
|
|
|
# Return the starting slot of the given ``epoch``.
|
2019-03-12 22:21:32 +00:00
|
|
|
(epoch * SLOTS_PER_EPOCH).Slot
|
2019-02-15 16:33:32 +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#is_active_validator
|
2019-02-20 01:33:58 +00:00
|
|
|
func is_active_validator*(validator: Validator, epoch: Epoch): bool =
|
2019-03-12 09:59:43 +00:00
|
|
|
### Check if ``validator`` is active
|
2019-01-29 03:22:22 +00:00
|
|
|
validator.activation_epoch <= epoch and epoch < validator.exit_epoch
|
2019-01-16 11:07:41 +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_active_validator_indices
|
2019-05-09 12:27:37 +00:00
|
|
|
func get_active_validator_indices*(state: BeaconState, epoch: Epoch):
|
|
|
|
seq[ValidatorIndex] =
|
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
|
|
|
# Get active validator indices at ``epoch``.
|
2019-05-09 12:27:37 +00:00
|
|
|
for idx, val in state.validator_registry:
|
|
|
|
if is_active_validator(val, epoch):
|
|
|
|
result.add idx.ValidatorIndex
|
|
|
|
|
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_committee_count
|
2019-05-09 12:27:37 +00:00
|
|
|
func get_epoch_committee_count*(state: BeaconState, epoch: Epoch): uint64 =
|
|
|
|
# Return the number of committees at ``epoch``.
|
|
|
|
let active_validator_indices = get_active_validator_indices(state, epoch)
|
|
|
|
clamp(
|
|
|
|
len(active_validator_indices) div SLOTS_PER_EPOCH div TARGET_COMMITTEE_SIZE,
|
|
|
|
1, SHARD_COUNT div SLOTS_PER_EPOCH).uint64 * SLOTS_PER_EPOCH
|
2019-01-29 03:22:22 +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_current_epoch
|
2019-02-20 01:33:58 +00:00
|
|
|
func get_current_epoch*(state: BeaconState): Epoch =
|
2019-02-11 15:10:46 +00:00
|
|
|
# Return the current epoch of the given ``state``.
|
2019-03-09 04:23:14 +00:00
|
|
|
doAssert state.slot >= GENESIS_SLOT, $state.slot
|
2019-01-29 03:22:22 +00:00
|
|
|
slot_to_epoch(state.slot)
|
|
|
|
|
2019-06-14 07:50:14 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.0/specs/core/0_beacon-chain.md#get_randao_mix
|
2019-01-29 03:22:22 +00:00
|
|
|
func get_randao_mix*(state: BeaconState,
|
2019-02-20 01:33:58 +00:00
|
|
|
epoch: Epoch): Eth2Digest =
|
2019-01-29 03:22:22 +00:00
|
|
|
## Returns the randao mix at a recent ``epoch``.
|
2019-06-12 07:48:49 +00:00
|
|
|
## ``epoch`` expected to be between (current_epoch -
|
|
|
|
## LATEST_RANDAO_MIXES_LENGTH, current_epoch].
|
2019-01-29 03:22:22 +00:00
|
|
|
state.latest_randao_mixes[epoch mod LATEST_RANDAO_MIXES_LENGTH]
|
|
|
|
|
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_active_index_root
|
2019-02-20 01:33:58 +00:00
|
|
|
func get_active_index_root(state: BeaconState, epoch: Epoch): Eth2Digest =
|
2019-01-29 03:22:22 +00:00
|
|
|
# Returns the index root at a recent ``epoch``.
|
2019-05-23 11:13:02 +00:00
|
|
|
## ``epoch`` expected to be between
|
|
|
|
## (current_epoch - LATEST_ACTIVE_INDEX_ROOTS_LENGTH + ACTIVATION_EXIT_DELAY, current_epoch + ACTIVATION_EXIT_DELAY].
|
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
|
|
|
## TODO maybe assert this, but omission of such seems conspicuously
|
|
|
|
## intentional
|
2019-02-19 23:07:56 +00:00
|
|
|
state.latest_active_index_roots[epoch mod LATEST_ACTIVE_INDEX_ROOTS_LENGTH]
|
2019-01-29 03:22:22 +00:00
|
|
|
|
2019-06-14 07:50:14 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.0/specs/core/0_beacon-chain.md#bytes_to_int
|
2019-03-18 15:42:42 +00:00
|
|
|
func bytes_to_int*(data: openarray[byte]): uint64 =
|
2019-03-13 23:04:43 +00:00
|
|
|
doAssert data.len == 8
|
2019-02-13 10:26:32 +00:00
|
|
|
|
|
|
|
# Little-endian data representation
|
|
|
|
result = 0
|
|
|
|
for i in countdown(7, 0):
|
|
|
|
result = result * 256 + data[i]
|
|
|
|
|
2019-06-05 10:25:30 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.6.3/specs/core/0_beacon-chain.md#int_to_bytes1-int_to_bytes2-
|
2019-03-18 15:42:42 +00:00
|
|
|
# Have 1, 4, 8, and 32-byte versions. 1+ more and maybe worth metaprogramming.
|
2019-03-13 21:23:01 +00:00
|
|
|
func int_to_bytes32*(x: uint64): array[32, byte] =
|
2019-03-13 01:46:44 +00:00
|
|
|
## Little-endian data representation
|
|
|
|
## TODO remove uint64 when those callers fade away
|
2019-02-13 10:26:32 +00:00
|
|
|
for i in 0 ..< 8:
|
2019-03-13 21:23:01 +00:00
|
|
|
result[24 + i] = byte((x shr i*8) and 0xff)
|
|
|
|
|
|
|
|
func int_to_bytes32*(x: Epoch): array[32, byte] {.borrow.}
|
2019-02-13 10:26:32 +00:00
|
|
|
|
2019-03-18 15:42:42 +00:00
|
|
|
func int_to_bytes8*(x: uint64): array[8, byte] =
|
|
|
|
for i in 0 ..< 8:
|
|
|
|
result[i] = byte((x shr i*8) and 0xff)
|
|
|
|
|
2019-02-13 10:26:32 +00:00
|
|
|
func int_to_bytes1*(x: int): array[1, byte] =
|
2019-03-13 23:04:43 +00:00
|
|
|
doAssert x >= 0
|
|
|
|
doAssert x < 256
|
2019-02-13 10:26:32 +00:00
|
|
|
|
|
|
|
result[0] = x.byte
|
|
|
|
|
|
|
|
func int_to_bytes4*(x: uint64): array[4, byte] =
|
2019-03-13 23:04:43 +00:00
|
|
|
doAssert x >= 0'u64
|
|
|
|
doAssert x < 2'u64^32
|
2019-02-13 10:26:32 +00:00
|
|
|
|
|
|
|
# Little-endian data representation
|
|
|
|
result[0] = ((x shr 0) and 0xff).byte
|
|
|
|
result[1] = ((x shr 8) and 0xff).byte
|
|
|
|
result[2] = ((x shr 16) and 0xff).byte
|
|
|
|
result[3] = ((x shr 24) and 0xff).byte
|
|
|
|
|
2019-06-12 07:48:49 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.6.3/specs/core/0_beacon-chain.md#get_domain
|
2019-03-18 15:42:42 +00:00
|
|
|
func get_domain*(
|
2019-04-29 16:48:30 +00:00
|
|
|
state: BeaconState, domain_type: SignatureDomain, message_epoch: Epoch): uint64 =
|
|
|
|
## Return the signature domain (fork version concatenated with domain type)
|
|
|
|
## of a message.
|
2019-03-18 15:42:42 +00:00
|
|
|
var buf: array[8, byte]
|
2019-04-29 16:48:30 +00:00
|
|
|
let
|
|
|
|
epoch = message_epoch
|
|
|
|
fork_version = if epoch < state.fork.epoch:
|
|
|
|
state.fork.previous_version
|
|
|
|
else:
|
|
|
|
state.fork.current_version
|
|
|
|
buf[0..3] = fork_version
|
2019-03-18 15:42:42 +00:00
|
|
|
buf[4..7] = int_to_bytes4(domain_type.uint64)
|
|
|
|
bytes_to_int(buf)
|
|
|
|
|
2019-04-29 16:48:30 +00:00
|
|
|
func get_domain*(state: BeaconState, domain_type: SignatureDomain): uint64 =
|
|
|
|
get_domain(state, domain_type, get_current_epoch(state))
|
|
|
|
|
2019-06-05 10:25:30 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.6.3/specs/core/0_beacon-chain.md#generate_seed
|
2019-02-20 01:33:58 +00:00
|
|
|
func generate_seed*(state: BeaconState, epoch: Epoch): Eth2Digest =
|
2019-01-29 03:22:22 +00:00
|
|
|
# Generate a seed for the given ``epoch``.
|
|
|
|
|
2019-02-13 10:26:32 +00:00
|
|
|
var seed_input : array[32*3, byte]
|
2019-04-05 14:18:13 +00:00
|
|
|
|
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
|
|
|
# Detect potential underflow
|
|
|
|
doAssert LATEST_RANDAO_MIXES_LENGTH >= MIN_SEED_LOOKAHEAD
|
|
|
|
|
2019-05-23 11:13:02 +00:00
|
|
|
seed_input[0..31] =
|
|
|
|
get_randao_mix(state,
|
|
|
|
epoch + LATEST_RANDAO_MIXES_LENGTH - MIN_SEED_LOOKAHEAD).data
|
2019-01-29 03:22:22 +00:00
|
|
|
seed_input[32..63] = get_active_index_root(state, epoch).data
|
2019-02-13 10:26:32 +00:00
|
|
|
seed_input[64..95] = int_to_bytes32(epoch)
|
2019-01-29 03:22:22 +00:00
|
|
|
eth2hash(seed_input)
|