2019-10-01 13:52:28 +00:00
|
|
|
# beacon_chain
|
2020-02-21 12:16:58 +00:00
|
|
|
# Copyright (c) 2018-2020 Status Research & Development GmbH
|
2019-10-01 13:52:28 +00:00
|
|
|
# Licensed and distributed under either of
|
2019-11-25 15:30:02 +00:00
|
|
|
# * 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).
|
2019-10-01 13:52:28 +00:00
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2020-04-22 05:53:02 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-04-15 02:41:22 +00:00
|
|
|
import
|
2020-08-12 17:48:31 +00:00
|
|
|
std/[strformat, sets, random],
|
2020-07-27 16:04:44 +00:00
|
|
|
./datatypes, ./helpers, ./validator
|
2020-02-21 12:16:58 +00:00
|
|
|
|
2019-10-01 13:52:28 +00:00
|
|
|
const
|
2020-09-25 11:52:50 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.3/specs/phase0/p2p-interface.md#topics-and-messages
|
2020-04-15 02:41:22 +00:00
|
|
|
topicBeaconBlocksSuffix* = "beacon_block/ssz"
|
|
|
|
topicVoluntaryExitsSuffix* = "voluntary_exit/ssz"
|
|
|
|
topicProposerSlashingsSuffix* = "proposer_slashing/ssz"
|
|
|
|
topicAttesterSlashingsSuffix* = "attester_slashing/ssz"
|
|
|
|
topicAggregateAndProofsSuffix* = "beacon_aggregate_and_proof/ssz"
|
2020-02-21 12:16:58 +00:00
|
|
|
|
2020-09-24 17:04:10 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.3/specs/phase0/validator.md#misc
|
2020-02-21 12:16:58 +00:00
|
|
|
ATTESTATION_SUBNET_COUNT* = 64
|
|
|
|
|
2020-09-25 11:52:50 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.3/specs/phase0/p2p-interface.md#eth2-network-interaction-domains
|
2020-08-19 11:33:52 +00:00
|
|
|
MAX_CHUNK_SIZE* = 1 * 1024 * 1024 # bytes
|
|
|
|
GOSSIP_MAX_SIZE* = 1 * 1024 * 1024 # bytes
|
|
|
|
TTFB_TIMEOUT* = 5.seconds
|
|
|
|
RESP_TIMEOUT* = 10.seconds
|
|
|
|
|
2020-03-16 22:28:54 +00:00
|
|
|
defaultEth2TcpPort* = 9000
|
|
|
|
|
|
|
|
# This is not part of the spec yet!
|
|
|
|
defaultEth2RpcPort* = 9090
|
|
|
|
|
2020-04-15 02:41:22 +00:00
|
|
|
func getBeaconBlocksTopic*(forkDigest: ForkDigest): string =
|
2020-04-22 05:53:02 +00:00
|
|
|
try:
|
2020-05-11 18:08:52 +00:00
|
|
|
&"/eth2/{$forkDigest}/{topicBeaconBlocksSuffix}"
|
2020-04-22 05:53:02 +00:00
|
|
|
except ValueError as e:
|
|
|
|
raiseAssert e.msg
|
2020-04-15 02:41:22 +00:00
|
|
|
|
2020-04-15 03:11:45 +00:00
|
|
|
func getVoluntaryExitsTopic*(forkDigest: ForkDigest): string =
|
2020-04-22 05:53:02 +00:00
|
|
|
try:
|
2020-05-11 18:08:52 +00:00
|
|
|
&"/eth2/{$forkDigest}/{topicVoluntaryExitsSuffix}"
|
2020-04-22 05:53:02 +00:00
|
|
|
except ValueError as e:
|
|
|
|
raiseAssert e.msg
|
2020-04-15 02:41:22 +00:00
|
|
|
|
|
|
|
func getProposerSlashingsTopic*(forkDigest: ForkDigest): string =
|
2020-04-22 05:53:02 +00:00
|
|
|
try:
|
2020-05-11 18:08:52 +00:00
|
|
|
&"/eth2/{$forkDigest}/{topicProposerSlashingsSuffix}"
|
2020-04-22 05:53:02 +00:00
|
|
|
except ValueError as e:
|
|
|
|
raiseAssert e.msg
|
2020-04-15 02:41:22 +00:00
|
|
|
|
|
|
|
func getAttesterSlashingsTopic*(forkDigest: ForkDigest): string =
|
2020-04-22 05:53:02 +00:00
|
|
|
try:
|
2020-05-11 18:08:52 +00:00
|
|
|
&"/eth2/{$forkDigest}/{topicAttesterSlashingsSuffix}"
|
2020-04-22 05:53:02 +00:00
|
|
|
except ValueError as e:
|
|
|
|
raiseAssert e.msg
|
2020-04-15 02:41:22 +00:00
|
|
|
|
|
|
|
func getAggregateAndProofsTopic*(forkDigest: ForkDigest): string =
|
2020-04-22 05:53:02 +00:00
|
|
|
try:
|
2020-05-11 18:08:52 +00:00
|
|
|
&"/eth2/{$forkDigest}/{topicAggregateAndProofsSuffix}"
|
2020-04-22 05:53:02 +00:00
|
|
|
except ValueError as e:
|
|
|
|
raiseAssert e.msg
|
2020-04-15 02:41:22 +00:00
|
|
|
|
2020-09-24 17:04:10 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.3/specs/phase0/validator.md#broadcast-attestation
|
2020-06-29 18:08:58 +00:00
|
|
|
func compute_subnet_for_attestation*(
|
2020-07-22 08:04:21 +00:00
|
|
|
committees_per_slot: uint64, slot: Slot, committee_index: CommitteeIndex):
|
|
|
|
uint64 =
|
2020-06-29 18:08:58 +00:00
|
|
|
# Compute the correct subnet for an attestation for Phase 0.
|
|
|
|
# Note, this mimics expected Phase 1 behavior where attestations will be
|
|
|
|
# mapped to their shard subnet.
|
|
|
|
let
|
2020-07-22 08:04:21 +00:00
|
|
|
slots_since_epoch_start = slot mod SLOTS_PER_EPOCH
|
2020-06-29 18:08:58 +00:00
|
|
|
committees_since_epoch_start =
|
2020-07-22 08:04:21 +00:00
|
|
|
committees_per_slot * slots_since_epoch_start
|
2020-06-29 18:08:58 +00:00
|
|
|
|
2020-07-22 08:04:21 +00:00
|
|
|
(committees_since_epoch_start + committee_index.uint64) mod
|
|
|
|
ATTESTATION_SUBNET_COUNT
|
2020-06-29 18:08:58 +00:00
|
|
|
|
2020-09-24 17:04:10 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.3/specs/phase0/validator.md#broadcast-attestation
|
2020-06-29 18:08:58 +00:00
|
|
|
func getAttestationTopic*(forkDigest: ForkDigest, subnetIndex: uint64):
|
|
|
|
string =
|
2020-08-17 12:07:29 +00:00
|
|
|
## For subscribing and unsubscribing to/from a subnet.
|
2020-07-22 08:04:21 +00:00
|
|
|
doAssert subnetIndex < ATTESTATION_SUBNET_COUNT
|
|
|
|
|
2020-04-22 05:53:02 +00:00
|
|
|
try:
|
2020-06-29 18:08:58 +00:00
|
|
|
&"/eth2/{$forkDigest}/beacon_attestation_{subnetIndex}/ssz"
|
2020-04-22 05:53:02 +00:00
|
|
|
except ValueError as e:
|
|
|
|
raiseAssert e.msg
|
2020-06-18 13:10:25 +00:00
|
|
|
|
2020-07-23 17:01:07 +00:00
|
|
|
func getAttestationTopic*(forkDigest: ForkDigest,
|
|
|
|
attestation: Attestation,
|
|
|
|
num_active_validators: uint64): string =
|
2020-06-29 18:08:58 +00:00
|
|
|
getAttestationTopic(
|
|
|
|
forkDigest,
|
2020-07-22 08:04:21 +00:00
|
|
|
compute_subnet_for_attestation(
|
2020-07-23 17:01:07 +00:00
|
|
|
get_committee_count_per_slot(num_active_validators),
|
2020-07-22 08:04:21 +00:00
|
|
|
attestation.data.slot, attestation.data.index.CommitteeIndex))
|
2020-07-27 16:04:44 +00:00
|
|
|
|
2020-08-11 15:08:44 +00:00
|
|
|
func get_committee_assignments(
|
2020-07-27 16:04:44 +00:00
|
|
|
state: BeaconState, epoch: Epoch,
|
|
|
|
validator_indices: HashSet[ValidatorIndex]):
|
|
|
|
seq[tuple[subnetIndex: uint64, slot: Slot]] =
|
|
|
|
var cache = StateCache()
|
|
|
|
|
|
|
|
let
|
|
|
|
committees_per_slot = get_committee_count_per_slot(state, epoch, cache)
|
|
|
|
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
|
|
|
|
if not disjoint(validator_indices,
|
|
|
|
get_beacon_committee(state, slot, idx, cache).toHashSet):
|
|
|
|
result.add(
|
|
|
|
(compute_subnet_for_attestation(committees_per_slot, slot, idx),
|
|
|
|
slot))
|
2020-08-12 17:48:31 +00:00
|
|
|
|
2020-09-24 17:04:10 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.3/specs/phase0/validator.md#phase-0-attestation-subnet-stability
|
2020-08-12 17:48:31 +00:00
|
|
|
proc getStabilitySubnetLength*(): uint64 =
|
|
|
|
EPOCHS_PER_RANDOM_SUBNET_SUBSCRIPTION +
|
|
|
|
rand(EPOCHS_PER_RANDOM_SUBNET_SUBSCRIPTION.int).uint64
|
|
|
|
|
|
|
|
proc get_attestation_subnet_changes*(
|
|
|
|
state: BeaconState, attachedValidators: openarray[ValidatorIndex],
|
|
|
|
prevAttestationSubnets: AttestationSubnets, epoch: Epoch):
|
|
|
|
tuple[a: AttestationSubnets, b: set[uint8], c: set[uint8]] =
|
2020-10-01 14:48:24 +00:00
|
|
|
static: doAssert ATTESTATION_SUBNET_COUNT == 64 # Fits in a set[uint8]
|
|
|
|
doAssert attachedValidators.len > 0
|
2020-08-12 17:48:31 +00:00
|
|
|
|
|
|
|
var attestationSubnets = prevAttestationSubnets
|
|
|
|
|
2020-09-24 17:04:10 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.3/specs/phase0/validator.md#phase-0-attestation-subnet-stability
|
2020-08-12 17:48:31 +00:00
|
|
|
let prevStabilitySubnet = {attestationSubnets.stabilitySubnet.uint8}
|
|
|
|
if epoch >= attestationSubnets.stabilitySubnetExpirationEpoch:
|
|
|
|
attestationSubnets.stabilitySubnet =
|
|
|
|
rand(ATTESTATION_SUBNET_COUNT - 1).uint64
|
|
|
|
attestationSubnets.stabilitySubnetExpirationEpoch =
|
|
|
|
epoch + getStabilitySubnetLength()
|
|
|
|
|
|
|
|
var nextEpochSubnets: set[uint8]
|
|
|
|
for it in get_committee_assignments(
|
|
|
|
state, state.slot.epoch + 1, attachedValidators.toHashSet):
|
|
|
|
nextEpochSubnets.incl it.subnetIndex.uint8
|
|
|
|
|
2020-10-01 14:48:24 +00:00
|
|
|
doAssert nextEpochSubnets.len <= attachedValidators.len
|
2020-08-12 17:48:31 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
epochParity = epoch mod 2
|
|
|
|
stabilitySet = {attestationSubnets.stabilitySubnet.uint8}
|
|
|
|
currentEpochSubnets = attestationSubnets.subscribedSubnets[1 - epochParity]
|
|
|
|
|
|
|
|
expiringSubnets =
|
|
|
|
(prevStabilitySubnet +
|
|
|
|
attestationSubnets.subscribedSubnets[epochParity]) -
|
|
|
|
nextEpochSubnets - currentEpochSubnets - stabilitySet
|
|
|
|
newSubnets =
|
|
|
|
(nextEpochSubnets + stabilitySet) -
|
|
|
|
(currentEpochSubnets + prevStabilitySubnet)
|
|
|
|
|
|
|
|
doAssert newSubnets.len <= attachedValidators.len + 1
|
|
|
|
|
|
|
|
attestationSubnets.subscribedSubnets[epochParity] = newSubnets
|
|
|
|
(attestationSubnets, expiringSubnets, newSubnets)
|