2020-07-30 19:18:17 +00:00
|
|
|
# beacon_chain
|
2022-01-29 01:05:39 +00:00
|
|
|
# Copyright (c) 2018-2022 Status Research & Development GmbH
|
2020-07-30 19:18:17 +00:00
|
|
|
# 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).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2020-11-27 22:16:13 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-07-30 19:18:17 +00:00
|
|
|
import
|
2021-10-20 11:36:38 +00:00
|
|
|
std/sequtils,
|
2022-01-08 23:28:49 +00:00
|
|
|
stew/results,
|
2020-10-22 10:53:33 +00:00
|
|
|
chronicles,
|
2020-08-06 19:48:47 +00:00
|
|
|
../extras,
|
2022-01-08 23:28:49 +00:00
|
|
|
../spec/[beaconstate, helpers, network, signatures, validator],
|
2021-08-12 13:08:20 +00:00
|
|
|
../spec/datatypes/base,
|
|
|
|
./block_pools_types, blockchain_dag
|
|
|
|
|
|
|
|
export
|
2022-01-08 23:28:49 +00:00
|
|
|
base, extras, block_pools_types, results
|
2020-07-30 19:18:17 +00:00
|
|
|
|
|
|
|
# Spec functions implemented based on cached values instead of the full state
|
|
|
|
func count_active_validators*(epochInfo: EpochRef): uint64 =
|
|
|
|
epochInfo.shuffled_active_validator_indices.lenu64
|
|
|
|
|
2022-01-04 03:57:15 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.8/specs/phase0/beacon-chain.md#get_committee_count_per_slot
|
2020-07-30 19:18:17 +00:00
|
|
|
func get_committee_count_per_slot*(epochInfo: EpochRef): uint64 =
|
|
|
|
get_committee_count_per_slot(count_active_validators(epochInfo))
|
|
|
|
|
2021-05-10 07:13:36 +00:00
|
|
|
iterator get_committee_indices*(epochRef: EpochRef): CommitteeIndex =
|
2022-01-08 23:28:49 +00:00
|
|
|
let committees_per_slot = get_committee_count_per_slot(epochRef)
|
|
|
|
for committee_index in get_committee_indices(committees_per_slot):
|
|
|
|
yield committee_index
|
|
|
|
|
|
|
|
func get_committee_index*(epochRef: EpochRef, index: uint64):
|
|
|
|
Result[CommitteeIndex, cstring] =
|
|
|
|
check_attestation_index(index, get_committee_count_per_slot(epochRef))
|
2021-05-10 07:13:36 +00:00
|
|
|
|
2022-01-04 03:57:15 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.8/specs/phase0/beacon-chain.md#get_beacon_committee
|
2020-10-22 10:53:33 +00:00
|
|
|
iterator get_beacon_committee*(
|
2022-01-08 23:28:49 +00:00
|
|
|
epochRef: EpochRef, slot: Slot, committee_index: CommitteeIndex):
|
|
|
|
(int, ValidatorIndex) =
|
2021-10-14 06:30:21 +00:00
|
|
|
## Return the beacon committee at ``slot`` for ``index``.
|
2020-10-22 10:53:33 +00:00
|
|
|
let
|
|
|
|
committees_per_slot = get_committee_count_per_slot(epochRef)
|
2022-01-08 23:28:49 +00:00
|
|
|
for index_in_committee, idx in compute_committee(
|
2020-10-22 10:53:33 +00:00
|
|
|
epochRef.shuffled_active_validator_indices,
|
2022-01-08 23:28:49 +00:00
|
|
|
(slot mod SLOTS_PER_EPOCH) * committees_per_slot + committee_index.asUInt64,
|
2020-10-22 10:53:33 +00:00
|
|
|
committees_per_slot * SLOTS_PER_EPOCH
|
2022-01-08 23:28:49 +00:00
|
|
|
): yield (index_in_committee, idx)
|
2020-10-22 10:53:33 +00:00
|
|
|
|
2022-01-04 03:57:15 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.8/specs/phase0/beacon-chain.md#get_beacon_committee
|
2020-07-30 19:18:17 +00:00
|
|
|
func get_beacon_committee*(
|
2022-01-08 23:28:49 +00:00
|
|
|
epochRef: EpochRef, slot: Slot, committee_index: CommitteeIndex):
|
|
|
|
seq[ValidatorIndex] =
|
2021-10-14 06:30:21 +00:00
|
|
|
## Return the beacon committee at ``slot`` for ``index``.
|
2020-07-30 19:18:17 +00:00
|
|
|
let
|
|
|
|
committees_per_slot = get_committee_count_per_slot(epochRef)
|
|
|
|
compute_committee(
|
|
|
|
epochRef.shuffled_active_validator_indices,
|
2022-01-08 23:28:49 +00:00
|
|
|
(slot mod SLOTS_PER_EPOCH) * committees_per_slot + committee_index.asUInt64,
|
2020-07-30 19:18:17 +00:00
|
|
|
committees_per_slot * SLOTS_PER_EPOCH
|
|
|
|
)
|
|
|
|
|
2022-01-04 03:57:15 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.8/specs/phase0/beacon-chain.md#get_beacon_committee
|
2020-10-22 10:53:33 +00:00
|
|
|
func get_beacon_committee_len*(
|
2022-01-08 23:28:49 +00:00
|
|
|
epochRef: EpochRef, slot: Slot, committee_index: CommitteeIndex): uint64 =
|
2021-10-14 06:30:21 +00:00
|
|
|
## Return the number of members in the beacon committee at ``slot`` for ``index``.
|
2020-10-22 10:53:33 +00:00
|
|
|
let
|
|
|
|
committees_per_slot = get_committee_count_per_slot(epochRef)
|
|
|
|
|
|
|
|
compute_committee_len(
|
|
|
|
count_active_validators(epochRef),
|
2022-01-08 23:28:49 +00:00
|
|
|
(slot mod SLOTS_PER_EPOCH) * committees_per_slot + committee_index.asUInt64,
|
2020-10-22 10:53:33 +00:00
|
|
|
committees_per_slot * SLOTS_PER_EPOCH
|
|
|
|
)
|
|
|
|
|
2022-01-04 03:57:15 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.8/specs/phase0/beacon-chain.md#get_attesting_indices
|
2020-10-22 10:53:33 +00:00
|
|
|
iterator get_attesting_indices*(epochRef: EpochRef,
|
2022-01-08 23:28:49 +00:00
|
|
|
slot: Slot,
|
|
|
|
committee_index: CommitteeIndex,
|
2020-10-22 10:53:33 +00:00
|
|
|
bits: CommitteeValidatorsBits):
|
|
|
|
ValidatorIndex =
|
2022-01-08 23:28:49 +00:00
|
|
|
if bits.lenu64 != get_beacon_committee_len(epochRef, slot, committee_index):
|
2020-10-22 10:53:33 +00:00
|
|
|
trace "get_attesting_indices: inconsistent aggregation and committee length"
|
|
|
|
else:
|
2022-01-08 23:28:49 +00:00
|
|
|
for index_in_committee, validator_index in get_beacon_committee(
|
|
|
|
epochRef, slot, committee_index).pairs():
|
|
|
|
if bits[index_in_committee]:
|
|
|
|
yield validator_index
|
2020-10-22 10:53:33 +00:00
|
|
|
|
2021-04-26 20:39:44 +00:00
|
|
|
func get_attesting_indices_one*(epochRef: EpochRef,
|
2022-01-08 23:28:49 +00:00
|
|
|
slot: Slot,
|
|
|
|
committee_index: CommitteeIndex,
|
2021-04-26 20:39:44 +00:00
|
|
|
bits: CommitteeValidatorsBits):
|
|
|
|
Option[ValidatorIndex] =
|
|
|
|
# A variation on get_attesting_indices that returns the validator index only
|
|
|
|
# if only one validator index is set
|
2022-01-08 23:28:49 +00:00
|
|
|
var res = none(ValidatorIndex)
|
|
|
|
for validator_index in get_attesting_indices(
|
|
|
|
epochRef, slot, committee_index, bits):
|
|
|
|
if res.isSome(): return none(ValidatorIndex)
|
|
|
|
res = some(validator_index)
|
|
|
|
res
|
2021-04-26 20:39:44 +00:00
|
|
|
|
2022-01-04 03:57:15 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.8/specs/phase0/beacon-chain.md#get_attesting_indices
|
2020-07-30 19:18:17 +00:00
|
|
|
func get_attesting_indices*(epochRef: EpochRef,
|
2022-01-08 23:28:49 +00:00
|
|
|
slot: Slot,
|
|
|
|
committee_index: CommitteeIndex,
|
2020-07-30 19:18:17 +00:00
|
|
|
bits: CommitteeValidatorsBits):
|
2021-02-08 07:27:30 +00:00
|
|
|
seq[ValidatorIndex] =
|
|
|
|
# TODO sequtils2 mapIt
|
2022-01-08 23:28:49 +00:00
|
|
|
for idx in get_attesting_indices(epochRef, slot, committee_index, bits):
|
2021-02-08 07:27:30 +00:00
|
|
|
result.add(idx)
|
2020-07-30 19:18:17 +00:00
|
|
|
|
2022-01-04 03:57:15 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.8/specs/phase0/beacon-chain.md#is_valid_indexed_attestation
|
2020-08-27 07:34:12 +00:00
|
|
|
proc is_valid_indexed_attestation*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
2021-04-14 14:43:29 +00:00
|
|
|
epochRef: EpochRef,
|
2020-08-27 07:34:12 +00:00
|
|
|
attestation: SomeAttestation, flags: UpdateFlags): Result[void, cstring] =
|
|
|
|
# This is a variation on `is_valid_indexed_attestation` that works directly
|
|
|
|
# with an attestation instead of first constructing an `IndexedAttestation`
|
|
|
|
# and then validating it - for the purpose of validating the signature, the
|
|
|
|
# order doesn't matter and we can proceed straight to validating the
|
|
|
|
# signature instead
|
2021-04-14 14:43:29 +00:00
|
|
|
let sigs = attestation.aggregation_bits.countOnes()
|
|
|
|
if sigs == 0:
|
|
|
|
return err("is_valid_indexed_attestation: no attesting indices")
|
2020-08-27 07:34:12 +00:00
|
|
|
|
|
|
|
# Verify aggregate signature
|
2021-01-25 16:09:31 +00:00
|
|
|
if not (skipBLSValidation in flags or attestation.signature is TrustedSig):
|
2021-04-14 14:43:29 +00:00
|
|
|
var
|
2021-06-01 11:13:40 +00:00
|
|
|
pubkeys = newSeqOfCap[CookedPubKey](sigs)
|
2021-04-14 14:43:29 +00:00
|
|
|
for index in get_attesting_indices(
|
|
|
|
epochRef, attestation.data, attestation.aggregation_bits):
|
2021-06-10 07:37:02 +00:00
|
|
|
pubkeys.add(epochRef.validatorKey(index).get())
|
2021-04-14 14:43:29 +00:00
|
|
|
|
2020-08-27 07:34:12 +00:00
|
|
|
if not verify_attestation_signature(
|
|
|
|
fork, genesis_validators_root, attestation.data,
|
|
|
|
pubkeys, attestation.signature):
|
2021-04-14 14:43:29 +00:00
|
|
|
return err("is_valid_indexed_attestation: signature verification failure")
|
2020-08-06 19:48:47 +00:00
|
|
|
|
2020-08-13 13:47:06 +00:00
|
|
|
ok()
|
2020-08-10 13:21:31 +00:00
|
|
|
|
|
|
|
func makeAttestationData*(
|
|
|
|
epochRef: EpochRef, bs: BlockSlot,
|
2020-11-04 21:52:47 +00:00
|
|
|
committee_index: CommitteeIndex): AttestationData =
|
2020-08-10 13:21:31 +00:00
|
|
|
## Create an attestation / vote for the block `bs` using the
|
|
|
|
## data in `epochRef` to fill in the rest of the fields.
|
|
|
|
## `epochRef` is the epoch information corresponding to the `bs` advanced to
|
|
|
|
## the slot we're attesting to.
|
|
|
|
|
|
|
|
let
|
|
|
|
slot = bs.slot
|
2022-01-11 10:01:54 +00:00
|
|
|
current_epoch = slot.epoch()
|
|
|
|
epoch_boundary_slot = current_epoch.start_slot()
|
2020-08-10 13:21:31 +00:00
|
|
|
epoch_boundary_block = bs.blck.atSlot(epoch_boundary_slot)
|
|
|
|
|
|
|
|
doAssert current_epoch == epochRef.epoch
|
|
|
|
|
2022-01-29 01:05:39 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/phase0/validator.md#attestation-data
|
2020-08-10 13:21:31 +00:00
|
|
|
AttestationData(
|
|
|
|
slot: slot,
|
2022-01-08 23:28:49 +00:00
|
|
|
index: committee_index.asUInt64,
|
2020-08-10 13:21:31 +00:00
|
|
|
beacon_block_root: bs.blck.root,
|
|
|
|
source: epochRef.current_justified_checkpoint,
|
|
|
|
target: Checkpoint(
|
|
|
|
epoch: current_epoch,
|
|
|
|
root: epoch_boundary_block.blck.root
|
|
|
|
)
|
|
|
|
)
|
2021-03-30 15:01:47 +00:00
|
|
|
|
2022-01-29 01:05:39 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/phase0/validator.md#validator-assignments
|
2021-03-30 15:01:47 +00:00
|
|
|
iterator get_committee_assignments*(
|
2021-10-20 11:36:38 +00:00
|
|
|
epochRef: EpochRef, validator_indices: HashSet[ValidatorIndex]):
|
2022-01-08 23:28:49 +00:00
|
|
|
tuple[committee_index: CommitteeIndex,
|
2021-05-10 07:13:36 +00:00
|
|
|
subnet_id: SubnetId, slot: Slot] =
|
2021-03-30 15:01:47 +00:00
|
|
|
let
|
|
|
|
committees_per_slot = get_committee_count_per_slot(epochRef)
|
2021-10-18 09:11:44 +00:00
|
|
|
epoch = epochRef.epoch
|
2021-03-30 15:01:47 +00:00
|
|
|
|
2022-01-11 10:01:54 +00:00
|
|
|
for slot in epoch.slots():
|
2022-01-08 23:28:49 +00:00
|
|
|
for committee_index in get_committee_indices(committees_per_slot):
|
|
|
|
if anyIt(get_beacon_committee(epochRef, slot, committee_index), it in validator_indices):
|
2021-03-30 15:01:47 +00:00
|
|
|
yield (
|
2022-01-08 23:28:49 +00:00
|
|
|
committee_index,
|
|
|
|
compute_subnet_for_attestation(committees_per_slot, slot, committee_index),
|
2021-03-30 15:01:47 +00:00
|
|
|
slot)
|
2021-08-24 19:49:51 +00:00
|
|
|
|
|
|
|
func is_aggregator*(epochRef: EpochRef, slot: Slot, index: CommitteeIndex,
|
|
|
|
slot_signature: ValidatorSig): bool =
|
|
|
|
let
|
|
|
|
committee_len = get_beacon_committee_len(epochRef, slot, index)
|
|
|
|
return is_aggregator(committee_len, slot_signature)
|