Skip to content

Commit

Permalink
Introduce a RuntimePreset object for the overridable testnet preset v…
Browse files Browse the repository at this point in the history
…alues
  • Loading branch information
zah committed Jul 3, 2020
1 parent 98761af commit 441ae9b
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 117 deletions.
2 changes: 2 additions & 0 deletions beacon_chain/beacon_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,8 @@ programMain:
let
networkName = config.eth2Network.get
metadata = case toLowerAscii(networkName)
of "mainnet":
mainnetMetadata
of "altona":
altonaMetadata
else:
Expand Down
49 changes: 47 additions & 2 deletions beacon_chain/network_metadata.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import
os, strutils,
stew/byteutils, nimcrypto/hash,
eth/common/[eth_types, eth_types_json_serialization]
stew/byteutils, stew/shims/macros, nimcrypto/hash,
eth/common/[eth_types, eth_types_json_serialization],
spec/presets/custom

# ATTENTION! This file will produce a large C file, because we are inlining
# genesis states as C literals in the generated code (and blobs in the final
Expand Down Expand Up @@ -29,8 +30,11 @@ type
customEth2Network
altona

PresetIncompatible* = object of CatchableError

Eth2NetworkMetadata* = object
eth1Network*: Eth1Network
runtimePreset*: RuntimePreset

# Parsing `enr.Records` is still not possible at compile-time
bootstrapNodes*: seq[string]
Expand All @@ -52,19 +56,60 @@ type
# unknown genesis state.
genesisData*: string

const presetValueLoaders = genCode(nnkBracket):
for constName in PresetValue:
let
constNameIdent = ident $constName
constType = ident getType(constName)

yield quote do:
proc (preset: RuntimePreset, presetValue: string): bool
{.gcsafe, noSideEffect, raises: [Defect].} =
try:
when `constNameIdent` in runtimeValues:
preset.`constNameIdent` = parse(`constType`, presetValue)
true
else:
`constNameIdent` == parse(`constType`, presetValue)
except CatchableError:
false

proc loadEth2NetworkMetadata*(path: string): Eth2NetworkMetadata
{.raises: [CatchableError, Defect].} =
let
genesisPath = path / "genesis.ssz"
config = readPresetFile(path / "config.yaml")

var runtimePreset = RuntimePreset()

for name, value in config.values:
if name notin runtimeValues:
if not presetValueLoaders[name](runtimePreset, value):
raise newException(PresetIncompatible,
"The preset '{path}' is not compatible with the current build due to an incompatible value {name} = {value}")

Eth2NetworkMetadata(
eth1Network: goerli,
runtimePreset: runtimePreset,
bootstrapNodes: readFile(path / "bootstrap_nodes.txt").split("\n"),
depositContractAddress: Eth1Address.fromHex readFile(path / "deposit_contract.txt").strip,
depositContractDeployedAt: Eth1BlockHash.fromHex readFile(path / "deposit_contract_block.txt").strip,
genesisData: if fileExists(genesisPath): readFile(genesisPath) else: "")

const
mainnetMetadata* = Eth2NetworkMetadata(
eth1Network: mainnet,
runtimePreset: RuntimePreset(
MIN_GENESIS_ACTIVE_VALIDATOR_COUNT: 16384,
MIN_GENESIS_TIME: 1578009600,
GENESIS_FORK_VERSION: [byte 0, 0, 0, 0],
GENESIS_DELAY: 172800),
# TODO The values below are just placeholders for now
bootstrapNodes: @[],
depositContractAddress: "0x1234567890123456789012345678901234567890",
depositContractDeployedAt: "",
genesisData: "")

altonaMetadata* = loadEth2NetworkMetadata(
currentSourcePath.parentDir / ".." / "vendor" / "eth2-testnets" / "shared" / "altona")

4 changes: 2 additions & 2 deletions beacon_chain/spec/beaconstate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ proc initialize_beacon_state_from_eth1*(

var state = BeaconStateRef(
fork: Fork(
previous_version: Version(GENESIS_FORK_VERSION),
current_version: Version(GENESIS_FORK_VERSION),
previous_version: GENESIS_FORK_VERSION,
current_version: GENESIS_FORK_VERSION,
epoch: GENESIS_EPOCH),
genesis_time: genesis_time_from_eth1_timestamp(eth1_timestamp),
eth1_data:
Expand Down
3 changes: 1 addition & 2 deletions beacon_chain/spec/datatypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ else:
Epoch* = distinct uint64

import ./presets/custom
loadCustomPreset const_preset
createConstantsFromPreset const_preset

const
SPEC_VERSION* = "0.12.1" ## \
Expand Down Expand Up @@ -163,7 +163,6 @@ type
data*: AttestationData
signature*: TrustedSig

Version* = distinct array[4, byte]
ForkDigest* = distinct array[4, byte]

# https://github.com/ethereum/eth2.0-specs/blob/v0.12.1/specs/phase0/beacon-chain.md#forkdata
Expand Down
2 changes: 1 addition & 1 deletion beacon_chain/spec/helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func compute_fork_digest*(current_version: Version,
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.1/specs/phase0/beacon-chain.md#compute_domain
func compute_domain*(
domain_type: DomainType,
fork_version: Version = Version(GENESIS_FORK_VERSION),
fork_version = GENESIS_FORK_VERSION,
genesis_validators_root: Eth2Digest = ZERO_HASH): Domain =
# Return the domain for the ``domain_type`` and ``fork_version``.
let fork_data_root =
Expand Down
Loading

0 comments on commit 441ae9b

Please sign in to comment.