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
|
|
|
|
2018-12-21 23:47:55 +00:00
|
|
|
# TODO spec candidate? there's bits in nim-ranges but that one has some API
|
|
|
|
# issues regarding bit endianess that need resolving..
|
|
|
|
func bitSet*(bitfield: var openArray[byte], index: int) =
|
|
|
|
bitfield[index div 8] = bitfield[index div 8] or 1'u8 shl (7 - (index mod 8))
|
|
|
|
|
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_bitfield_bit
|
2019-02-05 18:40:27 +00:00
|
|
|
func get_bitfield_bit*(bitfield: openarray[byte], i: int): byte =
|
|
|
|
# Extract the bit in ``bitfield`` at position ``i``.
|
2019-03-12 09:59:43 +00:00
|
|
|
doAssert 0 <= i div 8, "i: " & $i & " i div 8: " & $(i div 8)
|
|
|
|
doAssert i div 8 < bitfield.len, "i: " & $i & " i div 8: " & $(i div 8)
|
2019-02-05 18:40:27 +00:00
|
|
|
(bitfield[i div 8] shr (7 - (i mod 8))) mod 2
|
|
|
|
|
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#verify_bitfield
|
2019-02-05 18:40:27 +00:00
|
|
|
func verify_bitfield*(bitfield: openarray[byte], committee_size: int): bool =
|
|
|
|
# Verify ``bitfield`` against the ``committee_size``.
|
|
|
|
if len(bitfield) != (committee_size + 7) div 8:
|
|
|
|
return false
|
|
|
|
|
2019-02-14 03:16:51 +00:00
|
|
|
# Check `bitfield` is padded with zero bits only
|
|
|
|
for i in committee_size ..< (len(bitfield) * 8):
|
2019-02-05 18:40:27 +00:00
|
|
|
if get_bitfield_bit(bitfield, i) == 0b1:
|
|
|
|
return false
|
|
|
|
|
|
|
|
true
|
|
|
|
|
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#split
|
2018-11-23 19:42:47 +00:00
|
|
|
func split*[T](lst: openArray[T], N: Positive): seq[seq[T]] =
|
2018-11-23 22:44:43 +00:00
|
|
|
## split lst in N pieces, with each piece having `len(lst) div N` or
|
|
|
|
## `len(lst) div N + 1` pieces
|
2018-08-17 16:21:25 +00:00
|
|
|
# TODO: implement as an iterator
|
|
|
|
result = newSeq[seq[T]](N)
|
|
|
|
for i in 0 ..< N:
|
|
|
|
result[i] = lst[lst.len * i div N ..< lst.len * (i+1) div N] # TODO: avoid alloc via toOpenArray
|
|
|
|
|
2018-12-11 21:53:18 +00:00
|
|
|
func get_new_recent_block_roots*(old_block_roots: seq[Eth2Digest],
|
2018-11-23 19:42:47 +00:00
|
|
|
parent_slot, current_slot: int64,
|
2018-11-27 23:10:09 +00:00
|
|
|
parent_hash: Eth2Digest
|
|
|
|
): seq[Eth2Digest] =
|
2018-08-21 16:21:45 +00:00
|
|
|
|
|
|
|
# Should throw for `current_slot - CYCLE_LENGTH * 2 - 1` according to spec comment
|
|
|
|
let d = current_slot - parent_slot
|
2018-12-11 21:53:18 +00:00
|
|
|
result = old_block_roots[d .. ^1]
|
|
|
|
for _ in 0 ..< min(d, old_block_roots.len):
|
2018-08-21 16:21:45 +00:00
|
|
|
result.add parent_hash
|
|
|
|
|
2019-02-13 10:26:32 +00:00
|
|
|
func ceil_div8*(v: int): int = (v + 7) div 8 # TODO use a proper bitarray!
|
|
|
|
|
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#integer_squareroot
|
2018-12-19 04:36:10 +00:00
|
|
|
func integer_squareroot*(n: SomeInteger): SomeInteger =
|
|
|
|
## The largest integer ``x`` such that ``x**2`` is less than ``n``.
|
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-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_fork_version
|
2019-02-20 01:33:58 +00:00
|
|
|
func get_fork_version*(fork: Fork, epoch: Epoch): uint64 =
|
2019-02-07 23:07:15 +00:00
|
|
|
## Return the fork version of the given ``epoch``.
|
2019-01-30 08:41:07 +00:00
|
|
|
if epoch < fork.epoch:
|
|
|
|
fork.previous_version
|
|
|
|
else:
|
|
|
|
fork.current_version
|
2018-12-03 21:41:24 +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_domain
|
2018-12-11 17:55:45 +00:00
|
|
|
func get_domain*(
|
2019-02-20 01:33:58 +00:00
|
|
|
fork: Fork, epoch: Epoch, domain_type: SignatureDomain): uint64 =
|
2019-02-07 10:43:21 +00:00
|
|
|
# Get the domain number that represents the fork meta and signature domain.
|
|
|
|
(get_fork_version(fork, epoch) shl 32) + domain_type.uint32
|
2018-12-04 18:45:30 +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#is_power_of_two
|
2018-12-05 13:07:42 +00:00
|
|
|
func is_power_of_2*(v: uint64): bool = (v and (v-1)) == 0
|
2018-12-11 17:55:45 +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#merkle_root
|
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
|
|
|
|
assert (highest_internally_filled_index + 1) * 2 >= num_values
|
|
|
|
|
|
|
|
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.
|
|
|
|
assert highest_internally_filled_index * 2 + 1 < num_values
|
|
|
|
|
|
|
|
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
|
|
|
|
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#slot_to_epoch
|
2019-03-12 22:21:32 +00:00
|
|
|
func slot_to_epoch*(slot: Slot|uint64): Epoch =
|
|
|
|
(slot div SLOTS_PER_EPOCH).Epoch
|
2019-01-29 03:22:22 +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_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
|
|
|
|
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#is_double_vote
|
2019-02-07 23:07:15 +00:00
|
|
|
func is_double_vote*(attestation_data_1: AttestationData,
|
|
|
|
attestation_data_2: AttestationData): bool =
|
|
|
|
## Check if ``attestation_data_1`` and ``attestation_data_2`` have the same
|
|
|
|
## target.
|
|
|
|
let
|
2019-03-12 22:21:32 +00:00
|
|
|
# RLP artifact
|
|
|
|
target_epoch_1 = slot_to_epoch(attestation_data_1.slot.Slot)
|
|
|
|
target_epoch_2 = slot_to_epoch(attestation_data_2.slot.Slot)
|
2019-02-07 23:07:15 +00:00
|
|
|
target_epoch_1 == target_epoch_2
|
|
|
|
|
2019-03-08 17:44:31 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/0.4.0/specs/core/0_beacon-chain.md#is_surround_vote
|
2019-02-07 23:07:15 +00:00
|
|
|
func is_surround_vote*(attestation_data_1: AttestationData,
|
2018-12-19 04:36:10 +00:00
|
|
|
attestation_data_2: AttestationData): bool =
|
2019-02-07 23:07:15 +00:00
|
|
|
## Check if ``attestation_data_1`` surrounds ``attestation_data_2``.
|
2019-01-29 03:22:22 +00:00
|
|
|
let
|
|
|
|
source_epoch_1 = attestation_data_1.justified_epoch
|
|
|
|
source_epoch_2 = attestation_data_2.justified_epoch
|
2019-03-12 22:21:32 +00:00
|
|
|
# RLP artifact
|
|
|
|
target_epoch_1 = slot_to_epoch(attestation_data_1.slot.Slot)
|
|
|
|
target_epoch_2 = slot_to_epoch(attestation_data_2.slot.Slot)
|
2019-01-29 03:22:22 +00:00
|
|
|
|
2019-02-07 23:07:15 +00:00
|
|
|
source_epoch_1 < source_epoch_2 and target_epoch_2 < target_epoch_1
|
2019-01-16 11:07:41 +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#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
|
|
|
|
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_active_validator_indices
|
2019-02-20 01:33:58 +00:00
|
|
|
func get_active_validator_indices*(validators: openArray[Validator], epoch: Epoch): seq[ValidatorIndex] =
|
2019-01-16 11:07:41 +00:00
|
|
|
## Gets indices of active validators from validators
|
|
|
|
for idx, val in validators:
|
2019-01-29 03:22:22 +00:00
|
|
|
if is_active_validator(val, epoch):
|
2019-01-29 04:15:00 +00:00
|
|
|
result.add idx.ValidatorIndex
|
2019-01-16 11:07:41 +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_epoch_committee_count
|
2019-01-29 03:22:22 +00:00
|
|
|
func get_epoch_committee_count*(active_validator_count: int): uint64 =
|
2019-01-16 11:07:41 +00:00
|
|
|
clamp(
|
2019-02-20 01:33:58 +00:00
|
|
|
active_validator_count div SLOTS_PER_EPOCH div TARGET_COMMITTEE_SIZE,
|
|
|
|
1, SHARD_COUNT div SLOTS_PER_EPOCH).uint64 * SLOTS_PER_EPOCH
|
2019-01-16 11:07:41 +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_current_epoch_committee_count
|
2019-01-29 03:22:22 +00:00
|
|
|
func get_current_epoch_committee_count*(state: BeaconState): uint64 =
|
2019-02-13 10:26:32 +00:00
|
|
|
# Return the number of committees in the current epoch of the given ``state``.
|
2019-01-16 11:07:41 +00:00
|
|
|
let current_active_validators = get_active_validator_indices(
|
|
|
|
state.validator_registry,
|
2019-02-19 23:07:56 +00:00
|
|
|
state.current_shuffling_epoch,
|
2019-01-16 11:07:41 +00:00
|
|
|
)
|
2019-02-13 10:26:32 +00:00
|
|
|
get_epoch_committee_count(len(current_active_validators))
|
2019-01-29 03:22:22 +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_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-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_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-02-13 10:26:32 +00:00
|
|
|
|
|
|
|
# Cannot underflow, since GENESIS_EPOCH > LATEST_RANDAO_MIXES_LENGTH
|
2019-01-29 03:22:22 +00:00
|
|
|
assert get_current_epoch(state) - LATEST_RANDAO_MIXES_LENGTH < epoch
|
|
|
|
assert epoch <= get_current_epoch(state)
|
|
|
|
|
|
|
|
state.latest_randao_mixes[epoch mod LATEST_RANDAO_MIXES_LENGTH]
|
|
|
|
|
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_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-02-13 10:26:32 +00:00
|
|
|
|
2019-02-20 20:35:27 +00:00
|
|
|
## Cannot underflow, since GENESIS_EPOCH > LATEST_RANDAO_MIXES_LENGTH
|
|
|
|
## and ACTIVATION_EXIT_DELAY > 0.
|
2019-03-12 09:59:43 +00:00
|
|
|
doAssert get_current_epoch(state) - LATEST_ACTIVE_INDEX_ROOTS_LENGTH +
|
2019-02-20 20:35:27 +00:00
|
|
|
ACTIVATION_EXIT_DELAY < epoch
|
2019-03-12 09:59:43 +00:00
|
|
|
doAssert epoch <= get_current_epoch(state) + ACTIVATION_EXIT_DELAY
|
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-02-13 10:26:32 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#bytes_to_int
|
|
|
|
func bytes_to_int*(data: seq[byte]): uint64 =
|
|
|
|
assert data.len == 8
|
|
|
|
|
|
|
|
# Little-endian data representation
|
|
|
|
result = 0
|
|
|
|
for i in countdown(7, 0):
|
|
|
|
result = result * 256 + data[i]
|
|
|
|
|
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#int_to_bytes1-int_to_bytes2-
|
2019-02-13 10:26:32 +00:00
|
|
|
# Have 1, 4, and 32-byte versions. 2+ more and maybe worth metaprogramming.
|
2019-03-13 01:46:44 +00:00
|
|
|
func int_to_bytes32*(x: uint64|Epoch) : array[32, byte] =
|
|
|
|
## 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 01:46:44 +00:00
|
|
|
result[24 + i] = byte((x.uint64 shr i*8) and 0xff)
|
2019-02-13 10:26:32 +00:00
|
|
|
|
|
|
|
func int_to_bytes1*(x: int): array[1, byte] =
|
|
|
|
assert x >= 0
|
|
|
|
assert x < 256
|
|
|
|
|
|
|
|
result[0] = x.byte
|
|
|
|
|
|
|
|
func int_to_bytes4*(x: uint64): array[4, byte] =
|
|
|
|
assert x >= 0'u64
|
|
|
|
assert x < 2'u64^32
|
|
|
|
|
|
|
|
# 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-03-11 15:33:24 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/0.4.0/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-02-19 23:07:56 +00:00
|
|
|
# Cannot underflow, since GENESIS_EPOCH > MIN_SEED_LOOKAHEAD
|
|
|
|
seed_input[0..31] = get_randao_mix(state, epoch - 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)
|