2020-07-30 19:18:17 +00:00
|
|
|
# beacon_chain
|
2021-01-26 11:52:00 +00:00
|
|
|
# Copyright (c) 2018-2021 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-03-30 15:01:47 +00:00
|
|
|
std/[algorithm, intsets, sequtils],
|
2020-10-22 10:53:33 +00:00
|
|
|
chronicles,
|
2020-08-06 19:48:47 +00:00
|
|
|
../spec/[
|
2021-03-30 15:01:47 +00:00
|
|
|
crypto, datatypes, digest, helpers, network, presets, signatures,
|
2020-08-06 19:48:47 +00:00
|
|
|
validator],
|
|
|
|
../extras,
|
2021-03-04 09:13:44 +00:00
|
|
|
./block_pools_types, ./blockchain_dag
|
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
|
|
|
|
|
2021-02-25 13:37:22 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/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 =
|
|
|
|
for i in 0'u64..<get_committee_count_per_slot(epochRef):
|
|
|
|
yield CommitteeIndex(i)
|
|
|
|
|
2021-02-25 13:37:22 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#get_beacon_committee
|
2020-10-22 10:53:33 +00:00
|
|
|
iterator get_beacon_committee*(
|
|
|
|
epochRef: EpochRef, slot: Slot, index: CommitteeIndex): ValidatorIndex =
|
|
|
|
# Return the beacon committee at ``slot`` for ``index``.
|
|
|
|
let
|
|
|
|
committees_per_slot = get_committee_count_per_slot(epochRef)
|
|
|
|
for idx in compute_committee(
|
|
|
|
epochRef.shuffled_active_validator_indices,
|
|
|
|
(slot mod SLOTS_PER_EPOCH) * committees_per_slot +
|
|
|
|
index.uint64,
|
|
|
|
committees_per_slot * SLOTS_PER_EPOCH
|
|
|
|
): yield idx
|
|
|
|
|
2021-02-25 13:37:22 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#get_beacon_committee
|
2020-07-30 19:18:17 +00:00
|
|
|
func get_beacon_committee*(
|
|
|
|
epochRef: EpochRef, slot: Slot, index: CommitteeIndex): seq[ValidatorIndex] =
|
|
|
|
# Return the beacon committee at ``slot`` for ``index``.
|
|
|
|
let
|
|
|
|
committees_per_slot = get_committee_count_per_slot(epochRef)
|
|
|
|
compute_committee(
|
|
|
|
epochRef.shuffled_active_validator_indices,
|
|
|
|
(slot mod SLOTS_PER_EPOCH) * committees_per_slot +
|
|
|
|
index.uint64,
|
|
|
|
committees_per_slot * SLOTS_PER_EPOCH
|
|
|
|
)
|
|
|
|
|
2021-02-25 13:37:22 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#get_beacon_committee
|
2020-10-22 10:53:33 +00:00
|
|
|
func get_beacon_committee_len*(
|
|
|
|
epochRef: EpochRef, slot: Slot, index: CommitteeIndex): uint64 =
|
|
|
|
# Return the number of members in the beacon committee at ``slot`` for ``index``.
|
|
|
|
let
|
|
|
|
committees_per_slot = get_committee_count_per_slot(epochRef)
|
|
|
|
|
|
|
|
compute_committee_len(
|
|
|
|
count_active_validators(epochRef),
|
|
|
|
(slot mod SLOTS_PER_EPOCH) * committees_per_slot +
|
|
|
|
index.uint64,
|
|
|
|
committees_per_slot * SLOTS_PER_EPOCH
|
|
|
|
)
|
|
|
|
|
2021-02-25 13:37:22 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#get_attesting_indices
|
2020-10-22 10:53:33 +00:00
|
|
|
iterator get_attesting_indices*(epochRef: EpochRef,
|
|
|
|
data: AttestationData,
|
|
|
|
bits: CommitteeValidatorsBits):
|
|
|
|
ValidatorIndex =
|
|
|
|
if bits.lenu64 != get_beacon_committee_len(epochRef, data.slot, data.index.CommitteeIndex):
|
|
|
|
trace "get_attesting_indices: inconsistent aggregation and committee length"
|
|
|
|
else:
|
|
|
|
var i = 0
|
|
|
|
for index in get_beacon_committee(epochRef, data.slot, data.index.CommitteeIndex):
|
|
|
|
if bits[i]:
|
|
|
|
yield index
|
|
|
|
inc i
|
|
|
|
|
2021-04-26 20:39:44 +00:00
|
|
|
func get_attesting_indices_one*(epochRef: EpochRef,
|
|
|
|
data: AttestationData,
|
|
|
|
bits: CommitteeValidatorsBits):
|
|
|
|
Option[ValidatorIndex] =
|
|
|
|
# A variation on get_attesting_indices that returns the validator index only
|
|
|
|
# if only one validator index is set
|
|
|
|
if bits.lenu64 != get_beacon_committee_len(epochRef, data.slot, data.index.CommitteeIndex):
|
|
|
|
trace "get_attesting_indices: inconsistent aggregation and committee length"
|
|
|
|
none(ValidatorIndex)
|
|
|
|
else:
|
|
|
|
var res = none(ValidatorIndex)
|
|
|
|
var i = 0
|
|
|
|
for index in get_beacon_committee(epochRef, data.slot, data.index.CommitteeIndex):
|
|
|
|
if bits[i]:
|
|
|
|
if res.isNone():
|
|
|
|
res = some(index)
|
|
|
|
else:
|
|
|
|
return none(ValidatorIndex)
|
|
|
|
inc i
|
|
|
|
res
|
|
|
|
|
2021-02-25 13:37:22 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#get_attesting_indices
|
2020-07-30 19:18:17 +00:00
|
|
|
func get_attesting_indices*(epochRef: EpochRef,
|
|
|
|
data: AttestationData,
|
|
|
|
bits: CommitteeValidatorsBits):
|
2021-02-08 07:27:30 +00:00
|
|
|
seq[ValidatorIndex] =
|
|
|
|
# TODO sequtils2 mapIt
|
2020-10-22 10:53:33 +00:00
|
|
|
for idx in get_attesting_indices(epochRef, data, bits):
|
2021-02-08 07:27:30 +00:00
|
|
|
result.add(idx)
|
2020-07-30 19:18:17 +00:00
|
|
|
|
2021-02-25 13:37:22 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#get_indexed_attestation
|
2020-07-30 19:18:17 +00:00
|
|
|
func get_indexed_attestation*(epochRef: EpochRef, attestation: Attestation): IndexedAttestation =
|
|
|
|
# Return the indexed attestation corresponding to ``attestation``.
|
|
|
|
let
|
|
|
|
attesting_indices =
|
|
|
|
get_attesting_indices(
|
|
|
|
epochRef, attestation.data, attestation.aggregation_bits)
|
|
|
|
|
|
|
|
IndexedAttestation(
|
|
|
|
attesting_indices:
|
|
|
|
List[uint64, Limit MAX_VALIDATORS_PER_COMMITTEE].init(
|
|
|
|
sorted(mapIt(attesting_indices, it.uint64), system.cmp)),
|
|
|
|
data: attestation.data,
|
|
|
|
signature: attestation.signature
|
|
|
|
)
|
2020-08-06 19:48:47 +00:00
|
|
|
|
2021-02-25 13:37:22 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#is_valid_indexed_attestation
|
2020-08-06 19:48:47 +00:00
|
|
|
proc is_valid_indexed_attestation*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
epochRef: EpochRef, indexed_attestation: SomeIndexedAttestation,
|
2020-08-13 13:47:06 +00:00
|
|
|
flags: UpdateFlags): Result[void, cstring] =
|
2020-08-06 19:48:47 +00:00
|
|
|
# Check if ``indexed_attestation`` is not empty, has sorted and unique
|
|
|
|
# indices and has a valid aggregate signature.
|
|
|
|
|
|
|
|
template is_sorted_and_unique(s: untyped): bool =
|
2020-08-13 13:47:06 +00:00
|
|
|
var res = true
|
2020-08-06 19:48:47 +00:00
|
|
|
for i in 1 ..< s.len:
|
|
|
|
if s[i - 1].uint64 >= s[i].uint64:
|
2020-08-13 13:47:06 +00:00
|
|
|
res = false
|
|
|
|
break
|
|
|
|
res
|
2020-08-06 19:48:47 +00:00
|
|
|
|
2020-08-13 13:47:06 +00:00
|
|
|
if len(indexed_attestation.attesting_indices) == 0:
|
|
|
|
return err("indexed_attestation: no attesting indices")
|
2020-08-06 19:48:47 +00:00
|
|
|
|
|
|
|
# Not from spec, but this function gets used in front-line roles, not just
|
|
|
|
# behind firewall.
|
|
|
|
let num_validators = epochRef.validator_keys.lenu64
|
|
|
|
if anyIt(indexed_attestation.attesting_indices, it >= num_validators):
|
2020-08-13 13:47:06 +00:00
|
|
|
return err("indexed attestation: not all indices valid validators")
|
2020-08-06 19:48:47 +00:00
|
|
|
|
2020-08-13 13:47:06 +00:00
|
|
|
if not is_sorted_and_unique(indexed_attestation.attesting_indices):
|
|
|
|
return err("indexed attestation: indices not sorted and unique")
|
2020-08-06 19:48:47 +00:00
|
|
|
|
|
|
|
# Verify aggregate signature
|
2021-01-25 16:09:31 +00:00
|
|
|
if not (skipBLSValidation in flags or indexed_attestation.signature is TrustedSig):
|
2020-08-13 13:47:06 +00:00
|
|
|
let pubkeys = mapIt(
|
|
|
|
indexed_attestation.attesting_indices, epochRef.validator_keys[it])
|
2020-08-06 19:48:47 +00:00
|
|
|
if not verify_attestation_signature(
|
|
|
|
fork, genesis_validators_root, indexed_attestation.data,
|
|
|
|
pubkeys, indexed_attestation.signature):
|
2020-08-13 13:47:06 +00:00
|
|
|
return err("indexed attestation: signature verification failure")
|
2020-08-27 07:34:12 +00:00
|
|
|
|
|
|
|
ok()
|
|
|
|
|
2021-02-25 13:37:22 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/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
|
|
|
|
pubkeys = newSeqOfCap[ValidatorPubKey](sigs)
|
|
|
|
for index in get_attesting_indices(
|
|
|
|
epochRef, attestation.data, attestation.aggregation_bits):
|
|
|
|
pubkeys.add(epochRef.validator_keys[index])
|
|
|
|
|
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
|
|
|
|
current_epoch = slot.compute_epoch_at_slot()
|
|
|
|
epoch_boundary_slot = compute_start_slot_at_epoch(current_epoch)
|
|
|
|
epoch_boundary_block = bs.blck.atSlot(epoch_boundary_slot)
|
|
|
|
|
|
|
|
doAssert current_epoch == epochRef.epoch
|
|
|
|
|
2021-02-25 13:37:22 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/validator.md#attestation-data
|
2020-08-10 13:21:31 +00:00
|
|
|
AttestationData(
|
|
|
|
slot: slot,
|
2020-11-04 21:52:47 +00:00
|
|
|
index: committee_index.uint64,
|
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
|
|
|
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/validator.md#validator-assignments
|
|
|
|
iterator get_committee_assignments*(
|
|
|
|
epochRef: EpochRef, epoch: Epoch, validator_indices: IntSet):
|
|
|
|
tuple[validatorIndices: IntSet,
|
|
|
|
committeeIndex: 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)
|
|
|
|
start_slot = compute_start_slot_at_epoch(epoch)
|
|
|
|
|
|
|
|
for slot in start_slot ..< start_slot + SLOTS_PER_EPOCH:
|
|
|
|
for index in 0'u64 ..< committees_per_slot:
|
|
|
|
let
|
|
|
|
idx = index.CommitteeIndex
|
|
|
|
includedIndices =
|
|
|
|
toIntSet(get_beacon_committee(epochRef, slot, idx)) *
|
|
|
|
validator_indices
|
|
|
|
if includedIndices.len > 0:
|
|
|
|
yield (
|
|
|
|
includedIndices, idx,
|
2021-05-10 07:13:36 +00:00
|
|
|
compute_subnet_for_attestation(committees_per_slot, slot, idx),
|
2021-03-30 15:01:47 +00:00
|
|
|
slot)
|