diff --git a/beacon_chain/consensus_object_pools/block_pools_types.nim b/beacon_chain/consensus_object_pools/block_pools_types.nim index 5e814c543..1e001f6e0 100644 --- a/beacon_chain/consensus_object_pools/block_pools_types.nim +++ b/beacon_chain/consensus_object_pools/block_pools_types.nim @@ -1,5 +1,5 @@ # beacon_chain -# Copyright (c) 2018-2020 Status Research & Development GmbH +# Copyright (c) 2018-2021 Status Research & Development GmbH # Licensed and distributed under either of # * 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). @@ -9,7 +9,7 @@ import # Standard library - std/[deques, strformat, sets, tables, hashes], + std/[strformat, sets, tables, hashes], # Status libraries stew/[endians2, byteutils], chronicles, eth/keys, diff --git a/beacon_chain/spec/datatypes/altair.nim b/beacon_chain/spec/datatypes/altair.nim index 518b8c975..3a624e524 100644 --- a/beacon_chain/spec/datatypes/altair.nim +++ b/beacon_chain/spec/datatypes/altair.nim @@ -45,7 +45,20 @@ const TARGET_AGGREGATORS_PER_SYNC_SUBCOMMITTEE* = 4 SYNC_COMMITTEE_SUBNET_COUNT* = 8 +let + # https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.2/specs/altair/beacon-chain.md#misc + # Cannot be computed at compile-time due to importc dependency + G2_POINT_AT_INFINITY* = ValidatorSig.fromRaw([ + 0xc0'u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0]) + type + # https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.2/specs/altair/beacon-chain.md#custom-types + ParticipationFlags* = distinct uint8 + # https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.2/specs/altair/beacon-chain.md#syncaggregate SyncAggregate* = object sync_committee_bits*: BitArray[SYNC_COMMITTEE_SIZE] @@ -110,7 +123,13 @@ type subcommittee_index*: uint64 # https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.2/specs/altair/beacon-chain.md#participation-flag-indices - ValidatorFlag* = enum - TIMELY_HEAD_FLAG = 0 - TIMELY_SOURCE_FLAG = 1 - TIMELY_TARGET_FLAG = 2 + ParticipationFlag* = enum + TIMELY_HEAD_FLAG_INDEX = 0 + TIMELY_SOURCE_FLAG_INDEX = 1 + TIMELY_TARGET_FLAG_INDEX = 2 + +# TODO when https://github.com/nim-lang/Nim/issues/14440 lands in Status's Nim, +# switch proc {.noSideEffect.} to func. +proc `or`*(x, y: ParticipationFlags) : ParticipationFlags {.borrow, noSideEffect.} +proc `and`*(x, y: ParticipationFlags) : ParticipationFlags {.borrow, noSideEffect.} +proc `==`*(x, y: ParticipationFlags) : bool {.borrow, noSideEffect.} diff --git a/beacon_chain/spec/datatypes/base.nim b/beacon_chain/spec/datatypes/base.nim index 0bb8a3765..8e0996f0c 100644 --- a/beacon_chain/spec/datatypes/base.nim +++ b/beacon_chain/spec/datatypes/base.nim @@ -120,10 +120,12 @@ type DOMAIN_SELECTION_PROOF = 5 DOMAIN_AGGREGATE_AND_PROOF = 6 - # https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.1/configs/mainnet/altair.yaml#L31 + # https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.2/specs/altair/beacon-chain.md#domain-types # Needs to be in same enum definition and is safe regardless of whether one # only accesses phase 0 definitions DOMAIN_SYNC_COMMITTEE = 7 + DOMAIN_SYNC_COMMITTEE_SELECTION_PROOF = 8 + DOMAIN_CONTRIBUTION_AND_PROOF = 9 # https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#custom-types Eth2Domain* = array[32, byte] diff --git a/beacon_chain/spec/helpers.nim b/beacon_chain/spec/helpers.nim index 1b46f6da3..1b48807d3 100644 --- a/beacon_chain/spec/helpers.nim +++ b/beacon_chain/spec/helpers.nim @@ -15,7 +15,7 @@ import # Third-party stew/endians2, # Internal - ./datatypes, ./digest, ./crypto, ../ssz/merkleization + ./datatypes/[phase0, altair], ./digest, ./crypto, ../ssz/merkleization # https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#integer_squareroot func integer_squareroot*(n: SomeInteger): SomeInteger = @@ -178,3 +178,21 @@ func get_seed*(state: BeaconState, epoch: Epoch, domain_type: DomainType): Eth2D get_randao_mix(state, # Avoid underflow epoch + EPOCHS_PER_HISTORICAL_VECTOR - MIN_SEED_LOOKAHEAD - 1).data eth2digest(seed_input) + +# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.2/specs/altair/beacon-chain.md#get_flag_indices_and_weights +iterator get_flag_indices_and_weights*(): (ParticipationFlag, int) = + for item in [ + (TIMELY_HEAD_FLAG_INDEX, TIMELY_HEAD_WEIGHT), + (TIMELY_SOURCE_FLAG_INDEX, TIMELY_SOURCE_WEIGHT), + (TIMELY_TARGET_FLAG_INDEX, TIMELY_TARGET_WEIGHT)]: + yield item + +# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.2/specs/altair/beacon-chain.md#add_flag +func add_flag*(flags: ParticipationFlags, flag_index: int): ParticipationFlags = + let flag = ParticipationFlags(1'u8 shl flag_index) + flags or flag + +# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.2/specs/altair/beacon-chain.md#has_flag +func has_flag*(flags: ParticipationFlags, flag_index: int): bool = + let flag = ParticipationFlags(1'u8 shl flag_index) + (flags and flag) == flag diff --git a/beacon_chain/spec/preset_values.nim b/beacon_chain/spec/preset_values.nim index d004d05b9..ea8f983a3 100644 --- a/beacon_chain/spec/preset_values.nim +++ b/beacon_chain/spec/preset_values.nim @@ -10,10 +10,12 @@ type DOMAIN_AGGREGATE_AND_PROOF DOMAIN_BEACON_ATTESTER DOMAIN_BEACON_PROPOSER + DOMAIN_CONTRIBUTION_AND_PROOF DOMAIN_DEPOSIT DOMAIN_RANDAO DOMAIN_SELECTION_PROOF DOMAIN_SYNC_COMMITTEE + DOMAIN_SYNC_COMMITTEE_SELECTION_PROOF DOMAIN_VOLUNTARY_EXIT EFFECTIVE_BALANCE_INCREMENT EJECTION_BALANCE diff --git a/beacon_chain/spec/presets.nim b/beacon_chain/spec/presets.nim index c5d190c12..42fe42a81 100644 --- a/beacon_chain/spec/presets.nim +++ b/beacon_chain/spec/presets.nim @@ -63,6 +63,8 @@ const DOMAIN_SELECTION_PROOF, DOMAIN_AGGREGATE_AND_PROOF, DOMAIN_SYNC_COMMITTEE, + DOMAIN_SYNC_COMMITTEE_SELECTION_PROOF, + DOMAIN_CONTRIBUTION_AND_PROOF, CONFIG_NAME } diff --git a/beacon_chain/ssz/bytes_reader.nim b/beacon_chain/ssz/bytes_reader.nim index d18150939..db1df1ed8 100644 --- a/beacon_chain/ssz/bytes_reader.nim +++ b/beacon_chain/ssz/bytes_reader.nim @@ -1,5 +1,5 @@ # beacon_chain -# Copyright (c) 2018-2020 Status Research & Development GmbH +# Copyright (c) 2018-2021 Status Research & Development GmbH # Licensed and distributed under either of # * 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). @@ -11,7 +11,7 @@ import std/[typetraits, options], - stew/[bitops2, endians2, objects], + stew/[endians2, objects], ../spec/[digest, datatypes], ./types, ./spec_types, ./merkleization template raiseIncorrectSize(T: type) = diff --git a/beacon_chain/ssz/ssz_serialization.nim b/beacon_chain/ssz/ssz_serialization.nim index 4b204a2c2..b76988fb2 100644 --- a/beacon_chain/ssz/ssz_serialization.nim +++ b/beacon_chain/ssz/ssz_serialization.nim @@ -13,7 +13,7 @@ import std/[typetraits, options], - stew/[bitops2, endians2, leb128, objects], + stew/[endians2, leb128, objects], serialization, serialization/testing/tracing, ../spec/[digest, datatypes], ./bytes_reader, ./bitseqs, ./types, ./spec_types