2019-12-06 15:05:11 +00:00
|
|
|
# beacon_chain
|
2020-01-22 12:48:06 +00:00
|
|
|
# Copyright (c) 2019-2020 Status Research & Development GmbH
|
2019-12-06 15:05:11 +00:00
|
|
|
# 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.
|
|
|
|
|
2020-04-24 07:16:11 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2019-12-06 12:05:00 +00:00
|
|
|
import
|
2019-12-10 13:44:22 +00:00
|
|
|
options,
|
2020-03-19 23:48:03 +00:00
|
|
|
./spec/[beaconstate, datatypes, crypto, digest, helpers, validator,
|
|
|
|
state_transition_block],
|
2020-06-03 13:52:02 +00:00
|
|
|
./attestation_pool, ./beacon_node_types
|
2019-12-06 12:05:00 +00:00
|
|
|
|
2020-04-01 09:59:55 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/validator.md#aggregation-selection
|
2020-04-15 09:01:36 +00:00
|
|
|
func is_aggregator(state: BeaconState, slot: Slot, index: CommitteeIndex,
|
2019-12-06 12:05:00 +00:00
|
|
|
slot_signature: ValidatorSig): bool =
|
|
|
|
var cache = get_empty_per_epoch_cache()
|
|
|
|
|
|
|
|
let
|
|
|
|
committee = get_beacon_committee(state, slot, index, cache)
|
|
|
|
modulo = max(1, len(committee) div TARGET_AGGREGATORS_PER_COMMITTEE).uint64
|
2020-04-11 08:51:07 +00:00
|
|
|
bytes_to_int(eth2hash(slot_signature.toRaw()).data[0..7]) mod modulo == 0
|
2019-12-06 12:05:00 +00:00
|
|
|
|
2019-12-09 20:24:39 +00:00
|
|
|
proc aggregate_attestations*(
|
2020-04-15 09:01:36 +00:00
|
|
|
pool: AttestationPool, state: BeaconState, index: CommitteeIndex,
|
2020-04-01 09:59:55 +00:00
|
|
|
privkey: ValidatorPrivKey, trailing_distance: uint64): Option[AggregateAndProof] =
|
|
|
|
doAssert state.slot >= trailing_distance
|
|
|
|
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/p2p-interface.md#configuration
|
|
|
|
doAssert trailing_distance <= ATTESTATION_PROPAGATION_SLOT_RANGE
|
|
|
|
|
2019-12-10 13:44:22 +00:00
|
|
|
let
|
2020-04-01 09:59:55 +00:00
|
|
|
slot = state.slot - trailing_distance
|
|
|
|
slot_signature = get_slot_signature(
|
|
|
|
state.fork, state.genesis_validators_root, slot, privkey)
|
2019-12-10 13:44:22 +00:00
|
|
|
|
|
|
|
doAssert slot + ATTESTATION_PROPAGATION_SLOT_RANGE >= state.slot
|
|
|
|
doAssert state.slot >= slot
|
|
|
|
|
2020-04-01 09:59:55 +00:00
|
|
|
# TODO performance issue for future, via get_active_validator_indices(...)
|
2020-04-15 09:01:36 +00:00
|
|
|
doAssert index.uint64 < get_committee_count_at_slot(state, slot)
|
2020-04-01 09:59:55 +00:00
|
|
|
|
|
|
|
# TODO for testing purposes, refactor this into the condition check
|
|
|
|
# and just calculation
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/validator.md#aggregation-selection
|
2019-12-10 13:44:22 +00:00
|
|
|
if not is_aggregator(state, slot, index, slot_signature):
|
|
|
|
return none(AggregateAndProof)
|
2019-12-06 15:05:11 +00:00
|
|
|
|
2020-04-01 09:59:55 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/validator.md#attestation-data
|
|
|
|
# describes how to construct an attestation, which applies for makeAttestationData(...)
|
|
|
|
# TODO this won't actually match anything
|
|
|
|
let attestation_data = AttestationData(
|
|
|
|
slot: slot,
|
2020-04-15 09:01:36 +00:00
|
|
|
index: index.uint64,
|
2020-04-01 09:59:55 +00:00
|
|
|
beacon_block_root: get_block_root_at_slot(state, slot))
|
2019-12-10 13:44:22 +00:00
|
|
|
|
2020-04-01 09:59:55 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/validator.md#construct-aggregate
|
|
|
|
# TODO once EV goes in w/ refactoring of getAttestationsForBlock, pull out the getSlot version and use
|
|
|
|
# it. This is incorrect.
|
|
|
|
for attestation in getAttestationsForBlock(pool, state):
|
|
|
|
# getAttestationsForBlock(...) already aggregates
|
2019-12-10 14:21:07 +00:00
|
|
|
if attestation.data == attestation_data:
|
2020-04-01 09:59:55 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/validator.md#aggregateandproof
|
2019-12-10 13:44:22 +00:00
|
|
|
return some(AggregateAndProof(
|
2020-04-15 09:01:36 +00:00
|
|
|
aggregator_index: index.uint64,
|
2019-12-10 13:44:22 +00:00
|
|
|
aggregate: attestation,
|
|
|
|
selection_proof: slot_signature))
|
|
|
|
|
|
|
|
none(AggregateAndProof)
|