Skip to content

Commit a66349a

Browse files
authored
Update data directory defaults to include namespace, network (#3596)
status-im/nimbus-eth2#7418 Align with eth2 to enable the safe use of a shared data directory between eth1/2 and other nimbus, when using defaults In particular, this enforces permissions on the data directory
1 parent 91ee027 commit a66349a

File tree

16 files changed

+182
-319
lines changed

16 files changed

+182
-319
lines changed

execution_chain/common/chain_config.nim

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -414,13 +414,9 @@ proc parseGenesis*(data: string): Genesis
414414
{.gcsafe.} =
415415
try:
416416
result = JGenesis.decode(data, Genesis, allowUnknownFields = true)
417-
except JsonReaderError as e:
417+
except SerializationError as e:
418418
error "Invalid genesis config file format", msg=e.formatMsg("")
419419
return nil
420-
except CatchableError as e:
421-
error "Error loading genesis data",
422-
exception = e.name, msg = e.msg
423-
return nil
424420

425421
proc parseGenesisFile*(fileName: string): Genesis
426422
{.gcsafe.} =
@@ -429,13 +425,9 @@ proc parseGenesisFile*(fileName: string): Genesis
429425
except IOError as e:
430426
error "Genesis I/O error", fileName, msg=e.msg
431427
return nil
432-
except JsonReaderError as e:
428+
except SerializationError as e:
433429
error "Invalid genesis config file format", msg=e.formatMsg("")
434430
return nil
435-
except CatchableError as e:
436-
error "Error loading genesis file",
437-
fileName, exception = e.name, msg = e.msg
438-
return nil
439431

440432
proc validateNetworkParams(params: var NetworkParams, input: string, inputIsFile: bool): bool =
441433
if params.genesis.isNil:
@@ -460,39 +452,21 @@ proc loadNetworkParams*(fileName: string, params: var NetworkParams):
460452
except IOError as e:
461453
error "Network params I/O error", fileName, msg=e.msg
462454
return false
463-
except JsonReaderError as e:
455+
except SerializationError as e:
464456
error "Invalid network params file format", fileName, msg=e.formatMsg("")
465457
return false
466-
except CatchableError as e:
467-
error "Error loading network params file",
468-
fileName, exception = e.name, msg = e.msg
469-
return false
470458

471459
validateNetworkParams(params, fileName, true)
472460

473461
proc decodeNetworkParams*(jsonString: string, params: var NetworkParams): bool =
474462
try:
475463
params = JGenesis.decode(jsonString, NetworkParams, allowUnknownFields = true)
476-
except JsonReaderError as e:
464+
except SerializationError as e:
477465
error "Invalid network params format", msg=e.formatMsg("")
478466
return false
479-
except CatchableError:
480-
var msg = getCurrentExceptionMsg()
481-
error "Error decoding network params", msg
482-
return false
483467

484468
validateNetworkParams(params, jsonString, false)
485469

486-
proc parseGenesisAlloc*(data: string, ga: var GenesisAlloc): bool
487-
{.gcsafe, raises: [CatchableError].} =
488-
try:
489-
ga = JGenesis.decode(data, GenesisAlloc, allowUnknownFields = true)
490-
except JsonReaderError as e:
491-
error "Invalid genesis config file format", msg=e.formatMsg("")
492-
return false
493-
494-
return true
495-
496470
func defaultBlobSchedule*(): array[Cancun..HardFork.high, Opt[BlobSchedule]] =
497471
[
498472
Cancun: Opt.some(BlobSchedule(target: 3'u64, max: 6'u64, baseFeeUpdateFraction: 3_338_477'u64)),
@@ -664,6 +638,18 @@ func genesisBlockForNetwork*(id: NetworkId): Genesis
664638
else:
665639
Genesis()
666640

641+
func name*(id: NetworkId): string =
642+
if id == MainNet:
643+
"mainnet"
644+
elif id == SepoliaNet:
645+
"sepolia"
646+
elif id == HoleskyNet:
647+
"holesky"
648+
elif id == HoodiNet:
649+
"hoodi"
650+
else:
651+
$id
652+
667653
func networkParams*(id: NetworkId): NetworkParams =
668654
try:
669655
NetworkParams(

execution_chain/common/context.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Nimbus
2-
# Copyright (c) 2021-2024 Status Research & Development GmbH
2+
# Copyright (c) 2021-2025 Status Research & Development GmbH
33
# Licensed under either of
44
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
55
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
@@ -43,7 +43,7 @@ proc containsOnlyHexDigits(hex: string): bool =
4343
return false
4444
true
4545

46-
proc getNetKeys*(ctx: EthContext, netKey, dataDir: string): Result[KeyPair, string]
46+
proc getNetKeys*(ctx: EthContext, netKey: string): Result[KeyPair, string]
4747
{.gcsafe, raises: [OSError]} =
4848
if netKey.len == 0 or netKey == "random":
4949
let privateKey = ctx.randomPrivateKey()

execution_chain/common/logging.nim

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,16 @@
1010
{.used.}
1111

1212
import
13-
std/[typetraits, net],
14-
json_serialization,
15-
web3/primitives,
13+
std/typetraits,
14+
json_serialization/std/net as jsnet,
15+
web3/conversions,
1616
confutils/defs,
1717
eth/common/eth_types_json_serialization
1818

19-
# nim-eth
20-
proc writeValue*(
21-
w: var JsonWriter, v: EthTime
22-
) {.inline, raises: [IOError].} =
23-
w.writeValue distinctBase(v)
24-
25-
# nim-web3
26-
proc writeValue*(w: var JsonWriter, v: Quantity) {.inline, raises: [IOError].} =
27-
w.writeValue distinctBase(v)
19+
export conversions.writeValue, jsnet.writeValue, eth_types_json_serialization.writeValue
2820

2921
# nim-confutils
3022
proc writeValue*(
3123
w: var JsonWriter, v: InputFile | OutDir | OutFile | RestOfCmdLine | OutPath
3224
) {.inline, raises: [IOError].} =
3325
w.writeValue distinctBase(v)
34-
35-
# build-system
36-
proc writeValue*(w: var JsonWriter, v: Port) {.inline, raises: [IOError].} =
37-
w.writeValue distinctBase(v)

execution_chain/config.nim

Lines changed: 32 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,49 +21,34 @@ import
2121
chronicles,
2222
confutils,
2323
confutils/defs,
24-
confutils/std/net,
25-
results
24+
confutils/std/net as confnet,
25+
json_serialization/std/net as jsnet,
26+
results,
27+
beacon_chain/buildinfo,
28+
beacon_chain/nimbus_binary_common,
2629
],
2730
eth/[common, net/nat],
2831
./networking/[bootnodes, eth1_enr as enr],
2932
./[constants, compile_info, version],
3033
./common/chain_config,
3134
./db/opts
3235

33-
from beacon_chain/nimbus_binary_common import setupLogging, StdoutLogKind
34-
35-
export net, defs, StdoutLogKind
36-
36+
export net, defs, jsnet, nimbus_binary_common
3737
const
38-
# e.g.: Copyright (c) 2018-2025 Status Research & Development GmbH
39-
NimbusCopyright* = "Copyright (c) 2018-" &
40-
CompileDate.split('-')[0] &
41-
" Status Research & Development GmbH"
4238

4339
# e.g.:
4440
# nimbus_execution_client/v0.1.0-abcdef/os-cpu/nim-a.b.c/emvc
4541
# Copyright (c) 2018-2025 Status Research & Development GmbH
4642
NimbusBuild* = "$#\p$#" % [
4743
ClientId,
48-
NimbusCopyright,
44+
copyrights,
4945
]
5046

5147
NimbusHeader* = "$#\p\pNim version $#" % [
5248
NimbusBuild,
53-
NimVersion
49+
nimBanner()
5450
]
5551

56-
func defaultDataDir*(): string =
57-
when defined(windows):
58-
getHomeDir() / "AppData" / "Roaming" / "Nimbus"
59-
elif defined(macosx):
60-
getHomeDir() / "Library" / "Application Support" / "Nimbus"
61-
else:
62-
getHomeDir() / ".cache" / "nimbus"
63-
64-
func defaultKeystoreDir*(): string =
65-
defaultDataDir() / "keystore"
66-
6752
func getLogLevels(): string =
6853
var logLevels: seq[string]
6954
for level in LogLevel:
@@ -73,7 +58,6 @@ func getLogLevels(): string =
7358
join(logLevels, ", ")
7459

7560
const
76-
defaultDataDirDesc = defaultDataDir()
7761
defaultPort = 30303
7862
defaultMetricsServerPort = 9093
7963
defaultHttpPort = 8545
@@ -106,30 +90,27 @@ type
10690
NimbusConf* = object of RootObj
10791
## Main Nimbus configuration object
10892

109-
dataDir* {.
93+
dataDirFlag* {.
11094
separator: "ETHEREUM OPTIONS:"
11195
desc: "The directory where nimbus will store all blockchain data"
112-
defaultValue: defaultDataDir()
113-
defaultValueDesc: $defaultDataDirDesc
11496
abbr: "d"
115-
name: "data-dir" }: OutDir
97+
name: "data-dir" }: Option[OutDir]
11698

117-
era1DirOpt* {.
99+
era1DirFlag* {.
118100
desc: "Directory where era1 (pre-merge) archive can be found"
119101
defaultValueDesc: "<data-dir>/era1"
120102
name: "era1-dir" }: Option[OutDir]
121103

122-
eraDirOpt* {.
104+
eraDirFlag* {.
123105
desc: "Directory where era (post-merge) archive can be found"
124106
defaultValueDesc: "<data-dir>/era"
125107
name: "era-dir" }: Option[OutDir]
126108

127-
keyStore* {.
109+
keyStoreDirFlag* {.
128110
desc: "Load one or more keystore files from this directory"
129-
defaultValue: defaultKeystoreDir()
130111
defaultValueDesc: "inside datadir"
131112
abbr: "k"
132-
name: "key-store" }: OutDir
113+
name: "key-store" }: Option[OutDir]
133114

134115
importKey* {.
135116
desc: "Import unencrypted 32 bytes hex private key from a file"
@@ -528,7 +509,7 @@ type
528509
" CL which will result in a smaller memory footprint"
529510
name: "debug-beacon-sync-target-is-final".}: bool
530511

531-
of `import`:
512+
of NimbusCmd.`import`:
532513
maxBlocks* {.
533514
desc: "Maximum number of blocks to import"
534515
defaultValue: uint64.high()
@@ -579,7 +560,7 @@ type
579560
defaultValue: false
580561
name: "debug-store-slot-hashes".}: bool
581562

582-
of `import-rlp`:
563+
of NimbusCmd.`import-rlp`:
583564
blocksFile* {.
584565
argument
585566
desc: "One or more RLP encoded block(s) files"
@@ -591,6 +572,13 @@ func parseHexOrDec256(p: string): UInt256 {.raises: [ValueError].} =
591572
else:
592573
parse(p, UInt256, 10)
593574

575+
proc dataDir*(config: NimbusConf): string =
576+
# TODO load network name from directory, when using custom network?
577+
string config.dataDirFlag.get(OutDir defaultDataDir("", config.networkId.name()))
578+
579+
proc keyStoreDir*(config: NimbusConf): string =
580+
string config.keyStoreDirFlag.get(OutDir config.dataDir() / "keystore")
581+
594582
func parseCmdArg(T: type NetworkId, p: string): T
595583
{.gcsafe, raises: [ValueError].} =
596584
parseHexOrDec256(p)
@@ -867,11 +855,11 @@ func shareServerWithEngineApi*(conf: NimbusConf): bool =
867855
func httpServerEnabled*(conf: NimbusConf): bool =
868856
conf.wsEnabled or conf.rpcEnabled
869857

870-
func era1Dir*(conf: NimbusConf): OutDir =
871-
conf.era1DirOpt.get(OutDir(conf.dataDir.string & "/era1"))
858+
proc era1Dir*(conf: NimbusConf): string =
859+
string conf.era1DirFlag.get(OutDir conf.dataDir / "era1")
872860

873-
func eraDir*(conf: NimbusConf): OutDir =
874-
conf.eraDirOpt.get(OutDir(conf.dataDir.string & "/era"))
861+
proc eraDir*(conf: NimbusConf): string =
862+
string conf.eraDirFlag.get(OutDir conf.dataDir / "era")
875863

876864
func dbOptions*(conf: NimbusConf, noKeyCache = false): DbOptions =
877865
DbOptions.init(
@@ -897,20 +885,11 @@ func dbOptions*(conf: NimbusConf, noKeyCache = false): DbOptions =
897885
proc makeConfig*(cmdLine = commandLineParams()): NimbusConf
898886
{.raises: [CatchableError].} =
899887
## Note: this function is not gc-safe
900-
901-
# The try/catch clause can go away when `load()` is clean
902-
try:
903-
{.push warning[ProveInit]: off.}
904-
result = NimbusConf.load(
905-
cmdLine,
906-
version = NimbusBuild,
907-
copyrightBanner = NimbusHeader
908-
)
909-
{.pop.}
910-
except CatchableError as e:
911-
raise e
912-
913-
setupLogging(result.logLevel, result.logStdout, none(OutFile))
888+
result = NimbusConf.load(
889+
cmdLine,
890+
version = NimbusBuild,
891+
copyrightBanner = NimbusHeader
892+
)
914893

915894
processNetworkParamsAndNetworkId(result)
916895

@@ -919,11 +898,6 @@ proc makeConfig*(cmdLine = commandLineParams()): NimbusConf
919898
# if udpPort not set in cli, then
920899
result.udpPort = result.tcpPort
921900

922-
# see issue #1346
923-
if result.keyStore.string == defaultKeystoreDir() and
924-
result.dataDir.string != defaultDataDir():
925-
result.keyStore = OutDir(result.dataDir.string / "keystore")
926-
927901
when isMainModule:
928902
# for testing purpose
929903
discard makeConfig()

0 commit comments

Comments
 (0)