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
This commit is contained in:
Etan Kissling 2023-06-14 21:48:50 +02:00 committed by GitHub
parent 114453e67d
commit 411d809c82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 6 deletions

View File

@ -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

View File

@ -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