rm beacon_chain/networking/network_metadata_mainnet.S

This commit is contained in:
Jenkins 2024-03-20 04:43:26 +02:00
parent 28a429ceb2
commit ae9737ea0a
2 changed files with 27 additions and 199 deletions

View File

@ -6,31 +6,16 @@ import
eth/common/eth_types_json_serialization, eth/common/eth_types_json_serialization,
../spec/[eth2_ssz_serialization, forks] ../spec/[eth2_ssz_serialization, forks]
# TODO(zah):
# We can compress the embedded states with snappy before embedding them here.
# ATTENTION! This file is intentionally avoiding the Nim `/` operator for
# constructing paths. The standard operator is relying the `DirSep` constant
# which depends on the selected target OS (when doing cross-compilation), so
# the compile-time manipulation of paths performed here will break (e.g. when
# cross-compiling for Windows from Linux)
#
# Nim seems to need a more general solution for detecting the host OS during
# compilation, so a host OS specific separator can be used when deriving paths
# from `currentSourcePath`.
export export
web3types, conversions, RuntimeConfig web3types, conversions, RuntimeConfig
const const
vendorDir = currentSourcePath.parentDir.replace('\\', '/') & "/../../vendor" vendorDir = currentSourcePath.parentDir.replace('\\', '/') & "/../../vendor"
incbinEnabled* = sizeof(pointer) == 8
type type
Eth1BlockHash* = web3types.BlockHash Eth1BlockHash = web3types.BlockHash
Eth1Network* = enum Eth1Network = enum
mainnet mainnet
goerli goerli
sepolia sepolia
@ -42,44 +27,38 @@ type
BakedIn BakedIn
BakedInUrl BakedInUrl
DownloadInfo* = object DownloadInfo = object
url: string url: string
digest: Eth2Digest digest: Eth2Digest
GenesisMetadata* = object GenesisMetadata = object
case kind*: GenesisMetadataKind case kind*: GenesisMetadataKind
of NoGenesis: of NoGenesis:
discard discard
of UserSuppliedFile: of UserSuppliedFile:
path*: string path: string
of BakedIn: of BakedIn:
networkName*: string networkName: string
of BakedInUrl: of BakedInUrl:
url*: string url: string
digest*: Eth2Digest digest: Eth2Digest
Eth2NetworkMetadata* = object Eth2NetworkMetadata* = object
# If the eth1Network is specified, the ELManager will perform some eth1Network: Option[Eth1Network]
# additional checks to ensure we are connecting to a web3 provider
# serving data for the same network. The value can be set to `None`
# for custom networks and testing purposes.
eth1Network*: Option[Eth1Network]
cfg*: RuntimeConfig cfg*: RuntimeConfig
# Parsing `enr.Records` is still not possible at compile-time bootstrapNodes: seq[string]
bootstrapNodes*: seq[string]
depositContractBlock*: uint64 depositContractBlock: uint64
depositContractBlockHash*: Eth2Digest depositContractBlockHash: Eth2Digest
genesis*: GenesisMetadata genesis*: GenesisMetadata
genesisDepositsSnapshot*: string genesisDepositsSnapshot: string
func hasGenesis*(metadata: Eth2NetworkMetadata): bool = func hasGenesis*(metadata: Eth2NetworkMetadata): bool =
metadata.genesis.kind != NoGenesis metadata.genesis.kind != NoGenesis
proc readBootstrapNodes*(path: string): seq[string] {.raises: [IOError].} = proc readBootstrapNodes(path: string): seq[string] {.raises: [IOError].} =
# Read a list of ENR values from a YAML file containing a flat list of entries
if fileExists(path): if fileExists(path):
splitLines(readFile(path)). splitLines(readFile(path)).
filterIt(it.startsWith("enr:")). filterIt(it.startsWith("enr:")).
@ -87,8 +66,7 @@ proc readBootstrapNodes*(path: string): seq[string] {.raises: [IOError].} =
else: else:
@[] @[]
proc readBootEnr*(path: string): seq[string] {.raises: [IOError].} = proc readBootEnr(path: string): seq[string] {.raises: [IOError].} =
# Read a list of ENR values from a YAML file containing a flat list of entries
if fileExists(path): if fileExists(path):
splitLines(readFile(path)). splitLines(readFile(path)).
filterIt(it.startsWith("- enr:")). filterIt(it.startsWith("- enr:")).
@ -103,8 +81,6 @@ proc loadEth2NetworkMetadata*(
downloadGenesisFrom = none(DownloadInfo), downloadGenesisFrom = none(DownloadInfo),
useBakedInGenesis = none(string) useBakedInGenesis = none(string)
): Result[Eth2NetworkMetadata, string] {.raises: [IOError, PresetFileError].} = ): Result[Eth2NetworkMetadata, string] {.raises: [IOError, PresetFileError].} =
# Load data in eth2-networks format
# https://github.com/eth-clients/eth2-networks
try: try:
let let
@ -224,79 +200,15 @@ proc loadCompileTimeNetworkMetadata(
else: else:
macros.error "config.yaml not found for network '" & path macros.error "config.yaml not found for network '" & path
when const_preset == "gnosis": when const_preset == "mainnet":
when incbinEnabled:
let
gnosisGenesis* {.importc: "gnosis_mainnet_genesis".}: ptr UncheckedArray[byte]
gnosisGenesisSize* {.importc: "gnosis_mainnet_genesis_size".}: int
chiadoGenesis* {.importc: "gnosis_chiado_genesis".}: ptr UncheckedArray[byte]
chiadoGenesisSize* {.importc: "gnosis_chiado_genesis_size".}: int
# let `.incbin` in assembly file find the binary file through search path
{.passc: "-I" & vendorDir.}
{.compile: "network_metadata_gnosis.S".}
else:
const const
gnosisGenesis* = slurp( mainnetGenesis = slurp(
vendorDir & "/gnosis-chain-configs/mainnet/genesis.ssz")
chiadoGenesis* = slurp(
vendorDir & "/gnosis-chain-configs/chiado/genesis.ssz")
const
gnosisMetadata = loadCompileTimeNetworkMetadata(
vendorDir & "/gnosis-chain-configs/mainnet",
none(Eth1Network),
useBakedInGenesis = some "gnosis")
chiadoMetadata = loadCompileTimeNetworkMetadata(
vendorDir & "/gnosis-chain-configs/chiado",
none(Eth1Network),
useBakedInGenesis = some "chiado")
static:
for network in [gnosisMetadata, chiadoMetadata]:
checkForkConsistency(network.cfg)
for network in [gnosisMetadata, chiadoMetadata]:
doAssert network.cfg.ALTAIR_FORK_EPOCH < FAR_FUTURE_EPOCH
doAssert network.cfg.BELLATRIX_FORK_EPOCH < FAR_FUTURE_EPOCH
doAssert network.cfg.CAPELLA_FORK_EPOCH < FAR_FUTURE_EPOCH
doAssert network.cfg.DENEB_FORK_EPOCH < FAR_FUTURE_EPOCH
doAssert network.cfg.ELECTRA_FORK_EPOCH == FAR_FUTURE_EPOCH
static: doAssert ConsensusFork.high == ConsensusFork.Electra
elif const_preset == "mainnet":
when incbinEnabled:
# Nim is very inefficent at loading large constants from binary files so we
# use this trick instead which saves significant amounts of compile time
{.push hint[GlobalVar]:off.}
let
mainnetGenesis* {.importc: "eth2_mainnet_genesis".}: ptr UncheckedArray[byte]
mainnetGenesisSize* {.importc: "eth2_mainnet_genesis_size".}: int
praterGenesis* {.importc: "eth2_goerli_genesis".}: ptr UncheckedArray[byte]
praterGenesisSize* {.importc: "eth2_goerli_genesis_size".}: int
sepoliaGenesis* {.importc: "eth2_sepolia_genesis".}: ptr UncheckedArray[byte]
sepoliaGenesisSize* {.importc: "eth2_sepolia_genesis_size".}: int
{.pop.}
# let `.incbin` in assembly file find the binary file through search path
{.passc: "-I" & vendorDir.}
{.compile: "network_metadata_mainnet.S".}
else:
const
mainnetGenesis* = slurp(
vendorDir & "/eth2-networks/shared/mainnet/genesis.ssz") vendorDir & "/eth2-networks/shared/mainnet/genesis.ssz")
praterGenesis* = slurp( praterGenesis = slurp(
vendorDir & "/goerli/prater/genesis.ssz") vendorDir & "/goerli/prater/genesis.ssz")
sepoliaGenesis* = slurp( sepoliaGenesis = slurp(
vendorDir & "/sepolia/bepolia/genesis.ssz") vendorDir & "/sepolia/bepolia/genesis.ssz")
const const
@ -378,15 +290,7 @@ proc getMetadataForNetwork*(networkName: string): Eth2NetworkMetadata =
metadata metadata
proc getRuntimeConfig*(eth2Network: Option[string]): RuntimeConfig = proc getRuntimeConfig(eth2Network: Option[string]): RuntimeConfig =
## Returns the run-time config for a network specified on the command line
## If the network is not explicitly specified, the function will act as the
## regular Nimbus binary, returning the mainnet config.
##
## TODO the assumption that the input variable is a CLI config option is not
## quite appropriate in such as low-level function. The "assume mainnet by
## default" behavior is something that should be handled closer to the `conf`
## layer.
let metadata = let metadata =
if eth2Network.isSome: if eth2Network.isSome:
getMetadataForNetwork(eth2Network.get) getMetadataForNetwork(eth2Network.get)
@ -405,9 +309,6 @@ proc getRuntimeConfig*(eth2Network: Option[string]): RuntimeConfig =
when const_preset in ["mainnet", "gnosis"]: when const_preset in ["mainnet", "gnosis"]:
template bakedInGenesisStateAsBytes(networkName: untyped): untyped = template bakedInGenesisStateAsBytes(networkName: untyped): untyped =
when incbinEnabled:
`networkName Genesis`.toOpenArray(0, `networkName GenesisSize` - 1)
else:
`networkName Genesis`.toOpenArrayByte(0, `networkName Genesis`.high) `networkName Genesis`.toOpenArrayByte(0, `networkName Genesis`.high)
const const
@ -419,7 +320,7 @@ when const_preset in ["mainnet", "gnosis"]:
"Baked-in genesis states for the Gnosis network " & "Baked-in genesis states for the Gnosis network " &
"are available only in the gnosis build of Nimbus" "are available only in the gnosis build of Nimbus"
template bakedBytes*(metadata: GenesisMetadata): auto = template bakedBytes(metadata: GenesisMetadata): auto =
case metadata.networkName case metadata.networkName
of "mainnet": of "mainnet":
when const_preset == "mainnet": when const_preset == "mainnet":
@ -462,10 +363,10 @@ when const_preset in ["mainnet", "gnosis"]:
else: else:
Opt.none Eth2Digest Opt.none Eth2Digest
else: else:
func bakedBytes*(metadata: GenesisMetadata): seq[byte] = func bakedBytes(metadata: GenesisMetadata): seq[byte] =
raiseAssert "Baked genesis states are not available in the current build mode" raiseAssert "Baked genesis states are not available in the current build mode"
func bakedGenesisValidatorsRoot*(metadata: Eth2NetworkMetadata): Opt[Eth2Digest] = func bakedGenesisValidatorsRoot(metadata: Eth2NetworkMetadata): Opt[Eth2Digest] =
Opt.none Eth2Digest Opt.none Eth2Digest
import stew/io2 import stew/io2

View File

@ -1,73 +0,0 @@
# beacon_chain
# Copyright (c) 2023-2024 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.
#if defined(__APPLE__)
# define cdecl(s) _##s
#else
# define cdecl(s) s
#endif
#if defined(__linux__)
.section .note.GNU-stack, "", @progbits
.section .rodata,"a",@progbits
#elif defined(__APPLE__)
.section __TEXT,__const
#elif defined(__WIN32__)
.section .rdata,"dr"
#else
.text
#endif
# name_data = start of data
# name_end = end of data (without alignment)
# name = 64-bit pointer to data
# name_size = 64-bit length in bytes
eth2_mainnet_genesis_data:
.incbin "eth2-networks/shared/mainnet/genesis.ssz"
eth2_mainnet_genesis_end:
.global cdecl(eth2_mainnet_genesis_size)
.p2align 3
cdecl(eth2_mainnet_genesis_size):
.quad eth2_mainnet_genesis_end - eth2_mainnet_genesis_data
eth2_goerli_genesis_data:
.incbin "goerli/prater/genesis.ssz"
eth2_goerli_genesis_end:
.global cdecl(eth2_goerli_genesis_size)
.p2align 3
cdecl(eth2_goerli_genesis_size):
.quad eth2_goerli_genesis_end - eth2_goerli_genesis_data
eth2_sepolia_genesis_data:
.incbin "sepolia/bepolia/genesis.ssz"
eth2_sepolia_genesis_end:
.global cdecl(eth2_sepolia_genesis_size)
.p2align 3
cdecl(eth2_sepolia_genesis_size):
.quad eth2_sepolia_genesis_end - eth2_sepolia_genesis_data
#if defined(__linux__) && (defined(__pie__) || defined(__pic__))
.section .data.rel.ro,"aw",@progbits
#elif defined(__APPLE__)
.section __DATA,__const
#endif
.global cdecl(eth2_mainnet_genesis)
.p2align 3
cdecl(eth2_mainnet_genesis):
.quad eth2_mainnet_genesis_data
.global cdecl(eth2_goerli_genesis)
.p2align 3
cdecl(eth2_goerli_genesis):
.quad eth2_goerli_genesis_data
.global cdecl(eth2_sepolia_genesis)
.p2align 3
cdecl(eth2_sepolia_genesis):
.quad eth2_sepolia_genesis_data