Skip to content

Commit 407d9fa

Browse files
cmd/geth: add flag to set genesis (#32844)
This PR is an alternative to #32556. Instead of trying to be smart and reuse `geth init`, we can introduce a new flag `--genesis` that loads the `genesis.json` from file into the `Genesis` object in the same path that the other network flags currently work in. Question: is something like `--genesis` enough to start deprecating `geth init`? -- ```console $ geth --datadir data --hoodi .. INFO [10-06|22:37:11.202] - BPO2: @1762955544 .. $ geth --datadir data --genesis genesis.json .. INFO [10-06|22:37:27.988] - BPO2: @1862955544 .. ``` Pull the genesis [from the specs](https://raw.githubusercontent.com/eth-clients/hoodi/refs/heads/main/metadata/genesis.json) and modify one of the BPO timestamps to simulate a shadow fork. --------- Co-authored-by: rjl493456442 <[email protected]>
1 parent 0a8b820 commit 407d9fa

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

cmd/geth/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ var (
6666
utils.OverrideBPO1,
6767
utils.OverrideBPO2,
6868
utils.OverrideVerkle,
69+
utils.OverrideGenesisFlag,
6970
utils.EnablePersonal, // deprecated
7071
utils.TxPoolLocalsFlag,
7172
utils.TxPoolNoLocalsFlag,

cmd/utils/flags.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ var (
262262
Usage: "Manually specify the Verkle fork timestamp, overriding the bundled setting",
263263
Category: flags.EthCategory,
264264
}
265+
OverrideGenesisFlag = &cli.StringFlag{
266+
Name: "override.genesis",
267+
Usage: "Load genesis block and configuration from file at this path",
268+
Category: flags.EthCategory,
269+
}
265270
SyncModeFlag = &cli.StringFlag{
266271
Name: "syncmode",
267272
Usage: `Blockchain sync mode ("snap" or "full")`,
@@ -1605,7 +1610,7 @@ func setRequiredBlocks(ctx *cli.Context, cfg *ethconfig.Config) {
16051610
// SetEthConfig applies eth-related command line flags to the config.
16061611
func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
16071612
// Avoid conflicting network flags, don't allow network id override on preset networks
1608-
flags.CheckExclusive(ctx, MainnetFlag, DeveloperFlag, SepoliaFlag, HoleskyFlag, HoodiFlag, NetworkIdFlag)
1613+
flags.CheckExclusive(ctx, MainnetFlag, DeveloperFlag, SepoliaFlag, HoleskyFlag, HoodiFlag, NetworkIdFlag, OverrideGenesisFlag)
16091614
flags.CheckExclusive(ctx, DeveloperFlag, ExternalSignerFlag) // Can't use both ephemeral unlocked and external signer
16101615

16111616
// Set configurations from CLI flags
@@ -1891,6 +1896,18 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
18911896
if !ctx.IsSet(MinerGasPriceFlag.Name) {
18921897
cfg.Miner.GasPrice = big.NewInt(1)
18931898
}
1899+
case ctx.String(OverrideGenesisFlag.Name) != "":
1900+
f, err := os.Open(ctx.String(OverrideGenesisFlag.Name))
1901+
if err != nil {
1902+
Fatalf("Failed to read genesis file: %v", err)
1903+
}
1904+
defer f.Close()
1905+
1906+
genesis := new(core.Genesis)
1907+
if err := json.NewDecoder(f).Decode(genesis); err != nil {
1908+
Fatalf("Invalid genesis file: %v", err)
1909+
}
1910+
cfg.Genesis = genesis
18941911
default:
18951912
if cfg.NetworkId == 1 {
18961913
SetDNSDiscoveryDefaults(cfg, params.MainnetGenesisHash)

0 commit comments

Comments
 (0)