Skip to content

Commit 13954e2

Browse files
authored
Deprecate '--custom-network' and merge it's functionality to '--network' (#3567)
* Deprecate '--custom-network' and merge it's functionality to '--network' We cannot remove `--custom-network` yet, there are still two users depends on this: hive tests, and kurtosis interop test. We an remove/disable `--custom-network` after both of hive tests and kurtosis interop test accept the PR to fix them. Now, the `--custom-network` only deprecated and not printed on CLI anymore. * Remove --custom-network from nrpc and merge it's functionality to --network
1 parent eae763d commit 13954e2

File tree

13 files changed

+148
-93
lines changed

13 files changed

+148
-93
lines changed

execution_chain/common/chain_config.nim

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -664,10 +664,16 @@ func genesisBlockForNetwork*(id: NetworkId): Genesis
664664
else:
665665
Genesis()
666666

667-
func networkParams*(id: NetworkId): NetworkParams
668-
{.gcsafe, raises: [ValueError, RlpError].} =
669-
result.genesis = genesisBlockForNetwork(id)
670-
result.config = chainConfigForNetwork(id)
667+
func networkParams*(id: NetworkId): NetworkParams =
668+
try:
669+
NetworkParams(
670+
genesis: genesisBlockForNetwork(id),
671+
config : chainConfigForNetwork(id)
672+
)
673+
except ValueError as exc:
674+
raiseAssert exc.msg
675+
except RlpError as exc:
676+
raiseAssert exc.msg
671677

672678
func `==`*(a, b: Genesis): bool =
673679
if a.isNil and b.isNil: return true

execution_chain/config.nim

Lines changed: 86 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import
2121
chronicles,
2222
confutils,
2323
confutils/defs,
24-
confutils/std/net
24+
confutils/std/net,
25+
results
2526
],
2627
eth/[common, net/nat],
2728
./networking/[bootnodes, eth1_enr as enr],
@@ -163,13 +164,16 @@ type
163164
"- sepolia/11155111: Test network (proof-of-work)\n" &
164165
"- holesky/17000 : The holesovice post-merge testnet\n" &
165166
"- hoodi/560048 : The second long-standing, merged-from-genesis, public Ethereum testnet\n" &
166-
"- other : Custom"
167-
defaultValue: "" # the default value is set in makeConfig
167+
"- path : /path/to/genesis-or-network-configuration.json\n" &
168+
"Both --network: name/path --network:id can be set at the same time to override network id number"
169+
defaultValue: @[] # the default value is set in makeConfig
168170
defaultValueDesc: "mainnet(1)"
169171
abbr: "i"
170-
name: "network" }: string
172+
name: "network" }: seq[string]
171173

174+
# TODO: disable --custom-network if both hive and kurtosis not using this anymore.
172175
customNetwork {.
176+
hidden
173177
desc: "Use custom genesis block for private Ethereum Network (as /path/to/genesis.json)"
174178
defaultValueDesc: ""
175179
abbr: "c"
@@ -669,22 +673,86 @@ proc loadBootstrapFile(fileName: string, output: var seq[ENode]) =
669673
proc loadStaticPeersFile(fileName: string, output: var seq[ENode]) =
670674
fileName.loadEnodeFile(output, "static peers")
671675

672-
proc getNetworkId(conf: NimbusConf): Option[NetworkId] =
673-
if conf.network.len == 0:
674-
return none NetworkId
676+
func decOrHex(s: string): bool =
677+
const allowedDigits = Digits + HexDigits + {'x', 'X'}
678+
for c in s:
679+
if c notin allowedDigits:
680+
return false
681+
true
675682

676-
let network = toLowerAscii(conf.network)
677-
case network
678-
of "mainnet": return some MainNet
679-
of "sepolia": return some SepoliaNet
680-
of "holesky": return some HoleskyNet
681-
of "hoodi": return some HoodiNet
683+
proc parseNetworkId(network: string): NetworkId =
684+
try:
685+
return parseHexOrDec256(network)
686+
except CatchableError:
687+
error "Failed to parse network id", id=network
688+
quit QuitFailure
689+
690+
proc parseNetworkParams(network: string): (NetworkParams, bool) =
691+
case toLowerAscii(network)
692+
of "mainnet": (networkParams(MainNet), false)
693+
of "sepolia": (networkParams(SepoliaNet), false)
694+
of "holesky": (networkParams(HoleskyNet), false)
695+
of "hoodi" : (networkParams(HoodiNet), false)
682696
else:
683-
try:
684-
some parseHexOrDec256(network)
685-
except CatchableError:
686-
error "Failed to parse network name or id", network
697+
var params: NetworkParams
698+
if not loadNetworkParams(network, params):
699+
# `loadNetworkParams` have it's own error log
687700
quit QuitFailure
701+
(params, true)
702+
703+
proc processNetworkParamsAndNetworkId(conf: var NimbusConf) =
704+
if conf.network.len == 0 and conf.customNetwork.isNone:
705+
# Default value if none is set
706+
conf.networkId = MainNet
707+
conf.networkParams = networkParams(MainNet)
708+
return
709+
710+
var
711+
params: Opt[NetworkParams]
712+
id: Opt[NetworkId]
713+
simulatedCustomNetwork = false
714+
715+
for network in conf.network:
716+
if decOrHex(network):
717+
if id.isSome:
718+
warn "Network ID already set, ignore new value", id=network
719+
continue
720+
id = Opt.some parseNetworkId(network)
721+
else:
722+
if params.isSome:
723+
warn "Network configuration already set, ignore new value", network
724+
continue
725+
let (parsedParams, custom) = parseNetworkParams(network)
726+
params = Opt.some parsedParams
727+
# Simulate --custom-network while it is still not disabled.
728+
if custom:
729+
conf.customNetwork = some parsedParams
730+
simulatedCustomNetwork = true
731+
732+
if conf.customNetwork.isSome:
733+
if params.isNone:
734+
warn "`--custom-network` is deprecated, please use `--network`"
735+
elif not simulatedCustomNetwork:
736+
warn "Network configuration already set by `--network`, `--custom-network` override it"
737+
params = if conf.customNetwork.isSome: Opt.some conf.customNetwork.get
738+
else: Opt.none(NetworkParams)
739+
if id.isNone:
740+
# WARNING: networkId and chainId are two distinct things
741+
# their usage should not be mixed in other places.
742+
# We only set networkId to chainId if networkId not set in cli and
743+
# --custom-network is set.
744+
# If chainId is not defined in config file, it's ok because
745+
# zero means CustomNet
746+
id = Opt.some NetworkId(params.value.config.chainId)
747+
748+
if id.isNone and params.isSome:
749+
id = Opt.some NetworkId(params.value.config.chainId)
750+
751+
if conf.customNetwork.isNone and params.isNone:
752+
params = Opt.some networkParams(id.value)
753+
754+
conf.networkParams = params.expect("Network params exists")
755+
conf.networkId = id.expect("Network ID exists")
688756

689757
proc getRpcFlags(api: openArray[string]): set[RpcFlag] =
690758
if api.len == 0:
@@ -830,27 +898,7 @@ proc makeConfig*(cmdLine = commandLineParams()): NimbusConf
830898

831899
setupLogging(result.logLevel, result.logStdout, none(OutFile))
832900

833-
var networkId = result.getNetworkId()
834-
835-
if result.customNetwork.isSome:
836-
result.networkParams = result.customNetwork.get()
837-
if networkId.isNone:
838-
# WARNING: networkId and chainId are two distinct things
839-
# they usage should not be mixed in other places.
840-
# We only set networkId to chainId if networkId not set in cli and
841-
# --custom-network is set.
842-
# If chainId is not defined in config file, it's ok because
843-
# zero means CustomNet
844-
networkId = some(NetworkId(result.networkParams.config.chainId))
845-
846-
if networkId.isNone:
847-
# bootnodes is set via getBootNodes
848-
networkId = some MainNet
849-
850-
result.networkId = networkId.get()
851-
852-
if result.customNetwork.isNone:
853-
result.networkParams = networkParams(result.networkId)
901+
processNetworkParamsAndNetworkId(result)
854902

855903
if result.cmd == noCommand:
856904
if result.udpPort == Port(0):

hive_integration/nodocker/engine/engine_env.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ proc makeCom*(conf: NimbusConf): CommonRef =
6666

6767
proc envConfig*(): NimbusConf =
6868
makeConfig(@[
69-
"--custom-network:" & genesisFile,
69+
"--network:" & genesisFile,
7070
"--listen-address: 127.0.0.1",
7171
])
7272

hive_integration/nodocker/rpc/test_env.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ proc setupEnv*(taskPool: Taskpool): TestEnv =
6767
# "--nat:extip:0.0.0.0",
6868
"--network:7",
6969
"--import-key:" & initPath / "private-key",
70-
"--custom-network:" & initPath / "genesis.json",
70+
"--network:" & initPath / "genesis.json",
7171
"--rpc",
7272
"--rpc-api:eth,debug",
7373
# "--http-address:0.0.0.0",

nrpc/config.nim

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,22 @@ type
6060
defaultValue: ""
6161
name: "beacon-api" .}: string
6262

63-
network {.
63+
network* {.
6464
desc: "Name or id number of Ethereum network"
6565
longDesc:
6666
"- mainnet/1 : Ethereum main network\n" &
6767
"- sepolia/11155111: Test network (proof-of-work)\n" &
6868
"- holesky/17000 : The holesovice post-merge testnet\n" &
6969
"- hoodi/560048 : The second long-standing, merged-from-genesis, public Ethereum testnet\n" &
70-
"- other : Custom"
70+
"- path : Custom config for private Ethereum Network (as /path/to/metadata)\n" &
71+
" Path to a folder containing custom network configuration files\n" &
72+
" such as genesis.json, config.yaml, etc.\n" &
73+
" config.yaml is the configuration file for the CL client"
7174
defaultValue: "" # the default value is set in makeConfig
7275
defaultValueDesc: "mainnet(1)"
7376
abbr: "i"
7477
name: "network" }: string
7578

76-
customNetworkFolder* {.
77-
desc: "Use custom config for private Ethereum Network (as /path/to/metadata)"
78-
longDesc:
79-
"Path to a folder containing custom network configuration files\n" &
80-
"such as genesis.json, config.yaml, etc.\n" &
81-
"config.yaml is the configuration file for the CL client"
82-
defaultValue: ""
83-
name: "custom-network" .}: string
84-
8579
networkId* {.
8680
ignore # this field is not processed by confutils
8781
defaultValue: MainNet # the defaultValue value is set by `makeConfig`
@@ -153,22 +147,33 @@ proc parseCmdArg(T: type NetworkParams, p: string): T
153147
func completeCmdArg(T: type NetworkParams, val: string): seq[string] =
154148
return @[]
155149

150+
func decOrHex(s: string): bool =
151+
const allowedDigits = Digits + HexDigits + {'x', 'X'}
152+
for c in s:
153+
if c notin allowedDigits:
154+
return false
155+
true
156+
157+
proc parseNetworkId(network: string): Opt[NetworkId] =
158+
try:
159+
Opt.some parseHexOrDec256(network)
160+
except CatchableError:
161+
error "Failed to parse network id", id=network
162+
Opt.none NetworkId
156163

157164
proc getNetworkId(conf: NRpcConf): Opt[NetworkId] =
158165
if conf.network.len == 0:
159-
return Opt.none NetworkId
166+
return Opt.some MainNet
160167

161168
let network = toLowerAscii(conf.network)
162169
case network
163170
of "mainnet": return Opt.some MainNet
164171
of "sepolia": return Opt.some SepoliaNet
165172
of "holesky": return Opt.some HoleskyNet
173+
of "hoodi" : return Opt.some HoodiNet
166174
else:
167-
try:
168-
Opt.some parseHexOrDec256(network)
169-
except CatchableError:
170-
error "Failed to parse network name or id", network
171-
quit QuitFailure
175+
if decOrHex(network):
176+
return parseNetworkId(network)
172177

173178
# KLUDGE: The `load()` template does currently not work within any exception
174179
# annotated environment.
@@ -188,12 +193,15 @@ proc makeConfig*(cmdLine = commandLineParams()): NRpcConf
188193
except CatchableError as e:
189194
raise e
190195

191-
var networkId = result.getNetworkId()
196+
var
197+
networkId = result.getNetworkId()
198+
customNetwork = false
192199

193-
if result.customNetworkFolder.len > 0:
200+
if result.network.len > 0 and networkId.isNone:
201+
customNetwork = true
194202
var networkParams = NetworkParams()
195-
if not loadNetworkParams(result.customNetworkFolder.joinPath("genesis.json"), networkParams):
196-
error "Failed to load customNetwork", path=result.customNetworkFolder
203+
if not loadNetworkParams(result.network.joinPath("genesis.json"), networkParams):
204+
error "Failed to load customNetwork", path=result.network
197205
quit QuitFailure
198206
result.networkParams = networkParams
199207
if networkId.isNone:
@@ -205,13 +213,9 @@ proc makeConfig*(cmdLine = commandLineParams()): NRpcConf
205213
# zero means CustomNet
206214
networkId = Opt.some(NetworkId(result.networkParams.config.chainId))
207215

208-
if networkId.isNone:
209-
# bootnodes is set via getBootNodes
210-
networkId = Opt.some MainNet
211-
212-
result.networkId = networkId.get()
216+
result.networkId = networkId.expect("Network ID exists")
213217

214-
if result.customNetworkFolder.len == 0:
218+
if not customNetwork:
215219
result.networkParams = networkParams(result.networkId)
216220

217221

nrpc/nrpc.nim

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,7 @@ template loadNetworkConfig(conf: NRpcConf): (RuntimeConfig, uint64, uint64) =
9494
(getMetadataForNetwork("hoodi").cfg, 0'u64, 0'u64)
9595
else:
9696
notice "Loading custom network, assuming post-merge"
97-
if conf.customNetworkFolder.len == 0:
98-
error "Custom network file not provided"
99-
quit(QuitFailure)
100-
let (cfg, unloaded) = readRuntimeConfig(conf.customNetworkFolder.joinPath("config.yaml"))
97+
let (cfg, unloaded) = readRuntimeConfig(conf.network.joinPath("config.yaml"))
10198
debug "Fields unknown", unloaded = unloaded
10299
(cfg, 0'u64, 0'u64)
103100

@@ -210,7 +207,7 @@ proc syncToEngineApi(conf: NRpcConf) {.async.} =
210207
progressTrackedHead = currentBlockNumber
211208

212209
template estimateProgressForSync() =
213-
let
210+
let
214211
blocks = int(currentBlockNumber - progressTrackedHead)
215212
curTime = Moment.now()
216213
diff = curTime - time

tests/networking/p2p_test_helper.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ proc makeCom(conf: NimbusConf): CommonRef =
4040

4141
proc envConfig(): NimbusConf =
4242
makeConfig(@[
43-
"--custom-network:" & genesisFile,
43+
"--network:" & genesisFile,
4444
"--listen-address: 127.0.0.1",
4545
])
4646

tests/test_configuration.nim

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,36 +47,36 @@ proc configurationMain*() =
4747
check bb.cmd == NimbusCmd.`import-rlp`
4848
check bb.blocksFile[0].string == genesisFile
4949

50-
test "custom-network loading config file with no genesis data":
50+
test "network loading config file with no genesis data":
5151
# no genesis will fallback to geth compatibility mode
52-
let conf = makeConfig(@["--custom-network:" & noGenesis])
52+
let conf = makeConfig(@["--network:" & noGenesis])
5353
check conf.networkParams.genesis.isNil.not
5454

55-
test "custom-network loading config file with no 'config'":
55+
test "network loading config file with no 'config'":
5656
# no config will result in empty config, CommonRef keep working
57-
let conf = makeConfig(@["--custom-network:" & noConfig])
57+
let conf = makeConfig(@["--network:" & noConfig])
5858
check conf.networkParams.config.isNil == false
5959

6060
test "network-id":
6161
let aa = makeTestConfig()
6262
check aa.networkId == MainNet
6363
check aa.networkParams != NetworkParams()
6464

65-
let conf = makeConfig(@["--custom-network:" & genesisFile, "--network:345"])
65+
let conf = makeConfig(@["--network:" & genesisFile, "--network:345"])
6666
check conf.networkId == 345.u256
6767

68-
test "network-id first, custom-network next":
69-
let conf = makeConfig(@["--network:678", "--custom-network:" & genesisFile])
68+
test "network-id first, network next":
69+
let conf = makeConfig(@["--network:678", "--network:" & genesisFile])
7070
check conf.networkId == 678.u256
7171

72-
test "network-id set, no custom-network":
72+
test "network-id set, no network":
7373
let conf = makeConfig(@["--network:678"])
7474
check conf.networkId == 678.u256
7575
check conf.networkParams.genesis == Genesis()
7676
check conf.networkParams.config == ChainConfig()
7777

7878
test "network-id not set, copy from chainId of custom network":
79-
let conf = makeConfig(@["--custom-network:" & genesisFile])
79+
let conf = makeConfig(@["--network:" & genesisFile])
8080
check conf.networkId == 123.u256
8181

8282
test "network-id not set, sepolia set":
@@ -165,11 +165,11 @@ proc configurationMain*() =
165165
let cc = makeConfig(@["--static-peers:" & bootNode, "--static-peers:" & bootNode])
166166
check cc.getStaticPeers().len == 2
167167

168-
test "chainId of custom-network is oneof std network":
168+
test "chainId of network is oneof std network":
169169
const
170170
chainid1 = "tests" / "customgenesis" / "chainid1.json"
171171

172-
let conf = makeConfig(@["--custom-network:" & chainid1])
172+
let conf = makeConfig(@["--network:" & chainid1])
173173
check conf.networkId == 1.u256
174174
check conf.networkParams.config.londonBlock.get() == 1337
175175
check conf.getBootNodes().len == 0

0 commit comments

Comments
 (0)