mirror of
https://github.com/status-im/nimbus-eth2.git
synced 2025-01-11 14:54:12 +00:00
867d8f3223
Currently, we have a bit of a convoluted flow where when sending attestations, we start broadcasting them over gossip then pass them to the attestation validation to include them in the local attestation pool - it should be the other way around: we should be checking attestations _before_ gossipping them - this serves as an additional safety net to ensure that we don't publish junk - this becomes more important when publishing attestations from the API. Also, the REST API was performing its own validation meaning attestations coming from REST would be validated twice - finally, the JSON RPC wasn't pre-validating and would happily broadcast invalid attestations. * Unified attestation production pipeline with the same flow for gossip, locally and API-produced attestations: all are now validated and entered into the pool, then broadcast/republished * Refactor subnet handling with specific SubnetId alias, streamlining where subnets are computed, avoiding the need to pass around the number of active validators * Move some of the subnet handling code to eth2_network * Use BitArray throughout for subnet handling
85 lines
3.4 KiB
Nim
85 lines
3.4 KiB
Nim
# beacon_chain
|
|
# 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).
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
import
|
|
"."/[datatypes, digest, helpers]
|
|
|
|
const
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/p2p-interface.md#topics-and-messages
|
|
topicBeaconBlocksSuffix* = "beacon_block/ssz"
|
|
topicVoluntaryExitsSuffix* = "voluntary_exit/ssz"
|
|
topicProposerSlashingsSuffix* = "proposer_slashing/ssz"
|
|
topicAttesterSlashingsSuffix* = "attester_slashing/ssz"
|
|
topicAggregateAndProofsSuffix* = "beacon_aggregate_and_proof/ssz"
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/p2p-interface.md#eth2-network-interaction-domains
|
|
MAX_CHUNK_SIZE* = 1 * 1024 * 1024 # bytes
|
|
GOSSIP_MAX_SIZE* = 1 * 1024 * 1024 # bytes
|
|
TTFB_TIMEOUT* = 5.seconds
|
|
RESP_TIMEOUT* = 10.seconds
|
|
|
|
defaultEth2TcpPort* = 9000
|
|
|
|
# This is not part of the spec yet! Keep in sync with BASE_RPC_PORT
|
|
defaultEth2RpcPort* = 9190
|
|
|
|
# This is not part of the spec! But its port which uses Lighthouse
|
|
DefaultEth2RestPort* = 5052
|
|
|
|
template eth2Prefix(forkDigest: ForkDigest): string =
|
|
"/eth2/" & $forkDigest & "/"
|
|
|
|
func getBeaconBlocksTopic*(forkDigest: ForkDigest): string =
|
|
eth2Prefix(forkDigest) & topicBeaconBlocksSuffix
|
|
|
|
func getVoluntaryExitsTopic*(forkDigest: ForkDigest): string =
|
|
eth2Prefix(forkDigest) & topicVoluntaryExitsSuffix
|
|
|
|
func getProposerSlashingsTopic*(forkDigest: ForkDigest): string =
|
|
eth2Prefix(forkDigest) & topicProposerSlashingsSuffix
|
|
|
|
func getAttesterSlashingsTopic*(forkDigest: ForkDigest): string =
|
|
eth2Prefix(forkDigest) & topicAttesterSlashingsSuffix
|
|
|
|
func getAggregateAndProofsTopic*(forkDigest: ForkDigest): string =
|
|
eth2Prefix(forkDigest) & topicAggregateAndProofsSuffix
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/validator.md#broadcast-attestation
|
|
func compute_subnet_for_attestation*(
|
|
committees_per_slot: uint64, slot: Slot, committee_index: CommitteeIndex):
|
|
SubnetId =
|
|
# 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
|
|
slots_since_epoch_start = slot mod SLOTS_PER_EPOCH
|
|
committees_since_epoch_start =
|
|
committees_per_slot * slots_since_epoch_start
|
|
|
|
SubnetId(
|
|
(committees_since_epoch_start + committee_index.uint64) mod
|
|
ATTESTATION_SUBNET_COUNT)
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/validator.md#broadcast-attestation
|
|
func getAttestationTopic*(forkDigest: ForkDigest, subnet_id: SubnetId):
|
|
string =
|
|
## For subscribing and unsubscribing to/from a subnet.
|
|
eth2Prefix(forkDigest) & "beacon_attestation_" & $uint64(subnet_id) & "/ssz"
|
|
|
|
func getENRForkID*(fork: Fork, genesis_validators_root: Eth2Digest): ENRForkID =
|
|
let
|
|
current_fork_version = fork.current_version
|
|
fork_digest = compute_fork_digest(
|
|
current_fork_version, genesis_validators_root)
|
|
|
|
ENRForkID(
|
|
fork_digest: fork_digest,
|
|
next_fork_version: current_fork_version,
|
|
next_fork_epoch: FAR_FUTURE_EPOCH)
|