2019-10-01 13:52:28 +00:00
|
|
|
# beacon_chain
|
2021-01-14 08:43:21 +00:00
|
|
|
# Copyright (c) 2018-2021 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
|
2021-08-12 13:08:20 +00:00
|
|
|
"."/[helpers, forks],
|
2021-06-21 08:35:24 +00:00
|
|
|
"."/datatypes/base
|
2020-02-21 12:16:58 +00:00
|
|
|
|
2021-08-12 13:08:20 +00:00
|
|
|
export base
|
|
|
|
|
2019-10-01 13:52:28 +00:00
|
|
|
const
|
2021-10-14 06:30:21 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/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
|
|
|
|
2021-08-20 23:37:45 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.0.1/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
|
|
|
|
|
2020-11-12 10:46:02 +00:00
|
|
|
# This is not part of the spec yet! Keep in sync with BASE_RPC_PORT
|
|
|
|
defaultEth2RpcPort* = 9190
|
2020-03-16 22:28:54 +00:00
|
|
|
|
2021-03-29 10:59:39 +00:00
|
|
|
# This is not part of the spec! But its port which uses Lighthouse
|
|
|
|
DefaultEth2RestPort* = 5052
|
|
|
|
|
2021-08-10 06:19:13 +00:00
|
|
|
enrAttestationSubnetsField* = "attnets"
|
|
|
|
enrSyncSubnetsField* = "syncnets"
|
|
|
|
enrForkIdField* = "eth2"
|
|
|
|
|
2021-05-04 15:28:48 +00:00
|
|
|
template eth2Prefix(forkDigest: ForkDigest): string =
|
|
|
|
"/eth2/" & $forkDigest & "/"
|
|
|
|
|
2020-04-15 02:41:22 +00:00
|
|
|
func getBeaconBlocksTopic*(forkDigest: ForkDigest): string =
|
2021-05-04 15:28:48 +00:00
|
|
|
eth2Prefix(forkDigest) & topicBeaconBlocksSuffix
|
2020-04-15 02:41:22 +00:00
|
|
|
|
2020-04-15 03:11:45 +00:00
|
|
|
func getVoluntaryExitsTopic*(forkDigest: ForkDigest): string =
|
2021-05-04 15:28:48 +00:00
|
|
|
eth2Prefix(forkDigest) & topicVoluntaryExitsSuffix
|
2020-04-15 02:41:22 +00:00
|
|
|
|
|
|
|
func getProposerSlashingsTopic*(forkDigest: ForkDigest): string =
|
2021-05-04 15:28:48 +00:00
|
|
|
eth2Prefix(forkDigest) & topicProposerSlashingsSuffix
|
2020-04-15 02:41:22 +00:00
|
|
|
|
|
|
|
func getAttesterSlashingsTopic*(forkDigest: ForkDigest): string =
|
2021-05-04 15:28:48 +00:00
|
|
|
eth2Prefix(forkDigest) & topicAttesterSlashingsSuffix
|
2020-04-15 02:41:22 +00:00
|
|
|
|
|
|
|
func getAggregateAndProofsTopic*(forkDigest: ForkDigest): string =
|
2021-05-04 15:28:48 +00:00
|
|
|
eth2Prefix(forkDigest) & topicAggregateAndProofsSuffix
|
2020-04-15 02:41:22 +00:00
|
|
|
|
2021-08-20 23:37:45 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.0.1/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):
|
2021-05-10 07:13:36 +00:00
|
|
|
SubnetId =
|
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
|
|
|
|
2021-05-10 07:13:36 +00:00
|
|
|
SubnetId(
|
|
|
|
(committees_since_epoch_start + committee_index.uint64) mod
|
|
|
|
ATTESTATION_SUBNET_COUNT)
|
2020-06-29 18:08:58 +00:00
|
|
|
|
2021-10-12 10:17:37 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/validator.md#broadcast-attestation
|
2021-08-10 06:19:13 +00:00
|
|
|
func getAttestationTopic*(forkDigest: ForkDigest,
|
|
|
|
subnetId: SubnetId): string =
|
2020-08-17 12:07:29 +00:00
|
|
|
## For subscribing and unsubscribing to/from a subnet.
|
2021-08-10 06:19:13 +00:00
|
|
|
eth2Prefix(forkDigest) & "beacon_attestation_" & $(subnetId) & "/ssz"
|
2021-05-04 15:28:48 +00:00
|
|
|
|
2021-08-20 23:37:45 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.0-alpha.8/specs/altair/p2p-interface.md#topics-and-messages
|
2021-08-15 14:50:31 +00:00
|
|
|
func getSyncCommitteeTopic*(forkDigest: ForkDigest,
|
|
|
|
committeeIdx: SyncCommitteeIndex): string =
|
|
|
|
## For subscribing and unsubscribing to/from a subnet.
|
|
|
|
eth2Prefix(forkDigest) & "sync_committee_" & $(committeeIdx.asUInt8) & "/ssz"
|
|
|
|
|
2021-10-12 10:17:37 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/altair/p2p-interface.md#topics-and-messages
|
2021-08-15 14:50:31 +00:00
|
|
|
func getSyncCommitteeContributionAndProofTopic*(forkDigest: ForkDigest): string =
|
|
|
|
## For subscribing and unsubscribing to/from a subnet.
|
|
|
|
eth2Prefix(forkDigest) & "sync_committee_contribution_and_proof" & "/ssz"
|
|
|
|
|
2021-08-10 06:19:13 +00:00
|
|
|
func getENRForkID*(cfg: RuntimeConfig,
|
|
|
|
epoch: Epoch,
|
2021-07-07 09:09:47 +00:00
|
|
|
genesis_validators_root: Eth2Digest): ENRForkID =
|
2021-05-04 15:28:48 +00:00
|
|
|
let
|
2021-08-10 06:19:13 +00:00
|
|
|
current_fork_version = cfg.forkVersionAtEpoch(epoch)
|
2021-09-30 01:07:24 +00:00
|
|
|
next_fork_version = if cfg.nextForkEpochAtEpoch(epoch) == FAR_FUTURE_EPOCH:
|
2021-08-20 13:04:29 +00:00
|
|
|
current_fork_version
|
|
|
|
else:
|
2021-09-30 01:07:24 +00:00
|
|
|
cfg.forkVersionAtEpoch(cfg.nextForkEpochAtEpoch(epoch))
|
2021-08-10 06:19:13 +00:00
|
|
|
fork_digest = compute_fork_digest(current_fork_version,
|
|
|
|
genesis_validators_root)
|
2021-05-04 15:28:48 +00:00
|
|
|
ENRForkID(
|
|
|
|
fork_digest: fork_digest,
|
2021-08-20 13:04:29 +00:00
|
|
|
next_fork_version: next_fork_version,
|
2021-08-10 06:19:13 +00:00
|
|
|
next_fork_epoch: cfg.nextForkEpochAtEpoch(epoch))
|
2021-09-29 11:06:16 +00:00
|
|
|
|
|
|
|
func getDiscoveryForkID*(cfg: RuntimeConfig,
|
|
|
|
epoch: Epoch,
|
|
|
|
genesis_validators_root: Eth2Digest): ENRForkID =
|
|
|
|
# Until 1 epoch from fork, returns pre-fork value
|
|
|
|
if epoch + 1 >= cfg.ALTAIR_FORK_EPOCH:
|
|
|
|
getENRForkID(cfg, epoch, genesis_validators_root)
|
|
|
|
else:
|
|
|
|
let
|
|
|
|
current_fork_version = cfg.forkVersionAtEpoch(epoch)
|
|
|
|
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)
|