Skip to content

Commit f12f0ec

Browse files
lightclientfjl
andauthored
cmd/utils: allow --networkid to override other config options (#32999)
Recently in #31630 we removed support for overriding the network id in preset networks. While this feature is niche, it is useful for shadow forks. This PR proposes we add the functionality back, but in a simpler way. Instead of checking whether the flag is set in each branch of the network switch statement, simply apply the network flag after the switch statement is complete. This retains the following behavior: 1. Auto network id based on chain id still works, because `IsSet` only returns true if the flag is _actually_ set. Not if it just has a default set. 2. The preset networks will set their network id directly and only if the network id flag is set is it overridden. This, combined with the override genesis flag is what allows the shadow forks. 3. Setting the network id to the same network id that the preset _would have_ set causes no issues and simply emits the `WARN` that the flag is being set explicitly. I don't think people explicitly set the network id flag often. ``` WARN [10-22|09:36:15.052] Setting network id with flag id=10 ``` --------- Co-authored-by: Felix Lange <[email protected]>
1 parent fbbaa3c commit f12f0ec

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

cmd/geth/consolecmd_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ const (
3939
// child g gets a temporary data directory.
4040
func runMinimalGeth(t *testing.T, args ...string) *testgeth {
4141
// --holesky to make the 'writing genesis to disk' faster (no accounts)
42+
// --networkid=1337 to avoid cache bump
4243
// --syncmode=full to avoid allocating fast sync bloom
43-
allArgs := []string{"--holesky", "--authrpc.port", "0", "--syncmode=full", "--port", "0",
44+
allArgs := []string{"--holesky", "--networkid", "1337", "--authrpc.port", "0", "--syncmode=full", "--port", "0",
4445
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--cache", "64",
4546
"--datadir.minfreedisk", "0"}
4647
return runGeth(t, append(allArgs, args...)...)

cmd/utils/flags.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ var (
137137
}
138138
NetworkIdFlag = &cli.Uint64Flag{
139139
Name: "networkid",
140-
Usage: "Explicitly set network id (integer)(For testnets: use --sepolia, --holesky, --hoodi instead)",
140+
Usage: "Explicitly set network ID (integer)(For testnets: use --sepolia, --holesky, --hoodi instead)",
141141
Value: ethconfig.Defaults.NetworkId,
142142
Category: flags.EthCategory,
143143
}
@@ -1615,8 +1615,8 @@ func setRequiredBlocks(ctx *cli.Context, cfg *ethconfig.Config) {
16151615

16161616
// SetEthConfig applies eth-related command line flags to the config.
16171617
func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
1618-
// Avoid conflicting network flags, don't allow network id override on preset networks
1619-
flags.CheckExclusive(ctx, MainnetFlag, DeveloperFlag, SepoliaFlag, HoleskyFlag, HoodiFlag, NetworkIdFlag, OverrideGenesisFlag)
1618+
// Avoid conflicting network flags
1619+
flags.CheckExclusive(ctx, MainnetFlag, DeveloperFlag, SepoliaFlag, HoleskyFlag, HoodiFlag, OverrideGenesisFlag)
16201620
flags.CheckExclusive(ctx, DeveloperFlag, ExternalSignerFlag) // Can't use both ephemeral unlocked and external signer
16211621

16221622
// Set configurations from CLI flags
@@ -1663,9 +1663,6 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
16631663
}
16641664
}
16651665

1666-
if ctx.IsSet(NetworkIdFlag.Name) {
1667-
cfg.NetworkId = ctx.Uint64(NetworkIdFlag.Name)
1668-
}
16691666
if ctx.IsSet(CacheFlag.Name) || ctx.IsSet(CacheDatabaseFlag.Name) {
16701667
cfg.DatabaseCache = ctx.Int(CacheFlag.Name) * ctx.Int(CacheDatabaseFlag.Name) / 100
16711668
}
@@ -1915,10 +1912,18 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
19151912
}
19161913
cfg.Genesis = genesis
19171914
default:
1918-
if cfg.NetworkId == 1 {
1915+
if ctx.Uint64(NetworkIdFlag.Name) == 1 {
19191916
SetDNSDiscoveryDefaults(cfg, params.MainnetGenesisHash)
19201917
}
19211918
}
1919+
if ctx.IsSet(NetworkIdFlag.Name) {
1920+
// Typically it's best to automatically set the network ID to the chainID,
1921+
// by not passing the --networkid flag at all. Emit a warning when set
1922+
// explicitly in case overriding the network ID is not the user's intention.
1923+
id := ctx.Uint64(NetworkIdFlag.Name)
1924+
log.Warn("Setting network ID with command-line flag", "id", id)
1925+
cfg.NetworkId = id
1926+
}
19221927
// Set any dangling config values
19231928
if ctx.String(CryptoKZGFlag.Name) != "gokzg" && ctx.String(CryptoKZGFlag.Name) != "ckzg" {
19241929
Fatalf("--%s flag must be 'gokzg' or 'ckzg'", CryptoKZGFlag.Name)

0 commit comments

Comments
 (0)