Skip to content

Commit

Permalink
Disable the use of incbin as it breaks the retail build on Linux/ARM
Browse files Browse the repository at this point in the history
  • Loading branch information
zah committed May 19, 2023
1 parent a36cacd commit 4c3850f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 36 deletions.
84 changes: 53 additions & 31 deletions beacon_chain/networking/network_metadata.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ import
export
ethtypes, conversions, RuntimeConfig

const
vendorDir = currentSourcePath.parentDir.replace('\\', '/') & "/../../vendor"

# TODO: Currently, this breaks the Linux/ARM packaging due
# to a toolchain incompatibility problem
incbinEnabled* = false

type
Eth1BlockHash* = ethtypes.BlockHash

Expand Down Expand Up @@ -64,11 +71,21 @@ type

# `genesisData` will have `len == 0` for networks with a still
# unknown genesis state.
genesisData*: seq[byte]
genesisDepositsSnapshot*: seq[byte]
when incbinEnabled:
genesisData*: seq[byte]
else:
genesisData*: string

genesisDepositsSnapshot*: string
else:
incompatibilityDesc*: string

template genesisBytes*(metadata: Eth2NetworkMetadata): auto =
when incbinEnabled:
metadata.genesisData
else:
metadata.genesisData.toOpenArrayByte(0, metadata.genesisData.high)

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):
Expand Down Expand Up @@ -157,14 +174,17 @@ proc loadEth2NetworkMetadata*(
readBootEnr(bootEnrPath))

genesisData = if loadGenesis and fileExists(genesisPath):
toBytes(readFile(genesisPath))
when incbinEnabled:
toBytes readFile(genesisPath)
else:
readFile(genesisPath)
else:
@[]
""

genesisDepositsSnapshot = if fileExists(genesisDepositsSnapshotPath):
toBytes(readFile(genesisDepositsSnapshotPath))
readFile(genesisDepositsSnapshotPath)
else:
@[]
""

Eth2NetworkMetadata(
incompatible: false,
Expand Down Expand Up @@ -196,21 +216,19 @@ proc loadCompileTimeNetworkMetadata(
else:
macros.error "config.yaml not found for network '" & path

const vendorDir =
currentSourcePath.parentDir.replace('\\', '/') & "/../../vendor"

when const_preset == "gnosis":
import stew/assign2

let
gnosisGenesis {.importc: "gnosis_mainnet_genesis".}: ptr UncheckedArray[byte]
gnosisGenesisSize {.importc: "gnosis_mainnet_genesis_size".}: int

{.compile: "network_metadata_gnosis.S".}
when incbinEnabled:
let
gnosisGenesis {.importc: "gnosis_mainnet_genesis".}: ptr UncheckedArray[byte]
gnosisGenesisSize {.importc: "gnosis_mainnet_genesis_size".}: int
{.compile: "network_metadata_gnosis.S".}

const
gnosisMetadata = loadCompileTimeNetworkMetadata(
vendorDir & "/gnosis-chain-configs/mainnet")
vendorDir & "/gnosis-chain-configs/mainnet",
none(Eth1Network), not incbinEnabled)

static:
checkForkConsistency(gnosisMetadata.cfg)
Expand All @@ -220,30 +238,31 @@ when const_preset == "gnosis":
elif const_preset == "mainnet":
import stew/assign2

# Nim is very inefficent at loading large constants from binary files so we
# use this trick instead which saves significant amounts of compile time
let
mainnetGenesis {.importc: "eth2_mainnet_genesis".}: ptr UncheckedArray[byte]
mainnetGenesisSize {.importc: "eth2_mainnet_genesis_size".}: int
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
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
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
sepoliaGenesis {.importc: "eth2_sepolia_genesis".}: ptr UncheckedArray[byte]
sepoliaGenesisSize {.importc: "eth2_sepolia_genesis_size".}: int

{.compile: "network_metadata_mainnet.S".}
{.compile: "network_metadata_mainnet.S".}

const
eth2NetworksDir = vendorDir & "/eth2-networks"
sepoliaDir = vendorDir & "/sepolia"

mainnetMetadata = loadCompileTimeNetworkMetadata(
vendorDir & "/eth2-networks/shared/mainnet", some mainnet, false)
vendorDir & "/eth2-networks/shared/mainnet", some mainnet, not incbinEnabled)
praterMetadata = loadCompileTimeNetworkMetadata(
vendorDir & "/eth2-networks/shared/prater", some goerli, false)
vendorDir & "/eth2-networks/shared/prater", some goerli, not incbinEnabled)
sepoliaMetadata = loadCompileTimeNetworkMetadata(
vendorDir & "/sepolia/bepolia", some sepolia, false)
vendorDir & "/sepolia/bepolia", some sepolia, not incbinEnabled)

static:
for network in [mainnetMetadata, praterMetadata, sepoliaMetadata]:
Expand Down Expand Up @@ -272,9 +291,12 @@ proc getMetadataForNetwork*(
warn "Ropsten is unsupported; https://blog.ethereum.org/2022/11/30/ropsten-shutdown-announcement suggests migrating to Goerli or Sepolia"

template withGenesis(metadata, genesis: untyped): untyped =
var tmp = metadata
assign(tmp.genesisData, genesis.toOpenArray(0, `genesis Size` - 1))
tmp
when incbinEnabled:
var tmp = metadata
assign(tmp.genesisData, genesis.toOpenArray(0, `genesis Size` - 1))
tmp
else:
metadata

let metadata =
when const_preset == "gnosis":
Expand Down
4 changes: 2 additions & 2 deletions beacon_chain/nimbus_beacon_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ proc init*(T: type BeaconNode,
var genesisState =
if metadata.genesisData.len > 0:
try:
newClone readSszForkedHashedBeaconState(cfg, metadata.genesisData)
newClone readSszForkedHashedBeaconState(cfg, metadata.genesisBytes)
except CatchableError as err:
raiseAssert "Invalid baked-in state: " & err.msg
else:
Expand Down Expand Up @@ -2028,7 +2028,7 @@ proc handleStartUpCmd(config: var BeaconNodeConf) {.raises: [Defect, CatchableEr
stateId: "finalized")
genesis =
if network.genesisData.len > 0:
newClone(readSszForkedHashedBeaconState(cfg, network.genesisData))
newClone(readSszForkedHashedBeaconState(cfg, network.genesisBytes))
else: nil

if config.blockId.isSome():
Expand Down
4 changes: 2 additions & 2 deletions beacon_chain/nimbus_light_client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import
eth/db/kvstore_sqlite3, eth/keys,
./el/el_manager,
./gossip_processing/optimistic_processor,
./networking/topic_params,
./networking/[topic_params, network_metadata],
./spec/beaconstate,
./spec/datatypes/[phase0, altair, bellatrix, capella, deneb],
"."/[filepath, light_client, light_client_db, nimbus_binary_common, version]
Expand Down Expand Up @@ -65,7 +65,7 @@ programMain:
let
genesisState =
try:
newClone(readSszForkedHashedBeaconState(cfg, metadata.genesisData))
newClone(readSszForkedHashedBeaconState(cfg, metadata.genesisBytes))
except CatchableError as err:
raiseAssert "Invalid baked-in state: " & err.msg

Expand Down
2 changes: 1 addition & 1 deletion tests/test_network_metadata.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ template checkRoot(name, root) =
metadata = getMetadataForNetwork(name)
cfg = metadata.cfg
state = newClone(readSszForkedHashedBeaconState(
metadata.cfg, metadata.genesisData))
metadata.cfg, metadata.genesisBytes))

check:
$getStateRoot(state[]) == root
Expand Down

0 comments on commit 4c3850f

Please sign in to comment.