From 411d809c82bcb56ebe86414f1bed8a103e92fdec Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Wed, 14 Jun 2023 21:48:50 +0200 Subject: [PATCH] split file loading from parsing in helpers (#5054) * split file loading from parsing in helpers In `readSszForkedHashedBeaconState` and `readRuntimeConfig`, split the part that loads the file from the part that parses the file. The parsing portion can be reused with that, e.g., when loading from the network. * add missing export marker --- beacon_chain/spec/forks.nim | 10 +++++++--- beacon_chain/spec/presets.nim | 11 ++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/beacon_chain/spec/forks.nim b/beacon_chain/spec/forks.nim index a595832e8..373128be5 100644 --- a/beacon_chain/spec/forks.nim +++ b/beacon_chain/spec/forks.nim @@ -965,16 +965,20 @@ type slot: Slot func readSszForkedHashedBeaconState*( - cfg: RuntimeConfig, slot: Slot, data: openArray[byte]): + consensusFork: ConsensusFork, data: openArray[byte]): ForkedHashedBeaconState {.raises: [Defect, SszError].} = # TODO https://github.com/nim-lang/Nim/issues/19357 - result = ForkedHashedBeaconState( - kind: cfg.consensusForkAtEpoch(slot.epoch())) + result = ForkedHashedBeaconState(kind: consensusFork) withState(result): readSszBytes(data, forkyState.data) forkyState.root = hash_tree_root(forkyState.data) +template readSszForkedHashedBeaconState*( + cfg: RuntimeConfig, slot: Slot, data: openArray[byte]): + ForkedHashedBeaconState = + cfg.consensusForkAtEpoch(slot.epoch()).readSszForkedHashedBeaconState(data) + func readSszForkedHashedBeaconState*(cfg: RuntimeConfig, data: openArray[byte]): ForkedHashedBeaconState {.raises: [Defect, SszError].} = ## Read a state picking the right fork by first reading the slot from the byte diff --git a/beacon_chain/spec/presets.nim b/beacon_chain/spec/presets.nim index 7efba7a1e..892e34409 100644 --- a/beacon_chain/spec/presets.nim +++ b/beacon_chain/spec/presets.nim @@ -472,8 +472,8 @@ func parse(T: type DomainType, input: string): T {.raises: [ValueError, Defect].} = DomainType hexToByteArray(input, 4) -proc readRuntimeConfig*( - path: string): (RuntimeConfig, seq[string]) {. +proc readRuntimeConfig( + fileContent: string, path: string): (RuntimeConfig, seq[string]) {. raises: [IOError, PresetFileError, PresetIncompatibleError, Defect].} = var lineNum = 0 @@ -491,7 +491,7 @@ proc readRuntimeConfig*( names.add name var values: Table[string, string] - for line in splitLines(readFile(path)): + for line in splitLines(fileContent): inc lineNum if line.len == 0 or line[0] == '#': continue # remove any trailing comments @@ -605,6 +605,11 @@ proc readRuntimeConfig*( (cfg, unknowns) +proc readRuntimeConfig*( + path: string): (RuntimeConfig, seq[string]) {. + raises: [IOError, PresetFileError, PresetIncompatibleError, Defect].} = + readRuntimeConfig(readFile(path), path) + template name*(cfg: RuntimeConfig): string = if cfg.CONFIG_NAME.len() > 0: cfg.CONFIG_NAME